:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #273449;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #c084fc;
  --accent-ink: #1a0b2e;
  --line: rgba(148, 163, 184, 0.18);
  --radius: 12px;
  --shadow: 0 8px 24px rgba(2, 6, 23, 0.45);
  --ease: 180ms ease;
  color-scheme: dark;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.4;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

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

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

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
}

.brand h1 {
  margin: 0;
  font-size: 1.25rem;
  letter-spacing: 0.01em;
}

.brand h1::first-letter {
  color: var(--accent);
}

.tagline {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.top-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

/* Buttons */
.btn {
  min-height: 40px;
  min-width: 40px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  transition: background var(--ease), border-color var(--ease), transform var(--ease);
}

.btn:hover {
  background: var(--surface-2);
  border-color: rgba(192, 132, 252, 0.5);
}

.btn:active {
  transform: translateY(1px);
}

.btn:focus-visible,
.seg:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.btn-primary:hover {
  background: #d4a5fd;
}

.btn.is-active {
  border-color: var(--accent);
  color: var(--accent);
}

.btn-lang {
  font-weight: 700;
  letter-spacing: 0.05em;
}

/* Layout */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 16px;
  padding: 16px;
  width: 100%;
  max-width: 1500px;
  margin: 0 auto;
}

.stage-wrap {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}

.stage {
  position: relative;
  height: clamp(380px, calc(100vh - 272px), 980px);
  border-radius: var(--radius);
  background: radial-gradient(circle at 50% 45%, #16213a 0%, #0b1224 70%);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  overflow: hidden;
  touch-action: none;
}

.stage canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
}

.stage.is-drawing {
  border-color: var(--accent);
  cursor: crosshair;
}

.stage-hint {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  padding: 6px 12px;
  max-width: calc(100% - 24px);
  border-radius: 999px;
  background: rgba(30, 41, 59, 0.9);
  color: var(--text);
  font-size: 0.9rem;
  text-align: center;
  pointer-events: none;
}

.stage-stats {
  position: absolute;
  left: 12px;
  bottom: 10px;
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

.dock {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 12px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.dock-buttons {
  display: flex;
  gap: 8px;
}

.dock-buttons .btn {
  min-width: 84px;
}

.btn-panel {
  display: none;
}

.field-terms {
  flex: 1;
  min-width: 0;
}

/* Panel */
.panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
  max-height: calc(100vh - 150px);
  overflow-y: auto;
  scrollbar-width: thin;
}

.card {
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.card h2 {
  margin: 4px 0 8px;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.card h2 + .check {
  margin-top: 0;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 4px 0 10px;
}

.field:last-child {
  margin-bottom: 4px;
}

.field-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.9rem;
}

.field-head output {
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

input[type="range"] {
  width: 100%;
  height: 40px;
  margin: 0;
  accent-color: var(--accent);
  background: transparent;
}

select,
textarea {
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
}

textarea {
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 0.85rem;
}

.check {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  cursor: pointer;
}

.check input {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  border-radius: var(--radius);
  background: var(--bg);
}

.seg {
  min-height: 40px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: background var(--ease), color var(--ease);
}

.seg[aria-pressed="true"] {
  background: var(--surface-2);
  color: var(--accent);
  font-weight: 600;
}

.presets {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(88px, 1fr));
  gap: 6px;
}

.presets .btn {
  padding: 6px 8px;
  font-size: 0.9rem;
}

.chart {
  display: block;
  width: 100%;
  height: 90px;
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
}

.row.wrap {
  flex-wrap: wrap;
}

.note {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.row .note {
  margin: 0;
}

.footer {
  padding: 12px 16px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
}

.footer a {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  padding: 0 8px;
}

/* Phones and narrow tablets */
@media (max-width: 860px) {
  .layout {
    display: flex;
    flex-direction: column;
    padding: 12px 16px;
    gap: 12px;
  }

  .stage {
    height: auto;
    aspect-ratio: 1 / 1;
    max-height: 70vh;
  }

  .dock {
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
  }

  .dock-buttons .btn {
    flex: 1;
    min-width: 0;
  }

  .btn-panel {
    display: inline-block;
  }

  .panel {
    max-height: none;
    overflow: visible;
  }

  .panel.is-collapsed {
    display: none;
  }

  .tagline {
    display: none;
  }

  .keys {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition: none !important;
    animation: none !important;
  }
}
