:root {
  --bg: #0e0e12;
  --sidebar: #131318;
  --surface: #18181f;
  --surface-2: #202029;
  --text: #ececf1;
  --muted: #9494a6;
  --border: #2a2a34;
  --accent: #ff6369;
  --accent-text: #16161d;
  --danger: #ff6369;
  --stage: #000000;
  --chart-bar: #f2555a;   /* validated against the dark surface */
  --sb-width: 248px;
  --sb-collapsed: 68px;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f6f8;
    --sidebar: #ffffff;
    --surface: #ffffff;
    --surface-2: #f0f0f4;
    --text: #16161d;
    --muted: #5d5d6e;
    --border: #e2e2e8;
    --accent: #e5484d;
    --accent-text: #ffffff;
    --danger: #c62a2f;
    --chart-bar: #e5484d;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}
a { color: inherit; }
h1, h2 { letter-spacing: -0.01em; }
.muted { color: var(--muted); }
svg { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor;
      stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Shell ---------- */
.app { display: flex; min-height: 100vh; }

.sidebar {
  position: sticky; top: 0; height: 100vh; flex: none;
  width: var(--sb-width);
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  padding: 12px 10px;
  display: flex; flex-direction: column; gap: 6px;
  overflow-y: auto; overflow-x: hidden;
  transition: width .18s ease;
}
.sb-head { display: flex; align-items: center; gap: 8px; padding: 0 2px 8px; }
.brand { font-weight: 700; font-size: 18px; text-decoration: none; white-space: nowrap; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; flex: none;
  border: 0; border-radius: 8px; background: transparent; color: var(--text); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }

.sb-search {
  display: flex; align-items: center; gap: 10px;
  height: 40px; padding: 0 12px; margin-bottom: 4px;
  background: var(--surface-2); border-radius: 8px; color: var(--muted);
}
.sb-search input {
  flex: 1; min-width: 0; border: 0; outline: 0; background: transparent;
  color: var(--text); font: inherit;
}
.sb-search:focus-within { outline: 2px solid var(--accent); outline-offset: -2px; }

.sb-nav { display: grid; gap: 2px; }
.sb-nav a, .sb-games a {
  display: flex; align-items: center; gap: 12px;
  min-height: 40px; padding: 6px 12px; border-radius: 8px;
  text-decoration: none; color: var(--muted); white-space: nowrap;
}
.sb-nav a:hover, .sb-games a:hover { background: var(--surface-2); color: var(--text); }
.sb-nav a.active, .sb-games a.active { background: var(--surface-2); color: var(--text); font-weight: 600; }

.sb-section {
  margin: 14px 12px 4px; font-size: 12px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
}
.sb-games { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 2px; }
.sb-games a { padding: 4px 10px; overflow: hidden; }
.sb-games .label { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.thumb {
  width: 28px; height: 28px; flex: none; border-radius: 6px; overflow: hidden;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--border); color: var(--muted); font-size: 13px; font-weight: 700;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; }

/* Collapsed sidebar (desktop): icons only. */
@media (min-width: 861px) {
  .sb-collapsed .sidebar { width: var(--sb-collapsed); }
  .sb-collapsed .sidebar .label,
  .sb-collapsed .sidebar .brand,
  .sb-collapsed .sb-search input { display: none; }
  .sb-collapsed .sb-search { justify-content: center; padding: 0; cursor: pointer; }
  .sb-collapsed .sb-nav a, .sb-collapsed .sb-games a { justify-content: center; padding-left: 0; padding-right: 0; }
  .sb-collapsed .sb-section { height: 1px; margin: 12px 8px 6px; background: var(--border); }
  .sb-collapsed .sb-tags, .sb-collapsed .sb-tags-title { display: none; }
}

.content { flex: 1; min-width: 0; display: flex; flex-direction: column; }
main { width: 100%; max-width: 1400px; margin: 0 auto; padding: 20px 32px 56px; }
.topbar { display: none; }
.scrim { display: none; }

/* Mobile: sidebar becomes a drawer. */
@media (max-width: 860px) {
  .sidebar {
    position: fixed; z-index: 30; left: 0; top: 0; bottom: 0;
    transform: translateX(-100%); transition: transform .2s ease;
  }
  .sb-open .sidebar { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.4); }
  .sb-open .scrim { display: block; position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.5); }
  .topbar {
    display: flex; align-items: center; gap: 8px;
    position: sticky; top: 0; z-index: 10;
    padding: 8px 12px; background: var(--sidebar); border-bottom: 1px solid var(--border);
  }
  main { padding: 16px 16px 48px; }
}

