/* ستاكرون — مكونات مشتركة. الألوان كلها من tokens.css */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100dvh;
  font-family: var(--ui); font-size: 16px; line-height: 1.6;
  background: var(--bg); color: var(--fg);
  -webkit-tap-highlight-color: transparent;
}
/* كرّاسة: سطور الورق */
html[data-theme="s2"] body {
  background-image: repeating-linear-gradient(to bottom, transparent 0 31px, var(--rule) 31px 32px);
}
h1, h2, h3, h4, p { margin: 0; }
h1 { font-size: 25px; line-height: 1.35; font-weight: 800; }
h2 { font-size: 20px; line-height: 1.4; font-weight: 700; }
h3 { font-size: 16px; line-height: 1.5; font-weight: 600; }
a { color: var(--brand); }
button, input, select, textarea { font: inherit; color: inherit; }
img, svg { display: block; }
code, kbd, pre, .mono { font-family: var(--mono); }
.ltr { direction: ltr; unicode-bidi: isolate; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden, [hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
::selection { background: var(--brand-soft); }

:focus { outline: none; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: var(--r-sm); }

.ic { width: 20px; height: 20px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.ic-sm { width: 16px; height: 16px; }
[dir="rtl"] .flip-rtl { transform: scaleX(-1); }

/* ---------- الهايلايت (كرّاسة = قلم فسفوري، ورشة = ليموني) ---------- */
.hl {
  color: var(--hl-fg);
  background: linear-gradient(transparent 52%, var(--hl-bg) 52% 92%, transparent 92%);
  padding-inline: 2px;
  -webkit-box-decoration-break: clone; box-decoration-break: clone;
}

/* ---------- اللوجو ---------- */
.logo { display: inline-flex; align-items: center; gap: 8px; text-decoration: none; color: var(--fg); min-height: 44px; }
.logo-mark {
  display: inline-grid; place-items: center; width: 32px; height: 32px; flex: none;
  border-radius: var(--r-sm); background: var(--logo-bg); color: var(--logo-fg);
  font: 600 14px var(--mono); letter-spacing: -.5px; direction: ltr;
}
.logo-word { display: flex; flex-direction: column; line-height: 1.05; }
.logo-word b { font: 800 16px var(--ui); letter-spacing: 1.5px; direction: ltr; color: var(--fg); }
.logo-word small { font-size: 11px; color: var(--muted); font-weight: 600; }

/* ---------- الأزرار ---------- */
.btn {
  --b-bg: var(--surface); --b-fg: var(--fg); --b-bc: var(--line-c); --b-sh: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 16px; border-radius: var(--r-sm);
  border: var(--bw) solid var(--b-bc); background: var(--b-bg); color: var(--b-fg);
  font-weight: 600; font-size: 15px; line-height: 1.2; text-decoration: none; white-space: nowrap;
  cursor: pointer; user-select: none; box-shadow: var(--b-sh);
  transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease), background-color var(--dur) var(--ease), opacity var(--dur);
}
.btn:hover { --b-bg: color-mix(in srgb, var(--surface) 88%, var(--fg)); }
.btn:active { transform: translateY(1px); }
.btn[disabled], .btn[aria-disabled="true"] { opacity: .5; pointer-events: none; }
.btn-primary { --b-bg: var(--accent); --b-fg: var(--on-accent); --b-bc: transparent; --b-sh: var(--btn-shadow); }
.btn-primary:hover { --b-bg: color-mix(in srgb, var(--accent) 90%, var(--fg)); }
.btn-ok { --b-bg: var(--ok); --b-fg: var(--on-ok); --b-bc: transparent; }
.btn-ok:hover { --b-bg: color-mix(in srgb, var(--ok) 88%, var(--fg)); }
.btn-danger { --b-bg: var(--danger); --b-fg: var(--on-danger); --b-bc: transparent; }
.btn-danger:hover { --b-bg: color-mix(in srgb, var(--danger) 88%, var(--fg)); }
.btn-ghost { --b-bg: transparent; --b-bc: transparent; }
.btn-ghost:hover { --b-bg: var(--fill); }
.btn-text-danger { color: var(--danger); }
.btn-icon { padding: 0; width: 44px; }
.btn-lg { min-height: 52px; padding: 0 24px; font-size: 16px; }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: 13px; }
.btn-block { width: 100%; }
.btn .spinner { width: 16px; height: 16px; }
/* ورشة: برواز أسود وضل حاد بيتضغط */
html[data-theme="s3"] .btn:not(.btn-ghost) { --b-bc: var(--fg); }
html[data-theme="s3"][data-mode="dark"] .btn:not(.btn-ghost):not(.btn-primary) { --b-bc: var(--line-c); }
html[data-theme="s3"] .btn-primary, html[data-theme="s3"] .btn-ok, html[data-theme="s3"] .btn-danger { --b-sh: var(--btn-shadow); }
html[data-theme="s3"] .btn:not(.btn-ghost):active { transform: translate(3px, 3px); --b-sh: var(--btn-press); }
html[data-theme="s2"] .btn-primary:active, html[data-theme="s4"] .btn-primary:active { --b-sh: var(--btn-press); transform: translateY(2px); }
html[data-theme="s4"] .btn { border-radius: 999px; }
html[data-theme="s4"] .btn-icon { border-radius: 50%; }

/* ---------- الحقول ---------- */
.field { display: grid; gap: 6px; }
.field > span, .label { font-size: 14px; font-weight: 600; }
.field small { font-size: 12px; color: var(--muted); }
.input, .field input, .field select, .field textarea {
  width: 100%; min-height: 48px; padding: 10px 14px;
  border-radius: var(--r-sm); border: var(--bw) solid var(--line-c);
  background: var(--code-bg); color: var(--fg); font-size: 16px;
  transition: border-color var(--dur), box-shadow var(--dur);
}
html[data-theme="s1"][data-mode="dark"] .input, html[data-theme="s1"][data-mode="dark"] .field input { background: var(--bar); }
html[data-theme="s3"] .input, html[data-theme="s3"] .field input { border-color: var(--fg); }
html[data-theme="s3"][data-mode="dark"] .input, html[data-theme="s3"][data-mode="dark"] .field input { border-color: var(--line-c); }
.input:focus, .field input:focus, .field select:focus, .field textarea:focus {
  outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft);
}
.input::placeholder, .field input::placeholder { color: var(--muted); opacity: .8; }
.input.ltr, .field input.ltr { direction: ltr; text-align: right; }
.input.ltr:placeholder-shown { text-align: right; }
.form { display: grid; gap: 16px; }
.form-error { color: var(--danger); font-size: 14px; font-weight: 600; min-height: 0; }
.form-error:empty { display: none; }
.pw-wrap { position: relative; }
.pw-wrap input { padding-inline-end: 52px; }
.pw-wrap button { position: absolute; inset-inline-end: 2px; top: 2px; bottom: 2px; width: 44px; border: 0; background: transparent; color: var(--muted); cursor: pointer; display: grid; place-items: center; border-radius: var(--r-sm); }

