:root {
  color-scheme: dark;
  --bg: #0a0f1e;
  --surface: rgba(255, 255, 255, 0.05);
  --surface-strong: rgba(255, 255, 255, 0.08);
  --border: rgba(255, 255, 255, 0.09);
  --text: #f1f5f9;
  --text-dim: #94a3b8;
  --accent: #6366f1;
  --accent-2: #a855f7;
  --success: #22c55e;
  --error: #f87171;
  --warning: #f59e0b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at 15% -10%, #24305c 0%, transparent 50%),
    radial-gradient(circle at 100% 0%, #3b1f66 0%, transparent 45%),
    var(--bg);
  background-attachment: fixed;
  min-height: 100vh;
}

.app { max-width: 560px; margin: 0 auto; padding: clamp(16px, 4vw, 28px); }

.topbar { display: flex; flex-direction: column; gap: 10px; margin-bottom: 18px; }
.topbar-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.brand { min-width: 0; flex: 1 1 auto; overflow: hidden; }
.eyebrow { color: #93c5fd; text-transform: uppercase; letter-spacing: .12em; font-size: .75rem; margin: 0 0 4px; }
h1 { margin: 0; font-size: 1.5rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
h2 { margin: 0 0 12px; font-size: 1rem; color: var(--text); }

.lang-btn {
  flex: none;
  margin-top: 2px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font: inherit;
  font-size: .85rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform .1s;
}
.lang-btn:active { transform: scale(.95); }
.lang-btn span:first-child { font-size: 1rem; line-height: 1; }

/* Sensor bar: AHT21 temp/humidity + phototransistor brightness, reported
   back from the firmware alongside the live board state below it. */
.sensorbar-wrap { position: relative; margin-bottom: 10px; }
.sensorbar {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--surface);
  transition: opacity .3s, filter .3s;
}
.sensorbar.is-offline { opacity: .4; filter: grayscale(.7); }
.sensorbar-block {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 8px 6px;
  font-size: .78rem;
  font-weight: 700;
}
.sensorbar-block + .sensorbar-block { border-left: 1px solid var(--border); }
.sensorbar-label { color: var(--text-dim); }
.sensorbar-value { color: var(--text); font-variant-numeric: tabular-nums; }

/* Live board-state bar: LEDs / servo / sound, mirrors what the board is
   actually doing right now, reported back from the firmware. */
.livebar-wrap { position: relative; }
.livebar {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--surface);
  transition: opacity .3s, filter .3s;
}
.livebar.is-offline { opacity: .4; filter: grayscale(.7); }
.offline-badge {
  position: absolute;
  top: -8px;
  right: -6px;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--error);
  color: #fff;
  font-size: .65rem;
  font-weight: 800;
  letter-spacing: .04em;
  text-transform: uppercase;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .4);
}
.offline-badge[hidden] { display: none; }
.livebar-block {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 46px;
  padding: 8px 6px;
}
.livebar-block + .livebar-block { border-left: 1px solid var(--border); }
.livebar-sound { flex-direction: column; gap: 4px; }

.state-led {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: rgba(255, 255, 255, .12);
  box-shadow: none;
  transition: background .3s, box-shadow .3s;
}

.state-servo-mini { width: 40px; height: 24px; flex: none; }
#state-servo-needle { transition: transform .3s ease; }
/* Fixed width so the changing digit count (e.g. "5°" vs "180°") doesn't
   re-center the flex row and shift the needle icon next to it. */
.livebar-servo .livebar-label { min-width: 30px; text-align: left; font-variant-numeric: tabular-nums; }

