:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --stage: #dfe3e8;
  --text: #16191d;
  --muted: #5d6670;
  --line: #d9dde3;
  --accent: #c62828;
  --accent-2: #1f4e8c;
  --focus: #1e88e5;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111418;
    --surface: #1a1e23;
    --surface-2: #20252b;
    --stage: #0c0f12;
    --text: #e8eaed;
    --muted: #9aa3ad;
    --line: #2c333b;
    --accent: #ef5350;
    --accent-2: #7aa7e8;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #111418;
  --surface: #1a1e23;
  --surface-2: #20252b;
  --stage: #0c0f12;
  --text: #e8eaed;
  --muted: #9aa3ad;
  --line: #2c333b;
  --accent: #ef5350;
  --accent-2: #7aa7e8;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}

button, input, select { font: inherit; color: inherit; }

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 10px; margin-right: auto; }
a.brand { color: inherit; text-decoration: none; }
.brand svg { width: 34px; height: 34px; flex: none; }
.brand h1 { margin: 0; font-size: 17px; font-weight: 700; letter-spacing: 0.01em; }
.brand p { margin: 0; font-size: 12px; color: var(--muted); }

.tabs { display: flex; gap: 4px; overflow-x: auto; }
.tabs button {
  border: 0;
  background: transparent;
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--muted);
  font-weight: 600;
  white-space: nowrap;
}
.tabs button:hover { background: var(--surface-2); color: var(--text); }
.tabs button.active { background: var(--text); color: var(--surface); }

/* segmented controls */
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--surface-2); }
.seg button { border: 0; background: transparent; padding: 6px 10px; cursor: pointer; font-weight: 600; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.active { background: var(--text); color: var(--surface); }

main { flex: 1; min-height: 0; display: flex; flex-direction: column; }

.panel { flex: 1; min-height: 0; }
.panel[hidden] { display: none !important; }

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

.analog { display: grid; grid-template-columns: minmax(0, 1fr) 330px; min-height: 0; }

.stage { position: relative; background: var(--stage); min-height: 420px; }
.stage canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  outline: none;
  cursor: grab;
}
.stage canvas:focus-visible { box-shadow: inset 0 0 0 2px var(--focus); }
.stage canvas.panning { cursor: grabbing; }
#windCanvas[data-tool="dot"], #windCanvas[data-tool="line"], #windCanvas[data-tool="pen"] { cursor: crosshair; }
#windCanvas[data-tool="eraser"] { cursor: cell; }

.zoom { position: absolute; right: 12px; bottom: 12px; display: flex; flex-direction: column; gap: 6px; }
.zoom button {
  width: 36px; height: 36px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: 18px;
  cursor: pointer;
}

