/* ═══════════════════════════════════════════════════════════════
   Cartridge Vault — styles
   Themes are selected with <html data-theme="…"> (see js/theme.js)
   ═══════════════════════════════════════════════════════════════ */

:root,
[data-theme="cardinal"] {
  --bg: #FBF8F2;
  --surface: #FFFFFF;
  --surface-2: #F3EEE4;
  --text: #241A17;
  --muted: #6E625C;
  --border: #E2D9CB;
  --accent: #C8102E;      /* cardinal */
  --accent-ink: #FFFFFF;
  --accent-2: #F1BE48;    /* gold */
  --accent-2-ink: #3A2A00;
  --danger: #B3261E;
  --ok: #2E7D32;
  --shadow: 0 1px 2px rgba(60, 20, 10, .06), 0 6px 20px rgba(60, 20, 10, .07);
  --tile-ink: #FFFFFF;
  color-scheme: light;
}

[data-theme="midnight"] {
  --bg: #0F1626;
  --surface: #172036;
  --surface-2: #1E2A45;
  --text: #E6ECF8;
  --muted: #94A2BF;
  --border: #2A3858;
  --accent: #5B8DEF;
  --accent-ink: #0B1220;
  --accent-2: #9CC2FF;
  --accent-2-ink: #0B1220;
  --danger: #FF7A70;
  --ok: #6BD49A;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .35);
  --tile-ink: #FFFFFF;
  color-scheme: dark;
}

[data-theme="pocket"] {
  --bg: #C4CFA1;
  --surface: #D7DFB8;
  --surface-2: #B6C28E;
  --text: #0F380F;
  --muted: #3D5E2A;
  --border: #9BAB72;
  --accent: #306230;
  --accent-ink: #E0EBC0;
  --accent-2: #8BAC0F;
  --accent-2-ink: #0F380F;
  --danger: #7A1E12;
  --ok: #0F380F;
  --shadow: 0 2px 0 #8BA05E;
  --tile-ink: #E0EBC0;
  color-scheme: light;
}

[data-theme="classic"] {
  --bg: #E4E2E8;
  --surface: #F4F3F7;
  --surface-2: #D6D3DE;
  --text: #24212C;
  --muted: #615C6E;
  --border: #C3BFCC;
  --accent: #5B3E96;
  --accent-ink: #FFFFFF;
  --accent-2: #A79BCB;
  --accent-2-ink: #241A3D;
  --danger: #B3261E;
  --ok: #2E7D32;
  --shadow: 0 1px 2px rgba(30, 20, 60, .08), 0 6px 18px rgba(30, 20, 60, .08);
  --tile-ink: #FFFFFF;
  color-scheme: light;
}

[data-theme="charcoal"] {
  --bg: #1E1F22;
  --surface: #27292D;
  --surface-2: #303237;
  --text: #ECECEC;
  --muted: #A3A6AD;
  --border: #3C3F45;
  --accent: #E53935;
  --accent-ink: #FFFFFF;
  --accent-2: #FDD835;
  --accent-2-ink: #1E1F22;
  --danger: #FF6E66;
  --ok: #43A047;
  --shadow: 0 1px 2px rgba(0, 0, 0, .35), 0 8px 24px rgba(0, 0, 0, .35);
  --tile-ink: #FFFFFF;
  color-scheme: dark;
}

[data-theme="varia"] {
  --bg: #151B22;
  --surface: #1D252F;
  --surface-2: #26303C;
  --text: #EDE7DF;
  --muted: #A0A9B4;
  --border: #33404F;
  --accent: #F28C28;
  --accent-ink: #1A1006;
  --accent-2: #3FC1B0;
  --accent-2-ink: #06201C;
  --danger: #FF6E66;
  --ok: #3FC1B0;
  --shadow: 0 1px 2px rgba(0, 0, 0, .35), 0 8px 24px rgba(0, 0, 0, .35);
  --tile-ink: #FFFFFF;
  color-scheme: dark;
}

