:root {
  --bg: #f3efe6;
  --surface: #ffffff;
  --surface-soft: #faf8f3;
  --ink: #0f172a;
  --ink-muted: #475569;
  --border: rgba(15, 23, 42, 0.11);
  --accent: #4f46e5;
  --accent-light: #818cf8;
  --danger: #e11d48;
  --radius: 16px;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08), 0 2px 6px rgba(15, 23, 42, 0.04);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: radial-gradient(circle at top left, #faf7f0 0%, var(--bg) 60%, #eae3d2 100%);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Yu Gothic", Meiryo, sans-serif;
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

.app {
  max-width: 1040px;
  margin: 0 auto;
  padding: 16px 16px 32px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: var(--ink);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 22px;
  font-weight: 800;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.2);
}

.brand-title {
  margin: 0;
  font-size: 20px;
  font-weight: 900;
  letter-spacing: 0.06em;
  line-height: 1.15;
}

.brand-sub {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--ink-muted);
}

.audio-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
}

.icon-btn {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 6px;
}

.icon-btn:hover {
  background: rgba(15, 23, 42, 0.06);
}

.volume-slider {
  width: 84px;
  accent-color: var(--accent);
  cursor: pointer;
}

/* Song Selector Bar */
.song-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 2px;
  scrollbar-width: thin;
}

.song-tabs {
  display: flex;
  gap: 8px;
  flex: 1;
}

.song-tab {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  border-radius: 12px;
  padding: 8px 13px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
  font-size: 13px;
  font-weight: 700;
  transition: transform 0.12s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}

.song-tab:hover {
  transform: translateY(-1px);
  border-color: rgba(15, 23, 42, 0.25);
}

.song-tab.active {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.18);
}

.song-tab .dot {
  width: 9px;
  height: 9px;
  border-radius: 999px;
  flex-shrink: 0;
}

.song-tab .stars {
  font-size: 10px;
  opacity: 0.72;
  letter-spacing: 0.04em;
}

.upload-pill {
  border: 1.5px dashed rgba(15, 23, 42, 0.28);
  background: rgba(255, 255, 255, 0.65);
  color: var(--ink-muted);
  border-radius: 12px;
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
  transition: all 0.15s ease;
}

.upload-pill:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: #fff;
}

/* Main Stage */
.stage-wrap {
  position: relative;
}

.stage-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  border-radius: var(--radius);
  overflow: hidden;
  background: #fcfbf7;
  border: 1.5px solid rgba(15, 23, 42, 0.14);
  box-shadow: var(--shadow);
  user-select: none;
  touch-action: manipulation;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: pointer;
}

/* Live Music Notebook & Ink Sketch HUD */
.hud {
  position: absolute;
  top: 10px;
  left: 12px;
  right: 12px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  pointer-events: none;
}