.livebar-label {
  font-size: .7rem;
  font-weight: 700;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.eq { display: flex; align-items: flex-end; justify-content: center; gap: 3px; height: 15px; }
.eq-bar {
  width: 3px;
  height: 35%;
  border-radius: 2px;
  background: rgba(255, 255, 255, .18);
  transition: background .3s;
}
.livebar-sound.is-active .eq-bar {
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  animation: eq-bounce .9s ease-in-out infinite;
}
.livebar-sound.is-active .eq-bar:nth-child(1) { animation-delay: 0s; }
.livebar-sound.is-active .eq-bar:nth-child(2) { animation-delay: .15s; }
.livebar-sound.is-active .eq-bar:nth-child(3) { animation-delay: .3s; }
@keyframes eq-bounce {
  0%, 100% { height: 30%; }
  50% { height: 100%; }
}

.status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  max-width: 100%;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: .85rem;
  color: var(--text-dim);
  transition: background .2s, color .2s, border-color .2s;
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-dim); flex: none; }
.status-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.status--sending .status-dot { background: #60a5fa; animation: pulse 1s ease-in-out infinite; }
.status--sending { color: #bfdbfe; }
.status--success .status-dot { background: var(--success); }
.status--success { color: #bbf7d0; border-color: rgba(34, 197, 94, .35); }
.status--error .status-dot { background: var(--error); }
.status--error { color: #fecaca; border-color: rgba(248, 113, 113, .35); }
.status--warning .status-dot { background: var(--warning); }
.status--warning { color: #fde3b0; border-color: rgba(245, 158, 11, .35); }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }

.tabs {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  padding: 4px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  margin-bottom: 20px;
}
.tab {
  padding: 10px 6px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-size: .85rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background .15s, color .15s;
}
.tab.active { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; }

.panel { display: grid; gap: 14px; animation: fade-in .18s ease; }
.panel[hidden] { display: none; }
@keyframes fade-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.subtabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 3px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.subtab {
  padding: 9px 6px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-size: .82rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background .15s, color .15s;
}
.subtab.active { background: var(--surface-strong); color: var(--text); box-shadow: inset 0 0 0 1px var(--border); }

.subpanel { display: grid; gap: 14px; animation: fade-in .18s ease; }
.subpanel[hidden] { display: none; }

.card {
  padding: 18px;
  border-radius: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  backdrop-filter: blur(6px);
}
.card + .card { margin-top: 0; }

.hint { color: var(--text-dim); font-size: .82rem; margin: 6px 0 0; font-weight: 400; }

/* Segmented control */
.segmented { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.segmented-option {
  padding: 12px 4px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-strong);
  color: var(--text-dim);
  font: inherit;
  font-weight: 600;
  font-size: .85rem;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background .15s, color .15s, border-color .15s;
}
.segmented-option.active { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; border-color: transparent; }

/* OLED note preview (Display tab) */
.oled-live-status {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 6px;
  margin: 0 0 12px;
  font-size: .82rem;
  color: var(--text-dim);
}
.oled-live-text {
  color: var(--text);
  font-weight: 600;
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  white-space: pre-wrap;
  word-break: break-word;
}
.oled-preview-wrap { margin-bottom: 12px; }
.oled-preview {
  display: block;
  width: 100%;
  aspect-ratio: 128 / 64;
  box-sizing: border-box;
  margin: 0;
  padding: 10px;
  border-radius: 10px;
  border: 2px solid var(--border);
  background: #000;
  color: #eafcff;
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  font-size: .8rem;
  line-height: 1.35;
  resize: none;
  outline: none;
  transition: border-color .15s;
}
.oled-preview:focus { border-color: var(--accent); }
.oled-preview::placeholder { color: rgba(234, 252, 255, .35); }
.oled-preview:disabled { opacity: .7; cursor: default; }

/* Alignment toolbar: icon buttons instead of the text-pill segmented
   control used elsewhere, since a 4-column grid doesn't divide evenly
   into 3 options and text labels ("Left"/"Center"/"Right") add width
   without adding clarity once the icon shows the same thing at a glance. */
.align-segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}
.align-option {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 44px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-strong);
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .1s;
}
.align-option:active { transform: scale(.94); }
.align-option.active { background: linear-gradient(135deg, var(--accent), var(--accent-2)); border-color: transparent; }
.align-icon { width: 30px; height: 20px; fill: var(--text-dim); }
.align-option.active .align-icon { fill: #fff; }

.oled-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.oled-actions .btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.btn:disabled { opacity: .35; cursor: not-allowed; pointer-events: none; }

/* Color wheel + brightness + actions */
.color-picker-row { display: flex; align-items: flex-start; gap: 16px; }

.color-wheel-wrap { flex: 1 1 auto; display: flex; flex-direction: column; align-items: center; gap: 6px; min-width: 0; }
.color-wheel-wrap .hint { margin: 0; text-align: center; }
.color-wheel-inner { position: relative; width: 100%; max-width: 180px; }
#color-wheel { display: block; width: 100%; height: auto; border-radius: 50%; touch-action: none; cursor: pointer; }
.color-wheel-pointer {
  position: absolute;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, .4), 0 2px 6px rgba(0, 0, 0, .5);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.brightness-wrap { display: flex; flex-direction: column; align-items: center; gap: 6px; flex: none; }
.brightness-wrap .hint { margin: 0; text-align: center; }
input[type="range"].brightness-slider {
  writing-mode: vertical-lr;
  direction: rtl;
  width: 28px;
  height: 180px;
  margin: 0;
  background: rgba(255, 255, 255, .14);
  border-radius: 999px;
}
input[type="range"].brightness-slider::-webkit-slider-runnable-track { background: transparent; }
input[type="range"].brightness-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--accent);
  box-shadow: 0 2px 6px rgba(0, 0, 0, .45);
  cursor: pointer;
}
input[type="range"].brightness-slider::-moz-range-track { background: rgba(255, 255, 255, .14); border-radius: 999px; }
input[type="range"].brightness-slider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--accent);
  box-shadow: 0 2px 6px rgba(0, 0, 0, .45);
  cursor: pointer;
}

