/* ================================================================
   CSS CUSTOM PROPERTIES — Light mode defaults
================================================================ */
:root {
  --color-bg: #f8f9fc;
  --color-surface: #ffffff;
  --color-surface-2: #f1f3f9;
  --color-border: #e2e6ef;
  --color-text: #0f1117;
  --color-text-muted: #6b7280;
  --color-accent: #4f46e5;
  --color-accent-hover: #4338ca;
  --color-accent-fg: #ffffff;
  --color-danger: #dc2626;
  --color-warning: #d97706;
  --color-success: #16a34a;

  --sidebar-width: 240px;
  --sidebar-collapsed-width: 52px;
  --topbar-height: 60px;
  --transition-speed: 0.2s;
}

/* Dark mode via system preference */
@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #0d0f14;
    --color-surface: #161b27;
    --color-surface-2: #1e2535;
    --color-border: #2a3044;
    --color-text: #f0f2f8;
    --color-text-muted: #8892a4;
    --color-accent: #6366f1;
    --color-accent-hover: #818cf8;
    --color-accent-fg: #ffffff;
    --color-danger: #ef4444;
    --color-warning: #f59e0b;
    --color-success: #22c55e;
  }
  .btn-icon.danger:hover { background: #450a0a; }
  select.form-control {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%238892a4' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  }
}

/* Dark mode via explicit data-theme attribute (JS toggle overrides system) */
html[data-theme="dark"] {
  --color-bg: #0d0f14;
  --color-surface: #161b27;
  --color-surface-2: #1e2535;
  --color-border: #2a3044;
  --color-text: #f0f2f8;
  --color-text-muted: #8892a4;
  --color-accent: #6366f1;
  --color-accent-hover: #818cf8;
  --color-accent-fg: #ffffff;
  --color-danger: #ef4444;
  --color-warning: #f59e0b;
  --color-success: #22c55e;
}

html[data-theme="light"] {
  --color-bg: #f8f9fc;
  --color-surface: #ffffff;
  --color-surface-2: #f1f3f9;
  --color-border: #e2e6ef;
  --color-text: #0f1117;
  --color-text-muted: #6b7280;
  --color-accent: #4f46e5;
  --color-accent-hover: #4338ca;
  --color-accent-fg: #ffffff;
  --color-danger: #dc2626;
  --color-warning: #d97706;
  --color-success: #16a34a;
}

/* ================================================================
   RESET & BASE
================================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* Role-gating utility — toggled by applyRoleVisibility() in role-gate.js */
.hidden { display: none !important; }

html { font-size: 15px; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  min-height: 100vh;
  display: flex;
  transition: background var(--transition-speed), color var(--transition-speed);
}

/* ================================================================
   APP SHELL — sidebar + main layout
================================================================ */
#app-shell {
  display: flex;
  width: 100%;
  min-height: 100vh;
}

/* ================================================================
   SIDEBAR (repurposed #top-nav)
================================================================ */
#top-nav {
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  width: var(--sidebar-width);
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  z-index: 200;
  transition: width var(--transition-speed) ease, transform var(--transition-speed) ease,
              background var(--transition-speed), border-color var(--transition-speed);
  overflow: hidden;
}

/* Collapsed state */
#top-nav.collapsed {
  width: var(--sidebar-collapsed-width);
}

/* Sidebar top: logo + collapse button */
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  height: var(--topbar-height);
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  overflow: hidden;
  white-space: nowrap;
}

.brand-icon {
  font-size: 1.25rem;
  flex-shrink: 0;
  line-height: 1;
}

.brand-text {
  font-size: 0.875rem;
  font-weight: 700;
  color: var(--color-text);
  letter-spacing: -0.01em;
  white-space: nowrap;
  opacity: 1;
  transition: opacity var(--transition-speed);
}

#top-nav.collapsed .brand-text {
  opacity: 0;
  width: 0;
  pointer-events: none;
}

/* Collapse toggle button */
#sidebar-toggle {
  background: none;
  border: none;
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
  color: var(--color-text-muted);
  font-size: 0.85rem;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition-speed), color var(--transition-speed);
  line-height: 1;
  min-width: 32px;
  min-height: 32px;
}

#sidebar-toggle:hover {
  background: var(--color-surface-2);
  color: var(--color-text);
}

/* Nav links area */
#top-nav nav {
  display: flex;
  flex-direction: column;
  flex: 1;
  padding: 12px 0;
  overflow-y: auto;
  overflow-x: hidden;
}

/* Each nav button */
#top-nav nav button {
  display: flex;
  align-items: center;
  gap: 12px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--color-text-muted);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  padding: 10px 16px;
  border-radius: 0;
  text-align: left;
  white-space: nowrap;
  width: 100%;
  position: relative;
  transition: color var(--transition-speed), background var(--transition-speed);
  /* Left accent strip */
  border-left: 3px solid transparent;
}

