/* =====================================================================
   Attendance System — ملف التصميم الرئيسي
   الاتجاه الأساسي RTL. الخط: IBM Plex Sans Arabic.
   كل الألوان والمسافات معرفة كمتغيرات في :root لسهولة التعديل.
   ===================================================================== */

:root {
  /* الألوان */
  --bg: #f2f4f3;
  --surface: #ffffff;
  --surface-2: #f7f9f8;
  --ink: #1c2526;
  --muted: #5b6b6a;
  --line: #dce2e0;
  --primary: #0e6b5f;
  --primary-hover: #0a5a50;
  --primary-soft: #e3f1ee;
  --success: #23804a;
  --success-soft: #e4f3ea;
  --warn: #8a5a0f;
  --warn-soft: #fbf1de;
  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --focus: #2f8fd8;

  /* الخط */
  --font: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, Arial, sans-serif;
  --fs-xs: 0.78rem;
  --fs-sm: 0.875rem;
  --fs-md: 1rem;
  --fs-lg: 1.2rem;
  --fs-xl: 1.6rem;
  --fs-2xl: 2.4rem;

  /* المسافات والحواف */
  --sp-1: 4px; --sp-2: 8px; --sp-3: 12px; --sp-4: 16px; --sp-5: 24px; --sp-6: 32px; --sp-7: 48px;
  --r-sm: 6px; --r-md: 10px; --r-lg: 16px;
  --sidebar-w: 248px;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: var(--fs-md);
  line-height: 1.6;
  color: var(--ink);
  background: var(--bg);
}
h1, h2, h3 { margin: 0; line-height: 1.3; font-weight: 600; }
h1 { font-size: var(--fs-xl); }
h2 { font-size: var(--fs-lg); }
h3 { font-size: var(--fs-md); }
p { margin: 0; }
a { color: var(--primary); }
img { max-width: 100%; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: var(--fs-sm); }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

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

/* ---------------------------------------------------------------- Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  min-height: 44px; padding: 0 var(--sp-4);
  font: inherit; font-weight: 600; font-size: var(--fs-sm);
  border: 1px solid transparent; border-radius: var(--r-sm);
  cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: background-color .15s, border-color .15s;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-secondary { background: var(--surface); color: var(--ink); border-color: var(--line); }
.btn-secondary:hover:not(:disabled) { background: var(--surface-2); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-ghost { background: transparent; color: var(--primary); }
.btn-ghost:hover:not(:disabled) { background: var(--primary-soft); }
.btn-sm { min-height: 34px; padding: 0 var(--sp-3); }
.btn-block { width: 100%; }
.btn-lg { min-height: 56px; font-size: var(--fs-md); padding: 0 var(--sp-6); }

/* حالة التحميل داخل الزر */
.btn[aria-busy="true"] { position: relative; color: transparent !important; }
.btn[aria-busy="true"]::after {
  content: ""; position: absolute; width: 18px; height: 18px;
  border: 2px solid rgba(255,255,255,.6); border-top-color: #fff; border-radius: 50%;
  animation: spin .7s linear infinite;
}
.btn-secondary[aria-busy="true"]::after, .btn-ghost[aria-busy="true"]::after {
  border-color: var(--line); border-top-color: var(--primary);
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------------------------------------------------------- Forms */
.field { display: grid; gap: var(--sp-1); margin-bottom: var(--sp-4); align-content: start; }
[data-form-alert] { margin-bottom: var(--sp-4); }
.field label { font-size: var(--fs-sm); font-weight: 600; }
.field .hint { font-size: var(--fs-xs); color: var(--muted); }
.input, select.input, textarea.input {
  width: 100%; min-height: 44px; padding: var(--sp-2) var(--sp-3);
  font: inherit; color: var(--ink);
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-sm);
}
textarea.input { min-height: 88px; resize: vertical; }
.input:focus { border-color: var(--primary); outline: none; box-shadow: 0 0 0 3px var(--primary-soft); }
.input[aria-invalid="true"] { border-color: var(--danger); }
.input-ltr { direction: ltr; text-align: right; }
.field-error { font-size: var(--fs-xs); color: var(--danger); }
.field-error:empty { display: none; }
.form-row { display: grid; gap: var(--sp-4); grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); margin-bottom: var(--sp-4); }
.form-row > .field { margin-bottom: 0; }
.form-actions { display: flex; gap: var(--sp-3); justify-content: flex-start; flex-wrap: wrap; margin-top: var(--sp-2); }

