:root {
  --color-primary: #2d8659;
  --color-primary-dark: #1a5c3a;
  --color-primary-light: #4db380;
  --color-hover-bg: #e8f4e8;
  --color-accent-bg: #f0faf5;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: #f5f5f5;
  color: #333;
  user-select: none;
  -webkit-user-select: none;
}
input, textarea, select, .selectable { user-select: text; -webkit-user-select: text; }

/* The `hidden` attribute must always win over component display rules
   (e.g. .app-container/.gallery-overlay set display:flex). Without this,
   hidden overlays stay visible. */
[hidden] { display: none !important; }

.muted { color: #999; }

/* ─── Login ─────────────────────────────────────────── */
.login-container {
  position: fixed;
  inset: 0;
  background: linear-gradient(135deg, var(--color-primary-dark) 0%, var(--color-primary) 50%, var(--color-primary-light) 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10000;
}
.login-box {
  background: white;
  padding: 40px 25px;
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.3);
  width: 90%;
  max-width: 435px;
}
.login-header { text-align: center; margin-bottom: 30px; }
.login-logo {
  width: 120px; height: 120px; margin: 0 auto 18px;
  background: linear-gradient(135deg, var(--color-primary), var(--color-primary-light));
  color: white; border-radius: 16px;
  display: flex; align-items: center; justify-content: center;
  font-size: 42px; font-weight: 800; letter-spacing: 2px;
  box-shadow: 0 6px 18px rgba(45,134,89,0.35);
}
.login-header h1 { font-size: 20px; color: var(--color-primary); margin-bottom: 10px; }
.login-header p { font-size: 14px; color: #666; }
.login-form { display: flex; flex-direction: column; gap: 20px; }
.form-group { display: flex; flex-direction: column; gap: 8px; }
.form-group label { font-size: 14px; font-weight: 600; color: #333; }
.form-group input {
  padding: 12px 15px; border: 2px solid #e0e0e0; border-radius: 8px;
  font-size: 14px; transition: border-color 0.3s;
}
.form-group input:focus { outline: none; border-color: var(--color-primary); }
.login-btn {
  background: var(--color-primary); color: white; border: none;
  padding: 14px; border-radius: 8px; font-size: 16px; font-weight: 600;
  cursor: pointer; transition: background 0.3s;
}
.login-btn:hover { background: var(--color-primary-dark); }
.login-btn:disabled { background: #ccc; cursor: not-allowed; }
.login-error {
  background: #ffebee; color: #c62828; padding: 12px; border-radius: 8px;
  font-size: 14px; text-align: center; display: none;
}
.login-error.show { display: block; }

/* ─── App shell / header ────────────────────────────── */
.app-container { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }

.header {
  background: linear-gradient(to right, var(--color-primary-dark), #209b58);
  color: white;
  padding: 0.8rem 1.4rem;
  display: flex;
  align-items: center;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  gap: 20px;
  position: relative;
  min-height: 60px;
  overflow: visible;
  flex-shrink: 0;
}

.header-menu-wrapper { position: relative; z-index: 1000; }
.header-menu-btn {
  background: none; border: none; cursor: pointer; padding: 0 10px;
  margin: -4px -9px; border-radius: 4px; transition: background 0.2s;
  color: white; font-size: 40px; line-height: 1;
}
.header-menu-btn:hover { background: rgba(255,255,255,0.15); }
.header-menu-dropdown {
  display: none; position: absolute; top: calc(100% + 8px); left: 0;
  background: white; border-radius: 8px; box-shadow: 0 4px 20px rgba(0,0,0,0.2);
  min-width: 200px; overflow: hidden;
}
.header-menu-dropdown.open { display: block; }
.header-menu-user {
  padding: 14px 16px; display: flex; align-items: center; gap: 10px;
  color: #333; font-weight: 600; font-size: 14px;
}
.header-menu-user-icon { font-size: 18px; }
.header-menu-divider { height: 1px; background: #e5e5e5; }
.header-menu-item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 12px 16px; border: none; background: none; cursor: pointer;
  font-size: 14px; color: #000; transition: background 0.15s; text-align: left;
}
.header-menu-item.header-menu-status { cursor: default; color: #555; }
.header-menu-item.header-menu-status:hover { background: none; }
.header-menu-item:hover { background: #f5f5f5; }
.header-menu-item-icon { font-size: 16px; width: 20px; text-align: center; display: flex; align-items: center; justify-content: center; }

.header-logo {
  margin: 0 0.5rem; height: 48px; min-width: 64px; padding: 0 12px;
  border-radius: 8px; flex-shrink: 0;
  background: rgba(255,255,255,0.16); color: white;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px; font-weight: 800; letter-spacing: 1px;
  user-select: none; -webkit-user-select: none;
}

.header-content { flex: 1; min-width: 0; }
.header h1 { font-size: clamp(16px, 2vw, 25px); margin-bottom: 3px; line-height: 1.2; cursor: default; }
.header p { font-size: 12px; opacity: 0.9; cursor: default; }

.view-toggle { display: flex; gap: 0.8rem; margin-left: auto; align-items: center; flex-shrink: 0; }
.toggle-btn {
  background: rgba(255,255,255,0.2); color: white; border: 2px solid white;
  padding: 10px 18px; border-radius: 8px; cursor: pointer;
  font-size: 14px; font-weight: 600; transition: all 0.3s; white-space: nowrap;
  display: inline-flex; align-items: center; gap: 4px;
}
.toggle-btn:hover { background: rgba(255,255,255,0.3); }
.toggle-btn.active { background: white; color: var(--color-primary); }
.toggle-badge {
  background: #dc3545; color: #fff; border-radius: 10px;
  padding: 0 6px; font-size: 11px; font-weight: 700; line-height: 18px; min-width: 18px; text-align: center;
}
.ws-dot { width: 9px; height: 9px; border-radius: 50%; background: #bbb; display: inline-block; }
.ws-dot.live { background: #2e7d32; }

/* ─── Map view layout ───────────────────────────────── */
.container { position: relative; flex: 1; min-height: 0; }

.stats-panel {
  width: 272px; background: white; overflow: hidden;
  box-shadow: 2px 0 10px rgba(0,0,0,0.1); transition: transform 0.3s ease;
  position: absolute; top: 0; left: 0; bottom: 0; z-index: 500;
  display: flex; flex-direction: column;
}
.stats-panel.collapsed { transform: translateX(-100%); }

.panel-toggle-btn {
  position: absolute; top: 8px; left: 272px; z-index: 501;
  width: 14px; height: 36px; background: white; border: none;
  border-radius: 0 6px 6px 0; box-shadow: 1px 2px 2px rgba(0,0,0,0.38);
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  font-size: 11px; color: #555; transition: left 0.3s ease; padding: 0;
}
.panel-toggle-btn:hover { background: var(--color-primary); color: white; }

.map-panel { width: 100%; height: 100%; position: relative; }
#map { width: 100%; height: 100%; background: #4a5a4a; }
.leaflet-container { background: #4a5a4a !important; }
.leaflet-control-attribution, .leaflet-control-zoom { display: none !important; }

.leaflet-tile { border: none !important; }

/* Legend */
.legend { padding: 8px 5px 2px; display: flex; justify-content: center; flex-shrink: 0; }
.legend-items { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px 14px; width: 100%; }
.legend-item { display: flex; flex-direction: column; align-items: center; font-size: 10px; font-weight: 600; }
.legend-color { width: 12px; height: 12px; border-radius: 50%; margin-bottom: 4px; border: 2px solid #333; }

.total-fleet-bar {
  padding: 8px 20px; margin: 6px 0 0; font-size: 13px; font-weight: 600;
  color: #333; border-top: 1px solid #e0e0e0; border-bottom: 1px solid #e0e0e0;
  text-align: center; flex-shrink: 0;
}
.total-fleet-bar span { font-weight: bold; color: var(--color-primary); font-size: 16px; }

.search-filter-section {
  margin: 0; padding: 15px; background: white;
  flex: 1; display: flex; flex-direction: column; min-height: 0;
}
.search-filter-section h3 { font-size: 14px; margin-bottom: 10px; color: var(--color-primary); text-align: center; }

.search-box {
  width: 100%; padding: 8px 12px; border: 2px solid #e0e0e0;
  border-radius: 6px; font-size: 13px; transition: border-color 0.3s;
}
.search-box:focus { outline: none; border-color: var(--color-primary); }

.filter-row { display: flex; flex-direction: column; gap: 10px; margin-top: 12px; }
.filter-item { display: flex; flex-direction: column; gap: 3px; }
.filter-item label { font-size: 12px; font-weight: 600; color: #666; }
.filter-item select {
  padding: 8px 10px; border: 2px solid #e0e0e0; border-radius: 6px;
  font-size: 13px; cursor: pointer; background: white;
}
.filter-item select:hover, .filter-item select:focus { outline: none; border-color: var(--color-primary); }

.clear-filters-btn {
  margin-top: 12px; width: 100%; padding: 8px; background: #e0e0e0;
  border: none; border-radius: 6px; cursor: pointer; font-size: 12px;
  font-weight: 600; color: #666; flex-shrink: 0;
}
.clear-filters-btn:hover { color: #000; }

.map-count { text-align: center; font-size: 12px; color: #999; font-weight: 600; margin-top: 8px; flex-shrink: 0; }

.stats-scroll { margin-top: 10px; overflow-y: auto; flex: 1; min-height: 0; }

.stat-card { padding: 12px 0; }
.stat-card h2 {
  font-size: 15px; margin-bottom: 10px; color: var(--color-primary);
  border-bottom: 2px solid var(--color-primary); padding-bottom: 6px; text-align: center;
}
.stat-item {
  display: flex; justify-content: space-between; padding: 7px 4px;
  border-bottom: 1px solid #f0f0f0; transition: background 0.2s; cursor: pointer;
}
.stat-item:hover { background: var(--color-hover-bg); }
.stat-label { font-weight: 500; display: flex; align-items: center; gap: 6px; }
.stat-value { font-weight: bold; color: var(--color-primary); }
.stat-unit-list {
  max-height: 250px; overflow-y: auto; border: 1px solid #e0e0e0;
  border-radius: 6px; margin: 4px 0 8px; background: #fafafa;
}
.stat-unit-list .unit-search {
  width: 100%; padding: 6px 10px; border: none; border-bottom: 1px solid #e0e0e0;
  font-size: 12px; outline: none; background: white;
}
.stat-unit-list .unit-row {
  padding: 5px 10px; font-size: 11px; cursor: pointer; display: flex;
  justify-content: space-between; align-items: center; gap: 6px;
  border-bottom: 1px solid #f0f0f0;
}
.stat-unit-list .unit-row:hover { background: var(--color-hover-bg); }
.stat-unit-list .unit-row .unit-row-name { display: flex; align-items: center; gap: 5px; min-width: 0; }
.stat-unit-list .unit-row .unit-row-name span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ─── Marker labels / popups ────────────────────────── */
.leaflet-tooltip.marker-label {
  background: rgba(0,0,0,0.704); border: none; box-shadow: none;
  font-size: 12px; font-weight: bold; color: #fff; text-align: center;
  padding: 3px 5px; white-space: nowrap; line-height: 1; margin: 0;
}
.leaflet-tooltip.marker-label::before { display: none !important; }
.leaflet-tooltip-bottom::before { display: none !important; }

.leaflet-popup-content { margin: 10px 12px; line-height: 1.6; font-size: 13px; min-width: 230px; }
.popup-title { font-weight: 700; font-size: 14px; }
.popup-sub { color: #555; margin-bottom: 6px; }
.popup-row { font-size: 13px; margin: 2px 0; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.popup .dot, .popup-row .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.popup-row a { color: var(--color-primary); text-decoration: none; }
.popup-row a:hover { text-decoration: underline; }
.popup-h {
  font-weight: 700; margin-top: 8px; font-size: 11px;
  text-transform: uppercase; letter-spacing: 0.04em; color: var(--color-primary);
}
.popup-list { margin: 4px 0; padding-left: 16px; font-size: 13px; }
.popup-list .sev-critical { color: #c0392b; font-weight: 600; }
.popup-list .sev-warning { color: #b9770e; }
.popup-list .muted { color: #888; }
.popup-photos { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.popup-photos .thumb { width: 48px; height: 48px; }

/* Cluster styling (uniform green) */
.marker-cluster-small, .marker-cluster-medium, .marker-cluster-large {
  background-color: rgba(45,134,89,0.4) !important;
}
.marker-cluster-small div, .marker-cluster-medium div, .marker-cluster-large div {
  background-color: rgba(45,134,89,0.85) !important; color: white !important; font-weight: bold !important;
}

/* Cluster list panel */
.cluster-list-panel {
  position: absolute; min-width: 180px; max-width: 300px; max-height: 400px;
  background: white; border-radius: 10px; box-shadow: 0 4px 20px rgba(0,0,0,0.3);
  z-index: 1000; display: none; flex-direction: column; overflow: hidden;
}
.cluster-list-panel.visible { display: flex; }
.cluster-list-header {
  padding: 10px 14px; background: var(--color-primary); color: white;
  font-weight: bold; font-size: 13px; display: flex;
  justify-content: space-between; align-items: center;
}
.cluster-list-header .close-btn { cursor: pointer; font-size: 18px; line-height: 1; opacity: 0.8; }
.cluster-list-header .close-btn:hover { opacity: 1; }
.cluster-list-body { overflow-y: auto; max-height: 350px; }
.cluster-list-item {
  padding: 8px 14px; border-bottom: 1px solid #f0f0f0; cursor: pointer;
  display: flex; align-items: center; gap: 8px; font-size: 12px; transition: background 0.15s;
}
.cluster-list-item:hover { background: var(--color-hover-bg); }
.cluster-list-item .unit-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.cluster-list-item .unit-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ─── Analytics view ────────────────────────────────── */
.analytics-container {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  padding: 15px 20px; background: #f9f9f9; min-height: 0;
}
.summary-cards {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(min(180px,100%),1fr));
  gap: 10px; margin-bottom: 12px;
}
.summary-card { background: white; padding: 10px 16px; border-radius: 10px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
.summary-card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 2px; }
.summary-card-title { font-size: 13px; color: #666; font-weight: 600; text-transform: uppercase; }
.summary-card-icon { font-size: 22px; }
.summary-card-value { font-size: 34px; font-weight: bold; color: var(--color-primary); line-height: 1.1; }

.charts-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(min(460px,100%),1fr));
  gap: 20px; margin-bottom: 12px;
}
.chart-container { background: white; padding: 15px 20px; border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
.chart-title { font-size: 17px; font-weight: 600; color: #333; margin-bottom: 16px; }
.chart-canvas { position: relative; height: 300px; }

.filter-section { background: white; padding: 14px 20px; border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); margin-bottom: 12px; }
.filter-header { font-size: 17px; font-weight: 600; color: #333; margin-bottom: 10px; }
.filter-controls { display: grid; grid-template-columns: 1.6fr 1fr 1fr auto; gap: 10px; }
.filter-group { display: flex; flex-direction: column; gap: 4px; }
.filter-label { font-size: 13px; font-weight: 600; color: #666; }
.filter-select, .analytics-search-box {
  padding: 8px 12px; border: 2px solid #e0e0e0; border-radius: 8px;
  font-size: 14px; background: white; cursor: pointer; box-sizing: border-box; width: 100%;
}
.analytics-search-box { cursor: text; }
.filter-select:focus, .analytics-search-box:focus { outline: none; border-color: var(--color-primary); }
.filter-btn {
  background: #e0e0e0; color: #666; border: none; padding: 8px 20px;
  border-radius: 8px; cursor: pointer; font-size: 14px; font-weight: 600; align-self: flex-end;
}
.filter-btn:hover { background: #d0d0d0; color: #000; }

.filtered-results { background: white; padding: 15px 0 20px; border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
.results-header { display: flex; justify-content: center; align-items: center; margin-bottom: 8px; }
.results-count { font-size: 17px; font-weight: bold; color: var(--color-primary); }

.table-wrap { overflow-x: auto; }
.results-table { width: 100%; border-collapse: collapse; }
.results-table th {
  padding: 8px 12px; text-align: left; font-weight: 600; color: #000;
  border-bottom: 2px solid #d0d0d0; background: white; position: sticky; top: 0;
}
.results-table td { padding: 7px 12px; border-bottom: 1px solid #eee; text-align: left; }
.results-table td.selectable { user-select: text; -webkit-user-select: text; }
.results-table tr:hover td { background: #f5f5f5; }
.type-badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 12px; font-weight: 600; color: #222; }
.pill-alert { background: #dc3545; color: #fff; border-radius: 10px; padding: 1px 8px; font-size: 12px; font-weight: 600; }

/* ─── Alerts view ───────────────────────────────────── */
.alerts-container { flex: 1; overflow-y: auto; padding: 24px; background: #f9f9f9; min-height: 0; }
.alerts-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.alerts-head h2 { font-size: 20px; font-weight: 700; color: #333; }
.sound-toggle { font-size: 13px; color: #555; display: flex; gap: 6px; align-items: center; }
.alerts-list { display: flex; flex-direction: column; gap: 10px; }
.alerts-empty { text-align: center; color: #999; font-size: 14px; padding: 40px 0; background: white; border-radius: 10px; }
.alert-item {
  display: flex; align-items: center; justify-content: space-between; padding: 14px 18px;
  background: white; border: 1px solid #e8e8e8; border-left: 5px solid #999;
  border-radius: 10px; box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}
.alert-item.sev-warning { border-left-color: #f0ad4e; }
.alert-item.sev-critical { border-left-color: #dc3545; }
.alert-main { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.alert-type { font-weight: 700; text-transform: capitalize; color: #333; }
.alert-sev {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em;
  padding: 2px 8px; border-radius: 10px; background: #f0f0f0; color: #666; font-weight: 600;
}
.alert-item.sev-warning .alert-sev { background: #fff3cd; color: #856404; }
.alert-item.sev-critical .alert-sev { background: #fde8e8; color: #dc3545; }
.alert-dev { color: #777; font-size: 14px; }
.alert-actions { display: flex; gap: 8px; }

/* ─── Manage / admin ────────────────────────────────── */
.manage-container { flex: 1; overflow-y: auto; padding: 24px; background: #f9f9f9; min-height: 0; }
.subtabs { display: flex; gap: 8px; margin-bottom: 18px; flex-wrap: wrap; }
.subtab {
  background: white; color: #555; border: 1px solid #e0e0e0; padding: 8px 16px;
  border-radius: 8px; cursor: pointer; font-size: 14px; font-weight: 500;
}
.subtab.active { background: var(--color-primary); border-color: var(--color-primary); color: #fff; }
.admin-form {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 16px;
  background: white; padding: 14px; border-radius: 10px; box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}
.state-pill { font-size: 12px; padding: 2px 8px; border-radius: 10px; background: #f0f0f0; color: #555; }
.data-table { width: 100%; border-collapse: collapse; font-size: 14px; background: white; }
.data-table th, .data-table td { text-align: left; padding: 9px 12px; border-bottom: 1px solid #eee; }
.data-table th { background: #f5f5f5; color: #555; font-weight: 600; position: sticky; top: 0; }
.data-table tr:hover td { background: #fafafa; }
.manage-container .table-wrap {
  border: 1px solid #e8e8e8; border-radius: 10px; overflow: auto; box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

/* Shared fields/buttons for admin + manage */
.field {
  padding: 8px 12px; border-radius: 8px; border: 2px solid #e0e0e0;
  background: white; color: #333; font-size: 14px;
}
.field.small { padding: 7px 10px; font-size: 13px; }
.field:focus { outline: none; border-color: var(--color-primary); }
.btn-primary {
  padding: 8px 16px; border: none; border-radius: 8px; background: var(--color-primary);
  color: #fff; font-weight: 600; cursor: pointer; font-size: 14px;
}
.btn-primary:hover { background: var(--color-primary-dark); }
.btn-mini {
  padding: 5px 12px; font-size: 13px; border: 1px solid #ddd; border-radius: 6px;
  background: white; color: #444; cursor: pointer;
}
.btn-mini:hover { background: var(--color-hover-bg); border-color: var(--color-primary); color: var(--color-primary); }

/* ─── Photos ────────────────────────────────────────── */
.thumb { width: 56px; height: 56px; object-fit: cover; border-radius: 6px; border: 1px solid #e0e0e0; background: #f0f0f0; cursor: pointer; }
.photo-mgr { margin-top: 16px; padding: 14px; background: white; border: 1px solid #e8e8e8; border-radius: 10px; box-shadow: 0 2px 8px rgba(0,0,0,0.06); }
.pm-bar { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.pm-thumbs { display: flex; flex-wrap: wrap; gap: 8px; }
.pm-thumb { position: relative; }
.pm-thumb .pm-del {
  position: absolute; top: -6px; right: -6px; padding: 0 6px; border-radius: 50%;
  background: #dc3545; color: #fff; border: none; cursor: pointer; line-height: 1.4;
}

/* ─── Gallery / lightbox (view-only) ────────────────── */
.gallery-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.92); display: flex; flex-direction: column; z-index: 10003; }
.gallery-close {
  position: absolute; top: 24px; right: 24px; background: rgba(255,255,255,0.18);
  border: none; color: white; font-size: 28px; width: 48px; height: 48px;
  border-radius: 50%; cursor: pointer; z-index: 20; line-height: 1;
  display: flex; align-items: center; justify-content: center;
}
.gallery-close:hover { background: rgba(255,255,255,0.35); }
.gallery-main-area { flex: 1; display: flex; align-items: center; justify-content: center; width: 100%; padding: 60px 40px; min-height: 0; }
.gallery-image-container { flex: 1; display: flex; align-items: center; justify-content: center; height: 100%; min-height: 0; }
.gallery-image-container img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; }

@keyframes spin { to { transform: rotate(360deg); } }
.spinner { border: 4px solid #f3f3f3; border-top: 4px solid var(--color-primary); border-radius: 50%; width: 36px; height: 36px; animation: spin 1s linear infinite; margin: 16px auto; }

@media (max-width: 720px) {
  .header { flex-wrap: wrap; }
  .view-toggle { width: 100%; justify-content: center; }
  .header-content { order: -1; }
}
