:root {
  --bg: #0e0f11;
  --surface: #16181b;
  --surface-2: #1e2125;
  --line: #2a2e34;
  --text: #f3f2ef;
  --muted: #8d9299;
  --faint: #5a5f66;
  --accent: #ff5a1f;
  --accent-soft: rgba(255, 90, 31, 0.16);
  --ok: #7fd4a6;
  --danger: #ff6b6b;
  /* Colori delle sezioni (A, B, C…), in ordine fisso: tavolozza categorica validata sul fondo
     scuro (separazione per daltonici ΔE ≥ 8,4 tra vicini, contrasto ≥ 3:1). */
  --s1: #3987e5;
  --s2: #d95926;
  --s3: #199e70;
  --s4: #c98500;
  --s5: #d55181;
  --s6: #008300;
  --s7: #9085e9;
  --s8: #e66767;
  --radius: 18px;
  --num: ui-rounded, "SF Pro Rounded", -apple-system, system-ui, sans-serif;
  --sans: -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100dvh;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; }

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(env(safe-area-inset-bottom) + 24px);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---------- Barra superiore ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.seg {
  display: inline-flex;
  padding: 3px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
}

.seg-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 8px 16px;
  border-radius: 9px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.seg-btn[aria-pressed="true"] {
  background: var(--surface-2);
  color: var(--text);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.04) inset;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
}

.icon-btn[aria-expanded="true"] { color: var(--accent); border-color: var(--accent-soft); }

/* ---------- Area principale ---------- */

.pad {
  position: relative;
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 20px;
  min-height: min(64dvh, 540px);
  padding: 28px 16px 24px;
  border-radius: 28px;
  background: var(--surface);
  border: 1px solid var(--line);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

.pad[data-mode="screen"] { cursor: pointer; }
.pad[data-mode="screen"]:active { background: var(--surface-2); }

/* Quadrante: anello del salvataggio, impulso del tap e lettura. */
.dial {
  position: relative;
  display: grid;
  place-items: center;
  width: min(74vw, 300px);
  aspect-ratio: 1;
}

.timer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
  pointer-events: none;
}

.timer circle { fill: none; stroke-width: 2.5; }
.timer-track { stroke: var(--line); }
.timer-arc {
  stroke: var(--accent);
  stroke-linecap: round;
  stroke-dasharray: 1 1;
  stroke-dashoffset: 1;
  opacity: 0;
  transition: opacity 0.2s;
}
.pad[data-state="locked"] .timer-arc { stroke: var(--ok); stroke-dashoffset: 0; opacity: 0.6; }

.pulse {
  position: absolute;
  inset: 10%;
  border-radius: 50%;
  background: radial-gradient(circle, var(--accent-soft) 0%, transparent 70%);
  transform: scale(0.85);
  opacity: 0;
  pointer-events: none;
}

/* Tap scartato (fuori tempo o doppio): stesso impulso, ma neutro. */
.pulse.rejected { background: radial-gradient(circle, rgba(141, 146, 153, 0.2) 0%, transparent 70%); }

.readout {
  position: relative;
  display: grid;
  justify-items: center;
  gap: 2px;
}

.bpm {
  font-family: var(--num);
  font-weight: 600;
  font-size: clamp(64px, 22vw, 96px);
  line-height: 0.95;
  letter-spacing: -0.04em;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  transition: color 0.2s;
}

.bpm-dec {
  font-size: 0.4em;
  letter-spacing: -0.02em;
  color: var(--muted);
}

.pad[data-state="idle"] .bpm { color: var(--faint); }
.pad[data-state="rough"] .bpm { color: var(--muted); }

.unit {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.18em;
  color: var(--muted);
}

.meta {
  display: flex;
  gap: 12px;
  min-height: 22px;
  margin-top: 8px;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.plusminus:empty, .taps:empty { display: none; }

.quality {
  position: relative;
  display: grid;
  justify-items: center;
  gap: 8px;
  width: min(62vw, 220px);
}

.quality-track {
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: var(--line);
  overflow: hidden;
}

.quality-fill {
  height: 100%;
  width: 100%;
  border-radius: 2px;
  background: var(--accent);
  transform: scaleX(0);
  transform-origin: left center;
  transition: transform 0.25s ease-out, background 0.2s;
}

.pad[data-state="locked"] .quality-fill { background: var(--ok); }

.quality-label {
  font-size: 13px;
  color: var(--muted);
  min-height: 16px;
}

.status {
  position: relative;
  margin: 0;
  max-width: 30ch;
  text-align: center;
  font-size: 15px;
  line-height: 1.4;
  color: var(--muted);
}

.pad[data-state="locked"] .status { color: var(--ok); }

/* ---------- Pulsanti ---------- */

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr 2fr;
  gap: 8px;
}

