/* Theme colours (--primary, --on-primary, --sidebar, --sidebar-text, --bg) come from Settings. */
:root {
    --surface: #ffffff;
    --ink: #17252c;
    --muted: #566870;
    --line: #dbe2e5;
    --ok: #0f6e47;
    --warn: #8a5200;
    --danger: #b3261e;
    --primary-ink: color-mix(in srgb, var(--primary) 80%, #000);
    --primary-soft: color-mix(in srgb, var(--primary) 9%, #fff);
    --sidebar-hover: color-mix(in srgb, var(--sidebar-text) 10%, transparent);
    --sidebar-active: color-mix(in srgb, var(--sidebar-text) 16%, transparent);
    --sidebar-dim: color-mix(in srgb, var(--sidebar-text) 66%, var(--sidebar));
    --sidebar-w: 256px;
    --rail-w: 68px;
    --topbar-h: 56px;
    --radius: 6px;
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font: 400 15px/1.5 'IBM Plex Sans', system-ui, sans-serif;
    font-variant-numeric: tabular-nums;
}

h1, h2, p, dl, dd, ul { margin: 0; padding: 0; }
ul { list-style: none; }
img { max-width: 100%; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--primary-ink); }
[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.sidebar :focus-visible { outline-color: var(--sidebar-text); }

/* Shell */
.shell { display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); min-height: 100dvh; transition: grid-template-columns .18s ease; }
.sidebar-rail .shell { grid-template-columns: var(--rail-w) minmax(0, 1fr); }
.workspace { min-width: 0; display: flex; flex-direction: column; }
.content { width: 100%; max-width: 1400px; margin: 0 auto; padding: 24px; display: grid; gap: 20px; min-width: 0; }
.content > * { min-width: 0; }

.topbar {
    position: sticky; top: 0; z-index: 20;
    display: flex; align-items: center; gap: 12px;
    height: var(--topbar-h); padding: 0 16px;
    background: var(--surface); border-bottom: 1px solid var(--line);
}
.topbar-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-user { margin-inline-start: auto; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40vw; }

.icon-button {
    flex: none; width: 40px; height: 40px;
    display: grid; place-items: center;
    background: none; border: 0; border-radius: var(--radius); cursor: pointer;
}
.icon-button:hover { background: var(--primary-soft); }

/* Sidebar */
.sidebar {
    position: sticky; top: 0; height: 100dvh; min-width: 0; overflow: hidden;
    display: flex; flex-direction: column;
    background: var(--sidebar); color: var(--sidebar-text);
}
.sidebar-brand {
    flex: none; display: flex; align-items: center; gap: 12px;
    height: var(--topbar-h); padding: 0 16px;
    color: inherit; text-decoration: none; font-weight: 600;
}
.sidebar-mark { flex: none; width: 36px; height: 36px; display: grid; place-items: center; font-size: 20px; }
.sidebar-mark img { max-width: 36px; max-height: 36px; object-fit: contain; }
.sidebar-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.sidebar-nav { flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; padding: 8px 10px; scrollbar-width: thin; }
.nav-group + .nav-group { margin-top: 6px; }
.nav-group-toggle {
    width: 100%; display: flex; align-items: center; justify-content: space-between;
    padding: 8px 10px; background: none; border: 0; border-radius: var(--radius);
    color: var(--sidebar-dim); font-size: 13px; font-weight: 500; cursor: pointer;
}
.nav-group-toggle:hover { color: var(--sidebar-text); }
.nav-group-toggle i { font-size: 10px; transition: transform .15s ease; }
.nav-group.is-folded .nav-group-toggle i { transform: rotate(-90deg); }
.nav-group.is-folded .nav-list { display: none; }

.nav-item {
    width: 100%; display: flex; align-items: center; gap: 12px;
    min-height: 40px; padding: 8px 12px; border: 0; border-radius: var(--radius);
    background: none; color: inherit; text-decoration: none; text-align: start; cursor: pointer;
}
.nav-item i { flex: none; width: 20px; text-align: center; }
.nav-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
a.nav-item:hover, button.nav-item:hover { background: var(--sidebar-hover); }
.nav-item.is-active { background: var(--sidebar-active); font-weight: 600; box-shadow: inset 3px 0 0 var(--primary); }
.nav-item.is-pending { color: var(--sidebar-dim); opacity: .6; cursor: default; }

.sidebar-footer { flex: none; padding: 10px; border-top: 1px solid var(--sidebar-active); }
.sidebar-user { display: flex; align-items: center; gap: 10px; padding: 6px 6px 10px; min-width: 0; }
.avatar {
    flex: none; width: 36px; height: 36px; display: grid; place-items: center;
    border-radius: 50%; background: var(--primary); color: var(--on-primary); font-weight: 600;
}
.sidebar-user-text { min-width: 0; display: grid; line-height: 1.25; }
.sidebar-user-text strong, .sidebar-user-text small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar-user-text small { color: var(--sidebar-dim); }

.sidebar-rail .sidebar-name,
.sidebar-rail .nav-label,
.sidebar-rail .sidebar-user-text,
.sidebar-rail .nav-group-toggle { display: none; }
.sidebar-rail .nav-group.is-folded .nav-list { display: block; }
.sidebar-rail .nav-group + .nav-group { margin-top: 0; padding-top: 6px; border-top: 1px solid var(--sidebar-hover); }
.sidebar-rail .nav-item { justify-content: center; padding: 8px 0; }
.sidebar-rail .sidebar-user { justify-content: center; padding-inline: 0; }

.backdrop { position: fixed; inset: 0; z-index: 30; background: rgb(10 20 25 / .5); }

/* Page parts */
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px 24px; }
.page-head h1 { font-size: 26px; line-height: 1.2; font-weight: 600; letter-spacing: -.01em; }
.page-head p { color: var(--muted); max-width: 62ch; }
.page-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }

