:root {
  --bg: #f3f2ee;
  --surface: #ffffff;
  --surface-2: #faf9f6;
  --ink: #16181d;
  --ink-2: #3a3d45;
  --muted: #747882;
  --line: #e5e3dc;
  --line-2: #efede7;
  --accent: #0e6e5c;
  --accent-soft: #e3f0ec;
  --danger: #b42318;
  --danger-soft: #fbeae8;
  --side: #121418;
  --side-ink: #a9adb6;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 22, 28, .04), 0 2px 8px rgba(20, 22, 28, .04);
  --font: 'IBM Plex Sans Arabic', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }
.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }

/* ---------- Layout ---------- */
.shell { display: grid; grid-template-columns: 248px 1fr; min-height: 100vh; }

.sidebar {
  background: var(--side);
  color: var(--side-ink);
  display: flex;
  flex-direction: column;
  padding: 22px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 11px; padding: 0 8px 22px; border-bottom: 1px solid #22252b; }
.brand-mark {
  width: 36px; height: 36px; border-radius: 8px;
  background: #1f2228; color: #e8e9ec;
  display: grid; place-items: center;
}
.brand-name { color: #f2f3f5; font-weight: 600; font-size: 15px; }
.brand-sub { font-size: 12px; color: #6f747e; }

.nav { display: flex; flex-direction: column; gap: 2px; margin-top: 18px; }
.nav a {
  display: flex; align-items: center; gap: 11px;
  padding: 9px 10px; border-radius: 8px;
  font-size: 13.5px; transition: background .15s, color .15s;
  position: relative;
}
.nav a svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.nav a:hover { background: #1b1e23; color: #e5e7ea; }
.nav a.active { background: #1f2329; color: #fff; }
.nav a.active::before {
  content: ''; position: absolute; right: -14px; top: 8px; bottom: 8px;
  width: 3px; border-radius: 3px 0 0 3px; background: #3fb39b;
}

.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 6px; padding-top: 16px; border-top: 1px solid #22252b; }
.btn-ghost-dark {
  background: transparent; border: 1px solid #2a2e35; color: #b9bdc5;
  border-radius: 8px; padding: 8px 10px; font-size: 12.5px; cursor: pointer; text-align: center;
}
.btn-ghost-dark:hover { border-color: #3b4049; color: #fff; }

.main { min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 22px 32px 18px;
}
.topbar h1 { margin: 0; font-size: 22px; font-weight: 700; letter-spacing: -.2px; }
.topbar p { margin: 2px 0 0; font-size: 13px; }
.topbar-date { margin-inline-start: auto; font-size: 13px; color: var(--muted); background: var(--surface); border: 1px solid var(--line); padding: 6px 12px; border-radius: 999px; }
.menu-btn { display: none; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; width: 38px; height: 38px; cursor: pointer; }
.menu-btn svg { width: 20px; height: 20px; stroke: var(--ink); stroke-width: 1.8; fill: none; }

.view { padding: 0 32px 40px; }

/* ---------- Building blocks ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 16px 18px 0;
}
.card-head h3 { margin: 0; font-size: 14.5px; font-weight: 600; }
.card-head .hint { font-size: 12px; color: var(--muted); }
.card-body { padding: 16px 18px 18px; }

.grid { display: grid; gap: 16px; }
.g-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.g-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g-main { grid-template-columns: minmax(0, 1.7fr) minmax(0, 1fr); }
.mt { margin-top: 16px; }

.toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
  margin-bottom: 16px;
}
.toolbar .spacer { flex: 1; }

.seg { display: inline-flex; background: var(--surface); border: 1px solid var(--line); border-radius: 9px; padding: 3px; }
.seg button {
  border: 0; background: transparent; padding: 6px 12px; border-radius: 6px;
  font-size: 13px; cursor: pointer; color: var(--ink-2);
}
.seg button.on { background: var(--ink); color: #fff; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 12.5px; color: var(--ink-2); font-weight: 500; }
.input, select.input, textarea.input {
  width: 100%;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 8px; padding: 9px 11px; font-size: 13.5px;
  outline: none; transition: border-color .15s, box-shadow .15s;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.input.sm { padding: 6px 9px; font-size: 13px; width: auto; }
textarea.input { resize: vertical; min-height: 70px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  border: 1px solid var(--ink); background: var(--ink); color: #fff;
  padding: 9px 16px; border-radius: 8px; font-size: 13.5px; font-weight: 500;
  cursor: pointer; transition: opacity .15s, background .15s;
  white-space: nowrap;
}
.btn:hover { opacity: .9; }
.btn.accent { background: var(--accent); border-color: var(--accent); }
.btn.light { background: var(--surface); color: var(--ink); border-color: var(--line); }
.btn.light:hover { background: var(--surface-2); opacity: 1; }
.btn.sm { padding: 6px 11px; font-size: 12.5px; }
.btn.danger { background: var(--surface); color: var(--danger); border-color: #f0d0cc; }
.btn.danger:hover { background: var(--danger-soft); opacity: 1; }
.icon-btn {
  width: 30px; height: 30px; display: inline-grid; place-items: center;
  border: 1px solid transparent; background: transparent; border-radius: 7px; cursor: pointer; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); border-color: var(--line); color: var(--ink); }
.icon-btn.del:hover { color: var(--danger); background: var(--danger-soft); border-color: #f0d0cc; }
.icon-btn svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; padding: 3px 9px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--line); color: var(--ink-2);
}
.chip .dot { width: 7px; height: 7px; border-radius: 50%; }
.tag { font-size: 11.5px; padding: 2px 8px; border-radius: 5px; background: var(--line-2); color: var(--ink-2); }
.tag.server { background: #eef0f7; color: #374272; }
.tag.platform { background: var(--accent-soft); color: var(--accent); }
.tag.subscription { background: #f6efe2; color: #7a5413; }

/* ---------- KPIs ---------- */
.kpi { padding: 16px 18px; }
.kpi .label { font-size: 12.5px; color: var(--muted); }
.kpi .value { font-size: 26px; font-weight: 700; margin-top: 4px; letter-spacing: -.5px; }
.kpi .meta { display: flex; align-items: center; gap: 8px; margin-top: 6px; font-size: 12px; color: var(--muted); }
.delta { font-weight: 600; font-size: 12px; padding: 1px 6px; border-radius: 4px; }
.delta.up { color: #0f7a4f; background: #e5f4ec; }
.delta.down { color: var(--danger); background: var(--danger-soft); }
.kpi .who { display: flex; align-items: center; gap: 10px; margin-top: 6px; }
.kpi .who .value { margin: 0; font-size: 18px; }

/* ---------- Charts ---------- */
.chart-box { position: relative; height: 300px; width: 100%; min-width: 0; overflow: hidden; }
.chart-box canvas { position: absolute; inset: 0; max-width: 100%; }
.chart-box.tall { height: 340px; }
.chart-box.short { height: 240px; }
.legend-list { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.legend-row { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.legend-row .bar { flex: 1; height: 6px; border-radius: 3px; background: var(--line-2); overflow: hidden; }
.legend-row .bar i { display: block; height: 100%; border-radius: 3px; }
.legend-row .pct { width: 48px; text-align: left; color: var(--muted); }

/* ---------- Logos ---------- */
.logo {
  width: 38px; height: 38px; flex: none;
  border-radius: 8px; border: 1px solid var(--line);
  background: #fff; display: grid; place-items: center; overflow: hidden;
}
.logo img { width: 100%; height: 100%; object-fit: contain; padding: 3px; }
.logo.lg { width: 52px; height: 52px; }
.logo.sm { width: 28px; height: 28px; border-radius: 6px; }
.logo .initial { font-weight: 700; color: #fff; width: 100%; height: 100%; display: grid; place-items: center; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 11px 14px; text-align: right; border-bottom: 1px solid var(--line-2); white-space: nowrap; }
th { font-size: 12px; font-weight: 500; color: var(--muted); background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: #fcfbf9; }
.ch-cell { display: flex; align-items: center; gap: 10px; font-weight: 500; }

/* entry grid */
.entry-grid th.slot-h { text-align: center; }
.entry-grid th .t { display: block; color: var(--ink); font-weight: 600; font-size: 13px; }
.entry-grid td.slot-c { text-align: center; }
.num-input {
  width: 110px; text-align: center; direction: ltr;
  border: 1px solid var(--line); border-radius: 7px; padding: 7px 8px;
  background: var(--surface-2); font-variant-numeric: tabular-nums; outline: none;
}
.num-input:focus { border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px var(--accent-soft); }
.num-input.filled { background: #fff; border-color: #cfd8d4; }
.row-total { font-weight: 600; }

/* slots manager */
.slots { display: flex; flex-wrap: wrap; gap: 8px; }
.slot-pill {
  display: inline-flex; align-items: center; gap: 10px;
  border: 1px solid var(--line); background: var(--surface-2);
  padding: 6px 6px 6px 12px; border-radius: 9px;
}
.slot-pill .time { font-weight: 700; font-size: 14px; }
.slot-pill .lbl { font-size: 12px; color: var(--muted); }
.slot-pill .icon-btn { width: 24px; height: 24px; }
.slot-pill .icon-btn svg { width: 13px; height: 13px; }
.slot-add { display: flex; gap: 8px; flex-wrap: wrap; align-items: end; }

/* channel list */
.ch-list { display: flex; flex-direction: column; }
.ch-row {
  display: grid; grid-template-columns: minmax(220px, 280px) 1fr auto;
  gap: 18px; align-items: center;
  padding: 14px 18px; border-top: 1px solid var(--line-2);
}
.ch-row:first-child { border-top: 0; }
.ch-info { display: flex; align-items: center; gap: 12px; min-width: 0; }
.ch-info .name { font-weight: 600; font-size: 14.5px; }
.ch-info .plats { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; }
.ch-slots { display: flex; gap: 10px; flex-wrap: wrap; }
.ch-slot { display: flex; flex-direction: column; gap: 4px; }
.ch-slot span { font-size: 11.5px; color: var(--muted); text-align: center; }
.ch-slot .num-input { width: 96px; }
.ch-actions { display: flex; gap: 4px; align-items: center; }

/* platform cards */
.plat-card { padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.plat-top { display: flex; align-items: start; gap: 12px; }
.plat-icon { width: 40px; height: 40px; border-radius: 9px; display: grid; place-items: center; background: var(--surface-2); border: 1px solid var(--line); flex: none; }
.plat-icon svg { width: 19px; height: 19px; fill: none; stroke: var(--ink-2); stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.plat-name { font-weight: 600; font-size: 15px; }
.plat-url { font-size: 12px; color: var(--muted); direction: ltr; text-align: right; word-break: break-all; }
.plat-stats { display: flex; gap: 0; border-top: 1px solid var(--line-2); padding-top: 12px; }
.plat-stats > div { flex: 1; }
.plat-stats > div + div { border-inline-start: 1px solid var(--line-2); padding-inline-start: 14px; }
.plat-stats .k { font-size: 11.5px; color: var(--muted); }
.plat-stats .v { font-weight: 700; font-size: 17px; }
.plat-logos { display: flex; gap: 4px; }

.form-grid { display: grid; gap: 14px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.form-grid .full { grid-column: 1 / -1; }
.check-list { display: flex; flex-wrap: wrap; gap: 8px; }
.check {
  display: inline-flex; align-items: center; gap: 8px; cursor: pointer;
  border: 1px solid var(--line); border-radius: 8px; padding: 7px 11px; font-size: 13px; background: var(--surface);
}
.check input { accent-color: var(--accent); }
.check:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.color-input { width: 44px; height: 38px; border: 1px solid var(--line); border-radius: 8px; padding: 3px; background: #fff; cursor: pointer; }
.logo-pick { display: flex; align-items: center; gap: 12px; }

.notice {
  display: flex; align-items: center; gap: 12px;
  background: #fbf7ec; border: 1px solid #efe3c4; color: #6b5418;
  border-radius: var(--radius); padding: 10px 14px; margin-bottom: 16px; font-size: 13px;
}
.notice .spacer { flex: 1; }

.empty { padding: 40px 20px; text-align: center; color: var(--muted); }
.empty b { display: block; color: var(--ink); font-size: 15px; margin-bottom: 4px; }

.section-title { font-size: 13px; color: var(--muted); font-weight: 500; margin: 26px 0 10px; }

.toast {
  position: fixed; bottom: 24px; left: 24px;
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 9px;
  font-size: 13px; opacity: 0; transform: translateY(8px); pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 50;
}
.toast.show { opacity: 1; transform: none; }

dialog.modal {
  border: 1px solid var(--line); border-radius: 12px; padding: 0; width: min(560px, 92vw);
  box-shadow: 0 20px 60px rgba(0,0,0,.18);
}
dialog.modal::backdrop { background: rgba(18, 20, 24, .45); }
.modal-head { padding: 16px 20px; border-bottom: 1px solid var(--line-2); font-weight: 600; }
.modal-body { padding: 18px 20px; }
.modal-foot { padding: 12px 20px; border-top: 1px solid var(--line-2); display: flex; gap: 8px; justify-content: flex-start; }

/* ---------- Responsive ---------- */
@media (max-width: 1200px) {
  .g-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g-main { grid-template-columns: 1fr; }
  .ch-row { grid-template-columns: 1fr; gap: 12px; }
  .ch-actions { justify-content: flex-start; }
}
@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; right: 0; left: auto; width: 248px; z-index: 40; transform: translateX(100%); transition: transform .2s; }
  .sidebar.open { transform: none; }
  .menu-btn { display: grid; place-items: center; }
  .topbar, .view { padding-left: 16px; padding-right: 16px; }
  .g-3, .g-2, .form-grid { grid-template-columns: 1fr; }
  .topbar-date { display: none; }
}
@media (max-width: 560px) {
  .g-4 { grid-template-columns: 1fr; }
}

/* ---------- Auth & roles ---------- */
.auth { display: none; }
body.auth-mode .shell { display: none; }
body.auth-mode .auth {
  display: grid; place-items: center; min-height: 100vh; padding: 24px;
}
.auth-card { width: min(380px, 100%); padding: 26px 24px; }
.auth-brand { border-bottom: 1px solid var(--line-2); padding: 0 0 18px; margin-bottom: 20px; }
.auth-brand .brand-mark { background: var(--ink); color: #fff; }
.auth-brand .brand-name { color: var(--ink); }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form .btn { margin-top: 4px; }
.auth-err { color: var(--danger); font-size: 12.5px; min-height: 18px; }
.auth-err:empty { min-height: 0; }

body.is-viewer .admin-only { display: none !important; }

.who { display: flex; align-items: center; gap: 10px; padding: 4px 4px 10px; }
.who-avatar { width: 32px; height: 32px; border-radius: 50%; background: #1f2228; display: grid; place-items: center; flex: none; }
.who-avatar svg { fill: none; stroke: #9aa0aa; stroke-width: 1.7; }
.who-text { min-width: 0; flex: 1; }
#whoName { color: #e8e9ec; font-size: 13px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; direction: ltr; text-align: right; }
#whoRole { font-size: 11.5px; color: #6f747e; }
.sync { font-size: 11px; color: #6f747e; }
.sync[data-s="saving"] { color: #c9a24a; }
.sync[data-s="error"] { color: #e0796f; }
.foot-row { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }

.user-row td { vertical-align: middle; }
.role-tag { font-size: 11.5px; padding: 2px 8px; border-radius: 5px; }
.role-tag.admin { background: #16181d; color: #fff; }
.role-tag.viewer { background: var(--accent-soft); color: var(--accent); }
.pw-reveal { white-space: pre-line; font-family: ui-monospace, Menlo, monospace; direction: ltr; background: var(--surface-2); border: 1px dashed var(--line); padding: 8px 10px; border-radius: 7px; user-select: all; }
