/* Theme tokens. The referee can change these only via validated hex values and
   allowlisted data-attributes (font / shape / pattern) set from app.js. */
:root {
  --bg: #f4efe6; --surface: #fffdf8; --ink: #2b2622; --accent: #d9480f;
  --paper: #ffffff; --paper-ink: #1d1a17;
  --muted: color-mix(in srgb, var(--ink) 55%, transparent);
  --line: color-mix(in srgb, var(--ink) 14%, transparent);
  --radius: 14px;
  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Varela Round", system-ui, sans-serif;
  color-scheme: light;
}
body[data-font="sans"] { --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
body[data-font="serif"] { --font: "Iowan Old Style", Georgia, "Times New Roman", serif; }
body[data-font="mono"] { --font: ui-monospace, Menlo, Consolas, monospace; }
body[data-font="handwritten"] { --font: "Bradley Hand", "Segoe Print", "Chalkboard SE", "Comic Sans MS", cursive; }
body[data-font="slab"] { --font: Rockwell, "Roboto Slab", "Courier New", serif; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100dvh; background-color: var(--bg); color: var(--ink);
  font: 16px/1.45 var(--font);
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
}
body[data-pattern="dots"] { background-image: radial-gradient(color-mix(in srgb, var(--ink) 10%, transparent) 1px, transparent 1.5px); background-size: 18px 18px; }
body[data-pattern="grid"] { background-image: linear-gradient(var(--line) 1px, transparent 1px), linear-gradient(90deg, var(--line) 1px, transparent 1px); background-size: 24px 24px; }
body[data-pattern="stripes"] { background-image: repeating-linear-gradient(135deg, transparent 0 14px, color-mix(in srgb, var(--accent) 8%, transparent) 14px 28px); }
body[data-pattern="confetti"] { background-image: radial-gradient(color-mix(in srgb, var(--accent) 35%, transparent) 2px, transparent 2.5px), radial-gradient(color-mix(in srgb, var(--ink) 18%, transparent) 1.5px, transparent 2px); background-size: 37px 41px, 23px 29px; background-position: 0 0, 11px 7px; }
body[data-pattern="felt"] { background-image: radial-gradient(ellipse at center, color-mix(in srgb, var(--bg) 70%, white) 0%, var(--bg) 70%); background-attachment: fixed; }

#app { max-width: 760px; margin: 0 auto; padding: 12px 16px 48px; display: grid; gap: 14px; }
h1 { font-size: 1.4rem; margin: 0; line-height: 1.2; }
h2 { font-size: 1rem; margin: 0 0 10px; letter-spacing: .02em; }
h3 { font-size: .85rem; margin: 10px 0 6px; color: var(--muted); }
p { margin: 0 0 6px; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: 0 1px 0 var(--line); min-width: 0; }
.hero { display: flex; gap: 14px; align-items: center; padding: 16px 0 4px; }
.hero h1 { font-size: 1.8rem; }
.stack { display: grid; gap: 10px; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row > input, .row > select { flex: 1; min-width: 0; }
label { display: flex; gap: 6px; align-items: center; font-size: .9rem; }
label.check { align-items: flex-start; }
.row label input { width: 5em; }

input, textarea, select, button { font: inherit; color: inherit; }
input, textarea, select {
  background: color-mix(in srgb, var(--surface) 70%, var(--bg)); border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; width: 100%;
}
input[type=checkbox] { width: auto; margin-top: 3px; accent-color: var(--accent); }
button {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 8px 14px; cursor: pointer;
  min-height: 40px; touch-action: manipulation;
}
button:hover { border-color: var(--accent); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: var(--accent); color: #fff; border-color: transparent; font-weight: 600; }
button.ghost { background: transparent; }
button.link { background: none; border: 0; color: var(--muted); text-decoration: underline; min-height: 0; padding: 4px; }
button.icon { border-radius: 12px; padding: 6px 10px; }
button.small { min-height: 32px; padding: 4px 12px; font-size: .85rem; }
button.action { border-color: var(--accent); color: var(--accent); font-weight: 600; }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 2px; }

.games { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.games li { display: flex; align-items: center; gap: 4px; }
.games li button { margin-left: auto; }

.topbar { display: flex; align-items: center; gap: 8px; position: sticky; top: 0; z-index: 5; padding: 8px 0; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(6px); }
.topbar .tt { flex: 1; min-width: 0; }
.topbar h1 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back { text-decoration: none; font-size: 1.4rem; padding: 4px 8px; }

.banner { border-radius: var(--radius); padding: 12px 14px; border: 1px solid var(--line); background: var(--surface); display: grid; gap: 6px; }
.banner.thinking { display: flex; align-items: center; gap: 10px; }
.banner.vote, .banner.spot { border: 2px solid var(--accent); }
.banner.win { background: var(--accent); color: #fff; font-size: 1.15rem; font-weight: 700; }
.banner.soft { background: transparent; }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin 0.9s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }

/* Cards */
.cards-row { display: flex; gap: 10px; overflow-x: auto; padding: 4px 2px 10px; scroll-snap-type: x proximity; }
.card {
  --w: 168px;
  flex: none; width: var(--w); aspect-ratio: 5 / 7; display: flex; flex-direction: column; gap: 4px; text-align: left;
  background: var(--paper); color: var(--paper-ink); border: 1.5px solid color-mix(in srgb, var(--paper-ink) 25%, transparent);
  border-radius: 12px; padding: 8px; box-shadow: 0 2px 6px color-mix(in srgb, var(--ink) 18%, transparent);
  scroll-snap-align: start; overflow: hidden; transition: transform .12s;
}
.card.small { --w: 120px; padding: 6px; font-size: .78rem; }
body[data-shape="sharp"] .card { border-radius: 2px; }
body[data-shape="wobbly"] .card { border-radius: 255px 15px 225px 15px / 15px 225px 15px 255px; }
body[data-shape="ticket"] .card { border-style: dashed; border-radius: 6px; }
.c-title { font-weight: 700; line-height: 1.15; }
.card img, .c-blank { width: 100%; aspect-ratio: 600 / 420; object-fit: contain; border-radius: 6px; background: color-mix(in srgb, var(--paper-ink) 4%, transparent); }
.c-blank { display: grid; place-items: center; font-size: 1.6rem; color: color-mix(in srgb, var(--paper-ink) 25%, transparent); }
.c-rules { font-size: .82em; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 6; -webkit-box-orient: vertical; }
.c-by { margin-top: auto; font-size: .7rem; opacity: .6; }

.ruling-body { display: flex; gap: 12px; align-items: flex-start; }
.ruling-body > div { min-width: 0; }
.effects { margin: 6px 0; padding-left: 18px; font-size: .9rem; }
.rules { margin: 0 0 8px; padding-left: 20px; }
.scores { width: 100%; border-collapse: collapse; font-size: .92rem; }
.scores th, .scores td { padding: 6px 4px; border-bottom: 1px solid var(--line); text-align: left; }
.scores .num { text-align: right; font-variant-numeric: tabular-nums; }
.scores tr.turn { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.hand .target { width: auto; flex: 1 1 160px; }

.log { list-style: none; margin: 0 0 10px; padding: 0; display: grid; gap: 6px; max-height: 50vh; overflow-y: auto; font-size: .9rem; }
.log li.system { color: var(--muted); font-style: italic; }
.log li.play { font-weight: 600; }
.log li.ruling-log ul { margin: 2px 0 0; padding-left: 18px; font-weight: 400; }
.log q { display: block; font-weight: 400; }
.media { display: block; max-width: 100%; margin-top: 4px; border-radius: 8px; }
img.media, video.media { max-height: 280px; }

/* Dialogs */
dialog { border: 0; border-radius: 18px; padding: 0; background: var(--surface); color: var(--ink); width: min(640px, calc(100vw - 16px)); max-height: calc(100dvh - 16px); }
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
dialog footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.x { border: 0; background: none; font-size: 1.2rem; min-height: 0; }
.editor form, .recorder, .viewer.pad { padding: 14px; }
.viewer { background: transparent; width: auto; display: grid; justify-items: center; gap: 10px; }
.viewer:not(.pad) { box-shadow: none; }
.viewer.pad { background: var(--surface); width: min(480px, calc(100vw - 16px)); justify-items: stretch; }
.viewer .card { --w: min(320px, 80vw); font-size: 1rem; }

.card-edit { background: var(--paper); color: var(--paper-ink); border-radius: 14px; padding: 10px; display: grid; gap: 8px; border: 1px solid var(--line); }
.card-edit input, .card-edit textarea { background: transparent; color: var(--paper-ink); }
.title-in { font-weight: 700; font-size: 1.1rem; }
.canvas-wrap { border: 1px dashed color-mix(in srgb, var(--paper-ink) 30%, transparent); border-radius: 8px; overflow: hidden; }
canvas.draw { display: block; width: 100%; height: auto; aspect-ratio: 600 / 420; touch-action: none; cursor: crosshair; background: var(--paper); }
.toolbar { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 10px 0; }
.toolbar .sep { width: 8px; }
.swatch { width: 32px; height: 32px; min-height: 0; padding: 0; border-radius: 50%; background: var(--sw); border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--line); }
.swatch.on { box-shadow: 0 0 0 3px var(--ink); }
.size { width: 36px; min-height: 36px; padding: 0; display: grid; place-items: center; }
.size.on, .tool.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--surface)); }
.dot { display: block; border-radius: 50%; background: var(--ink); }
.dot0 { width: 4px; height: 4px; } .dot1 { width: 9px; height: 9px; } .dot2 { width: 16px; height: 16px; }
.tool { min-height: 36px; padding: 4px 10px; font-size: .85rem; }
.err { color: #c92a2a; min-height: 1.2em; margin: 4px 0 0; }
.live, .playback video { width: 100%; border-radius: 10px; background: #000; }
.playback audio { width: 100%; }

#toast { position: fixed; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom)); transform: translate(-50%, 30px); opacity: 0; background: var(--ink); color: var(--surface); padding: 10px 16px; border-radius: 999px; transition: .2s; pointer-events: none; max-width: calc(100vw - 32px); z-index: 50; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 480px) {
  .card { --w: 140px; }
  .ruling-body { flex-direction: column; }
  .hero h1 { font-size: 1.5rem; }
}

.invite { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; margin: 8px 0; }
.invite img { background: #fff; padding: 6px; border-radius: 10px; flex: none; }
.invite > div { flex: 1 1 180px; }
.code { font: 700 1.4rem/1.2 ui-monospace, Menlo, monospace; letter-spacing: .08em; user-select: all; }

/* Near-full-screen card viewer */
dialog.bigcard {
  width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0;
  background: transparent; color: #fff; display: flex; flex-direction: column;
  padding: calc(8px + env(safe-area-inset-top)) 12px calc(12px + env(safe-area-inset-bottom));
}
dialog.bigcard:not([open]) { display: none; }
dialog.bigcard::backdrop { background: rgb(10 8 6 / .82); backdrop-filter: blur(3px); }
.bigcard header { display: flex; justify-content: space-between; align-items: center; min-height: 40px; margin: 0; }
.bigcard header .muted { color: rgb(255 255 255 / .7); }
.bigcard .x { color: #fff; font-size: 1.5rem; }
.big-mid { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; gap: 4px; }
.big-stage { flex: 1; min-width: 0; height: 100%; display: grid; place-items: center; touch-action: pan-y; }
.card.big {
  --w: min(calc(100vw - 32px), 520px, calc((100dvh - 230px) * 5 / 7));
  width: var(--w); aspect-ratio: auto; min-height: calc(var(--w) * 1.25); max-height: 100%;
  padding: 18px; gap: 12px; border-radius: 22px; font-size: 1.1rem; overflow-y: auto; cursor: default;
}
body[data-shape="sharp"] .card.big { border-radius: 4px; }
.card.big .c-title { font-size: 1.6rem; margin: 0; }
.card.big .c-rules { display: block; -webkit-line-clamp: unset; overflow: visible; font-size: 1.15rem; line-height: 1.45; margin: 0; white-space: pre-wrap; }
.card.big .c-blank { font-size: 3rem; }
.card.big .c-by { font-size: .85rem; }
.nav { flex: none; width: 40px; height: 64px; padding: 0; border-radius: 12px; font-size: 2rem; line-height: 1; background: rgb(255 255 255 / .12); color: #fff; border-color: transparent; }
.nav:disabled { opacity: .2; }
.big-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; padding-top: 12px; }
.big-actions .target { flex: 1 1 220px; max-width: 520px; background: var(--surface); color: var(--ink); }
.big-actions .muted { color: rgb(255 255 255 / .75); }
.big-play { min-height: 48px; padding: 10px 22px; font-size: 1.05rem; }
@media (max-width: 480px) {
  .nav { display: none; }
  .big-actions .target, .big-play { flex: 1 1 100%; }
}

/* Blank cards */
.card.blank { border-style: dashed; background: color-mix(in srgb, var(--paper) 85%, var(--bg)); }
.card.blank .c-title, .card.blank .c-rules { opacity: .6; }
.card.blank .c-blank { color: var(--accent); background: transparent; }

/* Lobby + home */
.prep { border-top: 1px solid var(--line); margin-top: 12px; padding-top: 4px; }
details.house { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; display: grid; gap: 8px; }
details.house[open] { display: grid; }
details.house summary { cursor: pointer; font-size: .9rem; }

/* Aftergame */
.award { font-weight: 700; color: var(--accent); }
.fate-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; margin-top: 10px; }
.fate-item { display: grid; gap: 4px; justify-items: start; }
.fate { display: flex; flex-wrap: wrap; gap: 4px; }
.fate button { min-height: 34px; padding: 4px 10px; font-size: .85rem; }
.big-actions .fate { justify-content: center; }
.big-actions .fate button { color: var(--ink); }
.big-actions .fate button.primary { color: #fff; }

/* Rules window */
.rulesdlg { width: min(560px, calc(100vw - 16px)); }
.rb-sec { border-top: 1px solid var(--line); padding: 8px 0 4px; }
.rb-sec:first-of-type { border-top: 0; }
.rb-sec h3 { margin-top: 2px; }
.rb-sec ul, .rb-sec ol { margin: 0; padding-left: 20px; display: grid; gap: 4px; font-size: .95rem; }
.rulebook { white-space: pre-wrap; background: color-mix(in srgb, var(--accent) 7%, var(--surface)); border-radius: 10px; padding: 10px 12px; font-size: .95rem; }
.fate-item { min-width: 0; }
.fate-item .card { --w: 100%; }
.fate-item .fate { display: grid; grid-template-columns: 1fr 1fr auto; width: 100%; }
.fate-item .fate button { padding: 4px 6px; font-size: .8rem; white-space: nowrap; }
.fate-item > p { margin: 0; min-height: 1.2em; }

/* Minigames */
.warn { color: color-mix(in srgb, #b35c00 85%, var(--ink)); }
.mg-opt { border: 1px dashed var(--accent); border-radius: 10px; padding: 8px 10px; display: grid; gap: 4px; }
.c-mg { font-size: .72em; font-weight: 700; color: var(--accent); }
.c-mg.failed { color: #c92a2a; }
.card.building { opacity: .65; }
.card.building .c-mg { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .4; } }
@media (prefers-reduced-motion: reduce) { .card.building .c-mg { animation: none; } }
.card.big .c-mg { font-size: .9rem; }
.mg-how { font-size: .95rem; margin: 0; padding: 8px 10px; border-radius: 8px; background: color-mix(in srgb, var(--accent) 8%, transparent); }
dialog.mgdlg { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom); background: #000; color: #fff; display: flex; flex-direction: column; }
dialog.mgdlg:not([open]) { display: none; }
.mgdlg header { display: flex; gap: 10px; align-items: center; padding: 6px 10px; margin: 0; }
.mgdlg header strong { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mgdlg header .muted { color: rgb(255 255 255 / .6); }
.mgdlg header button { color: #fff; border-color: rgb(255 255 255 / .3); }
.mg-frame { flex: 1; width: 100%; border: 0; background: #111; }

/* Deck picker */
.pickdlg { width: min(760px, calc(100vw - 16px)); display: grid; gap: 10px; }
.pickdlg header { margin: 0; }
.chips .chip { min-height: 32px; padding: 4px 12px; font-size: .85rem; }
.chip.on { background: var(--accent); color: #fff; border-color: transparent; }
.pick-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 10px; max-height: 52dvh; overflow-y: auto; padding: 4px; }
.pick { position: relative; }
.pick .card { --w: 100%; }
.pick.on .card { outline: 3px solid var(--accent); outline-offset: 2px; }
.pick:not(.on) .card { opacity: .72; }
.pick .tick { position: absolute; top: -6px; right: -6px; width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; background: var(--accent); color: #fff; font-weight: 700; font-size: .9rem; pointer-events: none; }
.pick:not(.on) .tick { display: none; }
.pick .peek { position: absolute; bottom: 6px; right: 6px; min-height: 0; width: 30px; height: 30px; padding: 0; border-radius: 50%; font-size: .85rem; background: var(--surface); }
.pickdlg footer { align-items: center; margin: 0; }
.pickdlg .grow { flex: 1; }
.pick-grid { max-height: max(220px, calc(100dvh - 440px)); }
.pickdlg footer { flex-wrap: nowrap; gap: 6px; }
.pickdlg footer button { padding: 8px 12px; white-space: nowrap; }
.pick-count { white-space: nowrap; }

/* Card secrets */
.secret-box summary { cursor: pointer; font-size: .9rem; color: color-mix(in srgb, var(--paper-ink) 70%, transparent); }
.secret-in { margin-top: 6px; background: color-mix(in srgb, var(--paper-ink) 5%, transparent) !important; }
.c-secret { margin: 0; padding: 8px 10px; border-radius: 8px; font-size: .95rem; background: color-mix(in srgb, var(--paper-ink) 7%, transparent); white-space: pre-wrap; }
.c-secret.locked { font-style: italic; opacity: .75; }

/* Minigame bug reports */
.mgdlg { position: relative; }
.mg-err { color: #ffb4a8; font-size: .8rem; white-space: nowrap; }
.mg-bug { position: absolute; left: 12px; right: 12px; bottom: calc(16px + env(safe-area-inset-bottom)); background: var(--surface); color: var(--ink); border-radius: 16px; padding: 14px; display: grid; gap: 8px; box-shadow: 0 10px 40px rgb(0 0 0 / .5); }
.mg-bug[hidden] { display: none; }
.mg-bug h3 { color: var(--ink); margin: 0; font-size: 1rem; }
.mg-bug button { color: var(--ink); }
.mg-bug button.primary { color: #fff; }

/* Starting rules */
.house-list { display: grid; gap: 6px; margin-bottom: 8px; }
.house-row { display: flex; gap: 4px; align-items: flex-start; }
.house-row textarea { flex: 1; }
.rs-list { display: grid; gap: 8px; max-height: 60dvh; overflow-y: auto; }
.rs { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
.rs summary { cursor: pointer; }
.rs ol { margin: 6px 0; padding-left: 20px; }
.rs > button { margin-top: 6px; }
ol.rules.small { font-size: .9rem; }
