:root {
  --bg: #f7f7f4;
  --grid: rgba(20, 22, 27, 0.06);
  --ink: #1b1d22;
  --muted: #6b6f78;
  --panel: rgba(255, 255, 255, 0.86);
  --panel-border: rgba(20, 22, 27, 0.1);
  --hover: rgba(20, 22, 27, 0.07);
  --active: #2f6bff;
  --active-ink: #ffffff;
  --line-normal: #1b1d22;
  --line-accel: #e5383b;
  --line-scenery: #2bb673;
  --rider: #1b1d22;
  --rider-fill: #ffffff;
  --scarf: #e5383b;
  --flag: #2f6bff;
  --shadow: 0 6px 24px rgba(20, 22, 27, 0.12);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --grid: rgba(255, 255, 255, 0.045);
    --ink: #e8e8ea;
    --muted: #9298a4;
    --panel: rgba(26, 29, 36, 0.86);
    --panel-border: rgba(255, 255, 255, 0.09);
    --hover: rgba(255, 255, 255, 0.08);
    --active: #7aa2ff;
    --active-ink: #0f1115;
    --line-normal: #c9d4ff;
    --line-accel: #ff6b6e;
    --line-scenery: #3ddc84;
    --rider: #e8e8ea;
    --rider-fill: #1a1d24;
    --scarf: #ff5a5f;
    --flag: #7aa2ff;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --grid: rgba(255, 255, 255, 0.045);
  --ink: #e8e8ea;
  --muted: #9298a4;
  --panel: rgba(26, 29, 36, 0.86);
  --panel-border: rgba(255, 255, 255, 0.09);
  --hover: rgba(255, 255, 255, 0.08);
  --active: #7aa2ff;
  --active-ink: #0f1115;
  --line-normal: #c9d4ff;
  --line-accel: #ff6b6e;
  --line-scenery: #3ddc84;
  --rider: #e8e8ea;
  --rider-fill: #1a1d24;
  --scarf: #ff5a5f;
  --flag: #7aa2ff;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

canvas {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: block;
  touch-action: none;
  cursor: crosshair;
}
body[data-tool="pan"] canvas { cursor: grab; }
body.panning canvas { cursor: grabbing; }
body[data-tool="eraser"] canvas { cursor: cell; }

.bar {
  position: fixed;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  z-index: 2;
}

.tools { top: max(12px, env(safe-area-inset-top)); left: 12px; }
.top-right { top: max(12px, env(safe-area-inset-top)); right: 12px; }
.playback {
  bottom: max(12px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  width: min(560px, calc(100vw - 24px));
  padding-right: 12px;
}

.sep { width: 1px; height: 22px; background: var(--panel-border); margin: 0 4px; }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  border-radius: 8px;
  width: 36px;
  height: 36px;
  display: inline-grid;
  place-items: center;
  cursor: pointer;
  flex: none;
}
button:hover { background: var(--hover); }
button:focus-visible { outline: 2px solid var(--active); outline-offset: 1px; }
button[aria-pressed="true"] { background: var(--active); color: var(--active-ink); }
button:disabled { opacity: 0.35; cursor: default; background: none; }
button.primary { background: var(--active); color: var(--active-ink); }

svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
svg .fill { fill: currentColor; stroke: none; }
#flag svg .fill { stroke: currentColor; }

.swatch i { width: 18px; height: 4px; border-radius: 2px; display: block; }
.swatch[aria-pressed="true"] { background: var(--hover); color: inherit; box-shadow: inset 0 0 0 2px var(--active); }
.sw-normal { background: var(--line-normal); }
.sw-accel { background: var(--line-accel); }
.sw-scenery { background: var(--line-scenery); }

#theme svg { display: none; }
:root:not([data-theme]) #theme .ic-auto,
:root[data-theme="light"] #theme .ic-sun,
:root[data-theme="dark"] #theme .ic-moon { display: block; }

#play .ic-pause, body.playing #play .ic-play { display: none; }
body.playing #play .ic-pause { display: block; }
#flag.set { color: var(--flag); }

#scrub { flex: 1; min-width: 40px; accent-color: var(--active); margin: 0 8px; }
output { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; white-space: nowrap; }
#speed { min-width: 3.2em; text-align: right; }
#speed::after { content: " u/f"; font-size: 11px; }

.menu {
  position: fixed;
  top: calc(max(12px, env(safe-area-inset-top)) + 52px);
  right: 12px;
  display: flex;
  flex-direction: column;
  padding: 6px;
  min-width: 220px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  z-index: 3;
}
.menu[hidden] { display: none; }
.menu button { width: auto; height: 36px; padding: 0 12px; justify-content: start; display: flex; align-items: center; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(max(12px, env(safe-area-inset-bottom)) + 64px);
  transform: translate(-50%, 8px);
  padding: 8px 14px;
  border-radius: 10px;
  background: var(--ink);
  color: var(--bg);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 4;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

dialog {
  max-width: min(460px, calc(100vw - 32px));
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  background: var(--panel);
  color: var(--ink);
  box-shadow: var(--shadow);
  padding: 20px 22px;
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.3); }
dialog h2 { margin: 0 0 6px; font-size: 20px; }
dialog p { margin: 0 0 12px; }
dialog .tip { padding: 10px 12px; border-radius: 10px; background: var(--hover); }
dialog .muted { color: var(--muted); font-size: 13px; }
dialog table { border-collapse: collapse; width: 100%; margin-bottom: 12px; }
dialog td { padding: 4px 0; vertical-align: top; }
dialog td:first-child { padding-right: 16px; white-space: nowrap; width: 1%; }
dialog form { display: flex; justify-content: end; }
dialog form button { width: auto; padding: 0 16px; }
kbd {
  font: 12px ui-monospace, SFMono-Regular, Menlo, monospace;
  padding: 1px 6px;
  border-radius: 5px;
  border: 1px solid var(--panel-border);
  background: var(--hover);
}

@media (max-width: 640px) {
  button { width: 34px; height: 34px; }
  .tools { flex-wrap: wrap; max-width: calc(100vw - 150px); }
  .tools .sep { display: none; }
  #speed { display: none; }
}
