:root {
  --bg: #eef1f6;
  --panel: #ffffff;
  --border: #e5e8ef;
  --text: #1a2233;
  --text-muted: #6b7280;
  --text-faint: #9aa3b2;

  /* Nepal government color scheme -- flag blue + flag crimson */
  --primary: #003893;
  --primary-dark: #00256b;
  --primary-soft: #e5ebf7;
  --accent: #DC143C;
  --accent-soft: #fbe6ea;

  --danger: #DC143C;
  --danger-soft: #fbe6ea;
  --success: #1a9e5c;
  --success-soft: #e6f7ee;
  --warning: #c8850a;
  --warning-soft: #fdf3e0;
  --info: #3457d5;
  --info-soft: #eaefff;

  --sidebar-bg: #001f52;
  --sidebar-bg-2: #002d78;
  --sidebar-text: #a9b8d9;
  --sidebar-active-bg: #DC143C;

  --radius: 10px;
  --radius-sm: 7px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 4px 14px rgba(16, 24, 40, 0.08);
}

* { box-sizing: border-box; }

.main {
  animation: page-in 0.2s ease both;
}
.main.leaving {
  animation: page-out 0.12s ease both;
}

@keyframes page-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes page-out {
  from { opacity: 1; transform: translateY(0); }
  to { opacity: 0; transform: translateY(-4px); }
}

html, body { height: 100%; }

body {
  margin: 0;
  font-family: "Segoe UI", "Segoe UI Variable", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

.layout { display: flex; min-height: 100vh; }

/* ---------- Sidebar ---------- */

.sidebar {
  width: 234px;
  background: linear-gradient(180deg, var(--sidebar-bg) 0%, var(--sidebar-bg-2) 100%);
  color: var(--sidebar-text);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  padding: 18px 12px;
}

.sidebar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 10px 18px;
  margin-bottom: 10px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}

