:root {
  color-scheme: light;
  --bg: #f4f3f0;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e0da;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7873;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;   /* Tartil */
  --series-2: #eb6834;   /* Jandalah */
  --grid: #e8e6e1;
  --good: #1f7a3a; --good-bg: #e3f3e7;
  --warn: #8a5a00; --warn-bg: #fdf1d6;
  --bad: #b3261e;  --bad-bg: #fbe4e2;
  --seq-0: #f0efec; --seq-1: #cde2fb; --seq-2: #9ec5f4; --seq-3: #6da7ec;
  --seq-4: #3987e5; --seq-5: #256abf; --seq-6: #184f95; --seq-7: #0d366b;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 2px 8px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110; --surface: #1a1a19; --surface-2: #232321; --border: #2f2f2c;
    --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86;
    --accent: #3987e5; --series-1: #3987e5; --series-2: #d95926; --grid: #2a2a27;
    --good: #7fd49a; --good-bg: #16301f; --warn: #f0c35a; --warn-bg: #33280f; --bad: #f19b95; --bad-bg: #3a1a18;
    --seq-0: #232321; --seq-1: #104281; --seq-2: #184f95; --seq-3: #1c5cab;
    --seq-4: #256abf; --seq-5: #3987e5; --seq-6: #6da7ec; --seq-7: #9ec5f4;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110; --surface: #1a1a19; --surface-2: #232321; --border: #2f2f2c;
  --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86;
  --accent: #3987e5; --series-1: #3987e5; --series-2: #d95926; --grid: #2a2a27;
  --good: #7fd49a; --good-bg: #16301f; --warn: #f0c35a; --warn-bg: #33280f; --bad: #f19b95; --bad-bg: #3a1a18;
  --seq-0: #232321; --seq-1: #104281; --seq-2: #184f95; --seq-3: #1c5cab;
  --seq-4: #256abf; --seq-5: #3987e5; --seq-6: #6da7ec; --seq-7: #9ec5f4;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", "Noto Sans Arabic", sans-serif;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
h3 { margin: 0; font-size: 15px; }
h4 { margin: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.muted { color: var(--text-2); }
.small { font-size: 12px; }
.error { color: var(--bad); min-height: 1em; margin: 6px 0 0; white-space: pre-line; }

.brand { font-weight: 700; letter-spacing: .01em; display: flex; align-items: center; gap: 8px; }
.brand-mark { color: var(--series-2); }

.btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 8px; padding: 7px 12px; font: inherit; cursor: pointer; white-space: nowrap;
  display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.small { padding: 3px 8px; font-size: 12px; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 7px; padding: 6px 8px; width: 100%;
}
label { display: flex; flex-direction: column; gap: 4px; color: var(--text-2); font-size: 12px; }
label.inline { flex-direction: row; align-items: center; gap: 6px; }
label.inline input, label.inline select { width: auto; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.csv { font-size: 12px; cursor: pointer; text-decoration: none; border: 1px solid var(--border); border-radius: 6px; padding: 2px 8px; color: var(--text-2); }
.csv:hover { background: var(--surface-2); }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); display: flex; flex-direction: column; gap: 12px; }
.login-card .brand { font-size: 18px; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 16px;
  padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.tabs { display: flex; gap: 4px; }
.tab { border: 0; background: transparent; color: var(--text-2); font: inherit; padding: 7px 12px; border-radius: 8px; cursor: pointer; }
.tab[aria-selected="true"] { background: var(--surface-2); color: var(--text); font-weight: 600; }
.topbar-right { margin-left: auto; display: flex; gap: 4px; }

.view { max-width: 1320px; margin: 0 auto; padding: 16px 20px 40px; display: flex; flex-direction: column; gap: 16px; }
.filters { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: center; position: sticky; top: 56px; z-index: 4; }
.spacer { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); color: var(--text-2); font: inherit; padding: 6px 11px; cursor: pointer; border-right: 1px solid var(--border); }
.seg button:last-child { border-right: 0; }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }

