:root {
  --bg: #f4efe6;
  --panel: #fffaf2;
  --ink: #2a2530;
  --muted: #7a7182;
  --line: #d8cfc2;
  --wall: #3a3340;
  --accent: #a3283a;
  --accent-ink: #fff;
  --good: #2f7d4f;
  --bad: #c0392b;
  --room-l: 88%;
  --room-s: 55%;
  --block-l: 74%;
  --shadow: 0 2px 10px rgba(40, 30, 50, 0.12);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1a24;
    --panel: #26232f;
    --ink: #ece6f2;
    --muted: #9b93a6;
    --line: #3b3746;
    --wall: #0d0c12;
    --accent: #e0566a;
    --accent-ink: #fff;
    --good: #5fc488;
    --bad: #ff6b5b;
    --room-l: 31%;
    --room-s: 42%;
    --block-l: 19%;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1c1a24; --panel: #26232f; --ink: #ece6f2; --muted: #9b93a6; --line: #3b3746;
  --wall: #0d0c12; --accent: #e0566a; --good: #5fc488; --bad: #ff6b5b;
  --room-l: 31%; --room-s: 42%; --block-l: 19%; color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }
.hidden { display: none !important; }

#app { height: 100dvh; max-width: 560px; margin: 0 auto; }
.screen {
  height: 100%;
  padding: env(safe-area-inset-top) max(16px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(16px, env(safe-area-inset-left));
}

/* ---------- Home ---------- */
#home { display: flex; align-items: center; justify-content: center; overflow-y: auto; }
.home-inner { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 12px; text-align: center; padding: 24px 0; }
.logo { font-size: 56px; }
h1 { font-family: Georgia, "Times New Roman", serif; font-size: 40px; margin: 0; letter-spacing: 0.5px; }
.tagline { color: var(--muted); margin: 0 0 12px; }
.btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 12px;
  padding: 13px 16px; font-weight: 600; box-shadow: var(--shadow);
}
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn-ghost { background: transparent; box-shadow: none; border-color: transparent; color: var(--muted); }
.new-case { display: flex; flex-direction: column; gap: 6px; margin-top: 4px; }
.new-case .label { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stats { font-size: 13px; color: var(--muted); margin-top: 8px; line-height: 1.7; }

/* ---------- Game ---------- */
#game { display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 8px 0 6px; }
.icon-btn { background: none; border: 0; font-size: 34px; line-height: 1; padding: 0 10px 4px 0; color: var(--ink); }
.title { flex: 1; min-width: 0; }
#case-title { font-family: Georgia, serif; font-weight: 700; font-size: 18px; }
.sub { font-size: 12px; color: var(--muted); }
.timer { font-variant-numeric: tabular-nums; color: var(--muted); font-weight: 600; }

.board-area { flex: none; }
.board-frame {
  --label: 16px;
  display: grid;
  grid-template-columns: var(--label) 1fr;
  grid-template-rows: var(--label) auto;
  width: min(100%, calc(42dvh + var(--label)));
  margin: 0 auto;
}
.compass { font-size: 10px; font-weight: 700; color: var(--muted); display: flex; align-items: center; justify-content: center; }
.col-labels, .row-labels { display: grid; font-size: 11px; color: var(--muted); font-weight: 600; }
.col-labels > div, .row-labels > div { display: flex; align-items: center; justify-content: center; }
.board {
  display: grid; aspect-ratio: 1;
  border: 3px solid var(--wall); border-radius: 6px; overflow: hidden;
  background: var(--wall); box-shadow: var(--shadow);
}
.cell {
  position: relative; display: flex; align-items: center; justify-content: center;
  background: hsl(var(--h) var(--room-s) var(--room-l));
  border: 0 solid var(--wall);
  border-right-width: 1px; border-bottom-width: 1px;
  border-color: color-mix(in srgb, var(--wall) 22%, transparent);
  font-size: calc(var(--cell) * 0.56);
  line-height: 1;
  container-type: size;
}
.cell.wt { border-top: 2.5px solid var(--wall); }
.cell.wl { border-left: 2.5px solid var(--wall); }
.cell.wr { border-right: 2.5px solid var(--wall); }
.cell.wb { border-bottom: 2.5px solid var(--wall); }
.cell.blocked { background: hsl(var(--h) var(--room-s) var(--block-l)); }
.cell.rug::before {
  content: ""; position: absolute; inset: 14%;
  border-radius: 3px;
  background: repeating-linear-gradient(45deg, rgba(150, 60, 50, 0.35) 0 3px, rgba(200, 140, 70, 0.3) 3px 6px);
  border: 1.5px solid rgba(150, 60, 50, 0.55);
}
.cell.covered::after {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: repeating-linear-gradient(-45deg, transparent 0 4px, rgba(0, 0, 0, 0.07) 4px 6px);
}
.cell .item { position: relative; z-index: 1; }
.cell.has-person .item { position: absolute; right: 2px; bottom: 1px; font-size: 38%; opacity: 0.85; z-index: 3; }
.cell .mark { position: relative; z-index: 1; color: var(--muted); font-size: 60%; font-weight: 700; opacity: 0.8; }
.token {
  position: relative; z-index: 2;
  width: 84%; height: 84%; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--panel);
  border: 2.5px solid hsl(var(--ph) 60% 50%);
  font-size: 75%;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
.token.victim { border-style: dashed; border-color: var(--ink); }
.token.locked { border-color: var(--good); border-style: double; border-width: 4px; }
.token.conflict { border-color: var(--bad); animation: pulse 1s ease-in-out infinite; }
.token.wrong { background: color-mix(in srgb, var(--bad) 35%, var(--panel)); }
.token.selected { outline: 3px solid var(--accent); outline-offset: 1px; }
@keyframes pulse { 50% { transform: scale(0.9); } }
.board.placing .cell:not(.blocked):not(.has-person) { cursor: pointer; }
.board.placing .cell:not(.blocked):not(.has-person):active { filter: brightness(1.15); }

.legend { display: flex; flex-wrap: wrap; justify-content: center; gap: 2px 10px; font-size: 11.5px; padding: 6px 0 0; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 4px; }
.legend i { width: 11px; height: 11px; border-radius: 3px; background: hsl(var(--h) var(--room-s) var(--room-l)); border: 1px solid var(--line); }

.toolbar { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 6px 0 2px; }
.tool {
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  border: 1px solid var(--line); background: var(--panel); border-radius: 10px;
  padding: 6px 2px; font-size: 11px; font-weight: 600; color: var(--ink);
}
.tool span { font-size: 17px; line-height: 1.1; }
.tool:disabled { opacity: 0.4; }
.tool.accent:not(:disabled) { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.status { font-size: 12.5px; color: var(--muted); text-align: center; min-height: 20px; padding: 3px 0; }

.people { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: 16px; display: flex; flex-direction: column; gap: 8px; }
.person {
  flex: none;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  border-left: 5px solid hsl(var(--ph) 60% 50%);
  overflow: hidden;
}
.person.selected { outline: 2.5px solid var(--accent); }
.person-head { display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px 10px; background: none; border: 0; text-align: left; }
.person-head .emoji { font-size: 26px; width: 32px; text-align: center; }
.person-head .who { flex: 1; min-width: 0; }
.person-head .name { font-weight: 700; }
.person-head .role { font-size: 12px; color: var(--muted); }
.person-head .where { font-size: 12px; font-weight: 700; color: var(--muted); padding: 3px 8px; border-radius: 8px; border: 1px solid var(--line); }
.person-head .where.placed { color: var(--good); border-color: var(--good); }
.person.victim .role { color: var(--accent); font-weight: 600; }
.clues { list-style: none; margin: 0; padding: 0 10px 8px 52px; display: flex; flex-direction: column; gap: 2px; }
.clue { font-size: 14px; padding: 4px 0; cursor: pointer; }
.clue.done { text-decoration: line-through; color: var(--muted); opacity: 0.6; }
.clue.none { color: var(--muted); font-style: italic; cursor: default; }

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0; z-index: 10; background: rgba(10, 8, 14, 0.55);
  display: flex; align-items: center; justify-content: center; padding: 16px;
  -webkit-user-select: text; user-select: text;
}
.modal-card {
  background: var(--panel); color: var(--ink); border-radius: 16px; padding: 20px;
  width: 100%; max-width: 420px; max-height: 85dvh; overflow-y: auto; box-shadow: var(--shadow);
}
.modal-card h2 { font-family: Georgia, serif; margin: 0 0 8px; }
.modal-card p { margin: 8px 0; }
.rules { padding-left: 20px; }
.rules li { margin-bottom: 8px; }
.modal-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }
.suspect-pick { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.suspect-pick button { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 10px 6px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); }
.suspect-pick .e { font-size: 30px; }
.suspect-pick .n { font-weight: 600; font-size: 13px; }
.big-emoji { font-size: 54px; text-align: center; }
.result-stats { display: flex; justify-content: center; gap: 24px; margin: 12px 0; font-weight: 700; }
.result-stats div { text-align: center; }
.result-stats small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 20px; font-size: 14px;
  z-index: 20; box-shadow: var(--shadow); transition: opacity 0.3s; max-width: 90vw; text-align: center;
}
