:root {
  --bg: #f4f5f7;
  --surface: #fff;
  --border: #e2e5ec;
  --border-soft: #eef0f4;
  --text: #1f2430;
  --muted: #6b7280;
  --primary: #2d6cdf;
  --primary-dark: #2358bb;
  --danger: #d64545;
  --accent: #ffc94d;
  --sidebar-bg: #1c2230;
  --sidebar-text: #aeb6c8;
  --sidebar-w: 232px;
  --radius: 10px;
  --radius-sm: 6px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* ---- Layout ---- */

body.with-sidebar { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--sidebar-bg);
  padding: 18px 12px 14px;
}

.brand {
  color: #fff;
  text-decoration: none;
  font-weight: 700;
  font-size: 17px;
  padding: 4px 10px 16px;
}
.brand span { color: var(--accent); }
.brand small {
  font-size: 11px;
  font-weight: 600;
  color: var(--sidebar-text);
  text-transform: uppercase;
  letter-spacing: .08em;
  margin-left: 4px;
}

.nav { flex: 1; display: flex; flex-direction: column; gap: 18px; overflow-y: auto; }
.nav-group { display: flex; flex-direction: column; gap: 2px; }
.nav-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: #6c7689;
  padding: 0 10px 6px;
}
.nav-link {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--sidebar-text);
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
}
.nav-link:hover { background: rgba(255, 255, 255, .06); color: #fff; }
.nav-link.active { background: var(--primary); color: #fff; }
.nav-icon { width: 20px; text-align: center; font-size: 15px; }

.sidebar-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 12px 10px 0;
  border-top: 1px solid rgba(255, 255, 255, .08);
}
.sidebar-user {
  color: var(--sidebar-text);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar-logout { color: #fff; font-size: 13px; text-decoration: none; opacity: .8; }
.sidebar-logout:hover { opacity: 1; }

.wrap {
  flex: 1;
  min-width: 0;
  max-width: 1360px;
  margin: 0 auto;
  padding: 28px 32px 48px;
}
body.bare .wrap { padding: 24px 16px; }

@media (max-width: 800px) {
  body.with-sidebar { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; padding: 12px; }
  .nav { flex-direction: row; gap: 10px; overflow-x: auto; padding: 4px 0; }
  .nav-group { flex-direction: row; gap: 2px; }
  .nav-label { display: none; }
  .nav-link { white-space: nowrap; padding: 6px 10px; }
  .sidebar-foot { padding-top: 8px; }
  .wrap { padding: 16px; }
}

/* ---- Typography & page head ---- */

h1 { font-size: 22px; margin: 0 0 12px; }
h1 a { color: inherit; }
h2 { font-size: 15px; margin: 0 0 12px; }

.page-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.page-head h1 { margin: 0; }

.muted { color: var(--muted); font-size: 14px; }

/* ---- Cards ---- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin-bottom: 18px;
}

.card-narrow { max-width: 360px; margin: 60px auto; }
.card-narrow form { display: flex; flex-direction: column; gap: 10px; }

/* ---- Forms & buttons ---- */

input[type="text"], input[type="password"], input[type="email"], select {
  padding: 8px 10px;
  border: 1px solid #cfd4e0;
  border-radius: var(--radius-sm);
  font-size: 14px;
  background: #fff;
}
input:focus, select:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
}

.btn {
  padding: 8px 14px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  background: var(--border);
  color: var(--text);
  text-decoration: none;
  display: inline-block;
}
.btn:hover { filter: brightness(.96); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); filter: none; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-small { padding: 4px 10px; font-size: 12px; }

.btn-discord {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  padding: 11px 14px;
  background: #5865f2;
  color: #fff;
  text-decoration: none;
  font-weight: 600;
  border-radius: var(--radius-sm);
}
.btn-discord:hover { background: #4752c4; }

.inline-form { display: inline-flex; gap: 8px; }
.upload-form { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

.stack-form { display: flex; flex-direction: column; gap: 12px; max-width: 420px; }
.stack-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}
.stack-form button { align-self: flex-start; }

/* ---- Lists ---- */

.game-list { list-style: none; padding: 0; margin: 0; }
.game-list li {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 8px;
}
.game-list li:hover { border-color: var(--primary); }
.game-list a {
  display: block;
  padding: 12px 16px;
  text-decoration: none;
  color: var(--text);
  font-weight: 600;
}

/* ---- Tables ---- */

.table-scroll {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.table-scroll .file-table { border: none; border-radius: 0; }

.file-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.file-table th, .file-table td {
  text-align: left;
  padding: 9px 12px;
  border-bottom: 1px solid var(--border-soft);
  font-size: 14px;
}
.file-table tbody tr:last-child td { border-bottom: none; }
.file-table tbody tr:hover { background: #fafbfd; }
.file-table th {
  background: #fafbfc;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .03em;
  color: var(--muted);
}
.file-table a { color: var(--primary); text-decoration: none; word-break: break-all; }
.row-actions { white-space: nowrap; text-align: right; }

/* ---- Flash & chips ---- */

.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; font-size: 14px; }
.flash-success { background: #e3f6e8; color: #1c6b34; }
.flash-error { background: #fde8e8; color: #9b2222; }

.chip {
  display: inline-block;
  background: var(--border-soft);
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 12px;
  color: #4b5563;
  margin: 1px 2px 1px 0;
  white-space: nowrap;
}
.chip-warn { background: #fdf0d5; color: #8a5a00; }
.chip-ok { background: #e3f6e8; color: #1c6b34; }

/* ---- Characters (Blokies sprites) ---- */

.char-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.char-head h2 { margin: 0; }
.char-head code { font-size: 12px; font-weight: 400; }

.sprite-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
}
.sprite-tile {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  text-align: center;
}
.sprite-tile img {
  width: 100%;
  height: 96px;
  object-fit: contain;
  background:
    repeating-conic-gradient(#f0f1f4 0% 25%, #fafbfc 0% 50%) 0 0 / 16px 16px;
  border-radius: 6px;
}
.sprite-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  margin: 6px 0;
  font-size: 13px;
}
.sprite-actions { display: flex; justify-content: center; gap: 6px; }
.sprite-missing { border-style: dashed; }
.sprite-hole {
  height: 96px;
  border-radius: 6px;
  background: #fafbfc;
}

.held-list { list-style: none; padding: 0; margin: 0; font-size: 14px; }
.held-list li { padding: 4px 0; border-top: 1px solid var(--border-soft); }
.held-list li:first-child { border-top: none; }
.held-list code { color: var(--muted); font-size: 12px; }

/* ---- Jawi spelling audit ---- */

.filterbar {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.filterbar input[type="text"] { min-width: 220px; }

.jawi-cell {
  font-family: "Noto Naskh Arabic", "Geeza Pro", "Al Bayan", "Amiri", serif;
  font-size: 24px;
  line-height: 1.6;
  white-space: nowrap;
}
.jawi-input {
  font-family: "Noto Naskh Arabic", "Geeza Pro", "Al Bayan", "Amiri", serif;
  font-size: 18px;
  min-width: 140px;
}

.occ { margin-top: 6px; }
.occ summary { cursor: pointer; font-size: 12px; }
.occ ul { list-style: none; padding: 6px 0 0; margin: 0; }
.occ li {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  padding: 4px 0;
  border-top: 1px solid var(--border-soft);
  font-size: 13px;
}
.occ code { font-size: 12px; color: var(--muted); }
.row-actions .inline-form { display: flex; }
.row-actions .jawi-input { width: 300px; }

/* ---- Parents / purchases / metrics (business pages) ---- */

.chip-err { background: #fde8e8; color: #9b2222; }
a.chip { text-decoration: none; }
a.chip:hover { filter: brightness(.96); }
.chip-active { background: var(--primary); color: #fff; }

.search-form { display: flex; gap: 8px; margin-bottom: 16px; max-width: 480px; }
.search-form input[type="search"] { flex: 1; }

.filter-row { display: flex; gap: 4px; margin: 16px 0 10px; }
.pager { display: flex; gap: 12px; align-items: center; margin-top: 12px; }
.row-actions form { display: inline-block; margin-left: 4px; }

.kv-table th { text-align: left; padding: 4px 16px 4px 0; color: var(--muted); font-weight: 500; }
.kv-table td { padding: 4px 0; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}
.stat {
  background: #fff;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.stat-num { font-size: 24px; font-weight: 700; }
.stat-label { font-size: 12px; color: var(--muted); }

/* ---- Analytics dashboard ---- */

.tab-bar {
  display: flex;
  gap: 2px;
  border-bottom: 2px solid var(--border);
  margin-bottom: 16px;
}
.tab-link {
  padding: 8px 14px 9px;
  font-size: 13.5px;
  font-weight: 550;
  color: var(--muted);
  text-decoration: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
}
.tab-link:hover { color: var(--text); }
.tab-link.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.control-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.control-bar .js-right { margin-left: auto; }
.ctl-select {
  padding: 5px 8px;
  border: 1px solid #cfd4e0;
  border-radius: var(--radius-sm);
  font-size: 13px;
  background: #fff;
  color: var(--text);
}
.seg-link {
  display: inline-block;
  border: none;
  padding: 4px 11px;
  font-size: 12.5px;
  font-weight: 550;
  color: var(--muted);
  border-radius: 6px;
  text-decoration: none;
}
.seg-link:hover { color: var(--text); }
.seg-link.on {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(24, 32, 55, .12);
}
button.chip { border: none; cursor: pointer; font-family: inherit; }
button.chip:disabled { opacity: .5; cursor: default; }

.kpi-cover { position: absolute; inset: 0; z-index: 1; }
.kpi-linked:hover { border-color: var(--primary); }
.kpi-spark {
  width: 100%;
  height: 26px;
  margin-top: 6px;
  display: block;
}

.cohort-table { border-collapse: collapse; font-size: 12.5px; }
.cohort-table th, .cohort-table td {
  padding: 5px 8px;
  text-align: center;
  border: 1px solid #fff;
  min-width: 44px;
}
.cohort-table th {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  font-weight: 600;
}
.cohort-table td.c-label {
  text-align: left;
  white-space: nowrap;
  color: var(--text);
}
.cohort-table td.c-cell { font-weight: 600; font-variant-numeric: tabular-nums; }
.cohort-table td.c-dark { color: #fff; }
.cohort-table td.c-blank { background: var(--border-soft); }

table.sortable th[data-sort] { cursor: pointer; user-select: none; }
table.sortable th[data-sort]:hover { color: var(--text); }
table.sortable th[aria-sort="descending"]::after { content: " ▾"; }
table.sortable th[aria-sort="ascending"]::after { content: " ▴"; }

.spark-cell svg { width: 90px; height: 22px; display: block; }

.has-tip { border-bottom: 1px dotted var(--muted); cursor: help; }

.buyer-chip-never { background: #fde8e8; color: #9b2222; }
.buyer-chip-played { background: #fdf0d5; color: #8a5a00; }
.buyer-chip-retained { background: #e3f6e8; color: #1c6b34; }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 14px;
  margin-bottom: 22px;
}
.kpi {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  box-shadow: 0 1px 2px rgba(24, 32, 55, .04);
}
.kpi-num {
  font-size: 27px;
  font-weight: 750;
  letter-spacing: -.02em;
  line-height: 1.15;
}
.kpi-label {
  font-size: 12px;
  font-weight: 550;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
}
.kpi-delta {
  position: absolute;
  top: 12px;
  right: 14px;
  font-size: 12px;
  font-weight: 650;
  border-radius: 999px;
  padding: 2px 8px;
}
.kpi-delta.up { background: #e3f6e8; color: #1c6b34; }
.kpi-delta.down { background: #fde8e8; color: #9b2222; }

.dash-grid {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 14px;
  margin-bottom: 26px;
}
.dash-wide { grid-column: auto; }
@media (max-width: 1100px) {
  .dash-grid { grid-template-columns: 1fr; }
}

.dash-card { margin-bottom: 0; box-shadow: 0 1px 2px rgba(24, 32, 55, .04); }
.dash-card h3, .table-card h3 {
  margin: 0 0 14px;
  font-size: 14px;
  font-weight: 650;
}
.dash-card h3 .muted, .table-card h3 .muted {
  font-weight: 400;
  font-size: 12.5px;
}
.chart-box { position: relative; height: 240px; }
.chart-box-tall { height: 300px; }

.chart-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  flex-wrap: wrap;
}
.chart-controls { display: flex; gap: 8px; flex-wrap: wrap; }
.seg {
  display: inline-flex;
  background: var(--border-soft);
  border-radius: 8px;
  padding: 2px;
}
.seg button {
  border: none;
  background: transparent;
  padding: 4px 11px;
  font-size: 12.5px;
  font-weight: 550;
  color: var(--muted);
  border-radius: 6px;
  cursor: pointer;
  font-family: inherit;
}
.seg button:hover { color: var(--text); }
.seg button.on {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(24, 32, 55, .12);
}

.attn { display: flex; flex-direction: column; gap: 16px; }
.attn-group { display: flex; flex-direction: column; gap: 6px; }
.attn-title {
  font-size: 11.5px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
}
.attn-title.attn-bad { color: #9b2222; }
.attn-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  font-size: 13.5px;
  padding: 5px 0;
  border-bottom: 1px solid var(--border-soft);
}
.attn-group .attn-row:last-child { border-bottom: none; }
.attn-pct { font-weight: 650; font-variant-numeric: tabular-nums; }
.attn-pct.attn-bad { color: #9b2222; }

.funnel { display: flex; flex-direction: column; gap: 10px; }
.funnel-row {
  display: grid;
  grid-template-columns: 220px 1fr 110px;
  align-items: center;
  gap: 12px;
  font-size: 13.5px;
}
.funnel-label { color: var(--text); }
.funnel-track {
  height: 22px;
  background: var(--border-soft);
  border-radius: 6px;
  overflow: hidden;
}
.funnel-fill {
  height: 100%;
  min-width: 2px;
  background: linear-gradient(90deg, #4d84e8, var(--primary));
  border-radius: 6px;
}
.funnel-n { font-weight: 650; font-variant-numeric: tabular-nums; text-align: right; }
.funnel-row.wide-n { grid-template-columns: 220px 1fr 170px; }
.split-track { background: #f6dede; }
.split-good { background: linear-gradient(90deg, #46b06d, #37a463); }
@media (max-width: 700px) {
  .funnel-row { grid-template-columns: 1fr; gap: 4px; }
  .funnel-n { text-align: left; }
}

.table-card {
  padding: 16px 18px 6px;
  box-shadow: 0 1px 2px rgba(24, 32, 55, .04);
}
.table-card .file-table { border: none; margin: 0 -18px; width: calc(100% + 36px); }
.table-card .file-table th { background: transparent; }
.section-title { margin: 26px 0 14px; font-size: 16px; }

.metrics-table .bar {
  display: inline-block;
  height: 10px;
  background: var(--primary);
  opacity: .35;
  border-radius: 3px;
  margin-right: 6px;
  vertical-align: middle;
}
.metrics-table .bar-money { background: #1c6b34; }
.metrics-table .bar-kids { background: #8a5a00; }

/* ---- Mobile overrides that must beat component rules above ---- */

@media (max-width: 800px) {
  .file-table { display: block; overflow-x: auto; }
}

/* ---- UPKK content browser ---- */
.upkk-kpis { display:grid; grid-template-columns:repeat(4,1fr); gap:12px; margin-bottom:18px; }
.upkk-kpis .card { margin:0; display:flex; flex-direction:column; gap:3px; }
.upkk-kpis strong { font-size:28px; }
.upkk-kpis span { color:var(--muted); font-size:13px; }
.upkk-filters { display:flex; align-items:end; gap:12px; flex-wrap:wrap; }
.upkk-filters label { display:flex; flex-direction:column; gap:5px; font-size:12px; font-weight:650; }
.upkk-filters select,.upkk-filters input { min-height:38px; border:1px solid var(--border); border-radius:7px; padding:7px 9px; background:var(--surface); color:var(--text); }
.upkk-search { flex:1; min-width:220px; }
.upkk-item { padding:0; overflow:hidden; }
.upkk-item summary { cursor:pointer; display:flex; justify-content:space-between; align-items:center; gap:16px; padding:15px 18px; }
.upkk-item summary>span:first-child { display:flex; flex-direction:column; gap:5px; }
.upkk-item summary code { color:var(--muted); font-size:11px; }
.upkk-summary-meta { display:flex; align-items:center; gap:9px; color:var(--muted); font-size:12px; }
.upkk-item-body { border-top:1px solid var(--border-soft); padding:18px; display:grid; grid-template-columns:minmax(260px,.8fr) minmax(320px,1.2fr); gap:22px; }
.upkk-card-preview { background:#f7f8fb; border:1px solid var(--border-soft); border-radius:10px; padding:15px; }
.upkk-card-preview small { color:var(--muted); font-weight:700; text-transform:uppercase; letter-spacing:.04em; }
.upkk-card-preview p { margin:5px 0 15px; white-space:pre-wrap; }
.upkk-question { border-top:1px solid var(--border-soft); padding:10px 0; }
.upkk-question strong { display:block; margin-top:7px; }
.upkk-question p { margin:5px 0 0; }
@media (max-width:800px) {
  .upkk-kpis { grid-template-columns:repeat(2,1fr); }
  .upkk-item-body { grid-template-columns:1fr; }
  .upkk-item summary { align-items:flex-start; flex-direction:column; }
}

/* ---- Courses: subject×stage matrix ---- */
.legend { display: flex; align-items: center; gap: 14px; font-size: 13px; color: var(--text); }
.legend-item { display: inline-flex; align-items: center; gap: 5px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.dot-visible { background: #2f9e57; }
.dot-admin { background: #e0a72c; }
.dot-hidden { background: #b6bcc8; }
.help { margin: 0 0 14px; font-size: 13px; }
.help summary { cursor: pointer; color: var(--muted); user-select: none; }
.help p { margin: 8px 0 0; max-width: 760px; }
.matrix-card { padding: 0; overflow: hidden; }
.matrix th, .matrix td { text-align: center; }
.matrix thead th:first-child { text-align: left; }
.matrix .mx-subject {
  text-align: left; font-size: 13px; font-weight: 650;
  background: #fafbfd; white-space: nowrap;
}
.matrix .mx-cell { padding: 8px 6px 9px; min-width: 96px; vertical-align: top; }
.matrix .mx-cell.st-admin { background: #fffaf0; }
.matrix .mx-cell.st-hidden { background: #f6f7f9; }
.matrix .mx-empty { color: #cfd4de; }
.mx-games { display: block; font-size: 12px; margin-bottom: 5px; white-space: nowrap; }
.mx-note { display: block; font-size: 11px; color: var(--muted); margin-top: 4px; white-space: nowrap; }
.mx-cell .chip { display: inline-block; margin-top: 4px; }
.status-select {
  width: 100%; min-width: 84px; padding: 3px 2px; font-size: 12px;
  border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer;
}
.status-select.st-visible { background: #e3f6e8; border-color: #bfe5cb; color: #1c6b34; }
.status-select.st-admin { background: #fdf0d5; border-color: #efdaa8; color: #8a5a00; }
.status-select.st-hidden { background: #eef0f4; border-color: #dfe3ea; color: #4b5262; }
