:root {
  color-scheme: light;
  --bg: #f4f1ea;
  --surface: #ffffff;
  --ink: #1f2430;
  --ink-soft: #5b6270;
  --line: #26293a;
  --accent: #2563eb;
  --accent-soft: #dbe6fd;
  --good: #16a34a;
  --good-bg: #dcfce7;
  --bad: #dc2626;
  --bad-bg: #fee2e2;
  --border: #e3ded1;
  --key-white: #fdfdfb;
  --key-white-border: #b8b2a2;
  --key-black: #232323;
  --radius: 14px;
  font-size: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, "Segoe UI", Roboto, "Noto Sans", Arial, sans-serif;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

button {
  font-family: inherit;
  cursor: pointer;
}

/* ---------- App shell ---------- */

.app-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}

.tabs {
  display: flex;
  gap: 5px;
  flex: 1;
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }

.tab-btn {
  flex: 1 0 auto;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--ink-soft);
  border-radius: 999px;
  padding: 9px 12px;
  font-size: 0.82rem;
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.tab-btn.active {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}

.icon-btn {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 10px;
  width: 40px;
  height: 40px;
  font-size: 1.15rem;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink);
  flex: 0 0 auto;
}

.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.view { display: none; flex: 1; min-height: 0; flex-direction: column; }
.view.active { display: flex; }

/* ---------- Tryb 1: nauka nut ---------- */

#view-learn {
  padding: 10px 12px 16px;
  gap: 14px;
  align-items: stretch;
}

.learn-topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.score-pill {
  display: flex;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 14px;
  font-weight: 600;
}
.score-pill .good { color: var(--good); }
.score-pill .bad { color: var(--bad); }

.clef-indicator {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 14px;
  font-weight: 600;
}

.staff-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 190px;
}

.staff-card svg { width: 100%; height: auto; max-width: 520px; }

.feedback-line {
  min-height: 1.6em;
  text-align: center;
  font-weight: 700;
  font-size: 1.05rem;
}
.feedback-line.correct { color: var(--good); }
.feedback-line.incorrect { color: var(--bad); }

.answer-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 0 0 auto;
}

.accidental-row {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex: 0 0 auto;
}

.accidental-btn {
  border: 2px solid var(--border);
  background: var(--surface);
  border-radius: 14px;
  padding: 14px 28px;
  font-size: 1.9rem;
  font-weight: 700;
  color: var(--ink-soft);
  min-width: 88px;
}
.accidental-btn.selected {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
}

.letters-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 8px;
  flex: 0 0 auto;
}

.letter-btn {
  border: 2px solid var(--border);
  background: var(--surface);
  border-radius: 14px;
  width: 100%;
  padding: 20px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--ink);
  transition: transform 0.08s;
}
.letter-btn:active { transform: scale(0.94); }

.letter-btn.flash-correct { background: var(--good-bg); border-color: var(--good); color: var(--good); }
.letter-btn.flash-incorrect { background: var(--bad-bg); border-color: var(--bad); color: var(--bad); }

/* ---------- Tryb 2: klawiatura ---------- */

#view-keyboard {
  padding: 6px 8px 6px;
  gap: 5px;
}

.kb-topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--ink-soft);
  flex: 0 0 auto;
  min-height: 1.3em;
}

.kb-topbar .score-pill { flex: 0 0 auto; padding: 4px 12px; }

.grand-staff-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2px 6px;
  flex: 1 1 auto;
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.grand-staff-card svg { width: 100%; height: 100%; }

.kb-feedback.correct { color: var(--good); }
.kb-feedback.incorrect { color: var(--bad); }

.piano-zoom-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
  font-size: 0.75rem;
  color: var(--ink-soft);
  line-height: 1;
}
.piano-zoom-row input[type="range"] { flex: 1; }

.piano-wrap {
  overflow-x: auto;
  overflow-y: hidden;
  flex: 0 0 auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #dcd6c6;
  padding: 4px 0 6px;
  display: flex;
  justify-content: center;
}

.piano-inner {
  position: relative;
  height: clamp(68px, 22vh, 150px);
  flex: 0 0 auto;
  margin: 0 auto;
}

.key {
  position: absolute;
  top: 0;
  cursor: pointer;
  user-select: none;
  border-radius: 0 0 6px 6px;
  transition: filter 0.05s;
}

.key-white {
  height: 100%;
  background: linear-gradient(to bottom, var(--key-white), #efece2);
  border: 1px solid var(--key-white-border);
  border-top: none;
  z-index: 1;
}

.key-black {
  height: 62%;
  background: linear-gradient(to bottom, #3a3a3a, var(--key-black));
  border: 1px solid #000;
  z-index: 2;
}

.key-white:active, .key-white.key-pressed { background: #e4ddc8; }
.key-black:active, .key-black.key-pressed { background: #555; }

.key-label {
  position: absolute;
  bottom: 6px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.62rem;
  color: var(--ink-soft);
  font-weight: 600;
  pointer-events: none;
}

.key-correct {
  background: linear-gradient(to bottom, #34d058, var(--good)) !important;
  box-shadow: inset 0 0 0 3px #ffffffaa;
}
.key-incorrect {
  background: linear-gradient(to bottom, #f87171, var(--bad)) !important;
  box-shadow: inset 0 0 0 3px #ffffffaa;
}
.key-target {
  background: linear-gradient(to bottom, #4d8dff, var(--accent)) !important;
  box-shadow: inset 0 0 0 3px #ffffffaa;
  animation: key-target-pulse 0.9s ease-in-out infinite;
}
@keyframes key-target-pulse {
  0%, 100% { filter: brightness(1); }
  50% { filter: brightness(1.35); }
}

.key-colored {
  background: linear-gradient(to bottom, color-mix(in srgb, var(--key-color) 82%, white), var(--key-color)) !important;
  box-shadow: inset 0 0 0 3px #ffffffcc;
}

.kb-range-row {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
  flex: 0 0 auto;
}

.chip-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--ink-soft);
}
.chip-btn.active {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}

/* ---------- Ustawienia (panel/modal) ---------- */

.settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 20, 25, 0.45);
  display: none;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
}
.settings-overlay.open { display: flex; }

.settings-panel {
  background: var(--surface);
  width: 100%;
  max-width: 560px;
  border-radius: 18px 18px 0 0;
  padding: 18px 18px 26px;
  max-height: 85vh;
  overflow-y: auto;
}

.settings-panel h2 {
  margin: 0 0 14px;
  font-size: 1.1rem;
}

.settings-group {
  margin-bottom: 18px;
}

.settings-group label.group-title {
  display: block;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--ink-soft);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 8px;
}

.option-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.option-btn {
  border: 2px solid var(--border);
  background: var(--bg);
  border-radius: 10px;
  padding: 9px 14px;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--ink);
}
.option-btn.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0;
}

