/* Car Tracker — тёмная тема для мониторинга.
   Дизайн-система: Dark Mode (OLED), оранжевый акцент + зелёный статус, Fira Sans / Fira Code. */

:root {
  --bg: #0B1120;
  --bg-elev: #0F172A;
  --card: #141C2F;
  --card-2: #192134;
  --line: rgba(255, 255, 255, 0.08);
  --line-strong: rgba(255, 255, 255, 0.14);
  --fg: #F1F5F9;
  --fg-2: #CBD5E1;
  --muted: #94A3B8;
  --primary: #F97316;
  --primary-strong: #EA580C;
  --on-primary: #0B0B0B;
  --green: #34D399;
  --green-bg: rgba(16, 185, 129, 0.14);
  --amber: #FBBF24;
  --amber-bg: rgba(251, 191, 36, 0.14);
  --red: #F87171;
  --red-bg: rgba(239, 68, 68, 0.14);
  --blue: #60A5FA;
  --blue-bg: rgba(96, 165, 250, 0.14);
  --violet: #A78BFA;
  --violet-bg: rgba(167, 139, 250, 0.14);
  --orange-bg: rgba(249, 115, 22, 0.14);
  --s1: #34D399; --s2: #FBBF24; --s3: #F97316; --s4: #EF4444;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 0 rgba(255, 255, 255, 0.03) inset, 0 8px 24px rgba(0, 0, 0, 0.35);
  --font: "Fira Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: "Fira Code", ui-monospace, "SFMono-Regular", Consolas, monospace;
  --sidebar: 232px;
  --t-fast: 150ms;
  --t: 220ms;
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  color: var(--fg);
  background: var(--bg);
  background-image: radial-gradient(1200px 600px at 100% -10%, rgba(249, 115, 22, 0.07), transparent 60%),
                    radial-gradient(900px 500px at -10% 110%, rgba(52, 211, 153, 0.05), transparent 60%);
  background-attachment: fixed;
  min-height: 100vh;
}
a { color: inherit; }
h1, h2 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
.ic { flex-shrink: 0; }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.nowrap { white-space: nowrap; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); border: 0; }
.skip { position: absolute; left: -999px; top: 8px; z-index: 100; background: var(--primary); color: var(--on-primary); padding: 8px 12px; border-radius: 8px; }
.skip:focus { left: 8px; }

:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; border-radius: 6px; }

/* ---------------------------------------------------------------- Shell */
.shell { display: flex; min-height: 100vh; }
.sidebar {
  position: fixed; inset: 0 auto 0 0; width: var(--sidebar); z-index: 40;
  display: flex; flex-direction: column; gap: 8px;
  padding: 18px 12px;
  background: rgba(11, 17, 32, 0.92);
  backdrop-filter: blur(12px);
  border-right: 1px solid var(--line);
  transition: transform var(--t) ease;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 6px 10px 18px; text-decoration: none; }
