:root {
  --fg: #1a1a1a;
  --fg-soft: #555;
  --fg-muted: #888;
  --accent: #b33;
  --bg: #f7f6f2;
  --card: #fff;
  --card-hover: #f3f0e8;
  --border: #e2dfd5;
  --border-strong: #ccc8bb;
  --shadow: rgba(0,0,0,0.08);
  --overlay: rgba(0,0,0,0.25);
}
@media (prefers-color-scheme: dark) {
  :root {
    --fg: #e8e6df;
    --fg-soft: #999;
    --fg-muted: #777;
    --accent: #e66;
    --bg: #141416;
    --card: #1c1c20;
    --card-hover: #26262c;
    --border: #2a2a30;
    --border-strong: #3a3a42;
    --shadow: rgba(0,0,0,0.3);
    --overlay: rgba(0,0,0,0.5);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  color: var(--fg);
  background: var(--bg);
  padding: 0 1rem 3rem;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── header ─────────────────────────────────────────── */

.site-header {
  max-width: 760px;
  margin: 0 auto;
  padding: 1.5rem 0 0;
}
.header-top { margin-bottom: 1rem; }
.header-top h1 {
  margin: 0;
  font-size: 1.7rem;
  font-weight: 700;
  letter-spacing: -0.015em;
}
.header-top h1 a { color: var(--fg); }
.tagline {
  margin: 0.15rem 0 0;
  color: var(--fg-muted);
  font-size: 0.9rem;
}

.search-area {
  position: relative;
  margin-bottom: 1.5rem;
  z-index: 10;
}
.search-bar-row {
  position: relative;
}
#q {
  width: 100%;
  padding: 0.7rem 2.4rem 0.7rem 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--fg);
  font-size: 1rem;
  font-family: inherit;
  transition: border-color .15s, box-shadow .15s;
}
#q:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 2px rgba(187,51,51,0.15); }
#q:disabled { opacity: 0.5; cursor: not-allowed; }
#q::placeholder { color: var(--fg-muted); opacity: 0.7; }

#clear-btn {
  position: absolute;
  right: 0.5rem;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--fg-muted);
  font-size: 1.3rem;
  cursor: pointer;
  padding: 0.2rem 0.4rem;
  line-height: 1;
  border-radius: 4px;
}
#clear-btn:hover { background: var(--border); }
#clear-btn.hidden { display: none; }

#status {
  font-size: 0.8rem;
  color: var(--fg-muted);
  margin-top: 0.3rem;
  min-height: 1.1em;
}

/* ── search results dropdown ────────────────────────── */

#results.hidden { display: none; }
#results {
  position: absolute;
  top: calc(100% + 0.25rem);
  left: 0;
  right: 0;
  max-height: min(70vh, 600px);
  overflow-y: auto;
  background: var(--card);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  box-shadow: 0 8px 24px var(--shadow);
  z-index: 20;
  padding: 0.5rem;
}
.hit {
  padding: 0.7rem 0.75rem;
  border-radius: 6px;
  cursor: default;
  border-bottom: 1px solid var(--border);
}
.hit:last-child { border-bottom: none; }
.hit:hover { background: var(--card-hover); }

.hit .meta {
  font-size: 0.8rem;
  color: var(--fg-muted);
  margin-bottom: 0.3rem;
  display: flex;
  gap: 0.4rem;
  align-items: baseline;
  flex-wrap: wrap;
}
.hit .meta .kind {
  text-transform: uppercase;
  font-size: 0.6rem;
  letter-spacing: 0.06em;
  background: var(--bg);
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  border: 1px solid var(--border);
  color: var(--fg-soft);
}
.hit .meta .source { font-weight: 600; }
.hit .meta a.source { color: var(--accent); }
.hit .meta .section { color: var(--fg-muted); }
.hit .meta .dist { margin-left: auto; font-variant-numeric: tabular-nums; font-size: 0.7rem; }
.hit .text {
  font-size: 0.88rem;
  line-height: 1.5;
  word-wrap: break-word;
}
.hit .text strong { font-weight: 600; }
.hit .text em { font-style: italic; }
.hit .text code {
  font-size: 0.85em;
  background: var(--bg);
  padding: 0.1em 0.35em;
  border-radius: 3px;
  border: 1px solid var(--border);
}
.hit .text a { color: var(--accent); font-weight: 500; }
.hit .text a:hover { text-decoration: underline; }
.hit .text .md-li {
  display: block;
  padding-left: 1.2em;
  position: relative;
  margin: 0.1em 0;
}
.hit .text .md-li::before {
  content: "–";
  position: absolute;
  left: 0.2em;
  color: var(--fg-muted);
}

/* ── results backdrop ───────────────────────────────── */

#results-backdrop.hidden { display: none; }
#results-backdrop {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: 15;
}

