:root {
  color-scheme: light;
  --bg: #f5f5f3;
  --surface: #fcfcfb;
  --line: #e4e3df;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  /* chart series (validated pair: blue / orange) */
  --s1: #2a78d6;
  --s2: #eb6834;
  /* status: always shown with an icon and a label */
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110; --surface: #1a1a19; --line: #2e2e2c; --text: #ffffff;
    --text-2: #c3c2b7; --muted: #8f8e87; --accent: #3987e5; --s1: #3987e5; --s2: #d95926;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110; --surface: #1a1a19; --line: #2e2e2c; --text: #ffffff;
  --text-2: #c3c2b7; --muted: #8f8e87; --accent: #3987e5; --s1: #3987e5; --s2: #d95926;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.5 "Segoe UI", Tahoma, "Noto Sans Arabic", system-ui, sans-serif;
}
h1 { font-size: 18px; margin: 0; }
h2 { font-size: 15px; margin: 0 0 12px; }
.muted { color: var(--muted); }
.small { font-size: 12px; font-weight: normal; }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }

/* header */
.top {
  display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 16px; }
.logo { width: 40px; height: 40px; flex: none; }
.titles { display: flex; flex-direction: column; }
.titles .sub { color: var(--text-2); font-size: 12px; }
.sync { display: flex; align-items: center; gap: 10px; font-size: 12px; flex-wrap: wrap; }

.btn {
  background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 8px;
  padding: 8px 14px; font: inherit; cursor: pointer;
}
.btn:disabled { opacity: .5; cursor: default; }
.btn.ghost { background: transparent; color: var(--accent); border: 1px solid var(--line); }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn.plain { background: transparent; color: var(--text-2); border: 1px solid var(--line); }

