:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #efede8;
  --text: #1c1b19;
  --muted: #6b6862;
  --border: #dcd9d2;
  --accent: #2f6fdf;
  --accent-soft: #d6e3fa;
  --accent-text: #ffffff;
  --chord: #e2833a;
  --chord-soft: #f7d9c1;
  --key-white: #ffffff;
  --key-black: #22211f;
  --key-border: #b9b5ad;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161615;
    --surface: #201f1d;
    --surface-2: #2a2926;
    --text: #ecebe7;
    --muted: #a19e97;
    --border: #3a3935;
    --accent: #6c9cf0;
    --accent-soft: #2b3d5e;
    --accent-text: #0e1420;
    --chord: #f0a060;
    --chord-soft: #5a3c25;
    --key-white: #e9e7e2;
    --key-black: #0c0c0b;
    --key-border: #55524c;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #161615;
  --surface: #201f1d;
  --surface-2: #2a2926;
  --text: #ecebe7;
  --muted: #a19e97;
  --border: #3a3935;
  --accent: #6c9cf0;
  --accent-soft: #2b3d5e;
  --accent-text: #0e1420;
  --chord: #f0a060;
  --chord-soft: #5a3c25;
  --key-white: #e9e7e2;
  --key-black: #0c0c0b;
  --key-border: #55524c;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[x-cloak] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

h1, h2, h3 { margin: 0; }

.app {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px;
}

/* Barra in alto */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}
.topbar h1 { font-size: 1.25rem; letter-spacing: -0.01em; }

.segmented {
  display: inline-flex;
  background: var(--surface-2);
  border-radius: 8px;
  padding: 3px;
}
.segmented button {
  border: 0;
  background: transparent;
  padding: 5px 12px;
  border-radius: 6px;
  font-size: 0.875rem;
  color: var(--muted);
}
.segmented button.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.12);
}

/* Layout */
.layout {
  display: grid;
  grid-template-columns: 230px minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.sidebar,
.main > section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

/* Barra laterale */
.sidebar { padding: 10px; position: sticky; top: 16px; }

.type-tabs {
  display: grid;
  gap: 2px;
  padding-bottom: 10px;
  margin-bottom: 10px;
  border-bottom: 1px solid var(--border);
}
.type-tabs button {
  border: 0;
  background: transparent;
  text-align: left;
  padding: 7px 10px;
  border-radius: 6px;
}
.type-tabs button:hover { background: var(--surface-2); }
.type-tabs button.active { background: var(--accent); color: var(--accent-text); }

.tonic-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
}
.tonic-list button {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  border: 1px solid transparent;
  background: transparent;
  padding: 6px 8px;
  border-radius: 6px;
  font-weight: 500;
}
.tonic-list button:hover { background: var(--surface-2); }
.tonic-list button.active { border-color: var(--accent); background: var(--accent-soft); }
.tonic-list .sig { font-size: 0.75rem; color: var(--muted); font-weight: 400; }

/* Area principale */
.main { display: grid; gap: 16px; }
.main > section { padding: 16px; }

.scale-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
}
.scale-head h2 { font-size: 1.6rem; letter-spacing: -0.01em; }
.scale-notes { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 0; }
.note-chip {
  background: var(--surface-2);
  padding: 3px 10px;
  border-radius: 999px;
  font-weight: 500;
}
.note-chip.tonic { background: var(--accent); color: var(--accent-text); }
.hint { margin: 8px 0 0; font-size: 0.85rem; color: var(--muted); }

.play {
  border: 0;
  background: var(--accent);
  color: var(--accent-text);
  padding: 9px 16px;
  border-radius: 8px;
  font-weight: 600;
}
.play:hover { filter: brightness(1.08); }

/* Accordi */
.chords-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.chords-head h3 { font-size: 1rem; }
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 0.875rem; color: var(--muted); cursor: pointer; }

.chord-row { display: grid; grid-template-columns: 82px minmax(0, 1fr); gap: 8px; align-items: center; margin-bottom: 8px; }
.row-label { font-size: 0.8rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }

.chord-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 6px;
}
.chord {
  display: grid;
  gap: 1px;
  justify-items: center;
  padding: 7px 4px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  min-width: 0;
}
.chord:hover { border-color: var(--chord); }
.chord.active { background: var(--chord-soft); border-color: var(--chord); }
.chord .roman { font-size: 0.72rem; color: var(--muted); font-family: Georgia, "Times New Roman", serif; }
.chord .chord-name { font-weight: 600; font-size: 0.95rem; line-height: 1.2; text-align: center; overflow-wrap: anywhere; }
.chord .chord-notes { font-size: 0.7rem; line-height: 1.25; color: var(--muted); text-align: center; }

.selected-info { margin: 10px 0 0; min-height: 1.4em; font-size: 0.9rem; display: flex; gap: 6px; align-items: baseline; flex-wrap: wrap; }
.selected-info.empty { visibility: hidden; }
.link { border: 0; background: none; color: var(--chord); padding: 0; font-weight: 600; }

/* Tastiera */
.keyboard {
  position: relative;
  width: 100%;
  user-select: none;
  touch-action: manipulation;
}
.key {
  position: absolute;
  top: 0;
  padding: 0 0 8px;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  border-radius: 0 0 6px 6px;
  transition: background-color 80ms, transform 80ms;
}
.key.white {
  height: 100%;
  background: var(--key-white);
  border: 1px solid var(--key-border);
  z-index: 1;
}
.key.black {
  height: 62%;
  background: var(--key-black);
  border: 1px solid var(--key-black);
  z-index: 2;
  color: #fff;
}
.key-label { font-size: clamp(0.55rem, 1.3vw, 0.8rem); font-weight: 600; pointer-events: none; }

