:root {
  --navy: #0f2438;
  --navy-light: #1c3a57;
  --accent: #2e7d32;
  --accent-dark: #1b5e20;
  --brand-orange: #e8630a;
  --brand-orange-dark: #b94f08;
  --government: #8e27ad;
  --houthi: #0e6859;
  --bg: #f4f6f8;
  --panel: #ffffff;
  --border: #dfe4e8;
  --text: #1b2733;
  --text-muted: #5c6b78;
  --danger: #c0392b;
  --radius: 10px;
  --shadow: 0 2px 10px rgba(15, 36, 56, 0.08);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

button { font-family: inherit; cursor: pointer; }

/* ---------- Password gate ---------- */

.gate {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, var(--navy) 0%, #0a1826 100%);
  padding: 16px;
  z-index: 50;
}

.gate[hidden] {
  display: none;
}

.gate-card {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0,0,0,0.35);
  padding: 40px 32px;
  width: 100%;
  max-width: 380px;
  text-align: center;
}

.gate-logo {
  height: 56px;
  margin-bottom: 16px;
}

.gate-card h1 {
  font-size: 1.3rem;
  margin: 0 0 4px;
}

.gate-subtitle {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin: 0 0 24px;
}

.gate-card input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  margin-bottom: 14px;
}

.gate-error {
  color: var(--danger);
  font-size: 0.85rem;
  margin: 12px 0 0;
}

.gate-links {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin: 14px 0 0;
}
.gate-links .link-btn { margin-inline-start: 0; }

