:root {
  --ink: #0e0b09;
  --lacquer: #16110d;
  --lacquer-2: #1e1712;
  --ivory: #f4ecdc;
  --ivory-dim: #cfc4b0;
  --muted: #8f8371;
  --gold: #d9b26a;
  --gold-hi: #f1d595;
  --line: rgba(244, 236, 220, 0.08);
  --crimson: #d2504a;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--ink); color: var(--ivory); font-family: var(--sans); }
body { overscroll-behavior: none; -webkit-font-smoothing: antialiased; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
button:focus-visible, a:focus-visible, input:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
a { color: var(--gold-hi); }
[hidden] { display: none !important; }

#app { position: fixed; inset: 0; overflow: hidden;
  background:
    radial-gradient(120% 60% at 50% -10%, rgba(217, 178, 106, 0.13), transparent 60%),
    radial-gradient(80% 50% at 100% 100%, rgba(120, 60, 30, 0.12), transparent 70%),
    linear-gradient(180deg, var(--lacquer) 0%, var(--ink) 100%);
}

.screen { position: absolute; inset: 0; display: none; }
.screen.active { display: flex; flex-direction: column; }

/* ---------- Library ---------- */
#library { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: calc(var(--safe-t) + 10px) 0 calc(var(--safe-b) + 24px); align-items: center; }
#library > * { width: 100%; max-width: 640px; padding-left: 16px; padding-right: 16px; }

.lib-head { display: flex; align-items: center; gap: 12px; padding-top: 8px; padding-bottom: 6px; }
.brand { flex: 1; text-align: center; }
.brand h1 { margin: 0; font-family: var(--serif); font-weight: 400; font-size: 40px; letter-spacing: 0.06em; line-height: 1;
  background: linear-gradient(180deg, #fff6e4 0%, var(--gold) 120%); -webkit-background-clip: text; background-clip: text; color: transparent; }
.brand p { margin: 6px 0 0; font-size: 12px; letter-spacing: clamp(0.08em, 0.6vw, 0.22em); white-space: nowrap; text-transform: uppercase; color: var(--muted); }
.back, .icon-btn { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; color: var(--ivory-dim);
  background: rgba(244, 236, 220, 0.05); border: 1px solid var(--line); text-decoration: none; flex: none; transition: background .15s, transform .1s; }
.back:active, .icon-btn:active { transform: scale(0.94); background: rgba(244, 236, 220, 0.1); }

.controls { position: sticky; top: calc(-1 * var(--safe-t) - 10px); z-index: 2; padding-top: 14px; padding-bottom: 12px;
  background: rgba(18, 14, 11, 0.82); backdrop-filter: blur(16px) saturate(1.2); -webkit-backdrop-filter: blur(16px) saturate(1.2); }
#library > .controls { max-width: none; padding-left: max(16px, calc(50% - 304px)); padding-right: max(16px, calc(50% - 304px)); border-bottom: 1px solid var(--line); }
.seg { position: relative; display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); padding: 4px; border-radius: 14px;
  background: rgba(0, 0, 0, 0.35); border: 1px solid var(--line); }
.seg button { position: relative; z-index: 1; height: 38px; border-radius: 10px; font-size: clamp(12px, 3.3vw, 15px); min-width: 0; padding: 0; color: var(--muted); font-weight: 500; transition: color .2s; }
.seg button[aria-checked="true"] { color: var(--ink); }
.seg-thumb { position: absolute; z-index: 0; top: 4px; bottom: 4px; left: 4px; border-radius: 10px;
  background: linear-gradient(180deg, #fbf4e6, #e6d8bd); box-shadow: 0 2px 10px rgba(0, 0, 0, 0.4), inset 0 -1px 0 rgba(120, 90, 40, 0.25);
  transition: transform .28s cubic-bezier(.3, 1.3, .5, 1), width .28s; }
.seg.small button { height: 32px; font-size: 13px; }
.row2 { display: flex; gap: 10px; margin-top: 10px; }
.row2 .seg { flex: 1; }
.shuffle { flex: none; display: flex; align-items: center; gap: 8px; padding: 0 18px; border-radius: 14px; font-weight: 600; font-size: 14px;
  color: var(--ink); background: linear-gradient(180deg, var(--gold-hi), var(--gold)); box-shadow: 0 6px 18px rgba(217, 178, 106, 0.25), inset 0 1px 0 rgba(255,255,255,.5);
  transition: transform .1s; }
.shuffle:active { transform: scale(0.96); }

.search { margin-top: 10px; display: flex; align-items: center; gap: 8px; padding: 0 12px; height: 42px; border-radius: 12px;
  background: rgba(244, 236, 220, 0.05); border: 1px solid var(--line); color: var(--muted); }
.search input { flex: 1; min-width: 0; background: none; border: 0; outline: none; color: var(--ivory); font-size: 16px; }
.search input::placeholder { color: var(--muted); }
.chips { display: flex; gap: 8px; overflow-x: auto; margin: 10px -16px 0; padding: 0 16px 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips button { flex: none; height: 32px; padding: 0 13px; border-radius: 16px; font-size: 13px; color: var(--ivory-dim);
  border: 1px solid var(--line); background: rgba(244, 236, 220, 0.03); transition: all .15s; }
.chips button.on { color: var(--ink); background: var(--ivory); border-color: var(--ivory); }

.songs { list-style: none; margin: 4px 0 0; padding-top: 0; padding-bottom: 0; }
.song { display: flex; align-items: center; gap: 14px; padding: 12px 4px; border-bottom: 1px solid var(--line); cursor: pointer;
  border-radius: 12px; transition: background .12s, transform .12s; content-visibility: auto; contain-intrinsic-size: 64px; }
.song:active { background: rgba(244, 236, 220, 0.06); transform: scale(0.99); }
@media (hover: hover) { .song:hover { background: rgba(244, 236, 220, 0.04); } }
.badge { flex: none; width: 42px; height: 42px; border-radius: 11px; display: grid; place-items: center; font-family: var(--serif);
  font-size: 17px; color: var(--ink); box-shadow: inset 0 -2px 0 rgba(0,0,0,0.15), 0 2px 8px rgba(0,0,0,0.35); }
.meta { flex: 1; min-width: 0; }
.meta .t { font-family: var(--serif); font-size: 17px; line-height: 1.2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meta .c { margin-top: 3px; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side { flex: none; text-align: right; display: flex; flex-direction: column; align-items: flex-end; gap: 5px; }
.dots { display: flex; gap: 3px; }
.dots i { width: 5px; height: 5px; border-radius: 50%; background: rgba(244, 236, 220, 0.15); }
.dots i.on { background: var(--gold); }
.mini-stars { font-size: 11px; letter-spacing: 1px; color: var(--gold); min-height: 13px; }
.mini-stars .off { color: rgba(244, 236, 220, 0.15); }
.empty { text-align: center; color: var(--muted); padding: 40px 0; }

/* ---------- Game ---------- */
#game { background: transparent; touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
#stage { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.hud { position: absolute; left: 0; right: 0; top: 0; padding-top: var(--safe-t); pointer-events: none; }
.progress { height: 3px; background: rgba(244, 236, 220, 0.06); }
.progress i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--gold), var(--gold-hi)); box-shadow: 0 0 10px var(--gold); }
.hud-row { max-width: 580px; margin: 0 auto; display: flex; align-items: flex-start; justify-content: space-between; padding: 10px 14px 0; }
.lives { width: 72px; display: flex; gap: 4px; padding-top: 10px; }
.lives svg { width: 16px; height: 16px; transition: opacity .3s, transform .3s; }
.lives svg.lost { opacity: 0.18; transform: scale(0.8); }
.scorebox { text-align: center; }
#score { font-family: var(--serif); font-size: 46px; line-height: 1; color: var(--ivory); text-shadow: 0 2px 20px rgba(0, 0, 0, 0.6); font-variant-numeric: tabular-nums; }
#score.bump { animation: bump .18s ease-out; }
@keyframes bump { 40% { transform: scale(1.12); } }
#combo { margin-top: 4px; font-size: 12px; letter-spacing: 0.18em; text-transform: uppercase; color: var(--gold); min-height: 15px; }
.pause { pointer-events: auto; width: 72px; display: flex; justify-content: flex-end; background: none; border: 0; }
.pause svg { width: 40px; height: 40px; padding: 10px; border-radius: 50%; background: rgba(0, 0, 0, 0.3); border: 1px solid var(--line); }

.loading { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 14px; color: var(--muted);
  font-size: 13px; letter-spacing: 0.2em; text-transform: uppercase; background: rgba(14, 11, 9, 0.6); }
.ring svg { width: 56px; height: 56px; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 2.5; stroke: rgba(244, 236, 220, 0.1); }
.ring #ring-fg { stroke: var(--gold); stroke-linecap: round; stroke-dasharray: 119.4; stroke-dashoffset: 119.4; transition: stroke-dashoffset .2s; }

/* ---------- Sheets ---------- */
.sheet-wrap { position: absolute; inset: 0; z-index: 10; display: flex; align-items: flex-end; justify-content: center;
  background: rgba(8, 6, 5, 0.55); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); animation: fade .2s ease-out; }
.sheet { width: 100%; max-width: 520px; max-height: 92%; overflow-y: auto; padding: 26px 22px calc(var(--safe-b) + 20px); border-radius: 26px 26px 0 0; text-align: center;
  background: linear-gradient(180deg, #241c16, #17120e); border: 1px solid rgba(244, 236, 220, 0.1); border-bottom: 0;
  box-shadow: 0 -20px 60px rgba(0, 0, 0, 0.6); animation: rise .32s cubic-bezier(.2, 1.1, .4, 1); }
@media (min-width: 600px) { .sheet-wrap { align-items: center; } .sheet { border-radius: 26px; border-bottom: 1px solid rgba(244,236,220,.1); } }
.sheet.tall { text-align: left; }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
.eyebrow { margin: 0; font-size: 11px; letter-spacing: 0.24em; text-transform: uppercase; color: var(--gold); }
.sheet h2 { margin: 8px 0 0; font-family: var(--serif); font-weight: 400; font-size: 26px; line-height: 1.2; }
.composer { margin: 6px 0 0; color: var(--muted); font-size: 14px; }
.stars { display: flex; justify-content: center; gap: 10px; margin: 18px 0 6px; }
.stars svg { width: 38px; height: 38px; }
.stars svg path { fill: rgba(244, 236, 220, 0.1); }
.stars svg.on path { fill: var(--gold); }
.stars svg.on { filter: drop-shadow(0 0 8px rgba(217, 178, 106, 0.55)); }
.stars svg.on { overflow: visible; animation: pop .45s cubic-bezier(.3, 1.6, .5, 1) both; }
.stars svg.on:nth-child(2) { animation-delay: .15s; }
.stars svg.on:nth-child(3) { animation-delay: .3s; }
@keyframes pop { from { transform: scale(0.2); opacity: 0; } }
.big-score { font-family: var(--serif); font-size: 56px; line-height: 1.05; font-variant-numeric: tabular-nums; }
.best { margin: 2px 0 0; color: var(--muted); font-size: 13px; min-height: 16px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 18px 0 0; }
.stats div { padding: 10px 4px; border-radius: 12px; background: rgba(244, 236, 220, 0.04); border: 1px solid var(--line); }
.stats dt { font-size: 10px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); }
.stats dd { margin: 4px 0 0; font-size: 18px; font-variant-numeric: tabular-nums; }
.btns { display: grid; gap: 10px; margin-top: 22px; }
.btn { height: 52px; border-radius: 15px; font-size: 16px; font-weight: 600; background: rgba(244, 236, 220, 0.07); border: 1px solid var(--line); transition: transform .1s; }
.btn:active { transform: scale(0.98); }
.btn.primary { color: var(--ink); background: linear-gradient(180deg, #fcf5e7, #e5d6b9); border: 0; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35), inset 0 -2px 0 rgba(120, 90, 40, 0.2); }
.btn.ghost { background: none; border: 0; color: var(--muted); height: 44px; }

.credits-body { margin-top: 12px; font-size: 14px; line-height: 1.55; color: var(--ivory-dim); }
kbd { display: inline-block; min-width: 22px; padding: 1px 6px; border-radius: 5px; border: 1px solid rgba(244, 236, 220, 0.25); font-family: var(--sans); font-size: 12px; text-align: center; }
.credit-list { list-style: none; padding: 0; margin: 14px 0 0; font-size: 12.5px; }
.credit-list li { padding: 8px 0; border-top: 1px solid var(--line); }
.credit-list b { font-weight: 500; color: var(--ivory); }
.credit-list span { color: var(--muted); }
