/* Agado's dark navy / blue palette. Green and amber identify tuning state. */
:root {
  color-scheme: dark;
  --bg: #0a1628;
  --surface: #0c1a2e;
  --surface-2: #101f36;
  --line: #2a4a80;
  --line-soft: #1c304f;
  --tick: #627897;
  --text: #f3f4f6;
  --muted: #a2aec1;
  --faint: #627897;
  --accent: #8ab4f8;
  --accent-strong: #5b8ce0;
  --accent-soft: #1e3a6e;
  --primary-text: #0a1628;
  --in-tune: #6cdaa3;
  --in-tune-soft: #6cdaa324;
  --off: #f0bd72;
  --danger: #ff8e87;
  --link: #8ab4f8;
  --radius: 12px;
  --radius-sm: 8px;
  --gutter: 20px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-synthesis: none;
  -webkit-text-size-adjust: 100%;
}

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

body {
  margin: 0;
  min-height: 100svh;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
}

h1, h2, h3, p, dl, dd { margin: 0; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button, a { -webkit-tap-highlight-color: transparent; }
a { color: var(--link); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
::selection { background: var(--accent-soft); color: var(--text); }

/* App shell */
.app {
  display: flex;
  flex-direction: column;
  max-width: 480px;
  min-height: 100svh;
  margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) var(--gutter) max(12px, env(safe-area-inset-bottom));
}

.app-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 48px;
  border-bottom: 1px solid var(--line-soft);
}

.wordmark { font-size: 17px; font-weight: 600; letter-spacing: -0.01em; }
.app-bar-end { display: flex; align-items: center; gap: 6px; }

.live-state { display: flex; align-items: center; gap: 7px; font-size: 13px; color: var(--muted); }
.live-state i { width: 7px; height: 7px; border-radius: 50%; background: var(--tick); }
.live-state i.active { background: var(--accent); box-shadow: 0 0 0 3px #8ab4f814; }

.icon-button {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  margin-right: -11px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--muted);
}

/* Readout */
.instrument {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: stretch;
  padding: 8px 0 16px;
  text-align: center;
}

.mode { font-size: 12px; font-weight: 500; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }

.note-display {
  position: relative;
  align-self: center;
  display: flex;
  font-size: clamp(84px, 19svh, 148px);
  font-weight: 600;
  line-height: 1.08;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
}

#octave {
  position: absolute;
  left: 100%;
  bottom: 0.17em;
  margin-left: 0.07em;
  font-size: 0.26em;
  font-weight: 500;
  letter-spacing: 0;
  line-height: 1;
  color: var(--muted);
}

.frequency { font-size: 16px; color: var(--muted); font-variant-numeric: tabular-nums; }
.gauge { margin: clamp(14px, 3.4svh, 32px) 0 clamp(12px, 2.4svh, 22px); }

.instrument[data-state="idle"] #note { color: var(--faint); font-weight: 300; letter-spacing: 0; }
.instrument[data-state="in-tune"] #note,
.instrument[data-state="in-tune"] #direction { color: var(--in-tune); }
.instrument[data-state="flat"] #direction,
.instrument[data-state="sharp"] #direction { color: var(--off); }

/* Meter: 1% of the width is 1 cent, so the band can show the real tolerance */
.meter-area, #strobe-area { padding: 0 12px; }
.meter { --tolerance: 1; position: relative; height: 52px; }

.meter-band {
  position: absolute;
  inset: 0 auto 0 calc(50% - var(--tolerance) * 1%);
  width: calc(var(--tolerance) * 2%);
  min-width: 6px;
  border-radius: 3px;
  background: var(--in-tune-soft);
}