/* ── game detail view ────────────────────────────── */

#detail-view.hidden { display: none; }
#detail-view {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 0 3rem;
}
.detail-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
}
.detail-back {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.3rem 0.7rem;
  font-size: 0.85rem;
  color: var(--fg-soft);
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
}
.detail-back:hover { background: var(--card-hover); border-color: var(--border-strong); }
.detail-title {
  font-size: 1.15rem;
  font-weight: 600;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.detail-body {
  font-size: 0.95rem;
  line-height: 1.65;
}
.detail-body h2 {
  font-size: 1.2rem;
  font-weight: 600;
  margin: 1.5rem 0 0.5rem;
  padding-bottom: 0.25rem;
  border-bottom: 1px solid var(--border);
}
.detail-body strong { font-weight: 600; }
.detail-body em { font-style: italic; }
.detail-body code {
  font-size: 0.85em;
  background: var(--card);
  padding: 0.1em 0.35em;
  border-radius: 3px;
  border: 1px solid var(--border);
}
.detail-body a { color: var(--accent); }
.detail-body a:hover { text-decoration: underline; }
.detail-body li {
  padding-left: 0.5em;
  margin: 0.15em 0;
}
.detail-body blockquote {
  margin: 0.75rem 0;
  padding: 0.5rem 1rem;
  border-left: 3px solid var(--border-strong);
  background: var(--card);
  border-radius: 0 6px 6px 0;
  font-size: 0.92rem;
  color: var(--fg-soft);
}
.detail-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1rem 0;
}
.detail-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.75rem 0;
  font-size: 0.88rem;
}
.detail-body th, .detail-body td {
  padding: 0.2rem 0.5rem;
  border: 1px solid var(--border);
  text-align: left;
}
.detail-body th {
  background: var(--card);
  font-weight: 600;
}
.detail-body p { margin: 0.6rem 0; }

/* ── category tile grid ────────────────────────────── */

.tile-grid {
  max-width: 900px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
  gap: 1rem;
}

.tile {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.1rem;
  position: relative;
  transition: border-color .15s, box-shadow .15s;
  cursor: default;
}
.tile:hover {
  border-color: var(--border-strong);
  box-shadow: 0 2px 8px var(--shadow);
}

.tile-header {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  margin-bottom: 0.6rem;
}
.tile-icon {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
}
.tile-icon svg {
  width: 100%;
  height: 100%;
  display: block;
}
.tile h3 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.3;
  padding-top: 0.15rem;
}
.tile h3 .count {
  font-weight: 400;
  color: var(--fg-muted);
  font-size: 0.8rem;
  margin-left: 0.3em;
}

.tile-blurb {
  font-size: 0.82rem;
  color: var(--fg-soft);
  line-height: 1.4;
  margin-bottom: 0.7rem;
}

.tile-game-list {
  list-style: none;
  margin: 0;
  padding: 0;
}
.tile-game-list li {
  font-size: 0.82rem;
  padding: 0.2rem 0;
  display: flex;
  align-items: baseline;
  gap: 0.4rem;
}
.tile-game-list li a {
  font-weight: 500;
  color: var(--fg);
  cursor: pointer;
}
.tile-game-list li a:hover { color: var(--accent); text-decoration: none; }
.tile-game-list .sol-badge {
  font-size: 0.6rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.05rem 0.35rem;
  border-radius: 3px;
  border: 1px solid var(--border);
  color: var(--fg-muted);
  white-space: nowrap;
  flex-shrink: 0;
}
.sol-badge.solved { border-color: #4a8; color: #4a8; }
.sol-badge.unsolved { border-color: #c66; color: #c66; }
.sol-badge.partial { border-color: #ca6; color: #ca6; }

.tile-toggle {
  display: inline-block;
  font-size: 0.78rem;
  color: var(--fg-muted);
  cursor: pointer;
  margin-top: 0.2rem;
  padding: 0;
  background: none;
  border: none;
  font-family: inherit;
}
.tile-toggle:hover { color: var(--accent); }

.tile-game-list.extra { display: none; }
.tile-game-list.extra.open { display: block; }

/* ── hidden state (for search/grid toggle) ──────────── */
#category-grid.searching { opacity: 0.15; pointer-events: none; }

/* ── footer ──────────────────────────────────────────── */

.site-footer {
  max-width: 760px;
  margin: 3rem auto 0;
  font-size: 0.8rem;
  color: var(--fg-muted);
}
.site-footer summary { cursor: pointer; color: var(--fg-soft); }
.site-footer p { margin: 0.7rem 0; }
.site-footer a { color: var(--accent); }

/* ── responsive ───────────────────────────────────────── */

@media (max-width: 640px) {
  .tile-grid {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }
  .header-top h1 { font-size: 1.4rem; }
  #results { max-height: min(80vh, 500px); }
}