/* ── Base ──────────────────────────────────────────────────────── */

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

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  transition: background-color .2s, color .2s;
}

h1, h2, h3, .display {
  font-family: "Chakra Petch", "IBM Plex Sans", system-ui, sans-serif;
  font-weight: 600;
  letter-spacing: .01em;
  line-height: 1.15;
  margin: 0;
}

a { color: var(--accent); }

.mono { font-family: "IBM Plex Mono", ui-monospace, monospace; }

.muted { color: var(--muted); }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

[hidden] { display: none !important; }

/* ── Buttons and inputs ────────────────────────────────────────── */

.btn {
  --btn-bg: var(--surface-2);
  --btn-ink: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .45rem;
  min-height: 40px;
  padding: 0 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--btn-bg);
  color: var(--btn-ink);
  font: 600 14px/1 "IBM Plex Sans", system-ui, sans-serif;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  transition: filter .15s, transform .05s;
}
.btn:hover { filter: brightness(1.05); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { --btn-bg: var(--accent); --btn-ink: var(--accent-ink); border-color: transparent; }
.btn.gold { --btn-bg: var(--accent-2); --btn-ink: var(--accent-2-ink); border-color: transparent; }
.btn.ghost { --btn-bg: transparent; border-color: transparent; }
.btn.danger { --btn-bg: transparent; --btn-ink: var(--danger); border-color: var(--danger); }
.btn.block { width: 100%; }
.btn svg { width: 18px; height: 18px; flex: none; }

.field { display: grid; gap: .35rem; margin-bottom: 1rem; }
.field label { font-weight: 600; font-size: 13px; }
.field .hint { font-size: 12.5px; color: var(--muted); }

input[type="text"],
input[type="password"],
input[type="search"],
select {
  width: 100%;
  min-height: 42px;
  padding: 0 .8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: 15px/1.2 "IBM Plex Sans", system-ui, sans-serif;
}
input:focus, select:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }

input[type="file"] {
  width: 100%;
  padding: .6rem;
  border: 1px dashed var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 14px;
}

.check { display: flex; align-items: center; gap: .5rem; font-size: 14px; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }

.alert {
  padding: .7rem .9rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 14px;
  margin-bottom: 1rem;
}
.alert.error { border-color: var(--danger); color: var(--danger); background: color-mix(in srgb, var(--danger) 8%, var(--surface)); }
.alert.ok { border-color: var(--ok); color: var(--ok); background: color-mix(in srgb, var(--ok) 8%, var(--surface)); }

/* ── Brand mark ────────────────────────────────────────────────── */

.brand {
  display: flex;
  align-items: center;
  gap: .6rem;
  color: var(--text);
  text-decoration: none;
}
.brand-mark {
  width: 30px;
  height: 34px;
  flex: none;
}
.brand-mark .shell { fill: var(--accent); }
.brand-mark .label { fill: var(--accent-2); }
.brand-mark .ridge { fill: var(--accent-ink); opacity: .55; }
.brand-name { font-family: "Chakra Petch", sans-serif; font-weight: 700; font-size: 19px; letter-spacing: .02em; }

/* ── Sign-in page ──────────────────────────────────────────────── */

.auth-page {
  display: grid;
  place-items: center;
  padding: 24px 16px;
  background:
    radial-gradient(circle at 15% 10%, color-mix(in srgb, var(--accent) 12%, transparent), transparent 45%),
    radial-gradient(circle at 85% 90%, color-mix(in srgb, var(--accent-2) 18%, transparent), transparent 45%),
    var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.auth-card .stripe {
  height: 8px;
  background: linear-gradient(90deg, var(--accent) 0 65%, var(--accent-2) 65% 100%);
}
.auth-body { padding: 26px 24px 22px; }
.auth-body .brand { margin-bottom: 6px; }
.auth-body .tagline { color: var(--muted); font-size: 14px; margin: 0 0 20px; }

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  margin-bottom: 18px;
  background: var(--surface-2);
  border-radius: 10px;
}
.tabs button {
  min-height: 36px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font: 600 14px "IBM Plex Sans", sans-serif;
  cursor: pointer;
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

.auth-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 24px;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 13px;
  color: var(--muted);
}
.auth-foot select { width: auto; min-height: 34px; font-size: 13px; }