.brand-mark {
  display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px;
  color: var(--primary); background: var(--orange-bg); border: 1px solid rgba(249, 115, 22, 0.3);
}
.brand-mark.lg { width: 48px; height: 48px; border-radius: 14px; }
.brand-text { font-weight: 700; font-size: 17px; letter-spacing: -0.02em; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-link {
  display: flex; align-items: center; gap: 12px; min-height: 44px; padding: 0 12px;
  color: var(--fg-2); text-decoration: none; border-radius: var(--radius-sm);
  font-size: 15px; font-weight: 500; cursor: pointer;
  transition: background var(--t-fast), color var(--t-fast);
}
.nav-link:hover { background: rgba(255, 255, 255, 0.05); color: var(--fg); }
.nav-link.active { background: var(--orange-bg); color: var(--primary); }
.nav-link.active .ic { color: var(--primary); }
.as-button { width: 100%; border: 0; background: none; font: inherit; }
.logout { margin-top: auto; }

.content { flex: 1; min-width: 0; margin-left: var(--sidebar); display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 12px;
  height: 60px; padding: 0 24px;
  background: rgba(11, 17, 32, 0.8); backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.page-title { font-size: 20px; }
.topbar-right { margin-left: auto; display: flex; gap: 8px; }
.menu-btn { display: none; }
.main { padding: 20px 24px 32px; display: flex; flex-direction: column; gap: 16px; }
.scrim { display: none; }

/* ---------------------------------------------------------- Controls */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 40px; padding: 0 16px; border-radius: var(--radius-sm);
  border: 1px solid transparent; font: 600 14px/1 var(--font); text-decoration: none;
  cursor: pointer; transition: background var(--t-fast), border-color var(--t-fast), transform var(--t-fast), opacity var(--t-fast);
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--primary); color: var(--on-primary); }
.btn.primary:hover { background: #FB8A3C; }
.btn.ghost { background: rgba(255, 255, 255, 0.04); color: var(--fg); border-color: var(--line-strong); }
.btn.ghost:hover { background: rgba(255, 255, 255, 0.08); }
.btn.block { width: 100%; min-height: 46px; font-size: 15px; }
.btn.sm { min-height: 32px; padding: 0 10px; font-size: 13px; }
.btn[disabled], .btn.loading { opacity: 0.6; pointer-events: none; }

.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px; flex-shrink: 0;
  border-radius: var(--radius-sm); border: 1px solid transparent; background: transparent;
  color: var(--fg-2); cursor: pointer; text-decoration: none;
  transition: background var(--t-fast), color var(--t-fast);
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.07); color: var(--fg); }
.icon-btn.danger:hover { background: var(--red-bg); color: var(--red); }
.icon-btn.disabled { opacity: 0.3; pointer-events: none; }