/* ---------- كروت ---------- */
.card {
  background: var(--surface); border: var(--line); border-radius: var(--r);
  box-shadow: var(--shadow-sm); padding: 16px;
}
html[data-theme="s1"] .card { border-color: var(--line-c); }
.card-focal { box-shadow: var(--shadow); border: var(--frame); }

/* ---------- البار العلوي ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  min-height: 60px; padding: 8px 16px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--bar); border-bottom: var(--line);
}
.topbar .spacer { flex: 1; }
.topbar-title { flex: 1; min-width: 0; font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: var(--brand-soft); color: var(--brand); font-weight: 800; font-size: 16px;
  border: var(--bw) solid var(--line-c);
}
html[data-theme="s3"] .avatar { border-radius: var(--r-sm); background: var(--accent); color: var(--on-accent); border-color: var(--fg); }
.avatar-btn { min-width: 44px; min-height: 44px; display: grid; place-items: center; background: none; border: 0; padding: 0; cursor: pointer; border-radius: 50%; }

.page { width: 100%; max-width: 1080px; margin: 0 auto; padding: 24px 16px 96px; }
.page-narrow { max-width: 640px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 24px; flex-wrap: wrap; }
.page-head p { color: var(--muted); font-size: 14px; margin-top: 4px; }
.section { margin-top: 32px; }
.section > h2 { margin-bottom: 12px; font-size: 18px; }

/* ---------- بادجات وتشيبس ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 4px; min-height: 24px; padding: 2px 10px;
  border-radius: 999px; font-size: 12px; font-weight: 600; line-height: 1.4; white-space: nowrap;
  background: var(--fill); color: var(--fg);
}
.badge-brand { background: var(--brand-soft); color: var(--brand); }
.badge-hl { background: var(--hint-bg); color: var(--hint-fg); }
.badge-ok { background: var(--add-bg); color: var(--fg); }
.badge-danger { background: var(--del-bg); color: var(--fg); }
html[data-theme="s3"] .badge { border-radius: 6px; border: var(--bw) solid var(--line-c); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; display: inline-block; }
.dot-on { background: var(--ok); box-shadow: 0 0 0 0 var(--ok); animation: pulse 1.8s infinite; }
.dot-warn { background: var(--warn); }
.dot-err { background: var(--danger); }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--ok) 60%, transparent); } 70% { box-shadow: 0 0 0 8px transparent; } 100% { box-shadow: 0 0 0 0 transparent; } }

/* ---------- التابات / segmented ---------- */
.seg {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; padding: 4px;
  background: var(--fill); border-radius: calc(var(--r-sm) + 4px);
}
html[data-theme="s3"] .seg { border: var(--line); background: var(--surface); }
.seg button {
  min-height: 44px; border: 0; border-radius: var(--r-sm); background: transparent; color: var(--muted);
  font-weight: 600; font-size: 15px; cursor: pointer; transition: background-color var(--dur), color var(--dur);
}
.seg button[aria-selected="true"], .seg button[aria-pressed="true"] { background: var(--surface); color: var(--fg); box-shadow: var(--shadow-sm); }
html[data-theme="s3"] .seg button[aria-selected="true"], html[data-theme="s3"] .seg button[aria-pressed="true"] { background: var(--brand); color: var(--on-brand); box-shadow: none; }
html[data-theme="s4"] .seg, html[data-theme="s4"] .seg button { border-radius: 999px; }

