:root {
  color-scheme: dark;
  --bg: #0f0d17;
  --surface: #181524;
  --surface-2: #211d31;
  --line: #2f2945;
  --text: #f4f1ff;
  --muted: #a49ebd;
  --accent: #a3ff5c;      /* fairway green */
  --accent-ink: #12200a;
  --hot: #ff5fa2;         /* karaoke neon */
  --warn: #ffcc4d;
  --radius: 14px;
  --gutter: 16px;
  --top-h: 64px;
  --font: 'IBM Plex Sans Thai', system-ui, -apple-system, 'Sukhumvit Set', sans-serif;
  --display: 'Prompt', var(--font);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { fill: currentColor; }
[hidden] { display: none !important; }

/* ---------- header ---------- */
.top {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) var(--gutter) 10px;
  min-height: var(--top-h);
  background: rgba(15, 13, 23, .92);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; flex: none; border: 0; background: none; padding: 0; min-height: 44px; }
.brand span { font: 700 20px/1 var(--display); letter-spacing: .2px; }
.search {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px;
  height: 46px; padding: 0 12px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--line);
}
.search:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(163, 255, 92, .15); }
.search svg { width: 20px; height: 20px; fill: var(--muted); flex: none; }
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; font-size: 17px; }
.search input::-webkit-search-cancel-button { display: none; }
.clear { border: 0; background: var(--line); width: 26px; height: 26px; border-radius: 50%; font-size: 12px; flex: none; }
.icon-btn { flex: none; width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line); background: var(--surface); display: grid; place-items: center; }
.icon-btn svg { width: 22px; height: 22px; fill: var(--muted); }

/* ---------- layout ---------- */
.app {
  display: grid; gap: 12px;
  padding: 12px var(--gutter) calc(env(safe-area-inset-bottom) + 24px);
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "stage" "tabs" "panel";
  max-width: 1500px; margin: 0 auto;
}
.stage { grid-area: stage; }
.tabs { grid-area: tabs; }
.panel { grid-area: panel; display: none; min-width: 0; }
.panel.active { display: block; }

@media (min-width: 900px) {
  .app {
    grid-template-columns: minmax(0, 1.45fr) minmax(320px, 1fr);
    grid-template-rows: auto auto 1fr;
    grid-template-areas: "stage tabs" "stage panel" "queue panel";
    height: calc(100dvh - var(--top-h) - env(safe-area-inset-top));
    padding-bottom: calc(env(safe-area-inset-bottom) + 12px);
  }
  .app > * { min-height: 0; }
  .panel { overflow: auto; overscroll-behavior: contain; padding-right: 4px; }
  .panel-queue { grid-area: queue; display: block; }
  .tab-queue { display: none; }
  .tabs { position: static; align-self: start; }
  .stage { align-self: start; }
}

