/* Layout: sticky header + scrolling tab rail, one column of panels. Tiles are the hero everywhere;
   the page is a dark felt table by default, ivory paper in light mode. */
:root {
  color-scheme: dark;
  --bg: #12271c;
  --surface: #1a3627;
  --surface-2: #224634;
  --line: #2f5a42;
  --fg: #f1e9d4;
  --muted: #a9bba9;
  --accent: #d9463d;
  --accent-fg: #ffffff;
  --say: #ff9a8c;
  --gold: #e5bd5c;
  --ok: #63c882;
  --bad: #ef7a70;
  --ok-bg: rgba(99, 200, 130, .16);
  --bad-bg: rgba(239, 122, 112, .18);
  --font-display: 'Bricolage Grotesque', 'Avenir Next', 'Helvetica Neue', system-ui, sans-serif;
  --font-body: 'Atkinson Hyperlegible', 'Helvetica Neue', system-ui, sans-serif;
  --font-cjk: 'Noto Serif TC', 'Songti TC', 'PingFang TC', 'Hiragino Sans', 'Microsoft JhengHei', serif;
  --radius: 12px;
  --gutter-l: max(16px, env(safe-area-inset-left, 0px));
  --gutter-r: max(16px, env(safe-area-inset-right, 0px));
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;
    --bg: #f1eadb; --surface: #fbf7ec; --surface-2: #efe6d1; --line: #d6c9a9;
    --fg: #1c2a21; --muted: #5f6d62; --accent: #b7352d; --accent-fg: #ffffff; --say: #b7352d; --gold: #8f6c14;
    --ok: #2e8b4e; --bad: #bf3b32; --ok-bg: rgba(46, 139, 78, .12); --bad-bg: rgba(191, 59, 50, .12);
  }
}
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f1eadb; --surface: #fbf7ec; --surface-2: #efe6d1; --line: #d6c9a9;
  --fg: #1c2a21; --muted: #5f6d62; --accent: #b7352d; --accent-fg: #ffffff; --say: #b7352d; --gold: #8f6c14;
  --ok: #2e8b4e; --bad: #bf3b32; --ok-bg: rgba(46, 139, 78, .12); --bad-bg: rgba(191, 59, 50, .12);
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}
body::before { content: ""; position: fixed; inset: 0 0 auto; height: env(safe-area-inset-top, 0px); z-index: 10; background: var(--surface); }
a { color: inherit; }
h1, h2, h3 { font-family: var(--font-display); line-height: 1.15; text-wrap: balance; margin: 0; }
h1 { font-size: 1.9rem; font-weight: 800; }
h2 { font-size: 1.35rem; font-weight: 700; }
h3 { font-size: 1.05rem; font-weight: 700; }
p { margin: 0; }
button { font: inherit; color: inherit; }
.cjk { font-family: var(--font-cjk); }
.muted { color: var(--muted); }
.small { font-size: .9rem; }
.num { font-variant-numeric: tabular-nums; }