.color-actions { display: flex; flex-direction: column; gap: 10px; justify-content: center; flex: none; width: 110px; }
.color-actions .btn { padding: 12px 8px; font-size: .82rem; }

/* Buttons */
.btn {
  width: 100%;
  padding: 15px;
  border: 0;
  border-radius: 14px;
  color: #fff;
  font: inherit;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: transform .1s, filter .15s;
}
.btn:active { transform: scale(.98); }
.btn--primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); margin-top: 4px; }
.btn--primary:hover { filter: brightness(1.08); }
.btn--ghost { background: var(--surface-strong); border: 1px solid var(--border); color: var(--text); }
.btn--muted { background: rgba(255, 255, 255, .06); color: var(--text-dim); }

/* Fields / single sliders */
.field { display: grid; gap: 8px; font-weight: 600; }
.field-label { display: flex; align-items: center; justify-content: space-between; font-size: .88rem; }
.badge {
  font-weight: 700;
  font-size: .78rem;
  color: #fff;
  background: var(--surface-strong);
  border: 1px solid var(--border);
  padding: 3px 10px;
  border-radius: 999px;
}
.field-grid { display: grid; gap: 14px; margin: 14px 0; }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  border-radius: 999px;
  background: linear-gradient(to right, var(--accent) 0%, var(--accent) var(--fill, 50%), rgba(255, 255, 255, .14) var(--fill, 50%), rgba(255, 255, 255, .14) 100%);
  outline: none;
  margin: 6px 0;
}
input[type="range"]::-webkit-slider-runnable-track { -webkit-appearance: none; background: transparent; height: 6px; }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 22px;
  height: 22px;
  margin-top: -8px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--accent);
  box-shadow: 0 2px 6px rgba(0, 0, 0, .45);
  cursor: pointer;
}
input[type="range"]::-moz-range-track { background: transparent; height: 6px; border-radius: 999px; }
input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--accent);
  box-shadow: 0 2px 6px rgba(0, 0, 0, .45);
  cursor: pointer;
}