.button {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    min-height: 40px; padding: 8px 16px; border: 1px solid var(--line); border-radius: var(--radius); white-space: nowrap;
    background: var(--surface); color: var(--ink); font-weight: 500; text-decoration: none; cursor: pointer;
}
.button:hover { border-color: var(--muted); }
.button-primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.button-primary:hover { background: var(--primary-ink); border-color: var(--primary-ink); }
.button-block { width: 100%; }
.button:disabled, .button.is-disabled { opacity: .5; cursor: default; }

input[type=text], input[type=password], input[type=number], input[type=date], select { height: 40px; }
input[type=text], input[type=password], input[type=number], input[type=date], input[type=file], select {
    width: 100%; min-height: 40px; padding: 8px 10px;
    border: 1px solid #b9c5ca; border-radius: var(--radius); background: var(--surface);
}
.page-actions select { width: auto; max-width: 100%; }
.field { display: grid; gap: 4px; min-width: 0; }
.field > span { font-size: 14px; font-weight: 500; }
.stack { display: grid; gap: 16px; }

.alert { padding: 10px 14px; border-radius: var(--radius); border: 1px solid; background: var(--surface); }
.alert ul { display: grid; gap: 2px; }
.alert-success { color: var(--ok); border-color: currentColor; }
.alert-error { color: var(--danger); border-color: currentColor; }

/* Dashboard */
.coverage {
    display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 16px 40px; align-items: center;
    padding: 20px 24px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.coverage-figure { font-size: 17px; color: var(--muted); margin-bottom: 12px; }
.coverage-figure strong { font-size: 40px; line-height: 1; font-weight: 600; color: var(--ink); letter-spacing: -.02em; margin-inline-end: 4px; }
.coverage-bar { height: 8px; background: var(--primary-soft); border-radius: 4px; overflow: hidden; }
.coverage-bar span { display: block; height: 100%; background: var(--primary); }
.coverage-side { display: flex; gap: 32px; }
.coverage-side dt { color: var(--muted); font-size: 14px; }
.coverage-side dd { font-size: 24px; font-weight: 600; }

.filters summary { padding: 12px 16px; font-weight: 600; cursor: pointer; }
.filters .alert { margin: 0 16px 16px; }
.filter-grid {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 12px; align-items: end;
    padding: 0 16px 16px;
}
.filter-actions { display: flex; gap: 8px; }
.filter-actions .button { flex: 1; }

.table { width: 100%; border-collapse: collapse; }
.table th { padding: 10px 16px; text-align: start; font-size: 14px; font-weight: 600; color: var(--muted); border-bottom: 1px solid var(--line); }
.table td { padding: 10px 16px; border-bottom: 1px solid var(--line); }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: var(--primary-soft); }
.strong { font-weight: 600; }
.muted { color: var(--muted); }
.num { white-space: nowrap; }

.status { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; white-space: nowrap; }
.status::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.status-ok { color: var(--ok); }
.status-warn { color: var(--warn); }