/* ---------------------------------------------------------------- Alerts & notices */
.alert {
  padding: var(--sp-3) var(--sp-4); border-radius: var(--r-sm);
  border-inline-start: 4px solid currentColor; font-size: var(--fs-sm);
  background: var(--surface-2);
}
.alert-info { color: var(--primary); background: var(--primary-soft); }
.alert-success { color: var(--success); background: var(--success-soft); }
.alert-warn { color: var(--warn); background: var(--warn-soft); }
.alert-danger { color: var(--danger); background: var(--danger-soft); }
.alert strong { display: block; margin-bottom: 2px; }
.alert > * { color: var(--ink); }
.alert > strong { color: inherit; }

/* شريط انقطاع الإنترنت */
.offline-bar {
  position: fixed; inset-inline: 0; top: 0; z-index: 100;
  padding: var(--sp-2) var(--sp-4); text-align: center; font-size: var(--fs-sm); font-weight: 600;
  background: var(--danger); color: #fff;
}

/* ---------------------------------------------------------------- Badges */
.badge {
  display: inline-block; padding: 2px 10px; border-radius: 999px;
  font-size: var(--fs-xs); font-weight: 600; white-space: nowrap;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--line);
}
.badge-success { background: var(--success-soft); color: var(--success); border-color: transparent; }
.badge-warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge-danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge-primary { background: var(--primary-soft); color: var(--primary); border-color: transparent; }

/* ---------------------------------------------------------------- Panels & tables */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-md); }
.panel-head {
  display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5); border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.panel-body { padding: var(--sp-5); }

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.data { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
table.data th, table.data td { padding: var(--sp-3) var(--sp-4); text-align: right; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.data th { font-weight: 600; color: var(--muted); background: var(--surface-2); white-space: nowrap; }
table.data tbody tr:last-child td { border-bottom: 0; }
table.data tr.row-muted td { color: var(--muted); background: var(--surface-2); }
table.data .actions { display: flex; gap: var(--sp-2); justify-content: flex-end; }

/* على الموبايل: الجدول يتحول إلى بطاقات */
@media (max-width: 720px) {
  table.data.stack thead { display: none; }
  table.data.stack tr { display: block; padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--line); }
  table.data.stack td { display: flex; justify-content: space-between; gap: var(--sp-3); padding: var(--sp-1) 0; border: 0; }
  table.data.stack td::before { content: attr(data-label); color: var(--muted); font-weight: 600; }
  table.data.stack td.actions { justify-content: flex-start; padding-top: var(--sp-2); }
  table.data.stack td.actions::before { content: none; }
}

/* حالات: تحميل / فارغ / خطأ */
.state { padding: var(--sp-7) var(--sp-5); text-align: center; display: grid; gap: var(--sp-2); justify-items: center; }
.state h3 { font-size: var(--fs-md); }
.state p { color: var(--muted); font-size: var(--fs-sm); max-width: 44ch; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }

.pagination { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); padding: var(--sp-3) var(--sp-5); border-top: 1px solid var(--line); font-size: var(--fs-sm); }
.pagination .btns { display: flex; gap: var(--sp-2); }