/* ---------- Home ---------- */
.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 16px; margin-bottom: 16px; }
.page-head h1 { margin: 0; font-size: 24px; }

.grid { display: grid; gap: 20px; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
.card {
  display: block; text-decoration: none; background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px; overflow: hidden;
  transition: transform .12s ease, border-color .12s ease;
}
.card:hover, .card:focus-visible { transform: translateY(-2px); border-color: var(--accent); }
.cover { aspect-ratio: 16 / 10; width: 100%; object-fit: cover; display: block; background: var(--surface-2); }
.cover.placeholder {
  display: flex; align-items: center; justify-content: center;
  font-size: 48px; font-weight: 700; color: var(--muted);
}
.card-body { padding: 12px 14px 14px; }
.card-body h2 { margin: 0; font-size: 16px; }
.card-body p { margin: 4px 0 0; font-size: 14px; color: var(--muted);
               display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.empty { padding: 48px 0; text-align: center; color: var(--muted); }
.empty h1 { color: var(--text); }

/* ---------- Game page ---------- */
.crumbs { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.crumbs a { text-decoration: none; }
.crumbs a:hover { color: var(--text); }
.crumbs svg { width: 14px; height: 14px; }
.crumbs span { color: var(--text); font-weight: 600; }

.player {
  /* As wide as possible, but short enough to fit the window. */
  width: min(100%, calc((100vh - 150px) * var(--arw) / var(--arh)));
  margin: 0 auto;
  border: 1px solid var(--border); border-radius: 12px; overflow: hidden;
  background: var(--surface);
}
.stage { position: relative; aspect-ratio: var(--arw) / var(--arh); background: var(--stage); }
.stage iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; display: block; background: var(--stage); }
.stage:fullscreen, .stage:-webkit-full-screen { aspect-ratio: auto; width: 100%; height: 100%; }

.start {
  position: absolute; inset: 0; width: 100%; height: 100%;
  border: 0; padding: 0; cursor: pointer; background: var(--stage); color: #fff;
  display: flex; align-items: center; justify-content: center;
}
.start img {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover;
  opacity: .55; transition: opacity .15s ease, transform .3s ease;
}
.start:hover img { opacity: .7; transform: scale(1.02); }
.start-btn {
  position: relative; display: inline-flex; align-items: center; gap: 10px;
  padding: 14px 28px; border-radius: 999px;
  background: var(--accent); color: var(--accent-text);
  font: 700 18px/1 system-ui, sans-serif; box-shadow: 0 8px 30px rgba(0,0,0,.35);
}
.start-btn svg { width: 22px; height: 22px; fill: currentColor; stroke: none; }
.started .start { display: none; }

.player-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 10px 8px 16px; border-top: 1px solid var(--border);
}
.player-title { color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-actions { display: flex; gap: 4px; flex: none; }
.bar-btn {
  display: inline-flex; align-items: center; gap: 8px;
  height: 36px; padding: 0 12px; border: 0; border-radius: 8px;
  background: transparent; color: var(--text); font: inherit; font-size: 14px;
  text-decoration: none; cursor: pointer;
}
.bar-btn:hover { background: var(--surface-2); }
.bar-btn svg { width: 18px; height: 18px; }
.bar-btn.outline { border: 1px solid var(--border); }

.details {
  display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 40px;
  margin-top: 28px;
}
.about-head { display: flex; align-items: center; gap: 16px; }
.about-head .cover { width: 120px; flex: none; border-radius: 8px; }
.about-head .cover.placeholder { font-size: 32px; }
.about h1 { margin: 0; font-size: 26px; }
.about-head p { margin: 2px 0 0; }
.desc { white-space: pre-line; max-width: 70ch; margin: 16px 0; }
.about .bar-btn { margin-top: 8px; }

.more h2 { margin: 0 0 10px; font-size: 15px; }
.more-item {
  display: flex; gap: 12px; align-items: center; padding: 8px;
  margin: 0 -8px; border-radius: 10px; text-decoration: none;
}
.more-item:hover { background: var(--surface-2); }
.more-item .cover { width: 96px; flex: none; border-radius: 6px; }
.more-item .cover.placeholder { font-size: 24px; }
.more-item strong { display: block; font-size: 14px; }
.more-item .muted { font-size: 13px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

@media (max-width: 1100px) {
  .details { grid-template-columns: 1fr; gap: 28px; }
}
@media (max-width: 600px) {
  .player-bar { padding-left: 12px; }
  .player-title { display: none; }
  .bar-actions { width: 100%; justify-content: space-between; }
}

/* ---------- Upload ---------- */
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 20px; }
form.upload { display: grid; gap: 14px; max-width: 600px; }
label { display: grid; gap: 4px; font-weight: 600; font-size: 14px; }
label small { font-weight: 400; color: var(--muted); }
input[type=text], textarea, select, input[type=file] {
  font: inherit; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text); width: 100%;
}
label.check { display: flex; gap: 8px; align-items: flex-start; font-weight: 400; }
label.check input { margin-top: 4px; }
button.primary {
  font: inherit; font-weight: 600; border: 0; border-radius: 8px; padding: 10px 18px;
  background: var(--accent); color: var(--accent-text); cursor: pointer;
}
button.primary:disabled { opacity: .6; cursor: progress; }
button.danger {
  font: inherit; font-size: 14px; border-radius: 8px; padding: 6px 12px; cursor: pointer;
  background: transparent; color: var(--danger); border: 1px solid var(--border);
}
.error {
  margin: 0; padding: 10px 12px; border-radius: 8px;
  background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger);
}
progress { width: 100%; }
h2.section { margin-top: 40px; font-size: 18px; }
.game-table { width: 100%; max-width: 900px; border-collapse: collapse; }
.game-table td { padding: 10px 8px; border-top: 1px solid var(--border); vertical-align: middle; }
.game-table td:last-child { text-align: right; }

