:root {
  --bg-deep: #1a1208;
  --bg-mid: #2c2114;
  --wood: #5c3d1e;
  --wood-light: #8b5a2b;
  --jade: #2d6a4f;
  --jade-light: #40916c;
  --cream: #f5ecd7;
  --tile-face: #f7f0e0;
  --tile-edge: #c4a882;
  --text: #f5ecd7;
  --accent: #d4a017;
  --danger: #c44536;
  --shadow: rgba(0, 0, 0, 0.45);
  --tile-w: 44px;
  --tile-h: 56px;
  --z-lift: 6px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Outfit", sans-serif;
  min-height: 100vh;
  color: var(--text);
  background:
    radial-gradient(ellipse at 30% 20%, rgba(64, 145, 108, 0.18), transparent 50%),
    radial-gradient(ellipse at 70% 80%, rgba(212, 160, 23, 0.12), transparent 45%),
    linear-gradient(160deg, var(--bg-mid) 0%, var(--bg-deep) 55%, #0f0a05 100%);
  -webkit-tap-highlight-color: transparent;
}

.wrapper {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px;
}

.hidden {
  display: none !important;
}

.level-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 80vh;
  text-align: center;
  max-width: 420px;
  width: 100%;
}

.level-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}

.level-header h1,
.header h1 {
  margin: 0;
  font-size: clamp(1.3rem, 4vw, 2rem);
  font-weight: 700;
  letter-spacing: 0.02em;
}

.level-screen > p {
  margin: 0 0 24px;
  opacity: 0.9;
  line-height: 1.4;
}

.level-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
}

.level-btn {
  min-height: 44px;
  padding: 16px 20px;
  border: 2px solid rgba(212, 160, 23, 0.45);
  border-radius: 12px;
  background: linear-gradient(145deg, rgba(92, 61, 30, 0.85), rgba(44, 33, 20, 0.9));
  color: inherit;
  font-family: inherit;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.12s ease;
}

.level-btn:hover {
  background: linear-gradient(145deg, rgba(139, 90, 43, 0.9), rgba(92, 61, 30, 0.95));
  border-color: var(--accent);
  transform: translateY(-1px);
}

.level-btn:active {
  transform: translateY(0);
}

.level-name {
  font-weight: 700;
  font-size: 1.15rem;
}

.level-desc {
  font-size: 0.82rem;
  opacity: 0.88;
  line-height: 1.35;
}

.help-btn {
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  border-radius: 50%;
  border: 2px solid rgba(245, 236, 215, 0.4);
  background: rgba(45, 106, 79, 0.55);
  color: inherit;
  font-weight: 700;
  font-family: inherit;
  font-size: 1.1rem;
  cursor: pointer;
}

.help-btn:hover {
  background: rgba(64, 145, 108, 0.7);
}

.game-screen {
  width: 100%;
  max-width: 960px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
  position: relative;
}

.header {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
}