.sidebar .brand .mark {
  width: 36px;
  height: 36px;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.sidebar .brand .mark svg { width: 18px; height: 18px; stroke: #fff; }
.sidebar .brand .mark img { width: 100%; height: 100%; object-fit: contain; }

/* ---------- Official emblems (login/setup letterhead, PDF-style pairing) ---------- */

.emblem-row {
  display: flex;
  align-items: center;
  gap: 14px;
}
.emblem-row img { height: 48px; width: auto; }
.emblem-row .divider { width: 1px; height: 36px; background: var(--border); }

.emblem-watermark {
  position: absolute;
  opacity: 0.10;
  pointer-events: none;
}

.sidebar .brand .title { font-size: 14px; font-weight: 700; color: #fff; line-height: 1.25; }
.sidebar .brand .subtitle { font-size: 10.5px; color: var(--sidebar-text); }

.sidebar nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }

.sidebar a {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  color: var(--sidebar-text);
  text-decoration: none;
  font-size: 13.5px;
  font-weight: 500;
  transition: background 0.12s ease, color 0.12s ease;
}

.sidebar a svg { width: 17px; height: 17px; flex-shrink: 0; opacity: 0.85; }

.sidebar a:hover { background: rgba(255,255,255,0.06); color: #fff; }
.sidebar a.active { background: var(--sidebar-active-bg); color: #fff; box-shadow: var(--shadow-sm); }
.sidebar a.active svg { opacity: 1; }

.sidebar .footer-note {
  font-size: 11px;
  color: var(--text-faint);
  padding: 10px 12px 2px;
  border-top: 1px solid rgba(255,255,255,0.08);
  margin-top: 8px;
}

/* ---------- Main content ---------- */

.main { flex: 1; padding: 28px 36px 60px; max-width: 1240px; }

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 22px;
  gap: 16px;
  flex-wrap: wrap;
}

.page-header h1 { font-size: 21px; margin: 0; letter-spacing: -0.01em; }
.page-header .actions { display: flex; gap: 10px; flex-wrap: wrap; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  margin-bottom: 20px;
  box-shadow: var(--shadow-sm);
}

/* ---------- KPI cards ---------- */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}

.kpi-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.kpi-card .icon {
  width: 36px;
  height: 36px;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: var(--primary-soft);
  color: var(--primary);
}
.kpi-card .icon svg { width: 18px; height: 18px; }
.kpi-card.working .icon { background: var(--success-soft); color: var(--success); }
.kpi-card.notworking .icon { background: var(--danger-soft); color: var(--danger); }
.kpi-card.flickering .icon { background: var(--warning-soft); color: var(--warning); }
.kpi-card.repair .icon { background: var(--info-soft); color: var(--info); }
.kpi-card.accent .icon { background: var(--accent-soft); color: var(--accent); }

.kpi-card .body { min-width: 0; }
.kpi-card .label { font-size: 11.5px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.kpi-card .value { font-size: 22px; font-weight: 700; margin-top: 3px; }
.kpi-card.notworking .value { color: var(--danger); }
.kpi-card.working .value { color: var(--success); }

/* ---------- Tables ---------- */

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 14px; border-bottom: 1px solid var(--border); }
th { font-size: 11px; text-transform: uppercase; color: var(--text-muted); letter-spacing: .04em; font-weight: 700; background: #f8f9fc; }
th:first-child { border-top-left-radius: 8px; }
th:last-child { border-top-right-radius: 8px; }
tbody tr:hover td { background: #f6f8fd; }
tbody tr:last-child td { border-bottom: none; }
table a { color: var(--primary); text-decoration: none; font-weight: 600; }
table a:hover { text-decoration: underline; }

/* ---------- Badges ---------- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 11px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: .01em;
}
.badge.status-active { background: var(--success-soft); color: var(--success); }
.badge.status-inactive { background: var(--danger-soft); color: var(--danger); }
.badge.status-working { background: var(--success-soft); color: var(--success); }
.badge.status-flickering { background: var(--warning-soft); color: var(--warning); }
.badge.status-notworking { background: var(--danger-soft); color: var(--danger); }
.badge.status-damaged { background: var(--danger-soft); color: var(--danger); }
.badge.status-repair { background: var(--info-soft); color: var(--info); }

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--primary);
  background: var(--primary);
  color: #fff;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.12s ease, transform 0.05s ease;
}
.btn svg { width: 15px; height: 15px; }
.btn:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn:active { transform: translateY(1px); }
.btn.secondary { background: transparent; color: var(--primary); }
.btn.secondary:hover { background: var(--primary-soft); }
.btn.accent { background: var(--accent); border-color: var(--accent); }
.btn.accent:hover { background: #b5102f; border-color: #b5102f; }
.btn.danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn.danger:hover { background: var(--danger); color: #fff; }
.btn.small { padding: 5px 11px; font-size: 12px; }

form.inline { display: inline; }

/* ---------- Dropdown menu (Export button etc.) ---------- */

.dropdown { position: relative; display: inline-block; }
.dropdown-menu {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 220px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 6px;
  z-index: 20;
}
.dropdown-menu.open { display: block; }
.dropdown-menu a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  color: var(--text);
  text-decoration: none;
  font-size: 13.5px;
  font-weight: 500;
}
.dropdown-menu a:hover { background: var(--primary-soft); color: var(--primary); }
.dropdown-menu a svg { width: 16px; height: 16px; flex-shrink: 0; color: var(--text-muted); }
.dropdown-menu a:hover svg { color: var(--primary); }
.dropdown-menu .menu-note {
  padding: 6px 10px 4px;
  font-size: 11px;
  color: var(--text-faint);
  border-top: 1px solid var(--border);
  margin-top: 4px;
}

/* ---------- Forms ---------- */

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px 20px;
}
.form-grid .full { grid-column: 1 / -1; }

label { display: block; margin-bottom: 5px; font-size: 12.5px; font-weight: 600; color: var(--text-muted); }

input[type=text], input[type=number], input[type=date], input[type=email], input[type=tel], select, textarea {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
  background: #fbfbfd;
  color: var(--text);
  transition: border-color 0.12s ease, background 0.12s ease;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  background: #fff;
}
textarea { resize: vertical; min-height: 60px; }

.form-actions { margin-top: 20px; display: flex; gap: 10px; }

/* ---------- Empty states ---------- */

.empty-state {
  color: var(--text-muted);
  padding: 46px 20px;
  text-align: center;
}
.empty-state .icon {
  width: 46px;
  height: 46px;
  margin: 0 auto 12px;
  border-radius: 50%;
  background: var(--primary-soft);
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
}
.empty-state .icon svg { width: 22px; height: 22px; }
.empty-state a { color: var(--primary); font-weight: 600; text-decoration: none; }
.empty-state a:hover { text-decoration: underline; }

/* ---------- Misc ---------- */

.summary-row { display: flex; gap: 36px; margin-bottom: 4px; flex-wrap: wrap; }
.summary-row .item .label { font-size: 11.5px; color: var(--text-muted); text-transform: uppercase; font-weight: 600; letter-spacing: .03em; }
.summary-row .item .value { font-size: 19px; font-weight: 700; margin-top: 2px; }

.breadcrumb { font-size: 13px; color: var(--text-muted); margin-bottom: 10px; }
.breadcrumb a { color: var(--primary); text-decoration: none; }
.breadcrumb a:hover { text-decoration: underline; }

.section-title { font-size: 15px; font-weight: 700; margin: 0 0 14px; display: flex; align-items: center; gap: 8px; }

.toggle-row { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.toggle-row input[type=checkbox] { width: 17px; height: 17px; accent-color: var(--primary); }

/* ---------- Person avatar chips ---------- */

.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  color: #fff;
  font-size: 10.5px;
  font-weight: 700;
  flex-shrink: 0;
  letter-spacing: -0.02em;
}
.avatar-0 { background: var(--primary); }
.avatar-1 { background: var(--accent); }
.avatar-2 { background: var(--warning); }
.avatar-3 { background: #7c5cbf; }
.avatar-4 { background: #c04a72; }

code {
  background: #f2f4f9;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12.5px;
}

.alert {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  margin-bottom: 20px;
  font-size: 13.5px;
  font-weight: 600;
}
.alert svg { width: 17px; height: 17px; flex-shrink: 0; }
.alert.success { background: var(--success-soft); color: var(--success); }
.alert.error { background: var(--danger-soft); color: var(--danger); }

input[type=file] {
  width: 100%;
  padding: 8px 10px;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  background: #fbfbfd;
  font-size: 13.5px;
}

/* ---------- Geolocation tagging ---------- */

.geo-tag-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 12px 14px;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  background: #fbfbfd;
  margin-bottom: 16px;
}
.geo-tag-row .status { font-size: 12.5px; color: var(--text-muted); }
.geo-tag-row .status.ok { color: var(--success); font-weight: 600; }
.geo-tag-row .status.err { color: var(--danger); font-weight: 600; }

/* ---------- Map ---------- */

.map-container {
  height: 420px;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
}
.map-legend { display: flex; gap: 16px; flex-wrap: wrap; margin-top: 10px; font-size: 12px; color: var(--text-muted); }
.map-legend .dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; vertical-align: middle; }
.leaflet-popup-content b { color: var(--primary); }

/* ---------- Photo gallery ---------- */

.photo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
}
.photo-card {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: #fff;
}
.photo-card img { width: 100%; height: 120px; object-fit: cover; display: block; }
.photo-card .meta { padding: 8px 10px; font-size: 11px; color: var(--text-muted); }
.photo-card a { text-decoration: none; }

/* ---------- Timeline (inspection history) ---------- */

.timeline { border-left: 2px solid var(--border); margin-left: 6px; padding-left: 20px; }
.timeline-item { position: relative; padding-bottom: 22px; }
.timeline-item:last-child { padding-bottom: 0; }
.timeline-item::before {
  content: "";
  position: absolute;
  left: -25.5px;
  top: 3px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--primary);
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px var(--border);
}
.timeline-item .date { font-size: 11.5px; color: var(--text-muted); font-weight: 600; }
.timeline-item .desc { margin-top: 4px; font-size: 13.5px; }
.timeline-item .action { margin-top: 4px; font-size: 12.5px; color: var(--text-muted); font-style: italic; }

