:root {
  --bg: #f6f4ef;
  --panel: #ffffff;
  --ink: #1f1d1a;
  --muted: #6f6a60;
  --line: #e2ddd2;
  --accent: #2f6d5a;
  --accent-ink: #ffffff;
  --treasure: #d9a81e;
  --victory: #4c9a3c;
  --action: #9a958b;
  --curse: #7a4fa0;
  --attack: #c0503b;
  --reaction: #3d7cc9;
  --duration: #e08a2c;
  --night: #3b3f56;
  --chip-bg: #f1ede4;
  --bar: #2f6d5a;
  --bar-bg: #e7e2d8;
  --warn: #9a5b00;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4; --s6: #008300;
  --c-treasure: #eda100; --c-action: #2a78d6; --c-victory: #008300; --c-curse: #6250d6; --c-other: #898781;
  --viz-grid: #e1e0d9; --viz-axis: #c3c2b7; --ink-muted: #898781;
  --lc-treasure: #946d00; --lc-victory: #2e7d1e; --lc-curse: #7a4fa0; --lc-duration: #b0590b; --lc-reaction: #2f68b3;
  color-scheme: light;
}
/* Dark palette: follows the system setting unless the toggle picks light or dark. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17181a;
    --panel: #212327;
    --ink: #ece9e2;
    --muted: #9c978d;
    --line: #34373d;
    --accent: #5fb398;
    --accent-ink: #0f1311;
    --chip-bg: #2b2e33;
    --bar: #5fb398;
    --bar-bg: #34373d;
    --warn: #e0a33f;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #0ca30c;
    --c-treasure: #c98500; --c-action: #3987e5; --c-victory: #0ca30c; --c-curse: #9085e9; --c-other: #898781;
    --viz-grid: #2c2c2a; --viz-axis: #4a4c52; --ink-muted: #9c978d; --lc-treasure: #e3b53a; --lc-victory: #6cc35a; --lc-curse: #b597e6; --lc-duration: #f0a050; --lc-reaction: #7fb0f0;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
    --bg: #17181a;
    --panel: #212327;
    --ink: #ece9e2;
    --muted: #9c978d;
    --line: #34373d;
    --accent: #5fb398;
    --accent-ink: #0f1311;
    --chip-bg: #2b2e33;
    --bar: #5fb398;
    --bar-bg: #34373d;
    --warn: #e0a33f;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #0ca30c;
    --c-treasure: #c98500; --c-action: #3987e5; --c-victory: #0ca30c; --c-curse: #9085e9; --c-other: #898781;
    --viz-grid: #2c2c2a; --viz-axis: #4a4c52; --ink-muted: #9c978d; --lc-treasure: #e3b53a; --lc-victory: #6cc35a; --lc-curse: #b597e6; --lc-duration: #f0a050; --lc-reaction: #7fb0f0;
    color-scheme: dark;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); }
code { background: var(--chip-bg); padding: 0 4px; border-radius: 4px; }
.muted { color: var(--muted); font-weight: normal; }
.spacer { flex: 1; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; letter-spacing: .02em; }
.turn-info { font-weight: 600; }
.conn { width: 10px; height: 10px; border-radius: 50%; background: #b33; }
.conn.live { background: #3a3; }
select, input, textarea {
  font: inherit; color: inherit; background: var(--bg);
  border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px;
}
.btn {
  font: inherit; cursor: pointer; text-decoration: none; color: var(--ink);
  background: var(--chip-bg); border: 1px solid var(--line); border-radius: 6px; padding: 5px 10px;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: #c0392b; }

main { padding: 14px 16px 40px; }
.empty { max-width: 640px; margin: 60px auto; text-align: center; }
.section-title { font-weight: 700; margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .05em; }

.supply-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 14px; }
.supply { display: flex; flex-wrap: wrap; gap: 6px; }

.players { display: grid; grid-template-columns: repeat(auto-fit, minmax(360px, 1fr)); gap: 14px; }
.player { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px; min-width: 0; }
.player.current { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.player-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.player-name { font-size: 18px; font-weight: 700; }
.badge { font-size: 11px; padding: 1px 7px; border-radius: 99px; background: var(--chip-bg); border: 1px solid var(--line); }
.badge.me { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.badge.turn { border-color: var(--accent); color: var(--accent); }

.tile-groups { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; margin-bottom: 10px; }
.tile-group-name { font-size: 10.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 3px; }
.tile-group .tiles { grid-template-columns: repeat(auto-fit, minmax(66px, 1fr)); margin-bottom: 0; }
.tile-group .tile .v { font-size: 16px; }
.tile[title] { cursor: help; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(92px, 1fr)); gap: 6px; margin-bottom: 10px; }
.tile { background: var(--chip-bg); border-radius: 8px; padding: 6px 8px; }
.tile .v { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.tile .l { font-size: 11px; color: var(--muted); }

.odds { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.odds h4 { margin: 0 0 4px; font-size: 12px; }
.odds .sub { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.bar-row { display: grid; grid-template-columns: 34px 1fr 38px; align-items: center; gap: 6px; font-size: 11px; font-variant-numeric: tabular-nums; }
.bar { height: 8px; background: var(--bar-bg); border-radius: 4px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--bar); }

.zone { border-top: 1px solid var(--line); padding: 7px 0; }
.zone-head { display: flex; gap: 6px; align-items: baseline; font-size: 12px; font-weight: 600; margin-bottom: 4px; }
.zone-head .count { color: var(--muted); font-weight: normal; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }

.chip {
  position: relative; display: inline-flex; align-items: flex-end;
  width: 86px; height: 54px; border-radius: 6px; overflow: hidden;
  background: var(--chip-bg) center/cover no-repeat; border: 2px solid var(--action);
  text-decoration: none; color: #fff; flex: none;
}
button.chip { font: inherit; padding: 0; cursor: pointer; text-align: left; }
button.chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.chip .name {
  width: 100%; padding: 10px 4px 2px; font-size: 10.5px; font-weight: 600; line-height: 1.1;
  background: linear-gradient(transparent, rgba(0,0,0,.8)); text-shadow: 0 1px 2px #000;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chip .n {
  position: absolute; top: 2px; right: 2px; min-width: 20px; padding: 0 5px; border-radius: 99px;
  background: rgba(0,0,0,.75); font-size: 12px; font-weight: 700; text-align: center;
}
.chip .cost {
  position: absolute; top: 2px; left: 2px; padding: 0 4px; border-radius: 99px;
  background: var(--treasure); color: #000; font-size: 10px; font-weight: 700;
}
.chip.small { width: 74px; height: 46px; }
.chip.empty-pile { opacity: .35; }
.chip.unknown { background: repeating-linear-gradient(45deg, #5b4a35, #5b4a35 6px, #6b5840 6px, #6b5840 12px); border-color: #4a3b2a; }
.chip.t-Treasure { border-color: var(--treasure); }
.chip.t-Victory { border-color: var(--victory); }
.chip.t-Curse { border-color: var(--curse); }
.chip.t-Attack { border-color: var(--attack); }
.chip.t-Reaction { border-color: var(--reaction); }
.chip.t-Duration { border-color: var(--duration); }
.chip.t-Night { border-color: var(--night); }
.chip.frac .n { background: rgba(47,109,90,.9); }

.deck-list { width: 100%; border-collapse: collapse; font-size: 12px; }
.deck-list td { padding: 2px 4px; border-bottom: 1px solid var(--line); }
.deck-list td.num { text-align: right; font-variant-numeric: tabular-nums; width: 36px; }
details > summary { cursor: pointer; font-size: 12px; font-weight: 600; padding: 6px 0; }

.bottom { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 14px; margin-top: 14px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; min-width: 0; }
.log { margin: 0; max-height: 260px; overflow: auto; font: 12px/1.45 ui-monospace, SFMono-Regular, Consolas, monospace; white-space: pre-wrap; }
.warnings { margin: 0; padding-left: 18px; max-height: 260px; overflow: auto; font-size: 12px; color: var(--warn); }

dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--ink); width: min(560px, 92vw); padding: 0; }
dialog::backdrop { background: rgba(0,0,0,.4); }
.dialog-body { display: flex; flex-direction: column; gap: 12px; padding: 16px; }
.dialog-body h3 { margin: 0; }
.dialog-body label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.dialog-body textarea { width: 100%; margin: 6px 0; font: 12px ui-monospace, Consolas, monospace; }
.dialog-actions { display: flex; gap: 8px; align-items: center; }

@font-face { font-family: "Cinzel"; src: url("/static/fonts/Cinzel.ttf") format("truetype"); font-weight: 400 900; }
@font-face { font-family: "Crimson Text"; src: url("/static/fonts/CrimsonText-Regular.ttf") format("truetype"); font-weight: 400; }
@font-face { font-family: "Crimson Text"; src: url("/static/fonts/CrimsonText-Bold.ttf") format("truetype"); font-weight: 700; }
@font-face { font-family: "Crimson Text"; src: url("/static/fonts/CrimsonText-Italic.ttf") format("truetype"); font-style: italic; }

.card-view { width: min(380px, 94vw); padding: 14px; }
.card-view.wide { width: min(580px, 94vw); }
.card-face2 { position: relative; width: 100%; aspect-ratio: 630 / 994; container-type: size; }
.card-face2.landscape { aspect-ratio: 994 / 630; }
.cf-bg { z-index: 0; background: #000; border-radius: 4cqh; }
.card-face2.landscape .cf-bg { border-radius: 4cqw; }
.cf-layer { position: absolute; }
.cf-art { z-index: 1; background: top / cover no-repeat; }
.cf-frame { z-index: 2; background: center / cover no-repeat; }
.cf-text {
  z-index: 4; display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-family: "Times New Roman", "Liberation Serif", Tinos, serif; text-align: center; line-height: 1; overflow: hidden;
}
.cf-types, .cf-name {
  display: flex; align-items: center; justify-content: center; overflow: hidden; white-space: nowrap;
  font-family: "Cinzel", "Times New Roman", serif; font-weight: 700;
}
.cf-types { z-index: 5; }
.cf-name { z-index: 6; }
.cf-corner { transform: rotate(45deg); }
/* Proportions follow the game's TextLayer: bold lines 1.3em, big-symbol lines 2em, blank lines
   .4em (.2em on landscapes), divider .16em thick with .4em/.24em margins (halved on landscapes). */
