:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --line: #2a2f3a;
  --text: #e7e9ee;
  --muted: #9097a6;
  --accent: #5b8cff;
  --accent-ink: #ffffff;
  --accent-soft: rgba(91, 140, 255, 0.16);
  --warn: #f2b84b;
  --danger: #ef5b5b;
  --ok: #3ecf8e;
  --key: #232833;
  --key-top: #2b313d;
  --key-text: #dfe3ea;
  --key-mod: #3a2f55;
  --radius: 10px;
  --shadow: 0 1px 0 rgba(255, 255, 255, 0.03), 0 8px 24px rgba(0, 0, 0, 0.25);
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, Ubuntu, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
:root[data-theme='light'] {
  --bg: #f5f6f9;
  --panel: #ffffff;
  --panel-2: #f0f2f6;
  --line: #dde1e8;
  --text: #1b1f27;
  --muted: #646c7c;
  --accent: #3a6df0;
  --accent-soft: rgba(58, 109, 240, 0.12);
  --key: #e9ecf2;
  --key-top: #ffffff;
  --key-text: #222832;
  --key-mod: #e5ddff;
  --shadow: 0 1px 2px rgba(20, 30, 50, 0.06), 0 8px 24px rgba(20, 30, 50, 0.06);
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme='dark']) {
    --bg: #f5f6f9;
    --panel: #ffffff;
    --panel-2: #f0f2f6;
    --line: #dde1e8;
    --text: #1b1f27;
    --muted: #646c7c;
    --accent: #3a6df0;
    --accent-soft: rgba(58, 109, 240, 0.12);
    --key: #e9ecf2;
    --key-top: #ffffff;
    --key-text: #222832;
    --key-mod: #e5ddff;
    --shadow: 0 1px 2px rgba(20, 30, 50, 0.06), 0 8px 24px rgba(20, 30, 50, 0.06);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.5 var(--font); min-height: 100vh; }
h1, h2, h3 { margin: 0 0 0.5em; line-height: 1.25; }
h1 { font-size: 1.9rem; }
h2 { font-size: 1.05rem; font-weight: 600; }
p { margin: 0 0 0.8em; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
[hidden] { display: none !important; }

button, .button, select, input[type='number'], input[type='text'] {
  font: inherit; color: var(--text); background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 8px; padding: 7px 12px; min-height: 36px;
}
button, .button { cursor: pointer; display: inline-flex; align-items: center; gap: 6px; transition: background 0.15s, border-color 0.15s; }
button:hover, .button:hover { border-color: var(--accent); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.ghost { background: transparent; }
button.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, var(--line)); }
button.icon { width: 36px; justify-content: center; padding: 0; }
select { padding-right: 28px; }
input[type='range'] { accent-color: var(--accent); width: 100%; }
input[type='color'] { width: 44px; height: 32px; border: 1px solid var(--line); border-radius: 6px; background: none; padding: 2px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.topbar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 16px; padding: 10px 20px;
  background: color-mix(in srgb, var(--bg) 85%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: 0.2px; }
.brand svg { width: 28px; height: 28px; fill: var(--accent); }
.brand svg .k { fill: var(--accent-ink); }
.device-summary { display: flex; align-items: center; gap: 12px; margin-left: auto; flex-wrap: wrap; }
.device-summary .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.profile-pick { display: flex; align-items: center; gap: 6px; color: var(--muted); }
#themeBtn { margin-left: 4px; }
.topbar > #themeBtn:only-of-type { margin-left: auto; }

main { max-width: 1180px; margin: 0 auto; padding: 24px 20px 64px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); margin-bottom: 16px; }
.card.subtle { background: var(--panel-2); box-shadow: none; }
.card-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.card-head h2 { margin: 0; }
.spacer { flex: 1; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.hint { color: var(--muted); font-size: 0.85rem; margin-top: 10px; }

.connect { padding-top: 24px; }
.connect .lead { color: var(--muted); font-size: 1.05rem; max-width: 640px; }
.connect-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; margin-top: 24px; }
.connect-grid .card { display: flex; flex-direction: column; align-items: flex-start; }
.connect-grid .card p { color: var(--muted); flex: 1; }

.device-strip { display: flex; gap: 18px; flex-wrap: wrap; color: var(--muted); font-size: 0.9rem; margin-bottom: 12px; }
.device-strip b { color: var(--text); font-weight: 600; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 18px; overflow-x: auto; }
.tabs button { background: none; border: none; border-bottom: 2px solid transparent; border-radius: 0; padding: 10px 14px; color: var(--muted); }
.tabs button[aria-selected='true'] { color: var(--text); border-bottom-color: var(--accent); }
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.seg button { border: none; border-radius: 0; background: var(--panel); }
.seg button[aria-pressed='true'] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.dirty { color: var(--warn); font-size: 0.9rem; }

/* keyboard */
.kb-wrap { overflow-x: auto; padding: 4px 0 8px; }
.kb { --u: clamp(34px, calc((100vw - 72px) / 16.6), 56px); position: relative; width: calc(var(--u) * 16.25); height: calc(var(--u) * 6.25);
  margin: 0 auto 8px; padding: 0; user-select: none; }
