/* ==========================================================================
   Shared UI kit: reset, typography, buttons, forms, cards, badges, tables,
   modal, toast. Arabic-first (RTL) using logical properties.
   ========================================================================== */
@import url("tokens.css");

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

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

body {
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
  overflow-x: clip;
}

img, svg { display: block; max-width: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
a { color: inherit; text-decoration: none; }
ul { list-style: none; padding: 0; }
[hidden] { display: none !important; }
/* Focus: white gap + green ring, visible on light and dark surfaces (≥3:1) */
:focus-visible { outline: 3px solid var(--focus-color); outline-offset: 2px; border-radius: 6px; box-shadow: 0 0 0 5px #ffffff; }

h1, h2, h3, h4 { font-family: var(--font-display); line-height: 1.3; color: var(--brand-700); font-weight: 700; }
h1 { font-size: clamp(1.75rem, 3vw, 2.25rem); }
h2 { font-size: clamp(1.375rem, 2.2vw, 1.75rem); }
h3 { font-size: 1.125rem; font-weight: 600; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.nowrap { white-space: nowrap; }
/* Saudi riyal symbol: as tall as the digits, with clear space of about a third of its height */
.sar { display: inline-block; width: 0.662em; height: 0.74em; margin-inline: 0.3em; vertical-align: 0; fill: currentColor; }
.ltr { direction: ltr; unicode-bidi: isolate; display: inline-block; }

/* «عربي / English» on the public pages (site-lang.js fills it) */
.lang-switch {
  display: inline-flex; align-items: center; gap: 6px; min-height: 40px; padding-inline: 12px;
  border: 1px solid var(--line); border-radius: 999px; background: var(--surface, #fff);
  color: var(--brand-700); font-size: 0.9rem; font-weight: 600; white-space: nowrap;
}
.lang-switch:hover { border-color: var(--brand-600); background: var(--brand-50); }
.lang-switch svg { flex: none; }
.lang-switch .ls-short { display: none; }
/* A block written for one language (data-lang) shows only in it; site-lang.js sets <html lang> before the page draws. */
html:not([lang="en"]) [data-lang="en"], html[lang="en"] [data-lang="ar"] { display: none !important; }

/* ---------------------------------------------------------------- buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding-inline: 18px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 0.95rem;
  white-space: nowrap;
  border: 1px solid transparent;
  transition: background 0.15s, border-color 0.15s, transform 0.15s var(--spring), box-shadow 0.2s;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn svg { width: 18px; height: 18px; flex: none; }

/* Primary = brand green with white text (≈14:1). The "accent" call-to-action
   keeps the green fill (white on orange fails contrast) and adds an orange cue. */
.btn-primary { background: var(--brand-700); color: #fff; }
.btn-primary:hover { background: var(--brand-900); }
.btn-accent { background: var(--brand-700); color: #fff; box-shadow: inset 0 -3px 0 var(--accent-500); }
.btn-accent:hover { background: var(--brand-900); }
.btn-outline { border-color: var(--brand-700); background: var(--surface); color: var(--brand-700); }
.btn-outline:hover { background: var(--brand-50); }
.btn-ghost { color: var(--brand-700); }
.btn-ghost:hover { background: var(--brand-50); }
.btn-danger { background: var(--danger-bg); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-sm { min-height: 36px; padding-inline: 12px; font-size: 0.875rem; }
.btn-lg { min-height: 50px; padding-inline: 26px; font-size: 1rem; }
.btn-icon { width: 40px; height: 40px; min-height: 40px; padding: 0; border-radius: var(--radius-sm); }
.btn-block { width: 100%; }

.btn.is-loading { position: relative; color: transparent !important; pointer-events: none; }
.btn.is-loading::after {
  content: "";
  position: absolute;
  width: 18px;
  height: 18px;
  border: 2.5px solid rgba(255, 255, 255, 0.5);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
.btn-outline.is-loading::after, .btn-ghost.is-loading::after { border-color: var(--brand-100); border-top-color: var(--brand-700); }

/* ------------------------------------------------------------------ forms */
.field { display: grid; gap: 6px; }
.field > label, .label { font-size: 0.85rem; font-weight: 600; color: var(--brand-900); }
.field .hint { font-size: 0.78rem; color: var(--muted); }
.field .error-text { font-size: 0.78rem; color: var(--danger); }

.input, .select, .textarea {
  width: 100%;
  height: 44px;
  padding-inline: 14px;
  border: 1.5px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.textarea { height: auto; min-height: 96px; padding-block: 10px; resize: vertical; }
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--brand-600);
  /* A clearly visible focus ring (it was 12% opacity): keyboard users must see where they type. */
  box-shadow: 0 0 0 3px rgba(19, 100, 81, 0.4);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand-600) 45%, transparent);
}
.input[dir="ltr"] { text-align: left; }

.input-group { display: flex; align-items: stretch; direction: ltr; }
.input-group .addon {
  display: grid;
  place-items: center;
  padding-inline: 12px;
  background: var(--brand-50);
  border: 1.5px solid var(--line);
  border-inline-end: 0;
  border-radius: var(--radius-sm) 0 0 var(--radius-sm);
  color: var(--muted);
  font-size: 0.85rem;
}
.input-group .input { border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.form-grid .span-2 { grid-column: 1 / -1; }
@media (max-width: 640px) { .form-grid { grid-template-columns: 1fr; } }

/* Toggle switch. The hidden checkbox stays inside its switch (position: relative): placed against the page instead,
   a switch in a table wider than the phone escaped the table's scroll box and widened the whole page on mobile. */
.switch { position: relative; display: inline-flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.switch input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; pointer-events: none; }
.switch .track {
  position: relative;
  width: 42px;
  height: 24px;
  border-radius: var(--radius-pill);
  background: #cfd8d4;
  transition: background 0.2s;
  flex: none;
}
.switch .track::after {
  content: "";
  position: absolute;
  top: 3px;
  inset-inline-start: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow: var(--shadow-sm);
  transition: transform 0.25s var(--spring);
}
.switch input:checked + .track { background: var(--brand-600); }
.switch input:checked + .track::after { transform: translateX(-18px); }
[dir="ltr"] .switch input:checked + .track::after { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 3px solid var(--accent-500); outline-offset: 2px; }

/* ------------------------------------------------------------------ cards */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.card-pad { padding: 20px; }
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
}
.card-head h2, .card-head h3 { font-size: 1.05rem; }
.card-body { padding: 20px; }

/* ----------------------------------------------------------------- badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: var(--radius-pill);
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
  background: var(--brand-50);
  color: var(--brand-700);
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.no-dot::before { display: none; }
.badge-success { background: var(--success-bg); color: var(--success); }
.badge-warning { background: var(--warning-bg); color: var(--warning); }
.badge-danger { background: var(--danger-bg); color: var(--danger); }
.badge-info { background: var(--info-bg); color: var(--info); }
.badge-muted { background: #eef1f0; color: var(--muted); }
.badge-accent { background: var(--accent-100); color: var(--accent-700); }
/* «وسوم العملاء»: a customer tag in its colour (dashboard and POS) */
.ctag {
  display: inline-flex; align-items: center; gap: 4px; padding: 1px 9px; border-radius: 999px; white-space: nowrap;
  font-size: 0.75rem; font-weight: 700; color: var(--ctag, #4b5563);
  background: color-mix(in srgb, var(--ctag, #4b5563) 12%, #fff); border: 1px solid color-mix(in srgb, var(--ctag, #4b5563) 26%, #fff);
}
.ctag-gold { --ctag: #a16207; }
.ctag-blue { --ctag: #1d4ed8; }
.ctag-green { --ctag: #15803d; }
.ctag-red { --ctag: #b91c1c; }
.ctag-purple { --ctag: #7e22ce; }
.ctag-gray { --ctag: #4b5563; }
.ctag-row { display: inline-flex; flex-wrap: wrap; gap: 4px; }

/* Pages lay out many boxes with an inline `display:grid` and no columns: their one column would grow with its widest
   content (a long link, a nowrap row) past a phone's width. Their items may shrink instead (zero specificity). */
:where([style*="display:grid"], [style*="display: grid"]) > * { min-width: 0; }
.link-box { min-width: 0; }

/* Printing one page alone with the browser's print window (web/shared/js/printing.js printWithBrowser). */
.print-target { display: none; }
@media print {
  body.is-printing-target > *:not(.print-target) { display: none !important; }
  body.is-printing-target .print-target { display: block !important; }
  body.is-printing-target .print-target, body.is-printing-target .print-target * { visibility: visible !important; }
}

/* ----------------------------------------------------------------- tables */
/* position: relative keeps anything positioned inside a table within its scroll box, so a wide table scrolls on its
   own on a phone instead of widening the page. */
.table-wrap { overflow-x: auto; position: relative; }
.table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.table th {
  text-align: start;
  font-weight: 600;
  font-size: 0.78rem;
  color: var(--muted);
  padding: 12px 16px;
  background: #fafbfa;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
.table td { padding: 12px 16px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr { transition: background 0.15s; }
.table tbody tr:hover { background: #fafcfb; }
.table tr.is-clickable { cursor: pointer; }
.cell-main { display: flex; align-items: center; gap: 12px; }
.cell-main strong { display: block; color: var(--brand-900); }
.avatar {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  object-fit: cover;
  background: var(--brand-50);
  display: grid;
  place-items: center;
  font-weight: 700;
  color: var(--brand-700);
  flex: none;
  border: 1px solid var(--line);
}

/* ----------------------------------------------------------- empty state */
.empty {
  padding: 48px 20px;
  text-align: center;
  color: var(--muted);
}
.empty svg { width: 54px; height: 54px; margin: 0 auto 12px; color: var(--brand-600); opacity: 0.5; }
.empty strong { display: block; color: var(--brand-900); font-size: 1.05rem; margin-bottom: 4px; }

/* ------------------------------------------------------------------ modal */
.modal-root {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  grid-template-columns: minmax(0, 1fr);   /* fixed track: wide content (long links) must not widen the modal past the screen */
  place-items: center;
  padding: 16px;
  background: rgba(11, 30, 24, 0.55);
  backdrop-filter: blur(4px);
  animation: fade-in 0.2s var(--ease);
}
.modal-root.is-closing { animation: fade-out 0.18s var(--ease) forwards; }
.modal {
  width: min(560px, 100%);
  max-height: calc(100vh - 32px);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  animation: pop-in 0.35s var(--spring);
}
.modal.modal-lg { width: min(820px, 100%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 22px; border-bottom: 1px solid var(--line); }
.modal-body { padding: 22px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 22px; border-top: 1px solid var(--line); background: #fafbfa; border-radius: 0 0 var(--radius-lg) var(--radius-lg); }

/* ------------------------------------------------------------------ toast */
.toast-stack {
  position: fixed;
  bottom: 20px;
  inset-inline-start: 50%;
  transform: translateX(50%);
  z-index: 200;
  display: grid;
  gap: 8px;
  justify-items: center;
}
[dir="ltr"] .toast-stack { transform: translateX(-50%); }
.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 18px;
  border-radius: var(--radius);
  background: var(--brand-900);
  color: #fff;
  box-shadow: var(--shadow-lg);
  font-size: 0.9rem;
  animation: toast-in 0.35s var(--spring);
}
.toast.is-error { background: var(--danger); }
.toast.is-leaving { animation: fade-out 0.25s forwards; }

/* ---------------------------------------------------------------- loading */
.skeleton {
  border-radius: var(--radius-sm);
  background: linear-gradient(90deg, #eef2f0 25%, #f7f9f8 50%, #eef2f0 75%);
  background-size: 200% 100%;
  animation: shimmer 1.2s linear infinite;
}
.page-loader { display: grid; place-items: center; min-height: 280px; }
.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid var(--brand-100);
  border-top-color: var(--brand-700);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* ------------------------------------------------------------- keyframes */
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }
@keyframes pop-in { from { opacity: 0; transform: translateY(16px) scale(0.96); } }
@keyframes toast-in { from { opacity: 0; transform: translateY(16px) scale(0.9); } }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(14px); } }

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