:root {
  --bg: #111013;
  --surface: #1c1a20;
  --surface-2: #26232b;
  --line: #34303a;
  --text: #f4f1ec;
  --muted: #a39ea8;
  --accent: #ff7a45;
  --accent-2: #ffb35c;
  --good: #6fd49a;
  --bad: #ff8f86;
  --radius: 18px;
  color-scheme: dark;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overscroll-behavior: none; }
button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }
.muted { color: var(--muted); }
.error { color: var(--bad); }
[hidden] { display: none !important; }

#login { max-width: 360px; margin: 0 auto; padding: 18dvh 20px 0; display: grid; gap: 14px; justify-items: center; text-align: center; }
#login img { border-radius: 18px; }
#login h1 { margin: 0; font-size: 28px; }
#login p { margin: 0; line-height: 1.5; }
#plexLogin { width: 100%; border: 0; border-radius: 12px; padding: 15px; background: #e5a00d; color: #1f1400; font-weight: 750; font-size: 16px; }
#plexLogin:disabled { opacity: .6; }
input { width: 100%; font: inherit; font-size: 16px; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 13px 14px; }
input:focus { outline: 2px solid var(--accent); outline-offset: 0; }
#sync { border: 0; border-radius: 12px; padding: 13px; background: var(--accent); color: #1a0d06; font-weight: 700; }

