:root {
  --bg: #f3eee2;
  --bg-2: #ebe4d3;
  --card: #fffdf7;
  --ink: #15221a;
  --ink-2: #33433a;
  --muted: #6d7468;
  --line: #e0d8c5;
  --brand: #123526;
  --brand-2: #1d4a36;
  --gold: #c39437;
  --gold-soft: #f3e3bd;
  --hit: #2c8a52;
  --hit-ink: #fff;
  --near: #e5a822;
  --near-ink: #2a1d00;
  --miss: #dcd5c5;
  --miss-ink: #3c463f;
  --silk-a: #123526;
  --silk-b: #c39437;
  --turf-1: #3f8a4f;
  --turf-2: #367c45;
  --shadow: 0 1px 2px rgba(20, 30, 20, .06), 0 6px 20px -8px rgba(20, 30, 20, .18);
  --radius: 14px;
  --font: "DM Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --display: "Fraunces", Georgia, "Times New Roman", serif;
  color-scheme: light;
}
:root.dark {
  --bg: #0c1611;
  --bg-2: #101d16;
  --card: #14231b;
  --ink: #eee8da;
  --ink-2: #cfc9b9;
  --muted: #8f9a8f;
  --line: #24382c;
  --brand: #eee8da;
  --brand-2: #c39437;
  --gold-soft: #3a3020;
  --miss: #2a3a30;
  --miss-ink: #c9cfc4;
  --silk-a: #1f6a47;
  --turf-1: #2d6b3b;
  --turf-2: #275f34;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px -10px rgba(0, 0, 0, .6);
  color-scheme: dark;
}
:root.cb {
  --hit: #e8702a;
  --hit-ink: #fff;
  --near: #3d8fd6;
  --near-ink: #fff;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  background-image: radial-gradient(circle at 20% -10%, var(--bg-2), transparent 55%);
  min-height: 100dvh;
  line-height: 1.45;
}
button { font: inherit; color: inherit; cursor: pointer; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* ------------------------------------------------------------- header */
.top {
  position: sticky; top: 0; z-index: 20;
  background: var(--bg);
  backdrop-filter: saturate(1.4) blur(10px);
  -webkit-backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--line);
}
.top-inner {
  max-width: 1040px; margin: 0 auto;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
}
.top-actions { display: flex; justify-content: flex-end; gap: 4px; }
.brand {
  margin: 0; display: flex; align-items: center; gap: 10px;
  font-family: var(--display); font-weight: 800; font-size: 1.75rem; letter-spacing: -.02em;
  color: var(--brand);
}
.brand em { font-style: normal; color: var(--gold); }
.silk { width: 34px; height: 34px; color: var(--brand); flex: none; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; border: 0; background: transparent;
  display: grid; place-items: center; color: var(--ink-2); transition: background .15s;
}
.icon-btn:hover { background: var(--bg-2); }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn svg .fill { fill: currentColor; }

.wrap { max-width: 1040px; margin: 0 auto; padding: 18px 16px 40px; }