.locale-switch { display: flex; gap: 4px; justify-content: center; margin-bottom: 12px; }
.locale-btn {
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-muted);
}
.locale-btn.active { background: var(--navy); color: #fff; border-color: var(--navy); }
.topbar .locale-switch { margin-bottom: 0; }
.topbar .locale-btn { background: transparent; border-color: rgba(255,255,255,0.25); color: #fff; }
.topbar .locale-btn.active { background: rgba(255,255,255,0.18); border-color: rgba(255,255,255,0.4); }

#gate-admin-fields[hidden], #gate-team-fields[hidden] { display: none; }

/* ---------- App shell ---------- */

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.app[hidden] {
  display: none;
}

.topbar {
  background: var(--navy);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 18px;
  flex-wrap: wrap;
  gap: 10px;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-logo {
  height: 36px;
}

.brand-text h1 {
  font-size: 1.05rem;
  margin: 0;
  font-weight: 700;
}

.brand-subtitle {
  font-size: 0.72rem;
  color: #b7c6d3;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.updated-info {
  font-size: 0.75rem;
  color: #b7c6d3;
  margin-inline-end: 6px;
}

.toolbar {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 10px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.edit-hint {
  font-size: 0.78rem;
  color: var(--text-muted);
  max-width: 520px;
}

.btn {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 9px 16px;
  font-size: 0.85rem;
  font-weight: 600;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }

.btn-success { background: #1565c0; color: #fff; }
.btn-success:hover { background: #0d47a1; }

.btn-outline {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}
.btn-outline:hover { border-color: var(--navy); }

.btn-ghost {
  background: transparent;
  color: inherit;
  border-color: rgba(255,255,255,0.25);
}
.topbar .btn-ghost:hover { background: rgba(255,255,255,0.1); }

.btn-accent { background: var(--brand-orange); color: #fff; }
.btn-accent:hover { background: var(--brand-orange-dark); }

.btn-sm { padding: 6px 12px; font-size: 0.72rem; flex-shrink: 0; }

.btn-block { width: 100%; }

.gate-card .btn { font-size: 1rem; padding: 12px; }

/* ---------- Workspace ---------- */

.workspace {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

.sidebar {
  width: 300px;
  flex-shrink: 0;
  background: var(--panel);
  border-inline-end: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
}

.sidebar-tab {
  flex: 1;
  border: none;
  background: transparent;
  padding: 12px 8px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 2px solid transparent;
}

.sidebar-tab.active {
  color: var(--navy);
  border-bottom-color: var(--accent);
}

.pill {
  display: inline-block;
  background: var(--border);
  color: var(--text);
  border-radius: 10px;
  font-size: 0.68rem;
  padding: 1px 7px;
  margin-inline-start: 4px;
}

.sidebar-panel {
  display: none;
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.sidebar-panel.active { display: flex; flex-direction: column; gap: 12px; }

.legend-item { display: block; }

.legend-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.85rem;
  cursor: pointer;
}

.swatch {
  width: 16px;
  height: 16px;
  border-radius: 3px;
  display: inline-block;
  flex-shrink: 0;
  border: 1px solid rgba(0,0,0,0.15);
}

.swatch-government { background: var(--government); }
.swatch-houthi { background: var(--houthi); }
.swatch-districts {
  background: transparent;
  border: 1.5px dashed var(--text-muted);
}
.swatch-points {
  background: var(--navy);
  border-radius: 50%;
}
.swatch-round { border-radius: 50%; }

.points-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
  flex-wrap: wrap;
}

.legend-item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.legend-item-row .legend-toggle { flex: 1; min-width: 0; }

.point-groups-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 8px;
  padding-inline-start: 4px;
  border-inline-start: 2px solid var(--border);
}

.point-group-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-block: 4px;
  padding-inline-start: 8px;
}

.point-group-row .legend-toggle {
  flex: 1;
  min-width: 0;
  font-size: 0.8rem;
}
.point-group-row .legend-toggle span:not(.swatch) {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.point-group-actions {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}

.icon-btn {
  width: 26px;
  height: 26px;
  flex-shrink: 0;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 6px;
  font-size: 0.85rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
}
.icon-btn:hover { border-color: var(--navy); color: var(--navy); }

.style-swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.style-swatch {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
}
.style-swatch.selected { border-color: var(--navy); }

.field-label input[type="color"] {
  padding: 2px;
  height: 36px;
  cursor: pointer;
}

.sidebar-divider {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

.opacity-label {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.sidebar-help {
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.5;
}

.zone-context {
  background: #eef3f7;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.78rem;
}

.link-btn {
  background: none;
  border: none;
  color: #1565c0;
  font-size: 0.75rem;
  padding: 0;
  margin-inline-start: 6px;
  text-decoration: underline;
}

.note-form textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-family: inherit;
  font-size: 0.85rem;
  resize: vertical;
  margin-bottom: 8px;
}

.notes-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.note-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 0.82rem;
  background: #fbfcfd;
}

.note-card-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.author-badge {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--navy);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 700;
  flex-shrink: 0;
}

.note-author-name {
  font-weight: 700;
  font-size: 0.78rem;
  color: var(--text);
}

.note-time {
  font-size: 0.68rem;
  color: var(--text-muted);
  margin-inline-start: auto;
  white-space: nowrap;
}

.note-card-zone {
  display: inline-block;
  font-size: 0.68rem;
  color: var(--navy);
  background: #eef3f7;
  border-radius: 6px;
  padding: 1px 6px;
  margin-bottom: 4px;
}

.note-card-text { white-space: pre-wrap; word-break: break-word; }

.note-delete {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.7rem;
  text-decoration: underline;
  padding: 0;
  margin-top: 6px;
}
.note-delete:hover { color: var(--danger); }

.notes-empty {
  color: var(--text-muted);
  font-size: 0.8rem;
  text-align: center;
  padding: 20px 0;
}

.history-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  background: #fbfcfd;
  display: flex;
  align-items: center;
  gap: 10px;
}

.history-card-info {
  flex: 1;
  min-width: 0;
}

.history-card-change {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.map-wrap {
  flex: 1;
  position: relative;
}

#map {
  position: absolute;
  inset: 0;
}

/* Popup styling for zone info */
.zone-popup h3 {
  margin: 0 0 4px;
  font-size: 0.95rem;
}
.zone-popup .zone-popup-force {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 8px;
}
.zone-popup button {
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 0.75rem;
}
.zone-popup button.btn-accent {
  background: var(--brand-orange);
  color: #fff;
  border-color: transparent;
  width: 100%;
  padding: 7px 10px;
  font-weight: 600;
}
.zone-popup button.btn-accent:hover { background: var(--brand-orange-dark); }

.quick-note-textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font-family: inherit;
  font-size: 0.8rem;
  resize: vertical;
  margin-bottom: 6px;
  box-sizing: border-box;
}

.zone-popup-links {
  display: flex;
  gap: 10px;
  margin-top: 8px;
}
.zone-popup-links button {
  border: none;
  background: none;
  padding: 0;
  color: #1565c0;
  text-decoration: underline;
  font-size: 0.72rem;
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--navy);
  color: #fff;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 0.85rem;
  box-shadow: var(--shadow);
  /* Above the mobile bottom-sheet sidebar (z-index 1200) so a toast fired
     while it's expanded (e.g. after saving a note) is still visible. */
  z-index: 1300;
}
.toast.error { background: var(--danger); }

/* ---------- Confirm modal (replaces browser confirm()) ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 36, 56, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  /* Same reasoning as .toast above — must clear the mobile bottom sheet. */
  z-index: 1300;
  padding: 16px;
  transform: translateZ(0);
}

.modal-overlay[hidden] { display: none; }

.modal-card {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0,0,0,0.35);
  padding: 24px;
  width: 100%;
  max-width: 380px;
}

.modal-card h2 {
  margin: 0 0 8px;
  font-size: 1.05rem;
}