#top-nav nav button:hover {
  background: var(--color-surface-2);
  color: var(--color-text);
}

#top-nav nav button.active {
  color: var(--color-accent);
  background: color-mix(in srgb, var(--color-accent) 8%, transparent);
  border-left-color: var(--color-accent);
  font-weight: 600;
}

.nav-icon {
  font-size: 1rem;
  flex-shrink: 0;
  width: 20px;
  text-align: center;
  line-height: 1;
}

.nav-label {
  opacity: 1;
  transition: opacity var(--transition-speed);
  overflow: hidden;
}

#top-nav.collapsed .nav-label {
  opacity: 0;
  width: 0;
  pointer-events: none;
}

/* Tooltip when collapsed */
#top-nav.collapsed nav button {
  justify-content: center;
  padding: 12px 0;
}

#top-nav.collapsed nav button.active {
  border-left-color: transparent;
  border-bottom: 2px solid var(--color-accent);
}

#top-nav.collapsed nav button::after {
  content: attr(data-tooltip);
  position: absolute;
  left: calc(var(--sidebar-collapsed-width) + 8px);
  background: var(--color-text);
  color: var(--color-surface);
  font-size: 0.78rem;
  font-weight: 500;
  padding: 5px 10px;
  border-radius: 6px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 300;
}

#top-nav.collapsed nav button:hover::after {
  opacity: 1;
}

/* Sidebar bottom: theme toggle */
.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--color-border);
  flex-shrink: 0;
}

/* Signed-in user identity — name/email + role badge, shown above the footer actions */
.nav-user-identity {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 4px 12px;
  margin-bottom: 8px;
  border-bottom: 1px solid var(--color-border);
}

.nav-user-name {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--color-text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.nav-user-role {
  flex-shrink: 0;
}

/* Collapse alongside the other footer labels — no room to show name/badge */
#top-nav.collapsed .nav-user-identity {
  display: none;
}

#theme-toggle-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  background: none;
  border: 1px solid var(--color-border);
  border-radius: 7px;
  cursor: pointer;
  color: var(--color-text-muted);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 500;
  padding: 8px 12px;
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  transition: background var(--transition-speed), border-color var(--transition-speed),
              color var(--transition-speed);
}

#theme-toggle-btn:hover {
  background: var(--color-surface-2);
  color: var(--color-text);
}

#top-nav.collapsed #theme-toggle-btn {
  justify-content: center;
  padding: 8px 0;
}

#top-nav.collapsed .theme-toggle-label {
  display: none;
}

/* ================================================================
   MOBILE TOPBAR
================================================================ */
#mobile-topbar {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--topbar-height);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  z-index: 300;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
}

#mobile-menu-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.2rem;
  color: var(--color-text-muted);
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mobile-brand {
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--color-text);
}

/* Mobile sidebar overlay backdrop */
#sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 199;
}

#sidebar-backdrop.open {
  display: block;
}

/* ================================================================
   MAIN CONTENT AREA
================================================================ */
#main-content {
  margin-left: var(--sidebar-width);
  flex: 1;
  min-width: 0;
  transition: margin-left var(--transition-speed) ease;
}

body.sidebar-collapsed #main-content {
  margin-left: var(--sidebar-collapsed-width);
}

/* ================================================================
   SECTIONS
================================================================ */
.section {
  display: none;
  padding: 32px;
  max-width: 1160px;
  margin: 0 auto;
}

.section.active { display: block; }

/* Project Detail needs the full available width for the calendar +
   worker-groups grid below it — cap other sections at 1160px but let
   this one use whatever room the sidebar leaves. */
#section-project-detail.section {
  max-width: none;
}

/* ================================================================
   CARDS
================================================================ */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 10px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
  padding: 20px 24px;
  margin-bottom: 20px;
  transition: background var(--transition-speed), border-color var(--transition-speed);
}

html[data-theme="dark"] .card {
  box-shadow: 0 2px 8px rgba(0,0,0,.25);
}

.card-title {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--color-text-muted);
  margin-bottom: 12px;
}

/* ================================================================
   STAT BANNERS
================================================================ */
.stats-row {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 20px;
}

.stat-card {
  flex: 1;
  min-width: 140px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 10px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
  padding: 16px 20px;
  transition: background var(--transition-speed), border-color var(--transition-speed);
}

.stat-card .label {
  font-size: 0.7rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
}

.stat-card .value {
  font-size: 1.85rem;
  font-weight: 700;
  color: var(--color-text);
  line-height: 1.2;
  margin-top: 4px;
  letter-spacing: -0.02em;
}

.stat-card .sub {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  margin-top: 2px;
}

/* ================================================================
   TABLES
================================================================ */
.table-wrap { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

th {
  background: var(--color-surface-2);
  text-align: left;
  padding: 9px 16px;
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-text-muted);
  border-bottom: 1px solid var(--color-border);
  transition: background var(--transition-speed), color var(--transition-speed),
              border-color var(--transition-speed);
}

