/* Tokens: dark is the original app's palette; light is derived from it. */
:root, [data-theme=dark] {
  --bg: #10151f; --s1: #182030; --s2: #222c40; --s3: #2b3750;
  --tx: #e8edf6; --mu: #8793a8;
  --g: #2e9f6b; --r: #d4485a; --ac: #f2c14e; --ink: #1a1405;
  --dbg: #1b3a2e; --dtx: #bfead2; --tbg: #40212b; --ttx: #f4c4cc;
  --shadow: 0 1px 0 rgba(255, 255, 255, .03);
  color-scheme: dark;
}
[data-theme=light] {
  --bg: #eef1f6; --s1: #ffffff; --s2: #e2e7f0; --s3: #d3dae6;
  --tx: #141a26; --mu: #5d6779;
  --g: #1f8a5a; --r: #c23447; --ac: #e0a91f; --ink: #1a1405;
  --dbg: #d6efe2; --dtx: #145c3c; --tbg: #f7dade; --ttx: #8a1f2e;
  --shadow: 0 1px 2px rgba(20, 26, 38, .08);
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100vh;
  font: 16px/1.4 Bahnschrift, "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
  background: var(--bg); color: var(--tx);
}
button, input, select { font: inherit; color: inherit; }
button {
  background: var(--s2); color: var(--tx); border: 0; border-radius: 12px;
  padding: 12px 14px; cursor: pointer; transition: transform .08s, background .15s;
  touch-action: manipulation; -webkit-tap-highlight-color: transparent;
}
button:active { transform: scale(.96); }
button:disabled { opacity: .45; cursor: default; transform: none; }
button.primary { background: var(--ac); color: var(--ink); font-weight: 600; }
button.ghost { background: transparent; color: var(--mu); }
button.danger { background: var(--tbg); color: var(--ttx); }
:focus-visible { outline: 2px solid var(--tx); outline-offset: 2px; }
input[type=text], input[type=password], input[type=number], input:not([type]), select {
  background: var(--bg); border: 1px solid var(--s2); border-radius: 10px; padding: 11px; width: 100%; min-width: 0;
}
input:focus { border-color: var(--mu); outline: none; }
a { color: var(--ac); }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; gap: 6px; align-items: center;
  padding: 8px max(12px, env(safe-area-inset-left)); background: var(--bg);
  max-width: 1280px; margin: 0 auto;
}
.tabs { display: flex; gap: 6px; flex: 1; min-width: 0; }
.tabs button { flex: 1; background: transparent; color: var(--mu); padding: 10px 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tabs button.on { background: var(--s2); color: var(--tx); }
.tools { display: flex; gap: 6px; }
.icon { width: 42px; height: 42px; padding: 0; display: grid; place-items: center; background: transparent; color: var(--mu); }
.icon:hover { color: var(--tx); }
.icon.lang { font-size: 13px; font-weight: 600; letter-spacing: .04em; }

/* Layout */
.view { max-width: 760px; margin: 0 auto; padding: 4px 16px 32px; }
.view.wide { max-width: 1280px; }
.card { background: var(--s1); border-radius: 16px; padding: 14px; margin-bottom: 12px; box-shadow: var(--shadow); }
h1, h2, h3 { margin: 0 0 10px; font-weight: 600; line-height: 1.2; }
h1 { font-size: 24px; } h2 { font-size: 20px; } h3 { font-size: 17px; }
.mu, small { color: var(--mu); }
.row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.row > .grow { flex: 1; min-width: 0; }
.stack > * + * { margin-top: 8px; }
.err { color: var(--r); min-height: 1.4em; font-size: 14px; }
.ok { color: var(--g); font-size: 14px; }
.full { width: 100%; }
.center { text-align: center; }
label.field { display: block; margin-bottom: 10px; }
label.field > span { display: block; font-size: 13px; color: var(--mu); margin-bottom: 4px; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button { flex: 1 1 auto; padding: 10px 12px; }
.seg button.on { background: var(--ac); color: var(--ink); font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { border-radius: 999px; padding: 10px 16px; background: var(--s2); }
.chip.on { background: var(--ac); color: var(--ink); font-weight: 600; }
.chip .n { display: inline-grid; place-items: center; width: 20px; height: 20px; border-radius: 50%; background: var(--ink); color: var(--ac); font-size: 12px; margin-right: 6px; }
.badge { display: inline-block; font-size: 11px; font-weight: 600; border-radius: 999px; padding: 1px 8px; vertical-align: middle; }
.badge.lead { background: var(--ac); color: var(--ink); }
.badge.done { background: var(--dbg); color: var(--dtx); }
.badge.mu { background: var(--s2); color: var(--mu); }
.tabs2 { display: flex; gap: 6px; margin-bottom: 12px; }
.tabs2 button { flex: 1; }
.tabs2 button.on { background: var(--s3); }
.divider { display: flex; align-items: center; gap: 10px; color: var(--mu); margin: 14px 0; font-size: 14px; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--s2); }

/* Tables */
table { width: 100%; border-collapse: collapse; }
td, th { padding: 8px 6px; text-align: center; }
tr + tr td, tr + tr th { border-top: 1px solid var(--s2); }
th { font-weight: 600; }
.tl { text-align: left; }
.scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px); opacity: 0;
  background: var(--s3); color: var(--tx); padding: 10px 16px; border-radius: 12px; pointer-events: none;
  transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px); z-index: 20;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

