:root {
  --green-900: #064e3b;
  --green-800: #08654f;
  --green-700: #08785f;
  --green-600: #0a8c6c;
  --green-100: #dff4ec;
  --green-50: #f0faf6;
  --ink: #122033;
  --muted: #627083;
  --line: #dce6e2;
  --surface: #ffffff;
  --surface-soft: #f7faf9;
  --danger: #be123c;
  --danger-soft: #fff1f2;
  --shadow-sm: 0 4px 14px rgba(14, 57, 46, 0.07);
  --shadow-lg: 0 22px 60px rgba(10, 44, 36, 0.2);
  --radius-sm: 12px;
  --radius-md: 18px;
  --radius-lg: 26px;
  --focus: 0 0 0 4px rgba(10, 140, 108, 0.18);
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  min-width: 300px;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 6%, rgba(10, 140, 108, 0.09), transparent 26rem),
    linear-gradient(180deg, #f4faf7 0, #fbfdfc 38rem, #f7faf9 100%);
  font-family: "Tajawal", "Segoe UI", Tahoma, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.55;
}

button, input, select, textarea { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }
button:disabled, input:disabled, select:disabled, textarea:disabled { cursor: not-allowed; opacity: .62; }
[hidden] { display: none !important; }

.auth-screen {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 15% 12%, rgba(10, 140, 108, .15), transparent 25rem),
    linear-gradient(145deg, #eff9f5, #fbfdfc 58%, #e9f6f1);
}
.auth-card { width: min(100%, 480px); padding: clamp(24px, 5vw, 38px); background: #fff; border: 1px solid var(--line); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); }
.auth-card-center { text-align: center; }
.auth-heading { display: flex; align-items: center; gap: 16px; margin-bottom: 28px; }
.auth-heading h1 { margin-bottom: 5px; }
.auth-heading p:last-child { margin-bottom: 0; color: var(--muted); }
.auth-brand-mark { width: 58px; height: 58px; flex: 0 0 auto; display: grid; place-items: center; margin: 0 auto 15px; color: #fff; background: var(--green-700); border-radius: 18px; box-shadow: 0 9px 22px rgba(8, 120, 95, .22); }
.auth-heading .auth-brand-mark { margin: 0; }
.auth-brand-mark svg { width: 29px; height: 29px; }
.auth-card .field-group + .field-group { margin-top: 17px; }
.auth-submit { width: 100%; margin-top: 8px; }
.auth-error { min-height: 24px; margin: 10px 3px 0; color: var(--danger); font-size: .9rem; font-weight: 600; }
.auth-status { min-height: 22px; margin: 10px 0 0; color: var(--green-800); text-align: center; font-size: .88rem; }
.password-toggle { position: absolute; top: 50%; left: 8px; width: 42px; height: 42px; display: grid; place-items: center; color: var(--muted); background: transparent; border: 0; border-radius: 10px; transform: translateY(-50%); cursor: pointer; }
.password-toggle:hover, .password-toggle:focus-visible { color: var(--green-800); background: var(--green-50); outline: none; }
.password-toggle svg { width: 20px; }
.password-wrap input { padding-left: 54px; }
.loading-spinner { width: 38px; height: 38px; margin: 0 auto 18px; border: 4px solid var(--green-100); border-top-color: var(--green-700); border-radius: 50%; animation: spin .8s linear infinite; }
.header-with-actions { justify-content: space-between; }
.header-brand { display: flex; align-items: center; gap: 14px; }
.migration-message { display: flex; align-items: center; gap: 14px; padding: 16px; background: var(--green-50); border: 1px solid #d2e9e0; border-radius: 14px; }
.migration-message p { margin: 0; }
.inline-state { min-height: 180px; display: grid; place-items: center; padding: 28px; text-align: center; color: var(--muted); background: var(--surface-soft); border: 1.5px dashed #ccdad5; border-radius: var(--radius-md); }
.inline-state .loading-spinner { width: 32px; height: 32px; margin-bottom: 12px; }
.inline-state .btn { margin-top: 14px; }

.skip-link {
  position: fixed;
  z-index: 2000;
  top: 12px;
  right: 12px;
  padding: 10px 16px;
  color: #fff;
  background: var(--green-900);
  border-radius: 10px;
  transform: translateY(-150%);
}
.skip-link:focus { transform: translateY(0); }

.site-header { background: rgba(255, 255, 255, 0.86); border-bottom: 1px solid rgba(8, 101, 79, 0.12); backdrop-filter: blur(12px); }
.header-accent { height: 8px; background: linear-gradient(90deg, var(--green-900), var(--green-600)); }
.header-inner { max-width: 1120px; margin: auto; padding: 24px 24px 22px; display: flex; align-items: center; gap: 14px; }
.brand-mark { width: 54px; height: 54px; display: grid; place-items: center; color: #fff; background: var(--green-700); border-radius: 17px; box-shadow: 0 8px 20px rgba(8, 120, 95, .22); }
.brand-mark svg { width: 28px; height: 28px; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(1.65rem, 3vw, 2.25rem); line-height: 1.25; }
h2 { margin-bottom: 0; font-size: clamp(1.25rem, 2vw, 1.55rem); }
.eyebrow { margin-bottom: 3px; color: var(--green-700); font-size: .84rem; font-weight: 700; letter-spacing: .02em; }

.page-shell { width: min(1120px, calc(100% - 32px)); margin: 34px auto 60px; }
.control-card, .visits-section { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-lg); box-shadow: var(--shadow-sm); }
.control-card { padding: clamp(20px, 4vw, 34px); }
.section-heading, .visits-heading { display: flex; justify-content: space-between; align-items: center; gap: 16px; }
.section-heading { margin-bottom: 26px; }
.save-status { display: inline-flex; align-items: center; gap: 7px; color: var(--green-800); background: var(--green-50); border: 1px solid #cfe9df; border-radius: 999px; padding: 8px 12px; font-size: .87rem; font-weight: 600; }
.save-status svg { width: 18px; }

.fields-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px; }
.field-wide { grid-column: 1 / -1; }
.field-group { min-width: 0; }
.field-group label { display: block; margin: 0 4px 7px; color: #405064; font-size: .9rem; font-weight: 700; }
.input-wrap { position: relative; }
.input-wrap > svg:first-child { position: absolute; z-index: 1; top: 50%; right: 17px; width: 21px; height: 21px; color: var(--green-700); transform: translateY(-50%); pointer-events: none; }
input, select, textarea { width: 100%; color: var(--ink); background: var(--surface-soft); border: 1.5px solid #d5e2dd; border-radius: var(--radius-sm); outline: none; transition: border-color .18s, box-shadow .18s, background .18s; }
input, select { min-height: 54px; padding: 12px 50px 12px 16px; }
textarea { min-height: 104px; padding: 14px 50px 14px 16px; resize: vertical; }
.english-numbers {
  direction: ltr;
  unicode-bidi: isolate;
  font-family: Arial, Tahoma, sans-serif;
  font-variant-numeric: tabular-nums;
}
input[type="date"] {
  direction: ltr;
  text-align: left;
  font-family: Arial, sans-serif;
  font-variant-numeric: tabular-nums;
}
.date-picker-wrap { min-height: 54px; border-radius: var(--radius-sm); }
.date-display { pointer-events: none; text-align: left; padding-right: 50px; }
.date-native-picker {
  position: absolute;
  z-index: 2;
  inset: 0;
  min-height: 100%;
  opacity: 0;
  cursor: pointer;
}
.date-picker-wrap:focus-within .date-display {
  border-color: var(--green-600);
  background: #fff;
  box-shadow: var(--focus);
}
.textarea-wrap > svg:first-child { top: 18px; transform: none; }
select { appearance: none; padding-left: 48px; cursor: pointer; }
.select-chevron { position: absolute; top: 50%; left: 17px; width: 20px; color: var(--muted); transform: translateY(-50%); pointer-events: none; }
input:hover, select:hover, textarea:hover { border-color: #9fcbbd; background: #fff; }
input:focus, select:focus, textarea:focus { border-color: var(--green-600); background: #fff; box-shadow: var(--focus); }
.required { color: var(--danger); }
.optional { color: var(--muted); font-size: .78rem; font-weight: 500; }
.field-error { min-height: 20px; margin: 5px 5px 0; color: var(--danger); font-size: .83rem; font-weight: 600; }
.input-error { border-color: var(--danger) !important; box-shadow: 0 0 0 4px rgba(190, 18, 60, .1) !important; }

.actions-bar { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 26px; padding-top: 24px; border-top: 1px solid var(--line); }
.btn { min-height: 50px; display: inline-flex; justify-content: center; align-items: center; gap: 9px; padding: 11px 20px; border: 1.5px solid transparent; border-radius: var(--radius-sm); font-weight: 700; cursor: pointer; transition: transform .16s, box-shadow .16s, background .16s, border-color .16s; }
.btn svg { width: 20px; height: 20px; }
.btn:hover { transform: translateY(-2px); }
.btn:active { transform: translateY(0); }
.btn:focus-visible, .icon-btn:focus-visible, .history-card:focus-visible { outline: none; box-shadow: var(--focus); }
.btn-primary { color: #fff; background: var(--green-700); box-shadow: 0 7px 18px rgba(8, 120, 95, .18); }
.btn-primary:hover { background: var(--green-800); box-shadow: 0 9px 22px rgba(8, 120, 95, .25); }
.btn-secondary { color: var(--green-800); background: #fff; border-color: var(--green-700); }
.btn-secondary:hover { background: var(--green-50); }
.btn-ghost { color: #465568; background: #f3f6f5; border-color: #e3e9e7; }
.btn-danger { color: var(--danger); background: var(--danger-soft); border-color: #fecdd3; }
.btn-small { min-height: 40px; padding: 8px 13px; font-size: .9rem; }

.visits-section { margin-top: 24px; padding: clamp(20px, 4vw, 30px); }
.visits-heading { margin-bottom: 20px; }
.visit-count { min-width: 84px; display: flex; align-items: baseline; justify-content: center; gap: 5px; padding: 9px 13px; color: var(--green-800); background: var(--green-50); border-radius: 14px; font-weight: 700; }
.visit-count span:first-child { font-size: 1.4rem; }
.visits-list { display: grid; gap: 13px; }
.empty-state { min-height: 180px; display: grid; place-items: center; padding: 28px; text-align: center; color: var(--muted); background: var(--surface-soft); border: 1.5px dashed #ccdad5; border-radius: var(--radius-md); }
.empty-state-icon { width: 52px; height: 52px; margin: 0 auto 12px; display: grid; place-items: center; color: var(--green-700); background: var(--green-100); border-radius: 16px; }
.empty-state-icon svg { width: 26px; }
.empty-state p { margin: 0; }
.visit-card { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 16px; padding: 17px; background: #fff; border: 1px solid var(--line); border-radius: var(--radius-md); transition: transform .17s, box-shadow .17s, border-color .17s; }
.visit-card:hover { transform: translateY(-2px); border-color: #b9d9ce; box-shadow: var(--shadow-sm); }
.serial { width: 42px; height: 42px; display: grid; place-items: center; color: #fff; background: var(--green-700); border-radius: 13px; font-weight: 800; }
.visit-main { min-width: 0; }
.visit-title-row { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
.visit-number { margin: 0; font-size: 1.05rem; overflow-wrap: anywhere; }
.type-badge { padding: 4px 9px; color: var(--green-800); background: var(--green-50); border-radius: 999px; font-size: .78rem; font-weight: 700; }
.visit-meta { margin: 5px 0 0; display: flex; flex-wrap: wrap; gap: 8px 16px; color: var(--muted); font-size: .87rem; }
.visit-meta span { display: inline-flex; align-items: center; gap: 5px; }
.visit-meta svg { width: 15px; }
.visit-note { overflow-wrap: anywhere; }
.visit-actions { display: flex; gap: 7px; }

.modal[hidden] { display: none; }
.modal { position: fixed; z-index: 1000; inset: 0; display: grid; place-items: center; padding: 20px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(5, 26, 21, .64); backdrop-filter: blur(5px); animation: fadeIn .18s ease-out; }
.modal-panel { position: relative; width: min(100%, 760px); max-height: calc(100dvh - 40px); overflow-y: auto; overscroll-behavior: contain; padding: clamp(20px, 4vw, 30px); background: #fff; border: 1px solid rgba(255,255,255,.5); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); animation: modalIn .22s ease-out; }
.modal-panel-small { width: min(100%, 560px); }
.modal-panel-report { width: min(100%, 920px); }
.modal-panel-history { width: min(100%, 850px); }
.modal-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 24px; padding-bottom: 17px; border-bottom: 1px solid var(--line); }
.icon-btn { width: 44px; height: 44px; flex: 0 0 auto; display: grid; place-items: center; color: #536173; background: #f3f6f5; border: 1px solid #dfe7e4; border-radius: 12px; cursor: pointer; transition: color .17s, background .17s; }
.icon-btn:hover { color: var(--danger); background: var(--danger-soft); }
.modal-actions, .report-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 24px; }
body.modal-open { overflow: hidden; }

.report-sheet { padding: clamp(18px, 4vw, 34px); color: #172234; background: #fff; border: 1px solid var(--line); border-radius: var(--radius-md); }
.report-brand { padding-bottom: 18px; text-align: center; border-bottom: 3px solid var(--green-700); }
.report-brand-icon { width: 48px; height: 48px; display: grid; place-items: center; margin: 0 auto 9px; color: #fff; background: var(--green-700); border-radius: 15px; }
.report-brand h2 { font-size: 1.65rem; }
.report-brand p { margin: 3px 0 0; color: var(--muted); }
.report-info { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin: 22px 0; }
.info-item { padding: 12px 14px; background: var(--surface-soft); border: 1px solid var(--line); border-radius: 11px; }
.info-item span { display: block; color: var(--muted); font-size: .8rem; }
.info-item strong { display: block; margin-top: 2px; }
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat-card { padding: 15px 10px; text-align: center; background: var(--green-50); border: 1px solid #d4eae2; border-radius: 12px; }
.stat-card strong { display: block; color: var(--green-800); font-size: 1.65rem; line-height: 1.2; }
.stat-card span { color: #455567; font-size: .83rem; }
.stat-card.total { color: #fff; background: var(--green-700); border-color: var(--green-700); }
.stat-card.total strong, .stat-card.total span { color: #fff; }
.report-details { margin-top: 25px; }
.report-details h3 { margin-bottom: 12px; }
.report-table-wrap { overflow-x: auto; }
.report-table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.report-table th, .report-table td { padding: 11px 10px; text-align: right; border: 1px solid var(--line); vertical-align: top; overflow-wrap: anywhere; }
.report-table th { color: var(--green-900); background: var(--green-50); }
.report-empty { padding: 24px; color: var(--muted); text-align: center; background: var(--surface-soft); border-radius: 12px; }

.history-filters { display: grid; grid-template-columns: 1.3fr 1fr; gap: 14px; }
.results-count { margin: 18px 3px 9px; color: var(--muted); font-size: .9rem; }
.history-list { display: grid; gap: 10px; }
.history-card { width: 100%; display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 14px; padding: 15px; color: inherit; text-align: right; background: #fff; border: 1px solid var(--line); border-radius: 14px; cursor: pointer; transition: transform .17s, box-shadow .17s, border-color .17s; }
.history-card:hover { transform: translateY(-2px); border-color: var(--green-600); box-shadow: var(--shadow-sm); }
.history-icon { width: 45px; height: 45px; display: grid; place-items: center; color: var(--green-700); background: var(--green-50); border-radius: 12px; }
.history-card h3 { margin: 0; font-size: 1rem; }
.history-card p { margin: 3px 0 0; color: var(--muted); font-size: .86rem; }
.history-total { min-width: 70px; text-align: center; color: var(--green-800); font-weight: 800; }
.history-total small { display: block; font-size: .72rem; font-weight: 500; }

.toast-region { position: fixed; z-index: 1500; right: 20px; bottom: 20px; display: grid; gap: 10px; pointer-events: none; }
.toast { max-width: min(380px, calc(100vw - 40px)); display: flex; align-items: center; gap: 10px; padding: 13px 16px; color: #fff; background: #163b32; border-radius: 13px; box-shadow: var(--shadow-lg); animation: toastIn .22s ease-out; }
.toast.error { background: #9f1239; }
.toast svg { width: 20px; flex: 0 0 auto; }

.print-area { display: none; }

@keyframes fadeIn { from { opacity: 0; } }
@keyframes modalIn { from { opacity: 0; transform: translateY(16px) scale(.985); } }
@keyframes toastIn { from { opacity: 0; transform: translateY(12px); } }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 720px) {
  .auth-screen { padding: 12px; }
  .auth-card { border-radius: 21px; }
  .header-with-actions { align-items: center; }
  .header-with-actions #logoutBtn span { display: none; }
  .header-with-actions #logoutBtn { width: 46px; padding: 8px; }
  .header-inner { padding: 17px 16px; }
  .brand-mark { width: 47px; height: 47px; border-radius: 14px; }
  .page-shell { width: min(100% - 20px, 1120px); margin-top: 18px; }
  .control-card, .visits-section { border-radius: 20px; }
  .section-heading { align-items: flex-start; flex-direction: column; }
  .fields-grid, .history-filters { grid-template-columns: 1fr; gap: 15px; }
  .field-wide { grid-column: auto; }
  input, select { min-height: 57px; }
  .actions-bar { display: grid; grid-template-columns: 1fr 1fr; }
  .actions-bar .btn:first-child { grid-column: 1 / -1; }
  .visit-card { grid-template-columns: auto 1fr; align-items: start; }
  .visit-actions { grid-column: 1 / -1; justify-content: stretch; }
  .visit-actions .btn { flex: 1; }
  .modal { padding: 10px; align-items: end; }
  .modal-panel { width: 100%; max-height: calc(100dvh - 20px); padding: 20px 16px; border-radius: 22px; }
  .modal-actions .btn, .report-actions .btn { flex: 1 1 calc(50% - 10px); }
  .report-info, .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .report-sheet { padding: 16px; }
}

@media (max-width: 430px) {
  .auth-heading { align-items: flex-start; }
  .auth-heading .auth-brand-mark { width: 50px; height: 50px; border-radius: 15px; }
  .header-inner .eyebrow { display: none; }
  .save-status { font-size: .8rem; }
  .actions-bar { grid-template-columns: 1fr; }
  .actions-bar .btn:first-child { grid-column: auto; }
  .visits-heading { align-items: flex-start; }
  .visit-card { gap: 11px; padding: 14px; }
  .serial { width: 37px; height: 37px; }
  .report-info, .stats-grid { grid-template-columns: 1fr; }
  .report-actions .btn, .modal-actions .btn { flex-basis: 100%; }
  .history-card { grid-template-columns: auto 1fr; }
  .history-total { grid-column: 2; text-align: right; }
  .toast-region { right: 10px; bottom: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

@page { size: A4 portrait; margin: 10mm; }

@media print {
  html,
  body {
    width: 100% !important;
    min-width: 0 !important;
    min-height: 0 !important;
    height: auto !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: visible !important;
    color: #111 !important;
    background: #fff !important;
  }

  body > *:not(#printArea) { display: none !important; }

  #printArea {
    display: block !important;
    position: static !important;
    width: 100% !important;
    min-height: 0 !important;
    max-height: none !important;
    height: auto !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: visible !important;
    transform: none !important;
    direction: rtl;
    unicode-bidi: isolate;
    color: #111;
    background: #fff;
    font-family: "Tajawal", Arial, Tahoma, sans-serif;
    font-size: 10pt;
    line-height: 1.35;
  }

  .print-sheet { width: 100%; margin: 0; padding: 0; }
  .print-header { margin: 0 0 4mm; padding: 0 0 3mm; text-align: center; border-bottom: 1.5pt solid #08785f; }
  .print-header h1 { margin: 0; color: #111; font-size: 18pt; line-height: 1.25; }
  .print-header p { margin: 1.5mm 0 0; color: #444; font-size: 9pt; }

  .print-summary {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 2mm 6mm;
    margin: 0 0 4mm;
    padding: 3mm 0;
    border-bottom: .75pt solid #b9c9c3;
  }
  .print-info { display: flex; align-items: baseline; gap: 2mm; min-width: 0; }
  .print-info span { flex: 0 0 auto; color: #4b5563; font-size: 8.5pt; }
  .print-info strong { color: #111; font-size: 10pt; overflow-wrap: anywhere; }

  .print-stats { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 1.5mm; margin: 0 0 4mm; }
  .print-stat { padding: 2mm 1mm; text-align: center; border: .75pt solid #8fb9aa; border-radius: 2mm; }
  .print-stat strong { display: block; color: #075f4a; font-family: Arial, Tahoma, sans-serif; font-size: 14pt; line-height: 1; font-variant-numeric: tabular-nums; }
  .print-stat span { display: block; margin-top: 1mm; color: #222; font-size: 7.2pt; line-height: 1.2; }
  .print-stat-total { border-color: #08785f; border-width: 1.25pt; }

  .print-details { margin: 0; }
  .print-details h2 { margin: 0 0 2mm; font-size: 11.5pt; }
  .print-table { width: 100%; table-layout: fixed; border-collapse: collapse; font-size: 8.5pt; }
  .print-table thead { display: table-header-group; }
  .print-table th,
  .print-table td { padding: 1.8mm 2mm; text-align: right; vertical-align: top; border: .6pt solid #8d9d97; overflow-wrap: anywhere; }
  .print-table th { color: #064e3b; font-weight: 700; border-bottom: 1pt solid #08785f; }
  .print-table th:first-child,
  .print-table td:first-child { width: 8%; text-align: center; }
  .print-table th:nth-child(2),
  .print-table td:nth-child(2) { width: 24%; }
  .print-table th:nth-child(3),
  .print-table td:nth-child(3) { width: 27%; }
  .print-empty { margin: 0; padding: 6mm; text-align: center; border: .75pt solid #9eaaa6; }

  .print-summary,
  .print-stat,
  .print-info,
  .print-visit-row {
    break-inside: avoid;
    page-break-inside: avoid;
  }
}
