:root {
  --bg: #f7f7f5;
  --fg: #1a1a1a;
  --muted: #666;
  --accent: #2563eb;
  --danger: #b91c1c;
  --overlay-bg: #ffffff;
  --overlay-fg: #1a1a1a;
  --card: #fff;
  --border: #e5e5e5;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Pretendard", "Apple SD Gothic Neo", sans-serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.5;
}

/* Login screen */
.login-pane {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 28px 22px;
  box-shadow: 0 1px 3px rgba(0,0,0,.05);
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; text-align: center; }
.login-card > .muted { text-align: center; margin: 0 0 16px; font-size: 13px; }
.login-card form { flex-direction: column; align-items: stretch; gap: 10px; }
.login-card label { display: block; font-size: 13px; }
.login-card input { width: 100%; padding: 8px 10px; margin-top: 4px; font-size: 14px; }
.login-card button[type=submit] { width: 100%; padding: 10px; font-size: 14px; margin-top: 4px; }
.login-card > button { width: 100%; padding: 10px; margin-top: 8px; font-size: 14px; }
#google-signin-button { display: flex; justify-content: center; margin: 14px 0 8px; min-height: 44px; }
.local-login { margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); }

.subscription-dialog { width: min(380px, 92vw); }
.sub-info {
  display: grid;
  grid-template-columns: 90px 1fr;
  gap: 6px 12px;
  margin: 12px 0;
  font-size: 13px;
}
.sub-info dt { color: var(--muted); }
.sub-info dd { margin: 0; font-weight: 600; }

.paywall-dialog { width: min(400px, 92vw); }
.paywall-dialog h3 { margin: 0 0 8px; }
.paywall-features {
  margin: 12px 0;
  padding-left: 20px;
  font-size: 13px;
  color: var(--fg);
}
.paywall-features li { margin: 3px 0; }
.paywall-price {
  text-align: center;
  font-size: 16px;
  margin: 12px 0 8px;
  padding: 10px;
  background: #f0f4ff;
  border-radius: 6px;
}
.paywall-price strong { font-size: 22px; color: var(--accent); }

#logout-link { color: var(--accent); text-decoration: none; }
#logout-link:hover { text-decoration: underline; }