.switch {
  position: relative;
  width: 46px;
  height: 26px;
  flex: 0 0 auto;
}
.switch input { opacity: 0; width: 0; height: 0; }
.switch .track {
  position: absolute; inset: 0;
  background: var(--border);
  border-radius: 999px;
  transition: background 0.15s;
}
.switch .thumb {
  position: absolute;
  top: 3px; left: 3px;
  width: 20px; height: 20px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.15s;
  box-shadow: 0 1px 2px rgba(0,0,0,0.3);
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track + .thumb { transform: translateX(20px); }

.settings-close-btn {
  width: 100%;
  margin-top: 6px;
  border: none;
  background: var(--ink);
  color: #fff;
  border-radius: 12px;
  padding: 13px;
  font-size: 1rem;
  font-weight: 700;
}

/* ---------- SVG pięciolinia (współdzielone) ---------- */

.staff-line { stroke: var(--line); stroke-width: 1.6; }
.ledger-line { stroke: var(--line); stroke-width: 1.6; }
.barline { stroke: var(--line); stroke-width: 1.4; opacity: 0.55; }
.measure-number { fill: var(--ink-soft); font-family: -apple-system, "Segoe UI", Roboto, sans-serif; }
.staff-clef { fill: var(--line); }
.staff-accidental {
  fill: var(--note-color, var(--line));
  font-family: "Segoe UI Symbol", "Noto Music", "Apple Symbols", sans-serif;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
.notehead { fill: var(--note-color, var(--line)); }
.stem { stroke: var(--note-color, var(--line)); stroke-width: 1.6; }
.note-focus-box { fill: none; stroke: var(--accent); stroke-width: 2.4; }

.staff-note.state-correct .notehead { fill: var(--good); }
.staff-note.state-correct .note-focus-box { stroke: var(--good); }
.staff-note.state-incorrect .notehead { fill: var(--bad); }
.staff-note.state-incorrect .note-focus-box { stroke: var(--bad); }
.staff-note.dimmed { opacity: 0.32; }
.staff-note.passed { opacity: 0.08; }
.staff-note { transition: opacity 0.6s ease; }

/* ---------- Tryb 3: Utwory ---------- */

#view-songs {
  padding: 10px 12px 16px;
  gap: 12px;
  align-items: stretch;
}

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

.song-select {
  flex: 1 1 auto;
  min-width: 0;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--ink);
}

.songs-meta {
  font-size: 0.76rem;
  color: var(--ink-soft);
  line-height: 1.35;
}

.songs-staff-card {
  padding: 0;
  overflow: hidden;
  flex: 1 1 auto;
  min-height: 150px;
  max-height: 280px;
}

.songs-scroll {
  overflow-x: auto;
  overflow-y: hidden;
  width: 100%;
  height: 100%;
  padding: 6px 4px;
}

.songs-scroll svg,
#songs-staff { width: auto !important; max-width: none !important; height: 100% !important; display: block; }

/* ---------- Tryb 4: Akordy ---------- */

#view-chords {
  padding: 10px 12px 16px;
  gap: 12px;
  align-items: stretch;
}

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

.chords-mode-toggle {
  display: flex;
  gap: 8px;
  flex: 1;
}
.chords-mode-toggle .chip-btn { flex: 1; text-align: center; padding: 9px 6px; }

.chords-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chord-answer-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.chord-answer-btn {
  border: 2px solid var(--border);
  background: var(--surface);
  border-radius: 12px;
  padding: 14px 6px;
  font-size: 1rem;
  font-weight: 700;
  color: var(--ink);
}
.chord-answer-btn.flash-correct { background: var(--good-bg); border-color: var(--good); color: var(--good); }
.chord-answer-btn.flash-incorrect { background: var(--bad-bg); border-color: var(--bad); color: var(--bad); }

.chord-picker {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.chord-root-row, .chord-quality-row, .chord-bass-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.chord-root-btn, .chord-quality-btn {
  border: 2px solid var(--border);
  background: var(--surface);
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--ink);
}
.chord-root-btn.selected, .chord-quality-btn.selected {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
}

.chord-check-btn { flex: 0 0 auto; }

.chords-score-pill {
  align-self: center;
  padding: 6px 16px;
}

/* ---------- Orientation hints ---------- */

.orientation-hint {
  font-size: 0.7rem;
  color: var(--ink-soft);
  text-align: center;
  padding: 2px 0 0;
  flex: 0 0 auto;
}

@media (orientation: landscape), (max-height: 430px) {
  .orientation-hint { display: none; }
}

@media (min-width: 720px) {
  .answer-panel { max-width: 640px; margin: 0 auto; width: 100%; }
  .staff-card svg { max-width: 640px; }
}
