/* Khel — layout concept: the world as a game board. A lapis-and-shell games table
   (colours from the inlay of the Royal Game of Ur); the home page opens on a go-board
   world map where every game is a piece at its birthplace, then an atlas of games by
   region. Game pages are a quiet table: controls, status line, board, then the lore. */
:root {
  --shell: #eef1f7;      /* page ground */
  --tile: #ffffff;       /* raised surfaces */
  --ink: #19203b;        /* text */
  --ink-2: #4e5778;      /* secondary text */
  --rule: #d3d9e8;       /* borders */
  --lapis: #2a49b8;      /* primary accent */
  --lapis-ink: #ffffff;  /* text on lapis */
  --lapis-wash: #dfe5f7; /* selected / hover fills */
  --carnelian: #c2412f;  /* pieces, highlights */
  --gold: #a87a0c;       /* rosettes, stars */
  --ok: #2f7d4f;
  --warn: #b4581a;
  --map-line: #d3d9e8;
  --map-land: #93a0c4;
  --veil: rgba(238, 241, 247, 0.86);

  /* board materials: the same in both themes, like real wood and stone */
  --wood: #dcae68;
  --wood-2: #c9934a;
  --wood-line: #4a2f14;
  --stone-b: #1d1d24;
  --stone-w: #f3f2ec;
  --felt: #2a7a55;
  --felt-2: #23684a;

  --display: 'Young Serif', 'Iowan Old Style', 'Palatino Linotype', Palatino, Georgia, serif;
  --text: 'Atkinson Hyperlegible Next', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --step--1: 0.875rem;
  --step-0: 1rem;
  --step-1: 1.25rem;
  --step-2: 1.56rem;
  --step-3: 1.95rem;
  --step-4: 2.44rem;
  --radius: 14px;
  --gutter-l: max(16px, env(safe-area-inset-left, 0px));
  --gutter-r: max(16px, env(safe-area-inset-right, 0px));
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --shell: #0c1226; --tile: #151d3a; --ink: #e6eaf6; --ink-2: #a3acca; --rule: #28325c;
    --lapis: #93a8ff; --lapis-ink: #0c1226; --lapis-wash: #1f2a52; --carnelian: #f27453;
    --gold: #e6b84a; --ok: #6cc391; --warn: #f0a35e; --map-line: #1d2648; --map-land: #4b5a8f;
    --veil: rgba(12, 18, 38, 0.86); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --shell: #0c1226; --tile: #151d3a; --ink: #e6eaf6; --ink-2: #a3acca; --rule: #28325c;
  --lapis: #93a8ff; --lapis-ink: #0c1226; --lapis-wash: #1f2a52; --carnelian: #f27453;
  --gold: #e6b84a; --ok: #6cc391; --warn: #f0a35e; --map-line: #1d2648; --map-land: #4b5a8f;
  --veil: rgba(12, 18, 38, 0.86); color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--shell);
  color: var(--ink);
  font: 400 var(--step-0)/1.5 var(--text);
  -webkit-tap-highlight-color: transparent;
}
body::before {
  content: ""; position: fixed; inset: 0 0 auto; height: env(safe-area-inset-top, 0px);
  z-index: 10; background: var(--shell);
}
h1, h2, h3 { font-family: var(--display); font-weight: 400; line-height: 1.1; margin: 0; text-wrap: balance; }
p { margin: 0; }
a { color: var(--lapis); }
button { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--lapis); outline-offset: 2px; border-radius: 6px; }
[hidden] { display: none !important; }
.wrap { max-width: 1120px; margin-inline: auto; padding-left: var(--gutter-l); padding-right: var(--gutter-r); }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 5;
  background: var(--shell); border-bottom: 1px solid var(--rule);
}
.topbar .wrap { display: flex; align-items: center; gap: 10px; min-height: 56px; padding-block: 6px; }
.brand { display: inline-flex; align-items: center; gap: 8px; text-decoration: none; color: var(--ink); }
.brand svg { width: 30px; height: 30px; flex: none; }
.brand b { font: 400 1.6rem/1 var(--display); letter-spacing: 0.01em; }
.topbar .spacer { flex: 1; }
.iconbtn {
  display: inline-grid; place-items: center; width: 42px; height: 42px; border-radius: 12px;
  border: 1px solid transparent; background: transparent; cursor: pointer; color: var(--ink);
}
.iconbtn:hover { background: var(--lapis-wash); }
.iconbtn svg { width: 22px; height: 22px; }
.iconbtn[aria-pressed="true"] { color: var(--gold); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 14px; border-radius: 12px; cursor: pointer;
  border: 1px solid var(--rule); background: var(--tile); color: var(--ink); font-weight: 700;
  font-size: var(--step--1); text-decoration: none; white-space: nowrap;
}
.btn:hover { border-color: var(--lapis); }
.btn:disabled { opacity: 0.45; cursor: default; border-color: var(--rule); }
.btn.primary { background: var(--lapis); border-color: var(--lapis); color: var(--lapis-ink); }
.btn svg { width: 18px; height: 18px; }
@media (max-width: 520px) { .btn.compact { padding-inline: 11px; } .btn.compact .lbl { display: none; } }

