/* ============================================================
   Mahjong Tiles - stylesheet
   Vanilla CSS, no build step, no external fonts/images.
   ============================================================ */

:root {
  --felt-1: #0c5c46;
  --felt-2: #0a4a39;
  --ink: #16241d;
  --cream: #f7f0dc;
  --gold: #d9a441;
  --gold-bright: #f3c25f;
  --danger: #d1453b;
  --panel: #0e3c30;
  --panel-border: #1c6b52;
  --radius: 14px;
  --tile-w: 84px;
  --tile-h: 112px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: radial-gradient(ellipse at top, #124a3a 0%, #072e24 60%, #051f19 100%);
  color: var(--cream);
  font-family: "Segoe UI", Tahoma, Verdana, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
  /* A real, bounded viewport height (not just min-height) is required so
     main/.board-viewport below can resolve a definite remaining height for
     the JS auto-scale to fit the board into, instead of the whole page
     growing taller than the screen when a board is tall. 100dvh (where
     supported) accounts for mobile browser chrome; 100vh is the fallback. */
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ---------------- Header ---------------- */
.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: linear-gradient(180deg, #0a3527, #0a3527cc);
  border-bottom: 1px solid #114c3a;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-tile {
  font-size: 34px;
  line-height: 1;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,0.4));
}

.brand-text h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: 0.3px;
  color: var(--cream);
}

.tagline {
  margin: 2px 0 0;
  font-size: 12px;
  color: #b9d8c9;
}

.icon-btn {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  color: var(--cream);
  font-size: 18px;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:hover { background: #114f3d; }
.icon-btn.muted { opacity: 0.5; }

/* ---------------- Main layout ---------------- */
main {
  flex: 1;
  /* Flex items default to min-height:auto (content-based), which stops
     them shrinking below their content's natural size even when the flex
     container has a definite, smaller height. Without this, a tall board
     forces `main` (and the page) taller than the viewport instead of
     handing .board-viewport a bounded height to auto-scale into. */
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 14px 16px 8px;
  gap: 10px;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}

.mode-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}

.seg-group {
  display: inline-flex;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}

.seg-btn {
  border: none;
  background: transparent;
  color: #cfe9dc;
  padding: 7px 16px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}
.seg-btn:hover { color: #fff; }
.seg-btn.active {
  background: var(--gold);
  color: #2a1c05;
  font-weight: 600;
}

/* ---------------- HUD ---------------- */
.hud {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 10px 18px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 10px 16px;
}

.hud-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 64px;
}
.hud-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #a9d2c0;
}
.hud-value {
  font-size: 18px;
  font-weight: 700;
  color: var(--cream);
  font-variant-numeric: tabular-nums;
}

.hud-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-left: auto;
}

.btn {
  background: #12513f;
  border: 1px solid #1f7457;
  color: var(--cream);
  padding: 9px 14px;
  border-radius: 10px;
  font-size: 13px;
  cursor: pointer;
  font-weight: 600;
}
.btn:hover { background: #166049; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-primary {
  background: var(--gold);
  border-color: var(--gold);
  color: #2a1c05;
}
.btn-primary:hover { background: var(--gold-bright); }
.btn-small { padding: 6px 10px; font-size: 12px; }

/* ---------------- Banner ---------------- */
.banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: #5b2a1f;
  border: 1px solid #a5432f;
  color: #ffe6d9;
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 13px;
  text-align: center;
}
.banner.hidden { display: none; }

/* ---------------- Board ---------------- */
.board-viewport {
  flex: 1;
  min-height: 300px;
  padding: 10px 0 18px;
  overflow: auto;
  /* Positioning context for .board, which centers itself via top/left 50%
     + a JS-driven translate(-50%,-50%)+scale() transform (see app.js).
     Flexbox align-items/justify-content is deliberately NOT used here:
     transform:scale() never changes an element's layout box, so centering
     a scaled-down board with flex align-items centers its unscaled box
     instead of its visible (scaled) size, shoving it off-screen whenever
     the board is scaled down. The translate(-50%,-50%) trick centers
     correctly regardless of scale because the percentage translate and the
     scale share the same transform-origin (the box's own center). */
  position: relative;
}

.board {
  position: absolute;
  top: 50%;
  left: 50%;
  /* Width/height are set directly from JS (state.boardSize); the visible
     size is controlled purely by the JS-computed transform (see
     applyBoardScale in app.js), which also handles centering. */
}

.tile {
  --layer: 0;
  position: absolute;
  width: var(--tile-w);
  height: var(--tile-h);
  border-radius: 14px;
  background: linear-gradient(160deg, #ffffff 0%, #fbf6e8 45%, #eee0b8 100%);
  border: 1.5px solid #a98f57;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--tile-w) * 0.52);
  line-height: 1;
  color: #12201a;
  text-shadow: 0 1px 0 rgba(255,255,255,0.6);
  cursor: pointer;
  user-select: none;
  /* Layered box-shadow builds a chunky 3D "riser" under the tile face
     (short stacked opaque steps) plus a soft ambient shadow that grows
     with --layer, so tiles sitting on higher solitaire layers visibly
     cast a bigger, darker shadow onto whatever is beneath them. */
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,0.9),
    inset 0 -4px 8px rgba(110,86,36,0.16),
    0 2px 0 #d9c78e,
    0 4px 0 #c7b176,
    0 6px 0 #b39c60,
    0 8px 12px rgba(0,0,0,0.35),
    0 calc(8px + var(--layer) * 5px) calc(16px + var(--layer) * 7px) rgba(15,10,2,0.45);
  transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.15s ease, opacity 0.25s ease;
}