/* ── App header ────────────────────────────────────────────────── */

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px max(16px, env(safe-area-inset-left)) 10px max(16px, env(safe-area-inset-right));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.app-header .spacer { flex: 1; }
.app-header select { width: auto; min-height: 36px; font-size: 13.5px; }
.user-chip {
  display: inline-flex;
  align-items: center;
  gap: .45rem;
  padding: 0 .3rem 0 .7rem;
  min-height: 36px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-size: 13.5px;
  font-weight: 600;
}
.user-chip .btn { min-height: 28px; padding: 0 .6rem; font-size: 12.5px; border-radius: 999px; }

/* ── Library ───────────────────────────────────────────────────── */

.wrap { max-width: 1180px; margin: 0 auto; padding: 22px 16px 60px; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 18px;
}
.toolbar h1 { font-size: 26px; margin-right: auto; }
.toolbar .count { font-size: 13px; color: var(--muted); font-weight: 400; margin-left: .4rem; }

.filters {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  padding-bottom: 2px;
  margin-bottom: 20px;
}
.filters::-webkit-scrollbar { display: none; }
.chip {
  flex: none;
  min-height: 34px;
  padding: 0 .85rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font: 600 13px "IBM Plex Sans", sans-serif;
  cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }
.search { flex: 1 1 200px; max-width: 280px; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(168px, 1fr));
  gap: 18px;
}

.card {
  position: relative;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px 22px 10px 10px; /* notched corner, like a cartridge */
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: transform .15s, box-shadow .15s;
}
.card:hover { transform: translateY(-2px); }
.card .cover {
  position: relative;
  display: block;
  aspect-ratio: 3 / 4;
  background: var(--surface-2);
  overflow: hidden;
}
.card .cover img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block; }
.card .play-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, .35);
  opacity: 0;
  transition: opacity .15s;
}
.card .play-overlay span {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-ink);
}
.card .play-overlay svg { width: 26px; height: 26px; margin-left: 3px; }
.card .cover:hover .play-overlay,
.card .cover:focus-visible .play-overlay { opacity: 1; }

.tile {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 14px;
  color: var(--tile-ink);
  background:
    repeating-linear-gradient(0deg, rgba(255,255,255,.07) 0 2px, transparent 2px 6px),
    linear-gradient(160deg, var(--tint), color-mix(in srgb, var(--tint) 55%, #000));
}
.tile .tile-sys { font: 700 12px "IBM Plex Mono", monospace; letter-spacing: .12em; opacity: .9; }
.tile .tile-title { font: 700 20px/1.1 "Chakra Petch", sans-serif; text-wrap: balance; }
.tile .tile-ridges { height: 10px; background: repeating-linear-gradient(90deg, rgba(255,255,255,.25) 0 3px, transparent 3px 7px); border-radius: 2px; }

.card .meta { display: flex; align-items: flex-start; gap: 6px; padding: 10px 6px 10px 12px; }
.card .meta-text { flex: 1; min-width: 0; }
.card .title { font-weight: 600; font-size: 14.5px; line-height: 1.25; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.card .sub { display: flex; align-items: center; gap: 6px; margin-top: 5px; font-size: 12px; color: var(--muted); }
.badge {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--accent-2);
  color: var(--accent-2-ink);
  font: 700 10.5px/1.3 "IBM Plex Mono", monospace;
  letter-spacing: .05em;
}
.icon-btn {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  flex: none;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; }

.menu {
  position: fixed;
  z-index: 50;
  min-width: 190px;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0,0,0,.2);
}
.menu button {
  display: flex;
  align-items: center;
  gap: .6rem;
  width: 100%;
  min-height: 38px;
  padding: 0 .7rem;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  font: 14px "IBM Plex Sans", sans-serif;
  text-align: left;
  cursor: pointer;
}
.menu button:hover { background: var(--surface-2); }
.menu button.danger { color: var(--danger); }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 4px 0; }

