/*
 * Tuner. One screen, sized by the window's height: the instrument and the
 * view at the top, the strings, the dial with the note inside its arc (or
 * the strobe, with the note above it), the pitch trace in whatever height is
 * left, and at the bottom, in the thumb's reach, Tone and Start. The
 * instrument, the reference tone and the settings are sheets (app-shell.css,
 * run by nav.js). Light and dark come from theme.js.
 */

body.qp-app {
  --tu-cell: color-mix(in srgb, var(--text, #191918) 6%, var(--surface, #fffdf8));
  --tu-line: color-mix(in srgb, var(--text, #191918) 12%, transparent);
  --tu-dim: color-mix(in srgb, var(--text, #191918) 28%, transparent);
  --tu-ok: var(--ab-accent);
  --tu-off: #d9480f;
}

html[data-theme="dark"] body.qp-app { --tu-off: #ff8a4c; }

/* ---------- Stage ---------- */

.tu-main {
  display: flex;
  justify-content: center;
  height: calc(100vh - var(--qp-bar-actual, var(--qp-bar-h)));
  height: calc(100dvh - var(--qp-bar-actual, var(--qp-bar-h)));
  min-height: 480px;
  overflow: hidden;
}

html.qp-drawer-open .tu-main { padding-right: 380px; }

/* Spare height is shared out between the parts, and Start stays at the
   bottom where the thumb is. */
.tu-stage {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: clamp(10px, 2vh, 22px);
  width: min(100%, 620px);
  padding: clamp(12px, 2.4vh, 28px) 16px max(16px, env(safe-area-inset-bottom));
}

.tu-top { display: flex; flex: none; align-items: center; gap: 8px; }

.tu-pill {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-width: 0;
  height: 52px;
  padding: 0 12px 0 15px;
  border: 1px solid var(--border);
  border-radius: 15px;
  color: var(--text);
  background: var(--surface);
  cursor: pointer;
  text-align: left;
}

.tu-pill span { display: grid; gap: 2px; min-width: 0; }
.tu-pill b { font: 800 .98rem/1.1 "Manrope", "Inter", system-ui, sans-serif; }
.tu-pill small { overflow: hidden; color: var(--muted); font: 600 .7rem/1.2 "Inter", system-ui, sans-serif; text-overflow: ellipsis; white-space: nowrap; }
.tu-pill svg { flex: none; width: 18px; height: 18px; color: var(--muted); }

.tu-view {
  display: flex;
  flex: none;
  gap: 2px;
  height: 52px;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 15px;
  background: var(--tu-cell);
}

.tu-view button {
  padding: 0 13px;
  border: 0;
  border-radius: 11px;
  color: var(--muted);
  background: transparent;
  cursor: pointer;
  font: 700 .8rem/1 "Inter", system-ui, sans-serif;
}

.tu-view button[aria-pressed="true"] { color: var(--text); background: var(--surface); box-shadow: 0 1px 5px rgba(25, 25, 24, .1); }

@media (hover: hover) {
  .tu-pill:hover { border-color: var(--border-hover); }
}

/* Strings: one peg per string, lowest first. */
.tu-strings { display: flex; flex: none; justify-content: center; gap: 8px; }
.tu-strings[hidden] { display: none; }

.tu-string {
  position: relative;
  display: grid;
  flex: 1 1 0;
  max-width: 84px;
  min-width: 0;
  height: clamp(52px, 8vh, 70px);
  place-items: center;
  align-content: center;
  gap: 3px;
  padding: 0 4px;
  border: 1px solid var(--tu-line);
  border-radius: 14px;
  color: var(--text);
  background: var(--tu-cell);
  cursor: pointer;
  font: 800 1.12rem/1 "Manrope", "Inter", system-ui, sans-serif;
  touch-action: manipulation;
}

.tu-string small { color: var(--muted); font: 500 .62rem/1 "DM Mono", ui-monospace, monospace; }
.tu-string.is-near { border-color: color-mix(in srgb, var(--text) 40%, transparent); background: var(--surface); }
.tu-string.is-near.is-ok { border-color: var(--tu-ok); color: #fff; background: var(--tu-ok); }
.tu-string.is-near.is-ok small { color: rgba(255, 255, 255, .85); }
.tu-string.is-locked { box-shadow: 0 0 0 3px color-mix(in srgb, var(--ab-accent) 32%, transparent); }
.tu-string.is-tuned:not(.is-near) { border-color: color-mix(in srgb, var(--tu-ok) 55%, var(--tu-line)); }
.tu-string.is-tuned:not(.is-near) small::after { content: " ✓"; color: var(--tu-ok); }

@media (hover: hover) {
  .tu-string:hover { border-color: var(--border-hover); }
}

/* ---------- Readout: the dial with the note inside its arc ---------- */

.tu-readout { display: flex; flex: none; flex-direction: column; align-items: center; }

.tu-gauge {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: min(100%, calc(40vh * 1.8), 600px);
}

.tu-dial { width: 100%; }
.tu-dial svg { display: block; width: 100%; height: auto; overflow: visible; }

.tu-zone { fill: color-mix(in srgb, var(--tu-ok) 20%, transparent); stroke: none; }
.tu-tick { stroke: var(--tu-dim); stroke-width: 2; stroke-linecap: round; }
.tu-tick.is-major { stroke: var(--text-secondary); stroke-width: 3; }
.tu-tick.is-zero { stroke: var(--text); stroke-width: 3.6; }
.tu-flat, .tu-sharp { fill: var(--muted); font: 700 28px "Inter", system-ui, sans-serif; }

.tu-needle line {
  stroke: var(--text);
  stroke-width: 6;
  stroke-linecap: round;
  transition: stroke .2s;
}

.tu-needle circle { fill: var(--text); transition: fill .2s; }

.tu-readout[data-state="idle"] .tu-needle line,
.tu-readout[data-state="hold"] .tu-needle line { stroke: var(--tu-dim); }
.tu-readout[data-state="idle"] .tu-needle circle,
.tu-readout[data-state="hold"] .tu-needle circle { fill: var(--tu-dim); }
.tu-readout[data-state="ok"] .tu-needle line { stroke: var(--tu-ok); }
.tu-readout[data-state="ok"] .tu-needle circle { fill: var(--tu-ok); filter: drop-shadow(0 0 8px color-mix(in srgb, var(--tu-ok) 60%, transparent)); }
.tu-readout[data-state="off"] .tu-needle line { stroke: var(--tu-off); }
.tu-readout[data-state="off"] .tu-needle circle { fill: var(--tu-off); }

/* The note sits in the arc's hollow on the needle view, and above the
   strobe on the strobe view. */
.tu-centre {
  position: absolute;
  bottom: 0;
  left: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  transform: translateX(-50%);
  pointer-events: none;
}

.tu-readout[data-view="strobe"] .tu-centre { position: static; order: -1; margin-bottom: 10px; transform: none; }

.tu-note {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  color: var(--text);
  font: 800 clamp(4.2rem, 10.5vh, 7.4rem)/.92 "Manrope", "Inter", system-ui, sans-serif;
  letter-spacing: -.05em;
  transition: color .2s;
}

.tu-note-acc { margin-top: .04em; font-size: .46em; letter-spacing: 0; }
.tu-note-oct { align-self: flex-end; margin: 0 0 .06em .04em; color: var(--muted); font-size: .3em; letter-spacing: 0; }
.tu-note-acc:empty, .tu-note-oct:empty { display: none; }

.tu-readout[data-state="idle"] .tu-note,
.tu-readout[data-state="hold"] .tu-note { color: var(--tu-dim); }
.tu-readout[data-state="ok"] .tu-note { color: var(--tu-ok); }

.tu-cents {
  margin: 4px 0 0;
  color: var(--text);
  font: 700 1.2rem/1.1 "Manrope", "Inter", system-ui, sans-serif;
  font-variant-numeric: tabular-nums;
}

.tu-cents small { color: var(--muted); font-size: .66em; font-weight: 600; }
.tu-readout[data-state="idle"] .tu-cents { visibility: hidden; }
.tu-readout[data-state="ok"] .tu-cents { color: var(--tu-ok); }
.tu-readout[data-state="off"] .tu-cents { color: var(--tu-off); }

.tu-strobe {
  width: 100%;
  overflow: hidden;
  border: 1px solid var(--tu-line);
  border-radius: 16px;
  background: var(--tu-cell);
}

.tu-strobe canvas { display: block; width: 100%; height: clamp(96px, 16vh, 170px); }

.tu-hint {
  min-height: 1.4em;
  margin: 10px 0 0;
  color: var(--text-secondary);
  font: 700 .95rem/1.4 "Inter", system-ui, sans-serif;
  text-align: center;
}

.tu-readout[data-state="ok"] .tu-hint { color: var(--tu-ok); }
.tu-readout[data-state="off"] .tu-hint,
.tu-hint.is-error { color: var(--tu-off); }

.tu-detail {
  min-height: 1.3em;
  margin: 3px 0 0;
  color: var(--muted);
  font: 500 .8rem/1.4 "DM Mono", ui-monospace, monospace;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.tu-detail.is-error { max-width: 420px; color: var(--text-secondary); font-family: "Inter", system-ui, sans-serif; }

/* The pitch trace: the last ten seconds, a semitone to a line, in whatever
   height the rest leaves. */
.tu-trace {
  flex: 1 1 0;
  min-height: 0;
  max-height: 340px;
  overflow: hidden;
  border: 1px solid var(--tu-line);
  border-radius: 16px;
  background: var(--tu-cell);
}

.tu-trace[hidden] { display: none; }
.tu-trace canvas { display: block; width: 100%; height: 100%; }
.tu-stage:has(.tu-trace[hidden]) .tu-foot { margin-top: auto; }

/* ---------- Foot: Tone and Start ---------- */

.tu-foot { display: flex; flex: none; flex-direction: column; align-items: center; gap: 12px; }

.tu-level {
  position: relative;
  width: min(220px, 60%);
  height: 4px;
  border-radius: 9px;
  background: var(--tu-cell);
  box-shadow: inset 0 0 0 1px var(--tu-line);
}

.tu-level span { display: block; width: 0; height: 100%; border-radius: inherit; background: var(--tu-dim); }
.tu-level.is-voiced span { background: var(--ab-accent); }
.tu-level i { position: absolute; top: -3px; width: 2px; height: 10px; border-radius: 1px; background: var(--text-secondary); }

.tu-actions { display: flex; align-items: center; gap: 12px; width: 100%; }

.tu-listen {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  gap: 10px;
  height: clamp(58px, 8.4vh, 72px);
  padding: 0 20px;
  border: 0;
  border-radius: 99px;
  color: var(--bg);
  background: var(--text);
  box-shadow: 0 12px 30px rgba(25, 25, 24, .2);
  cursor: pointer;
  font: 800 1.05rem/1 "Manrope", "Inter", system-ui, sans-serif;
  touch-action: manipulation;
}

.tu-listen svg { width: 22px; height: 22px; }
.tu-listen:disabled { opacity: .6; cursor: default; }
.tu-listen.is-on { border: 1px solid var(--border); color: var(--text); background: var(--surface); box-shadow: none; }
.tu-listen.is-on svg { color: var(--tu-off); }
.tu-listen:active { transform: scale(.98); }

.tu-tone {
  display: flex;
  flex: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  width: clamp(58px, 8.4vh, 72px);
  height: clamp(58px, 8.4vh, 72px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  color: var(--text);
  background: var(--surface);
  cursor: pointer;
}

.tu-tone svg { width: 20px; height: 20px; }
.tu-tone small { font: 700 .62rem/1 "Inter", system-ui, sans-serif; }
.tu-tone.is-on { border-color: var(--ab-accent); color: #fff; background: var(--ab-accent); }

@media (hover: hover) {
  .tu-tone:hover { border-color: var(--border-hover); }
  .tu-listen:hover:not(:disabled) { transform: translateY(-1px); }
}

/* ---------- Inside the sheets ---------- */

.tu-label { display: block; margin: 0 0 8px; color: var(--text-secondary); font: 650 .76rem/1.3 "Inter", system-ui, sans-serif; }
.tu-gap { margin-top: 18px; }
.tu-help { margin: 10px 0 0; color: var(--muted); font-size: .74rem; line-height: 1.5; }

.tu-grid3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }

.tu-chip {
  height: 46px;
  min-width: 0;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 13px;
  color: var(--text-secondary);
  background: var(--surface);
  cursor: pointer;
  font: 700 .82rem/1.15 "Inter", system-ui, sans-serif;
}

.tu-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.tu-chips .tu-chip { height: 38px; padding: 0 12px; font-variant-numeric: tabular-nums; }

.tu-chip[aria-pressed="true"],
.tu-tuning[aria-pressed="true"],
.tu-seg button[aria-pressed="true"] {
  border-color: color-mix(in srgb, var(--ab-accent) 60%, var(--border));
  color: var(--text);
  background: color-mix(in srgb, var(--ab-accent) 13%, var(--surface));
}

.tu-tunings { margin-top: 20px; }
.tu-tunings[hidden] { display: none; }
.tu-tuning-list { display: grid; gap: 6px; }

.tu-tuning {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 48px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 13px;
  color: var(--text);
  background: var(--surface);
  cursor: pointer;
  text-align: left;
}

.tu-tuning b { font: 700 .86rem/1.2 "Inter", system-ui, sans-serif; }
.tu-tuning span { color: var(--muted); font: 500 .74rem "DM Mono", ui-monospace, monospace; white-space: nowrap; }

.tu-seg { display: grid; grid-auto-columns: minmax(0, 1fr); grid-auto-flow: column; gap: 6px; }

.tu-seg button {
  height: 44px;
  min-width: 0;
  padding: 0 6px;
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text-secondary);
  background: var(--surface);
  cursor: pointer;
  font: 700 .82rem/1 "Inter", system-ui, sans-serif;
}

/* The reference tone's twelve notes, laid out like the keys they are. */
.tu-keys { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 6px; }

.tu-key {
  height: 46px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  background: var(--surface);
  cursor: pointer;
  font: 800 .9rem/1 "Manrope", "Inter", system-ui, sans-serif;
}

.tu-key.is-black { color: var(--text-secondary); background: var(--tu-cell); }
.tu-key[aria-pressed="true"] { border-color: var(--ab-accent); color: #fff; background: var(--ab-accent); }

.tu-tone-row { display: flex; gap: 10px; margin: 14px 0 18px; }

.tu-stepper {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
}

.tu-stepper > span { display: grid; justify-items: center; gap: 2px; }
.tu-stepper b { font: 800 1.15rem/1 "Manrope", "Inter", system-ui, sans-serif; font-variant-numeric: tabular-nums; }
.tu-stepper small { color: var(--muted); font: 500 .66rem "DM Mono", ui-monospace, monospace; }
.tu-a4 > span { display: block; font: 600 .95rem "Inter", system-ui, sans-serif; }

.tu-mini {
  display: grid;
  width: 44px;
  height: 44px;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 11px;
  color: var(--text);
  background: var(--tu-cell);
  cursor: pointer;
  font: 700 1.2rem/1 "Inter", system-ui, sans-serif;
}

.tu-mini:disabled { opacity: .35; cursor: default; }

.tu-drone {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 118px;
  padding: 0 18px;
  border: 0;
  border-radius: 14px;
  color: var(--bg);
  background: var(--text);
  cursor: pointer;
  font: 800 .95rem/1 "Manrope", "Inter", system-ui, sans-serif;
}

.tu-drone svg { width: 16px; height: 16px; }
.tu-drone .tu-ico-stop,
.tu-drone[aria-pressed="true"] .tu-ico-play { display: none; }
.tu-drone[aria-pressed="true"] .tu-ico-stop { display: block; }
.tu-drone[aria-pressed="true"] { color: #fff; background: var(--ab-accent); }

.tu-range { margin-top: 18px; }

.tu-switch {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin: 0 0 16px;
  color: var(--text);
  cursor: pointer;
  font-size: .86rem;
  font-weight: 600;
  line-height: 1.35;
}

.qp-sec .tu-switch { margin: 0; padding: 9px 0; }
.qp-sec .tu-switch.tu-gap { margin-top: 10px; }
.tu-switch span { display: grid; gap: 2px; min-width: 0; }
.tu-switch small { color: var(--muted); font-size: .74rem; font-weight: 500; }

.tu-switch input {
  position: relative;
  flex: none;
  width: 46px;
  height: 28px;
  margin: 0;
  border: 0;
  border-radius: 99px;
  cursor: pointer;
  transition: background-color .15s;
  -webkit-appearance: none;
  appearance: none;
}

.tu-switch input::after {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .25);
  content: "";
  transition: transform .15s;
}

body.__accessbox-tool .tu-switch input,
html[data-theme="dark"] body.__accessbox-tool .tu-switch input { background-color: color-mix(in srgb, var(--text) 18%, transparent); }
body.__accessbox-tool .tu-switch input:checked,
html[data-theme="dark"] body.__accessbox-tool .tu-switch input:checked { background-color: var(--ab-accent); }
.tu-switch input:checked::after { transform: translateX(18px); }

.tu-theme-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding-top: 12px;
  font-size: .86rem;
  font-weight: 600;
}

@media (hover: hover) {
  .tu-chip:hover, .tu-tuning:hover, .tu-seg button:hover, .tu-key:hover { border-color: var(--border-hover); }
  .tu-mini:hover:not(:disabled) { background: color-mix(in srgb, var(--text) 10%, var(--surface)); }
}

/* ---------- Sizes ---------- */

/* Short windows: the trace goes first. */
@media (max-height: 660px) {
  .tu-trace { display: none; }
  .tu-foot { margin-top: auto; }
}

@media (max-width: 400px) {
  .tu-strings { gap: 5px; }
  .tu-string { border-radius: 12px; font-size: 1rem; }
  .tu-view button { padding: 0 10px; }
}

/* A phone on its side: the dial on the left; strings, words and Start on the right. */
@media (orientation: landscape) and (max-height: 540px) {
  .tu-main { min-height: 0; }
  .tu-stage {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-areas: "readout top" "readout strings" "readout trace" "readout foot";
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    column-gap: 28px;
    width: min(100%, 940px);
    padding-top: 10px;
  }

  .tu-top { grid-area: top; }
  .tu-strings { grid-area: strings; }
  .tu-readout { grid-area: readout; align-self: center; }
  .tu-gauge { width: min(100%, calc(62vh * 1.8)); }
  .tu-trace { display: block; grid-area: trace; max-height: none; }
  .tu-foot { grid-area: foot; margin: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .tu-note, .tu-needle line, .tu-needle circle, .tu-switch input, .tu-switch input::after { transition: none; }
}
