/* ===== CSS Reset & Variables ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #3b82f6; --primary-dark: #2563eb; --primary-light: #dbeafe;
  --success: #22c55e; --success-light: #dcfce7;
  --warning: #f59e0b; --warning-light: #fef3c7;
  --danger: #ef4444; --danger-light: #fee2e2;
  --purple: #8b5cf6; --purple-light: #ede9fe;
  --orange: #f97316; --orange-light: #fff7ed;
  --gray-50: #f8fafc; --gray-100: #f1f5f9; --gray-200: #e2e8f0;
  --gray-300: #cbd5e1; --gray-400: #94a3b8; --gray-500: #64748b;
  --gray-600: #475569; --gray-700: #334155; --gray-800: #1e293b; --gray-900: #0f172a;
  --sidebar-width: 240px; --topbar-height: 60px;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1);
  /* Light mode defaults */
  --bg-body: var(--gray-100); --bg-card: #ffffff; --bg-sidebar: var(--gray-900);
  --bg-topbar: #ffffff; --bg-input: #ffffff; --bg-hover: var(--gray-50);
  --bg-row-hover: var(--gray-50); --text-primary: var(--gray-800);
  --text-secondary: var(--gray-500); --text-muted: var(--gray-400);
  --border-color: var(--gray-200); --border-input: var(--gray-300);
  --modal-bg: #ffffff;
}

/* ===== Dark Mode ===== */
body.dark {
  --bg-body: #0f172a; --bg-card: #1e293b; --bg-sidebar: #0b1120;
  --bg-topbar: #1e293b; --bg-input: #334155; --bg-hover: #334155;
  --bg-row-hover: #334155; --text-primary: #f1f5f9;
  --text-secondary: #94a3b8; --text-muted: #64748b;
  --border-color: #334155; --border-input: #475569;
  --modal-bg: #1e293b;
  --primary-light: #1e3a5f; --success-light: #14532d;
  --warning-light: #451a03; --danger-light: #450a0a;
  --purple-light: #2e1065; --orange-light: #431407;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-body); color: var(--text-primary); line-height: 1.5;
  transition: background 0.3s, color 0.3s;
}