.empty {
  display: grid;
  justify-items: center;
  gap: 10px;
  padding: 56px 20px;
  text-align: center;
  border: 2px dashed var(--border);
  border-radius: 14px;
  color: var(--muted);
}
.empty h2 { color: var(--text); font-size: 20px; }

/* ── Dialogs ───────────────────────────────────────────────────── */

dialog {
  width: min(520px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 24px 60px rgba(0,0,0,.3);
}
dialog::backdrop { background: rgba(10, 8, 6, .5); }
.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px 12px;
  border-bottom: 1px solid var(--border);
}
.dialog-head h2 { font-size: 19px; }
.dialog-body { padding: 18px; }
.dialog-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 18px 16px;
  border-top: 1px solid var(--border);
}
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.progress {
  height: 8px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
  margin: 6px 0 4px;
}
.progress > div {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width .2s;
}

.cover-preview {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 8px;
}
.cover-preview img { width: 54px; height: 72px; object-fit: cover; border-radius: 4px; border: 1px solid var(--border); }

/* ── Admin panel ───────────────────────────────────────────────── */

.admin {
  margin-top: 40px;
  padding: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.admin h2 { font-size: 19px; margin-bottom: 4px; }
.admin table { width: 100%; border-collapse: collapse; font-size: 14px; margin-top: 12px; }
.admin th, .admin td { padding: 8px 6px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
.admin th { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.admin td.actions { text-align: right; white-space: nowrap; }
.admin td.actions .btn { min-height: 32px; padding: 0 .7rem; font-size: 13px; }
.usage-bar { height: 6px; border-radius: 999px; background: var(--surface-2); overflow: hidden; margin-top: 8px; max-width: 360px; }
.usage-bar > div { height: 100%; background: var(--accent); }
.status-pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.status-pill.pending { background: var(--accent-2); color: var(--accent-2-ink); }
.status-pill.active { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }

/* ── Player ────────────────────────────────────────────────────── */

.play-page {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: #000;
}
.play-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px max(12px, env(safe-area-inset-left)) 8px max(12px, env(safe-area-inset-right));
  background: var(--surface);
  border-bottom: 2px solid var(--accent);
  color: var(--text);
}
.play-bar .game-title { flex: 1; min-width: 0; font-family: "Chakra Petch", sans-serif; font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.play-bar .btn { min-height: 34px; padding: 0 .75rem; font-size: 13px; }
.sync {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
}
.sync .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.sync[data-state="ok"] .dot { background: var(--ok); }
.sync[data-state="busy"] .dot { background: var(--accent-2); animation: pulse 1s infinite; }
.sync[data-state="error"] .dot { background: var(--danger); }
.sync[data-state="error"] { color: var(--danger); }
@keyframes pulse { 50% { opacity: .3; } }

.stage { position: relative; flex: 1; min-height: 0; background: var(--bg); }
#game { width: 100%; height: 100%; }
.loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 12px;
  justify-items: center;
  text-align: center;
  padding: 20px;
  color: var(--text);
  background: var(--bg);
}
.loading .progress { width: min(320px, 80vw); }
.rotate-hint { display: none; font-size: 13px; color: var(--muted); }

/* ── Small screens ─────────────────────────────────────────────── */

@media (max-width: 640px) {
  .app-header { gap: 8px; }
  .brand-name { display: none; }
  .app-header select { max-width: 132px; }
  .user-chip .name { max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .toolbar h1 { font-size: 22px; width: 100%; }
  .search { max-width: none; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
  .tile .tile-title { font-size: 17px; }
  .row { grid-template-columns: 1fr; gap: 0; }
  .play-bar .sync .label { display: none; }
  .admin { padding: 14px; }
  .admin .hide-sm { display: none; }
}

@media (max-width: 640px) and (orientation: portrait) {
  .rotate-hint { display: block; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
