/* ---------- Design tokens ---------- */
:root {
  --paper: #fffdf4;            /* song sheet */
  --parchment: #f6ebc9;        /* app background */
  --parchment-2: #efdfb4;      /* background accent */
  --ink: #221d15;             /* lyrics (warm near-black) */
  --chord: #c0392b;           /* chords & rhythm (reddish) */
  --accent: #8a5a2b;          /* brown accents */
  --muted: #9a8a62;
  --line: #e6d6a8;            /* hairlines */
  --shadow: 0 10px 40px rgba(80, 55, 10, 0.16);

  --font-lyric: 'Frank Ruhl Libre', Georgia, 'Times New Roman', serif;
  --font-ui: 'Assistant', system-ui, sans-serif;
  --font-chord: 'JetBrains Mono', ui-monospace, 'Courier New', monospace;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font-ui);
  color: var(--ink);
  background:
    radial-gradient(1200px 800px at 80% -10%, var(--parchment-2), transparent 60%),
    linear-gradient(180deg, var(--parchment), var(--parchment-2));
  background-attachment: fixed;
}

.app {
  display: grid;
  grid-template-columns: 300px 1fr;
  min-height: 100vh;
}

/* ---------- Sidebar ---------- */
.sidebar {
  background: rgba(255, 253, 244, 0.7);
  backdrop-filter: blur(4px);
  border-inline-end: 1px solid var(--line);
  padding: 1.4rem 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  position: sticky;
  top: 0;
  align-self: start;
  height: 100vh;
  overflow: hidden;
}

.brand { text-align: center; border-bottom: 1px solid var(--line); padding-bottom: 0.8rem; }
.brand h1 {
  font-family: var(--font-lyric);
  font-weight: 700;
  font-size: 1.9rem;
  margin: 0;
  color: var(--accent);
  letter-spacing: 0.02em;
}
.brand-sub {
  margin: 0.1rem 0 0;
  font-size: 0.8rem;
  letter-spacing: 0.35em;
  text-transform: uppercase;
  color: var(--muted);
}

.search {
  width: 100%;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
  font-family: var(--font-ui);
  font-size: 1rem;
  color: var(--ink);
  outline: none;
}
.search:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(138, 90, 43, 0.12); }

.song-list { overflow-y: auto; flex: 1; margin: 0 -0.3rem; padding: 0 0.3rem; }

/* ---------- Categories ---------- */
.new-category { display: flex; gap: 0.4rem; }
.new-category input {
  flex: 1;
  min-width: 0;
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  font-family: var(--font-ui);
  font-size: 0.9rem;
  color: var(--ink);
}
.new-category input:focus { outline: none; border-color: var(--accent); }
.new-category button {
  border: 1px solid var(--line);
  background: var(--parchment);
  color: var(--accent);
  border-radius: 8px;
  width: 2.2rem;
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
}
.new-category button:disabled { opacity: 0.4; cursor: default; }

.category {
  border-radius: 10px;
  margin-bottom: 0.15rem;
  border: 1.5px solid transparent;
  transition: background 0.12s ease, border-color 0.12s ease;
}
.category.drag-over {
  border-color: var(--accent);
  background: rgba(138, 90, 43, 0.1);
}
/* Category reorder: a line marking where the dragged category will land. */
.category.cat-drop-before { box-shadow: inset 0 3px 0 -1px var(--accent); }
.category.cat-drop-after { box-shadow: inset 0 -3px 0 -1px var(--accent); }

.category-header {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.3rem 0.35rem;
}
.cat-handle {
  cursor: grab;
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1;
  padding: 0 0.1rem;
  opacity: 0;
  transition: opacity 0.12s ease;
  user-select: none;
}
.category:hover .cat-handle { opacity: 0.45; }
.cat-handle:hover { opacity: 0.9; }
.cat-handle:active { cursor: grabbing; }
.caret {
  border: none;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.75rem;
  padding: 0 0.15rem;
  transition: transform 0.12s ease;
}
.caret.open { transform: rotate(90deg); }
.cat-name {
  flex: 1;
  min-width: 0;
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--accent);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cat-rename {
  flex: 1;
  min-width: 0;
  font-family: var(--font-ui);
  font-size: 0.95rem;
  padding: 0.1rem 0.3rem;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
}
.cat-count {
  font-family: var(--font-chord);
  font-size: 0.72rem;
  color: var(--muted);
  min-width: 1.2rem;
  text-align: center;
}
.cat-btn {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 0.85rem;
  opacity: 0;
  padding: 0.1rem 0.25rem;
  border-radius: 6px;
  transition: opacity 0.12s ease;
}
.category:hover .cat-btn { opacity: 0.55; }
.cat-btn:hover { opacity: 1; background: rgba(138, 90, 43, 0.12); }

