/* KT68 web configurator — light/dark aware, no external assets. */
:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --bg-elev: #ffffff;
  --bg-sunken: #e9ebef;
  --ink: #14161a;
  --ink-dim: #5b6270;
  --line: #d7dae1;
  --accent: #2f6df6;
  --accent-ink: #ffffff;
  --ok: #12855a;
  --warn: #b25e00;
  --err: #c0322b;
  --key-bg: #ffffff;
  --key-line: #c8ccd6;
  --key-ink: #1b1e24;
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --bg-elev: #1c1f25;
    --bg-sunken: #0f1115;
    --ink: #e8eaee;
    --ink-dim: #99a1b0;
    --line: #2c313a;
    --accent: #5b8cff;
    --accent-ink: #0b0d10;
    --ok: #3ecf8e;
    --warn: #f0a44a;
    --err: #ff6b61;
    --key-bg: #23272f;
    --key-line: #363c47;
    --key-ink: #e8eaee;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
}
.app { display: flex; flex-direction: column; min-height: 100%; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 12px 20px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: baseline; gap: 8px; }
.brand-mark {
  font-weight: 700; letter-spacing: .04em;
  font-family: var(--mono); font-size: 16px;
  padding: 3px 8px; border-radius: 6px;
  background: var(--ink); color: var(--bg-elev);
}
.brand-sub { color: var(--ink-dim); font-size: 13px; }
.topbar-actions { margin-left: auto; display: flex; gap: 8px; }

.status { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-dim); }
.status .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--ink-dim); }
.status--on .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.status--on { color: var(--ink); }
.status--busy .dot { background: var(--warn); }

/* ---------- buttons & inputs ---------- */
.btn {
  font: inherit; cursor: pointer; white-space: nowrap;
  padding: 7px 13px; border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg-elev); color: var(--ink);
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn--primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn--ghost { background: transparent; }
.btn--danger { border-color: var(--err); color: var(--err); }
.input {
  font: inherit; padding: 7px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--bg-sunken); color: var(--ink);
  min-width: 0;
}
.input--mono { font-family: var(--mono); flex: 1; }
.check { display: flex; align-items: center; gap: 6px; color: var(--ink-dim); font-size: 13px; white-space: nowrap; }
select.input { background: var(--bg-elev); }

/* ---------- banners / layout ---------- */
.banner { margin: 12px 20px 0; padding: 10px 14px; border-radius: var(--radius); font-size: 13px; }
.banner--error { background: color-mix(in srgb, var(--err) 12%, var(--bg-elev)); border: 1px solid color-mix(in srgb, var(--err) 40%, var(--line)); }
.banner--warn  { background: color-mix(in srgb, var(--warn) 14%, var(--bg-elev)); border: 1px solid color-mix(in srgb, var(--warn) 40%, var(--line)); }

.body { display: flex; flex: 1; min-height: 0; align-items: flex-start; }
.tabs {
  display: flex; flex-direction: column; gap: 2px;
  padding: 16px 10px; min-width: 170px;
  border-right: 1px solid var(--line);
  position: sticky; top: 61px;
}
.tab {
  font: inherit; text-align: left; cursor: pointer; white-space: nowrap;
  padding: 8px 12px; border-radius: 8px;
  border: 1px solid transparent; background: transparent; color: var(--ink-dim);
}
.tab:hover { background: var(--bg-elev); color: var(--ink); }
.tab.is-active { background: var(--bg-elev); border-color: var(--line); color: var(--ink); font-weight: 600; }

.main { flex: 1; min-width: 0; padding: 20px 24px 60px; }
.view { display: none; }
.view.is-active { display: block; }
.view-head h2 { margin: 0 0 4px; font-size: 20px; }
.view-head p { margin: 0 0 18px; color: var(--ink-dim); max-width: 70ch; }

.panel { background: var(--bg-elev); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.panel-head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.panel-head h3 { margin: 0; font-size: 14px; }
.panel-body { padding: 14px; }
.split { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: 16px; margin-top: 18px; }
@media (max-width: 1000px) { .split { grid-template-columns: 1fr; } }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.row:last-child { margin-bottom: 0; }
.row--tight { margin: 10px 0 6px; }
.spacer { flex: 1; }
.muted, .hint { color: var(--ink-dim); font-size: 12.5px; }
.hint { margin: 8px 0 0; }
.field { display: flex; flex-direction: column; gap: 5px; min-width: 190px; }
.field > label { font-size: 12.5px; color: var(--ink-dim); }
.grid-fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; }

/* ---------- keyboard ---------- */
.keyboard {
  position: relative;
  width: 100%;
  max-width: 880px;
  aspect-ratio: 800 / 300;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 14px;
  container-type: inline-size;
  user-select: none;
}
.kb-screen {
  position: absolute; border-radius: 4px;
  border: 1px dashed var(--line);
  color: var(--ink-dim);
  font-size: 10px; display: flex; align-items: center; justify-content: center;
}
.key {
  position: absolute;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1px;
  border: 1px solid var(--key-line);
  border-radius: 5px;
  background: var(--key-bg);
  color: var(--key-ink);
  font: 600 11px/1.1 var(--sans);
  cursor: pointer;
  overflow: hidden;
  padding: 2px;
  text-align: center;
}
.key:hover { border-color: var(--accent); }
.key.is-selected { border-color: var(--accent); box-shadow: inset 0 0 0 2px var(--accent); }
.key.is-remapped { background: color-mix(in srgb, var(--accent) 16%, var(--key-bg)); }
.key .k-main { font-size: min(11px, 3cqw); line-height: 1.05; }
.key .k-sub { font-size: min(9px, 2.4cqw); color: var(--ink-dim); font-weight: 500; }
.key.is-painted .k-main { text-shadow: 0 1px 2px rgba(0,0,0,.55); }
.keyboard--paint .key { cursor: crosshair; }