.err:empty { min-height: 0; }
@media (max-width: 480px) {
  .topbar { gap: 4px; padding-left: 8px; padding-right: 8px; }
  .tabs { gap: 3px; }
  .tabs button { padding: 10px 2px; font-size: 14px; }
  .tools { gap: 0; }
  .icon { width: 36px; }
}

/* Setup */
h3.lbl { font-size: 13px; font-weight: 600; color: var(--mu); text-transform: uppercase; letter-spacing: .05em; margin: 16px 0 8px; }
p.hint { margin: 8px 0 0; font-size: 14px; }
.stepper { display: inline-flex; align-items: center; gap: 4px; background: var(--bg); border-radius: 12px; padding: 4px; }
.stepper button { width: 44px; height: 40px; padding: 0; font-size: 20px; }
.stepper b { min-width: 40px; text-align: center; font-size: 20px; }
.formats { display: flex; flex-wrap: wrap; gap: 16px; }
.formats > div { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.num { width: 24px; text-align: center; color: var(--mu); }
.big-cta { padding: 16px; font-size: 18px; margin-top: 4px; }
.card.live { box-shadow: inset 3px 0 var(--ac), var(--shadow); }

/* Play: board */
.play { display: block; }
.abandon { margin: 16px 0 0; }
.head .meta { font-size: 13px; }
.head .turn { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 4px; font-size: 18px; }
.visit { display: inline-flex; gap: 5px; }
.vd { min-width: 38px; height: 26px; padding: 0 6px; border-radius: 8px; background: var(--bg); display: grid; place-items: center; font-size: 13px; font-weight: 600; color: var(--mu); }
.vd.x { background: var(--ac); color: var(--ink); }
.head .last { font-size: 14px; margin-top: 4px; min-height: 1.4em; }
.unsaved { color: var(--r); font-size: 13px; }
.players { padding: 6px; }
.pl { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 10px; border-radius: 12px; }
.pl.on { background: var(--s2); box-shadow: inset 3px 0 var(--ac); }
.pl .who { min-width: 0; }
.pl .nm { font-size: 17px; font-weight: 600; }
.pl small { display: block; font-size: 13px; }
.pl .extra { font-size: 12px; margin-top: 2px; }
.pl .rem { font-size: 40px; font-weight: 700; line-height: 1; }
.badge.co { font-size: 12px; margin-left: 4px; }
.cboard { padding: 8px; }
.cboard td, .cboard th { padding: 6px 4px; }
.cboard th { font-size: 15px; }
.cboard .sm { font-size: 12px; font-weight: 400; }
.cboard .badge { display: block; width: max-content; margin: 3px auto 0; font-size: 10px; }
.cur { background: var(--s2); }
th.cur { box-shadow: inset 0 -3px var(--ac); }
.mk { font-size: 28px; line-height: 1; }
.mk.ok { color: var(--g); }
tr.dead { opacity: .35; }
.big { font-size: 30px; font-weight: 700; }
.hint { margin-top: 8px; color: var(--ac); font-size: 14px; }

/* Play: pads */
button.hit { font-size: 22px; font-weight: 700; min-height: 54px; padding: 6px 2px; display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.1; }
button.hit small { font-size: 11px; font-weight: 400; color: inherit; opacity: .75; }
button.hit.d, .mods .d { background: var(--dbg); color: var(--dtx); box-shadow: inset 0 -3px var(--g); }
button.hit.t, .mods .t { background: var(--tbg); color: var(--ttx); box-shadow: inset 0 -3px var(--r); }
button.dead, .n.dead { opacity: .35; }
.undo { font-size: 16px; }
.span3 { grid-column: span 3; }
.mods { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 8px; }
.mods button { padding: 12px 4px; font-weight: 600; }
.mods button.sel { outline: 3px solid var(--tx); outline-offset: -3px; }
.kp { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.cpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.pad-grid { display: none; padding: 10px; }
.cgrid { display: grid; grid-template-columns: 48px repeat(3, 1fr); gap: 6px; align-items: center; }
.xgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.gblock { display: grid; grid-template-columns: 30px repeat(3, 1fr); gap: 5px; align-items: center; }
.pad-grid .n { font-weight: 700; text-align: center; }
.pad-grid .h { font-size: 12px; color: var(--mu); text-align: center; }
.pad-grid button.hit { min-height: 44px; font-size: 16px; }
.xgrid button.hit { min-height: 38px; font-size: 14px; border-radius: 9px; }
.gfoot { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 8px; }
.gfoot.two { grid-template-columns: 1fr 1fr; }
.gfoot button { min-height: 46px; }

/* Wide screens and phones held sideways: board left, full grid pad right. */
@media (min-width: 700px), (orientation: landscape) and (min-width: 560px) {
  .play { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); gap: 12px; align-items: start; }
  .pad-stacked { display: none; }
  .pad-grid { display: block; }
  .pads { position: sticky; top: 64px; }
}
@media (orientation: landscape) and (max-height: 500px) {
  .topbar { position: static; }
  .pads { position: static; }
  .pad-grid { padding: 6px; }
  .xgrid button.hit { min-height: 27px; font-size: 12px; }
  .gblock { gap: 3px; }
  .gfoot { margin-top: 5px; }
  .gfoot button { min-height: 34px; padding: 4px; }
  .pl .rem { font-size: 30px; }
}

/* Summary */
.summary h1 { font-size: 26px; }
.sumtable td, .sumtable th { white-space: nowrap; }

/* Stats and history */
.lb td, .lb th { white-space: nowrap; }
.lb .sort { background: none; padding: 4px 2px; color: var(--mu); font-weight: 600; font-size: 13px; }
.lb .sort.on { color: var(--tx); }
.lb .rank { display: inline-block; width: 22px; color: var(--mu); }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 8px; text-align: center; }
.tile { background: var(--bg); border-radius: 12px; padding: 10px 4px; }
.tile b { display: block; font-size: 24px; }
.tile small { font-size: 12px; }
button.hist { display: block; width: 100%; text-align: left; padding: 12px 14px; }
button.hist:active { transform: scale(.99); }
.hist .hrow { display: flex; gap: 8px; align-items: baseline; margin-bottom: 2px; }
.hist .when { margin-left: auto; font-size: 13px; white-space: nowrap; }