.cat-songs { padding-inline-start: 0.7rem; padding-bottom: 0.2rem; }
/* Song reorder: a line showing the drop slot (before a row, or at the end of the list). */
.cat-songs.drop-end { box-shadow: inset 0 -3px 0 -1px var(--accent); }
.empty-hint { font-size: 0.8rem; font-style: italic; padding: 0.25rem 0.6rem; }

.song-link {
  display: block;
  width: 100%;
  text-align: start;
  background: none;
  border: none;
  border-radius: 8px;
  padding: 0.4rem 0.55rem;
  font-family: var(--font-lyric);
  font-size: 1.08rem;
  color: var(--ink);
  cursor: grab;
  transition: background 0.12s ease;
}
.song-link:hover { background: rgba(138, 90, 43, 0.08); }
.song-link.active { background: var(--accent); color: #fff; }
.song-link:active { cursor: grabbing; }
.song-link.drop-before { box-shadow: inset 0 3px 0 -1px var(--accent); }

.muted { color: var(--muted); font-size: 0.9rem; padding: 0.3rem 0.6rem; }
.error { color: var(--chord); font-size: 0.9rem; padding: 0.3rem 0.6rem; }

/* ---------- Stage / sheet ---------- */
.stage {
  padding: 2.4rem 2rem 4rem;
  display: flex;
  justify-content: center;
}

.placeholder {
  margin-top: 20vh;
  text-align: center;
  font-family: var(--font-lyric);
  font-size: 1.6rem;
  color: var(--accent);
}
.placeholder .muted { font-size: 1rem; }

.sheet {
  background: var(--paper);
  width: min(780px, 100%);
  border-radius: 14px;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  padding: 2.4rem 2.8rem 3rem;
  position: relative;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.4rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--line);
}
.transpose { display: flex; align-items: center; gap: 0.4rem; }
.transpose button, .print-btn {
  font-family: var(--font-ui);
  border: 1px solid var(--line);
  background: var(--parchment);
  color: var(--accent);
  border-radius: 8px;
  padding: 0.35rem 0.7rem;
  font-size: 1rem;
  cursor: pointer;
  line-height: 1;
}
.transpose button:hover, .print-btn:hover { background: var(--parchment-2); }
.transpose .offset {
  font-family: var(--font-chord);
  min-width: 2.4ch;
  text-align: center;
  color: var(--chord);
  font-weight: 700;
}
.transpose .reset { font-size: 0.8rem; padding: 0.3rem 0.5rem; }

.song-title {
  font-family: var(--font-lyric);
  font-weight: 700;
  font-size: 2.1rem;
  color: var(--accent);
  margin: 0 0 1.6rem;
  text-align: center;
}

/* ---------- The song body: chord-over-syllable ---------- */
.song-body {
  font-family: var(--font-lyric);
  font-size: 1.5rem;
  line-height: 1.25;
  color: var(--ink);
  position: relative;
  /* Two-zone start margin: repeat bracket / gutter at the far edge, then room for a chord
     pulled "before the first word", then the lyrics. */
  padding-inline-start: 2.3rem;
}

/* Repeat bracket: a vertical rule with inward caps and a ×N chip, in the start gutter. */
.repeat-bracket {
  position: absolute;
  inset-inline-start: 0.35rem;
  width: 0.55rem;
  border-inline-start: 2px solid var(--chord);
  pointer-events: none;
}
.repeat-bracket::before,
.repeat-bracket::after {
  content: '';
  position: absolute;
  inset-inline-start: -2px;
  width: 0.45rem;
  height: 2px;
  background: var(--chord);
}
.repeat-bracket::before { top: 0; }
.repeat-bracket::after { bottom: 0; }
.repeat-count {
  position: absolute;
  inset-inline-start: -0.25rem;
  /* Sit fully below the bracket's bottom cap so the ×N text never rides over the line. */
  top: 100%;
  margin-top: 2px;
  font-family: var(--font-chord);
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--chord);
  background: var(--paper);
  padding: 0 0.15rem;
  white-space: nowrap;
}

/* Editor: drag the start-side gutter to mark a repeat; its chip + ✕ sit ABOVE the gutter. */
.edit-gutter {
  position: absolute;
  inset-inline-start: 0;
  top: 0;
  bottom: 0;
  width: 1rem; /* far-edge strip only; chords pulled before a word sit to its inline-end */
  cursor: ns-resize;
  z-index: 2;
  border-radius: 6px;
  border-inline-start: 2px dashed var(--line);
  background: rgba(138, 90, 43, 0.05);
}
.edit-gutter:hover {
  background: rgba(192, 57, 43, 0.1);
  border-inline-start-color: var(--chord);
}
.repeat-sel {
  position: absolute;
  inset-inline-start: 0.2rem;
  width: 0.95rem;
  background: rgba(192, 57, 43, 0.2);
  border-radius: 4px;
  pointer-events: none;
  z-index: 3;
}
/* The bracket body stays click-through (so you can still drag the gutter under it); only the
   chip and ✕ are interactive, and they sit above the gutter. */
