/* --------------------------------------------------------------- foundation */
:root {
  --bg:        #f6f5fb;
  --surface:   #ffffff;
  --surface-2: #eceaf5;
  --line:      #dcd9ea;
  --text:      #1a1730;
  --muted:     #6d6892;
  --accent:    #4c3fa8;
  --gold:      #8a6206;
  --gold-bg:   #fdf3d8;
  --danger:    #b3261e;
  --radius:    14px;
  --cell-w:    72px;
  --on-accent: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:        #14122b;
    --surface:   #1e1b3f;
    --surface-2: #272350;
    --line:      #332e63;
    --text:      #eceaf8;
    --muted:     #9a94c4;
    --accent:    #a99bff;
    --gold:      #f5c451;
    --gold-bg:   #3a2f12;
    --danger:    #ff8a80;
    --on-accent: #14122b;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

/* Outranks the id-level display rules on the views below. */
[hidden] { display: none !important; }

/* Every view is capped and centred, so a laptop gets a readable column
   rather than a scoreboard stretched across 1600px. */
.view { width: 100%; max-width: 860px; margin-inline: auto; }

/* Views that own the whole window and scroll internally. */
#view-setup, #view-game, #view-history, #view-archive, #view-watch {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

/* Views that simply scroll with the page. */
.scroll-view {
  min-height: 100dvh;
  padding: calc(env(safe-area-inset-top) + 24px) 16px
           calc(env(safe-area-inset-bottom) + 24px);
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.scroll-body { flex: 1; overflow: auto; }

.setup-body {
  display: flex; flex-direction: column; gap: 18px;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 20px);
}

/* --------------------------------------------------------------------- home */
.hero { text-align: center; }
.hero-mark { border-radius: 18px; }
.hero h1 { margin: 12px 0 0; font-size: 30px; letter-spacing: -0.02em; }
.sub { margin: 2px 0 0; color: var(--muted); font-size: 15px; }

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
}
.panel h2 {
  margin: 0; font-size: 13px; text-transform: uppercase;
  letter-spacing: 0.08em; color: var(--muted);
}
.panel-note { margin: 6px 0 12px; font-size: 13px; color: var(--muted); }
.panel h2 + .player-list, .panel h2 + .field { margin-top: 12px; }

.player-list { list-style: none; margin: 0 0 10px; padding: 0;
               display: flex; flex-direction: column; gap: 8px; }

/* [up] [down] [name] [dealer] [remove] */
.player-row {
  display: grid;
  grid-template-columns: 30px 30px 1fr 34px 30px;
  gap: 4px;
  align-items: center;
}
/* Tap-to-pick grid of everyone in the group. */
.roster { display: flex; flex-wrap: wrap; gap: 7px; margin-bottom: 11px; }

.chip-pick {
  appearance: none;
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 40px; padding: 7px 13px;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 999px;
  font: inherit; font-size: 15px; font-weight: 600;
  cursor: pointer;
}
.chip-pick.is-on {
  background: var(--accent); border-color: var(--accent); color: var(--on-accent);
}
.chip-pick:disabled { opacity: 0.35; cursor: default; }

/* The seat number, so the playing order is legible from the grid alone. */
.seat {
  display: inline-flex; align-items: center; justify-content: center;
  width: 19px; height: 19px; border-radius: 50%;
  background: rgba(255, 255, 255, 0.28);
  font-size: 11px; font-weight: 800;
}

/* A chosen player's name: tapping it renames or retires them. */
.name-btn {
  appearance: none;
  min-width: 0; height: 44px; padding: 0 12px;
  text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px;
  font: inherit; font-size: 17px; cursor: pointer;
}
.name-btn:hover { border-color: var(--accent); }

.text-input {
  width: 100%; padding: 12px 13px;
  font: inherit; font-size: 17px;
  color: var(--text); background: var(--surface-2);
  border: 1px solid var(--line); border-radius: 10px;
}
.text-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.row-btn {
  appearance: none; background: transparent; border: 0;
  color: var(--muted); font-size: 17px; line-height: 1;
  height: 44px; padding: 0; cursor: pointer; border-radius: 8px;
}
.row-btn:disabled { opacity: 0.25; cursor: default; }
.row-btn:not(:disabled):hover { background: var(--surface-2); color: var(--text); }