td {
  padding: 14px 16px;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
  color: var(--color-text);
  transition: background var(--transition-speed), border-color var(--transition-speed);
}

tr:last-child td { border-bottom: none; }

tr:hover td { background: var(--color-surface-2); }

.sortable-th:hover { text-decoration: underline; }

.subtotal-row td {
  background: var(--color-surface-2);
  font-weight: 600;
}

.grand-total-row td {
  background: color-mix(in srgb, var(--color-accent) 8%, var(--color-surface));
  font-weight: 700;
}

/* ================================================================
   BADGES
================================================================ */
.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  vertical-align: middle;
}

.badge-green  { background: #dcfce7; color: #15803d; }
.badge-yellow { background: #fef9c3; color: #92400e; }
.badge-gray   { background: var(--color-surface-2); color: var(--color-text-muted); }
.badge-red    { background: #fee2e2; color: #b91c1c; }
.badge-blue   { background: #dbeafe; color: #1e40af; }

html[data-theme="dark"] .badge-green  { background: #14532d; color: #4ade80; }
html[data-theme="dark"] .badge-yellow { background: #78350f; color: #fbbf24; }
html[data-theme="dark"] .badge-red    { background: #450a0a; color: #f87171; }
html[data-theme="dark"] .badge-blue   { background: #1e3a5f; color: #60a5fa; }

/* "You" indicator — shown next to the current user's linked worker record */
.badge-you {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  vertical-align: middle;
  background: var(--color-surface-2);
  color: var(--color-text-muted);
  margin-left: 6px;
}

/* ================================================================
   BUTTONS
================================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: 7px;
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 500;
  transition: background 0.15s, color 0.15s, border-color 0.15s, opacity 0.15s;
  line-height: 1;
}

.btn:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

.btn-primary {
  background: var(--color-accent);
  color: var(--color-accent-fg);
}
.btn-primary:hover { background: var(--color-accent-hover); }

.btn-success { background: var(--color-success); color: #fff; }
.btn-success:hover { filter: brightness(0.9); }

.btn-danger { background: var(--color-danger); color: #fff; }
.btn-danger:hover { filter: brightness(0.9); }

.btn-warning { background: var(--color-warning); color: #fff; }
.btn-warning:hover { filter: brightness(0.9); }

.btn-ghost {
  background: transparent;
  color: var(--color-text-muted);
  border: 1px solid var(--color-border);
}
.btn-ghost:hover {
  background: var(--color-surface-2);
  color: var(--color-text);
}

.btn-sm { padding: 5px 10px; font-size: 0.78rem; }

.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  padding: 5px 7px;
  border-radius: 5px;
  font-size: 0.9rem;
  color: var(--color-text-muted);
  transition: color 0.15s, background 0.15s;
  font-family: inherit;
}
.btn-icon:hover {
  background: var(--color-surface-2);
  color: var(--color-text);
}
.btn-icon.danger:hover {
  background: #fee2e2;
  color: var(--color-danger);
}

html[data-theme="dark"] .btn-icon.danger:hover {
  background: #450a0a;
}

/* ================================================================
   FORMS
================================================================ */
.form-group { margin-bottom: 16px; }

.form-label {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--color-text-muted);
  margin-bottom: 5px;
  letter-spacing: 0.01em;
}

.form-control {
  display: block;
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--color-border);
  border-radius: 7px;
  font-family: inherit;
  font-size: 0.875rem;
  color: var(--color-text);
  background: var(--color-surface);
  transition: border-color 0.15s, box-shadow 0.15s, background var(--transition-speed),
              color var(--transition-speed);
}

.form-control:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-accent) 20%, transparent);
}

/* Fix — custom chevron replaces native OS arrow, giving controlled right spacing */
select.form-control {
  padding-right: 2rem;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%236b7280' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  background-size: 12px;
}

html[data-theme="dark"] select.form-control {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%238892a4' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
}

.form-row { display: flex; gap: 16px; flex-wrap: wrap; }
.form-row .form-group { flex: 1; min-width: 140px; }

.form-hint {
  font-size: 0.72rem;
  color: var(--color-text-muted);
  margin-top: 4px;
}

.field-error {
  font-size: 0.72rem;
  color: var(--color-danger);
  margin-top: 4px;
}

/* ================================================================
   MODAL
================================================================ */
.modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 400;
  align-items: center;
  justify-content: center;
  padding: 16px;
  animation: none;
}

.modal-backdrop.open {
  display: flex;
  animation: fadeInBackdrop 0.15s ease;
}

@keyframes fadeInBackdrop {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.modal {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 12px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.18);
  padding: 28px 32px;
  width: 100%;
  max-width: 520px;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
  animation: slideUpModal 0.18s ease;
  transition: background var(--transition-speed), border-color var(--transition-speed);
}

@keyframes slideUpModal {
  from { transform: translateY(12px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .modal-backdrop.open { animation: none; }
  .modal { animation: none; }
}

.modal.wide { max-width: 700px; }

.modal-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--color-text);
  margin-bottom: 20px;
  padding-right: 36px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 24px;
}

.modal-close-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.25rem;
  color: var(--color-text-muted);
  line-height: 1;
  padding: 4px;
  border-radius: 4px;
  transition: color 0.15s, background 0.15s;
}
.modal-close-btn:hover {
  color: var(--color-danger);
  background: color-mix(in srgb, var(--color-danger) 10%, transparent);
}

/* ================================================================
   TIME LOG GRID
================================================================ */
#log-grid-table { font-size: 0.83rem; }
#log-grid-table th,
#log-grid-table td { padding: 8px 10px; }

#log-grid-table input[type="time"],
#log-grid-table select { font-size: 0.82rem; padding: 5px 8px; }

#log-grid-table .hours-cell {
  color: var(--color-accent);
  font-weight: 600;
  min-width: 52px;
}

