:root {
  --bg: #14110d;
  --surface: #1d1913;
  --surface-2: #29231b;
  --line: #3b3228;
  --text: #f2e9da;
  --muted: #9d907c;
  --accent: #f0a23b;
  --accent-ink: #1b1208;
  --good: #86d69a;
  --radius: 18px;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-display: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --font-num: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5efe4;
    --surface: #fffaf1;
    --surface-2: #ece3d3;
    --line: #d9ccb6;
    --text: #221b12;
    --muted: #74675a;
    --accent: #c06a00;
    --accent-ink: #fffaf1;
    --good: #2c8a44;
    color-scheme: light;
  }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 var(--font-ui);
  -webkit-tap-highlight-color: transparent;
}

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

.app {
  max-width: 480px;
  margin: 0 auto;
  padding:
    max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

/* --- Header & tabs ------------------------------------------------------ */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 44px;
}

.brand {
  margin: 0;
  font: 600 1.25rem/1 var(--font-display);
  letter-spacing: 0.01em;
}
.brand span { color: var(--accent); font-style: italic; padding: 0 0.08em; }

.install {
  border: 1px solid var(--line);
  background: transparent;
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 0.875rem;
  cursor: pointer;
}

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  margin: 8px 0 16px;
  background: var(--surface-2);
  border-radius: 999px;
}
.tabs button {
  border: 0;
  border-radius: 999px;
  padding: 10px;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
  cursor: pointer;
  transition: background-color 150ms, color 150ms;
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / 0.2); }

/* --- Shared controls ---------------------------------------------------- */

.btn {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 14px;
  padding: 12px 18px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  transition: background-color 120ms, transform 80ms;
}
.btn:active { transform: scale(0.97); }
.btn.icon {
  min-width: 44px;
  min-height: 44px;
  padding: 0 10px;
  font-family: var(--font-num);
  font-weight: 500;
}
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-size: 1.125rem;
}
.btn[aria-pressed="true"]:not(.primary) { background: var(--surface-2); border-color: var(--accent); }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

select {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%239d907c' stroke-width='1.5'/%3E%3C/svg%3E") no-repeat right 12px center;
  border-radius: 12px;
  padding: 10px 32px 10px 12px;
  min-height: 44px;
  cursor: pointer;
}

input[type="range"] { accent-color: var(--accent); width: 100%; min-width: 0; }

.actions { display: grid; margin: 20px 0; }
.actions.two { grid-template-columns: 1fr 1.4fr; gap: 12px; }
.actions .btn { min-height: 56px; }

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 4px 16px;
}
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 60px;
  padding: 8px 0;
}
.row + .row { border-top: 1px solid var(--line); }
.row input[type="range"] { max-width: 200px; }
.label { color: var(--muted); font-size: 0.9375rem; }

.stepper { display: flex; align-items: center; gap: 6px; }
.stepper .value {
  min-width: 72px;
  text-align: center;
  font: 600 1rem var(--font-num);
  background: none;
  border: 0;
  padding: 8px 0;
}
.stepper button.value { cursor: pointer; }

.tone { display: flex; gap: 6px; }
.tone select { padding-right: 28px; }

.segmented { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.segmented span {
  display: grid;
  place-items: center;
  min-width: 40px;
  min-height: 38px;
  border-radius: 9px;
  font: 600 0.9375rem var(--font-num);
  color: var(--muted);
}
.segmented input:checked + span { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / 0.2); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }

.hint { color: var(--muted); font-size: 0.8125rem; text-align: center; margin: 20px 0 0; }
@media (pointer: coarse) { .hint { display: none; } }

.status {
  margin: 12px 0 0;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.9375rem;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.9375rem;
  font-weight: 600;
  white-space: nowrap;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.3);
}

/* --- Tuner -------------------------------------------------------------- */

.tuner-display {
  --state: var(--muted);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 12px 20px;
  text-align: center;
}
.tuner-display[data-state="flat"],
.tuner-display[data-state="sharp"] { --state: var(--accent); }
.tuner-display[data-state="in-tune"] { --state: var(--good); }

