:root {
  --navy: #1c2a3a;
  --navy-dark: #131e2b;
  --gold: #c9973f;
  --gold-light: #e6c07e;
  --bg: #f4f6f8;
  --card: #ffffff;
  --text: #1f2937;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --green: #1e8e5a;
  --red: #c0392b;
  --amber: #b7791f;
  --radius: 10px;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app-shell { display: flex; min-height: 100vh; }
.app-shell.hidden { display: none; }

.login-screen {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: var(--navy);
}
.login-screen.hidden { display: none; }
.login-card {
  background: var(--card); border-radius: var(--radius); padding: 36px 32px;
  width: 100%; max-width: 340px; text-align: center; box-shadow: 0 10px 40px rgba(0,0,0,0.25);
}
.login-card h1 { font-size: 20px; margin: 0 0 6px; color: var(--navy); }
.login-card .muted { margin-bottom: 20px; }
.login-card form { text-align: left; }
.login-error {
  background: #fbe9e7; color: var(--red); font-size: 13px; padding: 8px 10px;
  border-radius: 7px; margin: 10px 0;
}
.login-error.hidden { display: none; }

.logout-link { margin-top: 12px; border-top: 1px solid rgba(255,255,255,0.1); padding-top: 14px !important; }

.sidebar {
  width: 220px;
  background: var(--navy);
  color: #fff;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 20px 18px; border-bottom: 1px solid rgba(255,255,255,0.1); }
.brand-mark {
  width: 36px; height: 36px; border-radius: 8px;
  background: linear-gradient(135deg, var(--gold-light), var(--gold));
  object-fit: cover; flex-shrink: 0;
}
.brand-name { font-weight: 600; font-size: 15px; line-height: 1.2; }
.nav { display: flex; flex-direction: column; padding: 12px 8px; gap: 2px; }
.nav a {
  color: rgba(255,255,255,0.75);
  text-decoration: none;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
}
.nav a:hover { background: rgba(255,255,255,0.06); color: #fff; }
.nav a.active { background: var(--gold); color: var(--navy-dark); }
.nav a.hidden { display: none; }

.lang-switch, .theme-switch { display: flex; gap: 6px; padding: 12px 16px; }
.lang-switch { margin-top: auto; }
.theme-switch { padding-top: 0; }
.lang-switch button, .theme-switch button {
  flex: 1; background: rgba(255,255,255,0.08); color: rgba(255,255,255,0.75); border: 1px solid rgba(255,255,255,0.15);
  border-radius: 6px; padding: 6px 0; font-size: 12px; font-weight: 700; cursor: pointer; width: auto;
}
.lang-switch button.active, .theme-switch button.active { background: var(--gold); color: var(--navy-dark); border-color: var(--gold); }
.login-card .lang-switch, .login-card .theme-switch { padding: 0; }
.login-card .lang-switch button, .login-card .theme-switch button { color: var(--text-muted); border-color: var(--border); }
.login-card .lang-switch button.active, .login-card .theme-switch button.active { background: var(--gold); color: var(--navy-dark); }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { padding: 22px 32px 8px; }
.topbar h1 { margin: 0; font-size: 22px; }
.view { padding: 16px 32px 40px; }

/* Cards / stats */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; }
.stat-label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 6px; }
.stat-value { font-size: 26px; font-weight: 700; color: var(--navy); }

.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; margin-bottom: 20px; }
.card h3 { margin-top: 0; }

.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; gap: 12px; flex-wrap: wrap; }
.toolbar .search { flex: 1; max-width: 320px; }
/* Several filters that belong together on one side of a toolbar, kept on a single row.
   nowrap + flex-shrink:0 are both needed: the toolbar is itself a flex row, so without them the
   group gets squeezed below its content width and folds onto a second line (and crushes the
   Clear button). Below the breakpoint there genuinely isn't room, so it wraps as a unit. */