.repeat-bracket.editable { pointer-events: none; z-index: 4; }
.repeat-bracket.editable .repeat-count,
.repeat-bracket.editable .repeat-x { pointer-events: auto; cursor: pointer; }
.repeat-x {
  position: absolute;
  inset-inline-start: -0.5rem;
  top: -0.7rem;
  width: 1.05rem;
  height: 1.05rem;
  line-height: 1rem;
  text-align: center;
  font-size: 0.68rem;
  color: var(--muted);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 50%;
}
.repeat-x:hover { color: #fff; background: var(--chord); border-color: var(--chord); }

.stanza { margin-bottom: 1.8rem; }
.stanza:last-child { margin-bottom: 0; }

/* A line wraps only at the spaces between words. */
.line {
  white-space: normal;
  margin-bottom: 0.3em;
  min-height: 1em;
}

/* A unit is an unbreakable run of lyric text with its own floating-chord overlay.
   Lines wrap only between units, so each unit is a single row and reserves its own chord
   row above it. */
.unit {
  position: relative;
  display: inline-block;
  vertical-align: baseline;
  white-space: pre;
}
.unit .lyric { white-space: pre; }

/* Only lines that carry chords reserve the row above (keeps chordless lines compact). */
.line.has-chords .unit { padding-top: 1em; }

/* Chord overlay: spans the unit; chords are positioned absolutely (measured in JS) and may
   overhang — so a wide chord like Fmaj7 never widens the lyric beneath it. */
.chords {
  position: absolute;
  inset-inline: 0;
  top: 0;
  height: 1em;
  pointer-events: none;
}
.chord {
  position: absolute;
  top: 0;
  font-family: var(--font-chord);
  font-weight: 700;
  font-size: 0.6em;
  line-height: 1.1;
  color: var(--chord);
  white-space: nowrap;
  /* Keep Latin chord names (C#, Am, G/B) left-to-right even inside RTL lyrics. */
  unicode-bidi: isolate;
  direction: ltr;
}

/* Rhythm marks carry no glyph — the beat tick (below) is the whole notation. */
.chord.rhythm { width: 0; height: 0; }

/* Thin connector from a chord down to the exact character it applies to. */
.tick {
  position: absolute;
  bottom: 0;
  width: 1.5px;
  height: 0.34em;
  background: var(--chord);
  opacity: 0.45;
  border-radius: 1px;
}

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; max-height: 45vh; }
  .stage { padding: 1.2rem 0.8rem 3rem; }
  .sheet { padding: 1.6rem 1.3rem 2rem; }
  .song-body { font-size: 1.3rem; }
}