.btn {
  min-height: 48px;
  padding: 0 14px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

.btn:disabled { color: var(--faint); cursor: default; }
.btn:not(:disabled):active { background: var(--surface-2); }

.btn-primary {
  width: 100%;
  border: 0;
  background: var(--accent);
  color: #1a0b04;
}

.link-btn {
  border: 0;
  background: none;
  padding: 8px 0;
  color: var(--muted);
  font-size: 14px;
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
}

/* ---------- Pannelli ---------- */

.panel {
  padding: 16px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--line);
}

.panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.panel h2 {
  margin: 0;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.hint {
  margin: 0;
  font-size: 13px;
  line-height: 1.45;
  color: var(--muted);
}

.scope {
  display: block;
  width: 100%;
  height: 96px;
  border-radius: 10px;
  background: var(--bg);
}

.slider {
  display: grid;
  grid-template-columns: auto 1fr 2ch;
  align-items: center;
  gap: 12px;
  margin: 14px 0 10px;
  font-size: 15px;
}

.slider input { width: 100%; accent-color: var(--accent); }

.last-hit {
  margin-bottom: 8px;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.slider output { font-variant-numeric: tabular-nums; text-align: right; color: var(--muted); }

/* ---------- Storico ---------- */

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.history-item {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 4px 12px;
  padding: 12px 0;
  border-top: 1px solid var(--line);
}

.history-item:first-child { border-top: 0; padding-top: 0; }

.history-bpm {
  font-family: var(--num);
  font-size: 26px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

.history-bpm small {
  display: block;
  margin-top: 2px;
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 400;
  letter-spacing: 0;
  color: var(--muted);
}

.history-name {
  grid-column: 1 / -1;
  width: 100%;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* evita lo zoom automatico di iOS */
}

.history-name::placeholder { color: var(--faint); }

.history-meta {
  grid-column: 1 / -1;
  font-size: 12px;
  color: var(--faint);
}

.history-tools { display: flex; gap: 6px; }

.chip {
  min-width: 40px;
  height: 34px;
  padding: 0 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.chip:active { background: var(--surface-2); }

/* ---------- Foglio iniziale ---------- */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  align-items: end;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 16px);
  background: rgba(6, 7, 8, 0.72);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}

.sheet[hidden] { display: none; }

.sheet-card {
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  padding: 24px 20px 16px;
  border-radius: 24px;
  background: var(--surface);
  border: 1px solid var(--line);
  display: grid;
  gap: 14px;
  justify-items: center;
}

.sheet-card h1 { margin: 0; font-size: 24px; letter-spacing: -0.01em; justify-self: start; }

.steps {
  margin: 0;
  padding-left: 20px;
  display: grid;
  gap: 8px;
  font-size: 16px;
  line-height: 1.45;
  justify-self: start;
}

.sheet-card .hint { justify-self: start; }
.sheet-card .btn-primary { margin-top: 4px; }

.error { margin: 0; font-size: 14px; color: var(--danger); text-align: center; }

/* Dal secondo avvio basta il pulsante: le istruzioni le hai già viste. */
.sheet.compact .steps,
.sheet.compact .sheet-card > .hint { display: none; }

@media (prefers-reduced-motion: reduce) {
  .quality-fill, .timer-arc, .bpm { transition: none; }
}

/* ---------- Ascolto ---------- */

.seg-btn { padding: 8px 12px; }

.key-line {
  display: grid;
  justify-items: center;
  gap: 2px;
  margin-top: 10px;
}

.key-line[hidden] { display: none; }

.key-name {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
}

.key-alt {
  font-size: 13px;
  color: var(--muted);
}

.key-alt:empty { display: none; }

.listen-controls {
  display: grid;
  justify-items: center;
  gap: 12px;
  width: min(100%, 300px);
}

.listen-controls[hidden] { display: none; }

.level {
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: var(--line);
  overflow: hidden;
}

.level-fill {
  width: 100%;
  height: 100%;
  background: var(--muted);
  transform: scaleX(0);
  transform-origin: left center;
  transition: transform 0.15s linear;
}

.pad[data-listening="true"] .level-fill { background: var(--ok); }

.history-key {
  grid-column: 1 / -1;
  font-size: 14px;
  color: var(--text);
}

.key-check {
  display: grid;
  gap: 10px;
  width: min(100%, 320px);
  text-align: center;
}

.key-check[hidden] { display: none; }

.key-check-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.key-check .btn[aria-pressed="true"] { border-color: var(--ok); color: var(--ok); }

.key-confidence { font-size: 13px; color: var(--muted); }

/* ---------- Metronomo e cadenze ---------- */

.metro-btn {
  margin-top: 10px;
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.metro-btn[hidden] { display: none; }
.metro-btn[aria-pressed="true"],
.chip[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }

.history-key {
  display: flex;
  align-items: center;
  gap: 8px;
}

.chip-small {
  min-width: 30px;
  height: 28px;
  padding: 0 8px;
  font-size: 12px;
}

.seg-btn[hidden],
.icon-btn[hidden] { display: none; }

.steps[hidden],
.sheet-card .hint[hidden] { display: none; }
.steps a { color: var(--accent); }
.steps code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 13px;
  padding: 1px 5px;
  border-radius: 6px;
  background: var(--surface-2);
  user-select: all;
  -webkit-user-select: all;
}

/* ---------- Accordi ---------- */

.chord-line {
  display: grid;
  justify-items: center;
  gap: 8px;
  margin: 4px 0 10px;
}

.chord-line[hidden] { display: none; }

.chord-label {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.chord-chips {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
}

.chord-chip {
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.chord-chip:active { border-color: var(--accent); }

.history-chords {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.history-chords .chord-chip { padding: 3px 10px; font-size: 13px; }

/* ---------- Computer: interfaccia a due colonne ----------
   Solo con mouse o trackpad e finestra larga (telefoni e iPad restano a una colonna). */

.desktop-only { display: none; }

.side {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

kbd {
  display: inline-block;
  min-width: 1.6em;
  padding: 1px 6px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
  font: 600 12px/1.5 var(--sans);
  text-align: center;
}

@media (min-width: 1000px) and (hover: hover) and (pointer: fine) {
  .desktop-only { display: flex; }

  .app {
    max-width: 1280px;
    min-height: 100dvh;
    display: grid;
    grid-template-columns: minmax(0, 1.55fr) minmax(360px, 1fr);
    grid-template-rows: auto 1fr auto auto;
    grid-template-areas:
      "top top"
      "stage side"
      "actions side"
      "structure side";
    gap: 20px 28px;
    padding: 24px 32px 32px;
  }

  .topbar { grid-area: top; gap: 20px; }

  .brand {
    align-items: center;
    gap: 10px;
    margin-right: auto;
    font-size: 19px;
    font-weight: 700;
    letter-spacing: -0.01em;
  }

  .brand img { border-radius: 8px; }

  .shortcuts {
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 14px;
    margin: 0 0 0 auto;
    font-size: 13px;
    color: var(--muted);
  }

  .shortcuts span { display: inline-flex; align-items: center; gap: 4px; }

  .pad {
    grid-area: stage;
    min-height: 0;
    padding: 40px 32px 32px;
    gap: 24px;
  }

  .dial { width: min(50vh, 460px); }
  .bpm { font-size: clamp(110px, 15vh, 156px); }
  .meta { font-size: 17px; }
  .key-name { font-size: 24px; }
  .chord-chip { padding: 8px 16px; font-size: 17px; }
  .status { max-width: 44ch; font-size: 16px; }
  .quality { width: 280px; }

  .listen-controls { width: min(100%, 420px); }

  .actions {
    grid-area: actions;
    grid-template-columns: 110px 110px 1fr;
  }

  .structure { grid-area: structure; padding: 20px 24px; }

  .side {
    grid-area: side;
    position: sticky;
    top: 24px;
    align-self: start;
    max-height: calc(100dvh - 48px);
    overflow-y: auto;
    scrollbar-width: thin;
  }

  .history .panel-head h2 { font-size: 15px; }
  .history-item { padding: 14px 0; }
  .history-bpm { font-size: 30px; }

  /* Il foglio iniziale diventa una finestra al centro. */
  .sheet { align-items: center; justify-items: center; }
  .sheet-card { max-width: 520px; }

  .pad[data-mode="screen"]:hover { border-color: var(--faint); }
}

/* ---------- File audio ---------- */

.drop-overlay {
  position: fixed;
  inset: 12px;
  z-index: 20;
  display: grid;
  place-items: center;
  border: 2px dashed var(--accent);
  border-radius: 28px;
  background: rgba(14, 15, 17, 0.88);
  pointer-events: none;
}

.drop-overlay[hidden] { display: none; }
.drop-overlay p { margin: 0; text-align: center; font-size: 22px; font-weight: 600; line-height: 1.5; }
.drop-overlay small { font-size: 15px; font-weight: 400; color: var(--muted); }

/* ---------- Struttura e giri ---------- */

.structure[hidden] { display: none; }

.timeline {
  display: flex;
  gap: 2px;
  width: 100%;
  min-height: 40px;
}

.tl-block {
  flex: 1 1 0;
  min-width: 6px;
  display: flex;
  align-items: flex-end;
  padding: 0 0 5px 6px;
  border: 0;
  border-radius: 4px;
  background: color-mix(in srgb, var(--c) 42%, var(--surface));
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  text-align: left;
  overflow: hidden;
  white-space: nowrap;
}

button.tl-block { cursor: pointer; }
button.tl-block:active { background: color-mix(in srgb, var(--c) 60%, var(--surface)); }
.tl-block.variant { background: repeating-linear-gradient(135deg, color-mix(in srgb, var(--c) 42%, var(--surface)) 0 5px, color-mix(in srgb, var(--c) 20%, var(--surface)) 5px 10px); }
.tl-label { overflow: hidden; text-overflow: clip; }

.whole {
  margin: 12px 0 0;
  font-size: 14px;
  line-height: 1.45;
  color: var(--muted);
}

.whole[hidden] { display: none; }
.whole b { color: var(--text); }

.groups {
  list-style: none;
  margin: 12px 0 10px;
  padding: 0;
  display: grid;
  gap: 2px;
}

.group {
  padding: 12px 0;
  border-top: 1px solid var(--line);
}

.group:first-child { border-top: 0; }

.group-head {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 28px;
}

.group-badge {
  display: grid;
  place-items: center;
  min-width: 24px;
  height: 24px;
  padding: 0 4px;
  border-radius: 6px;
  background: color-mix(in srgb, var(--c) 26%, var(--surface));
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
}

.group-title { font-size: 15px; font-weight: 600; }
.group-times { margin-right: auto; font-size: 13px; color: var(--muted); }

.prog {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.prog-chord {
  display: inline-flex;
  align-items: baseline;
  gap: 5px;
  padding: 5px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-size: 15px;
}

.prog-chord b { font-weight: 600; }
.prog-chord.unsure { border-style: dashed; color: var(--muted); }
.prog-roman { font-size: 12px; color: var(--muted); font-variant-caps: normal; }
.prog-bars {
  font-size: 11px;
  font-weight: 600;
  color: var(--faint);
}
.prog-more {
  align-self: center;
  padding: 4px 8px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

.prog-note {
  margin: 8px 0 0;
  font-size: 13px;
  line-height: 1.45;
  color: var(--muted);
}

.prog-named { color: var(--text); }

.prog-alt {
  padding: 0;
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  text-decoration: underline dotted;
  text-underline-offset: 3px;
}

button.prog-alt { cursor: pointer; }

.history-structure { grid-column: 1 / -1; }

.history-structure-summary {
  display: grid;
  gap: 6px;
  list-style: none;
  cursor: pointer;
}

.history-structure-summary::-webkit-details-marker { display: none; }
.history-structure-label { font-size: 13px; color: var(--muted); }
.history-structure-label::before { content: "▸ "; }
.history-structure[open] .history-structure-label::before { content: "▾ "; }
.timeline-small { min-height: 8px; }
.timeline-small .tl-block { min-width: 3px; padding: 0; border-top-width: 8px; border-radius: 2px; background: none; }
.history-structure .groups { margin-bottom: 0; }
.history-structure .prog-chord { padding: 3px 8px; font-size: 13px; }
