/* ==================== Fonts & Variables ==================== */
@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap');

:root {
  --primary: #0d9488;
  --primary-dark: #0f766e;
  --primary-light: #14b8a6;
  --primary-glow: rgba(13, 148, 136, 0.4);
  --accent: #f59e0b;
  --accent-glow: rgba(245, 158, 11, 0.4);
  --bg-dark: #042f2e;
  --bg-mid: #0a3d3b;
  --bg-card: rgba(255, 255, 255, 0.95);
  --text-dark: #1e293b;
  --text-light: #64748b;
  --tile-bg-linear: linear-gradient(145deg, #fef3c7, #fde68a);
  --tile-bg-green: linear-gradient(145deg, #d1fae5, #a7f3d0);
  --tile-bg-red: linear-gradient(145deg, #fecaca, #fca5a5);
  --tile-bg-blue: linear-gradient(145deg, #bfdbfe, #93c5fd);
  --tile-border: #b45309;
  --tile-shadow: rgba(0, 0, 0, 0.25);
  --success: #10b981;
  --danger: #ef4444;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Plus Jakarta Sans', sans-serif;
  background: linear-gradient(135deg, var(--bg-dark) 0%, var(--bg-mid) 50%, #064e4b 100%);
  min-height: 100vh;
  color: var(--text-dark);
  overflow-x: hidden;
}

body::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background:
    radial-gradient(circle at 20% 30%, rgba(13, 148, 136, 0.08) 0%, transparent 50%),
    radial-gradient(circle at 80% 70%, rgba(20, 184, 166, 0.06) 0%, transparent 50%);
  pointer-events: none;
  z-index: 0;
}

/* ==================== Game Header ==================== */
.game-header {
  text-align: center;
  padding: 2.5rem 1rem 1.5rem;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(13, 148, 136, 0.15);
  position: relative;
  z-index: 1;
}

.game-title {
  font-size: 2.4rem;
  font-weight: 800;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  text-shadow: 0 2px 20px rgba(13, 148, 136, 0.4);
  letter-spacing: -0.02em;
}

.title-icon {
  font-size: 2.6rem;
  filter: drop-shadow(0 2px 8px rgba(0, 0, 0, 0.3));
  animation: tileFloat 3s ease-in-out infinite;
}

@keyframes tileFloat {
  0%, 100% { transform: translateY(0) rotate(-3deg); }
  50% { transform: translateY(-6px) rotate(3deg); }
}

.game-subtitle {
  font-size: 1.1rem;
  color: rgba(255, 255, 255, 0.75);
  margin-top: 0.5rem;
  font-weight: 500;
}

/* ==================== Main Container ==================== */
.main-container {
  flex: 1;
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 2rem;
  padding: 2rem 1.5rem;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
  align-items: start;
  position: relative;
  z-index: 1;
}

/* ==================== Sidebar ==================== */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  position: sticky;
  top: 2rem;
}

.stats-section,
.controls-section,
.info-section {
  background: var(--bg-card);
  padding: 1.5rem;
  border-radius: 16px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.12), 0 1px 3px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(13, 148, 136, 0.08);
}

.section-title {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--primary);
  text-transform: uppercase;
  letter-spacing: 1.5px;
  margin-bottom: 1rem;
  text-align: center;
  position: relative;
  padding-bottom: 0.6rem;
}

.section-title::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 3px;
  background: linear-gradient(90deg, var(--primary), var(--primary-light));
  border-radius: 2px;
}

.stat-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  background: linear-gradient(135deg, rgba(13, 148, 136, 0.04), rgba(20, 184, 166, 0.06));
  border-radius: 10px;
  margin-bottom: 0.6rem;
  border: 1px solid rgba(13, 148, 136, 0.06);
  transition: all 0.3s ease;
}

.stat-item:hover {
  background: linear-gradient(135deg, rgba(13, 148, 136, 0.08), rgba(20, 184, 166, 0.1));
  transform: translateX(2px);
}

.stat-item:last-child {
  margin-bottom: 0;
}

.stat-item .stat-label {
  font-size: 0.9rem;
  color: var(--text-light);
  font-weight: 600;
}

.stat-item .stat-value {
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--primary);
  font-variant-numeric: tabular-nums;
}

/* Control Buttons in Sidebar */
.control-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.8rem 1rem;
  border: 2px solid transparent;
  border-radius: 12px;
  font-size: 0.92rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  font-family: 'Plus Jakarta Sans', sans-serif;
  letter-spacing: 0.02em;
}