.modal-card p {
  margin: 0 0 20px;
  font-size: 0.85rem;
  color: var(--text-muted);
  line-height: 1.5;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.modal-actions .btn { padding: 8px 16px; }

.modal-card input[type="text"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.9rem;
  margin-bottom: 18px;
  font-family: inherit;
}

.modal-card-wide { max-width: 620px; }

.field-row {
  display: flex;
  gap: 12px;
  margin-bottom: 4px;
}

.field-row-3 { flex-wrap: wrap; }
.field-row-3 .field-label { flex: 1 1 160px; }

.field-label {
  display: block;
  flex: 1;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 14px;
}

.field-label input,
.field-label select {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.9rem;
  font-family: inherit;
  font-weight: 400;
  color: var(--text);
  background: #fff;
}

.modal-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin: 0 0 12px;
}

.csv-preview-wrap {
  max-height: 220px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 12px;
}

.csv-preview {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.75rem;
}

.csv-preview th, .csv-preview td {
  text-align: start;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.csv-preview th {
  background: #f4f6f8;
  position: sticky;
  top: 0;
}

/* ---------- Note map markers ---------- */

.note-marker-pin {
  width: 26px;
  height: 26px;
  background: var(--brand-orange);
  border: 2px solid #fff;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  box-shadow: 0 2px 6px rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
}

.note-marker-pin span {
  transform: rotate(45deg);
  font-size: 12px;
  line-height: 1;
}

/* ---------- Point markers ---------- */

.point-marker {
  display: flex;
  align-items: center;
  gap: 5px;
  pointer-events: none;
}

.point-marker-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--navy);
  border: 2px solid #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.5);
  flex-shrink: 0;
  pointer-events: auto;
}

.point-marker-label {
  background: rgba(255,255,255,0.92);
  color: var(--navy);
  font-size: 0.72rem;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 5px;
  border: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(0,0,0,0.15);
  white-space: nowrap;
  pointer-events: auto;
}

/* ---------- Responsive ---------- */

/* Tablets (portrait iPad and similar, ~600-1024px): keep the desktop
   side-by-side layout — there's room for it — just trim the sidebar a
   little and tighten spacing so more of the map stays visible. */
@media (max-width: 1024px) {
  .sidebar { width: 280px; }
  .topbar { padding: 8px 14px; }
  .toolbar { padding: 8px 14px; }
  .icon-btn { width: 30px; height: 30px; }
  .leaflet-touch .leaflet-bar a { width: 32px; height: 32px; line-height: 32px; }
}

/* Phones (~680px and below): the sidebar sits above the map (same
   document order as tablet/desktop, just stacked instead of side by
   side) and collapses to just its tab strip by default, so the map gets
   most of the screen instead of losing 42% of it to a panel nobody
   opened yet. Deliberately kept in normal document flow rather than
   position:fixed/absolute — those anchor against the *layout* viewport,
   which on phones (Safari's collapsing address bar especially) can be
   taller than what's actually on screen, silently pushing a
   bottom/top-anchored panel below the fold. Plain flexbox has no such
   failure mode: the browser can't get "how much space is left" wrong.
   Every touch target also grows a bit. */
@media (max-width: 680px) {
  .workspace { flex-direction: column; }
  .sidebar {
    width: 100%;
    flex-shrink: 0;
    border-inline-end: none;
    border-bottom: 1px solid var(--border);
  }
  .sidebar:not(.collapsed) {
    max-height: 55vh;
    max-height: 55dvh;
  }
  .sidebar::before {
    content: "";
    display: block;
    flex-shrink: 0;
    width: 36px;
    height: 4px;
    margin: 8px auto 0;
    background: var(--border);
    border-radius: 2px;
  }
  .sidebar.collapsed .sidebar-panel { display: none !important; }

  .brand-subtitle { display: none; }
  .edit-hint { display: none; }

  .topbar-brand { gap: 8px; }
  .brand-logo { height: 28px; }
  .brand-text h1 { font-size: 0.92rem; }

  .toolbar { gap: 8px; }
  .toolbar-group { gap: 8px; }

  .btn { padding: 10px 14px; font-size: 0.85rem; }
  .btn-sm { padding: 8px 12px; }
  .icon-btn { width: 32px; height: 32px; }

  .sidebar-tab { padding: 12px 6px; font-size: 0.78rem; }

  .field-row { flex-direction: column; gap: 0; }
  .field-row-3 .field-label { flex-basis: 100%; }

  .modal-card { padding: 20px; max-height: 88vh; overflow-y: auto; }

  .legend-toggle { font-size: 0.82rem; }

  /* Leaflet's default zoom buttons (~26px) are cramped for touch. */
  .leaflet-touch .leaflet-bar a { width: 34px; height: 34px; line-height: 34px; }
}

@media (max-width: 400px) {
  .updated-info { display: none; }
  .brand-text h1 { font-size: 0.85rem; }
}
