:root {
  color-scheme: light;
  --bg: #f3ead8;
  --paper: #fffaf0;
  --ink: #2a2420;
  --muted: #7a6d60;
  --line: #d9ccb4;
  --line-strong: #8a7a64;
  --accent: #a3262a;
  --accent-ink: #fff;
  --yes: #2f7d4a;
  --no: #a3262a;
  --chip: #f1e6d0;
  --shadow: 0 1px 2px rgba(60, 40, 20, .08), 0 4px 14px rgba(60, 40, 20, .06);
  --serif: "Iowan Old Style", "Palatino", Georgia, serif;
  --mono: ui-monospace, "SF Mono", Menlo, "Courier New", monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}
/* Dark theme: follows the system unless the player picked a theme (data-theme on <html>). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #16181d;
    --paper: #1f2229;
    --ink: #ece4d4;
    --muted: #9c9384;
    --line: #343843;
    --line-strong: #6d6a62;
    --accent: #e0a243;
    --accent-ink: #1a1a1a;
    --yes: #5cc47f;
    --no: #e8676b;
    --chip: #2a2e37;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #16181d;
  --paper: #1f2229;
  --ink: #ece4d4;
  --muted: #9c9384;
  --line: #343843;
  --line-strong: #6d6a62;
  --accent: #e0a243;
  --accent-ink: #1a1a1a;
  --yes: #5cc47f;
  --no: #e8676b;
  --chip: #2a2e37;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3);
}
:root[data-theme="light"] { color-scheme: light; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.45;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }
ul, ol { margin: 0; padding: 0; list-style: none; }

.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 6px;
  max-width: 560px; margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand h1 { font-family: var(--serif); font-size: 28px; margin: 0; letter-spacing: .5px; }
.logo { font-size: 24px; }
.top-actions { display: flex; gap: 8px; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--paper); font-weight: 700;
}

.controls {
  display: flex; gap: 8px; flex-wrap: wrap; justify-content: space-between;
  padding: 6px 16px 10px; max-width: 560px; margin: 0 auto;
}
.seg { display: flex; background: var(--chip); border-radius: 10px; padding: 3px; }
.seg button {
  border: 0; background: transparent; padding: 6px 11px; border-radius: 8px;
  font-size: 14px; font-weight: 600; color: var(--muted);
}
.seg button.on { background: var(--paper); color: var(--ink); box-shadow: var(--shadow); }

main { max-width: 560px; margin: 0 auto; padding: 0 16px; }
.card {
  background: var(--paper); border: 1px solid var(--line); border-radius: 14px;
  padding: 14px; margin-bottom: 12px; box-shadow: var(--shadow);
}
.card h3 {
  font-family: var(--serif); font-size: 17px; margin: 4px 0 8px;
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
}
.card h3:not(:first-child) { margin-top: 14px; }
.hint { font-family: var(--sans); font-size: 12px; font-weight: 400; color: var(--muted); }

.case-meta {
  display: flex; justify-content: space-between; font-size: 12px; text-transform: uppercase;
  letter-spacing: 1px; color: var(--muted); font-weight: 600;
}
#timer { font-family: var(--mono); letter-spacing: 0; }
.case h2 { font-family: var(--serif); font-size: 24px; margin: 4px 0 6px; line-height: 1.2; }
.case p { margin: 0; color: var(--ink); }
.banner {
  margin-top: 10px; padding: 10px 12px; border-radius: 10px; font-weight: 600;
  background: var(--chip); display: flex; justify-content: space-between; align-items: center; gap: 8px;
}
.banner button { border: 1px solid var(--line-strong); background: transparent; border-radius: 8px; padding: 4px 10px; font-size: 14px; }

.suspects li {
  display: flex; gap: 10px; align-items: center; padding: 6px 0;
  border-bottom: 1px dashed var(--line);
}
.suspects li:last-child { border-bottom: 0; }
.suspects .em { font-size: 26px; width: 34px; text-align: center; flex: none; }
.suspects .nm { font-weight: 600; }
.suspects .bl { font-size: 13px; color: var(--muted); }
.tag {
  display: inline-block; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px;
  padding: 1px 6px; border-radius: 6px; background: var(--chip); color: var(--muted); margin-left: 6px;
  vertical-align: 1px;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips li {
  background: var(--chip); border-radius: 10px; padding: 5px 9px; font-size: 14px;
  display: flex; align-items: center; gap: 5px;
}
.chips .tag { background: var(--paper); margin-left: 2px; }

.clues { counter-reset: c; }
.clues li {
  counter-increment: c; position: relative; padding: 7px 4px 7px 30px;
  font-family: var(--serif); font-size: 16px; border-bottom: 1px dashed var(--line);
  cursor: pointer; user-select: none; -webkit-user-select: none;
}
.clues li::before {
  content: counter(c); position: absolute; left: 2px; top: 8px; width: 20px; height: 20px;
  border-radius: 50%; background: var(--chip); font: 600 12px/20px var(--sans); text-align: center; color: var(--muted);
}
.clues li.done { color: var(--muted); text-decoration: line-through; text-decoration-color: var(--line-strong); }
.murder-clue {
  margin: 12px 0 0; padding: 10px 12px; border-radius: 10px; font-family: var(--serif); font-weight: 600;
  border: 2px solid var(--accent); color: var(--ink);
}
.murder-clue { display: flex; gap: 8px; align-items: flex-start; }
.murder-clue .ic { color: var(--accent); width: 20px; height: 20px; margin-top: 1px; }

/* ---------- Grid ---------- */
.grid {
  --cols: 9;
  display: grid;
  grid-template-columns: minmax(34px, 44px) repeat(calc(var(--cols) - 1), 1fr);
  width: 100%;
  user-select: none; -webkit-user-select: none;
}
.grid .c {
  aspect-ratio: 1; display: flex; align-items: center; justify-content: center;
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  font-size: clamp(14px, 4.4vw, 20px); font-weight: 700; padding: 0; background: var(--paper);
  border-top: 0; border-left: 0; min-width: 0;
}
.grid .h {
  background: transparent; border: 0; font-size: clamp(16px, 5vw, 24px); font-weight: 400; cursor: pointer;
}
.grid .h.col { border-bottom: 2px solid var(--line-strong); }
.grid .h.row { border-right: 2px solid var(--line-strong); }
.grid .corner { background: transparent; border: 0; }
.grid .c.bl { border-left: 2px solid var(--line-strong); }
.grid .c.br { border-right: 2px solid var(--line-strong); }
.grid .c.bb { border-bottom: 2px solid var(--line-strong); }
.grid .c.bt { border-top: 2px solid var(--line-strong); }
.grid .blank { background: transparent; border: 0; }
.grid .cell.s1 { color: var(--no); }
.grid .cell::after { content: ""; width: 58%; height: 58%; background: currentColor; -webkit-mask: var(--m) center / contain no-repeat; mask: var(--m) center / contain no-repeat; }
.grid .cell.s0::after { display: none; }
.grid .cell.s1 { --m: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M18 6 6 18M6 6l12 12'/%3E%3C/svg%3E"); }
.grid .cell.s2 { color: var(--yes); background: color-mix(in srgb, var(--yes) 14%, var(--paper)); }
.grid .cell.s2 { --m: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.75' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 6 9 17l-5-5'/%3E%3C/svg%3E"); }
.grid .cell.err { animation: err 1.8s ease; }
@keyframes err { 0%, 60% { background: color-mix(in srgb, var(--no) 45%, var(--paper)); } 100% { background: var(--paper); } }
.grid .cell:active { background: var(--chip); }

.grid-tools { display: flex; gap: 6px; margin-top: 12px; flex-wrap: wrap; align-items: center; }
.grid-tools button {
  border: 1px solid var(--line); background: var(--chip); border-radius: 9px; padding: 6px 11px; font-size: 14px; font-weight: 600;
}
.toggle { margin-left: auto; font-size: 14px; display: flex; align-items: center; gap: 5px; color: var(--muted); }
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); }

