/* Azul - phone first. Layouts: body[data-layout=mobile] (stacked table, landscape: side by side)
   and body[data-layout=desktop] (every board on screen, factories in a ring). */

@font-face {
  font-family: 'Vazirmatn';
  src: url('fonts/vazirmatn-arabic.woff2') format('woff2');
  font-weight: 100 900;
  font-display: swap;
  unicode-range: U+0600-06FF, U+0750-077F, U+200C-200E, U+2010-2011, U+204F, U+2E41, U+FB50-FDFF, U+FE70-FEFF;
}
@font-face {
  font-family: 'Vazirmatn';
  src: url('fonts/vazirmatn-latin.woff2') format('woff2');
  font-weight: 100 900;
  font-display: swap;
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

:root {
  --azul: #0e2a52;
  --azul-2: #163d72;
  --azul-3: #2b66b1;
  --ivory: #f7f0e3;
  --ivory-2: #efe4cf;
  --ink: #1d2738;
  --muted: #6b7486;
  --gold: #d9a441;
  --gold-2: #f3c96b;
  --terra: #c8572b;
  --ok: #2f8f5b;
  --danger: #c0392b;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(3, 10, 25, 0.35);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --topbar-h: 46px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: 'Vazirmatn', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Tahoma, sans-serif;
  background: radial-gradient(120% 80% at 50% 0%, #1b4a86 0%, var(--azul) 55%, #08172e 100%) fixed;
  color: #f4f7fb;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
  line-height: 1.6;
}

button, input { font: inherit; }
button { cursor: pointer; }
a { color: inherit; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.center { text-align: center; }

/* ============================================================ */
/* BOARDBAZI BAR                                                 */
/* ============================================================ */
.bb-bar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 40px;
  padding: var(--safe-top) 12px 0;
  padding-inline: max(12px, env(safe-area-inset-left, 0px)) max(12px, env(safe-area-inset-right, 0px));
  background: rgba(14, 18, 29, 0.92);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  color: #f8fafc;
  font-size: 14px;
  backdrop-filter: blur(6px);
}
.bb-brand, .bb-chip { display: inline-flex; align-items: center; gap: 8px; text-decoration: none; min-width: 0; height: 40px; }
.bb-mark {
  display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 6px; color: #fff;
  background: linear-gradient(135deg, #6366f1, #06b6d4); flex: none;
}
.bb-avatar {
  display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; flex: none;
  background: #334155; font-size: 12px; font-weight: 700;
}
.bb-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 40vw; }
.bb-level { font-size: 12px; color: #94a3b8; white-space: nowrap; }
.bb-xp { width: 48px; height: 4px; border-radius: 2px; background: rgba(255, 255, 255, 0.15); overflow: hidden; flex: none; }
.bb-xp-fill { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #6366f1, #06b6d4); }
@media (max-width: 360px) {
  .bb-word { display: none; }
}

/* ============================================================ */
/* COMMON PIECES                                                 */
/* ============================================================ */
.view { display: none; }
.view.active { display: block; }

.page {
  position: relative;
  max-width: 520px;
  margin: 0 auto;
  padding: 14px 14px calc(24px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.panel {
  background: rgba(247, 240, 227, 0.97);
  color: var(--ink);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  border: 1px solid rgba(217, 164, 65, 0.45);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.label { font-weight: 700; color: var(--azul); }
.stack { display: flex; flex-direction: column; gap: 10px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.row-center { display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 8px 16px; border-radius: 12px; border: 0;
  font-weight: 700; transition: transform 0.1s, filter 0.15s, background 0.15s;
  text-decoration: none;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-lg { min-height: 52px; font-size: 1.05rem; }
.btn-sm { min-height: 36px; padding: 4px 12px; font-size: 0.9rem; }
.btn-primary { background: linear-gradient(180deg, var(--azul-3), var(--azul-2)); color: #fff; box-shadow: 0 4px 0 #0a2142; }
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn-secondary { background: var(--ivory-2); color: var(--azul); border: 1px solid rgba(14, 42, 82, 0.18); }
.btn-ghost { background: transparent; color: inherit; border: 1px solid currentColor; opacity: 0.85; }
.panel .btn-ghost { color: var(--azul); }
.btn-danger { background: #fbe9e6; color: var(--danger); border: 1px solid rgba(192, 57, 43, 0.3); }

.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px; border-radius: 10px; border: 0;
  background: rgba(255, 255, 255, 0.12); color: inherit; font-size: 1.15rem;
}
.icon-btn.small { width: 32px; height: 32px; font-size: 0.9rem; background: rgba(14, 42, 82, 0.08); color: var(--azul); }
.panel .icon-btn, .modal-card .icon-btn { background: rgba(14, 42, 82, 0.08); color: var(--azul); }

.input {
  width: 100%; min-height: 46px; padding: 8px 12px; border-radius: 12px;
  border: 1px solid rgba(14, 42, 82, 0.25); background: #fff; color: var(--ink);
}
.input:focus { outline: 2px solid var(--azul-3); outline-offset: 1px; }

.chip {
  display: inline-flex; padding: 4px 10px; border-radius: 999px; font-size: 0.85rem;
  background: rgba(14, 42, 82, 0.08); color: var(--azul);
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }

.avatar {
  display: inline-grid; place-items: center; width: 40px; height: 40px; border-radius: 50%; flex: none;
  background: linear-gradient(135deg, var(--azul-3), var(--azul)); color: #fff; font-weight: 800;
}
.avatar.small { width: 28px; height: 28px; font-size: 0.85rem; }
.avatar-bot { background: linear-gradient(135deg, #8a96ad, #4a5670); }
.avatar-empty { background: transparent; border: 2px dashed rgba(14, 42, 82, 0.25); color: rgba(14, 42, 82, 0.4); }

.tag { font-size: 0.72rem; padding: 1px 8px; border-radius: 999px; background: rgba(14, 42, 82, 0.1); color: var(--azul); white-space: nowrap; }
.tag-host { background: #fbecc8; color: #8a5a00; }
.tag-you { background: #d9ebff; color: #134b8a; }
.tag-off { background: #f3d9d4; color: var(--danger); }
.badge { font-size: 0.8rem; padding: 0 8px; border-radius: 999px; background: var(--azul); color: #fff; font-weight: 600; }

.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 0.85rem; }
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: rgba(14, 42, 82, 0.15); }

.seg { display: inline-flex; background: rgba(14, 42, 82, 0.08); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 6px 10px; border-radius: 8px; color: var(--azul); font-size: 0.88rem; min-height: 34px; }
.seg button.active { background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15); font-weight: 700; }
.seg-sm button { padding: 4px 8px; font-size: 0.8rem; }

/* Tiles everywhere use the real tile art */
.tile, .tile-ico { background: center / contain no-repeat; border: 0; padding: 0; }
.t0 { background-image: url('img/tile-0.webp'); }
.t1 { background-image: url('img/tile-1.webp'); }
.t2 { background-image: url('img/tile-2.webp'); }
.t3 { background-image: url('img/tile-3.webp'); }
.t4 { background-image: url('img/tile-4.webp'); }
.tfirst { background-image: url('img/first.webp'); }
.tile-ico { display: inline-block; width: 1.15em; height: 1.15em; vertical-align: -0.2em; margin: 0 1px; border-radius: 3px; }

/* ============================================================ */
/* LANDING                                                       */
/* ============================================================ */
#landingView .hero {
  position: fixed; inset: 0; z-index: -1;
  background: linear-gradient(180deg, rgba(8, 23, 46, 0) 30%, rgba(8, 23, 46, 0.75) 62%, #08172e 100%),
    url('img/hero-portrait.webp') top center / cover no-repeat;
}
.landing-head { min-height: min(76vw, 400px); display: flex; align-items: flex-end; justify-content: center; }
.tagline {
  margin: 0; text-align: center; font-weight: 700; font-size: 1.05rem;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.8);
}
.lang-row { display: flex; justify-content: flex-end; }
.lang-row .btn { background: rgba(8, 23, 46, 0.55); color: #fff; border-color: rgba(255, 255, 255, 0.5); }
.intro { margin: 0; }
.account-row { display: flex; align-items: center; gap: 12px; }
.account-info { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.account-name { font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.join-row { display: flex; gap: 8px; }
.code-input { text-transform: uppercase; letter-spacing: 0.2em; text-align: center; font-weight: 800; }
.credits { color: rgba(255, 255, 255, 0.6); margin: 0; }

@media (min-width: 900px) {
  #landingView .hero {
    background: linear-gradient(180deg, rgba(8, 23, 46, 0) 25%, rgba(8, 23, 46, 0.8) 60%, #08172e 100%),
      url('img/hero-wide.webp') top center / cover no-repeat;
  }
  /* Two columns under the logo of the hero picture */
  .landing-page {
    max-width: 980px; display: grid; grid-template-columns: 1fr 1fr; align-items: start; gap: 16px;
  }
  .landing-page > .lang-row, .landing-page > .landing-head, .landing-page > .credits { grid-column: 1 / -1; }
  .landing-head { min-height: min(34vw, 40vh); }
  .tagline { font-size: 1.25rem; }
}

/* ============================================================ */
/* LOBBY & LOCAL SETUP                                          */
/* ============================================================ */
#lobbyView, #localView { padding-bottom: var(--safe-bottom); }
.live-pill { display: inline-flex; align-items: center; gap: 6px; font-size: 0.85rem; padding: 2px 10px; border-radius: 999px; background: rgba(47, 143, 91, 0.2); }
.pulse { width: 8px; height: 8px; border-radius: 50%; background: #4ade80; animation: pulse 1.6s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.3; } }

.code-card { align-items: center; text-align: center; }
.code-value { font-size: 2.6rem; font-weight: 900; letter-spacing: 0.25em; color: var(--azul); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; padding-inline-start: 0.25em; }

.seat-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.seat { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 12px; background: #fff; border: 1px solid rgba(14, 42, 82, 0.1); min-height: 56px; }
.seat-empty { background: transparent; border-style: dashed; }
.seat-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; }
.tags { display: flex; gap: 4px; flex-wrap: wrap; }
.local-seat .input { flex: 1; min-height: 40px; }
.notice { margin: 0; padding: 8px 12px; border-radius: 10px; background: #fdf3dc; color: #7a5200; font-size: 0.9rem; }
.notice.ok { background: #e1f4e8; color: #1f6b41; }

.result-list, .final-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.result-list li { display: flex; align-items: center; gap: 10px; padding: 6px 10px; border-radius: 10px; background: #fff; }
.result-list li.win { background: #fdf3dc; }
.place { min-width: 2.4em; text-align: center; font-weight: 800; color: var(--azul); }
.pts { font-size: 1.15rem; color: var(--azul); }

/* ============================================================ */
/* GAME TABLE                                                    */
/* ============================================================ */
/* The table fills the screen under the BoardBazi bar (shown again once the game is over) */
.app { min-height: 100dvh; display: flex; flex-direction: column; }
body.in-game .app { height: 100dvh; overflow: hidden; }
#gameView.active { display: flex; flex-direction: column; flex: 1; min-height: 0; overflow: hidden; }
body.in-game { overflow: hidden; }

.topbar {
  position: relative; flex: none; display: flex; align-items: center; gap: 10px;
  min-height: calc(var(--topbar-h) + var(--safe-top)); padding: var(--safe-top) 10px 0;
  background: rgba(6, 17, 36, 0.85); border-bottom: 1px solid rgba(217, 164, 65, 0.35);
}
.topbar-info { flex: 1; display: flex; gap: 6px; align-items: center; font-weight: 700; white-space: nowrap; overflow: hidden; }
.topbar-sep, .topbar-bag { color: rgba(255, 255, 255, 0.65); font-weight: 500; }
.topbar-room { font-family: ui-monospace, Menlo, monospace; letter-spacing: 0.15em; color: var(--gold-2); font-weight: 700; }
.timer { position: absolute; inset-inline: 0; bottom: -1px; height: 4px; background: rgba(255, 255, 255, 0.1); }
.timer-bar { height: 100%; width: 100%; background: linear-gradient(90deg, var(--gold), var(--gold-2)); }
[dir='rtl'] .timer-bar { margin-inline-start: auto; }
.timer.urgent .timer-bar { background: linear-gradient(90deg, #e74c3c, #ff8a65); animation: pulse 0.8s infinite; }

.table {
  flex: 1; min-height: 0; width: 100%; max-width: 720px; margin: 0 auto;
  display: flex; flex-direction: column; gap: 6px;
  padding: 6px 8px calc(6px + var(--safe-bottom));
}

/* Players other than you */
.opponents { flex: none; display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.opponents::-webkit-scrollbar { display: none; }
.opp-chip {
  flex: 1 1 0; min-width: 0; display: flex; align-items: center; gap: 8px;
  padding: 6px 8px; border-radius: 12px; border: 2px solid transparent;
  background: rgba(247, 240, 227, 0.12); color: inherit; text-align: start;
  transition: border-color 0.2s, background 0.2s;
}
.opp-chip.turn { border-color: var(--gold-2); background: rgba(243, 201, 107, 0.18); box-shadow: 0 0 12px rgba(243, 201, 107, 0.35); }
.opp-chip.away { opacity: 0.6; }
.opp-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.opp-name { font-size: 0.85rem; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.opp-meta { display: flex; align-items: center; gap: 4px; font-size: 0.85rem; }
.opp-meta b { color: var(--gold-2); font-size: 1rem; }
.opp-row { font-size: 0.6rem; letter-spacing: -1px; color: rgba(255, 255, 255, 0.55); }

/* Factories and the centre */
.market { flex: 1 1 0; min-height: 0; display: flex; flex-direction: column; justify-content: center; gap: 8px; }
.factories { display: grid; gap: 6px; justify-content: center; grid-template-columns: repeat(5, minmax(0, 86px)); }
.factories[data-count='7'] { grid-template-columns: repeat(4, minmax(0, 86px)); }
.factory {
  position: relative; aspect-ratio: 1; border-radius: 50%;
  background: url('img/factory.webp') center / contain no-repeat;
  filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.4));
  transition: opacity 0.3s;
}
.factory.empty { opacity: 0.35; }
.factory-tiles {
  position: absolute; inset: 19%; display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; gap: 6%;
}
.factory-tiles .tile { width: 100%; height: 100%; border-radius: 14%; transition: transform 0.15s, opacity 0.15s, filter 0.15s; }
.market.active .factory-tiles .tile:hover { transform: translateY(-2px) scale(1.06); }
.tile.picked { transform: translateY(-3px) scale(1.1); filter: drop-shadow(0 0 4px var(--gold-2)) drop-shadow(0 0 2px #fff); z-index: 1; }
.tile.leftover { opacity: 0.7; transform: scale(0.86); filter: saturate(0.55) brightness(0.85); }

.center-pool {
  display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 6px;
  min-height: 48px; padding: 6px 8px; border-radius: 16px;
  background: rgba(247, 240, 227, 0.1); border: 1px dashed rgba(247, 240, 227, 0.3);
}
.center-label { font-size: 0.75rem; color: rgba(255, 255, 255, 0.6); margin-inline-end: 4px; }
.pool-stack {
  position: relative; display: inline-flex; align-items: center; gap: 2px; padding: 3px 6px 3px 3px;
  border-radius: 10px; border: 2px solid transparent; background: rgba(6, 17, 36, 0.35); color: #fff;
  transition: transform 0.15s, border-color 0.15s;
}
.pool-stack .tile { width: 34px; height: 34px; display: block; }
.pool-stack .cnt { font-weight: 800; font-size: 0.9rem; min-width: 1.6em; }
.pool-stack.token { padding: 3px; background: transparent; }
.market.active button.pool-stack:hover { transform: translateY(-2px); }
.pool-stack.picked { border-color: var(--gold-2); background: rgba(243, 201, 107, 0.25); transform: translateY(-2px); }

.market.active .factory-tiles .tile, .market.active button.pool-stack { cursor: pointer; }
.market:not(.active) button { cursor: default; }

/* Turn hint */
.hint {
  flex: none; height: 48px; overflow: hidden; line-height: 1.35; display: flex; align-items: center; justify-content: center; gap: 6px; text-align: center;
  padding: 4px 10px; border-radius: 10px; font-size: 0.9rem; background: rgba(6, 17, 36, 0.4);
}
.hint.mine-turn { background: linear-gradient(90deg, rgba(217, 164, 65, 0.25), rgba(243, 201, 107, 0.35)); color: #fff7df; font-weight: 700; }
.hint.clickable { cursor: pointer; text-decoration: underline dotted; }

/* ============================================================ */
/* PLAYER BOARD (tiles drawn on the board art)                   */
/* ============================================================ */
.mine { flex: none; display: flex; justify-content: center; }
.mine .board { max-width: 640px; }
.board {
  position: relative; width: 100%; aspect-ratio: 1161 / 695;
  background: url('img/board.webp') center / 100% 100% no-repeat;
  border-radius: 1.6% / 2.6%; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  user-select: none; -webkit-user-select: none; flex: none;
}
.board.is-turn { box-shadow: 0 0 0 2px var(--gold-2), 0 0 18px rgba(243, 201, 107, 0.5), 0 6px 18px rgba(0, 0, 0, 0.45); }
.board .cell { position: absolute; display: block; }
.board .tile { border-radius: 10%; filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.35)); }
.wall-empty { background: rgba(246, 239, 224, 0.74); border-radius: 8%; }
.board .ghost { opacity: 0; pointer-events: none; transition: opacity 0.12s; }
.board:has(.line-hit[data-line='0']:hover) .ghost[data-gl='0'],
.board:has(.line-hit[data-line='1']:hover) .ghost[data-gl='1'],
.board:has(.line-hit[data-line='2']:hover) .ghost[data-gl='2'],
.board:has(.line-hit[data-line='3']:hover) .ghost[data-gl='3'],
.board:has(.line-hit[data-line='4']:hover) .ghost[data-gl='4'] { opacity: 0.6; }

.line-hit {
  position: absolute; display: block; padding: 0; border-radius: 6px; background: transparent;
  border: 0; z-index: 2;
}
.line-hit.legal { box-shadow: inset 0 0 0 2px rgba(243, 201, 107, 0.95), 0 0 10px rgba(243, 201, 107, 0.6); animation: glow 1.4s ease-in-out infinite; }
.line-hit.legal:hover, .line-hit.legal:focus-visible { background: rgba(243, 201, 107, 0.2); outline: 0; }
.line-hit.illegal { background: rgba(20, 24, 34, 0.28); cursor: not-allowed; }
.floor-hit.legal { box-shadow: inset 0 0 0 2px rgba(255, 140, 100, 0.95), 0 0 10px rgba(255, 140, 100, 0.5); }
@keyframes glow { 0%, 100% { box-shadow: inset 0 0 0 2px rgba(243, 201, 107, 0.95), 0 0 6px rgba(243, 201, 107, 0.4); } 50% { box-shadow: inset 0 0 0 3px rgba(255, 226, 150, 1), 0 0 14px rgba(243, 201, 107, 0.8); } }

.plate {
  position: absolute; left: 3.2%; top: 5.5%; width: 24.5%; height: 22%;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2%;
  border-radius: 10px; background: rgba(14, 42, 82, 0.92); border: 2px solid var(--gold);
  color: #fff; container-type: size; overflow: hidden; padding: 2% 4%;
}
.plate.turn { border-color: var(--gold-2); box-shadow: 0 0 10px rgba(243, 201, 107, 0.8); }
.plate-name { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: clamp(9px, 20cqh, 18px); font-weight: 700; line-height: 1.2; }
.plate-score { display: flex; align-items: center; gap: 6%; font-size: clamp(12px, 40cqh, 34px); line-height: 1; color: var(--gold-2); }
.plate-score .tile-ico { width: 0.8em; height: 0.8em; }

.arrive { animation: arrive 0.3s ease-out 0.5s both; }
@keyframes arrive { from { opacity: 0; transform: scale(0.5); } to { opacity: 1; transform: none; } }
.pop { animation: pop 0.7s cubic-bezier(0.2, 1.6, 0.4, 1) both; }
@keyframes pop {
  0% { transform: scale(0.3); opacity: 0; filter: drop-shadow(0 0 0 rgba(243, 201, 107, 0)); }
  60% { transform: scale(1.15); opacity: 1; filter: drop-shadow(0 0 8px rgba(243, 201, 107, 1)); }
  100% { transform: none; filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.35)); }
}

.fly-layer { position: fixed; inset: 0; pointer-events: none; z-index: 60; }
.tile.flying {
  position: fixed; display: block; border-radius: 12%; opacity: 1; z-index: 60;
  transition: transform 0.55s cubic-bezier(0.35, 0.05, 0.25, 1), opacity 0.55s;
  filter: drop-shadow(0 6px 6px rgba(0, 0, 0, 0.45));
}

/* Phones held sideways: market and your board side by side */
@media (orientation: landscape) and (max-height: 520px) {
  body[data-layout='mobile'] .table {
    max-width: none; display: grid; gap: 6px 10px;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas: 'opps opps' 'market mine' 'hint mine';
  }
  body[data-layout='mobile'] .opponents { grid-area: opps; }
  body[data-layout='mobile'] .market { grid-area: market; }
  body[data-layout='mobile'] .hint { grid-area: hint; }
  body[data-layout='mobile'] .mine { grid-area: mine; align-items: center; min-height: 0; container-type: size; }
  body[data-layout='mobile'] .mine .board { width: min(100cqw, 100cqh * 1161 / 695); max-width: none; }
  body[data-layout='mobile'] .factories { grid-template-columns: repeat(5, minmax(0, 64px)); }
  body[data-layout='mobile'] .factories[data-count='7'] { grid-template-columns: repeat(4, minmax(0, 64px)); }
  body[data-layout='mobile'] .pool-stack .tile { width: 26px; height: 26px; }
  body[data-layout='mobile'] .opp-chip { padding: 3px 8px; }
  :root { --topbar-h: 38px; }
}

/* Short phones in portrait */
@media (orientation: portrait) and (max-height: 640px) {
  body[data-layout='mobile'] .factories { grid-template-columns: repeat(5, minmax(0, 66px)); }
  body[data-layout='mobile'] .factories[data-count='7'] { grid-template-columns: repeat(4, minmax(0, 66px)); }
  body[data-layout='mobile'] .pool-stack .tile { width: 28px; height: 28px; }
}

/* ============================================================ */
/* DESKTOP: all boards, factories in a ring around the centre    */
/* ============================================================ */
body[data-layout='desktop'] .table {
  max-width: 1500px; display: grid; gap: 12px 20px; padding: 12px 18px;
  grid-template-columns: minmax(340px, 1fr) minmax(0, 1.25fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas: 'opps opps' 'market mine' 'hint mine';
}
body[data-layout='desktop'] .opponents { grid-area: opps; justify-content: center; gap: 14px; overflow: visible; }
body[data-layout='desktop'] .opponents .board { width: min(31%, 340px, calc((100dvh - 120px) * 0.3 * 1.67)); }
body[data-layout='desktop'] .market { grid-area: market; align-items: center; justify-content: center; position: relative; }
body[data-layout='desktop'] .hint { grid-area: hint; font-size: 1rem; }
body[data-layout='desktop'] .mine { grid-area: mine; align-items: center; min-height: 0; container-type: size; }
body[data-layout='desktop'] .mine .board { width: min(100cqw, 100cqh * 1161 / 695, 920px); max-width: none; }

body[data-layout='desktop'] .factories {
  position: relative; display: block; width: min(100%, calc(100dvh - 330px), 620px); aspect-ratio: 1;
}
body[data-layout='desktop'] .factory {
  position: absolute; width: 21%; left: var(--x); top: var(--y); transform: translate(-50%, -50%);
}
body[data-layout='desktop'] .center-pool {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(44%, 270px); max-width: 270px; background: rgba(6, 17, 36, 0.35);
}
body[data-layout='desktop'] .pool-stack .tile { width: 36px; height: 36px; }
body[data-layout='desktop'] .opp-chip { flex: none; }

/* ============================================================ */
/* MODALS                                                        */
/* ============================================================ */
.modal {
  position: fixed; inset: 0; z-index: 70; display: none; align-items: center; justify-content: center;
  padding: calc(12px + var(--safe-top)) 12px calc(12px + var(--safe-bottom));
  background: rgba(4, 10, 22, 0.6); backdrop-filter: blur(2px);
}
.modal.open { display: flex; animation: fade 0.2s ease-out; }
@keyframes fade { from { opacity: 0; } }
.modal-card {
  width: 100%; max-width: 520px; max-height: 100%; overflow: auto;
  background: var(--ivory); color: var(--ink); border-radius: 18px; padding: 16px;
  box-shadow: var(--shadow); border: 1px solid rgba(217, 164, 65, 0.6);
  display: flex; flex-direction: column; gap: 12px;
}
.modal-card h2 { margin: 0; color: var(--azul); font-size: 1.3rem; text-align: center; }
.modal.sheet { align-items: flex-end; }
.modal.sheet .modal-card { border-radius: 18px 18px 12px 12px; animation: up 0.22s ease-out; }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
@media (min-width: 900px) { .modal.sheet { align-items: center; } .modal.sheet .modal-card { max-width: 760px; } }
#boardSheetBody .board { margin: 0 auto; }

.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 4px 0; border-bottom: 1px solid rgba(14, 42, 82, 0.08); }
.log summary { cursor: pointer; font-weight: 700; color: var(--azul); padding: 6px 0; }
.log ol { margin: 0; padding-inline-start: 20px; max-height: 40vh; overflow: auto; font-size: 0.88rem; }
.log li { padding: 2px 0; }

.review-list { display: flex; flex-direction: column; gap: 8px; }
.review-row { background: #fff; border-radius: 12px; padding: 8px 10px; border: 1px solid rgba(14, 42, 82, 0.1); }
.review-row.me { border-color: var(--azul-3); }
.review-head { display: flex; align-items: center; gap: 8px; }
.review-head b:first-child { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.review-score { white-space: nowrap; color: var(--muted); }
.review-score b { color: var(--azul); font-size: 1.15rem; }
.review-detail { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.gain, .loss { display: inline-flex; align-items: center; gap: 2px; padding: 1px 8px; border-radius: 999px; font-size: 0.85rem; font-weight: 700; }
.gain { background: #e1f4e8; color: #1f6b41; }
.loss { background: #fbe9e6; color: var(--danger); direction: ltr; }
.ready { color: var(--ok); font-weight: 900; }

.result-headline { margin: 0; text-align: center; font-size: 1.15rem; font-weight: 800; color: var(--terra); }
.final-list li { background: #fff; border-radius: 12px; padding: 8px 10px; border: 1px solid rgba(14, 42, 82, 0.1); }
.final-list li.win { background: #fdf3dc; border-color: var(--gold); }
.final-list li.me { box-shadow: inset 0 0 0 2px var(--azul-3); }
.final-head { display: flex; align-items: center; gap: 8px; }
.final-detail { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 2px; }

.rules-body h3 { margin: 10px 0 2px; color: var(--azul); font-size: 1rem; }
.rules-body p { margin: 0; }

/* ============================================================ */
/* TOASTS & CONNECTION                                           */
/* ============================================================ */
.toasts {
  position: fixed; inset-inline: 0; top: calc(var(--topbar-h) + var(--safe-top) + 8px); z-index: 80;
  display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; padding: 0 12px;
}
.toast {
  max-width: 460px; padding: 8px 14px; border-radius: 12px; font-size: 0.9rem;
  background: rgba(6, 17, 36, 0.92); border: 1px solid rgba(217, 164, 65, 0.5); color: #fff;
  box-shadow: var(--shadow); opacity: 0; transform: translateY(-8px); transition: opacity 0.25s, transform 0.25s;
}
.toast.show { opacity: 1; transform: none; }
.toast.error { border-color: #e57373; background: rgba(80, 16, 12, 0.94); }
.toast .who { color: var(--gold-2); }

.connection {
  position: fixed; inset-inline: 0; bottom: 0; z-index: 90; text-align: center; padding: 8px 12px calc(8px + var(--safe-bottom));
  background: #8a2a1e; color: #fff; font-weight: 700;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
