:root {
  --bg: #0b1220;
  --surface: #131c2e;
  --surface-2: #1b2539;
  --line: #2a3650;
  --text: #e6edf7;
  --muted: #8a97ad;
  --accent: #60a5fa;
  --good: #34d399;
  --close: #fbbf24;
  --off: #f87171;
  --state: var(--muted);
  --radius: 14px;
  color-scheme: dark;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}

button,
select,
output {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

[hidden] {
  display: none !important;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- Layout ---------- */

.app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
  min-height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  padding:
    max(12px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
}

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tuning-picker {
  position: relative;
  flex: 1;
  min-width: 0;
}

.tuning-picker select {
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 10px 40px 10px 16px;
  font-size: 1rem;
  font-weight: 600;
  text-overflow: ellipsis;
}

.tuning-picker .chevron {
  position: absolute;
  right: 14px;
  top: 50%;
  width: 18px;
  height: 18px;
  transform: translateY(-50%);
  pointer-events: none;
  color: var(--muted);
}

.icon-button {
  display: grid;
  place-items: center;
  flex: none;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
}

.icon-button svg {
  width: 20px;
  height: 20px;
}

/* ---------- Tuner display ---------- */

.tuner {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 8px 0;
  text-align: center;
}

[data-state="in-tune"] { --state: var(--good); }
[data-state="close"] { --state: var(--close); }
[data-state="off"] { --state: var(--off); }

.note {
  display: flex;
  align-items: baseline;
  justify-content: center;
  min-height: 1em;
  font-size: clamp(5.5rem, 28vw, 9rem);
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.03em;
  color: var(--state);
  transition: color 0.2s, opacity 0.3s;
  font-variant-numeric: tabular-nums;
}

.note-octave {
  margin-left: 0.04em;
  font-size: 0.32em;
  font-weight: 600;
  color: var(--muted);
}

[data-state="in-tune"] .note {
  text-shadow: 0 0 40px color-mix(in srgb, var(--good) 45%, transparent);
}

.stale .note,
.stale .readout,
.stale .needle {
  opacity: 0.45;
}

.gauge {
  width: min(100%, 420px);
  height: auto;
  overflow: visible;
}

.gauge-track {
  stroke: var(--line);
  stroke-width: 6;
}

.gauge-zone {
  stroke: var(--good);
  stroke-width: 6;
  stroke-linecap: butt;
  opacity: 0.85;
}

#gauge-ticks line {
  stroke: var(--line);
  stroke-width: 2;
}

#gauge-ticks line.major {
  stroke: var(--muted);
  stroke-width: 3;
}

#gauge-ticks line.center {
  stroke: var(--good);
}

.gauge-label {
  fill: var(--muted);
  stroke: none;
  font-size: 22px;
  font-weight: 600;
}

.needle {
  color: var(--state);
  transition: color 0.2s, opacity 0.3s;
}

.needle line {
  stroke: currentColor;
  stroke-width: 4;
}

.needle circle {
  fill: currentColor;
  stroke: var(--bg);
  stroke-width: 3;
}

[data-state="idle"] .needle {
  opacity: 0.35;
}

.readout {
  display: flex;
  gap: 18px;
  justify-content: center;
  color: var(--muted);
  font-size: 1.05rem;
  font-variant-numeric: tabular-nums;
  transition: opacity 0.3s;
}

.readout .cents:empty {
  display: none;
}

.readout .cents {
  min-width: 4.5ch;
  color: var(--state);
  font-weight: 600;
}

.status {
  min-height: 1.5em;
  margin: 6px 0 0;
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--state);
  transition: color 0.2s;
}

[data-state="idle"] .status {
  color: var(--muted);
  font-weight: 500;
}

/* ---------- Strings ---------- */

.strings-bar {
  display: grid;
  gap: 14px;
  padding-top: 8px;
}

.strings {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 60px);
  justify-content: center;
  gap: 8px;
}

