:root {
  --bg: #1a1a1a;
  --fg: #f4f4f4;
  --muted: #888;
  --primary: #ff5722;
  --primary-hi: #ff7849;
  --ok: #4caf50;
  --danger: #e53935;
  --card: #262626;
  --border: #333;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  height: 100%;
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  color: var(--fg); background: var(--bg);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; cursor: pointer; }
input, textarea { font: inherit; }

#map {
  position: fixed; inset: 0;
  z-index: 1;
}

/* Bottom bar */
.bottom-bar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex; gap: 6px;
  padding: 8px calc(8px + env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) calc(8px + env(safe-area-inset-left));
  z-index: 1000;
  background: linear-gradient(to top, rgba(0,0,0,0.85), rgba(0,0,0,0.0));
}
.bottom-bar .btn {
  flex: 1; min-height: 56px;
  display: flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--card); color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 14px;
  padding: 0 8px;
}
.bottom-bar .btn:active { transform: scale(0.97); }
.bottom-bar .btn .lbl { font-weight: 600; }
.bottom-bar .btn.record .dot {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--primary);
  box-shadow: 0 0 8px var(--primary);
}
.bottom-bar .btn.record.on { background: var(--primary); border-color: var(--primary-hi); }
.bottom-bar .btn.record.on .dot { background: #fff; animation: pulse 1s infinite; }
@keyframes pulse {
  0%,100% { opacity: 1; }
  50% { opacity: 0.4; }
}
.bottom-bar .btn.armed { background: #2e2e2e; outline: 2px solid var(--primary); }

/* GPS badge */
.gps-badge {
  position: fixed;
  top: calc(8px + env(safe-area-inset-top));
  left: 8px;
  z-index: 999;
  background: rgba(0,0,0,0.7);
  color: var(--fg);
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 12px;
  pointer-events: none;
}
.gps-badge.good { color: #b9f6ca; }
.gps-badge.bad { color: #ff8a80; }

/* Drawer */
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0;
  width: min(420px, 92vw);
  background: var(--bg);
  border-left: 1px solid var(--border);
  z-index: 2000;
  transform: translateX(100%);
  transition: transform 0.2s ease;
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
}
.drawer.open { transform: translateX(0); }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.drawer-head h2 { margin: 0; font-size: 18px; }
.drawer-head .x {
  background: none; border: none; color: var(--fg); font-size: 22px;
}
.drawer-body {
  flex: 1; overflow-y: auto;
  padding: 12px 16px calc(16px + env(safe-area-inset-bottom)) 16px;
}
.drawer-body section {
  margin-bottom: 22px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
}
.drawer-body section:last-child { border-bottom: none; }
.drawer-body h3 { margin: 0 0 10px 0; font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }
.drawer-body button {
  display: block;
  width: 100%;
  margin: 4px 0;
  padding: 10px 14px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 8px;
  font-weight: 600;
}
.drawer-body button.ghost {
  background: transparent; border: 1px solid var(--border); color: var(--fg); font-weight: 500;
}
.drawer-body button.ghost.danger { color: var(--danger); border-color: var(--danger); }
.drawer-body input[type=file] { width: 100%; padding: 8px; background: var(--card); color: var(--fg); border: 1px solid var(--border); border-radius: 6px; }

/* Calibration list */
.cps { margin-top: 8px; }
.cp-row {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 0;
  border-bottom: 1px dashed var(--border);
  font-size: 12px;
}
.cp-row .nm { font-weight: 700; min-width: 22px; }
.cp-row .coords { flex: 1; color: var(--muted); }
.cp-row button {
  width: auto !important; margin: 0 !important;
  background: transparent !important; color: var(--danger) !important;
  border: 1px solid var(--danger) !important;
  padding: 2px 8px !important; font-size: 12px !important;
}

/* Layers */
.layers { max-height: 320px; overflow-y: auto; }
.layer-actions { display: flex; gap: 6px; }
.layer-actions button { flex: 1; }
.layer-row {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 0;
  font-size: 13px;
  border-bottom: 1px dashed var(--border);
}
.layer-row input { transform: scale(1.2); }
.layer-row .swatch {
  width: 14px; height: 14px; border-radius: 3px;
  border: 1px solid #555;
}
.layer-row .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer-row .cnt { color: var(--muted); font-size: 11px; }

/* Login */
.login-page {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #1a1a1a 0%, #2a2520 100%);
  padding: 20px;
}
.login-card {
  background: var(--card);
  padding: 24px;
  border-radius: 12px;
  width: 100%;
  max-width: 360px;
  border: 1px solid var(--border);
}
.login-card h1 { margin: 0 0 20px 0; font-size: 22px; }
.login-card label { display: block; margin: 12px 0; font-size: 13px; color: var(--muted); }
.login-card label small { font-weight: normal; opacity: 0.7; }
.login-card input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 12px;
  background: #1a1a1a;
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.login-card button {
  width: 100%;
  margin-top: 12px;
  padding: 12px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 8px;
  font-weight: 700;
  font-size: 16px;
}
.login-card .err { margin-top: 10px; color: var(--danger); font-size: 13px; min-height: 18px; }

/* Modal */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 3000;
  padding: 20px;
}
.modal.hidden { display: none; }
.modal-body {
  background: var(--card);
  padding: 20px;
  border-radius: 12px;
  width: 100%;
  max-width: 360px;
  border: 1px solid var(--border);
}
.modal-body h3 { margin: 0 0 12px 0; }
.modal-body input, .modal-body textarea {
  display: block;
  width: 100%;
  margin-bottom: 10px;
  padding: 10px;
  background: #1a1a1a;
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  resize: vertical;
  font-family: inherit;
}
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.modal-actions button {
  padding: 8px 16px;
  border-radius: 6px;
  border: none;
  background: var(--primary);
  color: white;
  font-weight: 600;
}
.modal-actions button.ghost {
  background: transparent; border: 1px solid var(--border); color: var(--fg);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.9);
  color: white;
  padding: 10px 16px;
  border-radius: 20px;
  z-index: 4000;
  font-size: 14px;
  max-width: 80%;
  text-align: center;
}
.toast.hidden { display: none; }

/* Progress bar */
.progress { margin-top: 10px; }
.bar {
  width: 100%;
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
}
.bar-inner {
  height: 100%;
  background: var(--primary);
  transition: width 0.2s ease;
  width: 0%;
}
.bar-label { font-size: 12px; color: var(--muted); margin-top: 4px; }

.parse-banner {
  position: fixed;
  top: calc(8px + env(safe-area-inset-top));
  right: 8px;
  z-index: 1500;
  background: rgba(0,0,0,0.85);
  color: white;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
  min-width: 200px;
}
.parse-banner .bar { margin-top: 6px; }

/* DXF Viewer (полноэкранный) */
.dxf-viewer-host {
  position: fixed; inset: 0;
  background: #0a0a0a;
  z-index: 5000;
  display: flex; flex-direction: column;
}
.dxf-viewer-host.hidden { display: none; }
.dxf-viewer-header {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px calc(10px + env(safe-area-inset-top)) 12px;
  background: rgba(0,0,0,0.9);
  color: white;
  font-size: 13px;
  padding-top: calc(10px + env(safe-area-inset-top));
}
.dxf-viewer-cancel, .dxf-viewer-fit, .dxf-viewer-zoom {
  background: var(--card);
  color: white;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  min-width: 36px;
  height: 32px;
}
.dxf-viewer-zoom { font-weight: 700; }
.dxf-viewer-instr { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dxf-viewer-canvas {
  display: block;
  width: 100%;
  cursor: crosshair;
  touch-action: none;
  background: #fafafa;
}
.dxf-viewer-status {
  background: rgba(0,0,0,0.85);
  color: #ccc;
  font-family: ui-monospace, monospace;
  font-size: 11px;
  padding: 4px 10px;
  height: 22px;
  line-height: 14px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  border-top: 1px solid #333;
}

/* Шаговый баннер калибровки */
.calib-banner {
  position: fixed;
  top: calc(8px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 1500;
  background: var(--primary);
  color: white;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 13px;
  display: flex; align-items: center; gap: 12px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.5);
}
.calib-banner.hidden { display: none; }
.calib-banner button {
  background: rgba(255,255,255,0.2);
  color: white;
  border: 1px solid rgba(255,255,255,0.4);
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
}

/* Projects page */
.projects-page { background: linear-gradient(135deg, #1a1a1a 0%, #2a2520 100%); min-height: 100vh; }
.projects-wrap {
  max-width: 760px;
  margin: 0 auto;
  padding: 24px 16px calc(24px + env(safe-area-inset-bottom));
}
.projects-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 24px;
  padding-top: env(safe-area-inset-top);
}
.projects-head h1 { margin: 0; font-size: 22px; }
.projects-head-right { display: flex; align-items: center; gap: 12px; }
.projects-head-right button {
  background: transparent; color: var(--fg); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 12px; cursor: pointer;
}
.projects-list { display: flex; flex-direction: column; gap: 12px; margin-bottom: 28px; }
.project-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
}
.project-card.active { border-color: var(--primary); }
.project-card-head {
  display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin-bottom: 6px;
}
.project-card-head h3 { margin: 0; font-size: 17px; }
.project-card-head .slug {
  font-family: ui-monospace, monospace;
  font-size: 11px;
  color: var(--muted);
  background: #1a1a1a;
  padding: 1px 6px;
  border-radius: 4px;
}
.project-card-stats {
  display: flex; flex-wrap: wrap; gap: 8px 16px;
  font-size: 12px; color: var(--muted);
  margin-bottom: 12px;
}
.project-card-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.project-card-actions button {
  flex: 1 0 auto;
  padding: 8px 12px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
}
.project-card-actions button.ghost {
  background: transparent; border: 1px solid var(--border); color: var(--fg); font-weight: 500;
}
.project-card-actions button.ghost.danger { color: var(--danger); border-color: var(--danger); }

.projects-new {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
}
.projects-new h2 { margin: 0 0 10px 0; font-size: 16px; }
.new-form { display: flex; gap: 8px; }
.new-form input {
  flex: 1; min-width: 0;
  padding: 10px 12px;
  background: #1a1a1a;
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.new-form button {
  padding: 10px 16px;
  background: var(--primary);
  color: white; border: none; border-radius: 8px;
  font-weight: 700; cursor: pointer;
}

/* Project badge on map */
.project-badge {
  position: fixed;
  top: calc(8px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 999;
  background: rgba(0,0,0,0.7);
  color: var(--fg);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  display: flex; align-items: center; gap: 8px;
  pointer-events: auto;
  cursor: pointer;
  border: 1px solid var(--border);
}
.project-badge:hover { background: rgba(0,0,0,0.85); }
.project-badge .switch { color: var(--primary); font-size: 11px; }

/* Курсор crosshair на карте в режиме привязки */
.cursor-crosshair, .cursor-crosshair .leaflet-grab, .cursor-crosshair .leaflet-dragging .leaflet-grab {
  cursor: crosshair !important;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.err { color: var(--danger); }

/* Leaflet overrides */
.leaflet-control-zoom { display: none; }
.leaflet-control-attribution {
  background: rgba(0,0,0,0.5) !important;
  color: #aaa !important;
  font-size: 10px !important;
}
.leaflet-control-attribution a { color: #ddd !important; }

/* Custom GPS marker */
.gps-marker {
  width: 22px; height: 22px;
  background: #2196f3;
  border: 3px solid white;
  border-radius: 50%;
  box-shadow: 0 0 16px rgba(33,150,243,0.5);
}
.gps-accuracy {
  border: 1px solid rgba(33,150,243,0.5);
  background: rgba(33,150,243,0.1);
  border-radius: 50%;
}

/* CP marker */
.cp-marker {
  min-width: 34px; height: 34px;
  background: #ffeb3b;
  color: #000;
  border: 2px solid #ff5722;
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
  font-size: 12px;
  padding: 0 4px;
  white-space: nowrap;
  box-shadow: 0 2px 4px rgba(0,0,0,0.4);
}
.cp-marker sub {
  font-size: 9px;
  font-weight: 500;
  margin-left: 2px;
}
.cp-marker.good { background: #b9f6ca; border-color: #2e7d32; }
.cp-marker.ok   { background: #fff59d; border-color: #f9a825; }
.cp-marker.bad  { background: #ff8a80; border-color: #c62828; }

/* POI marker */
.poi-marker {
  width: 18px; height: 18px;
  background: #ff5722;
  border: 2px solid white;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  box-shadow: 0 2px 4px rgba(0,0,0,0.4);
}