.control-btn svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.hint-btn {
  background: linear-gradient(135deg, rgba(245, 158, 11, 0.1), rgba(251, 191, 36, 0.15));
  color: #d97706;
  border-color: rgba(245, 158, 11, 0.3);
}

.hint-btn:hover {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: white;
  box-shadow: 0 4px 20px var(--accent-glow);
  transform: translateY(-2px);
}

.shuffle-btn {
  background: linear-gradient(135deg, rgba(13, 148, 136, 0.1), rgba(20, 184, 166, 0.15));
  color: var(--primary-dark);
  border-color: rgba(13, 148, 136, 0.3);
}

.shuffle-btn:hover {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  box-shadow: 0 4px 20px var(--primary-glow);
  transform: translateY(-2px);
}

.restart-btn {
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.1), rgba(129, 140, 248, 0.15));
  color: #4f46e5;
  border-color: rgba(99, 102, 241, 0.3);
}

.restart-btn:hover {
  background: linear-gradient(135deg, #6366f1, #4f46e5);
  color: white;
  box-shadow: 0 4px 20px rgba(99, 102, 241, 0.4);
  transform: translateY(-2px);
}

.controls-section {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

/* Info List */
.info-list {
  list-style: none;
  padding: 0;
}

.info-list li {
  padding: 0.55rem 0;
  color: var(--text-light);
  font-size: 0.88rem;
  line-height: 1.5;
  position: relative;
  padding-left: 1.75rem;
  transition: color 0.2s ease;
}

.info-list li:hover {
  color: var(--text-dark);
}

.info-list li::before {
  content: '◆';
  position: absolute;
  left: 0;
  color: var(--primary);
  font-size: 0.65rem;
  top: 0.65rem;
}

/* ==================== Game Main Area ==================== */
.game-main {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.25rem;
}

/* Status Bar */
.status-bar {
  display: flex;
  gap: 2rem;
  background: var(--bg-card);
  padding: 1rem 2.5rem;
  border-radius: 14px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(13, 148, 136, 0.08);
}

.status-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-dark);
}

.status-icon {
  font-size: 1.2rem;
}

#timerDisplay {
  color: var(--primary);
  font-variant-numeric: tabular-nums;
  font-size: 1.25rem;
}

#tilesLeft {
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  font-size: 1.25rem;
}

/* Board Wrapper */
.board-wrapper {
  position: relative;
  background: var(--bg-card);
  padding: 1.25rem;
  border-radius: 20px;
  box-shadow:
    0 10px 50px rgba(0, 0, 0, 0.15),
    0 1px 5px rgba(0, 0, 0, 0.08),
    inset 0 1px 0 rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(13, 148, 136, 0.1);
}

#lineCanvas {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
  z-index: 10;
  border-radius: 20px;
}

/* Game Board */
.game-board {
  display: grid;
  gap: 4px;
  padding: 8px;
  background: linear-gradient(145deg, #0c3d3a, #092e2c);
  border-radius: 14px;
  border: 2px solid rgba(13, 148, 136, 0.2);
  box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.3);
  position: relative;
}

/* ==================== Mahjong Tile (3D Effect) ==================== */
.tile {
  width: 52px;
  height: 66px;
  perspective: 600px;
  cursor: pointer;
  position: relative;
}

.tile-inner {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  border-radius: 8px;
}

.tile-face {
  position: absolute;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.6rem;
  font-weight: 700;
  user-select: none;
  box-shadow:
    0 4px 12px var(--tile-shadow),
    0 1px 3px rgba(0, 0, 0, 0.15),
    inset 0 1px 0 rgba(255, 255, 255, 0.6),
    inset 0 -2px 4px rgba(0, 0, 0, 0.08);
  border: 2.5px solid var(--tile-border);
  transition: all 0.25s ease;
  overflow: hidden;
}

.tile-face::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.35) 0%, transparent 50%, rgba(0, 0, 0, 0.03) 100%);
  border-radius: 6px;
  pointer-events: none;
}

.tile-face::after {
  content: '';
  position: absolute;
  bottom: 3px;
  left: 50%;
  transform: translateX(-50%);
  width: 70%;
  height: 3px;
  background: rgba(180, 83, 9, 0.15);
  border-radius: 2px;
  filter: blur(1px);
}

/* Tile Types by Suit */
.tile-suit-wan .tile-face {
  background: var(--tile-bg-linear);
  color: #92400e;
}

