:root {
  --ink: #fdf8ee;
  --muted: #cfd8c8;
  --panel: rgba(28, 38, 30, 0.78);
  --line: rgba(255, 255, 255, 0.14);
  --gold: #f6c431;
  --accent: #8fd16a;
  --danger: #ff8a73;
  --radius: 12px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: #2b3a2f;
  color: var(--ink);
  font-family: 'Avenir Next', 'Segoe UI', system-ui, -apple-system, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation; /* no double-tap page zoom on buttons */
  overscroll-behavior: none;
}
body { height: 100dvh; }

button { font: inherit; color: inherit; cursor: pointer; }

.app { position: relative; height: 100%; min-height: 320px; overflow: hidden; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.stage { position: absolute; inset: 0; }
.stage canvas { display: block; width: 100%; height: 100%; touch-action: none; }

/* ------------------------------------------------------------ top bar -- */

.topbar {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; gap: 12px;
  padding: 8px 12px;
  padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  background: linear-gradient(rgba(20, 28, 22, 0.75), rgba(20, 28, 22, 0));
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }
.brand { font-weight: 800; letter-spacing: 0.04em; font-size: 1.15rem; text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5); }
.status { flex: 1; text-align: center; font-weight: 600; text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status .threat { display: inline-flex; gap: 3px; margin-left: 8px; vertical-align: middle; }
.status .threat i { width: 9px; height: 9px; border-radius: 50%; background: rgba(255, 255, 255, 0.25); }
.status .threat i.on { background: #e5533b; box-shadow: 0 0 6px #e5533b; }
.actions { display: flex; gap: 6px; }

.icon-btn {
  width: 34px; height: 34px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--panel);
  display: inline-grid; place-items: center; font-size: 1rem; line-height: 1;
}
@media (hover: hover) { .icon-btn:hover { background: rgba(60, 80, 62, 0.9); } }

/* ------------------------------------------------------------ scores -- */

.scores {
  position: absolute; top: calc(52px + env(safe-area-inset-top)); left: max(10px, env(safe-area-inset-left));
  display: flex; flex-direction: column; gap: 4px;
  min-width: 190px;
}
.score-row {
  display: grid; grid-template-columns: 12px 1fr; align-items: center; gap: 8px;
  padding: 6px 10px; border-radius: 10px;
  background: var(--panel); border: 1px solid var(--line);
  font-size: 0.9rem; cursor: pointer;
}
.score-row.active { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold) inset; }
.score-row .dot { width: 12px; height: 12px; border-radius: 50%; box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.5); }
.score-row .who { font-weight: 700; white-space: nowrap; }
.score-row .meta { grid-column: 2 / 3; font-size: 0.75rem; color: var(--muted); display: flex; gap: 10px; }

/* ------------------------------------------------------------ prompt -- */

.prompt {
  /* left/right + auto margins rather than left: 50% so long prompts can use the full width before wrapping. */
  position: absolute; left: 0; right: 0; margin: 0 auto; width: fit-content; bottom: max(14px, env(safe-area-inset-bottom));
  max-width: min(860px, calc(100% - 20px));
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: center;
  padding: 10px 14px; border-radius: 14px;
  background: var(--panel); border: 1px solid var(--line);
  backdrop-filter: blur(6px);
  font-size: 0.95rem;
}
.prompt:empty { display: none; }
.prompt .hint { color: var(--muted); font-size: 0.82rem; }
.prompt kbd { font: inherit; font-size: 0.78rem; padding: 0 5px; border-radius: 4px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.08); }

