:root {
  --bg: #14130f;
  --panel: #1c1b16;
  --ink: #ece6d2;
  --muted: #8a8472;
  --line: #3a3528;
  --accent: #d2a857;
  --string: #b8b09a;
  --fret: #6b6450;
  --dot: #d2a857;
  --dot-ink: #14130f;
  --warn: #c97a4a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.45;
}

.bar {
  padding: 16px 24px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

footer.bar {
  border-bottom: none;
  border-top: 1px solid var(--line);
}

h1 {
  font-size: 18px;
  letter-spacing: 0.02em;
  font-weight: 600;
  margin: 0;
}

.controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

button {
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
}

button:hover {
  border-color: var(--accent);
}

#save-tuning-btn {
  background: var(--accent);
  color: var(--dot-ink);
  border-color: var(--accent);
}

#save-tuning-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#delete-tuning-btn {
  color: var(--warn);
  border-color: var(--warn);
}

.controls label {
  font-size: 13px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

select,
input[type="text"] {
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 14px;
  font-family: inherit;
}

#custom-tuning-input {
  width: 180px;
  font-variant-numeric: tabular-nums;
}

#custom-tuning-input.invalid {
  border-color: var(--warn);
}

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

.tabs {
  display: flex;
  gap: 4px;
  padding: 0 24px;
  border-bottom: 1px solid var(--line);
}
.tabs button {
  background: transparent;
  color: var(--muted);
  border: none;
  border-bottom: 2px solid transparent;
  padding: 12px 16px;
  font-family: inherit;
  font-size: 14px;
  cursor: pointer;
}
.tabs button.active {
  color: var(--ink);
  border-bottom-color: var(--accent);
}
.tabs button:hover:not(.active) {
  color: var(--ink);
}

[hidden] { display: none !important; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
  padding: 24px;
}

.songs-view {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 24px;
  padding: 24px;
  min-height: 60vh;
}

.songs-sidebar {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.new-song-btn {
  background: var(--accent);
  color: var(--dot-ink);
  border: none;
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
}

.songs-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.songs-list li {
  padding: 8px 12px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--panel);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}

.songs-list li:hover { border-color: var(--muted); }
.songs-list li.active {
  border-color: var(--accent);
  background: rgba(210, 168, 87, 0.08);
}

.local-note {
  font-size: 11px;
  color: var(--muted);
  line-height: 1.4;
  margin: 4px 0 0;
  padding-top: 8px;
  border-top: 1px solid var(--line);
}

.song-editor { min-width: 0; }
.song-editor .empty {
  color: var(--muted);
  padding: 24px;
  text-align: center;
}

.song-header {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 24px;
  flex-wrap: wrap;
}
.song-name-input {
  font-size: 20px;
  font-weight: 600;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 6px 10px;
  color: var(--ink);
  font-family: inherit;
  flex: 1;
  min-width: 200px;
}
.song-name-input:focus,
.song-name-input:hover { border-color: var(--line); outline: none; }

.delete-song-btn {
  color: var(--warn);
  border-color: var(--warn);
}

.detected-key {
  display: flex;
  align-items: center;
  gap: 8px;
  position: relative;
}
.key-label {
  font-size: 14px;
  color: var(--ink);
  padding: 6px 10px;
  background: var(--panel);
  border-radius: 6px;
  border: 1px solid var(--line);
}
.key-label.muted-key { color: var(--muted); }
.learn-btn {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
}
.learn-btn:hover { background: rgba(210, 168, 87, 0.08); }