/* ------------------------------------------------------------- modes */
.modes {
  display: flex; gap: 4px; padding: 4px; margin: 0 auto 16px; width: max-content;
  background: var(--bg-2); border-radius: 999px; border: 1px solid var(--line);
}
.mode {
  border: 0; background: transparent; padding: 7px 18px; border-radius: 999px;
  font-weight: 600; color: var(--muted); transition: all .2s;
}
.mode.active { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }
.mode span { color: var(--gold); font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------- track */
.track-card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px 14px; box-shadow: var(--shadow); margin-bottom: 14px;
}
.track-head { display: flex; justify-content: space-between; align-items: baseline; font-size: .9rem; margin-bottom: 8px; }
.track-head #track-label { font-weight: 700; }
.track {
  position: relative; height: 54px; border-radius: 10px; overflow: hidden;
  background: repeating-linear-gradient(90deg, var(--turf-1) 0 28px, var(--turf-2) 28px 56px);
}
.rail { position: absolute; left: 0; right: 0; top: 7px; height: 3px; background: rgba(255,255,255,.85); box-shadow: 0 1px 0 rgba(0,0,0,.15); }
.posts { position: absolute; inset: 0; }
.post {
  position: absolute; top: 4px; width: 2px; height: 12px; background: #fff; transform: translateX(-1px);
}
.post b {
  position: absolute; top: 15px; left: 50%; transform: translateX(-50%);
  font-size: 9px; font-weight: 700; color: rgba(255,255,255,.75); letter-spacing: .02em;
}
.finish {
  position: absolute; right: 8px; top: 0; bottom: 0; width: 6px;
  background: repeating-linear-gradient(180deg, #fff 0 6px, #c4302b 6px 12px);
}
.runner {
  position: absolute; left: 8px; bottom: 6px; width: 30px; height: 30px;
  transition: left .9s cubic-bezier(.3,.7,.3,1);
  filter: drop-shadow(0 2px 2px rgba(0,0,0,.35));
}
.runner svg { width: 100%; height: 100%; }
.runner.gallop { animation: bob .28s ease-in-out infinite alternate; }
.runner.lost { opacity: .55; filter: grayscale(.7); }
@keyframes bob { from { transform: translateY(0) rotate(-3deg); } to { transform: translateY(-4px) rotate(3deg); } }

/* ------------------------------------------------------------- search */
.guess-area { position: relative; z-index: 10; margin-bottom: 10px; }
.search {
  position: relative; display: flex; align-items: center; gap: 8px;
  background: var(--card); border: 2px solid var(--line); border-radius: 16px;
  padding: 6px 6px 6px 14px; box-shadow: var(--shadow); transition: border-color .15s;
}
.search:focus-within { border-color: var(--gold); }
.search-ico { width: 20px; height: 20px; fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; flex: none; }
.search input {
  flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--ink);
  font: inherit; font-size: 1.08rem; padding: 10px 2px;
}
.search input::placeholder { color: var(--muted); }
.go {
  border: 0; border-radius: 11px; padding: 11px 20px; font-weight: 700;
  background: var(--brand); color: var(--bg); transition: transform .1s, opacity .15s;
}
:root.dark .go { background: var(--gold); color: #1a1408; }
.go:active { transform: scale(.97); }
.go:disabled { opacity: .45; cursor: default; }
.suggestions {
  position: absolute; left: -2px; right: -2px; top: calc(100% + 6px); margin: 0; padding: 6px;
  list-style: none; background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: 0 18px 40px -12px rgba(0,0,0,.35); max-height: min(360px, 52vh); overflow-y: auto;
}
.suggestions li {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 12px; border-radius: 10px; cursor: pointer;
}
.suggestions li[aria-selected="true"], .suggestions li:hover { background: var(--gold-soft); }
.suggestions .nm { font-weight: 600; }
.suggestions .nm mark { background: none; color: var(--gold); font-weight: 800; }
:root:not(.dark) .suggestions .nm mark { color: #9a6f12; }
@media (pointer: fine) {
  .suggestions li[aria-selected="true"]::after {
    content: "Tab"; flex: none; font-size: .68rem; font-weight: 700; letter-spacing: .04em; color: var(--muted);
    border: 1px solid currentColor; border-radius: 5px; padding: 1px 5px; margin-left: -4px; opacity: .8;
  }
}
.suggestions .meta { font-size: .8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.suggestions .none { color: var(--muted); cursor: default; justify-content: center; }
.hint-line { font-size: .85rem; margin: 8px 4px 0; min-height: 1.2em; }

/* ------------------------------------------------------------- clues */
.clues { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; margin: 12px 0 18px; }
.clue {
  position: relative; text-align: left; border: 1px dashed var(--line); border-radius: 12px;
  background: transparent; padding: 9px 11px; min-height: 64px; transition: all .2s;
  display: flex; flex-direction: column; gap: 2px;
}
.clue .k { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; font-weight: 700; color: var(--muted); }
.clue .v { font-size: .86rem; font-weight: 600; line-height: 1.25; overflow-wrap: break-word; min-width: 0; }
.clue { min-width: 0; }
.clue.locked { opacity: .6; cursor: default; }
.clue.locked .v { color: var(--muted); font-weight: 500; }
.clue.ready { border: 1px solid var(--gold); background: var(--gold-soft); cursor: pointer; }
.clue.ready:hover { transform: translateY(-1px); }
.clue.ready .v { color: var(--ink); }
.clue.shown { border: 1px solid var(--line); background: var(--card); box-shadow: var(--shadow); cursor: default; }
.clue.pop { animation: pop .35s ease; }
@keyframes pop { 0% { transform: scale(.94); } 60% { transform: scale(1.03); } 100% { transform: scale(1); } }

/* ------------------------------------------------------------- board */
.board-head, .row-tiles { display: grid; grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 6px; }
.board-head { padding: 0 0 6px; margin-left: 170px; }
.board-head div { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; font-weight: 700; color: var(--muted); text-align: center; }
.row { display: grid; grid-template-columns: 164px 1fr; gap: 6px; margin-bottom: 6px; align-items: stretch; }
.row-name {
  display: flex; align-items: center; padding: 0 12px; border-radius: 10px;
  background: var(--card); border: 1px solid var(--line); font-weight: 700; line-height: 1.15;
  font-size: .95rem; min-height: 62px;
}
@media (min-width: 901px) { .row-name.long { font-size: .84rem; overflow-wrap: anywhere; } }
.row.correct .row-name { background: var(--hit); color: var(--hit-ink); border-color: transparent; }
.tile {
  position: relative; display: flex; min-height: 62px; border-radius: 10px; perspective: 600px;
}
.tile-in {
  position: relative; flex: 1; min-width: 0; border-radius: 10px; display: flex; flex-direction: column;
  align-items: center; justify-content: center; text-align: center; padding: 4px;
  font-weight: 700; font-size: .88rem; line-height: 1.1; overflow: hidden;
}
.tile .lbl { display: none; }
.tile.hit .tile-in { background: var(--hit); color: var(--hit-ink); }
.tile.near .tile-in { background: var(--near); color: var(--near-ink); }
.tile.miss .tile-in { background: var(--miss); color: var(--miss-ink); }
.tile .val { position: relative; z-index: 1; overflow-wrap: break-word; max-width: 100%; }
.tile .val small { display: block; font-weight: 500; font-size: .68rem; opacity: .8; margin-top: 2px; }
.tile .arrow {
  display: inline-grid; place-items: center; width: 1.35em; height: 1.35em; margin-left: 5px; vertical-align: 1px;
  border-radius: 50%; font-size: .72em; line-height: 1; background: rgba(0, 0, 0, .14);
}
.tile .arrow.down { padding-top: 1px; }
.tile .dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 4px; vertical-align: -1px; box-shadow: 0 0 0 1.5px rgba(255,255,255,.7); }
.sw-bay { background: #8a4b22; } .sw-dark-bay { background: #4b2a17; } .sw-brown { background: #5a3820; }
.sw-black { background: #1b1b1b; } .sw-chestnut { background: #b4581f; } .sw-grey { background: #c4c4bf; } .sw-roan { background: #a08a80; }
.tile.flip .tile-in { animation: flip .55s ease both; animation-delay: calc(var(--i) * 110ms); }
@keyframes flip {
  0% { transform: rotateX(90deg); filter: brightness(1.4); }
  100% { transform: rotateX(0); filter: none; }
}
.row.enter .row-name { animation: slide .35s ease both; }
@keyframes slide { from { opacity: 0; transform: translateX(-10px); } to { opacity: 1; transform: none; } }
.empty {
  border: 1px dashed var(--line); border-radius: var(--radius); padding: 22px; text-align: center;
  color: var(--ink-2); background: color-mix(in srgb, var(--card) 50%, transparent);
}
.empty p { margin: 0; }

/* ------------------------------------------------------------- result */
.result {
  margin-top: 18px; background: var(--card); border: 1px solid var(--line); border-radius: 18px;
  box-shadow: var(--shadow); padding: 22px; text-align: center; animation: rise .5s ease both;
}
@keyframes rise { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
.result .verdict { font-family: var(--display); font-weight: 800; font-size: 1.9rem; margin: 0 0 4px; letter-spacing: -.02em; }
.result .sub { margin: 0 0 16px; color: var(--ink-2); }
.answer-card {
  display: inline-flex; flex-direction: column; align-items: center; gap: 4px;
  border: 1px solid var(--line); border-radius: 14px; padding: 14px 22px; margin-bottom: 16px;
  background: linear-gradient(180deg, var(--gold-soft), transparent);
}
.answer-card .name { font-family: var(--display); font-weight: 800; font-size: 1.6rem; }
.answer-card .facts { font-size: .88rem; color: var(--ink-2); }
.answer-card a { color: var(--brand-2); font-weight: 600; font-size: .88rem; }
.actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.btn {
  border: 0; border-radius: 12px; padding: 12px 20px; font-weight: 700; display: inline-flex; gap: 8px; align-items: center;
  background: var(--bg-2); color: var(--ink); border: 1px solid var(--line);
}
.btn.primary { background: var(--hit); color: #fff; border-color: transparent; }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.next { margin-top: 16px; font-size: .95rem; }
.next strong { font-variant-numeric: tabular-nums; font-size: 1.1rem; }

/* ------------------------------------------------------------- footer */
.foot { margin-top: 36px; text-align: center; font-size: .82rem; color: var(--muted); }
.foot p { margin: 4px 0; }
.linkish { border: 0; background: none; padding: 0; color: var(--ink-2); text-decoration: underline; text-underline-offset: 3px; font-size: inherit; }

/* ------------------------------------------------------------- dialogs */
.dlg {
  border: 0; border-radius: 20px; padding: 24px 24px 22px; width: min(520px, calc(100vw - 24px));
  background: var(--card); color: var(--ink); box-shadow: 0 30px 80px -20px rgba(0,0,0,.5);
  max-height: calc(100dvh - 40px);
}
.dlg::backdrop { background: rgba(10, 20, 14, .45); backdrop-filter: blur(3px); }
.dlg[open] { animation: rise .25s ease; }
.dlg h2 { font-family: var(--display); font-weight: 800; margin: 0 0 12px; font-size: 1.6rem; letter-spacing: -.01em; }
.dlg h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 20px 0 8px; }
.dlg p { margin: 0 0 12px; }
.dlg-x {
  position: absolute; top: 12px; right: 12px; width: 36px; height: 36px; border-radius: 10px;
  border: 0; background: var(--bg-2); font-size: 1.4rem; line-height: 1;
}
.legend { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 0 14px; font-size: .9rem; }
.legend div { display: flex; align-items: center; gap: 8px; }
.sw { width: 22px; height: 22px; border-radius: 6px; flex: none; display: grid; place-items: center; font-size: .75rem; }
.sw.hit { background: var(--hit); } .sw.near { background: var(--near); } .sw.miss { background: var(--miss); }
.sw.arrow { background: var(--miss); color: var(--miss-ink); }
.rules { width: 100%; border-collapse: collapse; font-size: .88rem; margin-bottom: 14px; }
.rules th { text-align: left; padding: 6px 10px 6px 0; vertical-align: top; white-space: nowrap; }
.rules td { padding: 6px 0; color: var(--ink-2); }
.rules tr + tr { border-top: 1px solid var(--line); }
.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; text-align: center; }
.stat-grid div { background: var(--bg-2); border-radius: 12px; padding: 10px 4px; }
.stat-grid b { display: block; font-family: var(--display); font-size: 1.7rem; line-height: 1.1; }
.stat-grid span { font-size: .72rem; color: var(--muted); }
.dist { display: grid; gap: 4px; }
.dist .bar { display: grid; grid-template-columns: 18px 1fr; gap: 8px; align-items: center; font-size: .85rem; font-weight: 600; }
.dist .bar i {
  display: block; min-width: 26px; padding: 2px 8px; text-align: right; font-style: normal;
  background: var(--miss); color: var(--miss-ink); border-radius: 6px; transition: width .6s ease;
}
.dist .bar.today i { background: var(--hit); color: #fff; }
.toggle { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 12px 0; cursor: pointer; }
.toggle + .toggle { border-top: 1px solid var(--line); }
.toggle span { display: flex; flex-direction: column; }
.toggle small { color: var(--muted); font-size: .82rem; }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle i { position: relative; width: 46px; height: 28px; border-radius: 99px; background: var(--miss); flex: none; transition: background .2s; }
.toggle i::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.toggle input:checked + i { background: var(--hit); }
.toggle input:checked + i::after { transform: translateX(18px); }
.toggle input:focus-visible + i { outline: 2px solid var(--gold); outline-offset: 2px; }

/* ------------------------------------------------------------- toasts */
.toasts { position: fixed; left: 50%; top: 76px; transform: translateX(-50%); z-index: 100; display: grid; gap: 8px; pointer-events: none; }
.toast {
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600;
  font-size: .92rem; box-shadow: 0 10px 30px -10px rgba(0,0,0,.5); animation: toast 2.4s ease both; text-align: center;
}
@keyframes toast { 0% { opacity: 0; transform: translateY(-8px); } 10%, 85% { opacity: 1; transform: none; } 100% { opacity: 0; } }
#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 90; }

:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

/* ------------------------------------------------------------- mobile */
/* stacked board: phones, landscape phones and portrait tablets */
@media (max-width: 900px) {
  .clues { grid-template-columns: repeat(3, 1fr); }
  .board-head { display: none; }
  .row { grid-template-columns: 1fr; gap: 4px; margin-bottom: 12px; }
  .row-name { min-height: 0; padding: 8px 12px; font-size: 1rem; }
  .row-tiles { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; }
  .tile { min-height: 64px; }
  .tile .lbl {
    display: block; position: relative; z-index: 1; font-size: .58rem; text-transform: uppercase;
    letter-spacing: .08em; font-weight: 700; opacity: .7; margin-bottom: 3px;
  }
}
@media (max-width: 760px) {
  .brand { font-size: 1.45rem; }
  .silk { width: 28px; height: 28px; }
  .wrap { padding: 12px 10px 32px; }
  .tile-in { font-size: .84rem; padding: 6px 4px; }
  .clues { grid-template-columns: repeat(2, 1fr); }
  .clues .clue:last-child { grid-column: span 2; }
  .stat-grid b { font-size: 1.4rem; }
  .result { padding: 18px 14px; }
  .result .verdict { font-size: 1.6rem; }
}
@media (max-width: 560px) {
  .legend { grid-template-columns: 1fr; }
  .dlg { padding: 20px 18px 18px; }
  .suggestions li { flex-direction: column; align-items: flex-start; gap: 1px; padding: 9px 12px; }
  .suggestions .meta { white-space: normal; }
}
@media (max-width: 360px) {
  .top-inner { padding-left: 8px; padding-right: 8px; }
  .brand { font-size: 1.2rem; gap: 6px; }
  .silk { width: 24px; height: 24px; }
  .icon-btn { width: 38px; height: 38px; }
  .top-actions { gap: 0; }
  .tile-in { font-size: .78rem; }
  .tile .lbl { font-size: .54rem; letter-spacing: .05em; }
  .tile .val small { font-size: .62rem; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-delay: 0ms !important; transition-duration: .001ms !important; }
}
:root.calm *, :root.calm *::before, :root.calm *::after { animation-duration: .001ms !important; animation-delay: 0ms !important; transition-duration: .001ms !important; }