/* Painted symbol art: fills ~88% of the ivory face (large, not a tiny glyph),
   centered by the tile's own flexbox. The art is a transparent 3:4 PNG whose
   motif already sits at ~95% of its own frame, so 90% here reads ~85% of the
   face - large and clean, no "tile-on-a-tile" look. */
.tile-sym {
  width: 90%;
  height: 90%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
  filter: drop-shadow(0 1px 1px rgba(60,40,10,0.16));
}

.tile.free {
  border-color: #cdb474;
}

.tile.blocked {
  filter: brightness(0.6) saturate(0.6);
  cursor: not-allowed;
}

.tile.free:hover {
  transform: translateY(-4px);
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,0.95),
    inset 0 -4px 8px rgba(110,86,36,0.16),
    0 2px 0 #d9c78e,
    0 4px 0 #c7b176,
    0 6px 0 #b39c60,
    0 10px 16px rgba(0,0,0,0.4),
    0 calc(12px + var(--layer) * 5px) calc(20px + var(--layer) * 7px) rgba(15,10,2,0.5);
}

.tile.selected {
  transform: translateY(-10px);
  border-color: var(--gold-bright);
  box-shadow:
    0 0 0 4px rgba(243,194,95,0.9),
    0 2px 0 #d9c78e,
    0 4px 0 #c7b176,
    0 6px 0 #b39c60,
    0 20px 26px rgba(0,0,0,0.55);
}

.tile.hint {
  animation: hintPulse 0.9s ease-in-out infinite;
}

.tile.shake {
  animation: shake 0.32s ease;
}

.tile.matched {
  animation: matchPop 0.32s ease forwards;
  pointer-events: none;
}

@keyframes hintPulse {
  0%, 100% { box-shadow: 0 0 0 3px rgba(243,194,95,0.15), 0 8px 12px rgba(0,0,0,0.4); }
  50% { box-shadow: 0 0 0 6px rgba(243,194,95,0.9), 0 8px 14px rgba(0,0,0,0.4); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-5px); }
  40% { transform: translateX(5px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

@keyframes matchPop {
  0% { transform: scale(1) translateY(-6px); opacity: 1; }
  50% { transform: scale(1.18) translateY(-10px); opacity: 1; }
  100% { transform: scale(0.2) translateY(-10px); opacity: 0; }
}

/* Connect-mode grid cells share .tile styling but sit on a flat plane */
.connect-cell {
  position: absolute;
}

#connectPathOverlay {
  position: absolute;
  top: 0; left: 0;
  pointer-events: none;
}
#connectPathOverlay path {
  fill: none;
  stroke: var(--gold-bright);
  stroke-width: 4;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.95;
  filter: drop-shadow(0 0 4px rgba(243,194,95,0.8));
}

.hint-text {
  text-align: center;
  font-size: 12px;
  color: #9fc6b3;
  margin: 0 0 6px;
}

/* ---------------- Footer ---------------- */
.site-footer {
  text-align: center;
  padding: 12px 16px 18px;
  font-size: 12px;
  color: #7fa596;
  border-top: 1px solid #114c3a;
}

/* ---------------- Modal ---------------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(3, 15, 11, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
  padding: 20px;
}
.modal-overlay.hidden { display: none; }

.modal {
  background: linear-gradient(180deg, #0f4636, #0a3327);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 28px 30px;
  text-align: center;
  min-width: 260px;
  max-width: 90vw;
  box-shadow: 0 20px 50px rgba(0,0,0,0.5);
}
.modal h2 {
  margin: 0 0 12px;
  color: var(--gold-bright);
}
.modal-line {
  margin: 4px 0;
  font-size: 14px;
}
.modal-actions {
  margin-top: 18px;
}

/* ---------------- Responsive ---------------- */
@media (max-width: 640px) {
  .site-header { padding: 10px 12px; }
  .brand-tile { font-size: 26px; }
  .brand-text h1 { font-size: 16px; }
  .tagline { display: none; }
  .hud { padding: 8px 10px; gap: 8px 12px; }
  .hud-stat { min-width: 52px; }
  .hud-value { font-size: 15px; }
  .hud-actions { justify-content: center; margin-left: 0; width: 100%; }
  main { padding: 10px 8px 6px; }
  .seg-btn { padding: 6px 12px; font-size: 12px; }
}

/* ---------------- Embed mode (arcade shell, chapter 90) ----------------
   Inside the brain-platform rebus arcade the shell owns ALL chrome, HUD,
   economy, celebration and navigation, so the embedded board shows only the
   tiles plus a minimal Hint/Shuffle pair. The .mj-embed class is set on <html>
   by an inline head script (no flash of the standalone header). The felt table
   background is kept - it reads as a mahjong table inside the shell's card. */
.mj-embed .site-header,
.mj-embed .site-footer,
.mj-embed .mode-bar,
.mj-embed .hint-text,
.mj-embed .banner,
.mj-embed .hud-stat,
.mj-embed #undoBtn,
.mj-embed #newGameBtn {
  display: none !important;
}
/* The win overlay is redundant in embed mode: the shell runs its own solve
   ceremony and removes the iframe on the mahjong-win message. */
.mj-embed .modal-overlay { display: none !important; }
.mj-embed .hud {
  background: transparent;
  border: none;
  padding: 2px 0 0;
}
.mj-embed .hud-actions { margin-left: 0; }
.mj-embed main { padding: 6px 8px 4px; gap: 6px; }
.mj-embed .board-viewport { padding: 4px 0 8px; }