/* Dual-handle range (sweep / strike) */
.dual-range { position: relative; height: 34px; margin-top: 10px; }
.dual-range-track {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 6px;
  transform: translateY(-50%);
  background: rgba(255, 255, 255, .14);
  border-radius: 999px;
}
.dual-range-fill {
  position: absolute;
  top: 0;
  bottom: 0;
  background: linear-gradient(to right, var(--accent), var(--accent-2));
  border-radius: 999px;
}
.dual-range input[type="range"] {
  position: absolute;
  inset: 0;
  width: 100%;
  margin: 0;
  background: transparent;
  pointer-events: none;
}
.dual-range input[type="range"]::-webkit-slider-runnable-track { background: transparent; }
.dual-range input[type="range"]::-moz-range-track { background: transparent; }
.dual-range input[type="range"]::-webkit-slider-thumb { pointer-events: auto; margin-top: -8px; }
.dual-range input[type="range"]::-moz-range-thumb { pointer-events: auto; }
.dual-range-labels { display: flex; justify-content: space-between; color: var(--text-dim); font-size: .82rem; font-weight: 600; margin-bottom: 4px; }

/* Servo dial */
.dial-wrap { position: relative; }
.dial { width: 100%; display: block; touch-action: none; }
#dial-hit { cursor: pointer; }
.dial-value {
  display: block;
  text-align: center;
  font-size: 1.4rem;
  font-weight: 700;
  margin: 2px 0 12px;
}

/* Chips */
.chip-row { display: flex; gap: 8px; flex-wrap: wrap; margin: 4px 0 2px; }
.chip {
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-strong);
  color: var(--text);
  font: inherit;
  font-size: .82rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.chip:active { transform: scale(.96); }

/* Songs */
.song-list { display: grid; gap: 10px; }
.song-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface-strong);
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: transform .1s, background .15s;
}
.song-card:active { transform: scale(.98); }
.song-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--dot); flex: none; box-shadow: 0 0 10px var(--dot); }
.song-info { display: grid; gap: 2px; flex: 1; }
.song-info strong { font-size: .95rem; }
.song-info small { color: var(--text-dim); }
.song-play { color: var(--text-dim); font-size: .9rem; }

/* PIN modal */
dialog {
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 22px;
  background: #131a2e;
  color: var(--text);
  width: min(320px, 90vw);
}
dialog::backdrop { background: rgba(5, 8, 16, .65); backdrop-filter: blur(2px); }
dialog h2 { margin-top: 0; }
#pin-input {
  width: 100%;
  box-sizing: border-box;
  padding: 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-strong);
  color: var(--text);
  font-size: 1.4rem;
  letter-spacing: .3em;
  text-align: center;
  margin: 10px 0 4px;
}
#pin-input:focus { outline: 2px solid var(--accent); }
.dialog-actions { margin-top: 14px; }

small { color: var(--text-dim); font-weight: 400; }

/* ── Responsive ──
   The layout is already fluid (max-width + clamp()) down to phone widths,
   but two spots assumed enough horizontal room that they never actually
   had on a narrow screen: the color-picker row has two fixed-width children
   (brightness slider, action buttons) that don't shrink, so the wheel
   between them just got squeezed toward nothing instead of the row
   wrapping; and five/four-column tab-style grids get tight enough for
   longer labels to truncate via ellipsis rather than just fitting. */
@media (max-width: 420px) {
  .tab { font-size: .76rem; padding: 9px 3px; }
  .segmented-option { font-size: .78rem; padding: 10px 3px; }

  .color-picker-row { flex-wrap: wrap; }
  .color-wheel-wrap { flex: 1 1 180px; }
  .color-actions {
    flex: 1 1 100%;
    flex-direction: row;
    width: auto;
  }
  .color-actions .btn { flex: 1; }
}