/* ---------------------------------------------------------------- Dialog */
dialog.dialog {
  width: min(520px, calc(100vw - 32px)); max-height: calc(100dvh - 32px);
  padding: 0; border: 1px solid var(--line); border-radius: var(--r-lg);
  color: var(--ink); background: var(--surface);
}
dialog.dialog::backdrop { background: rgba(20, 30, 30, .45); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; padding: var(--sp-4) var(--sp-5); border-bottom: 1px solid var(--line); }
.dialog-body { padding: var(--sp-5); overflow-y: auto; }
.dialog-foot { display: flex; gap: var(--sp-3); padding: var(--sp-4) var(--sp-5); border-top: 1px solid var(--line); background: var(--surface-2); }
.icon-btn { width: 36px; height: 36px; display: inline-grid; place-items: center; border: 0; background: transparent; border-radius: var(--r-sm); cursor: pointer; color: var(--muted); font-size: 1.3rem; }
.icon-btn:hover { background: var(--surface-2); }

/* ---------------------------------------------------------------- Toasts */
.toast-stack { position: fixed; bottom: var(--sp-5); inset-inline-start: var(--sp-5); z-index: 200; display: grid; gap: var(--sp-2); max-width: min(380px, calc(100vw - 32px)); }
.toast { padding: var(--sp-3) var(--sp-4); border-radius: var(--r-sm); font-size: var(--fs-sm); font-weight: 500; color: #fff; background: var(--ink); animation: toast-in .2s ease-out; }
.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ---------------------------------------------------------------- Auth pages */
.auth-shell { min-height: 100dvh; display: grid; place-items: center; padding: var(--sp-5); }
.auth-card { width: min(420px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: var(--sp-6); }
.auth-card .brand { display: flex; align-items: center; gap: var(--sp-3); margin-bottom: var(--sp-5); }
.auth-card h1 { margin-bottom: var(--sp-1); }
.auth-card .sub { color: var(--muted); font-size: var(--fs-sm); margin-bottom: var(--sp-5); }
.auth-links { display: flex; justify-content: space-between; margin-top: var(--sp-4); font-size: var(--fs-sm); }
.link-btn { font: inherit; color: var(--primary); background: none; border: 0; padding: 0; cursor: pointer; text-decoration: underline; }

/* علامة النظام: مربع بداخله علامة صح */
.mark { width: 40px; height: 40px; border-radius: var(--r-md); background: var(--primary); display: grid; place-items: center; flex: none; }
.mark svg { width: 22px; height: 22px; }

/* صفحة البداية */
.chooser { display: grid; gap: var(--sp-3); }
.chooser a { display: flex; justify-content: space-between; align-items: center; gap: var(--sp-3); padding: var(--sp-4) var(--sp-5); border: 1px solid var(--line); border-radius: var(--r-md); text-decoration: none; color: var(--ink); }
.chooser a:hover { border-color: var(--primary); background: var(--primary-soft); }
.chooser strong { display: block; }
.chooser span { font-size: var(--fs-sm); color: var(--muted); }

/* إشعار الإعداد (عند عدم ربط Supabase) */
.setup-notice { margin: 0; }
.auth-card .setup-notice { margin-bottom: var(--sp-5); }

/* ---------------------------------------------------------------- Admin layout */
.admin-layout { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100dvh; }
.sidebar { background: var(--surface); border-inline-end: 1px solid var(--line); padding: var(--sp-5) var(--sp-4); display: flex; flex-direction: column; gap: var(--sp-5); position: sticky; top: 0; height: 100dvh; }
.sidebar .brand { display: flex; align-items: center; gap: var(--sp-3); padding: 0 var(--sp-2); }
.sidebar .brand strong { display: block; font-size: var(--fs-sm); }
.sidebar .brand span { display: block; font-size: var(--fs-xs); color: var(--muted); }
.nav { display: grid; gap: 2px; }
.nav a, .nav button {
  display: flex; align-items: center; gap: var(--sp-3); min-height: 42px; padding: 0 var(--sp-3);
  border-radius: var(--r-sm); color: var(--ink); text-decoration: none; font: inherit; font-size: var(--fs-sm);
  background: none; border: 0; cursor: pointer; width: 100%; text-align: right;
}
.nav a:hover, .nav button:hover { background: var(--surface-2); }
.nav a[aria-current="page"] { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.nav svg { width: 18px; height: 18px; flex: none; }
.sidebar .nav-bottom { margin-top: auto; }

.main { min-width: 0; }
.topbar { display: none; }
.content { padding: var(--sp-6); max-width: 1200px; display: grid; gap: var(--sp-5); }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: var(--sp-4); flex-wrap: wrap; }
.page-head p { color: var(--muted); font-size: var(--fs-sm); margin-top: var(--sp-1); }

@media (max-width: 960px) {
  .admin-layout { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; width: var(--sidebar-w); z-index: 50; transform: translateX(100%); transition: transform .2s; }
  .sidebar.open { transform: none; }
  .scrim { position: fixed; inset: 0; background: rgba(20,30,30,.4); z-index: 40; }
  .topbar { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); padding: var(--sp-3) var(--sp-4); background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 30; }
  .content { padding: var(--sp-4); }
}

/* إحصائيات */
.stats { display: grid; gap: var(--sp-3); grid-template-columns: repeat(4, 1fr); }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-md); padding: var(--sp-4) var(--sp-5); }
.stat .label { font-size: var(--fs-sm); color: var(--muted); }
.stat .value { font-size: var(--fs-2xl); font-weight: 600; line-height: 1.2; margin-top: var(--sp-1); }
.stat.lead { background: var(--primary); border-color: var(--primary); color: #fff; }
.stat.lead .label { color: rgba(255,255,255,.8); }
@media (max-width: 900px) { .stats { grid-template-columns: repeat(2, 1fr); } }

.filters { display: grid; gap: var(--sp-3); grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); align-items: end; padding: var(--sp-4) var(--sp-5); border-bottom: 1px solid var(--line); }
.filters .field { margin: 0; }