/* ---------- hint ---------- */
.hint {
  display: flex; gap: 8px; align-items: flex-start; padding: 12px 14px; border-radius: var(--r-sm);
  background: var(--hint-bg); color: var(--hint-fg); border: var(--bw) solid var(--hint-bc); font-size: 14px; line-height: 1.7;
}
.hint .ic { margin-top: 3px; }
.hint em { font-style: normal; font-weight: 700; color: var(--brand); }
html[data-theme="s3"]:not([data-mode="dark"]) .hint em { color: inherit; }

/* ---------- empty state ---------- */
.empty { text-align: center; padding: 48px 16px; display: grid; justify-items: center; gap: 12px; }
.empty-art { width: 96px; height: 96px; display: grid; place-items: center; border-radius: var(--r-lg); background: var(--brand-soft); color: var(--brand); margin-bottom: 8px; }
html[data-theme="s3"] .empty-art { background: var(--accent); color: var(--on-accent); border: var(--line); box-shadow: var(--shadow-sm); }
.empty-art .ic { width: 44px; height: 44px; stroke-width: 1.6; }
.empty h2 { font-size: 20px; }
.empty p { color: var(--muted); max-width: 34ch; font-size: 15px; }
.steps { display: grid; gap: 8px; text-align: start; margin-top: 8px; padding: 0; list-style: none; counter-reset: s; }
.steps li { display: flex; gap: 12px; align-items: center; font-size: 14px; counter-increment: s; }
.steps li::before { content: counter(s, arabic-indic); width: 28px; height: 28px; flex: none; display: grid; place-items: center; border-radius: 50%; background: var(--fill); font-weight: 700; font-size: 13px; }