/* ---------- Edit page ---------- */
.optional { font-weight: 400; font-size: 12px; color: var(--muted); }
.small { font-size: 13px; }
.button-link {
  display: inline-flex; align-items: center; height: 32px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 14px; text-decoration: none;
}
.button-link:hover { background: var(--surface-2); }
.row-actions { display: flex; gap: 8px; justify-content: flex-end; align-items: center; }
.row-actions form { margin: 0; }
.notice {
  margin: 0 0 16px; padding: 10px 12px; border-radius: 8px;
  background: color-mix(in srgb, #30a46c 18%, transparent); color: var(--text);
}
#error:not([hidden]) { margin-bottom: 16px; }

.edit-grid {
  display: grid; gap: 20px; align-items: start;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.5fr);
}
.edit-grid > :nth-child(2) { grid-column: 2; grid-row: 1 / span 3; }
.edit-grid .panel { display: grid; gap: 14px; }
.edit-grid h2 { margin: 0; font-size: 17px; }
.edit-grid p { margin: 0; }
.stack { display: grid; gap: 14px; }

.thumb-preview { display: grid; gap: 8px; }
.thumb-preview .cover { border-radius: 8px; border: 1px solid var(--border); }

.capture {
  width: min(100%, calc((100vh - 320px) * var(--arw) / var(--arh)));
  min-width: min(100%, 280px);
  margin: 0 auto; border-radius: 10px; overflow: hidden; border: 1px solid var(--border);
}
.capture-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.capture-result { display: grid; gap: 10px; }
.capture-result img { width: 100%; max-width: 480px; border-radius: 8px; border: 1px solid var(--border); }