.cl { display: flex; justify-content: center; align-items: center; white-space: nowrap; }
.cl-bold { font-size: 1.3em; font-weight: 700; }
.cl-big { font-size: 2em; }
.cl-gap { height: .4em; }
.cl-divider { flex: none; width: 80%; height: .16em; margin: .4em 0 .24em; background: currentColor; }
.landscape .cl-gap { height: .2em; }
.landscape .cl-divider { height: .08em; margin: .2em 0 .12em; }
.cf-text .ico { margin: 0 .1em; }
.cf-cost { z-index: 7; display: flex; align-items: center; gap: .03em; }
.ico {
  display: inline-flex; align-items: center; justify-content: center; width: 1.1em; height: 1.1em;
  vertical-align: middle; background: center / contain no-repeat; line-height: 1;
  font-family: "Crimson Text", Georgia, serif; font-weight: 700; font-style: normal;
}
.ico > span { font-size: .78em; margin-top: -.06em; }
.ico-coin { background-image: url("/img/elements/coin.png"); color: #000; }
.ico-debt { background-image: url("/img/elements/debt_130.png"); color: #fff; }
.ico-potion { background-image: url("/img/elements/potion.png"); width: .82em; }
.ico-vp { background-image: url("/img/elements/victory_150.png"); color: #000; }
.cf-cost .ico { width: 1em; height: 1em; }
.cf-cost .ico-potion { width: .75em; }
.cf-cost .ico > span { font-size: .72em; }
.card-face { border-radius: 14px; padding: 9px; box-shadow: 0 4px 18px rgba(0,0,0,.35); }
.cf-inner { background: #f7f1e1; color: #1d1a14; border-radius: 8px; overflow: hidden; display: flex; flex-direction: column; min-height: 440px; }
.cf-title { text-align: center; font: 700 20px/1.2 Georgia, "Times New Roman", serif; padding: 8px 6px 6px; }
.cf-art-old { aspect-ratio: 287 / 209; background: #cfc6b0 center/cover no-repeat; margin: 0 8px; border: 1px solid #00000040; border-radius: 3px; }
.cf-text-old { flex: 1; padding: 10px 14px; font: 14px/1.35 Georgia, "Times New Roman", serif; text-align: center; display: flex; flex-direction: column; justify-content: center; }
.cf-text-old p { margin: 3px 0; }
.cf-text-old p.big { font-size: 30px; }
.cf-text-old .divider { border: 0; border-top: 1px solid #1d1a14; width: 70%; margin: 8px auto; }
.cf-bottom { display: flex; align-items: center; gap: 8px; padding: 6px 10px 8px; border-top: 1px solid #0000001f; }
.cf-types-old { flex: 1; text-align: center; font: 700 13px Georgia, serif; letter-spacing: .03em; }
.cf-cost-old { display: inline-flex; gap: 3px; }
.coin, .debt, .potion, .vp {
  display: inline-flex; align-items: center; justify-content: center; min-width: 1.5em; height: 1.5em; padding: 0 .25em;
  border-radius: 50%; font-weight: 700; font-family: system-ui, sans-serif; font-size: .85em; vertical-align: middle;
}
.coin { background: radial-gradient(circle at 35% 30%, #ffe48a, #d4a017); color: #1d1a14; border: 1px solid #8a6a10; }
.debt { background: #c9774a; color: #fff; border-radius: 4px; }
.potion { background: #3d7cc9; color: #fff; }
.vp { background: #4c9a3c; color: #fff; border-radius: 4px; }
.cf-meta { margin-top: 10px; font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 2px; }
.cf-close { margin-top: 10px; width: 100%; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 8px; font-size: 13px;
}

@media (max-width: 480px) {
  .players { grid-template-columns: 1fr; }
  .odds { grid-template-columns: 1fr; }
}

/* ---- charts ------------------------------------------------------------------------------- */
.charts-wrap { margin-top: 14px; }
.viz-grid-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 540px), 1fr)); gap: 14px; }
.viz-card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; min-width: 0; }
.viz-head { display: flex; align-items: center; gap: 8px; }
.viz-head h3 { margin: 0; font-size: 14px; flex: 1; }
.viz-toggle { font-size: 12px; padding: 2px 8px; }
.viz-sub { font-size: 12px; color: var(--muted); margin: 2px 0 6px; }
.viz-legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.viz-legend-item { display: inline-flex; align-items: center; gap: 5px; }
.viz-swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; flex: none; }
.viz-swatch.dot { border-radius: 50%; }
.viz-swatch.line { height: 2px; width: 14px; border-radius: 1px; }
.viz-plot { width: 100%; }
.viz-svg { display: block; width: 100%; height: auto; overflow: visible; }
.viz-grid { stroke: var(--viz-grid); stroke-width: 1; }
.viz-axis { stroke: var(--viz-axis); stroke-width: 1; }
.viz-tick { fill: var(--ink-muted); font-size: 11px; font-family: system-ui, -apple-system, "Segoe UI", sans-serif; }
.viz-endlabel { fill: var(--muted); font-size: 11px; font-family: system-ui, -apple-system, "Segoe UI", sans-serif; }
.viz-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.viz-dot { stroke: var(--panel); stroke-width: 2; }
.viz-band { stroke: var(--panel); stroke-width: 2; stroke-linejoin: round; }
.viz-cross { stroke: var(--ink-muted); stroke-width: 1; }
.viz-hit { fill: transparent; cursor: crosshair; outline: none; }
.viz-hit:focus-visible { stroke: var(--accent); stroke-width: 1; }
.viz-multiples { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 8px; }
.viz-multiple-name { display: flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; margin-bottom: 2px; }
.viz-table-wrap { overflow-x: auto; max-height: 260px; }
.viz-table { border-collapse: collapse; font-size: 12px; font-variant-numeric: tabular-nums; }
.viz-table th, .viz-table td { padding: 3px 8px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
.viz-table th { position: sticky; top: 0; background: var(--panel); color: var(--muted); font-weight: 600; }
.viz-tip {
  position: fixed; z-index: 50; pointer-events: none; background: var(--panel); color: var(--ink);
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 9px; font-size: 12px;
  box-shadow: 0 4px 14px rgba(0,0,0,.18); max-width: 280px;
}
.viz-tip-head { font-weight: 600; margin-bottom: 3px; }
.viz-tip-row { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.viz-tip-row strong { font-variant-numeric: tabular-nums; }
.makeup-donut { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.makeup-donut .viz-legend { flex-direction: column; gap: 2px; margin: 0; }
.viz-arc { cursor: help; outline: none; }
.viz-donut-num { fill: var(--ink); font-size: 18px; font-weight: 700; font-family: system-ui, sans-serif; }
.viz-donut-lbl { fill: var(--muted); font-size: 10px; font-family: system-ui, sans-serif; }

/* ---- game log ------------------------------------------------------------------------------ */
.gamelog-panel { margin-top: 14px; }
.gamelog-panel > summary { cursor: pointer; margin: 0; list-style-position: inside; }
.gamelog-panel[open] > summary { margin-bottom: 8px; }
.gamelog {
  max-height: 420px; overflow-y: auto; font-size: 13px; line-height: 1.45;
  font-family: "Times New Roman", "Liberation Serif", Georgia, serif;
}
.gl { padding: 0 0.3em; white-space: pre-wrap; }
.gl-turn {
  margin-top: 8px; padding: 2px 6px; border-left: 3px solid; font-weight: 700;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif; font-size: 12.5px; background: var(--chip-bg); border-radius: 0 4px 4px 0;
}
.gl-shuffle { font-style: italic; color: var(--muted); }
.gl-player { font-weight: 700; }
.lc { font-weight: 700; cursor: pointer; }
.lc:hover { text-decoration: underline; }
.lc.t-Treasure { color: var(--lc-treasure); }
.lc.t-Victory { color: var(--lc-victory); }
.lc.t-Curse { color: var(--lc-curse); }
.lc.t-Duration { color: var(--lc-duration); }
.lc.t-Reaction { color: var(--lc-reaction); }
