:root {
  --bg: #0f0f1e;
  --bg-card: #1e1e35;
  --bg-card-hover: #2a2a45;
  --bg-card-selected: #3a3a60;
  --bg-input: #161628;
  --border: #2a2a4a;
  --text: #e8e8f0;
  --text-dim: #8888a0;
  --accent: #ff6b9d;
  --accent-hover: #ff8fb1;
  --accent-dim: rgba(255, 107, 157, 0.3);
  --secondary: #6c7ce0;
  --white-key: #f5f5f0;
  --white-key-hover: #e0e0d8;
  --white-key-active: #ffaecb;
  --black-key: #181818;
  --black-key-hover: #2a2a2a;
  --black-key-active: #ff6b9d;
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
  --radius: 12px;
  --radius-sm: 6px;
  --transition: 0.18s ease;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  min-height: 100vh;
  line-height: 1.5;
}

body {
  background:
    radial-gradient(circle at 20% 0%, rgba(108, 124, 224, 0.08), transparent 40%),
    radial-gradient(circle at 80% 100%, rgba(255, 107, 157, 0.08), transparent 40%),
    var(--bg);
  background-attachment: fixed;
}

.app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 20px 40px;
}

header {
  text-align: center;
  margin-bottom: 28px;
}

header h1 {
  font-size: 2.6rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent), var(--secondary));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.02em;
  margin-bottom: 4px;
}

.tagline {
  color: var(--text-dim);
  font-size: 0.95rem;
}