.key.white.in-scale { background: var(--accent-soft); }
.key.black.in-scale { background: color-mix(in srgb, var(--accent) 55%, var(--key-black)); }
.key.tonic.white { background: var(--accent); color: var(--accent-text); }
.key.tonic.black { background: var(--accent); color: var(--accent-text); }

.key.in-chord { background: var(--chord) !important; color: #1c1b19; }
.key.chord-root { box-shadow: inset 0 -5px 0 rgb(0 0 0 / 0.3); }

.key.pressed { transform: translateY(2px); filter: brightness(0.9); }

.legend {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  padding: 0;
  margin: 12px 0 0;
  font-size: 0.8rem;
  color: var(--muted);
}
.legend li { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 14px; height: 14px; border-radius: 3px; border: 1px solid var(--key-border); }
.swatch.tonic { background: var(--accent); }
.swatch.in-scale { background: var(--accent-soft); }
.swatch.in-chord { background: var(--chord); }

/* Schermi stretti */
@media (max-width: 760px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; }
  .type-tabs { grid-template-columns: 1fr 1fr; }
  .tonic-list { grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); }
  .chord-row { grid-template-columns: minmax(0, 1fr); }
  .chord-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .scale-head h2 { font-size: 1.3rem; }
}

/* MIDI */
:root { --held: #2e9e6a; --held-soft: #d3efe1; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { --held: #4cc48b; --held-soft: #1f4233; }
}
:root[data-theme="dark"] { --held: #4cc48b; --held-soft: #1f4233; }

.midi-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
  min-height: 34px;
}
.midi-connect { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.midi-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 0.875rem;
}
.midi-button:not(:disabled):hover { border-color: var(--held); }
.midi-button:disabled { cursor: default; }
.midi-button .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.midi-button.on .dot { background: var(--held); }
.midi-button.error .dot,
.midi-button.unsupported .dot { background: #d14b3a; }

.recognized { margin: 0; font-weight: 600; color: var(--held); }
.recognized.miss { color: var(--muted); font-weight: 500; }

.chord.played { border-color: var(--held); box-shadow: 0 0 0 2px var(--held); }
.key.held { background: var(--held) !important; color: #fff; }
.swatch.held { background: var(--held); }

/* Esercizi */
:root { --wrong: #d14b3a; --wrong-soft: #f8d9d4; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { --wrong: #f07a6a; --wrong-soft: #4a2420; }
}
:root[data-theme="dark"] { --wrong: #f07a6a; --wrong-soft: #4a2420; }

.topbar { flex-wrap: wrap; }
.topbar h1 { margin-right: auto; }

.options { display: grid; gap: 12px; }
.options fieldset { border: 0; margin: 0; padding: 0; display: grid; gap: 4px; }
.options legend {
  padding: 0 0 4px;
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.options label { display: flex; align-items: center; gap: 8px; font-size: 0.9rem; cursor: pointer; }
.options label:has(input:disabled) { cursor: default; opacity: 0.6; }
.sub-options { display: grid; gap: 4px; padding-left: 24px; }

.question { text-align: center; }
.question-key { margin: 0; color: var(--muted); font-size: 0.9rem; }
.question-prompt {
  margin: 4px 0 0;
  font-size: clamp(2rem, 6vw, 3rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.1;
}
.question-inversion { margin: 4px 0 0; font-size: 1.05rem; font-weight: 600; color: var(--accent); }

.choices {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  max-width: 560px;
  margin: 16px auto 0;
}
.choice {
  padding: 12px 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  font-size: 1.1rem;
  font-weight: 600;
}
.choice:hover { border-color: var(--accent); }
.choice.wrong { border-color: var(--wrong); background: var(--wrong-soft); color: var(--muted); cursor: default; }
.choice.right { border-color: var(--held); background: var(--held-soft); }

.midi-needed { margin: 12px 0 0; font-size: 0.9rem; color: var(--muted); }

.feedback { margin: 12px 0 0; min-height: 1.5em; font-weight: 600; }
.feedback.correct { color: var(--held); }
.feedback.wrong { color: var(--wrong); }

.question-actions { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.secondary {
  border: 1px solid var(--border);
  background: var(--surface);
  padding: 7px 14px;
  border-radius: 8px;
  font-size: 0.875rem;
}
.secondary:not(:disabled):hover { border-color: var(--accent); }
.secondary:disabled { opacity: 0.5; cursor: default; }

.key.wrong { background: var(--wrong) !important; }

@media (max-width: 760px) {
  .choices { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* Suona la scala */
.scale-progress {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px;
  max-width: 720px;
  margin: 16px auto 0;
  padding: 0;
}
.scale-progress li {
  min-width: 2.3em;
  padding: 3px 6px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--muted);
}
.scale-progress li.done { background: var(--held-soft); border-color: var(--held); color: var(--text); }
.scale-progress li.current { border-color: var(--accent); color: var(--text); box-shadow: 0 0 0 2px var(--accent-soft); }
.scale-progress li.wrong { border-color: var(--wrong); background: var(--wrong-soft); }