.btn {
  padding: 7px 14px; border-radius: 999px;
  border: 1px solid var(--line); background: rgba(255, 255, 255, 0.1);
  font-weight: 700;
}
.btn.primary { background: var(--accent); color: #183015; border-color: transparent; }
@media (hover: hover) {
  .btn:hover { background: rgba(255, 255, 255, 0.2); }
  .btn.primary:hover { background: var(--accent); filter: brightness(1.08); }
}

/* Touch: Rotate / Flip / Place / Cancel while holding a plant, just above the prompt bar. */
.holdbar {
  position: absolute; left: 0; right: 0; margin: 0 auto; width: fit-content;
  bottom: calc(max(14px, env(safe-area-inset-bottom)) + 64px);
  display: flex; gap: 8px; padding: 6px; border-radius: 999px;
  background: var(--panel); border: 1px solid var(--line); backdrop-filter: blur(6px);
}
.holdbar[hidden] { display: none; }
.holdbar .btn { padding: 10px 16px; }
.btn:disabled { opacity: 0.45; cursor: default; }

/* ------------------------------------------------------------- labels -- */

.labels { position: absolute; inset: 0; pointer-events: none; }
.mat-label {
  position: absolute; transform: translate(-50%, -100%);
  padding: 2px 9px; border-radius: 999px;
  font-size: 0.8rem; font-weight: 700; white-space: nowrap;
  background: rgba(20, 28, 22, 0.7); border: 1px solid var(--line);
}
.mat-label.turn { border-color: var(--gold); color: var(--gold); }

/* --------------------------------------------------------------- zoom -- */

.zoom {
  position: absolute; right: 12px; bottom: 12px;
  pointer-events: none; opacity: 0; transition: opacity 0.12s;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
}
.zoom.show { opacity: 1; }
.zoom img { width: 230px; border-radius: 10px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.55); }
.zoom img.tile { width: auto; max-width: 230px; max-height: 230px; border-radius: 3px; }
.zoom img.scorecard { width: 300px; border-radius: 3px; }
.zoom .cap { padding: 4px 10px; border-radius: 8px; background: var(--panel); font-size: 0.85rem; max-width: 260px; text-align: center; }

/* ------------------------------------------- season / final scoring -- */

.ceremony {
  position: absolute; right: 5%; top: 50%; transform: translateY(-50%);
  width: min(340px, 34vw); padding: 22px 24px; border-radius: 18px;
  background: var(--panel); border: 1px solid var(--line); box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
  text-align: center; pointer-events: none;
  animation: ceremony-in 0.45s ease-out;
}
.ceremony[hidden] { display: none; }
.ceremony h2 { margin: 0 0 4px; font-size: 2rem; letter-spacing: 0.01em; }
.ceremony .kicker { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted); }
.ceremony img { width: 120px; margin: 12px auto 6px; display: block; border-radius: 50%; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.4); }
.ceremony .token { font-weight: 700; font-size: 1.05rem; }
.ceremony .places { margin-top: 6px; color: var(--muted); font-size: 0.9rem; }
.ceremony .now { margin-top: 14px; min-height: 1.6em; font-size: 1.15rem; font-weight: 700; }
.ceremony .now span { display: inline-block; animation: ceremony-line 0.3s ease-out; }
@keyframes ceremony-in { from { opacity: 0; transform: translateY(calc(-50% + 12px)); } }
@keyframes ceremony-line { from { opacity: 0; transform: translateY(6px); } }

/* ------------------------------------------------------- toast / load -- */