main {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

section {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}

.section-header h2 {
  font-size: 1.2rem;
  color: var(--accent);
  font-weight: 600;
  letter-spacing: 0.01em;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

/* "Editing: <chord name>" heading — pill-style chord name to make the
   coupling between the selected card and the editor unmistakable. */
.editor-chord-name {
  color: var(--accent);
  background: rgba(255, 107, 157, 0.14);
  padding: 2px 12px;
  border-radius: 999px;
  font-family: 'SF Mono', Monaco, Consolas, monospace;
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: 0.02em;
  border: 1px solid rgba(255, 107, 157, 0.3);
}

.editor-chord-name.empty {
  color: var(--text-dim);
  background: transparent;
  border-color: var(--border);
  font-family: inherit;
  font-style: italic;
}

.editor-hint {
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-bottom: 14px;
  font-style: italic;
}

.progression-controls,
.editor-controls {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.btn {
  background: var(--bg-card-hover);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 500;
  font-size: 0.9rem;
  transition: var(--transition);
  font-family: inherit;
}

.btn:hover:not(:disabled) {
  background: var(--bg-card-selected);
  border-color: var(--accent-dim);
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Progression settings (Voice / BPM / Time sig) */
.progression-settings {
  display: flex;
  gap: 18px;
  flex-wrap: wrap;
  align-items: center;
  padding: 10px 14px;
  margin-bottom: 14px;
  background: var(--bg-input);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.setting {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
}

.setting-label {
  color: var(--text-dim);
  font-weight: 500;
  letter-spacing: 0.02em;
}

.progression-settings input[type="number"] {
  width: 72px;
  padding: 6px 9px;
}

.instrument-loading {
  font-size: 0.8rem;
  color: var(--accent);
  font-style: italic;
  margin-left: 6px;
  animation: instrument-loading-pulse 1.4s ease-in-out infinite;
}

@keyframes instrument-loading-pulse {
  0%, 100% { opacity: 0.55; }
  50%      { opacity: 1; }
}

.progression-settings select,
#duration-preset {
  background: var(--bg-card-hover);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 26px 6px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 500;
  font-size: 0.88rem;
  font-family: inherit;
  transition: var(--transition);
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24'><path fill='%23e8e8f0' d='M7 10l5 5 5-5z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 6px center;
}

.progression-settings select:hover,
#duration-preset:hover {
  background-color: var(--bg-card-selected);
  border-color: var(--accent-dim);
}

.progression-settings select:focus,
#duration-preset:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-dim);
}

/* Chord list */
.chord-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  min-height: 110px;
  background: var(--bg-input);
  padding: 14px;
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
  align-content: flex-start;
  border: 1px dashed var(--border);
}

/* The add-chord card lives inside the chord-list as a permanent last child,
   so the row never has to handle a separate empty state — the "+" card is
   always the visible affordance for adding a new chord. */

.chord-card {
  width: 110px;
  min-height: 80px;
  background: var(--bg-card-hover);
  border-radius: var(--radius-sm);
  padding: 12px;
  cursor: grab;
  position: relative;
  transition: var(--transition);
  border: 2px solid transparent;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  user-select: none;
}

.chord-card:hover {
  background: var(--bg-card-selected);
}

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

.chord-card.dragging {
  opacity: 0.4;
  cursor: grabbing;
}

.chord-card.drag-over {
  border-color: var(--secondary);
  border-style: dashed;
}

.chord-card.playing {
  border-color: var(--accent);
  box-shadow: 0 0 18px var(--accent);
  animation: pulse 0.55s ease-in-out infinite alternate;
}

/* "+ Add chord" card — always last child of the chord-list, so the new chord
   appears immediately to its left when clicked. Visually distinct from real
   chord cards (dashed border, no name/duration text). */
.chord-card--add {
  background: transparent;
  border: 2px dashed var(--border);
  color: var(--text-dim);
  cursor: pointer;
  font-family: inherit;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 0;
  transition: var(--transition);
}

.chord-card--add:hover:not(:disabled) {
  border-color: var(--accent);
  background: rgba(255, 107, 157, 0.05);
  color: var(--accent);
}

.chord-card--add:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.chord-card--add .add-icon {
  font-size: 1.8rem;
  font-weight: 300;
  line-height: 1;
}

.chord-card--add .add-label {
  font-size: 0.75rem;
  font-weight: 500;
  letter-spacing: 0.02em;
}

@keyframes pulse {
  from { transform: scale(1); }
  to { transform: scale(1.045); }
}

.chord-card .name {
  font-weight: 700;
  font-size: 1.05rem;
  word-break: break-word;
  line-height: 1.2;
}

.chord-card .name.empty {
  color: var(--text-dim);
  font-weight: 400;
  font-style: italic;
}

.chord-card .duration {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-top: 4px;
}

.chord-card .delete-btn {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: rgba(255, 80, 80, 0.7);
  border: none;
  color: white;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 0;
  display: none;
  align-items: center;
  justify-content: center;
}

.chord-card:hover .delete-btn {
  display: flex;
}

.chord-card .delete-btn:hover {
  background: rgb(255, 60, 60);
}

.hint {
  font-size: 0.8rem;
  color: var(--text-dim);
}

/* Export section — its own section near the bottom of the page. */
.export-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.export-label {
  font-size: 0.9rem;
  color: var(--text-dim);
  font-weight: 500;
  margin-right: 4px;
}

kbd {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 1px 5px;
  font-size: 0.75rem;
  font-family: 'SF Mono', Monaco, Consolas, monospace;
}

/* Piano */
.piano-wrapper {
  background: var(--bg-input);
  padding: 14px;
  border-radius: var(--radius-sm);
  margin-bottom: 18px;
  overflow-x: auto;
  border: 1px solid var(--border);
}

.piano {
  position: relative;
  display: flex;
  height: 160px;
  width: max-content;
  margin: 0 auto;
}

.white-key {
  width: 44px;
  height: 100%;
  background: var(--white-key);
  border: 1px solid #999;
  border-right: none;
  border-radius: 0 0 4px 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  padding-bottom: 8px;
  font-size: 0.7rem;
  color: #666;
  transition: background 0.1s;
  user-select: none;
  font-weight: 500;
}

.white-key:last-of-type {
  border-right: 1px solid #999;
}

.white-key:hover {
  background: var(--white-key-hover);
}

.white-key.active {
  background: var(--white-key-active);
  color: #3a1a2a;
  font-weight: 600;
}

.black-key {
  position: absolute;
  width: 28px;
  height: 60%;
  background: var(--black-key);
  border: 1px solid #000;
  border-radius: 0 0 4px 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  padding-bottom: 5px;
  font-size: 0.55rem;
  color: #ccc;
  z-index: 2;
  transition: background 0.1s;
  user-select: none;
}

.black-key:hover {
  background: var(--black-key-hover);
}

.black-key.active {
  background: var(--black-key-active);
  color: white;
}

/* Editor info */
.chord-info {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.info-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.info-row label {
  min-width: 130px;
  font-weight: 500;
  color: var(--text-dim);
  font-size: 0.9rem;
}

.muted {
  color: var(--text-dim);
  font-weight: 400;
}

.value {
  color: var(--text);
}

.value.accent {
  color: var(--accent);
}

.mono {
  font-family: 'SF Mono', Monaco, Consolas, monospace;
  font-size: 0.92rem;
}

input[type="text"], input[type="number"] {
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 7px 11px;
  font-family: inherit;
  font-size: 0.9rem;
  transition: var(--transition);
}

input[type="text"]:focus, input[type="number"]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-dim);
}

