:root {
  --bg: #f6f6f3;
  --surface: #ffffff;
  --text: #1d1f23;
  --muted: #6b7078;
  --line: #e2e2dc;
  --accent: #2563eb;
  --hold: #dc2626;
  --ready: #16a34a;
  --tile-off: 0.35;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171b;
    --surface: #1e2127;
    --text: #e8e9ec;
    --muted: #9298a2;
    --line: #2e323a;
    --accent: #60a5fa;
    --hold: #f87171;
    --ready: #4ade80;
    --tile-off: 0.3;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
}
header, main { max-width: 860px; margin: 0 auto; padding: 0 16px; }
header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-top: 16px; }
h1 { font-size: 1.2rem; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 1.1rem; margin: 0; }
h3 { font-size: 0.9rem; margin: 0 0 8px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

button, .button {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover, .button:hover { border-color: var(--muted); }
button.link { border: 0; background: none; padding: 0; color: var(--accent); font-size: 0.85rem; }

.switches { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.tabs { display: flex; gap: 4px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.tabs button { border: 0; padding: 4px 16px; font-weight: 600; color: var(--muted); }
.tabs button[aria-selected="true"] { background: var(--text); color: var(--bg); }

/* --- practice --- */
.practice { text-align: center; padding: 24px 0 8px; }
.hint { margin: 0; color: var(--muted); font-size: 0.9rem; }
.scramble {
  font: 600 clamp(1.15rem, 4vw, 1.6rem)/1.4 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  margin: 8px 0 16px;
  word-spacing: 0.15em;
  min-height: 1.4em;
}
.pad {
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  border-radius: 16px;
  padding: 20px 8px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  cursor: pointer;
  outline: none;
}
.time {
  font: 700 clamp(3.5rem, 16vw, 6.5rem)/1 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-variant-numeric: tabular-nums;
  transition: color 0.1s;
}
.pad-hint { margin: 10px 0 0; font-size: 0.8rem; color: var(--muted); }
.touch-only { display: none; }
@media (pointer: coarse) {
  .kbd-only { display: none; }
  .touch-only { display: inline; }
}
.queue { font-size: 0.85rem; color: var(--muted); margin: 8px 0 0; min-height: 1.3em; }
[data-phase="holding"] .time { color: var(--hold); }
[data-phase="ready"] .time { color: var(--ready); }

/* While the clock runs, only the clock is on screen. */
[data-phase="running"] header,
[data-phase="running"] .result,
[data-phase="running"] .panel,
[data-phase="running"] .hint,
[data-phase="running"] .scramble,
[data-phase="running"] .queue,
[data-phase="running"] .pad-hint { visibility: hidden; }
[data-phase="running"] .pad { border-color: transparent; background: transparent; }

/* --- last result --- */
.result {
  display: flex;
  gap: 16px;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
  margin: 16px 0;
}
.result-diagram { width: 108px; flex: none; }
.result-diagram svg { display: block; width: 100%; }
.result-body { min-width: 0; }
.result-time { font: 700 1.4rem/1.2 ui-monospace, Menlo, monospace; margin: 2px 0 6px; }
.alg { font-family: ui-monospace, Menlo, Consolas, monospace; margin: 0 0 4px; overflow-wrap: anywhere; }
.auf { color: var(--accent); }
.result .muted { margin: 0 0 6px; font-size: 0.9rem; }

/* --- panels --- */
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0 14px;
  margin: 16px 0;
}
.panel > summary { cursor: pointer; padding: 12px 0; font-weight: 600; }
.panel > summary .muted { font-weight: 400; font-size: 0.9rem; margin-left: 6px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 4px 0 14px; }
.mode { margin-left: auto; font-size: 0.9rem; color: var(--muted); display: flex; gap: 6px; align-items: center; }
select { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 4px 6px; }

.group { margin-bottom: 16px; }
.group h3 { display: flex; gap: 10px; align-items: baseline; flex-direction: row-reverse; justify-content: flex-end; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; }
.tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 6px;
  border-radius: 10px;
  font-size: 0.8rem;
  font-weight: 600;
  opacity: var(--tile-off);
}
.tile svg { width: 100%; max-width: 76px; display: block; }
.tile.on { opacity: 1; border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }

.table-wrap { overflow-x: auto; }
.stats { width: 100%; border-collapse: collapse; font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.stats th, .stats td { text-align: right; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.stats th:first-child, .stats td:first-child { text-align: left; }
.stats th { font-weight: 600; color: var(--muted); font-size: 0.8rem; }
.mini { display: inline-block; width: 26px; vertical-align: middle; margin-right: 8px; }
.mini svg { display: block; width: 100%; }

@media (max-width: 520px) {
  .result-diagram { width: 84px; }
  .mode { margin-left: 0; }
}

/* --- learn --- */
.learn-detail {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px;
  margin: 20px 0 16px;
  scroll-margin-top: 12px;
}
.learn-top { display: grid; grid-template-columns: 220px 1fr auto; gap: 20px; align-items: start; }
.learn-cube svg { width: 100%; display: block; }
.learn-title { display: flex; gap: 12px; align-items: center; }
.learn-diagram { width: 64px; flex: none; }
.learn-diagram svg { display: block; width: 100%; }
.learn-title h2 { margin-bottom: 6px; }
.learn-title p { margin: 4px 0 0; }
.learn-alg { font-size: 1.15rem; line-height: 1.9; margin: 14px 0 8px; }
.learn-alg .tok { cursor: pointer; border-radius: 4px; padding: 1px 2px; transition: opacity 0.15s, background 0.15s; }
.learn-alg .tok:hover { background: var(--line); }
.learn-alg .tok.now { background: var(--accent); color: var(--surface); }
.learn-alg .tok.later { opacity: 0.25; }
.learn-alg .tok.focus { font-weight: 700; }
.learn-alg.hidden-alg .tok { filter: blur(6px); }
.step-controls { margin-bottom: 0; }
.step-controls button { min-width: 40px; display: inline-flex; align-items: center; justify-content: center; min-height: 36px; }
.learn-close { align-self: start; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; padding: 3px 10px; font-size: 0.85rem; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button[aria-pressed="true"] { background: var(--text); color: var(--bg); }
.steps { margin: 20px 0 0; padding: 0; list-style: none; counter-reset: step; display: grid; gap: 12px; }
.steps li { counter-increment: step; border-top: 1px solid var(--line); padding-top: 12px; }
.steps h3 { font-size: 0.95rem; margin: 0 0 4px; }
.steps h3::before { content: counter(step) ". "; color: var(--muted); }
.steps p { margin: 0 0 8px; font-size: 0.92rem; }
.steps .toolbar { margin-bottom: 0; }
.check { display: flex; gap: 6px; align-items: center; font-size: 0.92rem; }
.panel-title { font-size: 1rem; margin: 14px 0 2px; }
.panel-title .muted { font-weight: 400; font-size: 0.9rem; margin-left: 6px; }
.learn-cases { padding-bottom: 4px; }
.learn-tile { opacity: 1; box-shadow: none; border-color: var(--line); }
.learn-tile.st-learning { border-color: #d97706; box-shadow: inset 0 0 0 1px #d97706; }
.learn-tile.st-known { border-color: var(--ready); box-shadow: inset 0 0 0 1px var(--ready); }
.learn-tile.open { outline: 2px solid var(--accent); outline-offset: 2px; }
#result-learn { margin-right: 14px; }

@media (max-width: 640px) {
  .learn-top { grid-template-columns: 1fr; }
  .learn-cube { width: 200px; margin: 0 auto; }
  .learn-close { position: absolute; }
  .learn-detail { position: relative; }
  .learn-close { top: 14px; right: 16px; }
  header { flex-wrap: wrap; }
}
