:root {
  --ink: #2b1230;
  --paper: #fff8ec;
  --blue: #1f62d8;
  --yellow: #ffd23a;
  --coral: #ff5a5f;
  --green: #2bb673;
  --shadow: 0 6px 24px rgba(43, 18, 48, 0.25);
  --radius: 18px;
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #ffb27a; }
canvas#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }

.screen { position: fixed; inset: 0; display: none; }
.screen.is-active { display: flex; }
.overlay { background: rgba(43, 18, 48, 0.45); align-items: center; justify-content: center; backdrop-filter: blur(3px); }

button { font: inherit; cursor: pointer; }
.btn {
  display: block; width: 100%; margin-top: 10px; padding: 12px 18px; border: 0; border-radius: 999px;
  background: #fff; color: var(--ink); font-weight: 700; font-size: 17px; box-shadow: 0 3px 0 rgba(0,0,0,0.15);
}
.btn.primary { background: var(--blue); color: #fff; }
.btn:hover { filter: brightness(1.06); }
.btn:active { transform: translateY(2px); box-shadow: none; }
:focus-visible { outline: 3px solid var(--yellow); outline-offset: 2px; }

/* ---------- menu ---------- */
#menu { align-items: flex-start; justify-content: flex-start; padding: 24px; pointer-events: none; }
.menu-card {
  pointer-events: auto; width: min(460px, 100%); max-height: calc(100% - 40px); display: flex; flex-direction: column;
  background: rgba(255, 248, 236, 0.93); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px 20px 12px;
}
.logo {
  margin: 0; font-size: 44px; line-height: 1; letter-spacing: -1px; color: var(--blue);
  font-family: "Arial Black", Impact, sans-serif; text-shadow: 3px 3px 0 var(--yellow);
}
.logo span { color: var(--coral); }
.tag { margin: 8px 0 14px; font-size: 15px; }
.settings { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 10px; font-size: 13px; font-weight: 700; }
.settings label { display: flex; flex-direction: column; gap: 4px; }
.settings select { font: inherit; font-weight: 400; font-size: 15px; padding: 7px 8px; border-radius: 10px; border: 2px solid #e5d6c0; background: #fff; }
.seg { grid-column: 1 / -1; display: flex; background: #efe2cf; border-radius: 999px; padding: 3px; }
.seg button { flex: 1; border: 0; border-radius: 999px; padding: 8px; background: transparent; font-weight: 700; }
.seg button[aria-checked="true"] { background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,0.15); }
.msg { margin: 10px 0 0; padding: 8px 12px; border-radius: 10px; background: #ffe1df; font-size: 14px; }
.tabs { display: flex; gap: 6px; margin: 14px 0 8px; flex-wrap: wrap; }
.tabs button { border: 0; border-radius: 999px; padding: 6px 12px; background: #efe2cf; font-weight: 700; font-size: 14px; }
.tabs button[aria-selected="true"] { background: var(--ink); color: #fff; }
.songs { list-style: none; margin: 0; padding: 0 2px 0 0; overflow-y: auto; }
.songs li { margin: 0 0 6px; }
.song {
  width: 100%; display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 2px 10px; text-align: left;
  border: 0; border-radius: 14px; padding: 10px 14px; background: #fff; box-shadow: 0 2px 0 rgba(0,0,0,0.08);
}
.song:hover { background: #fff3d6; }
.song .t { font-weight: 800; font-size: 16px; }
.song .o { grid-column: 1; font-size: 12px; opacity: 0.7; }
.song .lv { grid-row: 1 / 3; grid-column: 2; text-align: right; font-size: 12px; font-weight: 700; }
.song .lv .s { display: block; color: #e0a000; font-size: 15px; letter-spacing: 1px; }
.song .lv .s i { font-style: normal; color: #e3d5bf; }
.credit { position: fixed; right: 18px; bottom: 12px; margin: 0; font-size: 13px; color: #fff; text-shadow: 0 1px 3px rgba(0,0,0,0.5); }

/* ---------- HUD ---------- */
#play { flex-direction: column; justify-content: space-between; pointer-events: none; padding: 14px; padding-bottom: max(14px, env(safe-area-inset-bottom)); }
.hud-top, .hud-bottom { display: flex; gap: 12px; align-items: flex-start; }
.hud-top > *, .hud-bottom > * { pointer-events: auto; }
.score-box, .song-box, .tourists, .note-card, .heard-card {
  background: rgba(255, 248, 236, 0.9); border-radius: 16px; box-shadow: var(--shadow); padding: 8px 14px;
}
.score { font: 900 30px "Arial Black", Impact, sans-serif; line-height: 1; font-variant-numeric: tabular-nums; }
.mult { font-weight: 800; color: var(--coral); transition: transform .15s; }
.mult.bump { transform: scale(1.5); }
.song-box { flex: 1; min-width: 0; }
.hud-title { font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.progress { height: 8px; margin-top: 6px; background: #efe2cf; border-radius: 99px; overflow: hidden; }
.progress i { display: block; height: 100%; width: 0; background: var(--blue); border-radius: 99px; }
.tourists { display: flex; gap: 2px; font-size: 20px; }
.tourists span { transition: transform .2s, opacity .2s; }
.tourists .fall { opacity: 0.45; transform: rotate(160deg); }
.icon-btn { width: 46px; height: 46px; border: 0; border-radius: 50%; background: rgba(255, 248, 236, 0.9); box-shadow: var(--shadow); font-weight: 900; }
.label { font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.6px; opacity: 0.65; }
.note-card { display: grid; grid-template-columns: auto auto; gap: 0 12px; align-items: center; }
.note-card .label { grid-column: 1 / -1; }
.big-note { font: 900 46px "Arial Black", Impact, sans-serif; line-height: 1.05; min-width: 64px; }
.finger svg { height: 92px; width: auto; display: block; }
.heard-card { min-width: 150px; }
.heard { font: 800 26px "Arial Black", Impact, sans-serif; }
.heard.ok { color: var(--green); }
.heard.off { color: var(--coral); }
.meter { position: relative; height: 10px; margin-top: 6px; border-radius: 99px; background: linear-gradient(90deg, #ff8a80, #ffe082 35%, #69f0ae 45%, #69f0ae 55%, #ffe082 65%, #ff8a80); }
.meter i { position: absolute; top: -4px; left: 50%; width: 4px; height: 18px; margin-left: -2px; background: var(--ink); border-radius: 2px; transition: left .06s linear; }
.keys { margin-left: auto; display: flex; gap: 6px; align-self: flex-end; flex-wrap: wrap; justify-content: flex-end; max-width: 50%; }
.keys button {
  min-width: 54px; height: 62px; border: 0; border-radius: 12px; color: #fff; font-weight: 900; font-size: 18px; box-shadow: 0 4px 0 rgba(0,0,0,0.25);
  display: flex; flex-direction: column; align-items: center; justify-content: center; touch-action: none; user-select: none;
}
.keys button small { font-size: 11px; opacity: 0.85; }
.keys button.down { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,0.25); filter: brightness(1.15); }
.countin { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none;
  font: 900 140px "Arial Black", Impact, sans-serif; color: #fff; text-shadow: 0 6px 0 var(--coral), 0 10px 30px rgba(0,0,0,0.3); }
.countin:empty { display: none; }
.toast { position: absolute; left: 50%; top: 22%; transform: translateX(-50%); pointer-events: none;
  font: 900 34px "Arial Black", Impact, sans-serif; color: var(--yellow); text-shadow: 0 3px 0 var(--ink), 0 0 20px rgba(0,0,0,0.3);
  opacity: 0; transition: opacity .25s, transform .25s; white-space: nowrap; }
.toast.show { opacity: 1; transform: translateX(-50%) scale(1.08); }

/* ---------- panels ---------- */
.panel { width: min(380px, calc(100% - 32px)); background: var(--paper); border-radius: var(--radius); box-shadow: var(--shadow); padding: 22px; text-align: center; }
.panel h2 { margin: 0 0 6px; font-family: "Arial Black", Impact, sans-serif; }
.stars { font-size: 52px; color: #e0a000; letter-spacing: 4px; }
.stars .off { color: #d9cbb5; }
.res-line { margin: 4px 0 12px; font-weight: 700; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 0 0 8px; }
.stats div { background: #fff; border-radius: 12px; padding: 8px; }
.stats dt { font-size: 12px; opacity: 0.7; }
.stats dd { margin: 0; font-weight: 900; font-size: 20px; }
.best { margin: 4px 0; font-weight: 900; color: var(--coral); }

@media (max-width: 720px) {
  #menu { padding: 12px; }
  .logo { font-size: 34px; }
  .hud-bottom { flex-wrap: wrap; }
  .keys { max-width: 100%; margin-left: 0; }
  .finger svg { height: 64px; }
  .big-note { font-size: 34px; }
  .tourists { font-size: 15px; }
  .heard-card { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .toast, .mult, .tourists span { transition: none; }
}

/* Fingering chart (SVG from js/lib/fingering.js) */
.recorder { display: block; }
.r-body { fill: #e9c98f; stroke: #a87d45; stroke-width: 1.5; }
.r-window { fill: #2b1230; }
.r-joint { stroke: #a87d45; stroke-width: 2; }
.r-thumb-pad { fill: #e9c98f; stroke: #a87d45; stroke-width: 1.5; }
.r-thumb-link { stroke: #e9c98f; stroke-width: 2; stroke-dasharray: 3 3; }
.r-label { fill: #7a6a5a; font: 600 12px system-ui, sans-serif; }
.r-missing { fill: #7a6a5a; font: 700 48px system-ui, sans-serif; }
.h-closed { fill: #2b1230; }
.h-open { fill: #fff8ec; stroke: #a87d45; stroke-width: 2; }

/* ---------- song sources ---------- */
.btn.small { display: inline-block; width: auto; margin: 0; padding: 8px 14px; font-size: 14px; }
.btn.danger { background: #ffe1df; color: #b3261e; }
.add-row { padding-top: 8px; }
.add-row .btn { width: 100%; }
.shared { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center; margin-top: 10px; padding: 10px 12px; border-radius: 14px; background: #fff3d6; }
.shared strong { display: block; }
.song-row { display: grid; grid-template-columns: 1fr auto; gap: 6px; }
.edit-btn { border: 0; border-radius: 14px; background: #fff; width: 44px; box-shadow: 0 2px 0 rgba(0,0,0,0.08); font-size: 18px; }
dialog { border: 0; border-radius: var(--radius); padding: 0; width: min(560px, calc(100% - 24px)); box-shadow: var(--shadow); background: var(--paper); color: var(--ink); }
dialog::backdrop { background: rgba(43, 18, 48, 0.45); }
.sd { display: flex; flex-direction: column; gap: 10px; padding: 18px 20px; }
.sd header { display: flex; justify-content: space-between; align-items: center; }
.sd h2 { margin: 0; font-family: "Arial Black", Impact, sans-serif; }
.sd label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 700; }
.sd input, .sd select, .sd textarea { font: inherit; font-weight: 400; font-size: 15px; padding: 8px 10px; border-radius: 10px; border: 2px solid #e5d6c0; background: #fff; }
.sd textarea { font-family: ui-monospace, Consolas, monospace; resize: vertical; }
.sd-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.sd-row[hidden] { display: none; }
.sd-file { display: flex; align-items: center; gap: 10px; }
.file-btn { cursor: pointer; flex-direction: row !important; }
.hint { margin: 0; font-size: 12px; opacity: 0.75; }
.sd-status { margin: 0; min-height: 1.2em; font-size: 14px; word-break: break-all; }
.sd-status[data-kind="error"] { color: #b3261e; }
.sd-status[data-kind="warn"] { color: #8a5a00; }
.sd-status[data-kind="ok"] { color: #1d7a4a; }
.sd-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
.sd .icon-btn { box-shadow: none; width: 38px; height: 38px; }

[hidden] { display: none !important; }

.add-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.btn.record.on { background: var(--coral); color: #fff; animation: pulse 1s infinite; }
@keyframes pulse { 50% { filter: brightness(1.2); } }
.sd-file { flex-wrap: wrap; }
.settings label.wide { grid-column: 1 / -1; }

/* ---------- settings + tuner ---------- */
.title-row { display: flex; justify-content: space-between; align-items: center; }
.sd fieldset { border: 2px solid #efe2cf; border-radius: 14px; padding: 8px 12px 12px; display: flex; flex-direction: column; gap: 8px; }
.sd legend { font-weight: 800; padding: 0 6px; }
.sd label output { font-weight: 400; opacity: 0.75; margin-left: 6px; }
.sd label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 600; }
.sd input[type=range] { padding: 0; accent-color: var(--blue); }
#settings-dialog { max-height: calc(100% - 24px); }
.tu-row { display: flex; gap: 14px; align-items: center; }
.tu-note { font: 900 52px "Arial Black", Impact, sans-serif; min-width: 90px; text-align: center; }
.tu-note.ok { color: var(--green); }
.tu-note.off { color: var(--coral); }
.tu-side { flex: 1; display: flex; flex-direction: column; gap: 8px; }
.level { position: relative; height: 8px; border-radius: 99px; background: #efe2cf; overflow: hidden; }
.level i { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: var(--blue); }
.level b { position: absolute; top: -2px; bottom: -2px; width: 3px; background: var(--coral); }
.score-box.bump .score { transform: scale(1.12); }
.score { transition: transform .12s ease-out; display: inline-block; transform-origin: left center; }

/* ---------- polish ---------- */
#menu { padding: max(24px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(24px, env(safe-area-inset-left)); }
#play { padding-top: max(14px, env(safe-area-inset-top)); padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right)); }
.howto { margin: -4px 0 12px; font-size: 14px; background: #fff3d6; border-radius: 12px; padding: 6px 12px; }
.howto summary { cursor: pointer; font-weight: 800; }
.howto ol { margin: 6px 0 4px; padding-left: 20px; }
.howto li { margin: 3px 0; }
kbd { font: 700 12px ui-monospace, monospace; background: #fff; border: 1px solid #d8c8b0; border-radius: 4px; padding: 0 4px; }
.keys.compact button { min-width: 42px; height: 52px; font-size: 15px; }
.song:focus-visible { outline: 3px solid var(--blue); }
@media (max-width: 720px) {
  .credit { display: none; }
  .menu-card { max-height: calc(100% - 24px); }
  .keys button { min-width: 40px; height: 54px; font-size: 15px; }
  .note-card { padding: 6px 10px; }
  .hud-top { gap: 6px; }
  .score { font-size: 22px; }
  .song-box { display: none; }
}
