/* Reset + base */
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: 'Pretendard', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  background: #ffffff;
  color: #18181b;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, textarea, select { font-family: inherit; font-size: inherit; color: inherit; }
input:focus, textarea:focus, select:focus { outline: none; }

:root {
  --bg: #ffffff;
  --surface: #fafafa;
  --surface-2: #f4f4f5;
  --border: #e7e7ea;
  --border-strong: #d4d4d8;
  --ink: #18181b;
  --ink-2: #52525b;
  --ink-3: #a1a1aa;
  --accent: #18181b;
  --gold: #eab308;
  --danger: #e11d48;
  --max-width: 860px;
  --radius: 10px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 4px 16px rgba(0,0,0,.06), 0 1px 3px rgba(0,0,0,.04);
  --shadow-lg: 0 20px 50px rgba(0,0,0,.12);
}

/* App shell */
.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ===== Top bar ===== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(255,255,255,.92);
  backdrop-filter: saturate(180%) blur(10px);
  -webkit-backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 14px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
}
.topbar h1 {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.01em;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar h1 small {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-3);
  margin-top: 2px;
}
.icon-btn {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  color: var(--ink-2);
  transition: background .12s, color .12s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn.is-active { color: var(--gold); }
.icon-btn svg { width: 20px; height: 20px; }

.signin-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: #fff;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink);
  transition: background .12s, border-color .12s;
}
.signin-btn:hover { background: var(--surface); border-color: var(--ink); }
.avatar-btn {
  width: 32px;
  height: 32px;
  border-radius: 999px;
  background: var(--ink);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 600;
  overflow: hidden;
  transition: transform .12s;
}
.avatar-btn:hover { transform: scale(1.04); }
.avatar-btn img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* ===== Home ===== */
.home {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 24px 20px 120px;
  width: 100%;
}
.search {
  position: relative;
  margin-bottom: 18px;
}
.search input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 10px;
  padding: 11px 14px 11px 40px;
  font-size: 14px;
  transition: border-color .12s, background .12s;
}
.search input:focus { border-color: var(--ink); background: #fff; }
.search svg {
  position: absolute;
  left: 13px;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  color: var(--ink-3);
}

.folder-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 14px;
  overflow-x: auto;
  scrollbar-width: none;
  padding-bottom: 2px;
}
.folder-tabs::-webkit-scrollbar { display: none; }
.folder-tab {
  flex-shrink: 0;
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-2);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all .12s;
}
.folder-tab:hover { border-color: var(--border-strong); }
.folder-tab.is-active {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}
.folder-tab .count {
  font-size: 11px;
  opacity: .6;
  font-variant-numeric: tabular-nums;
}
.folder-tab.add { color: var(--ink-3); padding: 7px 10px; }

.song-list {
  display: flex;
  flex-direction: column;
}
.song-row {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 4px;
  border-bottom: 1px solid var(--border);
  transition: background .1s;
  text-align: left;
  width: 100%;
}
.song-row:hover { background: var(--surface); }
.song-row:last-child { border-bottom: none; }
.song-row .num {
  font-size: 12px;
  color: var(--ink-3);
  font-variant-numeric: tabular-nums;
  width: 22px;
  text-align: right;
  flex-shrink: 0;
}
.song-row .info { flex: 1; min-width: 0; }
.song-row .title {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.song-row .meta {
  font-size: 12.5px;
  color: var(--ink-3);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.song-row .meta .key-tag {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--ink-2);
  font-weight: 500;
  font-size: 11px;
  margin-left: 6px;
  font-variant-numeric: tabular-nums;
}
.song-row .star {
  color: var(--ink-3);
  padding: 6px;
  border-radius: 6px;
  transition: color .12s, background .12s;
}
.song-row .star.is-on { color: var(--gold); }
.song-row .star:hover { background: var(--surface-2); }

.empty-state {
  padding: 60px 20px;
  text-align: center;
  color: var(--ink-3);
}
.empty-state svg { width: 48px; height: 48px; margin: 0 auto 16px; opacity: .5; }
.empty-state h3 { font-size: 15px; font-weight: 600; color: var(--ink-2); margin-bottom: 4px; }
.empty-state p { font-size: 13px; }

/* ===== FAB ===== */
.fab {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 56px;
  height: 56px;
  border-radius: 999px;
  background: var(--ink);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-lg);
  transition: transform .15s ease, box-shadow .15s ease;
  z-index: 30;
}
.fab:hover { transform: translateY(-2px); box-shadow: 0 24px 60px rgba(0,0,0,.18); }
.fab:active { transform: translateY(0); }
.fab svg { width: 24px; height: 24px; }