.tile-suit-tiao .tile-face {
  background: var(--tile-bg-green);
  color: #065f46;
}

.tile-suit-tong .tile-face {
  background: var(--tile-bg-red);
  color: #991b1b;
}

.tile-suit-honor .tile-face {
  background: var(--tile-bg-blue);
  color: #1e40af;
}

.tile-suit-special .tile-face {
  background: linear-gradient(145deg, #fce7f3, #fbcfe8);
  color: #9d174d;
}

.tile-suit-flower .tile-face {
  background: linear-gradient(145deg, #e0e7ff, #c7d2fe);
  color: #3730a3;
}

/* 3D Edge Effect */
.tile-inner::before {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 8px;
  background: linear-gradient(
    to bottom,
    rgba(255, 255, 255, 0.15) 0%,
    transparent 15%,
    transparent 85%,
    rgba(0, 0, 0, 0.1) 100%
  );
  z-index: 2;
  pointer-events: none;
}

/* Tile Hover */
.tile:not(.tile-matched):not(.tile-empty):hover .tile-inner {
  transform: translateZ(8px) scale(1.06) rotateX(3deg);
}

.tile:not(.tile-matched):not(.tile-empty):hover .tile-face {
  box-shadow:
    0 8px 25px rgba(0, 0, 0, 0.3),
    0 2px 6px rgba(0, 0, 0, 0.15),
    0 0 20px var(--primary-glow);
  border-color: var(--primary-light);
}

/* Selected Tile */
.tile.selected .tile-inner {
  transform: translateZ(14px) scale(1.1) rotateX(2deg);
  animation: selectedPulse 0.8s ease-in-out infinite alternate;
}

.tile.selected .tile-face {
  border-color: var(--accent);
  box-shadow:
    0 8px 28px rgba(0, 0, 0, 0.3),
    0 0 0 3px rgba(245, 158, 11, 0.35),
    0 0 25px var(--accent-glow);
}

@keyframes selectedPulse {
  0% {
    filter: brightness(1);
  }
  100% {
    filter: brightness(1.15);
  }
}

/* Hint Highlight */
.tile.hint .tile-inner {
  animation: hintGlow 0.6s ease-in-out infinite alternate;
}

.tile.hint .tile-face {
  border-color: var(--success);
  box-shadow:
    0 4px 16px rgba(0, 0, 0, 0.2),
    0 0 0 3px rgba(16, 185, 129, 0.35),
    0 0 20px rgba(16, 185, 129, 0.3);
}

@keyframes hintGlow {
  0% {
    filter: brightness(1) drop-shadow(0 0 4px rgba(16, 185, 129, 0.3));
  }
  100% {
    filter: brightness(1.2) drop-shadow(0 0 12px rgba(16, 185, 129, 0.6));
  }
}

/* Matched / Eliminated Animation */
.tile.matched .tile-inner {
  animation: eliminateTile 0.5s ease forwards;
  pointer-events: none;
}

@keyframes eliminateTile {
  0% {
    transform: scale(1) rotateY(0);
    opacity: 1;
  }
  40% {
    transform: scale(1.2) rotateY(10deg);
    opacity: 1;
  }
  100% {
    transform: scale(0) rotateY(180deg) translateZ(-30px);
    opacity: 0;
  }
}

/* Empty Tile Slot */
.tile-empty .tile-face {
  background: rgba(13, 148, 136, 0.05);
  border: 2px dashed rgba(13, 148, 136, 0.15);
  box-shadow: none;
  cursor: default;
}

.tile-empty .tile-face::before,
.tile-empty .tile-face::after {
  display: none;
}

/* ==================== Game Over Modal ==================== */
.game-over-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(4, 47, 46, 0.92);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease;
}

.game-over-modal.active {
  opacity: 1;
  pointer-events: all;
}

.modal-content {
  background: linear-gradient(160deg, #ffffff 0%, #f8fafc 100%);
  padding: 3rem 2.5rem;
  border-radius: 24px;
  text-align: center;
  box-shadow:
    0 25px 80px rgba(0, 0, 0, 0.4),
    0 0 0 1px rgba(13, 148, 136, 0.15);
  transform: scale(0.8) translateY(20px);
  transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
  max-width: 460px;
  width: 90%;
}

.game-over-modal.active .modal-content {
  transform: scale(1) translateY(0);
  animation: modalBounce 0.6s ease;
}

@keyframes modalBounce {
  0% { transform: scale(0.4) translateY(30px) rotate(-3deg); }
  50% { transform: scale(1.04) translateY(-5px) rotate(1.5deg); }
  75% { transform: scale(0.98) translateY(2px); }
  100% { transform: scale(1) translateY(0) rotate(0); }
}

.modal-icon {
  font-size: 5rem;
  margin-bottom: 0.75rem;
  animation: iconBounce 1s ease-in-out infinite;
  display: inline-block;
}

@keyframes iconBounce {
  0%, 100% { transform: translateY(0) scale(1); }
  50% { transform: translateY(-10px) scale(1.1); }
}

.modal-title {
  font-size: 2.2rem;
  font-weight: 800;
  margin: 0 0 0.75rem;
  background: linear-gradient(135deg, var(--primary), var(--primary-light));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.modal-message {
  font-size: 1.1rem;
  color: var(--text-light);
  margin: 0 0 1.5rem;
  font-weight: 500;
}

.modal-stats {
  display: flex;
  justify-content: center;
  gap: 2rem;
  margin-bottom: 2rem;
}

.modal-stat {
  text-align: center;
}

.modal-stat-label {
  display: block;
  font-size: 0.82rem;
  color: var(--text-light);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 0.35rem;
}

.modal-stat-value {
  font-size: 1.8rem;
  font-weight: 800;
  color: var(--primary);
}

.modal-buttons {
  display: flex;
  justify-content: center;
}

.modal-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 1rem 2.5rem;
  border: none;
  border-radius: 14px;
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  font-family: 'Plus Jakarta Sans', sans-serif;
  letter-spacing: 0.02em;
}

.play-again-btn {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  box-shadow: 0 4px 20px var(--primary-glow);
}

.play-again-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 30px var(--primary-glow);
  background: linear-gradient(135deg, var(--primary-light), var(--primary));
}

.modal-btn svg {
  width: 20px;
  height: 20px;
}

/* ==================== Responsive Design ==================== */
@media screen and (max-width: 1100px) {
  .main-container {
    grid-template-columns: 240px 1fr;
    gap: 1.5rem;
  }
}

@media screen and (max-width: 960px) {
  .main-container {
    grid-template-columns: 1fr;
    gap: 1.25rem;
  }

  .sidebar {
    position: static;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 1rem;
  }

  .stats-section,
  .controls-section,
  .info-section {
    flex: 1;
    min-width: 220px;
  }
}

@media screen and (max-width: 768px) {
  .game-title {
    font-size: 1.8rem;
  }

  .title-icon {
    font-size: 2rem;
  }

  .game-header {
    padding: 2rem 1rem 1.25rem;
  }

  .sidebar {
    flex-direction: column;
  }

  .status-bar {
    padding: 0.75rem 1.5rem;
    gap: 1.25rem;
  }

  .tile {
    width: 44px;
    height: 56px;
  }

  .tile-face {
    font-size: 1.3rem;
    border-width: 2px;
    border-radius: 6px;
  }
}

@media screen and (max-width: 600px) {
  .game-title {
    font-size: 1.5rem;
    flex-direction: column;
    gap: 0.35rem;
  }

  .game-subtitle {
    font-size: 0.95rem;
  }

  .board-wrapper {
    padding: 0.75rem;
    border-radius: 14px;
  }

  .game-board {
    padding: 5px;
    gap: 3px;
  }

  .tile {
    width: 38px;
    height: 48px;
  }

  .tile-face {
    font-size: 1.1rem;
    border-radius: 5px;
    border-width: 1.5px;
  }

  .status-bar {
    flex-direction: column;
    gap: 0.5rem;
    padding: 0.75rem 1.25rem;
    align-items: center;
  }

  .modal-content {
    padding: 2rem 1.5rem;
  }

  .modal-title {
    font-size: 1.75rem;
  }

  .modal-icon {
    font-size: 3.5rem;
  }

  .modal-stats {
    flex-direction: column;
    gap: 1rem;
  }
}

@media screen and (max-width: 420px) {
  .main-container {
    padding: 1rem 0.75rem;
  }

  .tile {
    width: 33px;
    height: 42px;
  }

  .tile-face {
    font-size: 0.95rem;
  }

  .section-title {
    font-size: 0.78rem;
  }

  .control-btn {
    padding: 0.65rem 0.75rem;
    font-size: 0.84rem;
  }
}