/* ===== Login Screen ===== */
.login-screen { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(135deg, var(--gray-900) 0%, #1e3a5f 100%); }
.login-card { background: var(--bg-card); border-radius: 16px; padding: 40px; width: 400px; max-width: 90vw; box-shadow: var(--shadow-lg); color: var(--text-primary); }
.login-logo { text-align: center; margin-bottom: 32px; }
.login-logo i { font-size: 48px; color: #60a5fa; margin-bottom: 12px; }
.login-logo h1 { font-size: 28px; color: var(--text-primary); }
.login-logo p { color: var(--text-secondary); font-size: 14px; }
.login-hint { text-align: center; font-size: 12px; color: var(--text-muted); margin-top: 12px; }
.btn-block { width: 100%; }

/* ===== App Container ===== */
.app-container { display: flex; min-height: 100vh; }

/* ===== Sidebar ===== */
.sidebar { position: fixed; top: 0; left: 0; width: var(--sidebar-width); height: 100vh; background: var(--bg-sidebar); color: white; display: flex; flex-direction: column; z-index: 100; transition: transform 0.3s, background 0.3s; }
.sidebar-header { display: flex; align-items: center; gap: 10px; padding: 20px; border-bottom: 1px solid var(--gray-700); }
.sidebar-logo { font-size: 24px; color: #60a5fa; }
.sidebar-title { font-size: 20px; font-weight: 700; }
.nav-links { list-style: none; flex: 1; padding: 12px 8px; overflow-y: auto; }
.nav-link { display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-radius: var(--radius); cursor: pointer; color: var(--gray-400); font-size: 14px; font-weight: 500; transition: all 0.15s; margin-bottom: 2px; }
.nav-link:hover { background: rgba(255,255,255,0.08); color: white; }
.nav-link.active { background: var(--primary); color: white; }
.nav-link i { width: 20px; text-align: center; font-size: 16px; }
.badge-count { margin-left: auto; background: var(--danger); color: white; font-size: 11px; padding: 2px 6px; border-radius: 10px; font-weight: 600; }
.sidebar-footer { padding: 16px; border-top: 1px solid var(--gray-700); display: flex; align-items: center; justify-content: space-between; }
.user-info { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--gray-400); }
.btn-logout { background: none; border: none; color: var(--gray-400); cursor: pointer; font-size: 16px; padding: 4px; }
.btn-logout:hover { color: var(--danger); }

/* Dark mode toggle in sidebar */
.dark-toggle { display: flex; align-items: center; gap: 8px; padding: 8px 16px; border-top: 1px solid var(--gray-700); cursor: pointer; color: var(--gray-400); font-size: 13px; transition: color 0.15s; }
.dark-toggle:hover { color: white; }
.dark-toggle i { width: 20px; text-align: center; }

/* ===== Main Content ===== */
.main-content { margin-left: var(--sidebar-width); min-height: 100vh; flex: 1; }
.topbar { position: sticky; top: 0; height: var(--topbar-height); background: var(--bg-topbar); border-bottom: 1px solid var(--border-color); display: flex; align-items: center; padding: 0 24px; gap: 16px; z-index: 50; box-shadow: var(--shadow); transition: background 0.3s, border-color 0.3s; }
.page-title { font-size: 18px; font-weight: 600; flex: 1; color: var(--text-primary); }
.menu-toggle { display: none; background: none; border: none; font-size: 20px; cursor: pointer; color: var(--text-secondary); }
.topbar-actions { display: flex; gap: 8px; }
.content-area { padding: 24px; }

/* ===== Buttons ===== */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 16px; border: 1px solid var(--border-input); border-radius: var(--radius); background: var(--bg-card); color: var(--text-primary); font-size: 14px; font-weight: 500; cursor: pointer; transition: all 0.15s; white-space: nowrap; }
.btn:hover { background: var(--bg-hover); }
.btn-primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { background: var(--danger); color: white; border-color: var(--danger); }
.btn-danger:hover { background: #b91c1c; }
.btn-success { background: var(--success); color: white; border-color: var(--success); }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-icon { padding: 6px 8px; }

/* ===== Cards ===== */
.card { background: var(--bg-card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; transition: background 0.3s; }
.card-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border-color); }
.card-header h3 { font-size: 15px; font-weight: 600; display: flex; align-items: center; gap: 8px; color: var(--text-primary); }
.card-header h3 i { color: var(--text-muted); }
.card-body { padding: 20px; }

/* ===== Stats Grid ===== */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card { background: var(--bg-card); border-radius: var(--radius); padding: 20px; display: flex; align-items: center; gap: 16px; box-shadow: var(--shadow); transition: background 0.3s; }
.stat-icon { width: 48px; height: 48px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 20px; background: var(--primary-light); color: var(--primary); }
.stat-icon-blue { background: var(--primary-light); color: var(--primary); }
.stat-icon-green { background: var(--success-light); color: var(--success); }
.stat-icon-red { background: var(--danger-light); color: var(--danger); }
.stat-icon-purple { background: var(--purple-light); color: var(--purple); }
.stat-icon-orange { background: var(--orange-light); color: var(--orange); }
.stat-info h3 { font-size: 28px; font-weight: 700; line-height: 1; color: var(--text-primary); }
.stat-info p { font-size: 13px; color: var(--text-secondary); margin-top: 4px; }

/* ===== Dashboard Grid ===== */
.dashboard-grid { display: grid; grid-template-columns: 1fr 320px; gap: 20px; }

/* ===== Tables ===== */
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th { text-align: left; padding: 10px 12px; font-weight: 600; color: var(--text-secondary); font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; border-bottom: 2px solid var(--border-color); }
.table td { padding: 10px 12px; border-bottom: 1px solid var(--border-color); vertical-align: middle; color: var(--text-primary); }
.table tbody tr:hover { background: var(--bg-row-hover); }

/* ===== Badges ===== */
.badge { display: inline-block; padding: 3px 8px; border-radius: 12px; font-size: 12px; font-weight: 600; text-transform: capitalize; }
.badge-scheduled { background: var(--primary-light); color: var(--primary); }
.badge-on_my_way { background: #cffafe; color: #0891b2; }
body.dark .badge-on_my_way { background: #164e63; color: #67e8f9; }
.badge-in_progress { background: var(--warning-light); color: #b45309; }
.badge-paused { background: var(--gray-200); color: var(--gray-600); }
body.dark .badge-paused { background: #374151; color: #d1d5db; }
.badge-completed { background: var(--success-light); color: var(--success); }
.badge-cancelled { background: var(--gray-200); color: var(--gray-500); }
body.dark .badge-cancelled { background: #374151; color: #9ca3af; }
.badge-draft { background: var(--gray-200); color: var(--gray-600); }
body.dark .badge-draft { background: #374151; color: #d1d5db; }
.badge-sent { background: var(--primary-light); color: var(--primary); }
.badge-accepted { background: var(--success-light); color: var(--success); }
.badge-converted { background: var(--purple-light); color: var(--purple); }
.badge-paid { background: var(--success-light); color: var(--success); }
.badge-overdue { background: var(--danger-light); color: var(--danger); }
.badge-new { background: var(--primary-light); color: var(--primary); }
.badge-followup { background: var(--warning-light); color: #b45309; }
.badge-active { background: var(--success-light); color: var(--success); }
.badge-inactive { background: var(--gray-200); color: var(--gray-500); }
body.dark .badge-inactive { background: #374151; color: #9ca3af; }
.badge-low { background: var(--gray-200); color: var(--gray-600); }
body.dark .badge-low { background: #374151; color: #d1d5db; }
.badge-medium { background: var(--primary-light); color: var(--primary); }
.badge-high { background: var(--warning-light); color: #b45309; }
body.dark .badge-high { background: #451a03; color: #fcd34d; }
.badge-urgent { background: var(--danger-light); color: var(--danger); }
body.dark .badge-urgent { background: #450a0a; color: #fca5a5; }
.badge-type { background: var(--gray-100); color: var(--gray-700); }
body.dark .badge-type { background: #374151; color: #e2e8f0; }

/* ===== Forms ===== */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 600; color: var(--text-primary); margin-bottom: 4px; }
.form-group label .required { color: var(--danger); }
.form-control { width: 100%; padding: 8px 12px; border: 1px solid var(--border-input); border-radius: var(--radius); font-size: 14px; font-family: inherit; transition: border-color 0.15s, background 0.3s; background: var(--bg-input); color: var(--text-primary); }
.form-control:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,0.1); }
select.form-control { cursor: pointer; }
textarea.form-control { resize: vertical; min-height: 80px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.form-section { margin-bottom: 16px; padding-bottom: 16px; border-bottom: 1px solid var(--border-color); }

/* ===== Search & Filters ===== */
.search-input { padding: 8px 12px; border: 1px solid var(--border-input); border-radius: var(--radius); font-size: 14px; width: 220px; font-family: inherit; background: var(--bg-input); color: var(--text-primary); }
.search-input:focus { outline: none; border-color: var(--primary); }
.filters-bar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.filters-bar select { padding: 8px 12px; border: 1px solid var(--border-input); border-radius: var(--radius); font-size: 13px; cursor: pointer; background: var(--bg-input); color: var(--text-primary); font-family: inherit; }

/* ===== Calendar ===== */
.calendar-header { display: flex; align-items: center; gap: 12px; }
.calendar-header h3 { font-size: 16px; min-width: 160px; text-align: center; color: var(--text-primary); }
.calendar { user-select: none; }
.calendar-weekdays { display: grid; grid-template-columns: repeat(7,1fr); text-align: center; font-size: 13px; font-weight: 600; color: var(--text-secondary); padding-bottom: 8px; border-bottom: 1px solid var(--border-color); }
.calendar-days { display: grid; grid-template-columns: repeat(7,1fr); gap: 2px; }
.calendar-day { min-height: 90px; padding: 6px; border-radius: var(--radius); cursor: pointer; border: 1px solid transparent; transition: all 0.15s; background: var(--bg-card); }
.calendar-day:hover { background: var(--bg-hover); }
.calendar-day .day-number { font-size: 13px; font-weight: 600; color: var(--text-secondary); margin-bottom: 4px; }
.calendar-day.today .day-number { background: var(--primary); color: white; width: 26px; height: 26px; border-radius: 50%; display: flex; align-items: center; justify-content: center; }
.calendar-day.other-month { opacity: 0.3; }
.calendar-job { font-size: 11px; padding: 2px 6px; border-radius: 4px; margin-bottom: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: pointer; }
.calendar-job.type-repair { background: #dbeafe; color: #1e40af; }
.calendar-job.type-maintenance { background: #d1fae5; color: #065f46; }
.calendar-job.type-installation { background: #fef3c7; color: #92400e; }
.calendar-job.type-inspection { background: #e0e7ff; color: #3730a3; }
.calendar-job.type-emergency { background: #fee2e2; color: #991b1b; }
.calendar-job.type-service_call { background: #ccfbf1; color: #115e59; }
.calendar-job.type-estimate { background: #ede9fe; color: #5b21b6; }
.calendar-job.type-other { background: var(--gray-100); color: var(--gray-700); }
body.dark .calendar-job.type-repair { background: #1e3a5f; color: #93c5fd; }
body.dark .calendar-job.type-maintenance { background: #14532d; color: #6ee7b7; }
body.dark .calendar-job.type-installation { background: #451a03; color: #fcd34d; }
body.dark .calendar-job.type-inspection { background: #312e81; color: #a5b4fc; }
body.dark .calendar-job.type-emergency { background: #450a0a; color: #fca5a5; }
body.dark .calendar-job.type-service_call { background: #134e4a; color: #5eead4; }
body.dark .calendar-job.type-estimate { background: #3b0764; color: #c4b5fd; }
body.dark .calendar-job.type-other { background: #374151; color: #d1d5db; }

/* ===== Modal ===== */
.modal-overlay { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.6); z-index: 200; align-items: center; justify-content: center; }
.modal-overlay.active { display: flex; }
.modal { background: var(--modal-bg); border-radius: 12px; width: 560px; max-width: 95vw; max-height: 90vh; overflow: hidden; display: flex; flex-direction: column; box-shadow: var(--shadow-lg); transition: background 0.3s; color: var(--text-primary); }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border-color); }
.modal-header h3 { font-size: 16px; color: var(--text-primary); }
.modal-close { background: none; border: none; font-size: 18px; cursor: pointer; color: var(--text-muted); padding: 4px; }
.modal-close:hover { color: var(--text-primary); }
.modal-body { padding: 20px; overflow-y: auto; flex: 1; }
.modal-footer { padding: 12px 20px; border-top: 1px solid var(--border-color); display: flex; justify-content: flex-end; gap: 8px; }

/* ===== Empty State ===== */
.empty-state { text-align: center; padding: 40px 20px; color: var(--text-muted); }
.empty-state i { font-size: 48px; margin-bottom: 12px; }
.empty-state p { font-size: 14px; }

/* ===== Pages ===== */
.page { display: none; }
.page.active { display: block; }

/* ===== Action Buttons ===== */
.action-btns { display: flex; gap: 6px; align-items: center; }
.action-btn { display: inline-flex; align-items: center; justify-content: center; width: 32px; height: 32px; border: 1px solid var(--border-color); border-radius: 6px; cursor: pointer; color: var(--text-secondary); font-size: 14px; transition: all 0.15s; background: var(--bg-card); }
.action-btn:hover { transform: scale(1.1); }
.action-btn.view { border-color: var(--primary); color: var(--primary); }
.action-btn.view:hover { background: var(--primary); color: white; }
.action-btn.edit { border-color: var(--success); color: var(--success); }
.action-btn.edit:hover { background: var(--success); color: white; }
.action-btn.delete { border-color: var(--danger); color: var(--danger); }
.action-btn.delete:hover { background: var(--danger); color: white; }

/* ===== Tech Assign Column ===== */
.tech-select { padding: 4px 8px; font-size: 13px; min-width: 140px; }

/* ===== Chart Bars ===== */
.chart-bar { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; font-size: 13px; }
.chart-bar-label { width: 90px; text-transform: capitalize; color: var(--text-secondary); font-weight: 500; }
.chart-bar-track { flex: 1; height: 8px; background: var(--gray-200); border-radius: 4px; overflow: hidden; }
body.dark .chart-bar-track { background: #374151; }
.chart-bar-fill { height: 100%; border-radius: 4px; background: var(--primary); transition: width 0.3s; }
.chart-bar-value { font-weight: 600; color: var(--text-primary); min-width: 24px; text-align: right; }

/* ===== Detail View ===== */
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.detail-item { padding: 8px 0; }
.detail-label { font-size: 12px; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 2px; }
.detail-value { font-size: 14px; font-weight: 500; color: var(--text-primary); }
.detail-section { margin-bottom: 16px; padding-bottom: 16px; border-bottom: 1px solid var(--border-color); }
.detail-section:last-child { border-bottom: none; }

/* ===== Timeline ===== */
.timeline { position: relative; padding-left: 24px; }
.timeline::before { content: ''; position: absolute; left: 8px; top: 0; bottom: 0; width: 2px; background: var(--border-color); }
.timeline-item { position: relative; margin-bottom: 16px; }
.timeline-item::before { content: ''; position: absolute; left: -20px; top: 4px; width: 12px; height: 12px; border-radius: 50%; background: var(--primary); border: 2px solid var(--bg-card); box-shadow: var(--shadow); }
.timeline-item.event-started::before { background: var(--success); }
.timeline-item.event-on_my_way::before { background: #0891b2; }
.timeline-item.event-finished::before { background: var(--purple); }
.timeline-item.event-paused::before { background: var(--warning); }
.timeline-item.event-resumed::before { background: var(--success); }
.timeline-event { font-size: 13px; font-weight: 600; text-transform: capitalize; color: var(--text-primary); }
.timeline-time { font-size: 12px; color: var(--text-secondary); }

/* ===== Toast ===== */
.toast-container { position: fixed; bottom: 20px; right: 20px; z-index: 300; display: flex; flex-direction: column; gap: 8px; }
.toast { padding: 12px 20px; border-radius: var(--radius); background: var(--gray-800); color: white; font-size: 14px; box-shadow: var(--shadow-lg); animation: slideIn 0.3s ease; }
body.dark .toast { background: #e2e8f0; color: #0f172a; }
.toast.success { background: var(--success); color: white; }
.toast.error { background: var(--danger); color: white; }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ===== Responsive ===== */
@media (max-width: 900px) {
  .dashboard-grid { grid-template-columns: 1fr; }
  .form-row, .form-row-3 { grid-template-columns: 1fr; }
}
@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .main-content { margin-left: 0; }
  .menu-toggle { display: block; }
  .calendar-day { min-height: 60px; }
  .calendar-job { font-size: 10px; }
  .filters-bar { flex-direction: column; align-items: stretch; }
  .search-input { width: 100%; }
}

/* ===== Map ===== */
#mapContainer { width: 100%; height: 500px; z-index: 1; }
.leaflet-container { height: 100%; width: 100%; z-index: 1; }

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--gray-300); border-radius: 3px; }
body.dark ::-webkit-scrollbar-thumb { background: #475569; }

/* ===== Plans Grid ===== */
.plans-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 20px; }
.plan-card { background: var(--bg-card); border: 2px solid var(--border-color); border-radius: 12px; padding: 28px 24px; text-align: center; transition: all 0.3s; position: relative; }
.plan-card:hover { border-color: var(--primary); transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.plan-card.current { border-color: var(--success); }
.plan-card.current::after { content: 'Current Plan'; position: absolute; top: -12px; left: 50%; transform: translateX(-50%); background: var(--success); color: white; font-size: 11px; font-weight: 600; padding: 3px 12px; border-radius: 10px; }
.plan-card.popular { border-color: var(--primary); }
.plan-card.popular::before { content: 'Popular'; position: absolute; top: -12px; left: 50%; transform: translateX(-50%); background: var(--primary); color: white; font-size: 11px; font-weight: 600; padding: 3px 12px; border-radius: 10px; }
.plan-name { font-size: 18px; font-weight: 700; margin-bottom: 4px; color: var(--text-primary); }
.plan-price { font-size: 36px; font-weight: 700; color: var(--text-primary); margin: 12px 0; }
.plan-price span { font-size: 14px; font-weight: 400; color: var(--text-secondary); }
.plan-features { list-style: none; padding: 0; margin: 16px 0; text-align: left; }
.plan-features li { padding: 6px 0; font-size: 13px; color: var(--text-secondary); display: flex; align-items: center; gap: 8px; }
.plan-features li::before { content: '\f00c'; font-family: 'Font Awesome 6 Free'; font-weight: 900; color: var(--success); font-size: 12px; }
.plan-btn { width: 100%; padding: 12px; border: none; border-radius: 8px; font-size: 14px; font-weight: 600; cursor: pointer; background: var(--primary); color: white; transition: background 0.15s; }
.plan-btn:hover { background: var(--primary-dark); }
.plan-btn:disabled { background: var(--gray-400); cursor: default; }

/* ===== Estimate Items Table ===== */
.estimate-items-table { width: 100%; border-collapse: collapse; font-size: 13px; margin: 12px 0; }
.estimate-items-table th { text-align: left; padding: 6px 8px; font-size: 11px; text-transform: uppercase; color: var(--text-secondary); border-bottom: 1px solid var(--border-color); }
.estimate-items-table td { padding: 6px 8px; border-bottom: 1px solid var(--border-color); color: var(--text-primary); }
.estimate-items-table input { width: 100%; padding: 4px 6px; border: 1px solid var(--border-input); border-radius: 4px; font-size: 13px; background: var(--bg-input); color: var(--text-primary); }
.estimate-total { text-align: right; font-size: 18px; font-weight: 700; padding: 12px 0; color: var(--text-primary); }