#log-grid-table tr.conflict td {
  background: color-mix(in srgb, var(--color-danger) 8%, var(--color-surface)) !important;
}
#log-grid-table tr.conflict {
  outline: 2px solid var(--color-danger);
}

.grid-footer {
  background: color-mix(in srgb, var(--color-accent) 8%, var(--color-surface-2));
  border-radius: 0 0 8px 8px;
  padding: 10px 16px;
  font-size: 0.83rem;
  color: var(--color-accent);
  font-weight: 600;
  border-top: 1px solid var(--color-border);
  transition: background var(--transition-speed), color var(--transition-speed);
}

/* ================================================================
   WORKER GROUP (Dashboard)
================================================================ */
.worker-group {
  margin-bottom: 16px;
  border-radius: 7px;
  overflow: hidden;
  border: 1px solid var(--color-border);
}

.worker-group-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--color-surface-2);
  padding: 8px 16px;
  border-radius: 7px 7px 0 0;
  border-bottom: 1px solid var(--color-border);
  font-weight: 600;
  font-size: 0.85rem;
  transition: background var(--transition-speed), border-color var(--transition-speed);
}

.worker-group-totals {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  font-weight: 400;
}

/* ================================================================
   REPORTS
================================================================ */
.report-controls {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  align-items: flex-end;
  margin-bottom: 0;
}
.report-controls .form-group { margin-bottom: 0; }

.report-group-header td {
  background: var(--color-accent);
  color: var(--color-accent-fg);
  font-weight: 700;
  font-size: 0.82rem;
  padding: 8px 16px;
}

/* Worker-group band nested under a project heading — deliberately quieter than
   .report-group-header so the project/group hierarchy reads at a glance. */
.report-crew-header td {
  background: var(--color-surface-2);
  font-weight: 700;
  font-size: 0.76rem;
  letter-spacing: .04em;
  text-transform: uppercase;
  padding: 6px 16px 6px 28px;
}
.crew-subtotal-row td {
  font-size: 0.78rem;
  color: var(--color-text-muted);
  background: color-mix(in srgb, var(--color-surface-2) 45%, transparent);
}

/* ================================================================
   TOGGLE SWITCH
================================================================ */
.toggle-wrap { display: flex; align-items: center; gap: 12px; }

.toggle {
  position: relative;
  display: inline-block;
  width: 42px;
  height: 24px;
  flex-shrink: 0;
}
.toggle input { opacity: 0; width: 0; height: 0; }

.toggle-slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: var(--color-border);
  border-radius: 24px;
  transition: background 0.2s;
}
.toggle-slider::before {
  content: "";
  position: absolute;
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}
.toggle input:checked + .toggle-slider { background: var(--color-accent); }
.toggle input:checked + .toggle-slider::before { transform: translateX(18px); }

/* ================================================================
   ALERTS
================================================================ */
.alert {
  border-radius: 7px;
  padding: 12px 16px;
  font-size: 0.85rem;
  margin-bottom: 12px;
  border-left: 4px solid transparent;
}

.alert-error {
  background: color-mix(in srgb, var(--color-danger) 8%, var(--color-surface));
  color: var(--color-danger);
  border-left-color: var(--color-danger);
  border: 1px solid color-mix(in srgb, var(--color-danger) 25%, transparent);
  border-left-width: 4px;
}

.alert-success {
  background: color-mix(in srgb, var(--color-success) 8%, var(--color-surface));
  color: var(--color-success);
  border: 1px solid color-mix(in srgb, var(--color-success) 25%, transparent);
  border-left-width: 4px;
  border-left-color: var(--color-success);
}