.instrument[data-state="in-tune"] .meter-band { background: var(--in-tune); opacity: 0.35; }
.instrument[data-state="idle"] .meter-band { background: #8ab4f81a; }

.ticks { position: absolute; inset: 0; }
.ticks i {
  position: absolute;
  top: 50%;
  width: 1px;
  height: 12px;
  background: var(--tick);
  transform: translate(-50%, -50%);
}
.ticks i.major { height: 24px; }
.ticks i.center { width: 2px; height: 100%; background: var(--accent); }
.instrument[data-state="in-tune"] .ticks i.center { background: var(--in-tune); }

.needle {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 4px;
  border-radius: 2px;
  background: var(--off);
  box-shadow: 0 0 0 2px var(--bg);
  transform: translateX(-50%);
  transition: left 0.11s ease-out;
}
.instrument[data-state="in-tune"] .needle { background: var(--in-tune); }

.scale { position: relative; height: 18px; margin-top: 6px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.scale span { position: absolute; transform: translateX(-50%); }

/* Strobe */
.strobe {
  --band: var(--text);
  display: flex;
  flex-direction: column;
  gap: 3px;
  height: 52px;
  overflow: hidden;
  border-radius: 4px;
}
.strobe i {
  flex: 1;
  background-image: linear-gradient(to right, var(--band) 50%, transparent 50%);
  background-size: 28px 100%;
  background-position-x: var(--phase, 0px);
}
.strobe i:nth-child(2) { background-size: 14px 100%; background-position-x: calc(var(--phase, 0px) * 2); opacity: 0.65; }
.strobe i:nth-child(3) { background-size: 7px 100%; background-position-x: calc(var(--phase, 0px) * 4); opacity: 0.4; }
.strobe.idle { opacity: 0.18; }
.instrument[data-state="in-tune"] .strobe { --band: var(--in-tune); }

.strobe-labels {
  display: flex;
  justify-content: space-between;
  height: 18px;
  margin-top: 6px;
  font-size: 12px;
  color: var(--muted);
}

/* Feedback */
.feedback {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  font-size: 17px;
  font-weight: 600;
}
#cents { font-weight: 500; font-variant-numeric: tabular-nums; }

.signal-row { display: flex; align-items: center; gap: 12px; margin-top: 10px; font-size: 12px; color: var(--muted); }
.signal { flex: 1; height: 3px; overflow: hidden; border-radius: 2px; background: var(--line); }
#signal-level { width: 0; height: 100%; background: var(--accent-strong); transition: width 0.1s; }

/* Strings */
.strings-section { padding-top: 12px; border-top: 1px solid var(--line-soft); }
.strings-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
  min-height: 40px;
  margin-bottom: 10px;
}
.calibration { margin-left: auto; font-size: 13px; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }

.strings {
  display: grid;
  grid-template-columns: 1.2fr repeat(6, 1fr);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.string {
  position: relative;
  min-width: 0;
  height: 60px;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  border: 0;
  border-left: 1px solid var(--line);
  background: transparent;
}
.string:first-child { border-left: 0; }
.string:focus-visible { outline-offset: -3px; }
.string > span { font-size: 18px; font-weight: 600; line-height: 1.2; }
.string:first-child > span { font-size: 15px; }
.string sub { font-size: 11px; font-weight: 500; vertical-align: baseline; position: relative; top: 0.2em; margin-left: 1px; color: var(--muted); }
.string small { font-size: 12px; color: var(--muted); }
.string.selected { background: var(--accent-soft); color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent-strong); }
.string.selected:first-child { border-radius: calc(var(--radius) - 1px) 0 0 calc(var(--radius) - 1px); }
.string.selected:last-child { border-radius: 0 calc(var(--radius) - 1px) calc(var(--radius) - 1px) 0; }
.string.selected sub, .string.selected small { color: inherit; }
.string.detected::after {
  content: "";
  position: absolute;
  left: 25%;
  right: 25%;
  bottom: 4px;
  height: 2px;
  border-radius: 1px;
  background: currentColor;
}

/* Buttons */
.button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 48px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  text-decoration: none;
}
.button:disabled { opacity: 0.5; cursor: default; }
.button-primary { background: var(--accent); border-color: var(--accent); color: var(--primary-text); }
#start { min-height: 52px; margin-top: 12px; font-size: 16px; }
#start[data-state="on"] { background: var(--surface); border-color: var(--accent-strong); color: var(--accent); }
.string:active, .button:active { opacity: 0.8; }