/* ---------- home: hero + map ---------- */
.hero { padding-block: 28px 8px; display: grid; gap: 14px; }
.hero h1 { font-size: clamp(2.2rem, 8.5vw, 3.9rem); max-width: 14ch; }
.hero .lede { color: var(--ink-2); font-size: var(--step-1); line-height: 1.4; max-width: 46ch; }
.mapbox { margin-top: 6px; }
.map { width: 100%; height: auto; display: block; }
.map .grid line { stroke: var(--map-line); stroke-width: 0.6; }
.map .land { fill: var(--map-land); transition: fill 0.25s; }
.map .pin { cursor: pointer; }
.map .pin circle.stone { fill: var(--carnelian); stroke: var(--shell); stroke-width: 1.4; }
.map .pin circle.hit { fill: transparent; }
.map .pin text { font: 700 7px var(--text); fill: var(--lapis-ink); text-anchor: middle; dominant-baseline: central; pointer-events: none; }
.map .pin:focus-visible { outline: none; }
.map .pin:focus-visible circle.stone { stroke: var(--lapis); stroke-width: 2.4; }
.map[data-sel] .land { fill: color-mix(in srgb, var(--map-land) 55%, var(--shell)); }
.map[data-sel] .pin { opacity: 0.28; }
.map[data-sel="africa"] .land[data-r="africa"],
.map[data-sel="middle-east"] .land[data-r="middle-east"],
.map[data-sel="south-asia"] .land[data-r="south-asia"],
.map[data-sel="east-asia"] .land[data-r="east-asia"],
.map[data-sel="europe"] .land[data-r="europe"],
.map[data-sel="americas"] .land[data-r="americas"],
.map[data-sel="oceania"] .land[data-r="oceania"] { fill: var(--lapis); }
.map[data-sel="africa"] .pin[data-r="africa"],
.map[data-sel="middle-east"] .pin[data-r="middle-east"],
.map[data-sel="south-asia"] .pin[data-r="south-asia"],
.map[data-sel="east-asia"] .pin[data-r="east-asia"],
.map[data-sel="europe"] .pin[data-r="europe"],
.map[data-sel="americas"] .pin[data-r="americas"],
.map[data-sel="oceania"] .pin[data-r="oceania"] { opacity: 1; }
.mapnote { color: var(--ink-2); font-size: var(--step--1); margin-top: 6px; }

.today {
  display: flex; align-items: center; gap: 12px; margin-top: 4px; padding: 10px 12px;
  border: 1px solid var(--rule); border-radius: var(--radius); background: var(--tile);
  text-decoration: none; color: var(--ink); max-width: 520px;
}
.today .thumb { width: 52px; height: 52px; flex: none; }
.today small { display: block; color: var(--ink-2); font-size: var(--step--1); }
.today strong { font: 400 var(--step-1)/1.2 var(--display); }