.gauge { display: block; width: 100%; max-width: 360px; margin: 0 auto; overflow: visible; }
.gauge-arc { fill: none; stroke: var(--line); stroke-width: 2; }
.gauge-zone { fill: none; stroke: var(--good); stroke-width: 6; stroke-linecap: round; opacity: 0.35; transition: opacity 200ms; }
.tuner-display[data-state="in-tune"] .gauge-zone { opacity: 1; }
.tick { stroke: var(--muted); stroke-width: 1.5; opacity: 0.6; }
.tick.major { stroke-width: 2.5; opacity: 1; }
.tick-label { fill: var(--muted); font: 11px var(--font-num); text-anchor: middle; dominant-baseline: middle; }
.gauge-symbol { fill: var(--muted); font: 20px var(--font-display); text-anchor: middle; }

.needle {
  stroke: var(--state);
  stroke-width: 3.5;
  stroke-linecap: round;
  transform-box: view-box;
  transform-origin: 150px 172px;
  transition: transform 90ms linear, stroke 200ms;
}
.tuner-display[data-state="idle"] .needle { opacity: 0.35; }
.hub { fill: var(--state); transition: fill 200ms; }

.readout { margin-top: 4px; }
.note {
  font: 500 clamp(4.5rem, 24vw, 6.5rem)/1 var(--font-display);
  color: var(--text);
  min-height: 1em;
}
.tuner-display[data-state="idle"] .note,
.tuner-display[data-state="listening"] .note { color: var(--muted); }
.tuner-display[data-state="in-tune"] .note { color: var(--good); }
.octave { font-size: 0.4em; color: var(--muted); margin-left: 0.05em; vertical-align: -0.1em; }
.cents { margin-top: 10px; font: 600 1.0625rem var(--font-num); color: var(--state); }
.freq { margin-top: 4px; min-height: 1.4em; font: 0.8125rem var(--font-num); color: var(--muted); }

/* --- Metronome ---------------------------------------------------------- */

.tempo {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 24px 16px 20px;
  text-align: center;
}
.bpm { display: flex; align-items: baseline; justify-content: center; gap: 8px; }
.bpm input {
  width: 3.2ch;
  border: 0;
  background: none;
  padding: 0;
  text-align: center;
  font: 500 clamp(4.5rem, 24vw, 6.5rem)/1 var(--font-num);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.04em;
  -moz-appearance: textfield;
  appearance: textfield;
}
.bpm input::-webkit-inner-spin-button,
.bpm input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.bpm input:focus-visible { outline: none; border-bottom: 2px solid var(--accent); }
.bpm span { color: var(--muted); font-weight: 600; letter-spacing: 0.08em; font-size: 0.875rem; }
.tempo-name { margin-top: 6px; font: italic 1.25rem var(--font-display); color: var(--accent); }

.beats {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin: 20px 0;
}
.beat {
  width: 34px;
  height: 34px;
  padding: 0;
  border-radius: 50%;
  border: 2px solid var(--line);
  background: var(--surface-2);
  cursor: pointer;
  position: relative;
  transition: background-color 60ms, border-color 60ms, transform 60ms;
}
.beat[data-level="2"] { border-color: var(--accent); }
.beat[data-level="2"]::after {
  content: "";
  position: absolute;
  inset: 8px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.5;
}
.beat[data-level="0"] { background: transparent; border-style: dashed; opacity: 0.6; }
.beat.current { background: var(--text); border-color: var(--text); transform: scale(1.12); }
.beat.current[data-level="2"] { background: var(--accent); border-color: var(--accent); }
.beat.current[data-level="2"]::after { opacity: 0; }
.beat.current[data-level="0"] { background: transparent; border-color: var(--muted); }

.tempo-adjust { display: flex; align-items: center; gap: 6px; }
.tempo-adjust input { flex: 1; margin: 0 4px; }
.tempo-adjust .btn.icon { min-width: 44px; padding: 0 6px; font-size: 0.875rem; }

@media (max-width: 360px) {
  .tempo-adjust .btn.icon[data-bpm-delta="-5"],
  .tempo-adjust .btn.icon[data-bpm-delta="5"] { display: none; }
}

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