button, .button, select { transition: background-color 0.15s, border-color 0.15s, color 0.15s; }
@media (hover: hover) {
  .icon-button:hover, .text-button:hover { background: var(--surface-2); color: var(--accent); }
  .string:not(.selected):hover, .button:not(.button-primary):hover { background: var(--surface-2); }
  .button-primary:not(:disabled):hover { background: #a4c5fb; border-color: #a4c5fb; }
  #start[data-state="on"]:hover { background: var(--accent-soft); }
}

.status { min-height: 38px; margin-top: 8px; font-size: 13px; color: var(--muted); text-align: center; }
.status a { color: inherit; }

/* Selects */
.select { position: relative; display: inline-flex; align-items: center; }
.select::after {
  content: "";
  position: absolute;
  right: 12px;
  width: 7px;
  height: 7px;
  margin-top: -3px;
  border: solid var(--muted);
  border-width: 0 1.5px 1.5px 0;
  transform: rotate(45deg);
  pointer-events: none;
}
.select select {
  appearance: none;
  -webkit-appearance: none;
  min-height: 40px;
  padding: 0 30px 0 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 16px;
}
.select-tuning { flex: 1; min-width: 190px; }
.select-tuning::after { right: 16px; width: 8px; height: 8px; border-color: var(--accent); border-width: 0 2px 2px 0; }
.select-tuning select {
  width: 100%;
  min-height: 44px;
  padding: 0 40px 0 14px;
  border-color: var(--accent-strong);
  background: var(--surface-2);
  color: var(--accent);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
@media (hover: hover) {
  .select-tuning select:hover { background: var(--accent-soft); border-color: var(--accent); }
}

/* Settings sheet */
.sheet {
  width: 100%;
  max-width: 480px;
  max-height: 88svh;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 16px 16px 0 0;
  background: var(--bg);
  color: var(--text);
}
.sheet[open] { display: flex; flex-direction: column; }
.sheet::backdrop { background: #020817b3; }

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 56px;
  padding: 0 var(--gutter);
  border-bottom: 1px solid var(--line);
}
.sheet-header h2 { font-size: 17px; font-weight: 600; }

.text-button {
  min-height: 44px;
  margin-right: -10px;
  padding: 0 10px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
}

.sheet-body {
  overflow-y: auto;
  padding: 16px var(--gutter) max(20px, env(safe-area-inset-bottom));
}
.sheet-body h3 { margin: 24px 0 8px; font-size: 13px; font-weight: 600; color: var(--muted); }

.group { overflow: hidden; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 56px;
  padding: 8px 14px;
  border-top: 1px solid var(--line);
}
.row:first-child { border-top: 0; }

.segmented { display: flex; padding: 2px; border-radius: var(--radius-sm); background: var(--surface-2); }
.segmented button {
  min-width: 72px;
  min-height: 36px;
  padding: 0 12px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 500;
}
.segmented button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); box-shadow: 0 0 0 1px var(--accent-strong); }
.mode-switch { margin-bottom: 12px; }
.mode-switch button { flex: 1; min-height: 44px; }
.chromatic-label { font-size: 15px; font-weight: 600; }
.chromatic-help { display: grid; place-content: center; min-height: 60px; font-size: 13px; line-height: 1.65; color: var(--muted); text-align: center; }

.sheet input {
  width: 100%;
  min-width: 0;
  min-height: 40px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 16px;
  font-variant-numeric: tabular-nums;
}
.sheet input:invalid { border-color: var(--danger); }

.field-unit { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.field-unit input { width: 88px; color: var(--text); text-align: right; }

.offsets { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.offsets label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); }
.offsets input { color: var(--text); }

.diagnostics dd { font-variant-numeric: tabular-nums; }
.diagnostics .row { min-height: 44px; }

.note { margin-top: 10px; font-size: 13px; color: var(--muted); }
.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 20px; }