.kb.paint { touch-action: none; }
.key { position: absolute; left: calc(var(--x) * var(--u)); top: calc(var(--y) * var(--u)); width: calc(var(--w) * var(--u) - 4px); height: calc(var(--u) - 4px);
  border-radius: 7px; background: var(--key); border: 1px solid var(--line); padding: 0; min-height: 0; color: var(--key-text);
  display: flex; align-items: center; justify-content: center; text-align: center; font-size: clamp(9px, calc(var(--u) * 0.22), 13px); line-height: 1.1; overflow: hidden; }
.key::before { content: ''; position: absolute; inset: 2px 3px 5px; border-radius: 5px; background: var(--key-top); z-index: 0; }
.key span { position: relative; z-index: 1; padding: 0 2px; word-break: break-word; }
.key.changed::before { background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.key.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.key.special span { color: var(--accent); font-weight: 600; }
.knob { position: absolute; left: calc(15.25 * var(--u)); top: 0; width: calc(var(--u) - 4px); height: calc(var(--u) - 4px); border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, var(--key-top), var(--key)); border: 1px solid var(--line); padding: 0; min-height: 0; justify-content: center; font-size: 11px; color: var(--muted); }
.knob.selected { box-shadow: 0 0 0 2px var(--accent); }
.kb.paint .key::before { background: var(--c, var(--key-top)); }
.kb.paint .key span { color: transparent; }
.kb.paint .key:hover { border-color: var(--accent); }

.extras { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 16px; justify-content: center; }
.chip { font-size: 0.82rem; padding: 4px 10px; min-height: 0; border-radius: 99px; }
.chip.selected { border-color: var(--accent); background: var(--accent-soft); }
.chip.changed { border-color: var(--accent); }

.editor .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; align-items: end; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { color: var(--muted); font-size: 0.85rem; }
.field select, .field input { width: 100%; }
.capture.listening { border-color: var(--accent); background: var(--accent-soft); }
.editor-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.editor-head .now { color: var(--muted); }
.editor-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }

.light-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; }
.light-grid .card { margin-bottom: 0; }
.light-grid + .card, .light-grid + .card + .card { margin-top: 16px; }
.form { display: grid; gap: 14px; }
.form .inline { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.switch input { width: 18px; height: 18px; accent-color: var(--accent); }
.paint-tools { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.paint-tools label { display: inline-flex; align-items: center; gap: 8px; }
.meter { display: flex; gap: 2px; align-items: flex-end; height: 40px; }
.meter i { flex: 1; background: var(--accent); border-radius: 2px 2px 0 0; min-height: 2px; }
.swatch { width: 40px; height: 40px; border-radius: 8px; border: 1px solid var(--line); }

.macro-layout { display: grid; grid-template-columns: 260px 1fr; gap: 16px; }
.macro-slots ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; max-height: 520px; overflow: auto; }
.macro-slots li button { width: 100%; justify-content: space-between; background: transparent; }
.macro-slots li button.active { background: var(--accent-soft); border-color: var(--accent); }
.events { list-style: none; padding: 0; margin: 10px 0; display: grid; gap: 4px; max-height: 420px; overflow: auto; }
.events li { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); }
.events li .tag { font-size: 0.75rem; padding: 1px 8px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); min-width: 54px; text-align: center; }
.events li .tag.up { background: color-mix(in srgb, var(--muted) 18%, transparent); color: var(--muted); }
.events li .tag.delay { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.events li input[type='number'] { width: 90px; min-height: 30px; padding: 3px 8px; }
.events li .grow { flex: 1; }
.events li button { min-height: 28px; padding: 2px 8px; }
.usage { height: 6px; background: var(--panel-2); border-radius: 99px; overflow: hidden; margin: 6px 0 2px; }
.usage i { display: block; height: 100%; background: var(--accent); }
.usage.full i { background: var(--danger); }
.recording { color: var(--danger); font-weight: 600; }

.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; }
.settings-grid .card { margin: 0; }
.opt-list { display: grid; gap: 10px; }
.opt-list .switch { justify-content: space-between; width: 100%; }
.opt-list .desc { display: block; color: var(--muted); font-size: 0.82rem; }

.log { font: 12px/1.45 var(--mono); max-height: 520px; overflow: auto; background: var(--panel-2); border-radius: 8px; padding: 12px; margin: 0; white-space: pre-wrap; }

.busy { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.35); display: flex; align-items: center; justify-content: center; gap: 12px; z-index: 50; color: #fff; font-weight: 600; }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid rgba(255, 255, 255, 0.35); border-top-color: #fff; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none; background: var(--panel); border: 1px solid var(--line); box-shadow: var(--shadow);
  padding: 10px 16px; border-radius: 10px; transition: 0.2s; z-index: 60; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translateX(-50%); }
.toast.error { border-color: var(--danger); }

@media (max-width: 720px) {
  main { padding: 16px 16px 48px; }
  .topbar { padding: 10px 16px; flex-wrap: wrap; }
  .macro-layout { grid-template-columns: 1fr; }
  .light-grid, .settings-grid { grid-template-columns: 1fr; }
  h1 { font-size: 1.5rem; }
  #deviceName, .device-summary .dot { display: none; }
  .device-summary { margin-left: 0; }
}