.alert-info {
  background: color-mix(in srgb, var(--color-accent) 8%, var(--color-surface));
  color: var(--color-accent);
  border: 1px solid color-mix(in srgb, var(--color-accent) 25%, transparent);
  border-left-width: 4px;
  border-left-color: var(--color-accent);
}

.alert-warn {
  background: color-mix(in srgb, var(--color-warning) 8%, var(--color-surface));
  color: var(--color-warning);
  border: 1px solid color-mix(in srgb, var(--color-warning) 25%, transparent);
  border-left-width: 4px;
  border-left-color: var(--color-warning);
}

/* ================================================================
   SECTION HEADER & TITLE
================================================================ */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section-title {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--color-text);
  letter-spacing: -0.01em;
}

/* ================================================================
   MISC UTILITIES
================================================================ */
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.mt-1 { margin-top: 8px; }
.mt-2 { margin-top: 16px; }
.mb-1 { margin-bottom: 8px; }
.text-muted { color: var(--color-text-muted); font-size: 0.82rem; }
.text-right { text-align: right; }
.empty-state {
  text-align: center;
  padding: 40px 16px;
  color: var(--color-text-muted);
  font-size: 0.9rem;
}

code {
  font-family: ui-monospace, 'SF Mono', 'Fira Code', monospace;
  font-size: 0.82em;
  background: var(--color-surface-2);
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid var(--color-border);
}

/* Fix — spacing between action buttons in table cells */
td .btn + .btn { margin-left: 6px; }

/* Fix — consistent button width in action columns so rows never vary in height */
td .btn-sm {
  min-width: 80px;
  text-align: center;
  white-space: nowrap;
}

/* Fix — action cell wrapper keeps buttons on one line, vertically centered */
td > div.action-btns {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: nowrap;
}

/* Fix — narrow inputs for settings fields */
.input-narrow { width: 7rem; max-width: 100%; }

/* Fix — card heading for settings section */
.card-heading {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 16px;
}

/* ================================================================
   PAYROLL PERIODS PANEL
================================================================ */
#payroll-periods-details {
  /* <details> resets */
  list-style: none;
}
#payroll-periods-details::-webkit-details-marker { display: none; }

.payroll-periods-summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  user-select: none;
  padding: 4px 0 12px;
  /* Remove default marker arrow — we use our own chevron */
  list-style: none;
}
.payroll-periods-summary::-webkit-details-marker { display: none; }
.payroll-periods-summary:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
  border-radius: 4px;
}

.payroll-periods-chevron {
  font-size: 0.65rem;
  color: var(--color-text-muted);
  transition: transform 0.18s ease;
}

/* Rotate chevron when closed */
#payroll-periods-details:not([open]) .payroll-periods-chevron {
  transform: rotate(180deg);
}

.payroll-periods-body {
  padding-top: 4px;
}

/* Individual period rows */
.period-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 7px;
  border: 1px solid var(--color-border);
  margin-bottom: 8px;
  background: var(--color-surface-2);
  flex-wrap: wrap;
  transition: background var(--transition-speed), border-color var(--transition-speed);
}

.period-row-info {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  min-width: 0;
}

