:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --text: #1d1d1f;
  --muted: #64646a;
  --line: #e0ddd4;
  --tile: #ffffff;
  --key: #e6e2d8;
  --key-pressed: #d4cfc2;
  --accent: #2f5bd3;
  --fermi: #1a7a43;
  --fermi-ink: #ffffff;
  --pico: #f2b400;
  --pico-ink: #1d1d1f;
  --bagels: #6e6e73;
  --bagels-ink: #ffffff;
  --danger: #b3261e;
  --danger-text: #b3261e;
  --radius: 10px;
  --tile-w: 36px;
  --tile-h: 42px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121214;
    --surface: #1c1c20;
    --text: #f2f2f3;
    --muted: #a3a3aa;
    --line: #2e2e34;
    --tile: #1c1c20;
    --key: #2b2b31;
    --key-pressed: #3a3a42;
    --accent: #7b9cff;
    --bagels: #5f5f66;
    --danger-text: #ff8a80;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- layout ---------- */

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 520px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 0 4px;
}

.title {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 800;
  letter-spacing: 0.01em;
  text-align: center;
  white-space: nowrap;
}
.title span { color: var(--muted); font-weight: 400; }

.icon-btn {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border: 0; border-radius: 50%;
  background: transparent;
}
.icon-btn:active { background: var(--key); }
.icon-btn svg {
  width: 26px; height: 26px;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.icon-btn svg .dot { fill: currentColor; stroke: none; }

.modes {
  display: flex;
  gap: 4px;
  padding: 4px;
  border-radius: 12px;
  background: var(--key);
}
.modes button {
  flex: 1;
  min-height: 40px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  font-weight: 650;
  font-size: 0.95rem;
  position: relative;
}
.modes button[aria-pressed="true"] { background: var(--surface); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15); }
.modes button.in-progress::after {
  content: "";
  position: absolute; top: 7px; right: 9px;
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--pico);
}
.modes.small button { min-height: 36px; font-size: 0.88rem; }

.banner {
  margin: 8px 0 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: #fdecea;
  color: #5c1610;
  font-size: 0.88rem;
}

.status {
  margin: 8px 0 4px;
  min-height: 1.4em;
  text-align: center;
  color: var(--muted);
  font-size: 0.92rem;
}
.status strong { color: var(--text); }

/* ---------- history ---------- */

.board {
  flex: 1;
  min-height: 0;
  margin: 0;
  padding: 0 2px;
  list-style: none;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.empty {
  margin: 24px 8px;
  text-align: center;
  color: var(--muted);
}

.row {
  display: grid;
  grid-template-columns: 1.6em auto 1fr;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
}
.row.new { animation: rise 0.25s ease-out; }
.row-num { color: var(--muted); font-size: 0.85rem; text-align: right; font-variant-numeric: tabular-nums; }

.tiles { display: flex; gap: 4px; }

.tile {
  width: var(--tile-w);
  height: var(--tile-h);
  display: grid;
  place-items: center;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--tile);
  font-size: 1.35rem;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
}

.clues { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 4px; }

.chip {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 750;
  line-height: 1.5;
  white-space: nowrap;
}
.chip.fermi { background: var(--fermi); color: var(--fermi-ink); }
.chip.pico { background: var(--pico); color: var(--pico-ink); }
.chip.bagels { background: var(--bagels); color: var(--bagels-ink); }

.digits-5 { --tile-w: 30px; --tile-h: 38px; }
.digits-5 .tile { font-size: 1.2rem; }
.digits-5 .row { gap: 8px; }
.digits-5 .chip { font-size: 0.72rem; padding: 1px 5px; }

/* ---------- controls ---------- */