.hud-left-cluster {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.hud-score-assembly {
  display: flex;
  align-items: center;
}

.hud-score-plaque {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 13px 6px 10px;
  background:
    repeating-linear-gradient(180deg, transparent 0 4.5px, rgba(59, 130, 246, 0.14) 4.5px 5.2px),
    #fffdf6;
  border: 1.8px solid #172033;
  border-radius: 3px;
  transform: rotate(-1.1deg);
  box-shadow: 3.5px 3.5px 0 rgba(23, 32, 51, 0.25);
}

.hud-score-plaque::before {
  content: "";
  position: absolute;
  top: -5px;
  left: -8px;
  width: 34px;
  height: 10px;
  background: repeating-linear-gradient(
    -45deg,
    rgba(255, 255, 255, 0.35) 0 2px,
    transparent 2px 5px
  ), color-mix(in srgb, var(--accent, #4f46e5) 45%, transparent);
  transform: rotate(-22deg);
  border: 0.5px solid rgba(23, 32, 51, 0.12);
}

.hud-clef-coin {
  display: none;
}

.hud-score-body {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.hud-engraved-head {
  display: flex;
  align-items: center;
  gap: 4px;
}

.hud-rule {
  display: none;
}

.hud-serif-label {
  font-size: 8.5px;
  font-weight: 900;
  letter-spacing: 0.12em;
  color: #475569;
}

.hud-serif-label::before {
  content: "♪ ";
  color: var(--accent, #4f46e5);
}

.hud-score-num {
  font-family: ui-rounded, "Hiragino Maru Gothic ProN", system-ui, sans-serif;
  font-size: 20px;
  font-weight: 900;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
  color: #172033;
  border-bottom: 2px solid #d93025;
  padding-bottom: 1px;
}

.hud-mini-piano {
  display: none;
}

.hud-time-tab {
  margin-left: -6px;
  padding: 5px 10px 5px 12px;
  background: linear-gradient(135deg, #fef3b0, #fde68a);
  border: 1.5px solid #172033;
  border-radius: 3px;
  transform: rotate(1.8deg);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  box-shadow: 2.5px 2.5px 0 rgba(23, 32, 51, 0.22);
}

.hud-time-label {
  font-size: 7.5px;
  font-weight: 900;
  letter-spacing: 0.08em;
  color: #78350f;
}

.hud-time-val {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  font-weight: 900;
  color: #172033;
  font-variant-numeric: tabular-nums;
}

/* Hand-Sketched Loop Stamp + Kraft Index Plate */
.hud-loop-assembly {
  display: flex;
  align-items: center;
}

.hud-watch-dial {
  position: relative;
  z-index: 2;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: #fffdf6;
  border: 1.8px solid #172033;
  box-shadow: 2.5px 2.5px 0 rgba(23, 32, 51, 0.22);
  display: grid;
  place-items: center;
  transform: rotate(-2.5deg);
}

.hud-watch-crown {
  display: none;
}

.hud-watch-ring {
  position: absolute;
  inset: 2px;
  transform: rotate(-90deg);
}

.hud-watch-ring .ring-track {
  fill: none;
  stroke: rgba(23, 32, 51, 0.12);
  stroke-width: 3.2;
}

.hud-watch-ring .ring-fill {
  fill: none;
  stroke: #059669;
  stroke-width: 3.2;
  stroke-linecap: round;
  transition: stroke-dashoffset 0.4s ease, stroke 0.3s ease;
}

.hud-watch-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1;
}

.hud-watch-label {
  font-size: 6.5px;
  font-weight: 900;
  letter-spacing: 0.08em;
  color: #475569;
}

.hud-watch-num {
  font-size: 16px;
  font-weight: 900;
  color: #172033;
  font-variant-numeric: tabular-nums;
}

.hud-telemetry-plate {
  margin-left: -7px;
  padding: 5px 10px 5px 13px;
  background: #f5ead2;
  border: 1.5px solid #172033;
  border-radius: 3px;
  transform: rotate(0.8deg);
  display: flex;
  flex-direction: column;
  gap: 3px;
  box-shadow: 2.5px 2.5px 0 rgba(23, 32, 51, 0.2);
}

.hud-tele-row {
  display: flex;
  align-items: center;
  gap: 5px;
}

.hud-tele-k {
  font-size: 7.5px;
  font-weight: 900;
  letter-spacing: 0.06em;
  color: #475569;
}

.hud-tele-k.len,
.hud-tele-v.len {
  color: #059669;
}

.hud-tele-v {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 10.5px;
  font-weight: 900;
  color: #172033;
  font-variant-numeric: tabular-nums;
}

.hud-note-rail {
  display: inline-flex;
  width: 38px;
  height: 6px;
  border-radius: 2px;
  background: #fffdf6;
  border: 1px solid rgba(23, 32, 51, 0.55);
  overflow: hidden;
}

.hud-note-bar {
  display: block;
  height: 100%;
  background: #059669;
  transition: width 0.4s ease, background 0.3s ease;
}

/* Right Emblems (3x Sticker & Red-Ink Combo Burst) */
.hud-right-emblems {
  display: flex;
  align-items: center;
  gap: 10px;
}

.hud-wing-badge {
  display: flex;
  align-items: center;
}

.hud-wing-badge[hidden] {
  display: none;
}

.hud-wing {
  display: none;
}

.hud-combo-shield {
  padding: 5px 12px;
  background: #fffdf6;
  border: 2px solid #d93025;
  border-radius: 999px;
  transform: rotate(-4deg);
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.05;
  box-shadow: 2.5px 2.5px 0 rgba(23, 32, 51, 0.22);
}

.hud-combo-top strong {
  font-size: 16px;
  font-weight: 900;
  color: #d93025;
  font-variant-numeric: tabular-nums;
}

.hud-combo-top em {
  font-style: normal;
  font-size: 11px;
  font-weight: 900;
  color: #172033;
  margin-left: 1px;
}

.hud-combo-tag {
  font-size: 7px;
  font-weight: 900;
  letter-spacing: 0.08em;
  color: #172033;
}

.hud-triple-core {
  padding: 4px 10px;
  background: linear-gradient(135deg, #fef9c3, #fde047);
  border: 1.6px solid #172033;
  border-radius: 3px;
  transform: rotate(2deg);
  display: flex;
  align-items: center;
  gap: 5px;
  box-shadow: 2.5px 2.5px 0 rgba(23, 32, 51, 0.25);
}

.hud-triple-x {
  font-size: 15px;
  font-weight: 900;
  color: #172033;
}

.hud-triple-meta {
  display: flex;
  flex-direction: column;
  line-height: 1.05;
}

.hud-triple-tag {
  font-size: 6.8px;
  font-weight: 900;
  letter-spacing: 0.08em;
  color: #b45309;
}

.hud-triple-val {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  font-weight: 900;
  color: #d93025;
}

/* Overlays (Music Notebook Sheets pinned with Washi Tape) */
.overlay {
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at center, rgba(248, 245, 236, 0.22), rgba(23, 32, 51, 0.34));
  backdrop-filter: blur(2.5px);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 10;
}

.overlay[hidden] {
  display: none;
}

.overlay-card {
  position: relative;
  width: min(460px, 100%);
  background:
    linear-gradient(90deg, transparent 28px, rgba(225, 29, 72, 0.24) 28px 29.2px, transparent 29.2px),
    repeating-linear-gradient(180deg, transparent 0 22px, rgba(59, 130, 246, 0.14) 22px 23px),
    linear-gradient(145deg, #fffdf6, #f7f0de);
  border: 2px solid #172033;
  border-radius: 4px;
  padding: 20px 22px 18px 38px;
  box-shadow: 6px 6px 0 rgba(23, 32, 51, 0.26);
  text-align: center;
  color: #172033;
}

.overlay-card::before,
.overlay-card::after {
  content: "";
  position: absolute;
  top: -7px;
  width: 48px;
  height: 13px;
  border: 0.5px solid rgba(23, 32, 51, 0.12);
}

.overlay-card::before {
  left: -10px;
  background: repeating-linear-gradient(-45deg, rgba(255, 255, 255, 0.35) 0 2px, transparent 2px 5px), rgba(110, 231, 183, 0.62);
  transform: rotate(-20deg);
}

.overlay-card::after {
  right: -10px;
  background: repeating-linear-gradient(-45deg, rgba(255, 255, 255, 0.35) 0 2px, transparent 2px 5px), rgba(252, 211, 77, 0.66);
  transform: rotate(18deg);
}

.song-badge-row {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 8px;
}

.diff-badge,
.bpm-badge {
  font-size: 10.5px;
  font-weight: 900;
  letter-spacing: 0.08em;
  padding: 2.5px 10px;
  border-radius: 3px;
}

.diff-badge {
  background: color-mix(in srgb, var(--accent, #4f46e5) 12%, transparent);
  color: var(--accent, #4f46e5);
  border: 1.5px solid var(--accent, #4f46e5);
  transform: rotate(-2deg);
}

.bpm-badge {
  background: #f5ead2;
  color: #475569;
  border: 1px solid rgba(23, 32, 51, 0.45);
}

.overlay-title {
  margin: 0;
  font-size: 25px;
  font-weight: 900;
  color: #172033;
  display: inline-block;
  border-bottom: 2.5px solid #d93025;
  padding-bottom: 2px;
}

.overlay-sub {
  margin: 6px 0 16px;
  font-family: Georgia, "Times New Roman", serif;
  font-style: italic;
  font-size: 13px;
  color: #475569;
}

.howto-inline {
  margin-top: 14px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px 14px;
  font-size: 11.5px;
  font-weight: 700;
  color: #475569;
}

/* Buttons (Hand-Inked Chunky Sketch Buttons) */
.btn {
  border: 2px solid #172033;
  border-radius: 4px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 900;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 3px 3px 0 #172033;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}

.btn:hover {
  transform: translate(-1px, -1px);
  box-shadow: 4px 4px 0 #172033;
}

.btn:active {
  transform: translate(2px, 2px);
  box-shadow: 1px 1px 0 #172033;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.btn-lg {
  padding: 12px 28px;
  font-size: 16px;
  width: 100%;
}

.btn-primary {
  background: var(--accent, #4f46e5);
  color: #fff;
}

.btn-accent {
  background: #d93025;
  color: #fff;
}

.btn-outline {
  background: #fffdf6;
  color: #172033;
}

.kbd {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 10.5px;
  padding: 2px 6px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.22);
  color: #fff;
  letter-spacing: 0.05em;
}

/* Result Card */
.result-card {
  width: min(480px, 100%);
  padding: 18px 22px 18px 38px;
}

.result-head {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.gameover-tag {
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.12em;
  color: #d93025;
}

.record-badge {
  background: #fde68a;
  color: #172033;
  border: 1.5px solid #d93025;
  font-size: 10.5px;
  font-weight: 900;
  padding: 2px 8px;
  border-radius: 3px;
  transform: rotate(3deg);
  letter-spacing: 0.06em;
}

.final-score-box {
  margin: 4px 0 12px;
}

.final-score-label {
  display: block;
  font-size: 9.5px;
  font-weight: 800;
  letter-spacing: 0.14em;
  color: #475569;
}

.final-score-num {
  font-size: 38px;
  font-weight: 900;
  line-height: 1.05;
  color: #172033;
  display: inline-block;
  border-bottom: 3px solid #d93025;
  padding: 0 8px 2px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}

.stat-item {
  background: #fffdf6;
  border: 1.4px solid rgba(23, 32, 51, 0.72);
  border-radius: 3px;
  padding: 6px 4px;
}

.stat-label {
  display: block;
  font-size: 9.5px;
  color: #475569;
  font-weight: 800;
}

.stat-val {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 14px;
  font-weight: 900;
  color: #172033;
}

.score-form {
  display: flex;
  gap: 8px;
}

.name-input {
  flex: 1;
  min-width: 0;
  border: 1.6px solid #172033;
  border-radius: 3px;
  padding: 8px 11px;
  font-size: 14px;
  font-weight: 700;
  color: #172033;
  background: #fffdf6;
}

.name-input:focus {
  outline: none;
  border-color: #d93025;
}

.submit-status {
  margin: 6px 0 10px;
  min-height: 16px;
  font-size: 12px;
  font-weight: 800;
  color: #d93025;
}

.result-actions {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 8px;
}

/* Drop Overlay */
.drop-overlay {
  position: absolute;
  inset: 0;
  background: rgba(79, 70, 229, 0.16);
  backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  z-index: 20;
  pointer-events: none;
}

.drop-overlay[hidden] {
  display: none;
}

.drop-box {
  background: #fff;
  border: 2.5px dashed var(--accent);
  border-radius: 18px;
  padding: 24px 32px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 14px;
  color: var(--ink-muted);
}

.drop-box strong {
  font-size: 18px;
  color: var(--accent);
}

/* Bottom Grid */
.bottom-grid {
  display: grid;
  grid-template-columns: 1.25fr 1fr;
  gap: 14px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.04);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}

.panel-title {
  margin: 0;
  font-size: 15px;
  font-weight: 900;
}

.mode-badge {
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0.08em;
  padding: 3px 9px;
  border-radius: 999px;
  background: #e2e8f0;
  color: #334155;
}

.mode-badge.global {
  background: #dcfce7;
  color: #15803d;
}

/* Ranking List */
.ranking-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 290px;
  overflow-y: auto;
}

.ranking-row {
  display: grid;
  grid-template-columns: 32px 1fr auto auto;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  border-radius: 10px;
  background: var(--surface-soft);
  font-size: 13px;
}

.ranking-row.top-1 {
  background: #fef9c3;
}

.ranking-row.top-2 {
  background: #f1f5f9;
}

.ranking-row.top-3 {
  background: #ffedd5;
}

.rank-num {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-weight: 900;
  text-align: center;
  color: var(--ink-muted);
}

.rank-name {
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rank-meta {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  color: var(--ink-muted);
}

.rank-score {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-weight: 900;
  font-size: 14px;
  color: var(--ink);
}

.ranking-empty {
  text-align: center;
  padding: 26px 12px;
  font-size: 13px;
  color: var(--ink-muted);
}

/* Tips */
.tips-list {
  margin: 12px 0 0;
  padding-left: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-size: 13px;
  line-height: 1.55;
  color: var(--ink-muted);
}

.tips-list strong {
  color: var(--ink);
}

.tips-list kbd,
.tips-list code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  background: #f1f5f9;
  border: 1px solid rgba(15, 23, 42, 0.14);
  border-radius: 5px;
  padding: 1px 5px;
  color: var(--ink);
}

.footer {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 12px;
  color: var(--ink-muted);
  padding-top: 6px;
}

.footer a {
  color: var(--ink);
  font-weight: 700;
  text-decoration: none;
}

/* Mobile Responsive */
@media (max-width: 760px) {
  .app {
    padding: 10px 10px 24px;
    gap: 10px;
  }

  .stage-frame {
    aspect-ratio: 16 / 10;
  }

  .bottom-grid {
    grid-template-columns: 1fr;
  }

  .overlay-card {
    padding: 14px 14px;
  }

  .overlay-title {
    font-size: 20px;
  }

  .final-score-num {
    font-size: 32px;
  }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .hud-chip {
    padding: 3px 8px;
  }

  .hud-value {
    font-size: 13px;
  }

  .hud-score .hud-value {
    font-size: 15px;
  }
}
