:root {
  --bg: #f6f3ee;
  --card: #ffffff;
  --ink: #1d1a24;
  --muted: #6b6577;
  --line: #e4ded4;
  --accent: #7a4fd6;
  --accent-ink: #ffffff;
  --gold: #b8862b;
  --ok: #1f8a4c;
  --warn: #b54708;
  --trail-bg: #faf8f4;
  --trail: #7a4fd6;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 4px 16px rgb(0 0 0 / 0.05);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #110f17;
    --card: #1b1824;
    --ink: #ece8f4;
    --muted: #9c95ab;
    --line: #2d2839;
    --accent: #a988ff;
    --accent-ink: #120d1f;
    --gold: #e2b75d;
    --ok: #4ccf86;
    --warn: #f59e5b;
    --trail-bg: #14121b;
    --trail: #c9b2ff;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

h1, h2 { margin: 0; font-weight: 650; }
h1 { font-size: 1.15rem; }
h2 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin-bottom: 0.6rem; }
h2:not(:first-child) { margin-top: 1.2rem; }

a { color: var(--accent); }

.bar {
  display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between;
  padding: 14px 20px; border-bottom: 1px solid var(--line); background: var(--card);
}
.brand { display: flex; gap: 10px; align-items: center; }
.logo { font-size: 1.8rem; }
.sub { margin: 0; color: var(--muted); font-size: 0.85rem; }
.status { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

.pill {
  font-size: 0.8rem; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted);
}
.pill[data-state="connected"] { color: var(--ok); border-color: currentColor; }
.pill[data-state="demo"] { color: var(--gold); border-color: currentColor; }
.pill[data-state="error"] { color: var(--warn); border-color: currentColor; }
.pill[data-state="waiting"] { color: var(--accent); border-color: currentColor; }
.pill[data-battery="charging"] { color: var(--ok); border-color: currentColor; }
.pill[data-battery="low"], .pill[data-battery="critical"] { color: var(--warn); border-color: currentColor; }

button, select {
  font: inherit; border-radius: 8px; padding: 7px 14px; cursor: pointer;
  border: 1px solid var(--accent); background: var(--accent); color: var(--accent-ink);
}
button.ghost, select { background: transparent; color: var(--ink); border-color: var(--line); }
button.small { padding: 3px 10px; font-size: 0.85rem; }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.notice {
  margin: 16px 20px 0; padding: 12px 16px; border-radius: 10px;
  border: 1px solid var(--warn); color: var(--ink); background: color-mix(in srgb, var(--warn) 10%, transparent);
}

.grid {
  display: grid; gap: 16px; padding: 16px 20px;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  max-width: 1200px; margin: 0 auto;
}
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 16px 18px; box-shadow: var(--shadow); min-width: 0; }
.motion, .log-card { grid-column: 1 / -1; }

.spell {
  font-family: Georgia, "Times New Roman", serif; font-size: clamp(1.8rem, 5vw, 2.6rem); line-height: 1.15;
  text-transform: capitalize; color: var(--gold); min-height: 3rem; margin: 4px 0 8px;
}
.spell.empty { text-transform: none; color: var(--muted); }
.spell.cast { animation: glow 1.2s ease-out; }
@keyframes glow {
  0% { text-shadow: 0 0 0 transparent; transform: scale(0.96); }
  25% { text-shadow: 0 0 24px var(--gold); transform: scale(1.03); }
  100% { text-shadow: 0 0 0 transparent; transform: scale(1); }
}
.history { margin: 10px 0 0; padding-left: 1.2rem; color: var(--muted); max-height: 9rem; overflow: auto; text-transform: capitalize; }
.history time { font-variant-numeric: tabular-nums; margin-right: 6px; opacity: 0.8; }

.hint { color: var(--muted); font-size: 0.85rem; margin: 6px 0; }
.check { display: inline-flex; gap: 6px; align-items: center; font-size: 0.9rem; margin-right: 12px; }

.buttons { display: flex; gap: 10px; }
.btn-dot {
  width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center;
  border: 2px solid var(--line); color: var(--muted); font-weight: 600; transition: all 80ms;
}
.btn-dot.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); transform: scale(1.08); }
.buttons.all .btn-dot.on { background: var(--gold); border-color: var(--gold); }

.info { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; margin: 0; }
.info dt { color: var(--muted); }
.info dd { margin: 0; overflow-wrap: anywhere; }

.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
.row h2 { margin: 0 auto 0 0; }
.row.wrap { margin-top: 10px; }