.hover-readout {
  position: absolute;
  left: 12px; top: 12px;
  background: rgba(20, 24, 28, 0.82);
  color: #fff;
  padding: 6px 10px;
  border-radius: 8px;
  font: 12px/1.5 "Roboto Condensed", Inter, sans-serif;
  pointer-events: none;
  font-variant-numeric: tabular-nums;
}
.hover-readout b { font-weight: 600; color: #ffcdd2; }
.hover-readout .dim { opacity: 0.7; font-size: 11px; }

.side {
  background: var(--surface);
  border-left: 1px solid var(--line);
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.box { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; background: var(--surface); }
.box h2 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.box h2:not(:first-child) { margin-top: 14px; }
.box summary { cursor: pointer; font-weight: 600; color: var(--muted); }
.keys ul { margin: 8px 0 0; padding-left: 18px; color: var(--muted); font-size: 13px; }
kbd { font: 11px/1 ui-monospace, monospace; padding: 2px 5px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; background: var(--surface-2); }

.label { display: block; font-size: 12px; color: var(--muted); margin: 10px 0 5px; font-weight: 500; }

.tools { display: grid; grid-template-columns: repeat(5, 1fr); width: 100%; }
.tools button { display: flex; flex-direction: column; align-items: center; gap: 1px; padding: 6px 2px; font-size: 11px; }
.tools .ic { font-size: 16px; line-height: 1.1; }

.colors { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.swatch, .custom-color {
  width: 28px; height: 28px;
  border-radius: 50%;
  border: 2px solid var(--surface);
  box-shadow: 0 0 0 1px var(--line);
  background: var(--c);
  cursor: pointer;
  padding: 0;
}
.swatch.active { box-shadow: 0 0 0 2px var(--text); }
.custom-color { background: conic-gradient(#e53935, #fb8c00, #fdd835, #43a047, #1e88e5, #8e24aa, #e53935); position: relative; overflow: hidden; }
.custom-color input { opacity: 0; position: absolute; inset: 0; width: 100%; height: 100%; cursor: pointer; }

.range { display: flex; align-items: center; gap: 10px; margin-top: 10px; font-size: 12px; color: var(--muted); }
.range input { flex: 1; accent-color: var(--accent); }

.btnrow { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.btn {
  border: 1px solid var(--line);
  background: var(--surface-2);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  font-weight: 600;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--accent); }
.btn.ghost { background: transparent; color: var(--accent-2); border-color: transparent; padding-left: 0; }
.box > .btn { margin-top: 10px; }

.nudge { display: grid; grid-template-columns: auto auto 1fr auto auto; gap: 4px; }
.nudge:not(:has(input)) { grid-template-columns: repeat(4, 1fr); }
.nudge button { border: 1px solid var(--line); background: var(--surface-2); border-radius: 6px; padding: 6px 8px; cursor: pointer; font-size: 12px; font-weight: 600; }
.nudge input, .field input, .field select {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 8px;
  background: var(--surface);
  font-variant-numeric: tabular-nums;
}
.nudge input { text-align: center; font-weight: 600; }

.check { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 13px; cursor: pointer; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }

.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.field.wide { grid-column: 1 / -1; }
.box > .field.wide { margin-bottom: 8px; }
.field.solved input { background: var(--surface-2); border-style: dashed; }

.steps ol { margin: 12px 0 0; padding-left: 20px; display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.steps li.todo { color: var(--muted); opacity: 0.6; }
.steps li.done { color: var(--muted); }
.steps li.current { color: var(--text); font-weight: 500; }
.steps li.current::marker { color: var(--accent); font-weight: 700; }
.steps b { color: var(--accent); font-variant-numeric: tabular-nums; }
.guide-title { margin: 0; font-weight: 600; }
.guide-title:empty { display: none; }
.warn { color: var(--accent); margin: 8px 0 0; }
.hint { color: var(--muted); font-size: 12px; margin: 8px 0 0; }

.readout div { display: flex; justify-content: space-between; padding: 4px 0; border-bottom: 1px dashed var(--line); }
.readout b { font: 600 15px "Roboto Condensed", Inter, sans-serif; font-variant-numeric: tabular-nums; }

/* ---------- digital ---------- */

.digital, .help { overflow-y: auto; padding: 16px; }
.intro { max-width: 900px; color: var(--muted); margin: 0 0 16px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 14px; align-items: start; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.card h3 { margin: 0; font-size: 15px; }
.card .desc { margin: 2px 0 12px; color: var(--muted); font-size: 12.5px; }
.card .out { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 8px; }
.card .row { display: flex; justify-content: space-between; gap: 10px; padding: 3px 0; }
.card .row span { color: var(--muted); }
.card .row b { font: 600 15px "Roboto Condensed", Inter, sans-serif; font-variant-numeric: tabular-nums; text-align: right; }
.card .analog-btn { margin-top: 8px; }
.card .row.hl b { color: var(--accent); }

/* ---------- help ---------- */

.prose { max-width: 820px; margin: 0 auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 24px 20px; }
.prose h2 { font-size: 18px; margin: 22px 0 8px; }
.prose h3 { font-size: 15px; margin: 16px 0 6px; }
.prose li { margin: 4px 0; }

/* ---------- responsive ---------- */

@media (max-width: 900px) {
  .analog { grid-template-columns: 1fr; grid-template-rows: auto auto; }
  .stage { height: 72vh; min-height: 360px; }
  .side { border-left: 0; border-top: 1px solid var(--line); overflow: visible; }
  main { display: block; }
  body { display: block; }
}

@media (max-width: 560px) {
  .topbar { padding: 8px 12px; gap: 8px; }
  .brand p { display: none; }
  .tabs { order: 3; width: 100%; }
  .tabs button { flex: 1; padding: 8px 6px; font-size: 13px; }
  .digital, .help { padding: 12px; }
  .prose { padding: 4px 14px 14px; }
}
