/* Platform control panel (super admin) — RTL, Almarai, light/dark through data-theme tokens.
   Visual identity follows the staff app and patient portal: medical sky/teal on warm-neutral, dark-slate chrome. */
:root {
  color-scheme: light;
  --bg: var(--uk-canvas);
  --surface: var(--uk-surface);
  --surface-2: var(--uk-surface-subtle);
  --ink: var(--uk-text);
  --muted: var(--uk-text-muted);
  --line: var(--uk-border);
  --chrome: #18181B;
  --chrome-ink: #F4F4F5;
  --chrome-muted: #A1A1AA;
  --primary: var(--uk-brand);
  --primary-hover: var(--uk-brand-hover);
  --primary-ink: var(--uk-text-on-brand);
  --primary-soft: var(--uk-brand-soft-strong);
  --brand: var(--uk-brand);
  --ok: var(--uk-success-text);
  --ok-soft: var(--uk-success-soft);
  --warn: var(--uk-warning-text);
  --warn-soft: var(--uk-warning-soft);
  --danger: var(--uk-danger-text);
  --danger-soft: var(--uk-danger-soft);
  --focus: var(--uk-brand);
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 8px 24px -12px rgba(15, 23, 42, .12);
  --radius: 14px;
  --tap: 44px;
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --chrome: #0E0F10;
  --chrome-ink: #F4F4F5;
  --chrome-muted: #A1A1AA;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px -12px rgba(0, 0, 0, .5);
}

*, *::before, *::after { box-sizing: border-box; font-family: "Almarai", "Cairo", system-ui, sans-serif; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font-size: 15px; line-height: 1.6; min-height: 100vh; overflow-x: hidden; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 6px; }
.skip-link { position: absolute; inset-inline-start: 12px; top: -60px; background: var(--primary); color: var(--primary-ink); padding: 10px 16px; border-radius: 10px; z-index: 100; text-decoration: none; }
.skip-link:focus { top: 12px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; display: inline-block; }

/* ── buttons & fields ── */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: var(--tap); padding: 0 18px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); font-weight: 700; font-size: 14px; cursor: pointer; text-decoration: none; transition: background .15s, border-color .15s, transform .1s; }
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn[disabled] { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-danger { background: var(--danger-soft); border-color: transparent; color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); filter: brightness(.96); }
.btn-ok { background: var(--ok-soft); border-color: transparent; color: var(--ok); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-sm { padding: 0 12px; font-size: 13px; }
.icon-btn { width: var(--tap); height: var(--tap); padding: 0; flex: none; }
.btn svg, .icon-btn svg { flex: none; }

label.field { display: grid; gap: 6px; font-weight: 700; font-size: 13px; color: var(--muted); }
.input, select.input, textarea.input { width: 100%; min-height: var(--tap); border-radius: 12px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); padding: 8px 14px; font-size: 15px; font-weight: 400; }
.input:hover { border-color: color-mix(in srgb, var(--primary) 40%, var(--line)); }
.input[aria-invalid="true"] { border-color: var(--danger); }
.input.ltr { direction: ltr; text-align: left; }
.hint { font-size: 12.5px; font-weight: 400; color: var(--muted); min-height: 18px; }
.hint.bad { color: var(--danger); }
.hint.good { color: var(--ok); }
.alert { padding: 10px 14px; border-radius: 12px; background: var(--danger-soft); color: var(--danger); font-size: 14px; font-weight: 700; }

/* ── login ── */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: linear-gradient(180deg, var(--chrome) 0 38%, var(--bg) 38%); }
.login-card { width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--line); border-radius: 20px; box-shadow: var(--shadow); padding: 28px 24px; display: grid; gap: 16px; }
.login-card h1 { margin: 0; font-size: 20px; }
.login-card p { margin: 0; color: var(--muted); font-size: 14px; }