.header-top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.level-badge {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 4px 12px;
  border-radius: 8px;
  background: rgba(45, 106, 79, 0.65);
  border: 1px solid rgba(64, 145, 108, 0.5);
  font-size: 0.85rem;
  font-weight: 600;
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.hud {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  padding: 8px 12px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(245, 236, 215, 0.15);
  font-weight: 600;
  font-size: 0.92rem;
  white-space: nowrap;
}

.btn {
  min-height: 44px;
  min-width: 44px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 2px solid rgba(212, 160, 23, 0.5);
  background: linear-gradient(180deg, var(--wood-light), var(--wood));
  color: var(--cream);
  font-family: inherit;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: filter 0.15s ease, opacity 0.15s ease;
}

.btn:hover:not(:disabled) {
  filter: brightness(1.08);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-ghost {
  background: transparent;
  border-color: rgba(245, 236, 215, 0.35);
}

.btn-large {
  width: 100%;
  max-width: 260px;
  font-size: 1rem;
}

.warn-text {
  margin: 0;
  padding: 8px 12px;
  border-radius: 8px;
  background: rgba(196, 69, 54, 0.25);
  border: 1px solid rgba(196, 69, 54, 0.5);
  color: #ffc9c0;
  font-size: 0.9rem;
  text-align: center;
}

.board-scroll {
  width: 100%;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: 16px;
  border: 3px solid rgba(92, 61, 30, 0.85);
  background:
    repeating-linear-gradient(
      90deg,
      rgba(45, 106, 79, 0.08) 0 2px,
      transparent 2px 28px
    ),
    linear-gradient(145deg, #3d6b52 0%, #2d5a45 40%, #1e3d30 100%);
  box-shadow:
    inset 0 0 40px rgba(0, 0, 0, 0.25),
    0 8px 24px var(--shadow);
  padding: 16px;
  min-height: 280px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.board {
  position: relative;
  transform-origin: top center;
  margin: 0 auto;
}

.tile {
  position: absolute;
  width: var(--tile-w);
  height: var(--tile-h);
  border: none;
  padding: 0;
  margin: 0;
  cursor: pointer;
  font-family: inherit;
  border-radius: 6px;
  background: linear-gradient(145deg, #fff8ea 0%, var(--tile-face) 55%, #e8dcc4 100%);
  color: #2a1f12;
  box-shadow:
    2px 2px 0 var(--tile-edge),
    3px 3px 0 #9a7b55,
    4px 4px 8px var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  user-select: none;
  transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.tile::before {
  content: "";
  position: absolute;
  inset: 3px;
  border-radius: 4px;
  border: 1px solid rgba(92, 61, 30, 0.18);
  pointer-events: none;
}

.tile .face-label {
  font-weight: 700;
  font-size: 0.95rem;
  line-height: 1;
  z-index: 1;
}

.tile .face-sub {
  font-size: 0.65rem;
  font-weight: 600;
  opacity: 0.75;
  z-index: 1;
}

.tile.suit-circles {
  color: #1d4ed8;
}

.tile.suit-bamboo {
  color: #15803d;
}

.tile.suit-characters {
  color: #b91c1c;
}

.tile.suit-winds {
  color: #1e3a5f;
}

.tile.suit-dragons.dragon-r {
  color: #b91c1c;
}

.tile.suit-dragons.dragon-g {
  color: #15803d;
}

.tile.suit-dragons.dragon-w {
  color: #374151;
}

.tile:not(.free) {
  cursor: default;
  filter: brightness(0.92);
}

.tile.free:hover {
  filter: brightness(1.05);
}

.tile.selected {
  outline: 3px solid var(--accent);
  outline-offset: 1px;
  transform: translateY(-3px);
  z-index: 2000 !important;
  box-shadow:
    2px 2px 0 var(--tile-edge),
    3px 3px 0 #9a7b55,
    0 0 0 2px rgba(212, 160, 23, 0.4),
    6px 10px 16px var(--shadow);
}

.tile.hint {
  outline: 3px solid var(--jade-light);
  outline-offset: 1px;
  animation: hintPulse 0.8s ease-in-out 3;
}

.tile.removing {
  pointer-events: none;
  animation: tileOut 0.28s ease forwards;
}

@keyframes hintPulse {
  0%,
  100% {
    filter: brightness(1);
  }
  50% {
    filter: brightness(1.2);
  }
}

@keyframes tileOut {
  to {
    opacity: 0;
    transform: scale(0.6);
  }
}

.result-overlay,
.help-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 6, 2, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3000;
  padding: 16px;
}

.result-overlay {
  position: absolute;
  border-radius: 16px;
}

.game-screen .result-overlay {
  position: absolute;
  inset: 0;
}

.result-box,
.help-modal {
  background: linear-gradient(160deg, #3a2a18, #24180e);
  border: 2px solid rgba(212, 160, 23, 0.45);
  border-radius: 16px;
  padding: 24px;
  max-width: 400px;
  width: 100%;
  text-align: center;
  box-shadow: 0 16px 40px var(--shadow);
}

.result-box h2,
.help-header h2 {
  margin: 0 0 10px;
  font-size: 1.4rem;
}

.result-box p {
  margin: 0 0 20px;
  opacity: 0.92;
  line-height: 1.45;
}

.result-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 16px;
}

.result-actions .btn {
  width: 100%;
  margin: 0;
}

.help-modal {
  text-align: left;
  max-height: 85vh;
  overflow: auto;
}

.help-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.help-close {
  width: 44px;
  height: 44px;
  min-width: 44px;
  border: none;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.08);
  color: inherit;
  font-size: 1.5rem;
  cursor: pointer;
  font-family: inherit;
}

.help-body p {
  margin: 0 0 12px;
  line-height: 1.5;
  opacity: 0.95;
}

.help-body ul {
  margin: 0;
  padding-left: 1.2rem;
  line-height: 1.55;
}

.help-body li {
  margin-bottom: 6px;
}

@media (max-width: 600px) {
  .wrapper {
    padding: 8px;
  }

  .header-actions {
    gap: 6px;
  }

  .btn {
    padding: 10px 12px;
    font-size: 0.82rem;
  }

  .board-scroll {
    padding: 10px;
  }
}