input[type="text"]:disabled, input[type="number"]:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

input[type="text"] {
  flex: 1;
  min-width: 150px;
  max-width: 280px;
}

input[type="number"] {
  width: 80px;
}

/* Articulation controls (spread input appears next to the style dropdown) */
.spread-controls {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.spread-controls.hidden {
  display: none;
}

.spread-controls input[type="number"] {
  width: 70px;
}

.inline-sublabel {
  color: var(--text-dim);
  font-size: 0.85rem;
  font-weight: 500;
}

#articulation {
  background: var(--bg-card-hover);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 26px 6px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 500;
  font-size: 0.9rem;
  font-family: inherit;
  transition: var(--transition);
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24'><path fill='%23e8e8f0' d='M7 10l5 5 5-5z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 6px center;
}

#articulation:hover {
  background-color: var(--bg-card-selected);
  border-color: var(--accent-dim);
}

#articulation:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-dim);
}

/* Select (instrument picker) */
select#instrument-select {
  background-color: var(--bg-card-hover);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 30px 8px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 500;
  font-size: 0.9rem;
  font-family: inherit;
  transition: var(--transition);
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24'><path fill='%23e8e8f0' d='M7 10l5 5 5-5z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 8px center;
}

select#instrument-select:hover {
  background-color: var(--bg-card-selected);
  border-color: var(--accent-dim);
}

select#instrument-select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-dim);
}

/* Playback lock — editor disabled while progression is playing */
body.is-playing .chord-card {
  pointer-events: none;
}

body.is-playing .editor-controls,
body.is-playing .chord-info {
  opacity: 0.65;
  pointer-events: none;
  transition: opacity 0.2s;
}

/* Piano stays fully lit during playback so users can watch the keys
   light up — just disable interaction. */
body.is-playing .piano-wrapper {
  pointer-events: none;
}

/* Sheet Music Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  z-index: 100;
  overflow-y: auto;
  padding: 40px 20px;
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: #fafafa;
  color: #111;
  border-radius: var(--radius);
  max-width: 1100px;
  width: 100%;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid #ddd;
  background: #ececec;
  border-radius: var(--radius) var(--radius) 0 0;
  flex-wrap: wrap;
  gap: 10px;
}

.modal-header h2 {
  color: #111;
  font-size: 1.2rem;
  margin: 0;
}

.modal-controls {
  display: flex;
  gap: 8px;
}

.modal-body {
  padding: 24px 20px;
  overflow-x: auto;
  background: white;
  border-radius: 0 0 var(--radius) var(--radius);
}

#sheet-container {
  background: white;
  color: black;
  min-height: 160px;
  display: flex;
  justify-content: center;
}

#sheet-container svg {
  background: white;
  max-width: 100%;
  height: auto;
}

.modal-note {
  color: #666;
  font-size: 0.8rem;
  font-style: italic;
  margin-top: 16px;
  text-align: center;
}

/* Print: hide everything except the sheet itself */
@media print {
  body.printing-sheet > *:not(.modal-overlay) {
    display: none !important;
  }
  body.printing-sheet .modal-overlay {
    position: static;
    background: white;
    padding: 0;
    overflow: visible;
  }
  body.printing-sheet .modal {
    box-shadow: none;
    border-radius: 0;
    max-width: none;
    background: white;
  }
  body.printing-sheet .modal-header,
  body.printing-sheet .modal-note {
    display: none !important;
  }
  body.printing-sheet .modal-body {
    padding: 0;
    overflow: visible;
  }
  body.printing-sheet #sheet-container {
    page-break-inside: avoid;
  }
}

footer {
  text-align: center;
  margin-top: 32px;
  color: var(--text-dim);
  font-size: 0.85rem;
}

footer a {
  color: var(--secondary);
  text-decoration: none;
}

footer a:hover {
  text-decoration: underline;
}

/* Responsive */
@media (max-width: 640px) {
  .app {
    padding: 16px 12px 32px;
  }

  header h1 {
    font-size: 2rem;
  }

  section {
    padding: 16px;
  }

  .section-header {
    flex-direction: column;
    align-items: stretch;
  }

  .progression-controls,
  .editor-controls {
    justify-content: center;
  }

  .white-key {
    width: 38px;
  }

  .black-key {
    width: 24px;
  }

  .info-row label {
    min-width: 100%;
    margin-bottom: -6px;
  }

  .chord-card {
    width: calc(50% - 5px);
  }
}