/* ---------- stage / player ---------- */
.screen {
  position: relative; aspect-ratio: 16 / 9; width: 100%;
  border-radius: var(--radius); overflow: hidden;
  background: radial-gradient(120% 120% at 20% 0%, #2a1f45 0%, #120f1c 60%);
  border: 1px solid var(--line);
}
.screen iframe, #player { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }
.idle, .between {
  position: absolute; inset: 0; display: grid; place-content: center; justify-items: center;
  text-align: center; padding: 20px; gap: 4px;
}
.idle-title, .between h3 { margin: 8px 0 0; font: 700 24px/1.2 var(--display); }
.idle-sub { margin: 0; color: var(--muted); max-width: 32ch; }
.between { background: rgba(15, 13, 23, .94); z-index: 3; }
.between .count { font: 700 64px/1 var(--display); color: var(--accent); }
.between p { margin: 0; color: var(--muted); }
.stage.theater { position: fixed; inset: 0; z-index: 50; background: #000; padding: env(safe-area-inset-top) 0 0; display: flex; flex-direction: column; }
.stage.theater .screen { flex: 1; aspect-ratio: auto; border-radius: 0; border: 0; }
.stage:fullscreen { background: #000; display: flex; flex-direction: column; }
.stage:fullscreen .screen { flex: 1; aspect-ratio: auto; border-radius: 0; border: 0; }

.now { display: flex; align-items: center; gap: 12px; padding: 10px 2px 0; flex-wrap: wrap; }
.now-meta { flex: 1 1 220px; min-width: 0; }
.now-title { margin: 0; font: 600 18px/1.3 var(--display); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.now-sub { margin: 0; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.controls { display: flex; gap: 6px; align-items: center; }
.ctl {
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--surface); display: grid; place-items: center;
}
.ctl svg { width: 22px; height: 22px; }
.ctl-main { width: 54px; height: 54px; background: var(--accent); color: var(--accent-ink); border: 0; }
.ctl-main svg { width: 28px; height: 28px; }
.ctl.on { color: var(--hot); border-color: var(--hot); }
.ctl:active { transform: scale(.94); }

/* ---------- tabs ---------- */
.tabs {
  display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none;
  position: sticky; top: calc(var(--top-h) + env(safe-area-inset-top)); z-index: 10;
  background: var(--bg); padding: 4px 0;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: none; height: 40px; padding: 0 16px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); color: var(--muted); font-weight: 500;
}
.tab b { display: inline-grid; place-items: center; min-width: 22px; height: 22px; margin-left: 4px; padding: 0 6px; border-radius: 999px; background: var(--line); color: var(--text); font-size: 12px; }
.tab.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.tab.active b { background: var(--bg); color: var(--text); }

/* ---------- panels & lists ---------- */
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 6px 0 8px; }
.panel-head h2 { margin: 0; font: 600 17px/1.3 var(--display); }
.txt-btn { border: 0; background: none; color: var(--muted); padding: 8px 4px; font-size: 14px; }
.txt-btn:hover { color: var(--text); }
.txt-btn.danger { color: var(--hot); }
.hint { color: var(--muted); font-size: 14px; margin: 0 0 8px; }
.list { list-style: none; margin: 0 0 16px; padding: 0; display: grid; gap: 6px; grid-template-columns: minmax(0, 1fr); }
.row {
  display: flex; align-items: center; gap: 10px; padding: 8px;
  border-radius: 12px; background: var(--surface); border: 1px solid transparent;
  min-height: 60px;
}
.row:hover { border-color: var(--line); }
.row.playing { border-color: var(--accent); }
.thumb {
  flex: none; width: 48px; height: 48px; border-radius: 8px; object-fit: cover;
  background: var(--surface-2) center/60% no-repeat url('/icons/note.svg');
}
.thumb.wide { width: 86px; height: 48px; }
.row-main { flex: 1; min-width: 0; cursor: pointer; }
.row-title { margin: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { margin: 0; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-actions { display: flex; gap: 4px; flex: none; }
.mini {
  height: 40px; min-width: 40px; padding: 0 10px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface-2); font-size: 13px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
}
.mini.go { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.mini.in { background: transparent; color: var(--accent); border-color: rgba(163, 255, 92, .35); }
.mini svg { width: 16px; height: 16px; }
.qnum { flex: none; width: 22px; text-align: center; color: var(--muted); font-weight: 600; }
.empty { color: var(--muted); padding: 18px 8px; text-align: center; font-size: 14px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 10px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; height: 24px; padding: 0 8px;
  border-radius: 999px; background: var(--surface-2); color: var(--muted); font-size: 12px; white-space: nowrap;
}
.chip.match { background: rgba(163, 255, 92, .14); color: var(--accent); font-weight: 600; }
.chip.hot { background: rgba(255, 95, 162, .14); color: var(--hot); }
button.chip { border: 1px solid var(--line); height: 32px; padding: 0 12px; font-size: 13px; color: var(--text); }
.row .chips { margin: 4px 0 0; }

.tune { background: var(--surface); border-radius: 12px; padding: 12px; margin-bottom: 10px; display: grid; gap: 10px; }
.tune label { display: grid; grid-template-columns: 1fr 140px; align-items: center; gap: 8px; font-size: 14px; }
.tune label.check, .check { display: flex; align-items: center; gap: 8px; font-size: 14px; }
input[type=range] { accent-color: var(--accent); width: 100%; }
input[type=checkbox] { accent-color: var(--accent); width: 18px; height: 18px; }

/* ---------- search sheet ---------- */
.sheet {
  position: fixed; z-index: 25; left: 0; right: 0; bottom: 0;
  top: calc(var(--top-h) + env(safe-area-inset-top));
  background: var(--bg); overflow: auto;
  padding: 12px var(--gutter) calc(env(safe-area-inset-bottom) + 24px);
}
.sheet-inner { max-width: 860px; margin: 0 auto; }
.sec-title { margin: 14px 0 8px; font: 600 14px/1.3 var(--font); color: var(--muted); letter-spacing: .3px; }
.song-head { display: flex; gap: 12px; align-items: center; margin: 4px 0 10px; }
.song-head img, .song-head .thumb { width: 64px; height: 64px; }
.song-head h3 { margin: 0; font: 700 20px/1.25 var(--display); }
.song-head p { margin: 0; color: var(--muted); }
.back { border: 0; background: none; color: var(--muted); padding: 6px 0; font-size: 14px; }
.badge { font-size: 11px; color: var(--accent-ink); background: var(--accent); border-radius: 6px; padding: 1px 6px; margin-right: 6px; font-weight: 700; vertical-align: 1px; }
.skeleton { height: 60px; border-radius: 12px; background: linear-gradient(90deg, var(--surface) 0%, var(--surface-2) 50%, var(--surface) 100%); background-size: 200% 100%; animation: shimmer 1.2s infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- dialog / toast ---------- */
.dialog {
  width: min(460px, calc(100vw - 32px)); border: 1px solid var(--line); border-radius: 18px;
  background: var(--surface); color: var(--text); padding: 20px;
}
.dialog::backdrop { background: rgba(0, 0, 0, .6); }
.dialog h2 { margin: 0 0 14px; font: 700 20px/1.2 var(--display); }
.dialog form { display: grid; gap: 14px; }
.field { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.field select { height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2); padding: 0 10px; color: var(--text); }
.note { margin: 0; color: var(--muted); font-size: 13px; }
.dialog-actions { display: flex; justify-content: space-between; align-items: center; }
.btn { height: 44px; padding: 0 20px; border-radius: 999px; border: 0; background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.toast {
  position: fixed; z-index: 60; left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 20px);
  width: max-content; max-width: min(560px, calc(100vw - 32px)); padding: 10px 10px 10px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg); font-weight: 500; box-shadow: 0 10px 30px rgba(0, 0, 0, .4);
  display: flex; align-items: center; gap: 12px;
}
.toast > span { flex: 1; min-width: 0; padding: 2px 0; }
.toast-act {
  flex: none; min-height: 40px; padding: 0 14px; border-radius: 999px; border: 0;
  background: var(--bg); color: var(--accent); font-weight: 700; white-space: nowrap;
}
body.has-mini .toast { bottom: calc(env(safe-area-inset-bottom) + 88px); }

/* ---------- mini player (Spotify / YouTube Music style) ---------- */
.mini-player {
  position: fixed; z-index: 40; left: 0; right: 0; bottom: 0;
  display: flex; align-items: center; gap: 8px;
  padding: 8px var(--gutter) calc(env(safe-area-inset-bottom) + 8px);
  background: rgba(24, 21, 36, .96); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.mini-main { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; border: 0; background: none; padding: 0; text-align: left; min-height: 48px; }
.mini-main .thumb { width: 48px; height: 48px; }
.mini-text { display: grid; min-width: 0; }
.mini-text b, .mini-text small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mini-text b { font: 600 15px/1.3 var(--display); }
.mini-text small { color: var(--muted); font-size: 13px; }
.mini-player .ctl { width: 48px; height: 48px; flex: none; }
.mini-player .ctl-main svg { width: 26px; height: 26px; }
body.has-mini .app { padding-bottom: calc(env(safe-area-inset-bottom) + 96px); }
body.has-mini .sheet { padding-bottom: calc(env(safe-area-inset-bottom) + 96px); }
@media (min-width: 900px) {
  .mini-player { left: 50%; right: auto; bottom: 16px; transform: translateX(-50%); width: min(560px, calc(100vw - 32px)); border: 1px solid var(--line); border-radius: 16px; padding: 8px 10px; box-shadow: 0 10px 30px rgba(0, 0, 0, .45); }
  body.has-mini .toast { bottom: 100px; }
}

/* ---------- queue: drag handle, now playing ---------- */
.handle {
  flex: none; width: 32px; height: 44px; margin: -4px -4px -4px -2px; display: grid; place-items: center;
  color: var(--muted); cursor: grab; touch-action: none; -webkit-user-select: none; user-select: none;
}
.handle svg { width: 20px; height: 20px; }
.row.dragging { background: var(--surface-2); border-color: var(--accent); box-shadow: 0 8px 24px rgba(0, 0, 0, .45); position: relative; z-index: 2; }
.row.dragging .handle { cursor: grabbing; color: var(--accent); }
#nowCard .list { margin-bottom: 4px; }
#nowCard .sec-title, .panel-queue .sec-title { margin: 6px 0; }
.eq { display: inline-flex; align-items: flex-end; gap: 3px; height: 18px; padding: 0 10px; }
.eq i { width: 3px; background: var(--accent); border-radius: 2px; animation: eq 1s ease-in-out infinite; }
.eq i { animation-play-state: paused; }
body.is-playing .eq i { animation-play-state: running; }
.eq i:nth-child(2) { animation-delay: -.4s; }
.eq i:nth-child(3) { animation-delay: -.7s; }
@keyframes eq { 0%, 100% { height: 4px; } 50% { height: 18px; } }
.idle .btn { margin-top: 12px; }
.idle:has(#idleResume:not([hidden])) img { display: none; }
@media (max-width: 520px) {
  .idle img { width: 52px; height: 52px; }
  .idle-title, .between h3 { font-size: 20px; }
  .idle .btn { margin-top: 8px; }
}
.toast a { color: inherit; font-weight: 700; }

@media (max-width: 520px) {
  .brand span { display: none; }
  .controls { width: 100%; justify-content: space-between; }
  .thumb.wide { width: 72px; height: 40px; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