/* Next player button */
.span2 { grid-column: span 2; }
.span5 { grid-column: span 5; }
.next { font-size: 16px; }
.gfoot.five { grid-template-columns: repeat(5, 1fr); }
.gfoot.three { grid-template-columns: repeat(3, 1fr); }

/* Laptops and tablets: fill the screen height; board text and pad buttons scale with it. */
@media (min-width: 700px) and (min-height: 600px) {
  .topbar, .view.wide { max-width: 1600px; }
  .view.wide { padding-bottom: 0; }
  .play {
    height: calc(100dvh - 78px);
    grid-template-rows: minmax(0, 1fr) auto;
  }
  .board { grid-column: 1; grid-row: 1; display: flex; flex-direction: column; height: 100%; min-height: 0; }
  .board > .card { margin-bottom: 10px; }
  .board > .players, .board > .cboard { flex: 1; min-height: 0; overflow: auto; margin-bottom: 0; }
  .pads { grid-column: 2; grid-row: 1 / span 2; height: 100%; min-height: 0; position: static; }
  .pad-grid { height: 100%; display: flex; flex-direction: column; margin: 0; }
  .cgrid { flex: 1; grid-template-rows: auto repeat(7, minmax(0, 1fr)); }
  .xgrid { flex: 1; min-height: 0; }
  .gblock { grid-template-rows: repeat(10, minmax(0, 1fr)); height: 100%; }
  .pad-grid button.hit, .xgrid button.hit { min-height: 0; height: 100%; font-size: clamp(14px, 2.6vh, 30px); }
  .pad-grid .n { font-size: clamp(15px, 2.6vh, 30px); }
  .gfoot { flex: 0 0 clamp(48px, 9vh, 96px); }
  .gfoot button { height: 100%; font-size: clamp(15px, 2.2vh, 24px); }
  .gfoot button.hit { font-size: clamp(18px, 3vh, 34px); }
  .abandon { grid-column: 1; grid-row: 2; margin: 0; }

  .head .turn { font-size: clamp(18px, 2.8vh, 32px); }
  .head .meta, .head .last { font-size: clamp(13px, 1.8vh, 20px); }
  .vd { height: clamp(26px, 3.6vh, 44px); min-width: clamp(38px, 5vh, 64px); font-size: clamp(13px, 1.9vh, 22px); }
  .players { display: flex; flex-direction: column; }
  .players .pl { flex: 1; max-height: 22vh; }
  .pl .nm { font-size: clamp(17px, 2.8vh, 34px); }
  .pl small { font-size: clamp(13px, 1.8vh, 20px); }
  .pl .extra { font-size: clamp(12px, 1.6vh, 18px); }
  .pl .rem { font-size: clamp(40px, 8vh, 110px); }
  .cboard .scroll-x { height: 100%; }
  .cboard table { height: 100%; }
  .cboard th { font-size: clamp(15px, 2.3vh, 28px); }
  .cboard .mk { font-size: clamp(28px, 4.6vh, 60px); }
  .cboard .big { font-size: clamp(30px, 5vh, 64px); }
  .cboard td { font-size: clamp(14px, 2vh, 24px); }
}