/* Short phones: everything still fits on one screen */
@media (max-height: 700px) and (orientation: portrait) {
  .app { padding-top: max(2px, env(safe-area-inset-top)); padding-bottom: max(6px, env(safe-area-inset-bottom)); }
  .app-bar { min-height: 44px; }
  .instrument { padding: 0 0 6px; }
  .note-display { font-size: clamp(60px, 12svh, 84px); }
  .frequency { font-size: 15px; }
  .gauge { margin: 8px 0; }
  .meter, .strobe { height: 44px; }
  .signal-row { margin-top: 6px; }
  .strings-section { padding-top: 8px; }
  .mode-switch { margin-bottom: 8px; }
  .strings-header { min-height: 36px; margin-bottom: 8px; }
  .chromatic-help { min-height: 50px; }
  .string { height: 50px; }
  #start { min-height: 46px; margin-top: 8px; }
  .status { min-height: 19px; margin-top: 4px; }
}

@media (max-width: 350px) {
  :root { --gutter: 16px; }
  .string > span { font-size: 16px; }
  .string:first-child > span { font-size: 14px; }
  .calibration { font-size: 12px; }
}

/* Larger screens: keep the instrument composed in the middle of the page */
@media (min-width: 600px) and (min-height: 640px) {
  body { display: grid; place-items: center; }
  .app { width: 100%; min-height: min(100svh, 820px); }
  .sheet { margin: auto; max-height: min(88svh, 720px); border-radius: 16px; }
}

/* Phone in landscape: readout beside the controls */
@media (orientation: landscape) and (max-height: 500px) {
  .app {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-rows: auto 1fr auto auto;
    column-gap: 32px;
    max-width: 820px;
    padding-left: max(var(--gutter), env(safe-area-inset-left));
    padding-right: max(var(--gutter), env(safe-area-inset-right));
  }
  .app-bar { grid-column: 2; grid-row: 1; }
  .instrument { grid-column: 1; grid-row: 1 / -1; padding: 0; }
  .strings-section { grid-column: 2; grid-row: 2; align-self: center; }
  #start { grid-column: 2; grid-row: 3; }
  .status { grid-column: 2; grid-row: 4; min-height: 19px; margin-top: 6px; }
  .note-display { font-size: clamp(64px, 26svh, 96px); }
  .gauge { margin: 8px 0; }
  .string { height: 52px; }
  .sheet { margin: auto; max-height: 94svh; border-radius: 16px; }
}

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

/* Document pages: setup and measurement lab */
.page {
  max-width: 600px;
  margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) var(--gutter) max(32px, env(safe-area-inset-bottom));
}
.page h1 { margin: 16px 0 8px; font-size: 26px; font-weight: 600; letter-spacing: -0.015em; line-height: 1.2; }
.page h2 { margin: 32px 0 8px; font-size: 17px; font-weight: 600; }
.page p, .page li { color: var(--muted); }
.page p { margin: 8px 0; }
.page ol { margin: 8px 0; padding-left: 22px; }
.page li { margin-bottom: 10px; }
.page li strong { color: var(--text); font-weight: 600; }
.page .back { display: inline-block; padding: 6px 0; font-size: 14px; }
.page .button { margin: 16px 0 8px; }
.page .links { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px 16px; font-size: 14px; }

.page label { display: block; margin-top: 16px; font-size: 14px; font-weight: 500; }
.page label input, .page label select {
  display: block;
  width: 100%;
  min-height: 44px;
  margin-top: 6px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 16px;
  font-weight: 400;
}

.page .result {
  margin-top: 16px;
  padding: 14px 16px;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  font: 13px/1.8 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
}

.page details { margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--line); }
.page summary { min-height: 32px; font-weight: 600; cursor: pointer; }
.page code { display: block; font-size: 12px; overflow-wrap: anywhere; color: var(--muted); }
.scale span:nth-child(1) { left: 0; }
.scale span:nth-child(2) { left: 25%; }
.scale span:nth-child(3) { left: 50%; }
.scale span:nth-child(4) { left: 75%; }
.scale span:nth-child(5) { left: 100%; }