@media (max-width: 1000px) {
  .edit-grid { grid-template-columns: 1fr; }
  .edit-grid > :nth-child(2) { grid-column: auto; grid-row: auto; }
}

/* ---------- Tags, plays, how to play ---------- */
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 9px; border-radius: 999px; border: 1px solid var(--border);
  font-size: 13px; color: var(--muted); text-decoration: none; white-space: nowrap;
}
a.chip:hover { color: var(--text); border-color: var(--muted); }
.chip.active { background: var(--surface-2); color: var(--text); border-color: var(--muted); }
.chip.small { font-size: 12px; padding: 1px 7px; }
.chip-count { font-size: 11px; opacity: .7; }
.sb-tags { display: flex; flex-wrap: wrap; gap: 6px; padding: 2px 10px 4px; }

.page-head > div { display: grid; gap: 2px; }
.segmented { display: inline-flex; padding: 3px; gap: 2px; border-radius: 9px; background: var(--surface-2); flex: none; }
.segmented a { padding: 4px 12px; border-radius: 7px; font-size: 14px; text-decoration: none; color: var(--muted); }
.segmented a.active { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.15); }

.card-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 8px; }
.card-meta .plays { margin-left: auto; font-size: 12px; color: var(--muted); }

.tag-row { display: flex; flex-wrap: wrap; gap: 6px; margin: 12px 0; }
.how-to { margin-top: 20px; }
.how-to h2 { font-size: 16px; margin: 0 0 8px; }
.controls { white-space: pre-line; max-width: 70ch; margin: 0; padding: 12px 14px;
            background: var(--surface); border: 1px solid var(--border); border-radius: 10px; }
.badge { display: inline-flex; align-items: center; gap: 6px; margin: 0 0 10px; font-size: 14px; color: var(--muted); }
.badge svg { width: 18px; height: 18px; }

/* Only phones and tablets see the keyboard warning on the Play screen. */
.kb-warning {
  display: none; position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%);
  padding: 6px 12px; border-radius: 8px; background: rgba(0,0,0,.7); color: #fff; font-size: 13px; white-space: nowrap;
}
@media (hover: none) and (pointer: coarse) { .kb-warning { display: block; } }

/* ---------- Play statistics (edit page) ---------- */
.stat-tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat-tiles > div { display: grid; padding: 10px 12px; border-radius: 10px; background: var(--surface-2); }
.stat-value { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 12px; color: var(--muted); }
.spark { margin: 0; display: grid; gap: 6px; }
.spark-bars { display: flex; align-items: flex-end; gap: 2px; height: 72px; border-bottom: 1px solid var(--border); }
.spark-bar { position: relative; flex: 1; height: 100%; display: flex; align-items: flex-end; }
.spark-bar > span { width: 100%; background: var(--chart-bar); border-radius: 4px 4px 0 0; }
.spark-bar:hover > span { filter: brightness(1.15); }
.spark-bar:hover::after {
  content: attr(data-tip); position: absolute; bottom: calc(100% + 6px); left: 50%; transform: translateX(-50%);
  padding: 3px 8px; border-radius: 6px; background: var(--text); color: var(--bg);
  font-size: 12px; white-space: nowrap; pointer-events: none; z-index: 1;
}
.spark-bar:first-child:hover::after { left: 0; transform: none; }
.spark-bar:last-child:hover::after { left: auto; right: 0; transform: none; }
.spark-axis { display: flex; justify-content: space-between; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