.spacer { height: calc(80px + env(safe-area-inset-bottom)); }

.accuse-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; gap: 10px; justify-content: center;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.primary, .secondary {
  border: 0; border-radius: 12px; padding: 13px 22px; font-weight: 700; font-size: 17px;
  max-width: 360px; flex: 1;
}
.primary { background: var(--accent); color: var(--accent-ink); box-shadow: var(--shadow); }
.primary:disabled { opacity: .4; }
.secondary { background: var(--paper); border: 1px solid var(--line-strong); }

.toast {
  position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 12px); transform: translate(-50%, -20px);
  background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 10px; font-size: 14px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: all .2s; z-index: 20; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0; z-index: 10; background: rgba(10, 8, 6, .5);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal[hidden] { display: none; }
.modal-box {
  position: relative; background: var(--paper); width: 100%; max-width: 560px; max-height: 88vh; overflow-y: auto;
  border-radius: 18px 18px 0 0; padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
  animation: up .22s ease;
}
@media (min-width: 600px) { .modal { align-items: center; } .modal-box { border-radius: 18px; } }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.modal-close {
  position: absolute; right: 10px; top: 8px; border: 0; background: transparent; font-size: 28px; color: var(--muted); width: 40px; height: 40px;
}
.modal h2 { font-family: var(--serif); margin: 0 0 10px; font-size: 24px; }
.modal h4 { margin: 14px 0 6px; font-size: 14px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
.modal p, .modal li { font-size: 15px; }
.modal ul.howto { list-style: disc; padding-left: 20px; }
.modal ul.howto li { margin-bottom: 6px; }

.pick { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.pick button {
  display: flex; align-items: center; gap: 6px; text-align: left; padding: 8px 10px; border-radius: 10px;
  border: 2px solid var(--line); background: var(--chip); font-size: 14px; font-weight: 600; line-height: 1.2;
}
.pick button .em { font-size: 20px; }
.pick button.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--paper)); }
.modal .primary { width: 100%; max-width: none; margin-top: 16px; }