.empty { padding: 56px 20px; text-align: center; color: var(--muted); }
.empty h2 { font-size: 18px; font-weight: 600; color: var(--ink); margin-bottom: 4px; }

.pager { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; border-top: 1px solid var(--line); }
.pager-state { color: var(--muted); font-size: 14px; }

/* Settings */
.settings { display: grid; gap: 20px; }
.settings-section { display: grid; grid-template-columns: 240px minmax(0, 1fr); gap: 16px 32px; padding: 20px 24px; }
.settings-section h2 { font-size: 17px; font-weight: 600; }
.settings-section header p { color: var(--muted); font-size: 14px; }
.settings-body { display: grid; gap: 16px; justify-items: start; min-width: 0; }
.settings-body > * { max-width: 100%; }
.palettes { display: flex; flex-wrap: wrap; gap: 8px; }
.palette {
    display: inline-flex; align-items: center; gap: 8px; padding: 6px 12px 6px 6px;
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); cursor: pointer;
}
.palette:hover { border-color: var(--muted); }
.palette-swatches { display: inline-flex; border-radius: 4px; overflow: hidden; border: 1px solid var(--line); }
.palette-swatches span { width: 16px; height: 24px; }
.color-grid { width: 100%; display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 16px; }
.color-input { display: flex; align-items: center; gap: 10px; }
.color-input input { flex: none; width: 48px; height: 40px; padding: 2px; border: 1px solid #b9c5ca; border-radius: var(--radius); background: var(--surface); cursor: pointer; }
.upload-grid { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); justify-items: stretch; }
.upload { display: flex; gap: 14px; min-width: 0; }
.upload-preview {
    flex: none; width: 72px; height: 72px; display: grid; place-items: center;
    border: 1px solid var(--line); border-radius: var(--radius); background: var(--bg); color: var(--muted); font-size: 22px;
}
.upload-preview img { max-width: 60px; max-height: 60px; object-fit: contain; }
.upload-text { flex: 1; min-width: 0; display: grid; gap: 4px; align-content: start; }
.upload-text small { color: var(--muted); }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.form-grid { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); justify-items: stretch; }
.settings-save { display: flex; justify-content: flex-end; }

/* Login */
.login { min-height: 100dvh; display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); }
.login-brand {
    display: flex; flex-direction: column; justify-content: flex-end; gap: 8px;
    padding: 48px; background: var(--sidebar); color: var(--sidebar-text);
}
.login-mark { width: 64px; height: 64px; display: grid; place-items: center; font-size: 40px; margin-bottom: 8px; }
.login-mark img { max-width: 64px; max-height: 64px; object-fit: contain; }
.login-site { font-size: 32px; line-height: 1.15; font-weight: 600; letter-spacing: -.01em; }
.login-tagline { color: var(--sidebar-dim); font-size: 17px; max-width: 34ch; }
.login-panel { align-self: center; justify-self: center; width: 100%; max-width: 400px; padding: 32px 24px; display: grid; gap: 20px; }
.login-panel h1 { font-size: 24px; font-weight: 600; }

/* Tablet and phone: the sidebar becomes a drawer */
@media (max-width: 1023px) {
    .shell, .sidebar-rail .shell { grid-template-columns: minmax(0, 1fr); }
    .sidebar {
        position: fixed; inset: 0 auto 0 0; z-index: 40;
        width: min(300px, 86vw); transform: translateX(-100%); transition: transform .2s ease;
    }
    .drawer-open .sidebar { transform: none; }
    .drawer-open body { overflow: hidden; }

    .sidebar-rail .sidebar-name, .sidebar-rail .nav-label { display: inline; }
    .sidebar-rail .sidebar-user-text { display: grid; }
    .sidebar-rail .nav-group-toggle { display: flex; }
    .sidebar-rail .nav-group.is-folded .nav-list { display: none; }
    .sidebar-rail .nav-item { justify-content: flex-start; padding: 8px 12px; }
    .sidebar-rail .sidebar-user { justify-content: flex-start; padding-inline: 6px; }

    .settings-section { grid-template-columns: minmax(0, 1fr); }
}