#app { position: fixed; inset: 0; display: flex; flex-direction: column; padding-top: env(safe-area-inset-top); }
header { padding: 10px 16px 8px; }
.title { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.title strong { font-size: 19px; letter-spacing: -.01em; }
.title > span { font-size: 13px; text-align: right; }
.progress { height: 3px; background: var(--surface-2); border-radius: 3px; margin-top: 8px; overflow: hidden; }
#progressBar { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), var(--accent-2)); transition: width .4s; }

main { flex: 1; min-height: 0; position: relative; }
.view { display: none; height: 100%; }
.view.active { display: flex; flex-direction: column; }
.view:not(.active) { display: none !important; }

/* Rank */
#rank { padding: 4px 12px 8px; gap: 8px; }
#duel { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 8px; position: relative; }
.card {
  flex: 1; min-height: 0; position: relative; overflow: hidden; border: 0; padding: 0;
  border-radius: var(--radius); background: var(--surface) center / cover no-repeat;
  text-align: left; user-select: none; -webkit-user-select: none;
  transition: transform .15s ease, opacity .2s ease, box-shadow .2s;
}
.card::before { content: ""; position: absolute; inset: 0; pointer-events: none; background: linear-gradient(180deg, rgba(0,0,0,0) 30%, rgba(0,0,0,.35) 55%, rgba(0,0,0,.88)); }
.card:has(.pick:active) { transform: scale(.98); }
.card .pick { position: absolute; inset: 0; width: 100%; border: 0; padding: 0; background: none; text-align: left; }
.card .pick:focus-visible { outline: 3px solid var(--accent); outline-offset: -3px; border-radius: var(--radius); }
.card .meta { position: absolute; left: 16px; right: 16px; bottom: 14px; }
.card .song { display: block; font-size: clamp(19px, 5.4vw, 26px); font-weight: 750; line-height: 1.15; letter-spacing: -.01em; text-shadow: 0 1px 8px rgba(0,0,0,.5); overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.card .by { display: block; margin-top: 4px; font-size: 15px; color: #e7e2dc; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .by small { color: #bdb6ae; }
.card .rating { position: absolute; top: 12px; left: 12px; font-size: 12px; font-weight: 700; padding: 5px 9px; border-radius: 99px; background: rgba(0,0,0,.55); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); }
.card .play { position: absolute; top: 8px; right: 8px; width: 52px; height: 52px; border-radius: 50%; border: 0; display: grid; place-items: center; font-size: 19px; background: rgba(0,0,0,.55); color: #fff; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); }
.card.playing .play { background: var(--accent); color: #1a0d06; }
.card .bar { position: absolute; left: 0; bottom: 0; height: 4px; width: 0; background: var(--accent); }
.card.noart { background-image: linear-gradient(135deg, #3a2a44, #1d2a3a); }
.card.win { box-shadow: inset 0 0 0 4px var(--good); }
.card.lose { opacity: .35; }
.card .delta { position: absolute; inset: 0; display: grid; place-items: center; font-size: 44px; font-weight: 800; color: var(--good); text-shadow: 0 2px 20px rgba(0,0,0,.6); animation: pop .5s ease; }
@keyframes pop { from { transform: scale(.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.vs { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 2; width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; font-size: 13px; font-weight: 800; background: var(--bg); border: 3px solid var(--bg); box-shadow: 0 0 0 1px var(--line); pointer-events: none; }
.actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.ghost { border: 1px solid var(--line); background: var(--surface); border-radius: 14px; padding: 13px 8px; font-weight: 600; min-height: 48px; }
.ghost:disabled { opacity: .4; }
.empty { flex: 1; display: grid; place-items: center; text-align: center; color: var(--muted); padding: 24px; }

/* Leaderboard */
#board, #albums { overflow-y: auto; -webkit-overflow-scrolling: touch; }
.search { position: sticky; top: 0; z-index: 1; padding: 4px 12px 10px; background: var(--bg); display: grid; gap: 8px; }
.segmented { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 3px; }
.segmented button { border: 0; background: none; border-radius: 9px; padding: 9px; font-weight: 650; color: var(--muted); min-height: 40px; }
.segmented button.active { background: var(--surface-2); color: var(--text); }
#list, #albumList { list-style: none; margin: 0; padding: 0 12px 16px; }
.note { margin: 0; font-size: 13px; line-height: 1.4; }
.tracks { list-style: none; margin: 0; padding: 0; flex-basis: 100%; }
.tracks li { display: grid; grid-template-columns: 22px 1fr auto; gap: 10px; padding: 7px 0; border-top: 1px solid var(--line); font-size: 14px; color: var(--text); }
.tracks li span { color: var(--muted); font-variant-numeric: tabular-nums; }
.tracks li b { font-weight: 500; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tracks li.unrated b { color: var(--muted); }
.row { border-bottom: 1px solid var(--line); }
.row .line { all: unset; box-sizing: border-box; width: 100%; display: grid; grid-template-columns: 34px 48px 1fr auto; gap: 12px; align-items: center; padding: 8px 4px; cursor: pointer; }
.row .line:focus-visible { outline: 2px solid var(--accent); border-radius: 8px; }
.row .pos { font-size: 15px; font-weight: 750; color: var(--muted); text-align: center; font-variant-numeric: tabular-nums; }
.row.top .pos { color: var(--accent-2); }
.row img, .row .noimg { width: 48px; height: 48px; border-radius: 8px; object-fit: cover; background: var(--surface-2); }
.row .txt { min-width: 0; }
.row .txt b, .row .txt span { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .txt b { font-size: 15px; font-weight: 650; }
.row .txt span { font-size: 13px; color: var(--muted); margin-top: 2px; }
.row .score { text-align: right; font-variant-numeric: tabular-nums; }
.row .score b { display: block; font-size: 16px; }
.row .score span { font-size: 12px; color: var(--muted); }
.row.provisional .score b::after { content: "?"; color: var(--muted); font-weight: 500; margin-left: 1px; }
.row.hidden { opacity: .45; }
.details { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; padding: 2px 4px 12px 50px; font-size: 13px; color: var(--muted); }
.details button { border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 9px 12px; color: var(--text); min-height: 40px; }
.details .stat { flex-basis: 100%; }
.section { padding: 18px 4px 6px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }

/* Info */
#info { overflow-y: auto; padding: 4px 12px 16px; gap: 12px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.panel h2 { margin: 0 0 10px; font-size: 17px; }
.panel p { line-height: 1.5; margin: 8px 0 0; font-size: 14px; }
dl { display: grid; grid-template-columns: 1fr auto; gap: 8px 12px; margin: 0 0 14px; font-size: 15px; }
dt { color: var(--muted); }
dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
#sync { width: 100%; }
.account { display: grid; grid-template-columns: 44px 1fr auto; gap: 12px; align-items: center; }
.account img, #people img { border-radius: 50%; background: var(--surface-2); object-fit: cover; }
.account strong, .account span { display: block; }
.account span { font-size: 13px; margin-top: 2px; }
.account .ghost { padding: 10px 14px; }
.account #scrobble { grid-column: 1 / -1; margin: 0; font-size: 13px; }
#people { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
#people li { display: grid; grid-template-columns: 32px 1fr auto; gap: 10px; align-items: center; font-size: 15px; }
#people li span { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 14px; }
#sync:disabled { opacity: .5; }

nav { display: grid; grid-template-columns: repeat(4, 1fr); border-top: 1px solid var(--line); background: var(--surface); padding-bottom: env(safe-area-inset-bottom); }
nav button { border: 0; background: none; padding: 8px 0 10px; font-size: 12px; font-weight: 600; color: var(--muted); display: grid; gap: 2px; justify-items: center; min-height: 54px; }
nav button span { font-size: 19px; line-height: 1.2; }
nav button.active { color: var(--accent); }

#toast { position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px); background: var(--surface-2); border: 1px solid var(--line); padding: 10px 16px; border-radius: 12px; font-size: 14px; opacity: 0; pointer-events: none; transition: .25s; max-width: calc(100vw - 32px); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

kbd { display: none; font: 600 11px/1 system-ui, sans-serif; color: var(--muted); border: 1px solid var(--line); border-radius: 5px; padding: 3px 5px; margin-left: 6px; }
#top { display: none; }
.link { border: 0; background: none; color: var(--accent); font-weight: 600; padding: 8px 0; text-align: left; }

/* Side by side when there is room */
@media (min-aspect-ratio: 1/1) and (min-width: 640px) and (max-width: 899px) {
  #duel { flex-direction: row; }
  #rank, .search, #list, #albumList, #info { width: 100%; max-width: 1100px; margin-inline: auto; }
  #info { max-width: 640px; }
}
@media (min-aspect-ratio: 1/1) and (max-height: 500px) {
  header { display: none; }
  #duel { flex-direction: row; }
}
/* Desktop: tabs in the top bar, square covers side by side, live top 10 alongside. */
@media (min-width: 900px) {
  #app {
    display: grid; grid-template-columns: 1fr auto; grid-template-rows: auto minmax(0, 1fr); grid-template-areas: "header nav" "main main";
    column-gap: 24px; padding: 0 max(24px, calc((100vw - 1680px) / 2));
  }
  header { grid-area: header; padding: 18px 0 14px; align-self: center; }
  .title { justify-content: flex-start; gap: 16px; }
  .title strong { font-size: 22px; }
  .title > span { font-size: 14px; }
  .progress { max-width: 420px; }
  nav { grid-area: nav; display: flex; gap: 4px; align-self: center; border: 1px solid var(--line); border-radius: 14px; padding: 4px; }
  nav button { display: flex; gap: 8px; align-items: center; min-height: 0; padding: 9px 16px; border-radius: 10px; font-size: 14px; }
  nav button span { font-size: 16px; }
  nav button.active { background: var(--surface-2); color: var(--text); }
  main { grid-area: main; }

  /* Covers and buttons sit together as one group, centred vertically. */
  #rank { display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: 1fr auto auto 1fr; gap: 16px; padding: 8px 0 24px; container-type: size; }
  #duel { grid-row: 2; display: grid; grid-template-columns: 1fr 1fr; gap: 20px; align-items: center; justify-items: center; }
  #empty { grid-row: 2; }
  .actions { grid-row: 3; }
  /* Square, like album art, sized to whichever runs out first: column width or the height left after the buttons. */
  .card { flex: none; width: min(100%, 100cqh - 112px); aspect-ratio: 1; }
  .card .song { font-size: clamp(22px, 2vw, 30px); }
  .card .by { font-size: 16px; }
  .card .meta { left: 20px; right: 20px; bottom: 18px; }
  .vs { width: 52px; height: 52px; font-size: 14px; }
  .actions { width: min(100%, 560px); justify-self: center; }
  kbd { display: inline-block; }
  .card .rating kbd { margin: 0 6px 0 0; padding: 1px 5px; color: #ddd; border-color: rgba(255,255,255,.3); }

  #board, #albums { padding-bottom: 24px; }
  .search { grid-template-columns: 260px 1fr; padding: 8px 0 14px; max-width: 960px; margin-inline: auto; width: 100%; }
  #list, #albumList { max-width: 960px; margin-inline: auto; width: 100%; padding: 0; }
  #albums .search { align-items: center; }
  .row .line { grid-template-columns: 44px 56px 1fr auto; gap: 16px; padding: 10px 8px; border-radius: 10px; }
  .row img, .row .noimg { width: 56px; height: 56px; }
  .row .txt b { font-size: 16px; }
  .row .txt span { font-size: 14px; }
  .details { padding-left: 124px; }

  #info { display: grid; grid-template-columns: 1fr 1fr; align-content: start; align-items: start; max-width: 960px; width: 100%; margin-inline: auto; padding: 8px 0 24px; gap: 16px; }
  #info .account { grid-column: 1 / -1; }
  #toast { bottom: 32px; }
}
/* Vertical monitors: stack the covers so they fill the height instead of floating in the middle. */
@media (min-width: 900px) and (orientation: portrait) {
  #duel { grid-template-columns: 1fr; }
  .card { width: min(100%, (100cqh - 132px) / 2); }
}
@media (min-width: 1200px) {
  #rank { grid-template-columns: minmax(0, 1fr) 300px; column-gap: 28px; }
  #top { display: flex; flex-direction: column; grid-column: 2; grid-row: 1 / 5; min-height: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
  #top h2 { margin: 0 0 12px; font-size: 16px; }
  #top h2 span { font-weight: 500; }
  #topList { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; overflow-y: auto; }
  #topList li > span:nth-child(3) { min-width: 0; }
  #topList li { display: grid; grid-template-columns: 22px 40px 1fr auto; gap: 10px; align-items: center; padding: 6px 0; font-size: 14px; }
  #topList li.fresh { animation: pop .5s ease; }
  #topList .n { color: var(--muted); font-weight: 700; text-align: center; font-variant-numeric: tabular-nums; }
  #topList li:nth-child(-n+3) .n { color: var(--accent-2); }
  #topList img { width: 40px; height: 40px; border-radius: 6px; object-fit: cover; background: var(--surface-2); }
  #topList b, #topList small { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  #topList b { font-weight: 600; }
  #topList small { color: var(--muted); font-size: 12px; margin-top: 2px; }
  #topList .r { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; }
  #topMore { margin-top: auto; }
}
@media (hover: hover) {
  .card:hover { box-shadow: 0 14px 40px rgba(0,0,0,.45); transform: translateY(-2px); }
  .card.win:hover { box-shadow: inset 0 0 0 4px var(--good); }
  .card .play:hover { transform: scale(1.06); }
  .ghost:not(:disabled):hover, .details button:hover { background: var(--surface-2); }
  nav button:hover { color: var(--text); }
  .row .line:hover { background: var(--surface); }
  .link:hover { text-decoration: underline; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