.filter-group {
  display: flex; align-items: center; gap: 8px;
  flex-wrap: nowrap; flex-shrink: 0;
}
.filter-group > * { flex: 0 0 auto; }
.filter-group .filter-label { font-size: 13px; color: var(--text-muted); white-space: nowrap; }
.filter-group input[type="date"] { width: 150px; max-width: none; }
.filter-group .btn { white-space: nowrap; }

@media (max-width: 900px) {
  .filter-group { flex-wrap: wrap; flex-shrink: 1; }
}

/* Explanatory line under a form field — e.g. how a prefilled amount was arrived at. */
.form-hint { font-size: 12.5px; color: var(--text-muted); margin: 6px 0 0; }

.pagination { display: flex; justify-content: center; align-items: center; gap: 14px; margin-top: 16px; }
.pagination:empty { margin-top: 0; }

input, select, textarea {
  font-family: inherit; font-size: 14px;
  padding: 9px 11px; border: 1px solid var(--border); border-radius: 7px; background: #fff; color: var(--text);
  width: 100%;
}
label { font-size: 12px; font-weight: 600; color: var(--text-muted); display: block; margin-bottom: 5px; margin-top: 12px; }
label:first-child { margin-top: 0; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--navy); color: #fff; border: none; padding: 9px 16px;
  border-radius: 7px; font-size: 14px; font-weight: 600; cursor: pointer;
}
.btn:hover { background: var(--navy-dark); }
.btn-gold { background: var(--gold); color: var(--navy-dark); }
.btn-gold:hover { background: var(--gold-light); }
.btn-danger { background: #fff; color: var(--red); border: 1px solid var(--red); }
.btn-danger:hover { background: #fdecea; }
.btn-ghost { background: transparent; color: var(--navy); border: 1px solid var(--border); }
.btn-ghost:hover { background: #f0f2f4; }
.btn-sm { padding: 5px 10px; font-size: 12px; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
th { font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); font-weight: 600; }
tbody tr:hover { background: #fafbfc; }
.row-actions { display: flex; gap: 6px; }

.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 11px; font-weight: 700; text-transform: uppercase; }
.badge-green { background: #e5f5ec; color: var(--green); }
.badge-amber { background: #fdf1dd; color: var(--amber); }
.badge-red { background: #fbe9e7; color: var(--red); }
.badge-gray { background: #eef0f2; color: var(--text-muted); }

.empty-state { color: var(--text-muted); font-size: 14px; padding: 30px 0; text-align: center; }

/* Worker rating (average of customer ratings on their jobs). A gold five-star layer is clipped
   to the score's percentage over a grey five-star base, so 4.6 shows as a partly-filled fifth
   star rather than rounding up to a perfect score. direction:ltr because star ratings read
   left-to-right even in the Arabic UI, and the clip would otherwise fill from the wrong side. */
.rating-stars {
  position: relative; display: inline-block; direction: ltr;
  color: var(--border); letter-spacing: 1px; white-space: nowrap;
}
.rating-stars-fill {
  position: absolute; top: 0; left: 0; overflow: hidden;
  color: var(--gold); letter-spacing: 1px; white-space: nowrap;
}
.rating-count { color: var(--text-muted); font-size: 12px; margin-inline-start: 4px; white-space: nowrap; }

/* Loading spinner — shown while a page's API data is in flight. */
.loading-state {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; padding: 48px 0; color: var(--text-muted); font-size: 14px;
}
.spinner {
  width: 32px; height: 32px; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--gold);
  animation: spin 0.7s linear infinite;
}
.spinner-sm { width: 16px; height: 16px; border-width: 2px; }
@keyframes spin { to { transform: rotate(360deg); } }
/* Respect users who've asked the OS to reduce motion: keep the indicator, drop the spin. */
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2.5s; }
}

/* Error alerts — stacked toasts, used when an API call fails (most importantly when the
   server can't be reached at all, which otherwise looks like the page silently doing nothing). */
.toast-container {
  position: fixed; top: 16px; right: 16px; z-index: 1000;
  display: flex; flex-direction: column; gap: 10px;
  max-width: min(380px, calc(100vw - 32px));
}
html[dir="rtl"] .toast-container { right: auto; left: 16px; }
.toast {
  display: flex; align-items: flex-start; gap: 10px;
  background: var(--card); border: 1px solid var(--border); border-left: 4px solid var(--red);
  border-radius: var(--radius); padding: 12px 14px;
  box-shadow: 0 8px 28px rgba(0,0,0,0.18);
  animation: toast-in 0.18s ease-out;
}
html[dir="rtl"] .toast { border-left: 1px solid var(--border); border-right: 4px solid var(--red); }
.toast-text { flex: 1; font-size: 13px; line-height: 1.45; color: var(--text); word-break: break-word; }
.toast-title { display: block; font-weight: 700; margin-bottom: 2px; color: var(--red); }
.toast-close {
  background: none; border: none; cursor: pointer; padding: 0 2px;
  font-size: 18px; line-height: 1; color: var(--text-muted);
}
.toast-close:hover { color: var(--text); }
@keyframes toast-in { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }

/* Calendar */
.cal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.cal-header .cal-nav { display: flex; gap: 8px; align-items: center; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.cal-dow { text-align: center; font-size: 11px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; padding-bottom: 6px; }
.cal-day {
  background: var(--card); border: 1px solid var(--border); border-radius: 8px;
  min-height: 84px; padding: 8px; cursor: pointer; position: relative;
}
.cal-day:hover { border-color: var(--gold); }
.cal-day.today { border-color: var(--gold); box-shadow: inset 0 0 0 1px var(--gold); }
.cal-day.selected { background: var(--navy); border-color: var(--navy); }
.cal-day.selected .day-num { color: #fff; }
.cal-day.selected.today { border-color: var(--gold); box-shadow: inset 0 0 0 2px var(--gold); }
.cal-day.worked.selected { background: var(--red); border-color: var(--red); }
.cal-day.not-worked.selected { background: var(--green); border-color: var(--green); }
.cal-day.other-month { opacity: 0.4; }
.cal-day .day-num { font-size: 12px; font-weight: 700; color: var(--text-muted); }
.cal-day .job-count { margin-top: 6px; font-size: 11px; background: var(--navy); color: #fff; border-radius: 999px; padding: 2px 7px; display: inline-block; }
.cal-day.worked { background: #fbe9e7; border-color: var(--red); }
.cal-day.worked .day-num { color: var(--red); }
.cal-day.worked:hover { border-color: var(--red); }
.cal-day.not-worked { background: #e5f5ec; border-color: var(--green); cursor: default; }
.cal-day.not-worked .day-num { color: var(--green); }
.cal-day.not-worked:hover { border-color: var(--green); }

.legend-dot { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 4px; vertical-align: middle; }
.legend-red { background: var(--red); }
.quick-reasons { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.legend-green { background: var(--green); }

.day-panel-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 20px; }
@media (max-width: 900px) { .day-panel-grid { grid-template-columns: 1fr; } }
.employee-chip { display: inline-flex; align-items: center; gap: 6px; background: #f0f2f4; border-radius: 999px; padding: 4px 10px; font-size: 12px; margin: 3px 4px 3px 0; }
.employee-chip.unavailable { background: #fbe9e7; color: var(--red); }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(20,26,33,0.5);
  display: flex; align-items: flex-start; justify-content: center; padding: 40px 20px; overflow-y: auto; z-index: 50;
}
.modal-backdrop.hidden { display: none; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 520px; padding: 0; }
.modal-header { display: flex; justify-content: space-between; align-items: center; padding: 18px 22px; border-bottom: 1px solid var(--border); }
.modal-header h2 { margin: 0; font-size: 17px; }
.modal-body { padding: 20px 22px; }
.icon-btn { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--text-muted); line-height: 1; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.section-title { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-muted); margin: 18px 0 8px; }
.muted { color: var(--text-muted); font-size: 13px; }

/* Searchable combobox (client picker) */
.combobox { position: relative; }
.combobox-dropdown {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 20;
  background: var(--card); border: 1px solid var(--border); border-radius: 7px;
  max-height: 220px; overflow-y: auto; box-shadow: 0 6px 20px rgba(0,0,0,0.15);
}
.combobox-dropdown.hidden { display: none; }
.combobox-option { padding: 9px 12px; font-size: 14px; cursor: pointer; }
.combobox-option:hover { background: var(--bg); }
.combobox-empty { padding: 9px 12px; font-size: 13px; color: var(--text-muted); }
/* Sits at the top of the list, so the rule that separates it from the clients goes below it.
   Sticky keeps it reachable once the list is scrolled. */
.combobox-create {
  border-bottom: 1px solid var(--border); color: var(--gold); font-weight: 600;
  position: sticky; top: 0; background: var(--card); z-index: 1;
}
.combobox-inline-form { border: 1px solid var(--border); border-radius: 7px; padding: 12px; margin-top: 8px; background: var(--bg); }
.combobox-inline-form.hidden { display: none; }

/* ---------- RTL ---------- */
html[dir="rtl"] body { font-family: "Segoe UI", Tahoma, Arial, sans-serif; }
html[dir="rtl"] .login-card { text-align: right; }
html[dir="rtl"] .login-card form { text-align: right; }
html[dir="rtl"] .logout-link { border-top: 1px solid rgba(255,255,255,0.1); }
html[dir="rtl"] th, html[dir="rtl"] td { text-align: right; }
html[dir="rtl"] .legend-dot { margin-right: 0; margin-left: 4px; }
html[dir="rtl"] .cal-header .cal-nav button:first-child,
html[dir="rtl"] .cal-header .cal-nav button:last-child { }
html[dir="rtl"] .employee-chip { margin: 3px 0 3px 4px; }
html[dir="rtl"] #wcal-salary [style*="text-align:right"],
html[dir="rtl"] #esal-salary [style*="text-align:right"] { text-align: left !important; }
html[dir="rtl"] .toolbar .search { margin-left: 0; }

/* ---------- Dark mode ---------- */
:root[data-theme="dark"] {
  --bg: #0f1620;
  --card: #182330;
  --text: #e8ebef;
  --text-muted: #93a1b0;
  --border: #2b3746;
}
html[data-theme="dark"] .login-card h1 { color: var(--gold-light); }
html[data-theme="dark"] .stat-value { color: var(--gold-light); }
html[data-theme="dark"] .login-error { background: rgba(192,57,43,0.18); }
html[data-theme="dark"] input,
html[data-theme="dark"] select,
html[data-theme="dark"] textarea { background: #131b26; border-color: var(--border); color: var(--text); }
html[data-theme="dark"] .btn-danger { background: transparent; }
html[data-theme="dark"] .btn-danger:hover { background: rgba(192,57,43,0.15); }
html[data-theme="dark"] .btn-ghost { color: var(--text); }
html[data-theme="dark"] .btn-ghost:hover { background: rgba(255,255,255,0.06); }
html[data-theme="dark"] tbody tr:hover { background: rgba(255,255,255,0.03); }
html[data-theme="dark"] .badge-green { background: rgba(30,142,90,0.22); color: #6fe3ab; }
html[data-theme="dark"] .badge-amber { background: rgba(183,121,31,0.25); color: #f0c674; }
html[data-theme="dark"] .badge-red { background: rgba(192,57,43,0.25); color: #ff9686; }
html[data-theme="dark"] .badge-gray { background: rgba(255,255,255,0.08); color: var(--text-muted); }
html[data-theme="dark"] .employee-chip { background: rgba(255,255,255,0.06); color: var(--text); }
html[data-theme="dark"] .employee-chip.unavailable { background: rgba(192,57,43,0.22); color: #ff9686; }
html[data-theme="dark"] .cal-day.worked { background: rgba(192,57,43,0.18); }
html[data-theme="dark"] .cal-day.not-worked { background: rgba(30,142,90,0.16); }
html[data-theme="dark"] .modal { background: var(--card); }