/* ---------- spinner ---------- */
.spinner { width: 24px; height: 24px; border-radius: 50%; border: 2.5px solid var(--fill-2); border-top-color: var(--brand); animation: spin .8s linear infinite; display: inline-block; flex: none; }
.loading { display: grid; place-items: center; padding: 48px; }
@keyframes spin { to { transform: rotate(360deg); } }
.skel { background: var(--fill); border-radius: var(--r); animation: skel 1.4s ease-in-out infinite; }
@keyframes skel { 50% { opacity: .5; } }

/* ---------- dialogs: sheet + modal ---------- */
dialog.dlg {
  padding: 0; border: 0; color: var(--fg); background: transparent; max-width: none; max-height: none;
  width: 100%; height: 100%; margin: 0; inset: 0; display: flex; overscroll-behavior: contain;
}
dialog.dlg:not([open]) { display: none; }
dialog.dlg::backdrop { background: transparent; }
.dlg-scrim { position: fixed; inset: 0; background: var(--scrim); opacity: 0; transition: opacity var(--dur) var(--ease); }
.dlg-panel {
  position: relative; margin-top: auto; width: 100%; max-height: 88dvh; overflow: auto;
  background: var(--surface); border-radius: var(--r-lg) var(--r-lg) 0 0; border: var(--frame); border-bottom: 0;
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
  transform: translateY(24px); opacity: 0; transition: transform 220ms var(--ease), opacity 220ms var(--ease);
  box-shadow: var(--shadow);
}
html[data-theme="s3"] .dlg-panel { box-shadow: none; }
dialog.dlg.in .dlg-scrim { opacity: 1; }
dialog.dlg.in .dlg-panel { transform: none; opacity: 1; }
.dlg-grab { width: 40px; height: 4px; border-radius: 4px; background: var(--fill-2); margin: 4px auto 12px; }
.dlg-head { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.dlg-head h2 { flex: 1; font-size: 18px; }
.dlg-sub { color: var(--muted); font-size: 14px; margin-top: -8px; margin-bottom: 16px; }
.dlg-actions { display: flex; gap: 8px; margin-top: 20px; }
.dlg-actions .btn { flex: 1; }
/* مودال في النص (تأكيد) — وعلى الديسكتوب كل الشيتات */
dialog.dlg.modal .dlg-panel {
  margin: auto 16px; border-radius: var(--r-lg); border: var(--frame); max-width: 420px;
  padding: 24px 20px 20px; transform: scale(.96);
}
dialog.dlg.modal { align-items: center; justify-content: center; }
dialog.dlg.modal.in .dlg-panel { transform: none; }
@media (min-width: 900px) {
  dialog.dlg { align-items: center; justify-content: center; }
  .dlg-panel { margin: auto; max-width: 520px; border-radius: var(--r-lg); border-bottom: var(--frame); padding: 20px 24px 24px; transform: scale(.97); }
  .dlg-panel.wide { max-width: 860px; }
  .dlg-grab { display: none; }
}
html[data-theme="s3"] dialog.dlg.modal .dlg-panel, html[data-theme="s3"] .dlg-panel { box-shadow: var(--shadow); }
.confirm-ic { width: 48px; height: 48px; border-radius: var(--r); display: grid; place-items: center; margin-bottom: 16px; background: var(--brand-soft); color: var(--brand); }
.confirm-ic.danger { background: var(--del-bg); color: var(--danger); }
.confirm h2 { font-size: 19px; margin-bottom: 6px; }
.confirm p { color: var(--muted); font-size: 15px; }

/* ---------- قايمة أكشنز (جوه sheet أو popover) ---------- */
.menu { display: grid; gap: 2px; padding: 0; margin: 0; list-style: none; }
.menu button, .menu a {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 48px; padding: 0 12px;
  border: 0; background: transparent; color: var(--fg); text-align: start; text-decoration: none;
  font-size: 15px; border-radius: var(--r-sm); cursor: pointer;
}
.menu button:hover, .menu a:hover, .menu button:focus-visible { background: var(--fill); }
.menu .danger { color: var(--danger); }
.menu hr { border: 0; border-top: var(--line); margin: 4px 0; opacity: .7; }
.menu .ic { color: var(--muted); }
.menu .danger .ic { color: var(--danger); }
.popover {
  position: fixed; z-index: 60; min-width: 220px; max-width: 300px; padding: 6px;
  background: var(--surface); border: var(--frame); border-radius: var(--r); box-shadow: var(--shadow);
  opacity: 0; transform: translateY(-4px); transition: opacity 150ms var(--ease), transform 150ms var(--ease);
}
.popover.in { opacity: 1; transform: none; }

/* ---------- toast ---------- */
.toasts { position: fixed; z-index: 100; inset-inline: 0; bottom: calc(80px + env(safe-area-inset-bottom)); display: grid; justify-items: center; gap: 8px; pointer-events: none; padding: 0 16px; }
.toast {
  display: flex; align-items: center; gap: 10px; max-width: 440px; padding: 12px 16px; border-radius: var(--r);
  background: var(--fg); color: var(--bg); font-size: 14px; font-weight: 600; box-shadow: var(--shadow);
  opacity: 0; transform: translateY(8px); transition: opacity 200ms var(--ease), transform 200ms var(--ease);
  pointer-events: auto;
}
.toast.in { opacity: 1; transform: none; }
.toast.err { background: var(--danger); color: var(--on-danger); }
.toast.ok .ic { color: var(--ok); }
html[data-theme="s3"] .toast { border: 2px solid var(--fg); box-shadow: var(--btn-shadow); }
@media (min-width: 900px) { .toasts { bottom: 24px; } }

/* ---------- progress ---------- */
.progress { height: 8px; border-radius: 8px; background: var(--fill); overflow: hidden; }
.progress > i { display: block; height: 100%; width: 0; background: var(--brand); border-radius: inherit; transition: width 200ms linear; }

/* ---------- جدول/ليست ---------- */
.list { display: grid; gap: 0; background: var(--surface); border: var(--line); border-radius: var(--r); overflow: hidden; box-shadow: var(--shadow-sm); }
.list > .row { display: flex; align-items: center; gap: 12px; padding: 12px 16px; min-height: 64px; border-bottom: var(--line); }
.list > .row:last-child { border-bottom: 0; }
.row-main { flex: 1; min-width: 0; }
.row-main b { display: block; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-main small { display: block; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- toggle ---------- */
.switch { position: relative; display: inline-flex; align-items: center; min-height: 44px; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch i { width: 48px; height: 28px; border-radius: 28px; background: var(--fill-2); position: relative; transition: background-color var(--dur); border: var(--bw) solid var(--line-c); }
.switch i::after { content: ""; position: absolute; top: 2px; inset-inline-start: 2px; width: 22px; height: 22px; border-radius: 50%; background: var(--surface); box-shadow: var(--shadow-sm); transition: inset-inline-start var(--dur) var(--ease); }
.switch input:checked + i { background: var(--ok); }
.switch input:checked + i::after { inset-inline-start: 22px; }
.switch input:focus-visible + i { outline: 2px solid var(--focus); outline-offset: 2px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-iteration-count: 1 !important; transition-duration: 1ms !important; scroll-behavior: auto !important; }
}
html[data-theme="s3"][data-mode="dark"] .btn-primary { --b-bc: transparent; }