.period-row-label {
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

.period-row-dates {
  font-size: 0.78rem;
  color: var(--color-text-muted);
  white-space: nowrap;
}

.period-row-actions {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-shrink: 0;
  flex-wrap: nowrap;
}

/* Smaller action buttons inside period rows — override the .btn-sm min-width
   so "Load Dates" / "Reopen" don't force oversized row widths */
.period-row-actions .btn-sm {
  min-width: unset;
}

/* ================================================================
   PRINT — standard report print
================================================================ */
@media print {
  #top-nav, #mobile-topbar, #sidebar-backdrop,
  .report-controls, .no-print { display: none !important; }
  body { background: #fff; }
  #main-content { margin-left: 0 !important; }
  .section { display: block !important; padding: 0 !important; }
  .section:not(#section-reports) { display: none !important; }
  .card { box-shadow: none; border: 1px solid #e2e8f0; }
  #payslip-print-area { display: none !important; }
}

/* ================================================================
   PAYSLIP PRINT
   body.printing-payslips hides everything except #payslip-print-area.
================================================================ */
body.printing-payslips > *:not(#payslip-print-area) {
  display: none !important;
}

body.printing-payslips #payslip-print-area {
  display: block !important;
}

/* Payslip card layout (screen preview while body class is active) */
.payslip-card {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  padding: 32px 40px;
  max-width: 680px;
  margin: 0 auto;
  color: #0f1117;
  background: #fff;
}

.payslip-page-break {
  page-break-after: always;
}

.payslip-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 20px;
  border-bottom: 2px solid #0f1117;
  padding-bottom: 12px;
}

.payslip-title {
  font-size: 1.4rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  color: #0f1117;
}

.payslip-period {
  font-size: 0.82rem;
  color: #4b5563;
  font-weight: 500;
}

.payslip-meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 24px;
  margin-bottom: 20px;
  font-size: 0.875rem;
}

.payslip-meta > div {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.payslip-field-label {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: #6b7280;
}

.payslip-field-value {
  font-weight: 600;
  color: #0f1117;
}

.payslip-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  margin-bottom: 20px;
}

.payslip-table th {
  background: #f1f3f9;
  text-align: left;
  padding: 8px 12px;
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #4b5563;
  border-bottom: 1px solid #e2e6ef;
}

.payslip-table td {
  padding: 10px 12px;
  border-bottom: 1px solid #e2e6ef;
  color: #0f1117;
  vertical-align: middle;
}

.payslip-total-row td {
  background: #f1f3f9;
  font-size: 0.9rem;
  border-top: 2px solid #0f1117;
  border-bottom: none;
}

.payslip-signature-area {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 32px;
  margin-top: 40px;
}

.payslip-sig-line {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.payslip-sig-blank {
  border-bottom: 1px solid #0f1117;
  height: 36px;
}

.payslip-sig-label {
  font-size: 0.72rem;
  color: #6b7280;
  text-align: center;
}

@media print {
  /* When printing payslips specifically */
  body.printing-payslips {
    background: #fff !important;
  }

  body.printing-payslips #payslip-print-area {
    display: block !important;
  }

  .payslip-page-break {
    page-break-after: always;
    -webkit-column-break-after: always;
    break-after: page;
  }

  .payslip-card {
    padding: 20px 24px;
    max-width: 100%;
  }
}

/* ================================================================
   RESPONSIVE — MOBILE (< 768px)
================================================================ */
@media (max-width: 767px) {
  #top-nav {
    transform: translateX(-100%);
    width: var(--sidebar-width) !important; /* always full width on mobile */
    box-shadow: 4px 0 24px rgba(0,0,0,0.18);
  }

  #top-nav.mobile-open {
    transform: translateX(0);
  }

  /* Reset collapsed styles on mobile */
  #top-nav.collapsed { width: var(--sidebar-width) !important; }
  #top-nav.collapsed .nav-label { opacity: 1; width: auto; }
  #top-nav.collapsed .brand-text { opacity: 1; width: auto; }
  #top-nav.collapsed nav button { justify-content: flex-start; padding: 10px 16px; }
  #top-nav.collapsed nav button::after { display: none; }
  #top-nav.collapsed #theme-toggle-btn { justify-content: flex-start; padding: 8px 12px; }
  #top-nav.collapsed .theme-toggle-label { display: inline; }

  #mobile-topbar { display: flex; }

  #main-content {
    margin-left: 0 !important;
    padding-top: var(--topbar-height);
  }

  .section { padding: 20px 16px; }
  .form-row { flex-direction: column; }
  #sidebar-toggle { display: none; }
}

/* ================================================================
   PROJECT DETAIL — calendar heatmap + day drill-down
================================================================ */
.project-link { color: var(--color-accent); text-decoration: none; font-weight: 500; }
.project-link:hover { text-decoration: underline; }

.pd-cal-nav { display:flex; align-items:center; justify-content:center; gap:16px; margin-bottom:12px; }
.pd-cal-month { font-weight:600; min-width:160px; text-align:center; }

.pd-cal-weekdays,
.pd-cal-grid { display:grid; grid-template-columns:repeat(7,1fr); gap:6px; }
.pd-cal-weekdays { margin-bottom:6px; font-size:0.72rem; color:var(--color-text-muted); text-align:center; }

.pd-day {
  aspect-ratio: 1/1;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  background: var(--color-surface);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-between;
  padding: 6px;
  cursor: pointer;
  font: inherit;
  color: inherit;
  transition: transform .12s ease-out, box-shadow .12s ease-out;
}
.pd-day:hover { transform: translateY(-1px); box-shadow: 0 2px 8px rgba(0,0,0,.12); }
.pd-day:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
.pd-day-empty { background: transparent; border-color: transparent; cursor: default; pointer-events: none; }
.pd-day-num { font-size: 0.8rem; font-weight: 600; }
.pd-day-hrs { font-size: 0.72rem; opacity: .8; align-self: flex-end; }

/* Heat levels — 0 = no work, amber = partial day, green = full day */
.heat-zero  { background: var(--color-surface-2); }
.heat-amber { background: color-mix(in srgb, var(--color-warning) 20%, var(--color-surface)); border-color: var(--color-warning); }
.heat-green { background: color-mix(in srgb, var(--color-success) 20%, var(--color-surface)); border-color: var(--color-success); }

.pd-day.is-today  { outline: 2px solid var(--color-accent); outline-offset: 1px; }
.pd-day.is-selected { box-shadow: 0 0 0 2px var(--color-accent) inset; }