/* Header + tabs */
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 9;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding-block: 10px; padding-left: var(--gutter-l); padding-right: var(--gutter-r);
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; min-width: 0; }
.brand-tile {
  display: grid; place-items: center; width: 34px; height: 44px; border-radius: 6px;
  background: #f6efdb; color: #c8322b; font-family: var(--font-cjk); font-weight: 700; font-size: 22px;
  box-shadow: inset 0 -3px 0 #cdb98f, 0 2px 3px rgba(0, 0, 0, .35);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand-name { font-family: var(--font-display); font-weight: 800; font-size: 1.1rem; }
.brand-sub { font-size: .78rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.toggle { display: flex; align-items: center; gap: 6px; min-height: 44px; font-size: .85rem; color: var(--muted); white-space: nowrap; cursor: pointer; }
.toggle input { accent-color: var(--accent); width: 16px; height: 16px; }

.tabs {
  position: sticky; top: calc(env(safe-area-inset-top, 0px) + 65px); z-index: 8;
  display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none;
  padding-block: 8px; padding-left: var(--gutter-l); padding-right: var(--gutter-r);
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  flex: 0 0 auto; display: inline-flex; align-items: center; min-height: 44px; padding: 8px 14px; border-radius: 999px; text-decoration: none;
  font-family: var(--font-display); font-weight: 700; font-size: .92rem; color: var(--muted);
  border: 1px solid transparent;
}
.tabs a.active { color: var(--fg); background: var(--surface-2); border-color: var(--line); }
.tabs a:focus-visible, button:focus-visible, .tile-btn:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

/* View + panels */
.view {
  max-width: 860px; margin: 0 auto;
  padding-block: 18px 48px; padding-left: var(--gutter-l); padding-right: var(--gutter-r);
  display: flex; flex-direction: column; gap: 18px;
}
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.panel.quiet { background: transparent; border-style: dashed; }
.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.row.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: 8px; }
.prose { max-width: 62ch; display: flex; flex-direction: column; gap: 10px; }
.prose li { margin-block: 3px; }
.prose ul, .prose ol { margin: 0; padding-left: 1.2em; }
.eyebrow { font-family: var(--font-display); font-weight: 700; font-size: .75rem; letter-spacing: .08em; text-transform: uppercase; color: var(--gold); }
.lead { font-size: 1.1rem; }
hr.rule { border: 0; border-top: 1px solid var(--line); margin: 4px 0; width: 100%; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2);
  font-family: var(--font-display); font-weight: 700; font-size: 1rem; cursor: pointer; text-decoration: none;
  min-height: 44px;
}
.btn:hover { border-color: var(--gold); }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
.btn.big { font-size: 1.1rem; padding: 12px 20px; }
.btn.ghost { background: transparent; }
.btn[disabled] { opacity: .5; cursor: default; }
.btn.correct { background: var(--ok-bg); border-color: var(--ok); }
.btn.wrong { background: var(--bad-bg); border-color: var(--bad); }
.choices { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; }
.choices.calls { grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); }
.choices .btn { width: 100%; }
.pills { display: flex; flex-wrap: wrap; gap: 6px; }
.pill { min-height: 44px; padding: 8px 14px; border-radius: 999px; border: 1px solid var(--line); background: transparent; font-weight: 700; font-size: .88rem; cursor: pointer; }
.pill.active { background: var(--surface-2); border-color: var(--gold); }

/* Tiles */
.tile { display: inline-block; width: var(--tile-w, 44px); aspect-ratio: 60 / 84; vertical-align: middle; position: relative; flex: 0 0 auto; }
.tile svg { width: 100%; height: 100%; display: block; filter: drop-shadow(0 2px 2px rgba(0, 0, 0, .35)); overflow: visible; }
.tile.hl svg { outline: 3px solid var(--gold); outline-offset: 1px; border-radius: 10%; }
.tile.hl2 svg { outline: 3px solid var(--ok); outline-offset: 1px; border-radius: 10%; }
.tile.dim { opacity: .45; }
.tile.out { transform: translateY(-8px); }
.tile.out::after { content: "discard"; position: absolute; left: 50%; bottom: -1.1em; transform: translateX(-50%); font-size: .62rem; color: var(--gold); font-weight: 700; letter-spacing: .04em; white-space: nowrap; }
.tile.drawn::after { content: "drew"; position: absolute; left: 50%; bottom: -1.1em; transform: translateX(-50%); font-size: .62rem; color: var(--gold); font-weight: 700; letter-spacing: .04em; }
body.no-hints .hint { display: none; }

.hand { display: flex; flex-wrap: wrap; gap: 3px; align-items: flex-end; --tile-w: clamp(32px, calc((100vw - 52px - (var(--n, 13) - 1) * 3px) / var(--n, 13)), 46px); padding-bottom: 14px; }
.hand.loose { --tile-w: 46px; }
.hand.big { --tile-w: 72px; }
.hand.huge { --tile-w: 110px; }
.hand.center { justify-content: center; }
.meld { display: flex; gap: 2px; padding: 4px; border-radius: 8px; background: rgba(0, 0, 0, .18); }
.handgroup { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.handgroup .label { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.tile-btn { background: none; border: 0; padding: 4px; border-radius: 10px; cursor: pointer; display: inline-flex; flex-direction: column; align-items: center; gap: 4px; --tile-w: 52px; }
.tile-btn.sel .tile svg { outline: 3px solid var(--gold); outline-offset: 1px; border-radius: 10%; }
.tile-btn.good .tile svg { outline: 3px solid var(--ok); outline-offset: 1px; border-radius: 10%; }
.tile-btn.badpick .tile svg { outline: 3px solid var(--bad); outline-offset: 1px; border-radius: 10%; }
.tile-btn.missed .tile svg { outline: 3px dashed var(--ok); outline-offset: 1px; border-radius: 10%; }
.palette { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }

/* Dex */
.dex-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; }
.dex-card { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 4px; border-radius: 10px; border: 1px solid transparent; background: transparent; cursor: pointer; --tile-w: 54px; text-align: center; }
.dex-card.active { border-color: var(--gold); background: var(--surface-2); }
.dex-card .n { font-size: .8rem; font-weight: 700; line-height: 1.2; }
.dex-card .z { font-size: .78rem; color: var(--muted); }
.dex-detail { display: grid; grid-template-columns: auto 1fr; gap: 14px; align-items: start; }
.dex-detail .hand { --tile-w: 72px; }
.dex-detail dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; font-size: .95rem; }
.dex-detail dt { color: var(--muted); }
.dex-detail dd { margin: 0; }