.result-head { text-align: center; }
.result-head .big { font-size: 54px; line-height: 1; }
.sol { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: 14px; }
.sol td { padding: 6px 4px; border-bottom: 1px dashed var(--line); }
.sol tr.killer td { color: var(--accent); font-weight: 700; }
.row-btns { display: flex; gap: 8px; margin-top: 14px; }
.row-btns button { flex: 1; margin-top: 0 !important; }

.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.stats-grid .box { background: var(--chip); border-radius: 10px; padding: 8px 4px; }
.stats-grid .num { font-size: 22px; font-weight: 700; font-family: var(--serif); }
.stats-grid .lbl { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }

/* ---------- Line icons ---------- */
.ic { width: 1.15em; height: 1.15em; flex: none; vertical-align: -0.2em; }
.logo { display: flex; color: var(--accent); }
.logo .ic { width: 26px; height: 26px; }
.icon-btn { display: flex; align-items: center; justify-content: center; padding: 0; color: var(--ink); }
.icon-btn .ic { width: 20px; height: 20px; }
.suspects .em { display: flex; justify-content: center; color: var(--accent); }
.suspects .em .ic { width: 24px; height: 24px; }
.chips li .ic { color: var(--accent); width: 17px; height: 17px; }
.grid .h { color: var(--ink); }
.grid .h .ic { width: 62%; height: 62%; max-width: 24px; max-height: 24px; }
.grid-tools button { display: inline-flex; align-items: center; gap: 4px; }
.grid-tools .ic { width: 15px; height: 15px; }
.pick button .em { display: flex; color: var(--accent); }
.pick button .em .ic { width: 20px; height: 20px; }
.sol td .ic { width: 16px; height: 16px; margin-right: 5px; color: var(--muted); }
.sol tr.killer td .ic { color: var(--accent); }
.sol .ic.mark { margin: 0 0 0 5px; }
.banner span { display: inline-flex; align-items: center; gap: 6px; }
.banner .ok .ic, .big.ok { color: var(--yes); }
.banner .bad .ic, .big.bad { color: var(--no); }
.result-head .big { display: flex; justify-content: center; margin-bottom: 6px; }
.result-head .big .ic { width: 56px; height: 56px; stroke-width: 1.4; }