/* Day drill-down table */
.pd-day-table { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: 0.875rem; }
.pd-day-table th { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--color-border); font-weight: 600; color: var(--color-text-muted); }
.pd-day-table td { padding: 6px 8px; border-bottom: 1px solid var(--color-border); }

@media (max-width: 600px) {
  .pd-day { padding: 3px; border-radius: 6px; }
  .pd-day-num { font-size: 0.68rem; }
  .pd-day-hrs { font-size: 0.6rem; }
  .pd-cal-weekdays div { font-size: 0.6rem; }
  /* Force single-column group cards well before auto-fill's 280px min
     would risk overflow on narrow phones. */
  #section-project-detail #pd-day-body { grid-template-columns: 1fr; }
}

/* ================================================================
   CASH ADVANCES PANEL
================================================================ */
.advances-panel {
  padding: 12px 16px;
  background: var(--color-surface-alt, var(--color-surface));
  border-top: 1px solid var(--color-border);
  border-bottom: 1px solid var(--color-border);
}

/* Override the default td min-width rule inside the advances panel's inner table
   so action buttons don't inflate the narrow columns */
.advances-panel td .btn-sm {
  min-width: unset;
}

/* ================================================================
   CAL-SPLIT — shared two-column layout: calendar LEFT, content RIGHT
   Used on both Project Detail and Time Log pages.
================================================================ */
.cal-split {
  display: grid;
  grid-template-columns: minmax(280px, 360px) 1fr;
  gap: 16px;
  align-items: start;
}
@media (max-width: 720px) {
  .cal-split { grid-template-columns: 1fr; }
}

/* Project Detail overrides the shared side-by-side cal-split: calendar on
   top (capped width, centered so cells don't balloon on wide screens),
   worker-groups day-panel stacked below at full width. Time Log keeps the
   default side-by-side .cal-split above — this selector is scoped so it
   doesn't regress that page. */
#section-project-detail .cal-split {
  display: block;
}
#section-project-detail .cal-split > .card:first-child {
  max-width: 480px;
  margin: 0 auto 20px;
}
#section-project-detail #pd-day-panel {
  width: 100%;
}

/* ================================================================
   PROJECT DETAIL — Manage Workers panel
================================================================ */
.pd-worker-assignment-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--color-border);
}
.pd-worker-assignment-row:last-child { border-bottom: none; }
/* Worker groups editor — Workers section (mig 011) */
.group-edit-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 0;
}
.group-edit-row .form-control { flex: 1; min-width: 0; }
.pd-worker-assignment-info { display: flex; flex-direction: column; gap: 2px; }
.pd-worker-assignment-name { font-size: 0.875rem; font-weight: 500; }
.pd-worker-assignment-role { font-size: 0.75rem; color: var(--color-text-muted); }

/* ================================================================
   PROJECT DETAIL — per-worker log rows in day panel
================================================================ */
/* Responsive card grid: wide screens show multiple group columns,
   narrow screens reflow to a single column. Empty-state placeholder
   (rendered when no day is selected / no workers assigned) spans the
   full grid width instead of getting squeezed into one card column. */
#pd-day-body {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 14px;
  align-items: start;
}
#pd-day-body > .empty-state {
  grid-column: 1 / -1;
}
.pd-group-block {
  border: 1px solid var(--color-border);
  border-radius: 10px;
  background: var(--color-surface-2);
  padding: 12px 14px;
  min-width: 0;
}
.pd-group-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  margin-top: 0;
  margin-bottom: 4px;
  border-radius: 6px;
  background: var(--color-surface);
}
.pd-group-name {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: .05em;
  text-transform: uppercase;
}
.pd-group-count {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--color-text-muted);
  background: var(--color-surface);
  border-radius: 999px;
  padding: 1px 7px;
}
.pd-worker-log-row {
  padding: 12px 0;
  border-bottom: 1px solid var(--color-border);
}
/* Last row of each group, not just of the whole panel — the group header
   already separates blocks, so a trailing rule would double up. */