/* Streak + feedback */
.streak { display: flex; gap: 16px; align-items: baseline; font-family: var(--font-display); font-weight: 700; color: var(--muted); font-size: .9rem; }
.streak b { color: var(--fg); font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.streak .on b { color: var(--gold); }
.feedback { border-radius: 10px; padding: 12px 14px; border: 1px solid var(--line); background: var(--surface-2); display: flex; flex-direction: column; gap: 8px; }
.feedback.ok { border-color: var(--ok); background: var(--ok-bg); }
.feedback.bad { border-color: var(--bad); background: var(--bad-bg); }
.feedback .title { font-family: var(--font-display); font-weight: 800; font-size: 1.1rem; }
.feedback .hand { --tile-w: 40px; padding-bottom: 4px; }

/* Table diagram */
.table {
  display: grid; grid-template-columns: 1fr minmax(120px, 1.4fr) 1fr; grid-template-rows: auto auto auto;
  gap: 6px; align-items: center; justify-items: center;
  background: radial-gradient(ellipse at center, #2b6a46 0%, #1f4f34 70%); border: 4px solid #5a3a22; border-radius: 16px; padding: 10px;
  color: #f1e9d4;
}
.table .seat { font-family: var(--font-display); font-weight: 700; font-size: .85rem; text-align: center; line-height: 1.2; padding: 6px 8px; border-radius: 8px; border: 1px solid rgba(255, 255, 255, .15); background: rgba(0, 0, 0, .18); min-width: 72px; }
.table .seat small { display: block; font-weight: 400; font-size: .72rem; opacity: .8; }
.table .seat.active { border-color: var(--gold); background: rgba(229, 189, 92, .2); box-shadow: 0 0 0 2px rgba(229, 189, 92, .35); }
.table .seat.you { background: rgba(217, 70, 61, .22); border-color: rgba(217, 70, 61, .6); }
.table .top { grid-column: 2; grid-row: 1; }
.table .left { grid-column: 1; grid-row: 2; }
.table .right { grid-column: 3; grid-row: 2; }
.table .bottom { grid-column: 2; grid-row: 3; }
.table .center { grid-column: 2; grid-row: 2; display: flex; flex-direction: column; align-items: center; gap: 4px; min-height: 84px; justify-content: center; --tile-w: 46px; }
.table .center .cap { font-size: .75rem; opacity: .9; text-align: center; }
.table .arrow { grid-column: 1 / -1; grid-row: 4; font-size: .75rem; opacity: .85; text-align: center; }
.table .pile { display: flex; gap: 2px; --tile-w: 22px; flex-wrap: wrap; justify-content: center; max-width: 150px; }

/* Stepper */
.stepper-nav { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.dots { display: flex; gap: 5px; flex-wrap: wrap; justify-content: center; }
.dots span { width: 8px; height: 8px; border-radius: 50%; background: var(--line); }
.dots span.on { background: var(--gold); }
.frame-title { font-family: var(--font-display); font-weight: 800; font-size: 1.15rem; }

/* Cheat sheet */
.sheet-nav { display: flex; flex-wrap: wrap; gap: 6px; }
.sheet-nav a { font-size: .85rem; text-decoration: none; display: inline-flex; align-items: center; min-height: 44px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 999px; color: var(--muted); }
.score { display: grid; grid-template-columns: 1fr; gap: 6px; padding: 10px 0; border-top: 1px solid var(--line); }
.score:first-of-type { border-top: 0; }
.score .head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.score .name { font-family: var(--font-display); font-weight: 700; }
.score .name .cjk { color: var(--muted); font-weight: 400; margin-left: 6px; }
.score .fan { font-family: var(--font-display); font-weight: 800; color: var(--gold); white-space: nowrap; font-variant-numeric: tabular-nums; }
.score .handgroup { gap: 6px; }
.score .meld { --tile-w: 36px; }
.score .hand.loose { --tile-w: 36px; padding-bottom: 2px; }
.score .desc { font-size: .92rem; color: var(--muted); }
table.plain { width: 100%; border-collapse: collapse; font-size: .95rem; }
table.plain th, table.plain td { text-align: left; padding: 8px 6px; border-top: 1px solid var(--line); vertical-align: top; }
table.plain th { color: var(--muted); font-weight: 700; font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; border-top: 0; }
.tablewrap { overflow-x: auto; min-width: 0; }
.say { font-family: var(--font-display); font-weight: 800; color: var(--say); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: .95rem; }
.kv dt { font-weight: 700; }
.kv dd { margin: 0; color: var(--muted); }
.warn { border-left: 4px solid var(--gold); padding: 8px 12px; background: var(--surface-2); border-radius: 0 8px 8px 0; font-size: .95rem; }

/* Home */
.hero { display: flex; flex-direction: column; gap: 14px; }
.hero .hand { --tile-w: clamp(24px, calc((100vw - 68px - 13 * 3px) / 14), 52px); }
.plan { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; }
.plan a { display: flex; flex-direction: column; gap: 6px; padding: 12px; border: 1px solid var(--line); border-radius: 10px; text-decoration: none; background: var(--surface-2); }
.plan a .t { font-family: var(--font-display); font-weight: 700; }
.plan a .d { font-size: .9rem; color: var(--muted); }
.plan a .b { font-size: .8rem; color: var(--gold); font-weight: 700; }
.legend { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; }
.legend .item { display: flex; flex-direction: column; gap: 6px; padding: 10px; border: 1px solid var(--line); border-radius: 10px; }
.legend .item .hand { --tile-w: 38px; padding-bottom: 2px; }
.legend .item b { font-family: var(--font-display); }

@media (max-width: 480px) {
  .brand-sub { display: none; }
  .tabs { top: calc(env(safe-area-inset-top, 0px) + 65px); }
  .dex-detail { grid-template-columns: 1fr; }
  .table { grid-template-columns: 1fr minmax(96px, 1.2fr) 1fr; }
  .table .seat { min-width: 0; font-size: .78rem; padding: 5px 6px; }
}
@media (prefers-reduced-motion: no-preference) {
  .tile, .btn, .feedback { transition: transform .15s ease, background-color .15s ease, border-color .15s ease; }
}
.stepper-text { max-width: 62ch; }
.dex-h { font-size: 1rem; }
section.panel { scroll-margin-top: calc(env(safe-area-inset-top, 0px) + 126px); }
.note { font-size: .88rem; color: var(--muted); border-left: 3px solid var(--line); padding-left: 10px; }

/* Play */
.opp { display: flex; flex-direction: column; gap: 6px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); min-width: 0; }
.opp.active { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(229, 189, 92, .25); }
.opp-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.opp-head { display: flex; flex-wrap: wrap; gap: 8px; align-items: baseline; font-family: var(--font-display); }
.opp-sets { display: flex; flex-wrap: wrap; gap: 6px; }
.opp-sets .meld { --tile-w: 26px; }
.opp .pile, .pile.mine { display: flex; flex-wrap: wrap; gap: 2px; --tile-w: 22px; min-height: 10px; }
.pile .tile.hl svg { outline-width: 2px; }
.log { font-size: .95rem; display: flex; flex-direction: column; gap: 2px; min-height: 1.4em; }
.you-area { display: flex; flex-direction: column; gap: 8px; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); }
.you-area.active { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(229, 189, 92, .25); }
.you-area .opp-sets .meld { --tile-w: 30px; }
.myhand { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 2px; padding-top: 10px; --tile-w: clamp(30px, calc((100vw - 76px - (var(--n, 14) - 1) * 6px) / var(--n, 14)), 46px); }
.myhand .tile-btn { padding: 2px; gap: 0; --tile-w: var(--tile-w); min-height: 0; }
.myhand .tile-btn.sel { transform: translateY(-8px); }
.myhand .tile-btn.sel .tile svg { outline: 3px solid var(--gold); outline-offset: 1px; border-radius: 10%; }
.drawgap { width: 10px; flex: 0 0 auto; }
@media (max-width: 480px) { .opp-row { grid-template-columns: 1fr; } }