.toolbar { gap: 6px; }
.motion-views { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 12px; }
.view { margin: 0; min-width: 0; }
.view figcaption { font-size: 0.8rem; color: var(--muted); margin-top: 4px; }
.stage { width: 100%; aspect-ratio: 4 / 3; background: var(--trail-bg); border: 1px solid var(--line); border-radius: 10px; display: block; overflow: hidden; }
#wand3d canvas { width: 100% !important; height: 100% !important; display: block; touch-action: none; }
#wand3d.unavailable { display: grid; place-items: center; color: var(--muted); font-size: 0.9rem; padding: 16px; text-align: center; }
.align { border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; margin-bottom: 12px; background: var(--trail-bg); }
.align p { margin: 4px 0; }
.align ol { margin: 6px 0; padding-left: 1.3rem; }
.align li { margin: 6px 0; }
.align label { display: inline-flex; gap: 6px; align-items: center; font-size: 0.9rem; }
.meters { display: flex; flex-wrap: wrap; gap: 8px 24px; margin-top: 10px; }
.meters > div { display: flex; flex-direction: column; }
.lbl { font-size: 0.75rem; color: var(--muted); }
.mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 0.9rem; }

.leds { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.leds label { display: flex; flex-direction: column; gap: 4px; font-size: 0.85rem; color: var(--muted); }
.leds input[type="color"] { width: 100%; height: 38px; border: 1px solid var(--line); border-radius: 8px; padding: 2px; background: transparent; }

.log {
  max-height: 260px; overflow: auto; margin: 8px 0 0; padding: 10px; border-radius: 8px;
  background: var(--trail-bg); border: 1px solid var(--line);
  font: 12px/1.45 ui-monospace, "Cascadia Mono", Consolas, monospace; white-space: pre-wrap; word-break: break-all;
}
summary { cursor: pointer; font-weight: 600; }

footer { text-align: center; color: var(--muted); font-size: 0.8rem; padding: 8px 20px 28px; }

@media (max-width: 520px) {
  .bar, .grid { padding-left: 16px; padding-right: 16px; }
  .status { width: 100%; }
  .status button { flex: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .spell.cast { animation: none; }
  .btn-dot { transition: none; }
}

.sens { margin-top: 10px; margin-bottom: 0; }
.sens label { font-size: 0.9rem; }

/* Spell guide */
.guide-card { grid-column: 1 / -1; }
.guide-card .row h2 { margin-right: auto; }
#guide-search { font: inherit; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--line); background: transparent; color: var(--ink); min-width: 0; width: 220px; max-width: 100%; }
.guide { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 8px; margin: 10px 0; }
.guide[data-empty]:not([data-empty=""])::after { content: attr(data-empty); color: var(--muted); grid-column: 1 / -1; padding: 12px 0; }
.tile {
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 6px 6px;
  background: var(--trail-bg); color: var(--ink); border: 1px solid var(--line); border-radius: 10px;
  font-size: 0.78rem; line-height: 1.2; text-align: center; cursor: pointer;
}
.tile:hover { border-color: var(--accent); }
.tile[aria-pressed="true"] { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.tile.cast { animation: tile-cast 1.6s ease-out; }
@keyframes tile-cast { 0%, 40% { border-color: var(--gold); box-shadow: 0 0 0 3px color-mix(in srgb, var(--gold) 40%, transparent); } }
.tile .shape { width: 100%; height: 72px; }
@media (max-width: 520px) {
  .guide { grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: 6px; }
  .tile .shape { height: 56px; }
}

.shape { overflow: visible; }
.shape-stroke { fill: none; stroke: var(--trail); stroke-linecap: round; stroke-linejoin: round; }
.shape-arrow { fill: var(--trail); }
.shape-start { fill: var(--gold); }
.shape.drawing .shape-stroke { stroke-dasharray: 1; animation: draw-on 0.9s ease-in-out var(--delay, 0s) both; }
@keyframes draw-on { from { stroke-dashoffset: 1; } to { stroke-dashoffset: 0; } }

.spell-row { display: flex; align-items: center; gap: 12px; justify-content: space-between; }
.spell-shape { flex: none; width: 64px; height: 64px; }
.spell-shape .shape { width: 100%; height: 100%; }

@media (prefers-reduced-motion: reduce) {
  .shape.drawing .shape-stroke { animation: none; }
  .tile.cast { animation: none; border-color: var(--gold); }
}