/* ── brand ── */
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark { width: 42px; height: 42px; border-radius: 12px; background: var(--brand); color: #fff; display: grid; place-items: center; flex: none; }
.brand h1 { margin: 0; font-size: 17px; line-height: 1.25; }
.brand p { margin: 0; font-size: 12.5px; color: var(--chrome-muted); }

/* ── chrome ── */
.top { background: var(--chrome); color: var(--chrome-ink); }
.top-inner { max-width: 1280px; margin: 0 auto; padding: 14px 16px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.top .btn { background: rgba(255,255,255,.07); border-color: rgba(255,255,255,.14); color: var(--chrome-ink); }
.top .btn:hover { background: rgba(255,255,255,.14); }
.top-actions { display: flex; align-items: center; gap: 8px; }
.who { font-size: 13px; color: var(--chrome-muted); max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.i-sun { display: none; }
:root[data-theme="dark"] .i-sun { display: block; }
:root[data-theme="dark"] .i-moon { display: none; }

main { max-width: 1280px; margin: 0 auto; padding: 0 16px 56px; }

/* ── KPI strip: one surface, divided cells ── */
.kpis { margin-top: -22px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); display: grid; grid-template-columns: repeat(2, 1fr); overflow: hidden; }
.kpi { padding: 14px 16px; display: grid; gap: 2px; border-block-end: 1px solid var(--line); border-inline-end: 1px solid var(--line); }
.kpi:nth-child(2n) { border-inline-end: 0; }
.kpi-label { font-size: 12.5px; color: var(--muted); font-weight: 700; }
.kpi-value { font-size: 26px; font-weight: 800; line-height: 1.2; }
.kpi-sub { font-size: 12px; color: var(--muted); }
.kpi.is-alert { background: var(--warn-soft); }
.kpi.is-alert .kpi-value, .kpi.is-alert .kpi-label { color: var(--warn); }

/* ── toolbar ── */
.toolbar { display: grid; gap: 12px; margin: 22px 0 14px; }
.search { position: relative; }
.search svg { position: absolute; inset-inline-start: 14px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.search .input { padding-inline-start: 42px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { min-height: var(--tap); padding: 0 16px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); font-weight: 700; font-size: 13.5px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; }
.chip[aria-pressed="true"] { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.chip .count { font-size: 12px; opacity: .8; }

/* ── clinic list (cards on phones, table-like rows from 1024px) ── */
.list { display: grid; gap: 12px; }
.list-head { display: none; }
.clinic { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; display: grid; gap: 12px; }
.clinic-name { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.clinic-name strong { font-size: 16px; overflow-wrap: anywhere; }
.clinic-name small { color: var(--muted); font-size: 12.5px; overflow-wrap: anywhere; }
.cell-label { display: block; font-size: 12px; color: var(--muted); font-weight: 700; margin-bottom: 2px; }
.clinic-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.badge { display: inline-flex; align-items: center; gap: 6px; min-height: 26px; padding: 0 10px; border-radius: 999px; font-size: 12.5px; font-weight: 800; white-space: nowrap; }
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge.trial { background: var(--primary-soft); color: var(--primary); }
.badge.active { background: var(--ok-soft); color: var(--ok); }
.badge.suspended { background: var(--danger-soft); color: var(--danger); }
.badge.expired { background: var(--warn-soft); color: var(--warn); }
.soon { display: inline-block; margin-inline-start: 6px; font-size: 12px; font-weight: 800; color: var(--warn); }
.plan { font-weight: 800; }
.sub-line { font-size: 13px; color: var(--muted); }
.bar { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; margin: 6px 0 4px; }
.bar > i { display: block; height: 100%; width: 0; background: var(--brand); border-radius: inherit; }
.bar.warn > i { background: var(--warn); }
.bar.danger > i { background: var(--danger); }
.storage-text { font-size: 12.5px; color: var(--muted); }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.actions .btn { padding: 0 10px; font-size: 13px; white-space: nowrap; }
.actions .span-2 { grid-column: span 2; }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); }
.empty strong { display: block; color: var(--ink); font-size: 16px; margin-bottom: 4px; }
.skeleton { height: 120px; border-radius: var(--radius); background: linear-gradient(90deg, var(--surface-2), var(--surface), var(--surface-2)); background-size: 200% 100%; animation: sk 1.4s infinite linear; }
@keyframes sk { to { background-position: -200% 0; } }

/* ── dialogs ── */
dialog { border: 1px solid var(--line); border-radius: 20px; padding: 0; background: var(--surface); color: var(--ink); width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 24px); max-height: calc(100dvh - 24px); box-shadow: 0 24px 64px -16px rgba(2, 6, 23, .5); }
dialog::backdrop { background: rgba(2, 6, 23, .55); backdrop-filter: blur(2px); }
dialog[open] { display: flex; flex-direction: column; }
dialog form { display: flex; flex-direction: column; min-height: 0; flex: 1 1 auto; }
.dlg-head { flex: none; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px; border-block-end: 1px solid var(--line); position: sticky; top: 0; background: var(--surface); z-index: 1; }
.dlg-head h2 { margin: 0; font-size: 17px; }
.dlg-body { padding: 18px 20px; display: grid; gap: 14px; overflow-y: auto; flex: 1 1 auto; min-height: 0; align-content: start; }
.dlg-foot { flex: none; display: flex; flex-wrap: wrap; gap: 10px; justify-content: flex-end; padding: 14px 20px 18px; border-block-start: 1px solid var(--line); }
.grid-2 { display: grid; gap: 14px; grid-template-columns: 1fr; }
.row-inline { display: flex; gap: 8px; flex-wrap: wrap; }
.row-inline .btn { flex: 1 1 auto; }
fieldset { border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px 14px; margin: 0; display: grid; gap: 12px; min-width: 0; }
legend { padding: 0 6px; font-weight: 800; font-size: 13px; color: var(--ink); }
.pw-row { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: end; }

/* ── toasts ── */
.toasts { position: fixed; inset-inline: 12px; bottom: 16px; display: grid; gap: 8px; z-index: 200; pointer-events: none; justify-items: center; }
.toast { pointer-events: auto; max-width: 420px; width: 100%; background: var(--chrome); color: var(--chrome-ink); padding: 12px 16px; border-radius: 12px; box-shadow: var(--shadow); font-size: 14px; font-weight: 700; border-inline-start: 4px solid var(--brand); }
.toast.bad { border-inline-start-color: #fb7185; }

@media (min-width: 640px) {
  .kpis { grid-template-columns: repeat(4, 1fr); }
  .kpi:nth-child(2n) { border-inline-end: 1px solid var(--line); }
  .kpi:nth-child(4n) { border-inline-end: 0; }
  .toolbar { grid-template-columns: 1fr auto; align-items: start; }
  .chips { grid-column: 1 / -1; }
  .grid-2 { grid-template-columns: 1fr 1fr; }
}
@media (min-width: 1024px) {
  .kpis { grid-template-columns: repeat(7, 1fr); }
  .kpi, .kpi:nth-child(n) { border-block-end: 0; border-inline-end: 1px solid var(--line); }
  .kpi:last-child { border-inline-end: 0; }
  .toolbar { grid-template-columns: minmax(280px, 1fr) auto auto; }
  .chips { grid-column: auto; grid-row: 2; grid-column: 1 / -1; }
}
@media (min-width: 1180px) {
  .list { gap: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
  .list-head, .clinic { display: grid; grid-template-columns: minmax(190px, 2fr) 84px 126px 146px minmax(150px, 1.2fr) 332px; gap: 16px; align-items: center; padding: 12px 18px; }
  .list-head { background: var(--surface-2); color: var(--muted); font-size: 12.5px; font-weight: 800; border-block-end: 1px solid var(--line); }
  .clinic { border: 0; border-block-end: 1px solid var(--line); border-radius: 0; box-shadow: none; }
  .clinic:last-child { border-block-end: 0; }
  .clinic-meta { display: contents; }
  .cell-label { display: none; }
  .actions { grid-template-columns: repeat(3, auto); justify-content: start; }
  .actions .span-2 { grid-column: auto; }
  .empty, .skeleton { border-radius: 0; }

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

.flush { margin: 0; }
.i-mirror { transform: scaleX(-1); }   /* arrows that point "out" must flip in RTL */
.kpi-unit { font-size: 13px; font-weight: 700; color: var(--muted); }
.login-card .brand h1 { color: var(--ink); }
.login-card .brand p { color: var(--muted); }

/* layout polish: header never wraps its title; the 7th KPI fills its row */
.brand h1 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand > div { min-width: 0; }
.kpi:last-child { grid-column: span 2; }
@media (max-width: 639px) {
  .who, .brand p { display: none; }
  .top-actions { gap: 8px; }
}
@media (min-width: 1024px) {
  .kpi:last-child { grid-column: auto; }
}
  /* iOS Safari zooms the page when a focused field has a font-size below 16px */
  @media (max-width: 640px) {
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not(.otp), select, textarea { font-size: 16px !important; }
  }
@media (max-width: 640px) {
  .kpi-label, .chip .count, .cell-label, .clinic-name small, .badge, .hint, .storage-text, small, .num { font-size: 13px; }
  .btn, button, select, .input { min-height: 44px; }
  .btn-primary, button[type="submit"] { min-height: 48px; }
}

/* clinic users table (users modal) */
.users-table { font-size: 13px; }
.users-table th { text-align: start; font-weight: 600; color: var(--muted, inherit); white-space: nowrap; }
.users-table th, .users-table td { padding: 8px 10px; border-bottom: 1px solid var(--line, rgba(128,128,128,.25)); vertical-align: middle; }
.users-table td:last-child { white-space: nowrap; }
.pw-row.pw-row-4 { grid-template-columns: 1fr auto auto auto; }