.swatches { display: flex; gap: 5px; }
.swatch { width: 22px; height: 22px; border-radius: 5px; border: 1px solid var(--line); cursor: pointer; }
.paint-tools { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
input[type=color] { width: 40px; height: 30px; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: none; }

/* ---------- key picker ---------- */
.picker-groups { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.chip {
  font: inherit; cursor: pointer; padding: 5px 10px; border-radius: 999px;
  border: 1px solid var(--line); background: transparent; color: var(--ink-dim);
  white-space: nowrap;   /* CJK labels must not break mid-word inside a pill */
}
.chip.is-active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.layer-switch { display: flex; gap: 6px; margin-bottom: 12px; }
.usage-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); gap: 6px; max-height: 260px; overflow: auto; }
.usage {
  font: 12px var(--sans); cursor: pointer; padding: 7px 4px; border-radius: 6px;
  border: 1px solid var(--line); background: var(--bg-sunken); color: var(--ink);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.usage:hover { border-color: var(--accent); }
.usage.is-current { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 18%, var(--bg-sunken)); }

/* ---------- console ---------- */
.wire-log {
  font: 12px/1.55 var(--mono);
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  max-height: 55vh; overflow: auto;
  white-space: pre; margin: 0;
}
.wire-log .l-out { color: var(--accent); }
.wire-log .l-in { color: var(--ok); }
.wire-log .l-note { color: var(--ink-dim); }

table.spec { border-collapse: collapse; width: 100%; font-size: 13px; }
table.spec th, table.spec td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.spec td.mono, table.spec th.mono { font-family: var(--mono); white-space: nowrap; }
.scroll-x { overflow-x: auto; }

/* ---------- toasts ---------- */
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast {
  padding: 10px 14px; border-radius: 10px; font-size: 13px; max-width: 380px;
  background: var(--bg-elev); border: 1px solid var(--line);
  box-shadow: 0 8px 24px rgba(0,0,0,.18);
}
.toast--ok { border-color: color-mix(in srgb, var(--ok) 55%, var(--line)); }
.toast--err { border-color: color-mix(in srgb, var(--err) 55%, var(--line)); }
.toast--warn { border-color: color-mix(in srgb, var(--warn) 55%, var(--line)); }

/* ---------- 65x5 animation editor ---------- */
.ed { display: flex; flex-direction: column; gap: 12px; }

.ed-bar {
  display: flex; align-items: center; gap: 8px;
  flex-wrap: wrap; min-height: 34px;
}
.ed-bar--wrap { flex-wrap: wrap; }
.ed-sep { width: 1px; height: 22px; background: var(--line); flex: none; }
.ed-tools { display: flex; gap: 4px; }
.ed-palette { display: flex; gap: 3px; }
.ed-palette .swatch { width: 18px; height: 18px; border-radius: 4px; }
.btn--icon { padding: 5px 9px; font-family: var(--mono); line-height: 1; }
.input--slim { padding: 4px 7px; font-size: 12.5px; }
.field--inline { flex-direction: row; align-items: center; gap: 7px; min-width: 0; }
.field--inline > label { white-space: nowrap; }
.field--inline input[type=range] { width: 110px; }
.ed-scrub { flex: 1; min-width: 120px; }
.ed-count { font-family: var(--mono); font-size: 12px; white-space: nowrap; }

.ed-canvas-wrap {
  background: #08090b;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px;
  overflow-x: auto;
}
.ed-canvas {
  display: block;
  image-rendering: pixelated;
  cursor: crosshair;
  background: #000;
  touch-action: none;
}

.ed-strip-wrap {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg-sunken);
  overflow-x: auto;
  overflow-y: hidden;
}
.ed-strip { display: flex; gap: 6px; padding: 8px; align-items: flex-start; }
.ed-thumb {
  position: relative; flex: none;
  padding: 4px; border-radius: 6px;
  border: 1px solid var(--line);
  background: #000;
  cursor: pointer;
}
.ed-thumb canvas {
  display: block; width: 130px; height: 10px;
  image-rendering: pixelated;
}
.ed-thumb.is-current { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.ed-thumb.is-drop { border-color: var(--ok); border-style: dashed; }
.ed-thumb-n {
  display: block; text-align: center;
  font: 10px/1.4 var(--mono); color: var(--ink-dim);
  margin-top: 2px;
}
.split--gen { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); margin-top: 4px; }
@media (max-width: 900px) { .split--gen { grid-template-columns: 1fr; } }

kbd {
  font: 11px/1 var(--mono);
  padding: 2px 5px; border-radius: 4px;
  border: 1px solid var(--line); background: var(--bg-sunken);
  color: var(--ink); white-space: nowrap;
}

/* ---------- animated preview ---------- */
.ed-preview {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg-elev);
  padding: 12px;
  display: flex; flex-direction: column; gap: 10px;
}
.ed-preview-stage {
  display: flex; justify-content: center;
  padding: 14px 10px;
  border-radius: 8px;
  background:
    radial-gradient(120% 160% at 50% 0%, #1a1d24 0%, #0b0c0f 70%);
  overflow-x: auto;
}
.ed-prev-canvas {
  display: block;
  border-radius: 8px;
  box-shadow: 0 6px 26px rgba(0, 0, 0, .55), 0 0 0 1px rgba(255, 255, 255, .06);
  max-width: none;
}
.ed-thumb.is-preview { outline: 2px solid var(--ok); outline-offset: 1px; }

.panel--bare { background: transparent; border: 0; }
.panel--bare > .panel-body { padding: 0; }