/* ===== Score view ===== */
.score-view {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 20px 20px 80px;
  width: 100%;
}
.score-header {
  margin-bottom: 24px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--border);
}
.score-header h2 {
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 4px;
}
.score-header .artist {
  font-size: 14px;
  color: var(--ink-2);
}
.score-header .controls {
  margin-top: 14px;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.transpose {
  display: inline-flex;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.transpose button {
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--ink-2);
}
.transpose button:hover { background: var(--surface-2); color: var(--ink); }
.transpose .val {
  padding: 0 12px;
  font-size: 13px;
  font-weight: 600;
  min-width: 56px;
  text-align: center;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  color: var(--ink);
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #fff;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 12.5px;
  color: var(--ink-2);
  font-weight: 500;
}
.chip svg { width: 14px; height: 14px; }

/* ===== Sheet ===== */
.sheet {
  display: flex;
  flex-direction: column;
  gap: 28px;
}
.sheet-line {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  position: relative;
  border-top: 1.5px solid var(--ink);
  /* Bottom is handled by individual measure barlines */
}
.measure {
  position: relative;
  padding: 8px 0 0 10px;
  min-height: 110px;
  display: flex;
  flex-direction: column;
}
/* Left barline (vertical) — first measure: full, others: full as well, this is just the divider */
.measure::before {
  content: '';
  position: absolute;
  left: 0;
  top: -1.5px;
  bottom: 0;
  width: 1.5px;
  background: var(--ink);
  cursor: pointer;
}
.measure::after {
  /* invisible click hit area for the barline */
  content: '';
  position: absolute;
  left: -6px;
  top: 0;
  bottom: 0;
  width: 12px;
  cursor: pointer;
}
.measure[data-bar-left="repeat-start"]::before {
  width: 4px;
  box-shadow: 6px 0 0 0 var(--ink);
}
.measure[data-bar-left="repeat-start"]::after {
  /* repeat dots */
  content: '';
  position: absolute;
  left: 14px;
  top: 50%;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--ink);
  box-shadow: 0 -8px 0 0 var(--ink);
  transform: translateY(-50%);
}
.measure[data-bar-left="double"]::before {
  width: 1.5px;
  box-shadow: 3px 0 0 0 var(--ink);
}

/* The last measure of each line gets a right-end barline */
.sheet-line .measure:last-child {
  border-right: 1.5px solid var(--ink);
}
.sheet-line .measure:last-child[data-bar-right="repeat-end"] {
  border-right: 4px solid var(--ink);
  box-shadow: inset -8px 0 0 -6.5px var(--ink);
}
.sheet-line .measure:last-child[data-bar-right="final"] {
  border-right: 4px solid var(--ink);
  box-shadow: inset -8px 0 0 -6.5px var(--ink);
}
.sheet-line .measure:last-child[data-bar-right="double"] {
  border-right: 1.5px solid var(--ink);
  box-shadow: inset -4px 0 0 -2.5px var(--ink);
}

.measure-top {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  flex: 0 0 auto;
  min-height: 56px;
  padding-right: 4px;
  flex-wrap: wrap;
}
.chord-cluster {
  display: inline-flex;
  align-items: center;
  gap: 3px;
}
.chord-name {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
  line-height: 1;
  margin-right: 1px;
}

.time-sig {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  font-weight: 700;
  font-size: 16px;
  line-height: 1;
  margin-right: 6px;
  padding-top: 2px;
  font-variant-numeric: tabular-nums;
}
.time-sig span { display: block; }

.measure-bottom {
  margin-top: auto;
  padding: 6px 4px 8px 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.lyric-line {
  font-size: 14.5px;
  line-height: 1.45;
  letter-spacing: -0.01em;
  color: var(--ink);
  white-space: pre-wrap;
  word-break: keep-all;
  min-height: 1.45em;
}
.lyric-line.placeholder { color: var(--ink-3); font-style: italic; }

/* Repeat ending bracket (volta) */
.measure[data-volta]::before { /* unchanged */ }
.measure[data-volta]::before {
  /* still standard barline */
}
.measure[data-volta] .volta {
  position: absolute;
  top: -18px;
  left: 0;
  right: 0;
  height: 14px;
  border-top: 1.5px solid var(--ink);
  border-left: 1.5px solid var(--ink);
  font-size: 11px;
  font-weight: 600;
  padding-left: 4px;
  line-height: 14px;
}

/* End-of-sheet final bar visual (for the very last measure in the song) */
.measure[data-is-final="true"] {
  border-right: 4px solid var(--ink) !important;
  box-shadow: inset -8px 0 0 -6.5px var(--ink) !important;
}

/* Verse add */
.verse-add {
  margin-top: 12px;
  display: flex;
  justify-content: flex-end;
}
.verse-add button {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--ink-3);
  padding: 4px 8px;
  border-radius: 6px;
  border: 1px dashed var(--border-strong);
}
.verse-add button:hover { color: var(--ink); border-color: var(--ink); }