.toast {
  position: absolute; left: 50%; top: 64px; transform: translate(-50%, -10px);
  padding: 8px 16px; border-radius: 999px; background: rgba(120, 30, 20, 0.92);
  opacity: 0; transition: opacity 0.2s, transform 0.2s; pointer-events: none; font-weight: 600;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.info { background: rgba(30, 60, 36, 0.94); }
.loading { position: absolute; inset: 0; display: grid; place-items: center; font-size: 1.3rem; background: #2b3a2f; }
.loading[hidden] { display: none; }

/* --------------------------------------------------------------- log -- */

.log-panel {
  position: absolute; top: 52px; right: 10px; bottom: 80px; width: min(340px, 90vw);
  display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  backdrop-filter: blur(6px);
}
.log-panel[hidden] { display: none; }
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 10px; border-bottom: 1px solid var(--line); }
.panel-head h2 { margin: 0; font-size: 1rem; }
.log-list { list-style: none; margin: 0; padding: 8px 12px; overflow: auto; font-size: 0.85rem; display: flex; flex-direction: column; gap: 4px; user-select: text; }
.log-list .round { margin-top: 6px; color: var(--gold); font-weight: 700; }
.log-list .season { color: var(--accent); font-weight: 700; }
.log-list .threat { color: var(--danger); font-weight: 700; }
.log-list .end { color: var(--gold); font-weight: 800; }

/* ------------------------------------------------------------- modals -- */

.modal-root:empty { display: none; }
.modal-root { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(10, 16, 12, 0.55); z-index: 10; padding: 16px; }
.modal {
  max-width: min(920px, 100%); max-height: 100%; overflow: auto;
  padding: 20px 22px; border-radius: 16px;
  background: #24322a; border: 1px solid var(--line); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.modal h2 { margin: 0 0 6px; font-size: 1.35rem; }
.modal p { margin: 6px 0 12px; color: var(--muted); }
.modal .row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 14px; }
.modal.small { max-width: 460px; text-align: center; }

/* Docked: bottom of the screen, table left undimmed and clickable around it. */
.modal-root.dock { place-items: end center; background: none; pointer-events: none; }
.modal-root.dock .modal { pointer-events: auto; max-height: 62vh; padding: 12px 16px 14px; background: rgba(36, 50, 42, 0.94); box-shadow: 0 -6px 30px rgba(0, 0, 0, 0.35); }
.modal.docked h2 { font-size: 1.15rem; }
.modal.docked p { margin: 2px 0 8px; }
.modal.docked .card-pick img { width: min(180px, 26vw, 27vh); }
.season-strip { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin: 0 0 10px; }
.season-tok { display: flex; align-items: center; gap: 8px; padding: 5px 10px 5px 5px; border-radius: 30px; background: rgba(255, 255, 255, 0.07); max-width: 250px; }
.season-tok img { width: 46px; height: 46px; border-radius: 50%; flex: none; }
.season-tok div { display: flex; flex-direction: column; line-height: 1.15; font-size: 0.85rem; }
.season-tok span { color: var(--text, #fff); }
.season-tok small { color: var(--muted); }
.season-tok.past { opacity: 0.4; }

.card-pick { display: flex; gap: 14px; justify-content: center; flex-wrap: wrap; }
.card-pick button { padding: 0; border: 3px solid transparent; border-radius: 12px; background: none; transition: transform 0.12s, border-color 0.12s; }
@media (hover: hover) { .card-pick button:hover { transform: translateY(-4px); border-color: var(--gold); } }
.card-pick img { display: block; width: 210px; border-radius: 9px; }
.card-one { width: 220px; border-radius: 10px; display: block; margin: 0 auto; }

.title { font-size: 2.6rem; font-weight: 900; letter-spacing: 0.03em; margin: 0; text-align: center; }
.subtitle { text-align: center; color: var(--muted); margin: 2px 0 16px; }
.seats { display: flex; flex-direction: column; gap: 8px; }
.seat { display: grid; grid-template-columns: 26px 1fr auto auto; gap: 10px; align-items: center; padding: 8px 10px; border-radius: 10px; background: rgba(255, 255, 255, 0.05); }
.seat.off { opacity: 0.35; }
.seat input[type=text] { font: inherit; color: inherit; background: rgba(0, 0, 0, 0.25); border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; min-width: 0; }
.swatches { display: flex; gap: 4px; }
.swatch { width: 22px; height: 22px; border-radius: 50%; border: 2px solid transparent; padding: 0; }
.swatch.on { border-color: #fff; }
.swatch:disabled { opacity: 0.2; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; }
.seg button { border: 0; background: none; padding: 5px 11px; font-size: 0.85rem; }
.seg button.on { background: var(--accent); color: #183015; font-weight: 700; }
.count-row { display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.count-row .hint { color: var(--muted); font-size: 0.82rem; flex-basis: 100%; text-align: center; margin-top: -4px; }
.seats + .count-row { margin-top: 14px; }

.results { border-collapse: collapse; margin: 8px auto; font-variant-numeric: tabular-nums; }
.results th, .results td { padding: 5px 10px; text-align: right; border-bottom: 1px solid var(--line); }
.results th:first-child, .results td:first-child { text-align: left; }
.results tr.win td { color: var(--gold); font-weight: 800; }
.results td.total { font-weight: 800; }

.help { max-width: 640px; }
.help h3 { margin: 14px 0 4px; font-size: 1rem; color: var(--gold); }
.help ul { margin: 4px 0; padding-left: 20px; color: var(--muted); }
.help li { margin: 3px 0; }
.help a { color: var(--accent); }
.settings label { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 6px 0; }

.noscript { padding: 20px; }

@media (max-width: 720px) {
  .scores { min-width: 0; font-size: 0.8rem; }
  .score-row .meta { display: none; }
  .brand { display: none; }
  .card-pick img { width: min(150px, 40vw); }
}

/* ---------------------------------------------------- phones / touch -- */

body.touch .btn { padding: 9px 15px; }
body.touch .seg button { padding: 8px 13px; }
body.touch input[type=text] { font-size: 16px; } /* iOS zooms the page into smaller inputs */

/* Small screens (phones in either orientation). */
@media (max-width: 720px), (max-height: 520px) {
  .topbar { gap: 8px; }
  .status { font-size: 0.85rem; text-align: left; }
  .status .hint { display: none; }
  .actions { gap: 4px; }
  .icon-btn { width: 36px; height: 36px; }
  .prompt { font-size: 0.88rem; padding: 8px 10px; gap: 4px 8px; bottom: max(8px, env(safe-area-inset-bottom)); max-width: calc(100% - 12px); }
  .prompt .btn { padding: 7px 13px; }
  .prompt .hint { font-size: 0.78rem; }
  .holdbar { bottom: calc(max(8px, env(safe-area-inset-bottom)) + 58px); }
  .score-row { padding: 4px 8px; }
  .mat-label { font-size: 0.72rem; padding: 1px 7px; }
  .zoom { left: 50%; right: auto; top: calc(56px + env(safe-area-inset-top)); bottom: auto; transform: translateX(-50%); }
  .zoom img { width: min(230px, 60vw, 45vh); }
  .zoom img.tile { max-width: min(200px, 50vw); max-height: min(200px, 40vh); }
  .zoom img.scorecard { width: min(300px, 90vw, 60vh); }
  .toast { top: calc(56px + env(safe-area-inset-top)); max-width: calc(100% - 24px); text-align: center; }
  .log-panel { top: calc(50px + env(safe-area-inset-top)); bottom: 70px; right: max(6px, env(safe-area-inset-right)); }
  .modal-root { padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left)); }
  .modal { padding: 14px 14px; }
  .modal-root.dock .modal { padding: 10px 12px 12px; }
  .modal.docked h2 { font-size: 1rem; }
  .modal.docked p { font-size: 0.85rem; }
  .season-tok { max-width: none; padding-right: 8px; }
  .season-tok img { width: 34px; height: 34px; }
  .season-tok div { font-size: 0.75rem; }
  .title { font-size: 2rem; }
  .seat { grid-template-columns: 22px 1fr auto; row-gap: 6px; }
  .seat .seg { grid-column: 3; grid-row: 1; }
  .seat .swatches { grid-column: 2 / 4; grid-row: 2; }
  .score-row .meta { display: none; }
  .card-pick { gap: 8px; }
  .modal.docked .card-pick img { width: min(180px, 25vw, 27vh); }
  .ceremony { padding: 12px 14px; }
  .ceremony h2 { font-size: 1.3rem; }
  .ceremony img { width: 56px; margin: 6px auto 2px; }
  .ceremony .token { font-size: 0.92rem; }
  .ceremony .places { font-size: 0.8rem; margin-top: 2px; }
  .ceremony .now { margin-top: 6px; font-size: 1rem; }
}
/* Small screens: the scoring panel replaces the prompt bar while it's up. */
@media (max-width: 720px), (max-height: 520px) {
  body:has(#ceremony:not([hidden])) :is(.prompt, .holdbar) { visibility: hidden; }
}
/* Portrait phones: season / final scoring becomes a strip along the bottom; the camera frames the scorecard above it. */
@media (max-width: 720px) {
  .ceremony {
    left: 50%; right: auto; top: auto; bottom: max(8px, env(safe-area-inset-bottom)); transform: translateX(-50%);
    width: min(420px, calc(100% - 16px)); padding: 10px 14px;
    animation-name: ceremony-in-low;
  }
}
@keyframes ceremony-in-low { from { opacity: 0; transform: translate(-50%, 12px); } }
/* Short landscape phones: a docked season-strip would hide most of the table. */
@media (max-height: 520px) {
  .holdbar { left: auto; right: max(8px, env(safe-area-inset-right)); top: 50%; bottom: auto; margin: 0; transform: translateY(-50%); flex-direction: column; border-radius: 20px; }
  .modal.docked .season-strip { display: none; }
  .modal.docked p { display: none; }
}