/* live */
.live-head { display: grid; grid-template-columns: minmax(180px, 260px) 1fr; gap: 20px; align-items: center; }
.eyebrow { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-2); display: flex; align-items: center; gap: 8px; }
.pulse { width: 8px; height: 8px; border-radius: 50%; background: var(--good); box-shadow: 0 0 0 0 var(--good); animation: pulse 2s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--good) 60%, transparent); } 100% { box-shadow: 0 0 0 10px transparent; } }
.hero { font-size: 56px; font-weight: 700; line-height: 1.05; font-variant-numeric: tabular-nums; }
.live-chart { height: 130px; }
.live-lists { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-top: 14px; }
.rank { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 4px; }
.rank li { display: flex; justify-content: space-between; gap: 8px; border-bottom: 1px dashed var(--border); padding: 3px 0; }
.rank li span:last-child { font-variant-numeric: tabular-nums; color: var(--text-2); }

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.kpi .label { font-size: 12px; color: var(--text-2); }
.kpi .value { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; }
.kpi .sub { font-size: 11px; color: var(--muted); }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.chart { position: relative; height: 260px; }
.chart.tall { height: 360px; }

/* heatmap */
.heatmap { display: grid; grid-template-columns: 34px repeat(24, 1fr); gap: 2px; font-size: 10px; color: var(--text-2); }
.heatmap .cell { aspect-ratio: 1; border-radius: 3px; background: var(--seq-0); position: relative; }
.heatmap .cell:hover { outline: 2px solid var(--text); z-index: 1; }
.heatmap .hl { text-align: center; }
.heatmap .dl { display: flex; align-items: center; }
.legend-scale { display: flex; align-items: center; gap: 4px; margin-top: 8px; font-size: 11px; color: var(--text-2); }
.legend-scale i { width: 16px; height: 10px; border-radius: 2px; display: inline-block; }

/* tables */
.table-wrap { overflow-x: auto; max-height: 440px; overflow-y: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th { text-align: left; font-weight: 600; color: var(--text-2); font-size: 12px; position: sticky; top: 0; background: var(--surface); border-bottom: 1px solid var(--border); padding: 6px 8px; cursor: default; }
.table th.sortable { cursor: pointer; }
.table td { padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table td.num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.bar-cell { display: flex; align-items: center; gap: 6px; justify-content: flex-end; }
.bar-cell i { display: inline-block; height: 8px; border-radius: 0 4px 4px 0; background: var(--series-1); }
.empty { color: var(--muted); padding: 20px; text-align: center; }

/* playlists */
.pl-status { color: var(--text-2); }
.card .table-wrap:has(> .tracks) { max-height: 70vh; }
.tracks tr.dirty td:first-child { box-shadow: inset 3px 0 0 var(--series-2); }
.tracks tr.dragging { opacity: .4; }
.tracks tr.drop-above td { border-top: 2px solid var(--accent); }
.handle { cursor: grab; color: var(--muted); user-select: none; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 99px; font-weight: 600; white-space: nowrap; }
.badge.cleared { background: var(--good-bg); color: var(--good); }
.badge.pending { background: var(--warn-bg); color: var(--warn); }
.badge.blocked { background: var(--bad-bg); color: var(--bad); }
.row-actions { display: flex; gap: 6px; }
.host { color: var(--muted); font-size: 12px; }
.title-cell { min-width: 180px; text-align: left; unicode-bidi: plaintext; }
.table.tracks td:nth-child(3), #t-tracks td.title-cell { width: 45%; }
.title-cell .sub { color: var(--text-2); font-size: 12px; }

/* dialogs */
dialog { border: 1px solid var(--border); border-radius: 12px; padding: 0; background: var(--surface); color: var(--text); width: min(720px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.4); }
.dialog { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; }
.form-grid .wide { grid-column: 1 / -1; }
.measure { display: flex; align-items: flex-end; gap: 8px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.diff li { margin: 3px 0; }
.diff .added { color: var(--good); }
.diff .removed { color: var(--bad); }
.problems { background: var(--bad-bg); color: var(--bad); border-radius: 8px; padding: 10px 14px; }
.problems ul { margin: 6px 0 0; padding-left: 18px; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; z-index: 50; max-width: calc(100vw - 32px); }
.foot { text-align: center; padding-top: 8px; }

@media (max-width: 900px) {
  .grid2, .grid3 { grid-template-columns: 1fr; }
  .live-head { grid-template-columns: 1fr; }
  .filters { position: static; }
}
@media (max-width: 600px) {
  .topbar { padding: 10px 16px; flex-wrap: wrap; }
  .view { padding: 12px 16px 32px; }
  .live-lists, .form-grid { grid-template-columns: 1fr; }
  .hero { font-size: 44px; }
  .heatmap { font-size: 8px; grid-template-columns: 26px repeat(24, 1fr); gap: 1px; }
}