/* ===== Editor (modal) ===== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(24,24,27,.4);
  z-index: 50;
  display: flex;
  align-items: stretch;
  justify-content: center;
  padding: 0;
  animation: fadeIn .15s ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.modal {
  background: #fff;
  width: 100%;
  max-width: 720px;
  border-radius: 0;
  display: flex;
  flex-direction: column;
  max-height: 100vh;
  animation: slideUp .2s ease;
}
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
@media (min-width: 720px) {
  .modal-backdrop { align-items: center; padding: 20px; }
  .modal { border-radius: 14px; max-height: calc(100vh - 40px); }
}
.modal-header {
  display: flex;
  align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}
.modal-header h3 { font-size: 16px; font-weight: 700; flex: 1; }
.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}
.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
.btn {
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 600;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--ink);
  transition: background .1s, border-color .1s;
}
.btn:hover { background: var(--surface); }
.btn.primary { background: var(--ink); color: #fff; border-color: var(--ink); }
.btn.primary:hover { background: #000; }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: #fef2f2; }
.btn:disabled { opacity: .4; cursor: not-allowed; }

.field { margin-bottom: 16px; }
.field label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-2);
  margin-bottom: 6px;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}
.field label .hint {
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  color: var(--ink-3);
  margin-left: 6px;
  font-size: 11.5px;
}
.field input[type="text"],
.field select,
.field textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  background: #fff;
  transition: border-color .12s;
}
.field input[type="text"]:focus,
.field select:focus,
.field textarea:focus {
  border-color: var(--ink);
}
.field textarea {
  resize: vertical;
  min-height: 80px;
  font-family: 'Pretendard', monospace;
  font-feature-settings: "tnum";
  line-height: 1.6;
}
.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.verse-block { position: relative; margin-bottom: 12px; }
.verse-block .verse-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}
.verse-block .remove-verse {
  font-size: 11px;
  color: var(--ink-3);
  padding: 2px 6px;
  border-radius: 4px;
}
.verse-block .remove-verse:hover { color: var(--danger); background: #fef2f2; }

.add-verse-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12.5px;
  color: var(--ink-2);
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px dashed var(--border-strong);
  background: #fff;
  font-weight: 500;
}
.add-verse-btn:hover { background: var(--surface); color: var(--ink); border-color: var(--ink); }

/* Barline picker popover */
.bar-popover {
  position: absolute;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-md);
  z-index: 40;
  padding: 6px;
  display: grid;
  grid-template-columns: repeat(3, 56px);
  gap: 2px;
}
.bar-popover button {
  width: 56px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  transition: background .1s;
}
.bar-popover button:hover { background: var(--surface-2); }
.bar-popover button.is-active { background: var(--surface-2); }
.bar-popover .bar-label {
  grid-column: 1 / -1;
  font-size: 11px;
  color: var(--ink-3);
  padding: 4px 6px 2px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  z-index: 60;
  animation: toastIn .2s ease;
  box-shadow: var(--shadow-lg);
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* Folder editor menu */
.dropdown {
  position: absolute;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-md);
  z-index: 40;
  padding: 4px;
  min-width: 180px;
}
.dropdown button {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 13px;
  text-align: left;
}
.dropdown button:hover { background: var(--surface-2); }
.dropdown button.danger { color: var(--danger); }
.dropdown button svg { width: 14px; height: 14px; opacity: .7; }
.dropdown hr { border: none; border-top: 1px solid var(--border); margin: 4px 0; }

/* Mobile tweaks */
@media (max-width: 600px) {
  /* Sheet line column count is driven inline by JS (perLine). */
  .topbar-inner { padding: 12px 16px; }
  .home { padding: 16px 16px 100px; }
  .score-view { padding: 16px 16px 60px; }
  .score-header h2 { font-size: 20px; }
  .fab { right: 16px; bottom: 16px; }
}

/* Print */
@media print {
  .topbar, .fab, .score-header .controls, .verse-add { display: none !important; }
  body { background: #fff; }
}