/* The "deals first" marker: a radio drawn as a crown. */
.dealer-pick { position: relative; height: 44px; }
.dealer-pick input {
  position: absolute; inset: 0; margin: 0; opacity: 0; cursor: pointer;
}
.dealer-pick span {
  display: flex; align-items: center; justify-content: center;
  height: 100%; border-radius: 8px;
  border: 1px solid var(--line);
  color: var(--muted); font-size: 15px; font-weight: 800;
}
.dealer-pick input:checked + span {
  background: var(--gold-bg); border-color: var(--gold); color: var(--gold);
}
.dealer-pick input:focus-visible + span { outline: 2px solid var(--accent); }

/* ------------------------------------------------------------------- fields */
.field { margin-bottom: 16px; }
.field-label {
  display: block; font-size: 13px; color: var(--muted); margin-bottom: 7px;
}

.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.segmented label { position: relative; }
.segmented input { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: pointer; }
.segmented span {
  display: flex; align-items: center; justify-content: center; text-align: center;
  min-height: 46px; padding: 6px 8px;
  background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 10px; font-size: 15px; font-weight: 600;
}
.segmented input:checked + span {
  background: var(--accent); border-color: var(--accent); color: var(--on-accent);
}
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.field-check { display: flex; gap: 11px; align-items: flex-start; cursor: pointer; }
.field-check input { width: 22px; height: 22px; margin: 1px 0 0; flex: none; accent-color: var(--accent); }
.field-check b { display: block; font-size: 15px; font-weight: 600; }
.field-check small { display: block; margin-top: 2px; font-size: 13px; color: var(--muted); }

/* ------------------------------------------------------------------ buttons */
.btn {
  appearance: none;
  width: 100%;
  padding: 14px 16px;
  font: inherit;
  font-weight: 600;
  border-radius: 12px;
  border: 1px solid transparent;
  cursor: pointer;
}
.btn-primary { background: var(--accent); color: var(--on-accent); font-weight: 700; }
/* Plainly inert rather than a faded copy of the ready state. */
.btn-primary:disabled {
  background: var(--surface-2); color: var(--muted);
  border-color: var(--line); cursor: default;
}
.btn-ghost   { background: var(--surface-2); color: var(--text); border-color: var(--line); }
.btn-danger  { background: transparent; color: var(--danger); border-color: var(--line); }
.btn-plain   { background: transparent; color: var(--muted); }
.btn-sm      { padding: 10px 14px; font-size: 15px; }

.stack { display: flex; flex-direction: column; gap: 10px; }
#view-home .stack { margin-top: auto; }

.icon-btn {
  appearance: none; background: transparent; border: 0;
  color: var(--text); font-size: 22px; line-height: 1;
  min-width: 40px; height: 40px; padding: 0; cursor: pointer;
}

.chip {
  appearance: none; flex: none;
  max-width: 44%;
  padding: 7px 11px;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 999px;
  font: inherit; font-size: 13px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  cursor: pointer;
}

.hint { margin: 8px 0 0; font-size: 14px; color: var(--muted); min-height: 1.2em; }
.legend { margin: 0 0 2px; font-size: 15px; color: var(--muted); text-align: center; }
.legend .crown { width: 1.3em; height: 1.3em; color: var(--gold); }

/* The drawn crown, sized to its text unless something says otherwise. */
.crown {
  display: inline-block; width: 1.2em; height: 1.2em;
  vertical-align: -0.25em; fill: currentColor;
}
.winner .crown, .hist-won .crown { width: 1.35em; height: 1.35em; margin-right: 6px; }

/* -------------------------------------------------------------------- game */
.appbar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 12px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.appbar-text { flex: 1; min-width: 0; }
.round-label { font-size: 18px; font-weight: 700; letter-spacing: -0.01em; }
.round-label b { color: var(--gold); }
.wild-label  { font-size: 13px; color: var(--muted); }
.wild-label b { color: var(--gold); }

.winner {
  padding: 13px 16px;
  background: var(--gold-bg);
  color: var(--gold);
  font-weight: 700;
  text-align: center;
}

/* The grid scrolls on its own so the header and footer stay put. */
.board-wrap { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; }

.board { border-collapse: separate; border-spacing: 0; width: 100%; }
.board th, .board td { padding: 0; border-bottom: 1px solid var(--line); text-align: center; }

