:root {
  --bg: #e8dfcf;
  --table: #d4c5ad;
  --table-deep: #c1ae91;
  --ink: #293139;
  --muted: #746f67;
  --line: rgba(73, 65, 54, .18);
  --tile-face: #fffaf0;
  --tile-edge: #e4d1aa;
  --tile-side: #c9a66d;
  --accent: #4d8f78;
  --accent-dark: #326953;
  --gold: #c69a42;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  color: var(--ink);
  background:
    radial-gradient(circle at 20% 12%, rgba(255,255,255,.62), transparent 27%),
    linear-gradient(145deg, #eee7db, #ddd0bc 55%, #d1c0a5);
  font-family: ui-rounded, "Avenir Next", "Trebuchet MS", Arial, sans-serif;
}
button { font: inherit; }

.app-shell {
  width: min(1180px, calc(100% - 28px));
  margin: 0 auto;
  padding: 24px 0 36px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark { color: var(--gold); font-size: 1.7rem; }
.brand h1 { margin: 0; font-size: clamp(1.8rem, 4vw, 3.2rem); letter-spacing: -.045em; line-height: 1; }
.brand p { margin: 5px 0 0; color: var(--muted); font-size: .92rem; }

.stats { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.stat {
  min-width: 92px;
  padding: 9px 13px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: rgba(255,255,255,.45);
  text-align: center;
}
.stat span { display: block; color: var(--muted); font-size: .7rem; font-weight: 800; text-transform: uppercase; letter-spacing: .08em; }
.stat strong { display: block; margin-top: 2px; font-size: 1.2rem; }
.stat.highest { border-color: rgba(198,154,66,.42); background: rgba(255,249,229,.7); }

.game-card {
  background: rgba(255,255,255,.3);
  border: 1px solid rgba(90,73,49,.15);
  border-radius: 24px;
  padding: 14px;
  box-shadow: 0 18px 50px rgba(81,65,43,.13);
}
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-bottom: 10px; }
.control {
  min-height: 42px;
  padding: 0 15px;
  border: 1px solid rgba(72,64,53,.2);
  border-radius: 11px;
  background: rgba(255,255,255,.68);
  color: var(--ink);
  cursor: pointer;
  font-weight: 800;
}
.control.primary { background: var(--accent); color: white; border-color: var(--accent-dark); }
.control:disabled { opacity: .38; cursor: not-allowed; }
.control:not(:disabled):hover { transform: translateY(-1px); }
.control:focus-visible, .tile:focus-visible, .overlay-action:focus-visible { outline: 3px solid #2a86a4; outline-offset: 3px; }

.how-panel {
  max-width: 760px;
  margin: 0 auto 12px;
  padding: 11px 15px;
  border-radius: 13px;
  background: rgba(255,255,255,.62);
  border: 1px solid var(--line);
  color: #565048;
  font-size: .9rem;
  line-height: 1.45;
}
.how-panel p { margin: 4px 0; }

.board-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 1.48 / 1;
  min-height: 520px;
  overflow: hidden;
  border-radius: 20px;
  background:
    radial-gradient(ellipse at 50% 42%, rgba(255,255,255,.28), transparent 45%),
    repeating-linear-gradient(8deg, rgba(77,60,38,.022) 0 2px, transparent 2px 8px),
    linear-gradient(145deg, var(--table), var(--table-deep));
  border: 1px solid rgba(93,71,43,.18);
  box-shadow: inset 0 0 55px rgba(92,65,31,.10);
}
.board {
  position: absolute;
  inset: 4% 4% 6%;
}

.tile {
  position: absolute;
  border: 1px solid var(--tile-edge);
  border-radius: 10px;
  background: linear-gradient(145deg, #fffef9 0%, var(--tile-face) 70%, #f3e4c8 100%);
  box-shadow:
    0 5px 0 var(--tile-side),
    0 8px 10px rgba(69,48,23,.20),
    inset 0 1px 0 rgba(255,255,255,.95),
    inset 0 -2px 5px rgba(157,121,68,.12);
  display: grid;
  place-items: center;
  padding: 0;
  color: #2c3d45;
  font-family: ui-rounded, "Avenir Next", "Trebuchet MS", Arial, sans-serif;
  font-weight: 900;
  line-height: 1;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform .13s ease, filter .13s ease, box-shadow .13s ease, opacity .13s ease;
}
.tile .number {
  font-size: clamp(18px, 2.65vw, 36px);
  letter-spacing: -.05em;
  text-shadow: 0 1px 0 rgba(255,255,255,.8);
}
.tile.playable { cursor: pointer; filter: brightness(1.035) saturate(1.04); }
.tile.playable:hover { transform: translateY(-2px); }
.tile.side-blocked { cursor: not-allowed; filter: brightness(.92) saturate(.86); }
.tile.layer-locked { cursor: not-allowed; filter: brightness(.90) saturate(.78); opacity: .82; }
.tile.layer-locked .number, .tile.side-blocked .number { opacity: .90; }
.tile.selected {
  transform: translateY(-7px) scale(1.035);
  box-shadow:
    0 8px 0 #b8894d,
    0 0 0 3px rgba(73,143,120,.7),
    0 14px 18px rgba(69,48,23,.25);
  z-index: 9999 !important;
}
.tile.hint { animation: hintPulse .75s ease-in-out 2; }
.tile.invalid { animation: shake .26s linear; }
.tile.merged { animation: mergePop .32s ease-out; }
@keyframes hintPulse { 50% { box-shadow: 0 7px 0 #b8894d, 0 0 0 4px rgba(73,143,120,.62), 0 12px 18px rgba(69,48,23,.24); } }
@keyframes shake { 25% { transform: translateX(-4px); } 50% { transform: translateX(4px); } 75% { transform: translateX(-2px); } }
@keyframes mergePop { 45% { transform: translateY(-5px) scale(1.10); } }

.overlay-card {
  position: absolute;
  inset: 0;
  z-index: 9000;
  display: grid;
  place-items: center;
  background: rgba(231,222,207,.52);
  backdrop-filter: blur(3px);
}
.overlay-card[hidden] { display: none; }
.overlay-inner {
  width: min(390px, calc(100% - 34px));
  padding: 25px;
  border-radius: 20px;
  background: rgba(255,253,247,.94);
  border: 1px solid rgba(89,73,51,.2);
  box-shadow: 0 20px 44px rgba(75,57,35,.22);
  text-align: center;
}
.overlay-inner h2 { margin: 8px 0; font-size: 1.65rem; }
.overlay-inner p { margin: 7px 0 18px; color: var(--muted); line-height: 1.45; }
.overlay-action {
  min-height: 46px;
  border: 0;
  border-radius: 12px;
  padding: 0 22px;
  background: var(--accent);
  color: white;
  cursor: pointer;
  font-weight: 900;
}
.mini-tile {
  display: inline-grid;
  place-items: center;
  width: 56px;
  height: 68px;
  border-radius: 9px;
  background: var(--tile-face);
  color: #4d8f78;
  font-size: 1.8rem;
  font-weight: 900;
  border: 1px solid var(--tile-edge);
  box-shadow: 0 5px 0 var(--tile-side), 0 9px 13px rgba(69,48,23,.18);
}
.board-message {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  z-index: 8500;
  max-width: calc(100% - 28px);
  padding: 7px 12px;
  border-radius: 999px;
  background: rgba(255,252,245,.9);
  border: 1px solid var(--line);
  color: #5e584f;
  font-size: .82rem;
  font-weight: 800;
  text-align: center;
  pointer-events: none;
}
.board-message:empty { display: none; }

.legend {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  flex-wrap: wrap;
  padding: 10px 4px 0;
  color: var(--muted);
  font-size: .78rem;
  font-weight: 700;
}
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 5px; }
.free-dot { background: var(--accent); }
.locked-dot { background: #a79d8d; }

.leaderboard-block {
  margin: 14px auto 0;
  width: min(760px, 100%);
  padding: 12px 14px;
  border-top: 1px solid rgba(84,67,44,.15);
}
.leaderboard-block h2 { margin: 0 0 8px; font-size: .9rem; color: var(--muted); text-align: center; text-transform: uppercase; letter-spacing: .08em; }
#mis-leaderboard { min-height: 68px; }

@media (max-width: 760px) {
  .app-shell { width: min(100% - 16px, 1180px); padding-top: 14px; }
  .topbar { align-items: flex-start; flex-direction: column; }
  .stats { width: 100%; justify-content: stretch; }
  .stat { flex: 1; min-width: 0; }
  .board-wrap { min-height: 430px; aspect-ratio: 1.1 / 1; }
  .board { inset: 5% 2% 8%; }
  .tile .number { font-size: clamp(16px, 5vw, 29px); }
}
@media (max-width: 450px) {
  .brand h1 { font-size: 2rem; }
  .brand p { font-size: .78rem; }
  .toolbar { gap: 6px; }
  .control { min-height: 40px; padding: 0 11px; font-size: .82rem; }
  .board-wrap { min-height: 365px; border-radius: 15px; }
  .tile { border-radius: 7px; box-shadow: 0 4px 0 var(--tile-side), 0 7px 8px rgba(69,48,23,.18); }
  .legend { gap: 10px; }
}