input, select {
  min-height: 40px; padding: 0 12px; width: 100%;
  background: var(--bg-elev); color: var(--fg);
  border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
  font: 15px var(--font);
  transition: border-color var(--t-fast), box-shadow var(--t-fast);
}
input:hover, select:hover { border-color: rgba(255, 255, 255, 0.24); }
input:focus, select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(249, 115, 22, 0.25); }
input[type="date"] { font-family: var(--mono); font-size: 14px; }
input[type="date"]::-webkit-calendar-picker-indicator { filter: invert(0.8); cursor: pointer; }
select { cursor: pointer; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 13px; font-weight: 500; color: var(--fg-2); }
.field.inline { min-width: 160px; }
.input-icon { position: relative; }
.input-icon .ic { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.input-icon input { padding-left: 40px; min-height: 46px; }

.toolbar { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; }
.date-nav { display: flex; align-items: center; gap: 4px; }
.date-nav input { width: 160px; }

.alert { padding: 12px 14px; border-radius: var(--radius-sm); font-size: 14px; border: 1px solid; }
.alert.error { background: var(--red-bg); color: #FECACA; border-color: rgba(248, 113, 113, 0.35); }
.alert.success { background: var(--green-bg); color: #A7F3D0; border-color: rgba(52, 211, 153, 0.35); }

/* ------------------------------------------------------------- Cards */
.card {
  position: relative;
  background: linear-gradient(180deg, var(--card-2), var(--card));
  border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.card-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 14px 16px; border-bottom: 1px solid var(--line);
}
.card-head.compact { padding: 12px 16px; }
.card-head h2 { display: flex; align-items: center; gap: 8px; font-size: 15px; }
.card-head h2 .ic { color: var(--muted); }

.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; }
.kpi {
  display: flex; align-items: center; gap: 14px; padding: 16px;
  background: linear-gradient(180deg, var(--card-2), var(--card));
  border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
}
.kpi-icon { display: grid; place-items: center; width: 44px; height: 44px; border-radius: 12px; flex-shrink: 0; }
.kpi-icon.green { background: var(--green-bg); color: var(--green); }
.kpi-icon.orange { background: var(--orange-bg); color: var(--primary); }
.kpi-icon.blue { background: var(--blue-bg); color: var(--blue); }
.kpi-icon.violet { background: var(--violet-bg); color: var(--violet); }
.kpi-label { font-size: 13px; color: var(--muted); }
.kpi-value { font-family: var(--mono); font-size: 26px; font-weight: 600; line-height: 1.2; letter-spacing: -0.02em; }
.kpi-sub { font-size: 14px; color: var(--muted); font-weight: 400; }

/* --------------------------------------------------------- Dashboard */
.dash-grid { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 16px; min-height: 540px; }
.map-card { display: flex; flex-direction: column; min-height: 420px; }
.map { flex: 1; min-height: 420px; background: #0d1424; }
.map.tall { min-height: 520px; }
.list-card { display: flex; flex-direction: column; }
.device-list { list-style: none; margin: 0; padding: 6px; overflow: auto; }
.device-item { display: flex; align-items: center; gap: 4px; }
.device-btn {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 12px;
  min-height: 56px; padding: 8px 10px; border: 0; border-radius: var(--radius-sm);
  background: none; color: inherit; font: inherit; text-align: left; cursor: pointer;
  transition: background var(--t-fast);
}
.device-btn:hover { background: rgba(255, 255, 255, 0.05); }
.device-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.device-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device-meta { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device-speed { font-weight: 600; font-size: 16px; white-space: nowrap; }
.device-speed small { font-size: 11px; color: var(--muted); font-weight: 400; }

.status-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; background: var(--muted); }
.status-dot.on, .pill.on .status-dot { background: var(--green); box-shadow: 0 0 0 4px rgba(52, 211, 153, 0.15); }
.status-dot.warn, .pill.warn .status-dot { background: var(--amber); box-shadow: 0 0 0 4px rgba(251, 191, 36, 0.15); }
.status-dot.off, .pill.off .status-dot { background: #64748B; }
@media (prefers-reduced-motion: no-preference) {
  .status-dot.on { animation: pulse 2.4s ease-in-out infinite; }
}
@keyframes pulse { 50% { box-shadow: 0 0 0 7px rgba(52, 211, 153, 0.05); } }

.pill { display: inline-flex; align-items: center; gap: 8px; padding: 4px 10px; border-radius: 999px; font-size: 13px; font-weight: 500; white-space: nowrap; }
.pill.on { background: var(--green-bg); color: var(--green); }
.pill.warn { background: var(--amber-bg); color: var(--amber); }
.pill.off { background: rgba(148, 163, 184, 0.12); color: var(--muted); }

.empty { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 4px; padding: 40px 24px; color: var(--fg-2); }
.empty .ic { color: var(--muted); margin-bottom: 8px; }
.empty p { margin: 0; }
.empty a { color: var(--primary); }

/* ------------------------------------------------------------ Track */
.track-grid {
  display: grid; gap: 16px;
  grid-template-columns: minmax(0, 1fr) 320px;
  grid-template-areas: "map side" "chart side";
}
.track-grid .map-card { grid-area: map; }
.side-card { grid-area: side; display: flex; flex-direction: column; max-height: 820px; }
.chart-card { grid-area: chart; }
.chart-box { position: relative; height: 220px; padding: 8px 12px 12px; }

.legend {
  position: absolute; left: 12px; bottom: 12px; z-index: 500;
  display: flex; gap: 12px; flex-wrap: wrap; padding: 6px 10px;
  background: rgba(11, 17, 32, 0.85); backdrop-filter: blur(6px);
  border: 1px solid var(--line); border-radius: 10px; font-size: 12px; color: var(--fg-2);
}
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.sw { display: inline-block; width: 16px; height: 4px; border-radius: 2px; }
.sw.s1 { background: var(--s1); } .sw.s2 { background: var(--s2); } .sw.s3 { background: var(--s3); } .sw.s4 { background: var(--s4); }

.map-overlay {
  position: absolute; inset: 0; z-index: 600; display: none;
  align-items: center; justify-content: center; gap: 10px;
  background: rgba(11, 17, 32, 0.55); color: var(--fg-2); font-size: 14px;
}
.map-overlay.show { display: flex; }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2px solid rgba(255, 255, 255, 0.2); border-top-color: var(--primary); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--line); border-bottom: 1px solid var(--line); }
.stat { display: flex; flex-direction: column; gap: 2px; padding: 12px 16px; background: var(--card-2); }
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value { font-family: var(--mono); font-size: 20px; font-weight: 600; }

.trips { list-style: none; margin: 0; padding: 6px; overflow: auto; flex: 1; }
.trip {
  display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; width: 100%;
  padding: 10px; border: 1px solid transparent; border-radius: var(--radius-sm);
  background: none; color: inherit; font: inherit; text-align: left; cursor: pointer;
  transition: background var(--t-fast), border-color var(--t-fast);
}
.trip:hover { background: rgba(255, 255, 255, 0.05); }
.trip.active { background: var(--orange-bg); border-color: rgba(249, 115, 22, 0.35); }
.trip-num {
  grid-row: span 2; display: grid; place-items: center; width: 30px; height: 30px; border-radius: 8px;
  background: rgba(255, 255, 255, 0.06); font-family: var(--mono); font-size: 13px; font-weight: 600;
}
.trip.active .trip-num { background: var(--primary); color: var(--on-primary); }
.trip-time { font-family: var(--mono); font-weight: 600; font-size: 14px; }
.trip-meta { font-size: 13px; color: var(--muted); }
.trip-stop { padding: 4px 10px 4px 52px; font-size: 12px; color: var(--muted); }
.trips .empty { padding: 24px 12px; }

/* ------------------------------------------------------------ Table */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th {
  position: sticky; top: 0; text-align: left; font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--muted); padding: 10px 14px; background: var(--card-2); border-bottom: 1px solid var(--line); white-space: nowrap;
}
.table td { padding: 10px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table tbody tr { transition: background var(--t-fast); }
.table tbody tr:hover { background: rgba(255, 255, 255, 0.03); }
.table tbody tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; }
.link { display: inline-flex; align-items: center; gap: 6px; color: var(--fg); text-decoration: none; border-bottom: 1px dashed transparent; }
.link:hover { color: var(--primary); }
.link .ic { color: var(--muted); }
.badge { display: inline-block; padding: 2px 8px; border-radius: 6px; font-size: 12px; font-weight: 500; }
.badge.good { background: var(--green-bg); color: var(--green); }
.badge.mid { background: var(--amber-bg); color: var(--amber); }
.badge.bad { background: var(--red-bg); color: var(--red); }
.pager { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; border-top: 1px solid var(--line); }

.rename { display: flex; align-items: center; gap: 4px; }
.rename input { min-height: 36px; max-width: 240px; font-weight: 600; background: transparent; border-color: transparent; padding: 0 8px; margin-left: -8px; }
.rename input:hover { border-color: var(--line-strong); }
.rename input:focus { background: var(--bg-elev); }
.inline-form { display: inline; }

.setup { padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.setup p { margin: 0 0 4px; }
.copy-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.copy-label { width: 120px; color: var(--fg-2); font-size: 14px; }
.copy-row code {
  flex: 1; min-width: 200px; padding: 9px 12px; border-radius: var(--radius-sm);
  background: var(--bg-elev); border: 1px solid var(--line); font-size: 13px; word-break: break-all;
}

/* ------------------------------------------------------------ Login */
.login-page { display: grid; place-items: center; padding: 24px 16px; }
.login-wrap { width: 100%; max-width: 400px; }
.login-card {
  padding: 32px 28px; border-radius: 20px;
  background: linear-gradient(180deg, var(--card-2), var(--card));
  border: 1px solid var(--line); box-shadow: 0 30px 80px rgba(0, 0, 0, 0.5);
}
.login-brand { display: flex; align-items: center; gap: 14px; margin-bottom: 28px; }
.login-title { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; }
.form { display: flex; flex-direction: column; gap: 16px; }

/* ----------------------------------------------------------- Leaflet */
.leaflet-container { font-family: var(--font); background: #0d1424; }
.leaflet-bar a, .leaflet-bar a:hover { background: var(--card-2); color: var(--fg); border-color: var(--line); }
.leaflet-bar { border: 1px solid var(--line-strong) !important; box-shadow: var(--shadow) !important; }
.leaflet-control-attribution { background: rgba(11, 17, 32, 0.75) !important; color: var(--muted) !important; }
.leaflet-control-attribution a { color: var(--fg-2) !important; }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--card-2); color: var(--fg); box-shadow: var(--shadow); border: 1px solid var(--line); }
.leaflet-popup-content { margin: 12px 14px; font-size: 13px; line-height: 1.5; }
.leaflet-popup-content b { font-size: 14px; }
.leaflet-container a.leaflet-popup-close-button { color: var(--muted); }
.car-marker { display: grid; place-items: center; }
.car-marker .arrow {
  width: 34px; height: 34px; display: grid; place-items: center; border-radius: 50%;
  background: var(--primary); color: var(--on-primary); border: 3px solid #0B1120;
  box-shadow: 0 0 0 2px var(--primary), 0 6px 16px rgba(0, 0, 0, 0.5);
}
.car-marker.off .arrow { background: #64748B; box-shadow: 0 0 0 2px #64748B, 0 6px 16px rgba(0, 0, 0, 0.5); }
.car-label {
  margin-top: 4px; padding: 2px 8px; border-radius: 6px; white-space: nowrap;
  background: rgba(11, 17, 32, 0.9); border: 1px solid var(--line); font-size: 12px; font-weight: 600; color: var(--fg);
}
.end-marker { width: 14px; height: 14px; border-radius: 50%; border: 3px solid #0B1120; }
.end-marker.start { background: var(--green); box-shadow: 0 0 0 2px var(--green); }
.end-marker.finish { background: var(--red); box-shadow: 0 0 0 2px var(--red); }

/* -------------------------------------------------------- Responsive */
@media (max-width: 1200px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .dash-grid { grid-template-columns: 1fr; }
  .list-card { max-height: none; }
  .track-grid { grid-template-columns: 1fr; grid-template-areas: "map" "side" "chart"; }
  .side-card { max-height: none; }
  .trips { max-height: 360px; }
}
@media (max-width: 860px) {
  .sidebar { transform: translateX(-100%); }
  body.menu-open .sidebar { transform: none; }
  body.menu-open .scrim { display: block; position: fixed; inset: 0; z-index: 35; background: rgba(0, 0, 0, 0.5); }
  .content { margin-left: 0; }
  .menu-btn { display: inline-grid; margin-left: -8px; }
  .topbar { padding: 0 16px; }
  .main { padding: 16px; }
  .map, .map.tall { min-height: 360px; }
  .hide-sm { display: none; }
  .field.inline { min-width: 0; flex: 1 1 140px; }
}
@media (max-width: 480px) {
  .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
  .kpi { padding: 12px; gap: 10px; }
  .kpi-icon { width: 36px; height: 36px; }
  .kpi-value { font-size: 20px; }
  .copy-label { width: 100%; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0ms !important; animation-duration: 0ms !important; }
}

/* Тёмная подложка из обычных тайлов OSM */
.tiles-dark { filter: invert(1) hue-rotate(180deg) brightness(0.95) contrast(0.9) saturate(0.35); }
.leaflet-control-layers { background: var(--card-2) !important; color: var(--fg) !important; border: 1px solid var(--line-strong) !important; border-radius: 10px !important; box-shadow: var(--shadow) !important; }
.leaflet-control-layers-expanded { padding: 8px 12px; }
.leaflet-control-layers label { display: flex; align-items: center; gap: 6px; min-height: 28px; cursor: pointer; font-size: 13px; }
.leaflet-control-layers-toggle { filter: invert(1); }