/* ---------- Mobile nav (top bar + off-canvas drawer) ---------- */

.mobile-topbar {
  display: none;
  align-items: center;
  gap: 12px;
  height: 54px;
  padding: 0 14px;
  background: linear-gradient(180deg, var(--sidebar-bg) 0%, var(--sidebar-bg-2) 100%);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 1001;
}

.hamburger-btn {
  background: none;
  border: none;
  color: #fff;
  padding: 6px;
  margin: -6px;
  display: flex;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.hamburger-btn svg { width: 22px; height: 22px; }

.mobile-topbar-brand { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 14px; }
.mobile-topbar-brand .mark {
  width: 28px;
  height: 28px;
  border-radius: 7px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  overflow: hidden;
}
.mobile-topbar-brand .mark svg { width: 15px; height: 15px; stroke: #fff; }
.mobile-topbar-brand .mark img { width: 100%; height: 100%; object-fit: contain; }

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(10, 14, 25, 0.5);
  z-index: 1002;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.sidebar-overlay.visible { opacity: 1; pointer-events: auto; }

@media (max-width: 860px) {
  .mobile-topbar { display: flex; }
  .sidebar-overlay { display: block; }

  .layout { flex-direction: column; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    /* 100vh is the LARGEST possible mobile viewport (address bar hidden) --
       using it alone can make this fixed drawer taller than what's
       actually visible once the browser chrome is showing, pushing "Log
       out" past the bottom of the screen. 100dvh tracks the real visible
       height as the browser chrome shows/hides; kept as a fallback since
       older mobile browsers don't support dvh. */
    height: 100vh;
    height: 100dvh;
    width: 78vw;
    max-width: 280px;
    z-index: 1003;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    overflow-y: auto;
    /* Without this, iOS Safari can fail to enable touch/drag scrolling
       inside a fixed-position container, making an overflowing sidebar
       feel "stuck" even though overflow-y: auto is set correctly. */
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }
  .sidebar.open { transform: translateX(0); box-shadow: var(--shadow-md); }

  body.drawer-open { overflow: hidden; }

  .main {
    max-width: 100%;
    padding: 16px 14px 40px;
  }

  .page-header { flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
  .page-header h1 { font-size: 18px; }

  .panel { padding: 16px; }

  .panel { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .panel table { min-width: 560px; }

  .form-grid { grid-template-columns: 1fr; }

  .kpi-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
  .summary-row { gap: 20px; }

  .btn { padding: 9px 15px; }

  .map-container { height: 300px; }
}

@media (max-width: 460px) {
  .kpi-grid { grid-template-columns: 1fr; }
}