/* Sticky player names across the top, sticky round labels down the left. */
.board thead th {
  position: sticky; top: 0; z-index: 2;
  background: var(--surface);
  padding: 8px 5px;
  border-bottom: 2px solid var(--line);
}
.board th.corner { left: 0; z-index: 3; text-align: left; padding-left: 14px; }
.board td.round-cell, .board th.corner {
  position: sticky; left: 0;
  background: var(--bg);
  text-align: left;
  padding: 0 8px 0 14px;
  white-space: nowrap;
}
.board td.round-cell { z-index: 1; }

.pname  { display: block; font-size: 13px; font-weight: 600;
          overflow: hidden; text-overflow: ellipsis; max-width: 84px; }
.ptotal { display: block; font-size: 19px; font-weight: 700; margin-top: 1px;
          font-variant-numeric: tabular-nums; }
.leading .ptotal, .leading .pname { color: var(--gold); }

.rwild { font-size: 15px; font-weight: 700; font-variant-numeric: tabular-nums; }
.chip b { color: var(--accent); font-weight: 800; }
tr.is-current .rwild { color: var(--gold); }

tr.is-current td, tr.is-current td.round-cell { background: var(--surface-2); }
tr.is-locked .rwild { opacity: 0.45; }

/* Score cells: the input fills the cell so the whole cell is the tap target. */
.cell { position: relative; min-width: var(--cell-w); }
.cell input {
  appearance: none; -moz-appearance: textfield;
  width: 100%; height: 46px;
  padding: 0 2px 0 26px;
  border: 0; background: transparent;
  color: var(--text);
  font: inherit; font-size: 18px; font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: center;
  border-radius: 8px;
}
.cell input::-webkit-outer-spin-button,
.cell input::-webkit-inner-spin-button { appearance: none; margin: 0; }
.cell input:focus { outline: 2px solid var(--accent); outline-offset: -3px; }
.cell input:disabled { color: var(--muted); opacity: 0.55; }

.cell.went-out  input { color: var(--accent); }
.cell.first-out input { color: var(--gold); }
.cell.first-out { background: var(--gold-bg); }
tr.is-current .cell.first-out { background: var(--gold-bg); }

/* Sits over the left edge of the score box; tap it to cycle who went out. */
.out-btn {
  position: absolute; inset: 0 auto 0 0;
  width: 28px; border: 0; background: transparent;
  display: flex; align-items: center; justify-content: center;
  color: var(--muted); opacity: 0.35; cursor: pointer; padding: 0;
}
.out-btn .crown { width: 17px; height: 17px; vertical-align: 0; }
tr.is-current .out-btn   { opacity: 0.8; }
tr.is-locked .out-btn    { opacity: 0; pointer-events: none; }
.cell.went-out  .out-btn { opacity: 1; color: var(--accent); }
.cell.first-out .out-btn { opacity: 1; color: var(--gold); }

.actions {
  padding: 10px 16px calc(env(safe-area-inset-bottom) + 10px);
  background: var(--surface);
  border-top: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 8px;
}
.actions .hint { margin: 0; text-align: center; font-size: 16px; font-weight: 600; }
.actions .hint.is-problem { color: var(--text); }
.actions .hint.flash { animation: hint-flash 0.9s ease-out; }
@keyframes hint-flash {
  0%, 40% { color: var(--danger); transform: scale(1.04); }
  100%    { transform: none; }
}

/* The row after the current one: tapping it finishes the round. */
tr.is-next { cursor: pointer; }
tr.is-next input, tr.is-next .out-btn { pointer-events: none; }

/* ----------------------------------------------------------------- history */
.hist-list { list-style: none; margin: 0; padding: 0; }
.hist-row {
  display: block; width: 100%; text-align: left;
  padding: 14px 16px;
  background: transparent; border: 0; border-bottom: 1px solid var(--line);
  color: inherit; font: inherit; cursor: pointer;
}
.hist-row:hover { background: var(--surface-2); }
.hist-when { font-size: 15px; font-weight: 600; }
.hist-who  { font-size: 13px; color: var(--muted); margin-top: 2px; }
.hist-won  { font-size: 13px; color: var(--gold); margin-top: 3px; font-weight: 600; }