.learn-popover {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 8px;
  background: var(--bg);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 16px 20px 16px 16px;
  z-index: 10;
  min-width: 300px;
  max-width: 400px;
  box-shadow: 0 6px 24px rgba(0,0,0,0.4);
}
.learn-popover h3 {
  margin: 0 0 8px;
  font-size: 14px;
  color: var(--accent);
  font-weight: 600;
}
.learn-popover p {
  margin: 8px 0;
  font-size: 13px;
  line-height: 1.5;
  color: var(--ink);
}
.learn-popover p.muted {
  color: var(--muted);
  font-size: 12px;
}
.learn-close {
  position: absolute;
  top: 6px;
  right: 6px;
  background: transparent;
  color: var(--muted);
  border: none;
  font-size: 20px;
  cursor: pointer;
  width: 24px;
  height: 24px;
  line-height: 1;
  padding: 0;
}
.learn-close:hover { color: var(--ink); }

.diatonic-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 16px;
  margin: 12px 0;
  padding: 12px;
  background: var(--panel);
  border-radius: 6px;
}
.diatonic-row {
  display: flex;
  gap: 8px;
  align-items: baseline;
  font-size: 13px;
  color: var(--muted);
}
.diatonic-row .numeral {
  font-style: italic;
  min-width: 32px;
  font-family: ui-monospace, monospace;
}
.diatonic-row .chord { font-weight: 500; }
.diatonic-row.in-song {
  color: var(--accent);
}
.diatonic-row.in-song .chord { font-weight: 600; }

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

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

.section-header {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
}
.section-name-input {
  font-size: 16px;
  font-weight: 600;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 4px;
  padding: 4px 8px;
  color: var(--ink);
  font-family: inherit;
  flex: 1;
}
.section-name-input:focus,
.section-name-input:hover { border-color: var(--line); outline: none; }

.delete-section-btn {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 12px;
  cursor: pointer;
}
.delete-section-btn:hover { color: var(--warn); border-color: var(--warn); }

.section-chords {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 110px));
  gap: 10px;
  align-items: stretch;
}

.chord-chip {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  width: 110px;
  position: relative;
  justify-content: flex-start;
}
.chord-chip .chip-name {
  font-size: 14px;
  font-weight: 600;
  text-align: center;
}
.chord-chip .diagram {
  width: 100%;
  height: auto;
}
.chord-chip .chip-meta {
  font-size: 10px;
  color: var(--muted);
  text-align: center;
  padding: 20px 4px;
}
.chord-chip .chip-remove {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 18px;
  height: 18px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 50%;
  color: var(--muted);
  cursor: pointer;
  font-size: 11px;
  line-height: 1;
  padding: 0;
  display: none;
}
.chord-chip:hover .chip-remove { display: block; }
.chord-chip .chip-remove:hover { color: var(--warn); border-color: var(--warn); }

.chord-chip .chip-duplicate {
  position: absolute;
  top: -6px;
  left: -6px;
  width: 18px;
  height: 18px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 50%;
  color: var(--muted);
  cursor: pointer;
  font-size: 11px;
  line-height: 16px;
  padding: 0;
  display: none;
  text-align: center;
}
.chord-chip:hover .chip-duplicate { display: block; }
.chord-chip .chip-duplicate:hover { color: var(--accent); border-color: var(--accent); }
.chord-chip.custom { border-color: var(--accent); }
.chord-chip {
  cursor: grab;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}
.chord-chip:active { cursor: grabbing; }
.chord-chip.dragging {
  opacity: 0.35;
  cursor: grabbing;
}
.chord-chip.editing {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
  background: rgba(210, 168, 87, 0.08);
}

.add-chord-btn {
  background: transparent;
  color: var(--accent);
  border: 1px dashed var(--accent);
  border-radius: 6px;
  padding: 8px 12px;
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  width: 110px;
  align-self: stretch;
}
.add-chord-btn:hover { background: rgba(210, 168, 87, 0.08); }

.add-section-btn {
  background: transparent;
  color: var(--muted);
  border: 1px dashed var(--line);
  border-radius: 8px;
  padding: 12px;
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
}
.add-section-btn:hover { color: var(--accent); border-color: var(--accent); }