.pd-worker-log-row:last-child { border-bottom: none; }
.pd-worker-log-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}
.pd-worker-log-name { font-size: 0.875rem; font-weight: 600; }
.pd-drag-handle {
  cursor: grab;
  font-size: 1.05rem;
  line-height: 1;
  color: var(--color-text-muted);
  padding: 4px 6px;
  border-radius: 4px;
  user-select: none;
  /* Scoped to the handle only (NOT the row) so page/section scroll and the
     time inputs elsewhere in the row stay normally interactive on touch. */
  touch-action: none;
}
.pd-drag-handle:hover,
.pd-drag-handle:focus-visible {
  color: var(--color-text);
  background: var(--color-surface-2);
}
.pd-drag-handle:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 1px;
}
.pd-worker-log-row-dragging {
  position: relative;
  z-index: 5;
  cursor: grabbing;
  background: var(--color-surface-2);
  border-radius: 6px;
  box-shadow: 0 4px 10px rgba(0,0,0,.15);
}
.pd-worker-log-summary {
  font-size: 0.85rem;
  color: var(--color-text);
}
.pd-log-edit-btn { flex-shrink: 0; }
.pd-worker-log-badge {
  font-size: 0.72rem;
  padding: 2px 7px;
  border-radius: 4px;
  font-weight: 500;
}
.pd-worker-log-badge.has-log {
  background: color-mix(in srgb, var(--color-success) 15%, var(--color-surface));
  color: var(--color-success);
}
.pd-worker-log-badge.no-log {
  background: var(--color-surface-2);
  color: var(--color-text-muted);
}
.pd-worker-unassigned-tag {
  font-size: 0.72rem;
  font-style: italic;
  color: var(--color-text-muted);
}
.pd-worker-log-fields {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}
.pd-worker-log-field {
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.pd-worker-log-field label,
.pd-worker-log-field span.field-label {
  font-size: 0.72rem;
  color: var(--color-text-muted);
  font-weight: 500;
}

/* ================================================================
   PROJECT DETAIL — fixed AM/PM/OT slot editor (one Save per worker)
================================================================ */
.pd-worker-slots {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.pd-worker-slot {
  display: flex;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 12px;
}
.pd-slot-label {
  width: 32px;
  flex-shrink: 0;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--color-text-muted);
  padding-bottom: 10px;
}
.pd-slot-clear-btn {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  margin-bottom: 3px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  background: var(--color-surface);
  color: var(--color-text-muted);
  font-size: 0.82rem;
  line-height: 1;
  cursor: pointer;
  transition: background 150ms ease-out, color 150ms ease-out, border-color 150ms ease-out;
}
.pd-slot-clear-btn:hover,
.pd-slot-clear-btn:focus-visible {
  color: var(--color-danger);
  border-color: color-mix(in srgb, var(--color-danger) 50%, var(--color-border));
  background: color-mix(in srgb, var(--color-danger) 8%, var(--color-surface));
}
.pd-slot-clear-btn:focus-visible {
  outline: 2px solid var(--color-danger);
  outline-offset: 2px;
}
.pd-worker-log-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 10px;
}
.pd-worker-log-actions .pd-log-status { font-size: 0.78rem; }

/* ================================================================
   TIME LOG — calendar view worker cards
================================================================ */
.tl-worker-card {
  border: 1px solid var(--color-border);
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 10px;
  background: var(--color-surface);
  transition: border-color 0.15s;
}
.tl-worker-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--color-surface-2);
  cursor: pointer;
  user-select: none;
  border: none;
  width: 100%;
  font: inherit;
  color: inherit;
  text-align: left;
  transition: background 0.15s;
}
.tl-worker-card-header:hover {
  background: color-mix(in srgb, var(--color-accent) 8%, var(--color-surface-2));
}
.tl-worker-card-header[aria-expanded="true"] {
  background: color-mix(in srgb, var(--color-accent) 12%, var(--color-surface-2));
}
.tl-worker-card-header:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: -2px;
}
.tl-worker-card-info { display: flex; flex-direction: column; gap: 1px; }
.tl-worker-card-name { font-size: 0.875rem; font-weight: 600; }
.tl-worker-card-meta { font-size: 0.75rem; color: var(--color-text-muted); }
.tl-worker-card-chevron {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  transition: transform 0.15s ease;
  flex-shrink: 0;
}
.tl-worker-card-header[aria-expanded="true"] .tl-worker-card-chevron {
  transform: rotate(180deg);
}
.tl-worker-card-body {
  display: none;
  padding: 14px;
  border-top: 1px solid var(--color-border);
}
.tl-worker-card-body.open { display: block; }

/* Compact grid inside expanded worker card */
.tl-inner-grid { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
.tl-inner-grid th,
.tl-inner-grid td { padding: 6px 8px; }
.tl-inner-grid th {
  text-align: left;
  border-bottom: 1px solid var(--color-border);
  font-weight: 600;
  color: var(--color-text-muted);
  font-size: 0.75rem;
}
.tl-inner-grid td { border-bottom: 1px solid var(--color-border); }
.tl-inner-grid tr:last-child td { border-bottom: none; }
.tl-inner-grid input[type="time"],
.tl-inner-grid select { font-size: 0.8rem; padding: 4px 6px; }
.tl-inner-grid .hours-cell {
  color: var(--color-accent);
  font-weight: 600;
  min-width: 44px;
}

/* "Add for another worker" section in timelog day panel */
.tl-add-worker-section {
  padding: 12px 0 4px;
  border-top: 1px solid var(--color-border);
  margin-top: 8px;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

@media (prefers-reduced-motion: reduce) {
  .tl-worker-card-chevron { transition: none; }
  .tl-worker-card-header { transition: none; }
}