/* QR في الإعدادات */
.qr-box { display: grid; grid-template-columns: auto 1fr; gap: var(--sp-5); align-items: start; }
.qr-frame { width: 200px; height: 200px; border: 1px solid var(--line); border-radius: var(--r-md); display: grid; place-items: center; background: #fff; padding: var(--sp-3); }
.qr-frame img, .qr-frame canvas, .qr-frame svg { width: 100%; height: auto; }
@media (max-width: 640px) { .qr-box { grid-template-columns: 1fr; } }
.copy-row { display: flex; gap: var(--sp-2); }
.copy-row .input { font-size: var(--fs-xs); }

/* ---------------------------------------------------------------- Employee portal */
.emp-shell { min-height: 100dvh; display: flex; flex-direction: column; max-width: 560px; margin: 0 auto; background: var(--bg); }
.emp-header { display: flex; align-items: center; justify-content: space-between; padding: var(--sp-4); }
.emp-header .who { display: flex; align-items: center; gap: var(--sp-3); }
.emp-main { flex: 1; padding: 0 var(--sp-4) calc(84px + env(safe-area-inset-bottom)); display: grid; gap: var(--sp-4); align-content: start; }
.bottom-nav { position: fixed; bottom: 0; inset-inline: 0; background: var(--surface); border-top: 1px solid var(--line); display: flex; justify-content: center; padding-bottom: env(safe-area-inset-bottom); z-index: 20; }
.bottom-nav .inner { display: grid; grid-template-columns: repeat(3, 1fr); width: min(560px, 100%); }
.bottom-nav a { display: grid; justify-items: center; gap: 2px; padding: var(--sp-2) 0; font-size: var(--fs-xs); color: var(--muted); text-decoration: none; }
.bottom-nav a svg { width: 22px; height: 22px; }
.bottom-nav a[aria-current="page"] { color: var(--primary); font-weight: 600; }

/* بطاقة الحالة: العنصر الأبرز في واجهة الموظف */
.status-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); overflow: hidden; }
.status-band { padding: var(--sp-2) var(--sp-5); font-size: var(--fs-sm); font-weight: 600; background: var(--surface-2); color: var(--muted); }
.status-card[data-state="checked_in"] .status-band { background: var(--success); color: #fff; }
.status-card[data-state="day_finished"] .status-band { background: var(--primary); color: #fff; }
.status-card[data-state="incomplete"] .status-band { background: var(--warn-soft); color: var(--warn); }
.status-body { padding: var(--sp-5); display: grid; gap: var(--sp-1); }
.clock { font-size: 3.2rem; font-weight: 600; line-height: 1.1; letter-spacing: -0.01em; }
.clock-date { color: var(--muted); }
.status-detail { margin-top: var(--sp-3); padding-top: var(--sp-3); border-top: 1px dashed var(--line); font-size: var(--fs-sm); }

.scan-cta { min-height: 64px; font-size: var(--fs-lg); border-radius: var(--r-md); }

/* الماسح */
.steps { display: flex; gap: var(--sp-2); font-size: var(--fs-xs); color: var(--muted); }
.steps li { flex: 1; list-style: none; padding-top: var(--sp-2); border-top: 3px solid var(--line); }
.steps li[aria-current="step"] { border-color: var(--primary); color: var(--primary); font-weight: 600; }
.steps li.done { border-color: var(--success); }
.steps { padding: 0; margin: 0; }
#qr-reader { width: 100%; border-radius: var(--r-md); overflow: hidden; background: #000; min-height: 260px; }
#qr-reader video { border-radius: var(--r-md); }
.pin-input { font-size: 2rem; text-align: center; letter-spacing: .5em; direction: ltr; padding-inline-start: .5em; }
.result { text-align: center; padding: var(--sp-6) var(--sp-5); display: grid; gap: var(--sp-3); justify-items: center; }
.result-icon { width: 72px; height: 72px; border-radius: 50%; display: grid; place-items: center; }
.result-icon svg { width: 36px; height: 36px; }
.result.ok .result-icon { background: var(--success-soft); color: var(--success); }
.result.fail .result-icon { background: var(--danger-soft); color: var(--danger); }
.result .when { font-size: var(--fs-2xl); font-weight: 600; }

/* سجل الموظف كبطاقات */
.day-list { display: grid; gap: var(--sp-2); }
.day-item { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-md); padding: var(--sp-3) var(--sp-4); display: grid; grid-template-columns: 1fr auto; gap: var(--sp-1) var(--sp-3); }
.day-item .date { font-weight: 600; }
.day-item .times { font-size: var(--fs-sm); color: var(--muted); }
.day-item .dur { grid-row: span 2; align-self: center; font-weight: 600; }

/* ---------------------------------------------------------------- Kiosk */
.kiosk { min-height: 100dvh; display: grid; grid-template-rows: auto 1fr auto; background: var(--surface); }
.kiosk-head { display: flex; justify-content: space-between; align-items: center; padding: var(--sp-5) var(--sp-6); border-bottom: 1px solid var(--line); }
.kiosk-head h1 { font-size: var(--fs-xl); }
.kiosk-body { display: grid; place-items: center; padding: var(--sp-5); text-align: center; gap: var(--sp-4); }
.kiosk-qr { width: min(62vmin, 460px); aspect-ratio: 1; padding: var(--sp-4); border: 2px solid var(--ink); border-radius: var(--r-lg); background: #fff; display: grid; place-items: center; }
.kiosk-qr img, .kiosk-qr svg { width: 100%; height: 100%; image-rendering: pixelated; }
.kiosk-instr { font-size: var(--fs-lg); max-width: 32ch; }
.kiosk-foot { display: flex; justify-content: space-between; align-items: center; gap: var(--sp-4); padding: var(--sp-4) var(--sp-6); border-top: 1px solid var(--line); font-size: var(--fs-sm); color: var(--muted); }
.kiosk-clock { font-size: var(--fs-2xl); font-weight: 600; color: var(--ink); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--muted); margin-inline-end: var(--sp-2); vertical-align: middle; }
.dot.ok { background: var(--success); }
.dot.bad { background: var(--danger); }
.dot.warn { background: #d49a2a; }

/* ---------------------------------------------------------------- Print (طباعة QR) */
@media print {
  body * { visibility: hidden; }
  .printable, .printable * { visibility: visible; }
  .printable { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; }
  .printable .qr-frame { width: 12cm; height: 12cm; border: 0; }
}