.controls {
  padding: 10px 0 calc(8px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}

.entry {
  display: flex;
  justify-content: center;
  gap: 6px;
  margin-bottom: 10px;
}
.entry .tile {
  width: 46px;
  height: 54px;
  font-size: 1.7rem;
  border-width: 2px;
}
.entry .tile.filled { border-color: var(--text); }
.entry .tile.cursor { border-color: var(--accent); }
.entry.shake { animation: shake 0.35s; }

.notes {
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 4px;
  margin-bottom: 8px;
}
.note {
  height: 34px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: transparent;
  font-size: 0.95rem;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  position: relative;
}
.note.out { color: var(--muted); text-decoration: line-through; background: var(--key); }
.note.in { border-color: var(--fermi); color: var(--fermi); background: color-mix(in srgb, var(--fermi) 12%, transparent); }
.note.out::after, .note.in::after {
  position: absolute; top: -1px; right: 2px;
  font-size: 0.6rem; text-decoration: none;
}
.note.out::after { content: "✕"; }
.note.in::after { content: "✓"; }

.keypad {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 6px;
}
.key {
  min-height: 54px;
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  background: var(--key);
  font-size: 1.45rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  user-select: none;
  -webkit-user-select: none;
}
.key:active:not(:disabled) { background: var(--key-pressed); }
.key:disabled { opacity: 0.35; cursor: default; }
.key.note-out { color: var(--muted); text-decoration: line-through; }
.key.note-in { box-shadow: inset 0 0 0 2px var(--fermi); color: var(--fermi); }
.key.action { font-size: 0.95rem; }
.key.go { background: var(--fermi); color: var(--fermi-ink); }
.key.go:active:not(:disabled) { background: #13603a; }
.key svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }

.sub-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 36px;
  margin-top: 6px;
}
.hint { color: var(--muted); font-size: 0.78rem; text-align: right; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.link-btn {
  flex: none;
  white-space: nowrap;
  min-height: 36px;
  padding: 0 4px;
  border: 0;
  background: transparent;
  color: var(--danger-text);
  font-size: 0.9rem;
  font-weight: 650;
}
.link-btn[hidden] { display: inline-block !important; visibility: hidden; }

.result { text-align: center; padding: 4px 0 2px; }
.result h2 { margin: 0 0 4px; font-size: 1.3rem; }
.result p { margin: 4px 0; color: var(--muted); }
.result .tiles { justify-content: center; margin: 8px 0; }
.result .tile { border-color: var(--fermi); }
.result.lost .tile { border-color: var(--danger); }
.result .points { color: var(--fermi); font-weight: 750; }
.result-actions { display: flex; gap: 8px; margin-top: 10px; }
.result-actions button { flex: 1; }

.primary-btn, .plain-btn, .danger-btn {
  min-height: 48px;
  padding: 0 16px;
  border: 0;
  border-radius: var(--radius);
  font-weight: 700;
}
.primary-btn { background: var(--fermi); color: var(--fermi-ink); }
.plain-btn { background: var(--key); }
.danger-btn { background: var(--danger); color: #fff; }

.toast {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 64px);
  transform: translate(-50%, -8px);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-weight: 650;
  font-size: 0.95rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s, transform 0.15s;
  z-index: 10;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- dialogs ---------- */

dialog {
  width: min(480px, calc(100% - 24px));
  max-height: calc(100dvh - 32px);
  padding: 18px 18px 20px;
  border: 0;
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  overflow-y: auto;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog h2 { margin: 0 0 10px; font-size: 1.25rem; }
dialog h2:focus { outline: none; }
dialog h3 { margin: 18px 0 6px; font-size: 1rem; }
dialog p { margin: 8px 0; }

.dlg-close { float: right; margin: -6px -6px 0 0; }
.dlg-close button {
  width: 44px; height: 44px;
  border: 0; border-radius: 50%;
  background: transparent;
  font-size: 1.2rem;
}

.rules { list-style: none; padding: 0; margin: 10px 0; }
.rules li { display: flex; align-items: center; gap: 10px; margin: 8px 0; }
.rules .chip { min-width: 4.6em; text-align: center; }

.examples { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.examples td { padding: 6px 4px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.examples td:nth-child(2) { white-space: nowrap; }
.mono { font-weight: 750; font-variant-numeric: tabular-nums; letter-spacing: 0.08em; font-size: 1.05rem; }
.fine { color: var(--muted); font-size: 0.85rem; }

#dlg-confirm { width: min(380px, calc(100% - 32px)); }
.dlg-actions { display: flex; gap: 8px; margin-top: 16px; }
.dlg-actions button { flex: 1; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 12px;
}
.stat { text-align: center; padding: 8px 2px; border-radius: 10px; background: var(--bg); }
.stat b { display: block; font-size: 1.35rem; font-variant-numeric: tabular-nums; }
.stat span { display: block; color: var(--muted); font-size: 0.72rem; line-height: 1.2; }

.dist { display: grid; gap: 3px; }
.dist-row { display: grid; grid-template-columns: 1.8em 1fr; align-items: center; gap: 6px; font-size: 0.85rem; }
.dist-row .n { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.dist-bar {
  display: block;
  min-width: 1.8em;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--key);
  text-align: right;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.dist-bar.has { background: var(--bagels); color: var(--bagels-ink); }
.dist-bar.last { background: var(--fermi); color: var(--fermi-ink); }

.recent { list-style: none; margin: 0; padding: 0; font-size: 0.88rem; }
.recent li { display: flex; justify-content: space-between; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.recent .r-main { font-weight: 650; }
.recent .r-sub { color: var(--muted); font-size: 0.8rem; }
.recent .r-pts { font-weight: 750; color: var(--fermi); white-space: nowrap; }
.recent .r-pts.zero { color: var(--muted); }
.recent .empty-recent { color: var(--muted); }

#btn-reset { width: 100%; margin-top: 6px; }

/* ---------- motion ---------- */

@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

@media (prefers-reduced-motion: reduce) {
  .row.new, .entry.shake { animation: none; }
  .toast { transition: none; }
}

/* Short screens (most phones once the browser bars are drawn): tighten the chrome
   so the guess history keeps several rows visible. */
@media (max-height: 720px) {
  .top { padding: 4px 0 2px; }
  .icon-btn { width: 40px; height: 40px; }
  .modes button { min-height: 34px; }
  .status { margin: 4px 0 2px; }
  .controls { padding-top: 8px; }
  .entry { margin-bottom: 8px; }
  .entry .tile { width: 42px; height: 46px; font-size: 1.5rem; }
  .notes { margin-bottom: 6px; }
  .note { height: 30px; }
  .key { min-height: 48px; }
  .sub-actions { min-height: 30px; margin-top: 4px; }
  .link-btn { min-height: 30px; }
}
@media (max-height: 600px) {
  .key { min-height: 44px; }
  .entry .tile { width: 40px; height: 42px; }
  .note { height: 28px; }
  .row { padding: 4px 0; }
  .digits-5 { --tile-h: 34px; }
}