.string {
  position: relative;
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  padding: 0;
  border: 2px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
  font-size: clamp(1rem, 4.6vw, 1.3rem);
  font-weight: 700;
  line-height: 1;
  white-space: nowrap;
  transition: border-color 0.15s, background-color 0.15s, color 0.15s;
}

.string small {
  font-size: 0.55em;
  font-weight: 600;
  color: var(--muted);
}

.string.active {
  border-color: var(--state, var(--accent));
}

.string.locked {
  background: var(--surface-2);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}

.string.tuned::after {
  content: "";
  position: absolute;
  top: -3px;
  right: -3px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--good) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M5 12.5l4.5 4.5L19 7.5' fill='none' stroke='%230b1220' stroke-width='4' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 10px no-repeat;
  border: 2px solid var(--bg);
}

.controls {
  display: flex;
  justify-content: center;
  gap: 10px;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 18px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-weight: 600;
}

.pill svg {
  width: 18px;
  height: 18px;
}

.pill[aria-pressed="true"] {
  border-color: var(--accent);
  background: #1e3150;
  background: color-mix(in srgb, var(--accent) 18%, var(--surface));
}

.pill:disabled {
  opacity: 0.5;
  cursor: default;
}

/* ---------- Notice (microphone problems) ---------- */

.notice {
  display: grid;
  justify-items: center;
  gap: 10px;
  max-width: 340px;
  margin-top: 6px;
  padding: 14px 18px;
  border: 1px solid color-mix(in srgb, var(--off) 45%, var(--line));
  border-radius: var(--radius);
  background: var(--surface);
}

.notice p {
  margin: 0;
  color: var(--text);
  line-height: 1.45;
}

.primary {
  min-height: 48px;
  padding: 0 28px;
  border: none;
  border-radius: 999px;
  background: var(--good);
  color: #062016;
  font-size: 1.05rem;
  font-weight: 700;
}

.primary:disabled {
  opacity: 0.6;
}

/* ---------- Settings ---------- */

dialog {
  width: min(100% - 32px, 460px);
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--surface);
  color: var(--text);
}

dialog::backdrop {
  background: rgb(0 0 0 / 0.55);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
}

.settings {
  display: grid;
  gap: 4px;
  padding: 8px 20px 20px;
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.settings-header h2 {
  margin: 0;
  font-size: 1.2rem;
}

.settings-header .icon-button {
  border: none;
  background: none;
  margin-right: -10px;
}

.setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 0;
  border-top: 1px solid var(--line);
}

.setting-block {
  display: grid;
  justify-items: start;
  gap: 10px;
}

.setting-block p {
  margin: 0;
}

.setting-hint.warning {
  color: var(--close);
}

.setting-title {
  font-weight: 600;
}

.setting-hint {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.4;
}

kbd {
  padding: 1px 5px;
  border: 1px solid var(--line);
  border-radius: 4px;
  font: inherit;
  font-size: 0.85em;
}

.stepper {
  display: flex;
  align-items: center;
  flex: none;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg);
}

.stepper button {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: none;
  font-size: 1.3rem;
}

.stepper output {
  min-width: 4.6em;
  text-align: center;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.switch {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  flex: none;
  width: 50px;
  height: 30px;
  margin: 0;
  border-radius: 999px;
  background: var(--line);
  cursor: pointer;
  transition: background-color 0.2s;
}

.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--text);
  transition: transform 0.2s;
}

.switch:checked {
  background: var(--good);
}

.switch:checked::after {
  transform: translateX(20px);
}

/* ---------- Update toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 8px 8px 8px 18px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-2);
  box-shadow: 0 10px 30px rgb(0 0 0 / 0.4);
  transform: translateX(-50%);
}

.toast > button:not(.icon-button) {
  min-height: 36px;
  padding: 0 16px;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: #06162b;
  font-weight: 700;
}

.toast .icon-button {
  width: 36px;
  height: 36px;
  border: none;
  background: none;
}

/* ---------- Small / short screens ---------- */

@media (max-height: 640px) {
  .note {
    font-size: clamp(4rem, 20vw, 6rem);
  }

  .gauge {
    width: min(100%, 320px);
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}