.chord-picker {
  margin-top: 12px;
  padding: 12px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
}
.picker-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.picker-header .picker-title {
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.picker-buttons {
  display: flex;
  gap: 6px;
}
.picker-options {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(56px, 1fr));
  gap: 6px;
  margin-bottom: 8px;
}
.picker-option {
  padding: 8px 4px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  text-align: center;
  font-family: inherit;
  color: var(--ink);
}
.picker-option:hover { border-color: var(--muted); }
.picker-option.suggested {
  border-color: var(--accent);
  background: rgba(210, 168, 87, 0.08);
}
.picker-option.saved-voicing {
  border-color: var(--accent);
  position: relative;
  padding-right: 18px;
}
.picker-option.saved-voicing {
  padding-right: 32px;
}
.picker-option.saved-voicing .voicing-delete,
.picker-option.saved-voicing .voicing-edit {
  position: absolute;
  top: 2px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
  padding: 2px;
}
.picker-option.saved-voicing .voicing-delete { right: 4px; }
.picker-option.saved-voicing .voicing-edit { right: 18px; }
.picker-option.saved-voicing .voicing-delete:hover { color: var(--warn); }
.picker-option.saved-voicing .voicing-edit:hover { color: var(--accent); }

.editor-banner {
  background: rgba(210, 168, 87, 0.1);
  border: 1px solid var(--accent);
  color: var(--ink);
  font-size: 12px;
  padding: 6px 10px;
  border-radius: 4px;
  margin-bottom: 8px;
}

.custom-editor {
  margin-top: 12px;
  padding: 12px;
  background: var(--bg);
  border: 1px solid var(--accent);
  border-radius: 8px;
}
.custom-editor input[type="text"] {
  width: 100%;
  margin-bottom: 8px;
}
.custom-editor .custom-frets {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 8px;
}
.custom-editor .custom-diagram-wrap {
  max-width: 200px;
  margin: 0 auto 8px;
}
.custom-editor .custom-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  transition: opacity 150ms, border-color 150ms, background 150ms;
}

.card[data-chord] { cursor: pointer; }
.card[data-chord]:hover { border-color: var(--muted); }
.card.selected {
  border-color: var(--accent);
  background: rgba(210, 168, 87, 0.08);
  box-shadow: 0 0 0 1px var(--accent);
}
.card.compatible {
  border-color: var(--accent);
}
.card.dimmed {
  opacity: 0.3;
}

.card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}

.chord-name {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.identified {
  font-size: 13px;
  color: var(--accent);
}

.identified.warn { color: var(--warn); }
.identified.approx { color: var(--muted); font-style: italic; }

.diagram { width: 100%; height: auto; display: block; }
.diagram.interactive .hit { cursor: pointer; }
/* No :hover fill on the hit cells. The faint highlight read as a grey smudge,
   and worse, after a tap the card re-renders and the browser re-applies :hover
   to whatever cell is under the pointer, leaving a box stuck in place (on both
   touch and desktop). The pointer cursor is enough of a click affordance. */

.identify-card {
  border-color: var(--accent);
}

.reset-btn {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 11px;
  font-family: inherit;
  cursor: pointer;
}
.reset-btn:hover { color: var(--ink); border-color: var(--muted); }

.position-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.position-controls button {
  width: 28px;
  padding: 4px 0;
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}
.position-controls button:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}
.position-controls button:hover:not(:disabled) {
  border-color: var(--accent);
}
.position-controls span {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  min-width: 86px;
  text-align: center;
}

.notes-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  font-size: 12px;
}

.note-chip {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 2px 6px;
  font-variant-numeric: tabular-nums;
}

.note-chip.muted-string {
  opacity: 0.4;
  text-decoration: line-through;
}

.meta {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
}

/* ============================================================
   Responsive + touch support (mobile and iPad first-class)
   ============================================================ */

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

* {
  /* Transparent, not a tint: on the SVG fret cells a tinted tap highlight
     lingers as a grey/gold box after a tap (the "smudge"). */
  -webkit-tap-highlight-color: transparent;
}

