/* ============================================================
   Abela Sleeper Trains — analytics dashboard
   Look: the control room of a night sleeper — deep rail-blue sidebar, signal amber for focus and actions,
   cool paper surfaces. Local fonts only. Logical properties (inline-start/end) make Arabic RTL work without a second stylesheet.
   ============================================================ */
:root {
  --bg: #f1f4f9; --surface: #ffffff; --surface-2: #f7f9fc; --ink: #172033; --muted: #5b6880; --line: #e1e7f0;
  --nav: #172033; --nav-ink: #c9d3e6; --nav-active: #243350;
  --accent: #f0a830; --accent-ink: #2b1d00; --accent-text: #8a5a00;
  --good: #1f8a64; --bad: #c43a52; --warn: #b9770e; --info: #2f6fb0;
  --s0: #e59a14; --s1: #3a6ea5; --s2: #2e9e7d; --s3: #d4506a; --s4: #7d6bd0; --s5: #6b7890;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 4px 14px rgba(16,24,40,.05);
  --radius: 14px;
  --font: "Segoe UI", Tahoma, "Noto Sans Arabic", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace;
  color-scheme: light;
}
:root[data-theme="dark"] { color-scheme: dark; }

/* dark palette (applied by explicit choice, or by the device when the choice is "system") */
:root[data-theme="dark"] {
  --bg: #0e1524; --surface: #172033; --surface-2: #1c2740; --ink: #e8edf7; --muted: #9aa8c2; --line: #28344f;
  --nav: #0a101d; --nav-ink: #b3bfd6; --nav-active: #1b2742;
  --accent-text: #f2b13c; --good: #4cc0a0; --bad: #ef7b93; --warn: #e3a339; --info: #6aa2dd;
  --s0: #f2b13c; --s1: #6aa2dd; --s2: #4cc0a0; --s3: #ef7b93; --s4: #a393ee; --s5: #94a1b8;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 18px rgba(0,0,0,.28);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0e1524; --surface: #172033; --surface-2: #1c2740; --ink: #e8edf7; --muted: #9aa8c2; --line: #28344f;
    --nav: #0a101d; --nav-ink: #b3bfd6; --nav-active: #1b2742;
    --accent-text: #f2b13c; --good: #4cc0a0; --bad: #ef7b93; --warn: #e3a339; --info: #6aa2dd;
    --s0: #f2b13c; --s1: #6aa2dd; --s2: #4cc0a0; --s3: #ef7b93; --s4: #a393ee; --s5: #94a1b8;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 18px rgba(0,0,0,.28);
  }
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body { font-family: var(--font); background: var(--bg); color: var(--ink); font-size: 15px; line-height: 1.5; -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
a { color: var(--info); }
button, input, select { font: inherit; color: inherit; }
.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.mono { font-family: var(--mono); font-size: 12.5px; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px;
  background: radial-gradient(120% 90% at 50% 0%, #26365a 0%, #172033 45%, #0c1322 100%); }
.login-card { width: min(380px, 100%); background: var(--surface); border-radius: 18px; padding: 30px 26px 24px; box-shadow: 0 30px 70px rgba(0,0,0,.45); text-align: center; border-top: 4px solid var(--accent); }
.login-logo { border-radius: 50%; margin-bottom: 10px; }
.login-card h1 { font-size: 22px; }
.login-sub { margin: 4px 0 22px; color: var(--muted); }
.login-card .field { text-align: start; margin-bottom: 14px; }
.login-foot { margin: 16px 0 0; font-size: 12.5px; color: var(--muted); }
.form-error { margin: 0 0 12px; padding: 9px 12px; border-radius: 10px; background: color-mix(in srgb, var(--bad) 12%, transparent); color: var(--bad); font-size: 14px; text-align: start; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 252px minmax(0, 1fr); min-height: 100vh; }
.sidebar { position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; background: var(--nav); color: var(--nav-ink); padding: 18px 12px; z-index: 30; }
.brand { display: flex; align-items: center; gap: 11px; padding: 4px 8px 20px; }
.brand img { border-radius: 50%; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text strong { color: #fff; font-size: 15px; }
.brand-text span { font-size: 12px; color: var(--accent); letter-spacing: .08em; text-transform: uppercase; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px; color: var(--nav-ink); text-decoration: none; font-weight: 500; border-inline-start: 3px solid transparent; }
.nav a svg { width: 20px; height: 20px; fill: currentColor; flex: none; opacity: .85; }
.nav a:hover { background: var(--nav-active); color: #fff; }
.nav a.active { background: var(--nav-active); color: #fff; border-inline-start-color: var(--accent); }
.nav a.active svg { fill: var(--accent); opacity: 1; }
.nav-logout { margin-top: auto; background: transparent; border: 1px solid #34456a; color: var(--nav-ink); padding: 9px 12px; border-radius: 10px; cursor: pointer; }
.nav-logout:hover { background: var(--nav-active); color: #fff; }
.scrim { position: fixed; inset: 0; background: rgba(8,12,22,.55); z-index: 20; }

.content { min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 14px 24px; position: sticky; top: 0; z-index: 10; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); }
.page-title { font-size: 22px; flex: 1; min-width: 0; }
.topbar-actions { display: flex; gap: 8px; }
.icon-btn { display: grid; place-items: center; min-width: 40px; height: 40px; padding: 0 8px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-weight: 700; }
.icon-btn:hover { border-color: var(--accent); }
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }
.menu-toggle { display: none; }

.range-bar { display: flex; flex-wrap: wrap; align-items: end; gap: 12px 16px; padding: 14px 24px 0; }
.custom-range { display: flex; gap: 12px; flex-wrap: wrap; }
.range-text { margin-inline-start: auto; color: var(--muted); font-size: 13.5px; padding-bottom: 8px; }
.view { padding: 18px 24px 56px; outline: none; display: flex; flex-direction: column; gap: 18px; }

/* ---------- forms + buttons ---------- */
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field.grow { flex: 1; min-width: 200px; }
.field-label { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.input { height: 42px; padding: 0 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--ink); width: 100%; min-width: 0; }
select.input { padding-inline-end: 8px; }
.input:focus-visible { outline: 3px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.btn { height: 40px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-weight: 600; }
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.06); }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 45%, var(--line)); }
.btn.wide { width: 100%; height: 44px; }
.check { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.toolbar { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; margin-bottom: 14px; }

/* ---------- cards ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; min-width: 0; }
.card-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 6px 12px; margin-bottom: 12px; }
.card-head h2 { font-size: 16.5px; }
.card-sub { color: var(--muted); font-size: 13px; }
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(158px, 1fr)); gap: 14px; }
.kpi { padding: 16px 18px; border-top: 3px solid var(--line); }
.kpi.good { border-top-color: var(--good); } .kpi.bad { border-top-color: var(--bad); } .kpi.warn { border-top-color: var(--warn); }
.kpi-label { color: var(--muted); font-size: 13px; font-weight: 600; }
.kpi-value { font-size: 30px; font-weight: 700; line-height: 1.2; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi.bad .kpi-value { color: var(--bad); }
.kpi-hint { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); gap: 18px; }
.note { color: var(--muted); font-size: 13px; margin: 10px 0 0; }
.warn-note { color: var(--warn); }
.note-card p { margin: 0; } .note-card .note { margin-top: 6px; }

/* ---------- states ---------- */
.state { text-align: center; padding: 38px 16px; color: var(--muted); }
.state-icon { font-size: 34px; opacity: .55; }
.state-title { font-weight: 700; color: var(--ink); font-size: 17px; margin: 4px 0; }
.state-error { color: var(--bad); }
.state-error .btn { margin-top: 12px; }
.skeleton-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; }
.skeleton { height: 92px; border-radius: var(--radius); background: linear-gradient(100deg, var(--surface) 30%, var(--surface-2) 50%, var(--surface) 70%) 0 0 / 220% 100%; border: 1px solid var(--line); animation: shimmer 1.3s linear infinite; }
@keyframes shimmer { to { background-position: -120% 0; } }
.toasts { position: fixed; inset-block-end: 18px; inset-inline-end: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 60; max-width: min(380px, calc(100vw - 36px)); }
.toast { background: var(--nav); color: #fff; padding: 11px 14px; border-radius: 12px; box-shadow: var(--shadow); border-inline-start: 4px solid var(--accent); }
.toast.error { border-inline-start-color: var(--bad); } .toast.ok { border-inline-start-color: var(--good); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; border-radius: 10px; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th { text-align: start; font-size: 12.5px; color: var(--muted); font-weight: 700; padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; background: var(--surface-2); position: sticky; top: 0; }
.table td { padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table .num { text-align: end; font-variant-numeric: tabular-nums; }
.table tr.clickable { cursor: pointer; }
.table tr.clickable:hover td, .table tr.clickable:focus-visible td { background: var(--surface-2); }
.th-btn { all: unset; cursor: pointer; display: inline-flex; gap: 5px; align-items: center; }
.th-btn:focus-visible { outline: 3px solid var(--accent); }
.arrow { font-size: 9px; color: var(--accent-text); }
.sub { font-size: 12px; color: var(--muted); }
.contact { font-variant-numeric: tabular-nums; font-weight: 600; }
.contact-wrap { display: inline-flex; align-items: center; gap: 8px; }
.tag { font-size: 11px; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); }
.tag.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 45%, var(--line)); background: color-mix(in srgb, var(--warn) 10%, transparent); }
.pager { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 10px; margin-top: 14px; color: var(--muted); font-size: 13.5px; }
.pager-btns { display: flex; gap: 8px; }
.badge { display: inline-block; font-size: 11.5px; font-weight: 700; padding: 2px 9px; border-radius: 999px; }
.b-delivered, .b-read { background: color-mix(in srgb, var(--good) 15%, transparent); color: var(--good); }
.b-failed { background: color-mix(in srgb, var(--bad) 15%, transparent); color: var(--bad); }
.b-sent { background: color-mix(in srgb, var(--info) 15%, transparent); color: var(--info); }
.b-accepted { background: var(--surface-2); color: var(--muted); }

/* ---------- profile ---------- */
.back { display: inline-block; text-decoration: none; font-weight: 600; }
.profile-title { font-size: 24px; }
.profile-head .note { margin-top: 4px; }
.timeline { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.tl-item { display: flex; align-items: flex-start; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.tl-item:last-child { border-bottom: 0; }
.tl-icon { width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; flex: none; font-size: 14px; background: var(--surface-2); border: 1px solid var(--line); }
.tl-incoming .tl-icon { color: var(--s1); } .tl-outgoing .tl-icon { color: var(--s0); } .tl-selection .tl-icon { color: var(--s2); }
.tl-body { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.tl-sub { color: var(--muted); }
.tl-time { color: var(--muted); font-size: 12.5px; white-space: nowrap; }

/* ---------- reports / settings ---------- */
.report-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr)); gap: 18px; }
.report { display: flex; flex-direction: column; gap: 6px; }
.report h2 { font-size: 17px; }
.report .note { flex: 1; margin: 0 0 8px; }
.report-status { min-height: 20px; font-size: 13px; color: var(--muted); }
.report-status.ok { color: var(--good); } .report-status.bad { color: var(--bad); }
.prefs { display: grid; gap: 14px; max-width: 460px; }
.defs { margin: 0; display: grid; gap: 4px 18px; grid-template-columns: minmax(150px, 220px) 1fr; }
.defs dt { font-weight: 700; padding-top: 8px; }
.defs dd { margin: 0; padding: 8px 0; color: var(--muted); border-bottom: 1px solid var(--line); }

/* ---------- charts ---------- */
.chart-host, .hbar-host { min-width: 0; }
.chart { position: relative; direction: ltr; }
.chart svg { display: block; overflow: visible; }
.s0 { --c: var(--s0); } .s1 { --c: var(--s1); } .s2 { --c: var(--s2); } .s3 { --c: var(--s3); } .s4 { --c: var(--s4); } .s5 { --c: var(--s5); }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; font-family: var(--font); }
.chart .bar, .chart .slice, .chart .dot { fill: var(--c); }
.chart .area { fill: var(--c); opacity: .13; }
.chart .line { stroke: var(--c); stroke-width: 2.3; stroke-linejoin: round; stroke-linecap: round; }
.chart .ring { stroke: var(--c); }
.chart .slice:hover { opacity: .85; }
.chart .hit { fill: transparent; cursor: crosshair; }
.chart .hit:hover { fill: color-mix(in srgb, var(--muted) 9%, transparent); }
.chart .guide { stroke: var(--muted); stroke-dasharray: 3 3; }
.chart .donut-total { fill: var(--ink); font-size: 22px; font-weight: 700; font-family: var(--font); }
.chart-empty { padding: 30px 10px; text-align: center; color: var(--muted); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-bottom: 8px; direction: inherit; }
.legend-item { all: unset; display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); cursor: pointer; padding: 2px 4px; border-radius: 6px; }
.legend-item:focus-visible { outline: 3px solid var(--accent); }
.legend-item.off { opacity: .45; text-decoration: line-through; }
.legend-dot, .tip-dot { width: 10px; height: 10px; border-radius: 3px; background: var(--c); flex: none; }
.chart-tip { position: absolute; z-index: 5; pointer-events: none; background: var(--nav); color: #fff; border-radius: 10px; padding: 8px 11px; font-size: 12.5px; box-shadow: var(--shadow); max-width: 260px; direction: ltr; }
.tip-title { font-weight: 700; margin-bottom: 3px; unicode-bidi: plaintext; }
.tip-row { display: flex; align-items: center; gap: 7px; }
.tip-name { flex: 1; opacity: .9; unicode-bidi: plaintext; }
.tip-val { font-weight: 700; font-variant-numeric: tabular-nums; }
.donut-wrap { display: flex; flex-wrap: wrap; align-items: center; gap: 18px; }
.donut-legend { flex: 1; min-width: 190px; display: grid; gap: 6px; direction: inherit; }
.donut-row { display: flex; align-items: center; gap: 8px; font-size: 13.5px; }
.donut-name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.donut-val { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.hbars { display: grid; gap: 12px; }
.hbar-head { display: flex; justify-content: space-between; gap: 12px; font-size: 14px; }
.hbar-label { min-width: 0; overflow-wrap: anywhere; }
.hbar-val { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.hbar-track { height: 9px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); overflow: hidden; margin-top: 4px; }
.hbar-fill { height: 100%; border-radius: 999px; background: var(--c); }
.hbar-sub { font-size: 12px; color: var(--muted); margin-top: 3px; }
.meter { height: 12px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); overflow: hidden; margin-top: 8px; }
.meter-fill { height: 100%; background: var(--good); border-radius: 999px; }
.meter-fill.warn { background: var(--warn); } .meter-fill.bad { background: var(--bad); }
.meter-num { font-size: 26px; font-variant-numeric: tabular-nums; } .meter-num span { color: var(--muted); font-size: 18px; }

/* ---------- animations (respect reduced motion) ---------- */
.view > * { animation: rise .28s ease-out both; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

/* ---------- tablet / phone ---------- */
@media (max-width: 960px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; width: 272px; transform: translateX(-102%); transition: transform .22s ease; box-shadow: 0 0 40px rgba(0,0,0,.4); }
  :root[dir="rtl"] .sidebar { transform: translateX(102%); }
  .sidebar.open { transform: none !important; }
  .menu-toggle { display: grid; }
  .topbar, .range-bar, .view { padding-inline: 14px; }
  .range-text { margin-inline-start: 0; width: 100%; }
  .defs { grid-template-columns: 1fr; } .defs dt { padding-bottom: 0; } .defs dd { padding-top: 2px; }
}
@media (max-width: 520px) {
  .kpi-value { font-size: 25px; } .page-title { font-size: 19px; }
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
  .card { padding: 14px; }
  .tl-time { display: none; }
}