.live-row {
  display: block; width: 100%; text-align: left;
  padding: 12px 13px; margin-bottom: 8px;
  background: var(--surface-2); color: inherit;
  border: 1px solid var(--line); border-radius: 11px;
  font: inherit; cursor: pointer;
}
.live-row:last-child { margin-bottom: 0; }
.live-row:hover { border-color: var(--accent); }
.live-who { font-size: 15px; font-weight: 600; }
.live-at  { font-size: 13px; color: var(--muted); margin-top: 2px; }
.live-go  { font-size: 13px; color: var(--accent); font-weight: 600; margin-top: 3px; }

.empty { padding: 40px 24px; text-align: center; color: var(--muted); }
.strip {
  margin: 0; padding: 11px 16px;
  background: var(--surface-2); color: var(--muted);
  font-size: 13px; text-align: center;
  border-bottom: 1px solid var(--line);
}

/* ------------------------------------------------------------------ sheets */
.sheet {
  width: min(420px, calc(100% - 24px));
  margin: auto auto calc(env(safe-area-inset-bottom) + 12px);
  padding: 18px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 18px;
  max-height: 80dvh;
  overflow: auto;
  flex-direction: column; gap: 9px;
}
.sheet[open] { display: flex; }
.sheet::backdrop { background: rgba(10, 8, 30, 0.55); }
.sheet h2 { margin: 0 0 4px; font-size: 17px; }

.ref { width: 100%; border-collapse: collapse; font-size: 15px; }
.ref td { padding: 8px 2px; border-bottom: 1px solid var(--line); }
.ref td:last-child { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }

.note {
  padding: 11px 13px;
  background: var(--gold-bg); color: var(--gold);
  border-radius: 10px; font-size: 13px; line-height: 1.45;
}

/* --------------------------------------------------- tablets and up */
@media (min-width: 640px) {
  :root { --cell-w: 90px; }
  .out-btn { width: 32px; }
  .out-btn .crown { width: 20px; height: 20px; }
  .cell input { padding-left: 30px; }
  .legend { font-size: 16px; }

  .scroll-view { padding-inline: 24px; gap: 22px; }
  .setup-body { padding: 22px 24px calc(env(safe-area-inset-bottom) + 24px); gap: 22px; }

  /* Home has little on it, so gather it in the middle of a big screen. */
  #view-home { justify-content: center; }
  #view-home .stack, #view-home #live-panel {
    width: 100%; max-width: 440px; margin-inline: auto;
  }
  #view-home .stack { margin-top: 0; }
  .hero h1 { font-size: 34px; }
  .panel { padding: 20px; }
  .appbar { padding: calc(env(safe-area-inset-top) + 14px) 20px 14px; }
  .round-label { font-size: 21px; }
  .wild-label { font-size: 14px; }
  .actions { padding: 14px 20px calc(env(safe-area-inset-bottom) + 14px); }
  .board td.round-cell, .board th.corner { padding-left: 20px; }
  .rwild { font-size: 16px; }
  .cell input { height: 52px; font-size: 19px; }
  .pname { font-size: 14px; max-width: 110px; }
  .ptotal { font-size: 22px; }
  .rnum { font-size: 14px; }

  .hist-row { padding: 16px 20px; }

  /* The two primary actions sit side by side once there is room. */
  .actions { display: grid; grid-template-columns: 1fr 1fr; align-items: end; }
  .actions .legend, .actions .hint { grid-column: 1 / -1; }
  .actions .btn-primary { grid-column: 1; }
  #shape-toggle { grid-column: 2; }
  .actions:has(#shape-toggle[hidden]) .btn-primary { grid-column: 1 / -1; }

  .player-row { grid-template-columns: 36px 36px 1fr 40px 36px; gap: 6px; }
}

/* ------------------------------------------------- laptops: frame the column */
@media (min-width: 900px) {
  #view-setup, #view-game, #view-history, #view-archive, #view-watch {
    border-inline: 1px solid var(--line);
  }
  .board thead th { padding: 10px 6px; }
}

/* ---------------------------------------- short windows (phone in landscape) */
@media (max-height: 520px) {
  .hero { display: none; }
  .scroll-view { padding-top: 16px; gap: 14px; }
  .cell input { height: 40px; }
  .legend { display: none; }
}