/* The chip move (reorder) buttons are hidden by default; shown on touch. */
.chip-move { display: none; }
.chip-move-btn {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 4px;
  padding: 6px 0;
  font-size: 12px;
  cursor: pointer;
}
.chip-move-btn:active { color: var(--accent); border-color: var(--accent); }

/* On touch devices (no hover): always reveal the per-chip action buttons and
   the saved-voicing edit/delete icons, since there's no hover to trigger them.
   Also surface the touch reorder buttons. */
@media (hover: none) {
  .chord-chip .chip-remove,
  .chord-chip .chip-duplicate { display: block; }
  .picker-option.saved-voicing .voicing-edit,
  .picker-option.saved-voicing .voicing-delete { display: inline; }
  .chip-move { display: flex; gap: 6px; margin-top: 4px; }
  /* Larger hit targets for the corner buttons on touch */
  .chord-chip .chip-remove,
  .chord-chip .chip-duplicate {
    width: 22px;
    height: 22px;
    font-size: 13px;
    line-height: 20px;
  }
}

/* Tablet / iPad portrait and below: stack the songs view so the editor gets
   full width instead of sharing with a fixed 220px sidebar. */
@media (max-width: 820px) {
  .songs-view {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  .songs-sidebar {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
  }
  .new-song-btn { flex: 0 0 auto; }
  .songs-list {
    flex-direction: row;
    flex-wrap: wrap;
    flex: 1;
  }
  .songs-list li { flex: 0 0 auto; }
}

/* Phones: tighten padding, let header controls breathe, single-column cards. */
@media (max-width: 560px) {
  .bar { padding: 12px 14px; gap: 10px; }
  .grid { padding: 14px; gap: 12px; }
  .songs-view { padding: 14px; }

  /* Let the tuning dropdown shrink instead of overflowing. */
  #tuning { max-width: 60vw; }
  .controls { gap: 8px; width: 100%; }

  /* Tabs span full width and are easy to tap. */
  .tabs button { flex: 1; padding: 12px 8px; }

  /* Library cards go single-column on the smallest screens. */
  .grid { grid-template-columns: 1fr; }

  /* Song header wraps cleanly; key + buttons drop to their own row. */
  .song-header { gap: 8px; }
  .song-name-input { min-width: 0; flex: 1 1 100%; }
  .detected-key { flex-wrap: wrap; }

  /* Learn popover should not overflow the screen. */
  .learn-popover {
    position: fixed;
    left: 12px;
    right: 12px;
    top: auto;
    min-width: 0;
    max-width: none;
    margin-top: 8px;
  }

  /* Section chord chips: a touch larger min so diagrams stay legible. */
  .section-chords {
    grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  }
  .chord-chip, .add-chord-btn { width: auto; }
}

/* Comfortable minimum tap target for small buttons on touch. */
@media (hover: none) {
  button, .picker-option, select {
    min-height: 38px;
  }
}

/* ============================================================
   About modal
   ============================================================ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}
.modal {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--accent);
  border-radius: 12px;
  padding: 24px 28px;
  max-width: 460px;
  width: 100%;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.5);
}
.modal h2 {
  margin: 0 0 12px;
  font-size: 20px;
  color: var(--accent);
  letter-spacing: 0.02em;
}
.modal p {
  margin: 12px 0;
  font-size: 14px;
  line-height: 1.55;
  color: var(--ink);
}
.modal .modal-note {
  background: rgba(210, 168, 87, 0.08);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  font-size: 13px;
}
.modal .modal-fine {
  font-size: 12px;
  color: var(--muted);
}
.modal-close {
  position: absolute;
  top: 10px;
  right: 12px;
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
}
.modal-close:hover { color: var(--ink); }

#about-btn {
  font-size: 13px;
  padding: 6px 14px;
}