/* ---------- filters ---------- */
.filters { display: grid; gap: 10px; padding-block: 14px 6px; position: sticky; top: calc(env(safe-area-inset-top, 0px) + 57px); z-index: 4; background: var(--shell); }
.chips { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; margin-inline: -4px; padding-inline: 4px; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; min-height: 36px; padding: 6px 13px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--rule); background: transparent; color: var(--ink); font-weight: 700; font-size: var(--step--1);
}
.chip[aria-pressed="true"] { background: var(--ink); color: var(--shell); border-color: var(--ink); }
.search { display: flex; align-items: center; gap: 8px; border: 1px solid var(--rule); border-radius: 12px; background: var(--tile); padding-inline: 12px; }
.search svg { width: 18px; height: 18px; color: var(--ink-2); flex: none; }
.search input { flex: 1; min-width: 0; border: 0; background: transparent; color: var(--ink); font: inherit; min-height: 42px; outline: none; }

/* ---------- atlas of games ---------- */
.atlas { display: grid; gap: 34px; padding-block: 18px 56px; }
.region { display: grid; gap: 14px; }
.region header { display: grid; gap: 4px; }
.region h2 { font-size: var(--step-3); }
.region header p { color: var(--ink-2); max-width: 60ch; }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px 14px; }
@media (min-width: 640px) { .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (min-width: 900px) { .grid { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
@media (min-width: 1080px) { .grid { grid-template-columns: repeat(5, minmax(0, 1fr)); } }
.game { display: grid; gap: 8px; text-decoration: none; color: var(--ink); align-content: start; min-width: 0; }
.game .thumb { aspect-ratio: 1; width: 100%; max-width: 100%; border-radius: var(--radius); overflow: hidden; display: block; }
.game .thumb svg, .today .thumb svg { width: 100%; height: 100%; display: block; }
.game:hover .thumb { outline: 2px solid var(--lapis); outline-offset: 2px; }
.game h3 { font-size: 1.12rem; }
.game .meta { color: var(--ink-2); font-size: var(--step--1); line-height: 1.35; }
.game .players { font-size: 0.8rem; color: var(--ink-2); display: flex; align-items: center; gap: 5px; }
.game .players svg { width: 14px; height: 14px; }
.empty { padding: 28px 0; color: var(--ink-2); }
.recent { display: flex; gap: 10px; overflow-x: auto; scrollbar-width: none; }
.recent a { flex: none; display: inline-flex; align-items: center; gap: 8px; padding: 6px 12px 6px 6px; border: 1px solid var(--rule); border-radius: 12px; text-decoration: none; color: var(--ink); background: var(--tile); font-weight: 700; font-size: var(--step--1); }
.recent .thumb { width: 30px; height: 30px; border-radius: 8px; overflow: hidden; }
.recent .thumb svg { width: 100%; height: 100%; display: block; }
.footer { border-top: 1px solid var(--rule); padding-block: 24px 40px; color: var(--ink-2); font-size: var(--step--1); display: grid; gap: 8px; }
.footer a { font-weight: 700; }

/* ---------- play page ---------- */
.playbar .wrap { gap: 6px; }
.playbar .title { min-width: 0; flex: 1; display: grid; }
.playbar h1 { font-size: 1.35rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.playbar .title small { color: var(--ink-2); font-size: 0.8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.table { padding-block: 14px 8px; }
.lore { padding-block: 22px 56px; display: grid; gap: 22px; border-top: 1px solid var(--rule); margin-top: 18px; }
.lore section { display: grid; gap: 10px; max-width: 68ch; }
.lore h2 { font-size: var(--step-2); }
.lore ul, .lore ol { margin: 0; padding-left: 1.2em; display: grid; gap: 6px; }
.lore .aka { color: var(--ink-2); }
.lore .facts { display: flex; flex-wrap: wrap; gap: 8px; }
.lore .facts span { border: 1px solid var(--rule); border-radius: 999px; padding: 3px 11px; font-size: var(--step--1); }

/* ---------- game frame (shared by every game) ---------- */
.g { display: grid; gap: 12px; }
.g-controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.seg { display: inline-flex; border: 1px solid var(--rule); border-radius: 12px; overflow: hidden; background: var(--tile); }
.seg button { border: 0; background: transparent; padding: 8px 11px; min-height: 38px; font-weight: 700; font-size: var(--step--1); cursor: pointer; color: var(--ink-2); }
.seg button + button { border-left: 1px solid var(--rule); }
.seg button[aria-checked="true"] { background: var(--lapis); color: var(--lapis-ink); }
.g-status { display: flex; align-items: center; gap: 10px; min-height: 30px; font-size: 1.05rem; font-weight: 700; }
.g-status .sw { width: 22px; height: 22px; flex: none; display: inline-grid; place-items: center; }
.g-status .sw svg { width: 22px; height: 22px; filter: drop-shadow(0 0 1.2px var(--ink-2)); }
.g-status .sub { font-weight: 400; color: var(--ink-2); font-size: var(--step--1); }
.g-status.busy .dots::after { content: ""; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }
.g-stage {
  position: relative; margin-inline: auto;
  width: min(100%, var(--maxw, 620px), calc((100vh - var(--chrome, 250px)) * var(--ar, 1)));
}
@supports (height: 100dvh) {
  .g-stage { width: min(100%, var(--maxw, 620px), calc((100dvh - var(--chrome, 250px)) * var(--ar, 1))); }
}
.g-stage > svg, .g-stage > canvas { width: 100%; height: auto; display: block; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
.g-stage svg text { user-select: none; -webkit-user-select: none; }
.g-foot { display: flex; flex-wrap: wrap; gap: 8px 16px; justify-content: center; color: var(--ink-2); font-size: var(--step--1); }
.g-foot b { color: var(--ink); }
.g-over {
  position: absolute; inset: 0; display: grid; place-items: center; padding: 14px;
  background: var(--veil); border-radius: var(--radius); z-index: 3;
}
.g-over .card { background: var(--tile); border: 1px solid var(--rule); border-radius: var(--radius); padding: 18px; display: grid; gap: 10px; text-align: center; max-width: 320px; width: 100%; box-shadow: 0 10px 30px rgba(10, 16, 40, 0.18); }
.g-over h2 { font-size: var(--step-3); }
.g-over p { color: var(--ink-2); }
.g-over .row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.hl { animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.45; } }
.clickable { cursor: pointer; }
.pop { animation: pop 0.18s ease-out; }
.appear { animation: appear 0.18s ease-out; }
@keyframes pop { 50% { transform: scale(1.12); } }
@keyframes appear { from { transform: scale(0.3); opacity: 0.2; } }
.g-pad { display: grid; grid-template-columns: repeat(var(--cols, 5), minmax(0, 1fr)); gap: 6px; max-width: 520px; margin-inline: auto; width: 100%; }
.g-pad button { min-height: 46px; border-radius: 10px; border: 1px solid var(--rule); background: var(--tile); font-weight: 700; font-size: 1.15rem; cursor: pointer; }
.g-pad button[aria-pressed="true"] { background: var(--lapis); color: var(--lapis-ink); border-color: var(--lapis); }
.g-pad button:disabled { opacity: 0.35; }
.dice { display: inline-flex; gap: 6px; align-items: center; }
.dice svg { width: 40px; height: 40px; }
.roll { min-width: 110px; }

/* ---------- sheets, toasts ---------- */
.sheet-back { position: fixed; inset: 0; background: rgba(8, 12, 28, 0.5); z-index: 20; display: grid; align-items: end; justify-items: center; }
@media (min-width: 640px) { .sheet-back { align-items: center; } }
.sheet {
  background: var(--tile); color: var(--ink); width: min(100%, 520px); max-height: 88vh; overflow: auto;
  border-radius: 20px 20px 0 0; padding: 20px max(18px, env(safe-area-inset-right, 0px)) calc(20px + env(safe-area-inset-bottom, 0px)) max(18px, env(safe-area-inset-left, 0px));
  display: grid; gap: 18px; border: 1px solid var(--rule);
}
@media (min-width: 640px) { .sheet { border-radius: 20px; } }
.sheet h2 { font-size: var(--step-2); }
.sheet .field { display: grid; gap: 8px; }
.sheet .field > span { font-weight: 700; }
.sheet .field small { color: var(--ink-2); }
.sheet .row { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(18px + env(safe-area-inset-bottom, 0px)); z-index: 30;
  background: var(--ink); color: var(--shell); padding: 12px 16px; border-radius: 14px; max-width: min(92vw, 460px);
  display: flex; gap: 12px; align-items: center; box-shadow: 0 8px 24px rgba(10, 16, 40, 0.25); font-weight: 700;
}
.toast .btn { min-height: 34px; padding: 6px 12px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.001ms !important; }
}