/* Phone */
@media (max-width: 767px) {
    .content { padding: 16px; gap: 16px; }
    .page-head h1 { font-size: 22px; }
    .page-actions, .page-actions select, .page-actions .button { width: 100%; }

    .coverage { grid-template-columns: minmax(0, 1fr); padding: 16px; }
    .coverage-figure strong { font-size: 32px; }
    .coverage-side { justify-content: space-between; }

    .settings-section { padding: 16px; }
    .settings-save .button { width: 100%; }

    /* Rows become labelled blocks, so no table ever needs sideways scrolling. */
    .table-stack thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
    .table-stack, .table-stack tbody, .table-stack tr, .table-stack td { display: block; width: 100%; }
    .table-stack tr { padding: 10px 16px; border-bottom: 1px solid var(--line); }
    .table-stack tbody tr:last-child { border-bottom: 0; }
    .table-stack td {
        display: flex; justify-content: space-between; gap: 16px; padding: 3px 0; border: 0; text-align: end;
        overflow-wrap: anywhere;
    }
    .table-stack td::before { content: attr(data-label); flex: none; color: var(--muted); font-size: 14px; font-weight: 400; }
    .num { white-space: normal; }

    .login { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr; }
    .login-brand { padding: 24px 20px; }
    .login-mark { width: 44px; height: 44px; font-size: 28px; margin-bottom: 0; }
    .login-mark img { max-width: 44px; max-height: 44px; }
    .login-site { font-size: 22px; }
    .login-tagline { font-size: 15px; }
    .login-panel { align-self: start; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { transition: none !important; }
}

/* Wide tables scroll inside their panel; positioned so hidden labels inside cannot stretch the page. */
.table-scroll { position: relative; overflow-x: auto; max-width: 100%; }
@media (min-width: 768px) {
    .panel { position: relative; overflow-x: auto; }
}
@media (max-width: 767px) {
    .status { white-space: normal; }
}

/* Language menu (topbar and login page) */
.locale-switch { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; flex: none; }
.locale-switch select {
    appearance: auto; border: 1px solid var(--line); border-radius: 999px; padding: 5px 10px;
    background: var(--surface); color: var(--ink); font-size: 13px; cursor: pointer; max-width: 9rem;
}
.login-lang { display: flex; justify-content: flex-end; }
.login-lang .locale-switch { color: var(--muted); }

/* Right-to-left (Arabic). Layout uses logical properties; only transforms and icons need flipping. */
[dir="rtl"] body { font-family: 'IBM Plex Sans Arabic', 'IBM Plex Sans', system-ui, sans-serif; }
[dir="rtl"] .nav-logout i, [dir="rtl"] .fa-arrow-right, [dir="rtl"] .fa-arrow-left, [dir="rtl"] .fa-chevron-right, [dir="rtl"] .fa-chevron-left { transform: scaleX(-1); }
[dir="rtl"] .matrix .col-name { box-shadow: inset -1px 0 0 var(--line); }
[dir="rtl"] .num, [dir="rtl"] .device-sn, [dir="rtl"] .token-box, [dir="rtl"] .code-block, [dir="rtl"] code, [dir="rtl"] input[type="time"], [dir="rtl"] input[type="date"], [dir="rtl"] input[type="month"] { direction: ltr; unicode-bidi: isolate; }
[dir="rtl"] .table-stack td { text-align: start; }
@media (max-width: 1023px) {
    [dir="rtl"] .sidebar { inset: 0 0 0 auto; transform: translateX(100%); }
    [dir="rtl"] .drawer-open .sidebar { transform: none; }
}

/* Demo mode (config app.demo): notice bar on every page, account chips on the login page */
.demo-banner {
    display: flex; align-items: center; justify-content: center; gap: 8px; flex-wrap: wrap;
    padding: 6px 16px; font-size: 13px; font-weight: 500; text-align: center;
    background: color-mix(in srgb, var(--warn) 14%, #fff); color: var(--warn);
    border-bottom: 1px solid color-mix(in srgb, var(--warn) 30%, #fff);
}
.demo-accounts {
    display: grid; gap: 8px; padding: 12px 14px; font-size: 13px;
    border: 1px dashed color-mix(in srgb, var(--warn) 45%, #fff); border-radius: var(--radius);
    background: color-mix(in srgb, var(--warn) 6%, #fff);
}
.demo-accounts p { font-weight: 600; color: var(--warn); }
.demo-accounts > small { color: var(--muted); }
.demo-account-list { display: flex; flex-wrap: wrap; gap: 8px; }
.demo-account {
    display: grid; justify-items: start; gap: 0; padding: 6px 12px; line-height: 1.25; text-align: start;
    border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); cursor: pointer;
}
.demo-account:hover { border-color: var(--primary); background: var(--primary-soft); }
.demo-account strong { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; direction: ltr; }
.demo-account small { color: var(--muted); font-size: 12px; }