/* ---------- Print ---------- */
@media print {
  body { background: #fff; }
  .sidebar, .toolbar { display: none !important; }
  .app { display: block; }
  .stage { padding: 0; display: block; }
  .sheet {
    box-shadow: none;
    border: none;
    width: 100%;
    padding: 0;
  }
  .song-title { color: var(--accent); }
  .song-body { color: #000; }
  /* chords keep their reddish color in print (per spec) */
}

/* ---------- Toolbar tools / editable title ---------- */
.tools { display: flex; gap: 0.4rem; align-items: center; }
.tool-btn {
  font-family: var(--font-ui);
  border: 1px solid var(--line);
  background: var(--parchment);
  color: var(--accent);
  border-radius: 8px;
  padding: 0.35rem 0.6rem;
  font-size: 0.95rem;
  line-height: 1;
  cursor: pointer;
}
.tool-btn:hover { background: var(--parchment-2); }
.tool-btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.tool-btn.danger:hover { background: #f3d6d0; color: var(--chord); border-color: var(--chord); }

.song-title-input {
  display: block;
  width: 100%;
  box-sizing: border-box;
  font-family: var(--font-lyric);
  font-weight: 700;
  font-size: 2.1rem;
  color: var(--accent);
  text-align: center;
  border: none;
  border-bottom: 2px dashed var(--line);
  background: transparent;
  margin: 0 0 1rem;
  padding: 0.2rem;
}
.song-title-input:focus { outline: none; border-bottom-color: var(--accent); }
.edit-hint { text-align: center; color: var(--muted); font-size: 0.85rem; margin: 0 0 1rem; }

.add-song { padding: 0.2rem 0.3rem 0.4rem; }
.add-song input {
  width: 100%;
  box-sizing: border-box;
  padding: 0.4rem 0.55rem;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--paper);
  font-family: var(--font-lyric);
  font-size: 1.02rem;
  color: var(--ink);
}
.add-song input:focus { outline: none; }

/* ---------- WYSIWYG editor ---------- */
.edit-line {
  position: relative;
  z-index: 3; /* above the repeat gutter, so a pulled chord stays clickable over it */
  padding-top: 1.15em;
  margin-bottom: 0.3em;
  font-family: var(--font-lyric);
  font-size: 1.5rem;
  line-height: 1.3;
}
.edit-line .lyric {
  outline: none;
  white-space: pre-wrap;
  min-height: 1.25em;
  color: var(--ink);
  caret-color: var(--accent);
  border-radius: 4px;
}
.edit-line .lyric:empty::before { content: '\200b'; } /* keep height when empty */

.echords {
  position: absolute;
  inset-inline: 0;
  top: 0;
  height: 1.05em;
  cursor: copy; /* click to inject a chord / beat */
  border-radius: 4px;
}
.edit-line:hover .echords { background: rgba(138, 90, 43, 0.06); }

.emark {
  position: absolute;
  top: 0;
  font-family: var(--font-chord);
  font-weight: 700;
  font-size: 0.6em;
  line-height: 1.1;
  color: var(--chord);
  white-space: nowrap;
  unicode-bidi: isolate;
  direction: ltr;
  cursor: grab;
  padding: 0 0.12em;
  border-radius: 4px;
  user-select: none;
  touch-action: none;
}
.emark:hover { background: rgba(192, 57, 43, 0.14); }
.emark.dragging { cursor: grabbing; opacity: 0.85; }
/* Rhythm mark: no glyph (just the tick, like View) — an invisible grab area over its tick. */
.emark.rhythm { width: 0.7em; height: 1.05em; }
.etick {
  position: absolute;
  bottom: 0;
  width: 1.5px;
  height: 0.34em;
  background: var(--chord);
  opacity: 0.45;
  border-radius: 1px;
  pointer-events: none;
}
/* While dragging, the connector snaps to the exact boundary and stands out. */
.etick.snapping { height: 0.72em; width: 2px; opacity: 0.95; }

/* ---------- Chord popover ---------- */
.chord-popover {
  position: fixed;
  z-index: 100;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  font-family: var(--font-ui);
}
.pop-row { display: flex; gap: 0.4rem; align-items: center; }
.pop-chords {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  max-width: 15rem;
  border-top: 1px solid var(--line);
  padding-top: 0.4rem;
}
.pop-chip {
  padding: 0.25rem 0.5rem !important;
  font-family: var(--font-chord) !important;
  font-weight: 700;
  color: var(--chord) !important;
}

/* Hover preview: where a chord would land if you click the chord row. */
.hover-caret {
  position: absolute;
  top: 0;
  height: 1.05em;
  width: 2px;
  background: var(--accent);
  opacity: 0.55;
  border-radius: 1px;
  pointer-events: none;
  display: none;
}
.hover-caret::before {
  content: '＋';
  position: absolute;
  top: -0.9em;
  inset-inline-start: -0.4em;
  font-size: 0.7em;
  color: var(--accent);
}
.chord-popover input {
  width: 8rem;
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--parchment);
  font-family: var(--font-chord);
  font-size: 0.95rem;
  color: var(--ink);
}
.chord-popover button {
  padding: 0.35rem 0.55rem;
  border: 1px solid var(--line);
  background: var(--parchment);
  color: var(--accent);
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  white-space: nowrap;
}
.chord-popover button:hover { background: var(--parchment-2); }
.chord-popover .pop-label { color: var(--muted); font-size: 0.9rem; }

/* ---------- Modal + toast ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(40, 25, 5, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal {
  background: var(--paper);
  border-radius: 14px;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  padding: 1.6rem 1.8rem;
  max-width: 90vw;
  font-family: var(--font-ui);
}
.modal-text { font-size: 1.1rem; color: var(--ink); margin: 0 0 1.2rem; }
.modal-actions { display: flex; gap: 0.6rem; justify-content: flex-end; }
.modal-actions button {
  padding: 0.5rem 0.9rem;
  border: 1px solid var(--line);
  background: var(--parchment);
  color: var(--accent);
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.95rem;
  font-family: var(--font-ui);
}
.modal-actions button:hover { background: var(--parchment-2); }
.modal-actions .danger { background: var(--chord); color: #fff; border-color: var(--chord); }
.modal-actions .danger:hover { background: #a93226; }

.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 300;
  background: var(--accent);
  color: #fff;
  padding: 0.6rem 1.1rem;
  border-radius: 999px;
  font-family: var(--font-ui);
  font-size: 0.95rem;
  box-shadow: var(--shadow);
}