/* tabs & filters */
.tabs { display: flex; gap: 4px; padding: 0 24px; background: var(--surface); border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs button {
  background: none; border: 0; border-bottom: 2px solid transparent; padding: 12px 14px;
  font: inherit; color: var(--text-2); cursor: pointer; white-space: nowrap;
}
.tabs button.on { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.count { background: var(--critical); color: #fff; border-radius: 9px; padding: 0 7px; font-size: 11px; }
.count:empty { display: none; }

.filters { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; padding: 16px 24px 0; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--surface); flex-wrap: wrap; }
.seg button { background: none; border: 0; padding: 7px 12px; font: inherit; color: var(--text-2); cursor: pointer; white-space: nowrap; }
.seg button + button { border-inline-start: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
.seg.small button { padding: 5px 10px; font-size: 12px; }
.range { display: inline-flex; gap: 8px; align-items: center; color: var(--text-2); flex-wrap: wrap; }
select, input[type=search], input[type=text], input[type=date] {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px;
}

/* layout */
main { padding: 16px 24px 40px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.tile .label { color: var(--text-2); font-size: 13px; }
.tile .value { font-size: 24px; font-weight: 600; margin-top: 4px; }
.tile .unit { font-size: 14px; font-weight: normal; color: var(--text-2); margin-inline-start: 4px; }
.tile .hint { color: var(--muted); font-size: 12px; margin-top: 2px; }
.value-tiles { margin-bottom: 4px; }
.tile.big .value { font-size: 30px; }
.tile.accent { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 16px; margin-bottom: 16px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; min-width: 0; margin-bottom: 16px; position: relative; }
.grid2 > .card { margin-bottom: 0; }
.cardhead { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.cardhead h2 { margin: 0; }
.note { color: var(--muted); font-size: 12px; margin: 0 0 16px; }

/* chart */
.legend { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; font-size: 12px; color: var(--text-2); margin-bottom: 4px; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-inline-end: 6px; vertical-align: middle; }
.chart svg { display: block; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; font-family: inherit; }
.chart .hit { fill: transparent; cursor: crosshair; }
.chart .hit:hover { fill: color-mix(in srgb, var(--text) 5%, transparent); }
.tip { position: absolute; z-index: 5; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 12px; box-shadow: 0 6px 20px rgba(0,0,0,.18); pointer-events: none; min-width: 150px; }
.datatable { margin-top: 8px; }
.datatable summary { cursor: pointer; color: var(--text-2); font-size: 12px; }

/* tables */
.tablewrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 10px; text-align: start; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--text-2); font-weight: 600; font-size: 12px; background: var(--surface); }
table.data th[data-k] { cursor: pointer; user-select: none; }
table.data th[data-k]:hover { color: var(--text); }
th.sorted::after { content: " ▾"; }
th.sorted.asc::after { content: " ▴"; }
td.name { white-space: normal; min-width: 200px; max-width: 380px; }
td.r, th.r { text-align: end; }
tr:hover td { background: color-mix(in srgb, var(--accent) 5%, transparent); }
.tablewrap .data { background: var(--surface); }
section[data-panel="products"] > .tablewrap, section[data-panel="links"] > .tablewrap, section[data-panel="ic"] > .tablewrap { border: 1px solid var(--line); border-radius: var(--radius); }
.inbar { height: 4px; background: var(--line); border-radius: 2px; margin-top: 4px; overflow: hidden; }
.inbar > div { height: 100%; background: var(--s1); border-start-end-radius: 2px; border-end-end-radius: 2px; }

#splitTable td, #splitTable th { text-align: end; }
#splitTable td:first-child, #splitTable th:first-child { text-align: start; }
#splitTable tr.total td { font-weight: 600; border-top: 2px solid var(--line); }
#splitTable td.both { background: color-mix(in srgb, var(--accent) 7%, transparent); }

.toolbar { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar.end { justify-content: flex-end; margin: 12px 0 0; }
.toolbar input[type=search] { min-width: 240px; flex: 1; max-width: 400px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 16px; padding: 4px 10px; font: inherit; font-size: 12px; cursor: pointer; color: var(--text-2); }
.chip.on { border-color: var(--accent); color: var(--text); box-shadow: inset 0 0 0 1px var(--accent); }
.pager { display: flex; gap: 8px; align-items: center; justify-content: center; margin-top: 12px; color: var(--text-2); }

/* status: icon + label, never colour alone */
.st { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; color: var(--text); white-space: nowrap; }
.st i { width: 16px; height: 16px; border-radius: 50%; display: inline-grid; place-items: center; font-style: normal; font-size: 10px; color: #fff; flex: none; }
.st.fast i, .st.ok i { background: var(--good); }
.st.normal i { background: var(--muted); }
.st.slow i, .st.low i { background: var(--warning); color: #111; }
.st.dead i { background: var(--critical); }
.st.out i { background: var(--critical); }
.st.none i { background: var(--line); }

.bars .row { display: grid; grid-template-columns: 120px 1fr 110px; gap: 10px; align-items: center; margin: 8px 0; }
.bars .track { height: 10px; background: var(--line); border-radius: 5px; overflow: hidden; }
.bars .fill { height: 100%; background: var(--s1); border-start-end-radius: 4px; border-end-end-radius: 4px; }
.bars .val { text-align: end; color: var(--text-2); font-size: 12px; }

.calc { border-bottom: 1px dotted var(--muted); cursor: help; }
.method { font-size: 11px; border: 1px solid var(--line); border-radius: 6px; padding: 1px 6px; color: var(--text-2); }
.tag { font-size: 11px; border-radius: 6px; padding: 1px 6px; border: 1px solid var(--serious); color: var(--text); }
.picker { display: inline-flex; flex-direction: column; gap: 4px; }
.picker .results { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; min-width: 300px; max-height: 220px; overflow: auto; }
.picker .results button { display: block; width: 100%; text-align: start; background: none; border: 0; padding: 8px 10px; font: inherit; color: var(--text); cursor: pointer; }
.picker .results button:hover { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.explain { margin: 0; padding-inline-start: 18px; color: var(--text-2); font-size: 13px; }
.explain li { margin-bottom: 6px; }

#toast { position: fixed; bottom: 20px; inset-inline-start: 20px; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 10; }
#toast.show { opacity: 1; }

@media (max-width: 640px) {
  .top, .filters, main { padding-inline: 16px; }
  .tabs { padding-inline: 8px; }
  .grid2 { grid-template-columns: 1fr; }
  .logo { width: 34px; height: 34px; }
  .toolbar input[type=search] { min-width: 0; width: 100%; max-width: none; }
  .bars .row { grid-template-columns: 90px 1fr 80px; }
  .tile.big .value { font-size: 24px; }
}

/* signed-in user */
.who { display: inline-flex; align-items: center; gap: 8px; padding-inline-start: 10px; border-inline-start: 1px solid var(--line); }
#whoName { font-weight: 600; color: var(--text); }

/* forms */
.fld { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-2); }
.fld input, .fld select { font: inherit; font-size: 14px; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
.fld input:focus, .fld select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.formrow { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
.formrow .fld { min-width: 170px; flex: 1; }
.err { color: var(--critical); font-size: 13px; margin: 4px 0; }
.btn.wide { width: 100%; padding: 10px; font-weight: 600; }
td select { font: inherit; font-size: 12px; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 6px; padding: 3px 6px; }

/* sign-in page */
body.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.authbox { width: 100%; max-width: 380px; padding: 0; }
.authhead { text-align: center; margin-bottom: 16px; }
.authhead h1 { margin-top: 10px; font-size: 22px; }
.authbox form { display: flex; flex-direction: column; gap: 14px; padding: 22px; }
.authfoot { text-align: center; }

/* dialog */
dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 22px; width: min(400px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog form { display: flex; flex-direction: column; gap: 12px; }

.namebox { display: inline-flex; align-items: center; gap: 6px; }
.namebox .pen { color: var(--muted); font-size: 13px; }
input.inline { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; min-width: 200px; cursor: text; }
input.inline:hover { border-color: var(--muted); }
input.inline:focus { border-color: var(--accent); background: var(--surface); outline: none; }

/* live sync indicator */
.live { display: inline-flex; align-items: center; gap: 8px; font-size: 12px; font-weight: 600; color: var(--accent);
        border: 1px solid var(--accent); border-radius: 999px; padding: 3px 10px; background: color-mix(in srgb, var(--accent) 10%, transparent); }
.spin { width: 12px; height: 12px; border-radius: 50%; border: 2px solid color-mix(in srgb, var(--accent) 30%, transparent);
        border-top-color: var(--accent); animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spin { animation: none; } }