.key-badge {
  background: #f0f4ff;
  border: 1px solid #d1ddff;
  color: var(--accent);
  padding: 3px 12px;
  border-radius: 6px;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  margin: 0 4px;
  font-family: inherit;
}
.key-badge:hover { background: #e0eaff; border-color: var(--accent); }
#edit-orig-key { text-decoration: none; }
#edit-orig-key:hover { text-decoration: underline; }

.app-header {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 12px;
  padding: 10px 20px 0;
  font-size: 13px;
}
.app-header a { color: var(--accent); text-decoration: none; }
.app-header a:hover { text-decoration: underline; }

.tier-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
}
.tier-badge.tier-free  { background: #f3f4f6; color: var(--muted); }
.tier-badge.tier-paid  { background: #f0f4ff; color: var(--accent); }
.tier-badge.tier-admin { background: #fff4d6; color: #92400e; }

/* Admin dialog */
.admin-dialog {
  width: min(960px, 96vw);
  max-height: 90vh;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}
.admin-dialog::backdrop { background: rgba(0,0,0,.4); }
.admin-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: #fafafa;
}
.admin-header h3 { margin: 0; font-size: 16px; }
.admin-close {
  background: none; border: none; cursor: pointer; font-size: 22px;
  color: var(--muted); padding: 0 4px; line-height: 1;
}
.admin-close:hover { color: var(--fg); }
.admin-body { padding: 16px 20px; overflow-y: auto; max-height: calc(90vh - 60px); }
.admin-tablewrap { overflow-x: auto; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.admin-table th, .admin-table td {
  text-align: left; padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
.admin-table th {
  font-weight: 600; color: var(--muted); font-size: 12px;
  background: #fafafa; border-bottom: 2px solid var(--border);
}
.admin-table td.exp-expired { color: var(--danger); }
.admin-table td.exp-active { color: #15803d; font-weight: 600; }
.admin-grant-cell { display: flex; gap: 4px; flex-wrap: wrap; }
.admin-grant-cell button {
  font-size: 11px; padding: 3px 8px;
  background: #fff; color: var(--fg);
  border: 1px solid var(--border); border-radius: 4px;
  cursor: pointer;
}
.admin-grant-cell button:hover { background: #f0f4ff; border-color: var(--accent); }
.admin-grant-cell button.danger { color: var(--danger); border-color: #f3c8c8; }
.admin-role-pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
}
.admin-role-pill.admin { background: #fff4d6; color: #92400e; }
.admin-role-pill.user { background: #f0f4ff; color: var(--accent); }
.admin-role-toggle {
  background: none; border: none; cursor: pointer;
  font-size: 11px; color: var(--muted); margin-left: 4px;
  text-decoration: underline;
}

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px;
  display: grid;
  gap: 16px;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px 22px;
}

.card h2 { margin-top: 0; font-size: 18px; }
.card h3 { font-size: 14px; margin: 14px 0 6px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.hidden { display: none !important; }
hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }

form { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
input[type=file] { flex: 1; min-width: 200px; }
input[type=text] {
  padding: 6px 10px; font-size: 14px;
  border: 1px solid var(--border); border-radius: 6px;
  background: #fff;
}

button {
  padding: 7px 14px;
  background: var(--accent);
  color: #fff; border: none; border-radius: 6px;
  cursor: pointer; font-size: 14px;
}
button:hover { opacity: 0.92; }
button.ghost { background: #fff; color: var(--fg); border: 1px solid var(--border); }
button.ghost:hover { background: #f3f3f3; }
button.ghost.danger { color: var(--danger); border-color: #f3c8c8; }
button.ghost.danger:hover { background: #fef2f2; }

select {
  padding: 5px 8px; font-size: 14px;
  border: 1px solid var(--border); border-radius: 6px;
  background: #fff;
}

.library-controls { margin: 8px 0 12px; }
.library-controls input[type=search] {
  width: 100%;
  padding: 7px 12px;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
}
.library-tablewrap { overflow-x: auto; }
.library-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.library-table th,
.library-table td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
.library-table th {
  background: #fafafa;
  cursor: pointer;
  user-select: none;
  font-weight: 600;
  font-size: 12px;
  color: var(--muted);
  border-bottom: 2px solid var(--border);
}
.library-table th:hover { color: var(--fg); }
.library-table th.sort-asc::after  { content: " ▲"; font-size: 10px; }
.library-table th.sort-desc::after { content: " ▼"; font-size: 10px; }
.library-table tbody tr {
  cursor: pointer;
}
.library-table tbody tr:hover { background: #f0f4ff; }
.library-table td.title-col { font-weight: 600; }
.library-table td.date-col,
.library-table th.date-col { white-space: nowrap; color: var(--muted); font-variant-numeric: tabular-nums; }
.library-table td.key-col,
.library-table th.key-col { text-align: center; }
.library-table .key-badge-sm {
  display: inline-block;
  background: #f0f4ff;
  color: var(--accent);
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.fav-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 3px 8px;
  font-size: 14px;
  border-radius: 6px;
  margin-left: 4px;
  cursor: pointer;
}
.fav-btn:hover { color: var(--fg); }
.fav-btn.active {
  color: #f59e0b;
  border-color: #f59e0b;
  background: #fff8e6;
}

.score-header {
  display: flex; justify-content: space-between; align-items: flex-start;
  gap: 16px; flex-wrap: wrap; margin-bottom: 14px;
}
.score-header h2 { margin: 0; font-size: 22px; }
.meta { display: flex; gap: 14px; flex-wrap: wrap; color: var(--muted); font-size: 13px; margin-top: 6px; align-items: center; }
.meta label { display: inline-flex; gap: 6px; align-items: center; }
.meta strong { color: var(--fg); }
.actions { display: flex; gap: 6px; }

.hint { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
.status { font-size: 13px; color: var(--muted); min-height: 1.3em; }
.status.error { color: var(--danger); }
.status.ok { color: #15803d; }

/* PDF + overlay stack */
.pdf-stack { display: flex; flex-direction: column; gap: 14px; align-items: center; }
.page-wrapper {
  position: relative;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 1px 3px rgba(0,0,0,.05);
  overflow: hidden;
  max-width: 100%;
}
.page-wrapper img {
  display: block;
  width: 100%;
  height: auto;
}
.chord-overlay {
  position: absolute;
  background: var(--overlay-bg);
  color: var(--overlay-fg);
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
  font-weight: 600;
  text-align: center;
  line-height: 1;
  white-space: nowrap;
  /* Always size to fit the (possibly transposed) text so we never leak the
     original chord behind a too-narrow overlay box. min-width still anchors
     the box at the original chord's position. */
  width: max-content;
  border-radius: 2px;
  padding: 1px 4px;
  cursor: pointer;
  box-shadow: 0 0 0 1px rgba(37, 99, 235, .25);
  user-select: none;
}
.chord-overlay:hover {
  box-shadow: 0 0 0 1.5px var(--accent);
  outline: none;
}
/* Transposed (display key differs from original key) — accent blue text */
.chord-overlay.changed { color: var(--accent); }

/* User-edited — amber background + dark text, distinct from transpose blue */
.chord-overlay.edited {
  background: #fff4d6;
  color: #92400e;
  box-shadow: 0 0 0 1.5px #f59e0b;
}
.chord-overlay.edited.changed { color: #92400e; }  /* edited wins over changed */

/* Performance mode — fullscreen, up to 2 pages side-by-side */
.perf-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: #fff;
  overflow: auto;
  padding: 12px;
}
.perf-stack {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));
  gap: 12px;
  height: 100%;
}
.perf-stack .page-wrapper {
  margin: 0 auto;
  max-height: 96vh;
  max-width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.perf-stack .page-wrapper img {
  max-height: 96vh;
  width: auto;
  max-width: 100%;
}
.perf-close {
  position: fixed;
  top: 10px;
  right: 14px;
  z-index: 10000;
  width: 38px;
  height: 38px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 50%;
  font-size: 22px;
  line-height: 1;
  color: var(--fg);
  cursor: pointer;
  padding: 0;
  box-shadow: 0 1px 4px rgba(0,0,0,.1);
}
.perf-close:hover { background: #f3f3f3; }

dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px 24px;
  min-width: 320px;
}
dialog::backdrop { background: rgba(0,0,0,.3); }
dialog h3 { margin-top: 0; }
dialog label { display: block; margin: 8px 0; font-size: 13px; }
dialog input { width: 100%; margin-top: 4px; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }
