:root {
  --navy: #2c4a6e;
  --navy-dark: #1f3855;
  --navy-darker: #16283e;
  --blue: #a8c8e0;
  --blue-light: #c5d9e8;
  --cream: #e0f4f7;
  --cream-dim: #c8e0e4;
  --coral: #e8694a;
  --coral-dark: #cf5638;
  --charcoal: #2a3441;
  --charcoal-light: #3a4555;
  --charcoal-dark: #1d242e;
  --text: #e0f4f7;
  --text-dim: #a8c8e0;
  --muted: #6b7d92;
  --border: #3a4555;
  --border-light: #4a5566;
  --ok: #7cb87c;
  --warn: #e8a04a;
  --danger: #e8694a;
  --shadow: 0 8px 24px rgba(0,0,0,0.4);
}

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

html, body {
  height: 100vh; width: 100vw; overflow: hidden;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--charcoal);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

body { display: flex; }

/* ─── Sidebar ─────────────────────────────────── */
#sidebar {
  width: 320px;
  background: var(--charcoal-dark);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
  min-height: 0;
  transition: width 0.25s ease;
}
body.sidebar-collapsed #sidebar { border-right: none; }
body.sidebar-collapsed #sidebar > * { display: none; }
body.analysis-collapsed #analysisPanel { border-left: none; }
body.analysis-collapsed #analysisPanel > * { display: none; }

/* Desktop panel toggles — fixed on body, positioned at panel edges */
.panel-toggle-desktop {
  display: none;
  position: fixed;
  top: 50%; transform: translateY(-50%);
  width: 24px; height: 56px;
  background: var(--charcoal-dark);
  border: 1px solid var(--border);
  color: var(--cream);
  cursor: pointer;
  z-index: 700;
  align-items: center; justify-content: center;
  transition: left 0.25s ease, right 0.25s ease, background 0.15s, color 0.15s;
  font-family: inherit;
}
.panel-toggle-desktop:hover {
  background: var(--coral);
  color: var(--cream);
  border-color: var(--coral);
}

/* Left toggle — sits at the right edge of the sidebar */
.panel-toggle-left {
  left: 320px;
  border-left: none;
  border-radius: 0 6px 6px 0;
}
body.sidebar-collapsed .panel-toggle-left { left: 0; }

/* Right toggle — sits at the left edge of the analysis panel */
.panel-toggle-right {
  right: 340px;
  border-right: none;
  border-radius: 6px 0 0 6px;
}
body.analysis-collapsed .panel-toggle-right { right: 0; }

@media (min-width: 901px) {
  .panel-toggle-desktop { display: flex; }
}
@media (max-width: 1200px) {
  .panel-toggle-right { right: 300px; }
  body.analysis-collapsed .panel-toggle-right { right: 0; }
}

.sidebar-scroll {
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex; flex-direction: column;
}

.brand {
  flex-shrink: 0;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(135deg, rgba(168,200,224,0.08) 0%, rgba(44,74,110,0.04) 100%);
}
.brand h1 {
  font-size: 14px; font-weight: 700; letter-spacing: 1.5px;
  color: var(--cream);
  white-space: nowrap;
}
.brand .tag {
  font-size: 9px; color: var(--text-dim);
  text-transform: uppercase; letter-spacing: 1px;
  display: block; margin-top: 1px;
}

.mobile-toggle {
  display: none;
  background: transparent; border: 1px solid var(--border); color: var(--cream);
  padding: 6px; cursor: pointer; margin-left: auto;
  border-radius: 6px;
  font-family: inherit;
  align-items: center; justify-content: center;
}
.mobile-toggle:hover { border-color: var(--blue); color: var(--blue); }
.mobile-toggle .mt-burger,
.mobile-toggle .mt-close { display: none; }
#sidebar.collapsed .mobile-toggle .mt-burger { display: block; }
#sidebar:not(.collapsed) .mobile-toggle .mt-close { display: block; }

.help-btn {
  background: transparent; border: 1px solid var(--border); color: var(--cream);
  padding: 5px; cursor: pointer; margin-left: auto;
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-family: inherit;
}
.help-btn:hover { border-color: var(--coral); color: var(--coral); }

.mobile-toggle { margin-left: 6px !important; }

/* ─── How-to overlay ──────────────────────────── */
#howtoOverlay {
  display: none;
  position: fixed; inset: 0; z-index: 1900;
  align-items: center; justify-content: center;
  background: rgba(15, 25, 40, 0.8);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
#howtoOverlay.show { display: flex; }
.ht-window {
  position: relative;
  max-width: 480px; width: 90vw;
  background: var(--charcoal-dark);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px 28px 24px;
  box-shadow: var(--shadow);
  max-height: 85vh;
  overflow-y: auto;
}
.ht-close {
  position: absolute; top: 14px; right: 14px;
  background: transparent; border: none; color: var(--text-dim);
  cursor: pointer; padding: 4px;
  border-radius: 4px;
}
.ht-close:hover { color: var(--coral); }
.ht-window h2 {
  font-size: 18px; font-weight: 700;
  letter-spacing: 1.5px; text-transform: uppercase;
  color: var(--cream);
  margin-bottom: 18px;
}
.ht-window ol {
  list-style: none;
  counter-reset: htsteps;
  margin: 0;
}
.ht-window ol li {
  counter-increment: htsteps;
  position: relative;
  padding: 8px 0 8px 32px;
  font-size: 13px; line-height: 1.5;
  color: var(--text-dim);
}
.ht-window ol li::before {
  content: counter(htsteps);
  position: absolute; left: 0; top: 8px;
  width: 22px; height: 22px;
  background: var(--coral);
  color: var(--cream);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700;
  font-family: 'JetBrains Mono', monospace;
}
.ht-window ol li strong { color: var(--cream); font-weight: 600; }
.ht-tip {
  margin-top: 18px;
  padding: 12px 14px;
  background: rgba(168,200,224,0.08);
  border-left: 3px solid var(--blue);
  border-radius: 4px;
  font-size: 12px; color: var(--text-dim);
  line-height: 1.5;
}
.ht-tip strong { color: var(--blue); margin-right: 4px; }
.ht-cta {
  display: block;
  width: 100%;
  margin-top: 18px;
  padding: 10px;
  background: var(--coral);
  color: var(--cream);
  border: none; border-radius: 6px;
  font-size: 13px; font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}
.ht-cta:hover { background: var(--coral-dark); }

.section {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
}
.section h3 {
  font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 1.2px;
  color: var(--text-dim); margin-bottom: 10px;
}

.cg { margin-bottom: 10px; }
.cg:last-child { margin-bottom: 0; }
.cg label {
  display: flex; justify-content: space-between;
  font-size: 11px; color: var(--text-dim);
  margin-bottom: 4px; font-weight: 500;
}
.cg .unit { color: var(--muted); font-weight: 400; }
.cg input, .cg select {
  width: 100%; padding: 9px 11px;
  background: var(--charcoal); color: var(--cream);
  border: 1px solid var(--border); border-radius: 6px;
  font-size: 13px; font-family: inherit;
  transition: all 0.15s;
}
.cg input:focus, .cg select:focus {
  outline: none; border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(168,200,224,0.15);
}

.tool-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 6px;
}
.btn-tool {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 8px; font-size: 12px; font-weight: 500;
  background: var(--charcoal); color: var(--cream);
  border: 1px solid var(--border); border-radius: 6px;
  cursor: pointer; font-family: inherit;
  transition: all 0.15s;
}
.btn-tool:hover:not(:disabled) {
  background: var(--charcoal-light); border-color: var(--blue);
  color: var(--blue);
}
.btn-tool.active {
  background: var(--coral); color: var(--cream);
  border-color: var(--coral); font-weight: 600;
}
.btn-tool:disabled { opacity: 0.4; cursor: not-allowed; }

.actions { display: grid; gap: 6px; }
.actions button {
  padding: 10px 12px; font-size: 13px; font-weight: 600;
  border: none; border-radius: 6px;
  cursor: pointer; font-family: inherit;
  transition: all 0.15s;
}
.btn-secondary {
  background: var(--charcoal); color: var(--cream);
  border: 1px solid var(--border) !important;
}
.btn-secondary:hover:not(:disabled) {
  background: var(--charcoal-light); border-color: var(--blue);
}
.btn-primary {
  background: var(--navy); color: var(--cream);
}
.btn-primary:hover:not(:disabled) { background: var(--navy-dark); }
.btn-accent {
  background: var(--coral); color: var(--cream);
}
.btn-accent:hover:not(:disabled) { background: var(--coral-dark); }
.actions button:disabled { opacity: 0.4; cursor: not-allowed; filter: none; }

#statusbar {
  margin-top: auto;
  padding: 12px 20px;
  font-size: 12px; color: var(--text-dim);
  background: var(--charcoal);
  border-top: 1px solid var(--border);
}

/* ─── Main / Map ──────────────────────────────── */
#main {
  flex: 1; display: flex;
  position: relative;
  min-width: 0;
}
#mapid {
  flex: 1; height: 100vh;
  cursor: crosshair;
  background: var(--charcoal);
  touch-action: none;
}
#mapid.panning { cursor: grab; }

#mapHint {
  display: none;
  position: absolute;
  top: 14px; left: 50%; transform: translateX(-50%);
  background: var(--coral); color: var(--cream);
  padding: 8px 16px; border-radius: 20px;
  font-size: 12px; font-weight: 600;
  letter-spacing: 0.3px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  z-index: 450;
  pointer-events: none;
  animation: hintPulse 1.5s ease-in-out infinite;
}
#mapHint.show { display: block; }
@keyframes hintPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

/* ─── Analysis Panel ──────────────────────────── */
#analysisPanel {
  width: 340px;
  background: var(--charcoal-dark);
  border-left: 1px solid var(--border);
  flex-shrink: 0;
  display: flex; flex-direction: column;
  overflow: hidden;
}
.ap-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.ap-header {
  padding: 16px 20px;
  display: flex; justify-content: space-between; align-items: center;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(135deg, rgba(168,200,224,0.06) 0%, transparent 100%);
}
.ap-handle {
  display: none;
  width: 40px; height: 4px;
  background: var(--border-light);
  border-radius: 2px;
  margin: 6px auto 0;
}
.ap-header h3 {
  font-size: 13px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 1px;
  color: var(--cream);
}
.badge {
  padding: 3px 9px; border-radius: 10px;
  font-size: 10px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.5px;
}
.badge-idle { background: var(--charcoal); color: var(--text-dim); }
.badge-ok { background: rgba(124,184,124,0.15); color: var(--ok); }
.badge-warn { background: rgba(232,160,74,0.15); color: var(--warn); }
.badge-danger { background: rgba(232,105,74,0.15); color: var(--danger); }
.badge-loading { background: rgba(168,200,224,0.15); color: var(--blue); }

.ap-section {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
}
.ap-section h4 {
  font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 1px;
  color: var(--text-dim); margin-bottom: 10px;
  display: flex; justify-content: space-between; align-items: center;
}
.refresh-btn {
  background: transparent; border: none; color: var(--text-dim);
  cursor: pointer; padding: 4px;
  border-radius: 4px; display: flex;
  transition: all 0.15s;
}
.refresh-btn:hover { color: var(--blue); background: var(--charcoal); }

.ap-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 0; font-size: 13px;
}
.ap-row .ap-label { color: var(--text-dim); }
.ap-row strong { font-weight: 600; color: var(--cream); }

.zone-item {
  padding: 10px 12px;
  background: var(--charcoal);
  border-radius: 6px;
  margin-bottom: 6px;
  border-left: 3px solid var(--warn);
  font-size: 12px;
}
.zone-item.zone-danger { border-left-color: var(--danger); }
.zone-item.zone-ok { border-left-color: var(--ok); }
.zone-item .zt {
  font-weight: 600; font-size: 12px; margin-bottom: 3px;
  display: flex; justify-content: space-between; gap: 8px;
  color: var(--cream);
}
.zone-item .zd { color: var(--text-dim); font-size: 11px; line-height: 1.4; }
.zone-tag {
  display: inline-block;
  padding: 2px 7px; border-radius: 8px;
  font-size: 9px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.4px;
  background: rgba(232,105,74,0.2); color: var(--danger);
  flex-shrink: 0;
}
.zone-tag.tag-warn { background: rgba(232,160,74,0.2); color: var(--warn); }
.zone-tag.tag-ok { background: rgba(124,184,124,0.2); color: var(--ok); }

.compl-item {
  display: flex; align-items: flex-start; gap: 8px;
  padding: 6px 0; font-size: 12px;
}
.compl-icon {
  flex-shrink: 0; width: 16px; height: 16px;
  border-radius: 50%; display: flex; align-items: center; justify-content: center;
  font-size: 10px; font-weight: 700;
  margin-top: 1px;
}
.compl-icon.ok { background: rgba(124,184,124,0.18); color: var(--ok); }
.compl-icon.warn { background: rgba(232,160,74,0.18); color: var(--warn); }
.compl-icon.fail { background: rgba(232,105,74,0.18); color: var(--danger); }
.compl-text { color: var(--cream); line-height: 1.4; }
.compl-text .ct-sub { color: var(--text-dim); font-size: 11px; margin-top: 2px; }

.ap-footer {
  margin-top: auto; padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: var(--charcoal);
}
.ap-footer small { color: var(--muted); font-size: 10px; line-height: 1.5; display: block; }

.muted { color: var(--muted); font-size: 12px; font-style: italic; }

.spinner {
  display: inline-block; width: 12px; height: 12px;
  border: 2px solid var(--charcoal-light);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ─── Disclaimer overlay ──────────────────────── */
#disclaimerOverlay {
  display: none;
  position: fixed; inset: 0; z-index: 2000;
  align-items: center; justify-content: center;
  background: rgba(15, 25, 40, 0.85);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
#disclaimerOverlay.show { display: flex; }
.disc-window {
  max-width: 460px; width: 90vw;
  background: var(--charcoal-dark);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 32px 28px;
  box-shadow: var(--shadow);
  text-align: center;
}
.disc-icon {
  display: flex; justify-content: center;
  color: var(--coral);
  margin-bottom: 14px;
}
.disc-window h2 {
  font-size: 18px; font-weight: 700;
  letter-spacing: 1.5px; text-transform: uppercase;
  color: var(--cream);
  margin-bottom: 14px;
}
.disc-window p {
  font-size: 13px; color: var(--text-dim);
  line-height: 1.5; margin-bottom: 14px;
  text-align: left;
}
.disc-window p strong { color: var(--cream); }
.disc-window ul {
  text-align: left;
  list-style: none;
  font-size: 12px; color: var(--text-dim);
  margin-bottom: 20px;
}
.disc-window ul li {
  padding: 6px 0 6px 18px;
  position: relative;
  line-height: 1.5;
}
.disc-window ul li::before {
  content: '→'; color: var(--coral);
  position: absolute; left: 0;
}
.disc-window ul li a { color: var(--blue); text-decoration: none; }
.disc-window ul li a:hover { color: var(--cream); }
.disc-actions {
  display: flex; justify-content: space-between; align-items: center;
  gap: 14px; margin-top: 12px;
}
.disc-check {
  display: flex; align-items: center; gap: 6px;
  font-size: 11px; color: var(--text-dim);
  cursor: pointer;
}
.disc-check input { cursor: pointer; }
.disc-actions .btn-primary {
  padding: 9px 18px; font-size: 13px; font-weight: 600;
  border: none; border-radius: 6px; cursor: pointer;
  background: var(--coral); color: var(--cream);
  font-family: inherit;
}
.disc-actions .btn-primary:hover { background: var(--coral-dark); }

/* ─── Saved missions ──────────────────────────── */
.mission-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 10px;
  background: var(--charcoal);
  border-radius: 6px;
  margin-bottom: 4px;
  font-size: 12px;
}
.mission-name {
  flex: 1; color: var(--cream);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  cursor: pointer;
}
.mission-name:hover { color: var(--blue); }
.mission-date { font-size: 10px; color: var(--muted); margin-right: 6px; }
.mission-del {
  background: transparent; border: none; color: var(--muted);
  cursor: pointer; padding: 2px 4px;
}
.mission-del:hover { color: var(--danger); }

/* ─── Modal preview ───────────────────────────── */
#previewOverlay {
  display: none;
  position: fixed; inset: 0; z-index: 1000;
  align-items: center; justify-content: center;
}
#previewOverlay.open { display: flex; }
.modal-backdrop {
  position: absolute; inset: 0;
  background: rgba(15, 25, 40, 0.78);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.modal-window {
  position: relative;
  width: min(85vh * 5 / 3, 85vw);
  height: min(85vh, 85vw * 3 / 5);
  background: var(--charcoal-dark);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 18px;
  background: var(--charcoal);
  border-bottom: 1px solid var(--border);
}
.modal-header h3 {
  font-size: 13px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 1px;
  color: var(--cream);
}
.modal-close {
  background: var(--charcoal-light); border: 1px solid var(--border);
  color: var(--cream);
  cursor: pointer; padding: 6px 10px;
  border-radius: 6px; display: flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 600;
  font-family: inherit;
  transition: all 0.15s;
}
.modal-close:hover { background: var(--coral); border-color: var(--coral); }
.modal-window iframe { flex: 1; border: none; width: 100%; }

/* ─── Leaflet overrides ──────────────────────── */
.leaflet-control-layers, .leaflet-control-zoom a {
  background: var(--charcoal-dark) !important;
  color: var(--cream) !important;
  border-color: var(--border) !important;
}
.leaflet-control-layers-expanded {
  padding: 10px !important; font-family: 'Inter', sans-serif !important;
  font-size: 12px !important; color: var(--cream) !important;
}
.leaflet-control-zoom a { color: var(--cream) !important; }
.leaflet-control-zoom a:hover { background: var(--charcoal-light) !important; }
.leaflet-control-attribution {
  background: rgba(29,36,46,0.85) !important;
  color: var(--text-dim) !important;
  font-size: 10px !important;
}
.leaflet-control-attribution a { color: var(--blue) !important; }

/* ─── Responsive ──────────────────────────────── */
@media (max-width: 1200px) {
  #analysisPanel { width: 300px; }
  #analysisPanel.desktop-collapsed { width: 0; }
}

@media (max-width: 900px) {
  body { flex-direction: column; position: relative; }

  /* Sidebar stays in flow (relative, never fixed) — only the brand bar shows when collapsed.
     When expanded, the sections fill the rest of the viewport and scroll internally. */
  #sidebar {
    width: 100%;
    border-right: none;
    border-bottom: none;
    flex-shrink: 0;
    z-index: 600;
    background: var(--charcoal-dark);
    display: flex; flex-direction: column;
    height: 44px;
    transition: height 0.25s ease;
    overflow: hidden;
  }
  #sidebar:not(.collapsed) {
    height: 100vh;
  }
  #sidebar .brand {
    padding: 0 12px;
    height: 44px;
    flex-shrink: 0;
    border-bottom: none;
  }
  #sidebar .brand h1 { font-size: 12px; letter-spacing: 1px; }
  #sidebar .brand .tag { font-size: 8px; }
  #sidebar .brand .mobile-toggle { display: flex; }

  /* Wrap scrollable sections — only visible when not collapsed */
  #sidebar.collapsed .sidebar-scroll { display: none; }
  #sidebar:not(.collapsed) .sidebar-scroll {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    overscroll-behavior: contain;
    padding-bottom: 100px; /* space for the bottom-sheet handle (60px) + breathing room */
    display: flex; flex-direction: column;
  }

  #main {
    flex: 1;
    min-height: 0;
    margin-top: 0;
  }

  /* Analysis = bottom sheet, ALWAYS above leaflet controls */
  #analysisPanel {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    width: 100%; height: 85vh;
    max-height: 85vh;
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    transform: translateY(calc(100% - 60px));
    transition: transform 0.3s ease;
    z-index: 9999;
    box-shadow: 0 -10px 30px rgba(0,0,0,0.4);
    overflow: hidden;
  }
  #analysisPanel.open { transform: translateY(0); }
  .ap-handle { display: block; }
  .ap-header {
    flex-shrink: 0;
    flex-direction: column;
    gap: 8px;
    padding: 8px 20px 14px;
    cursor: grab;
    touch-action: none; /* drag handle, not scrollable */
    background: var(--charcoal-dark);
  }
  .ap-header .ap-row-top {
    display: flex; justify-content: space-between; align-items: center;
    width: 100%;
  }
  .ap-body {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y; /* allow vertical scroll */
    overscroll-behavior: contain;
    padding-bottom: 40px; /* breathing room at the bottom */
  }

  /* leaflet controls must stay below analysis panel */
  .leaflet-top, .leaflet-bottom {
    z-index: 400 !important;
  }
  .leaflet-control-container { z-index: 400 !important; }
  .leaflet-pane { z-index: auto !important; }

  .modal-window {
    width: 100vw;
    height: 100dvh;
    height: calc(100dvh - 60px); /* bottom sheet handle space */
    max-width: none;
    max-height: none;
    border-radius: 0;
    border: none;
    margin-bottom: 60px;
    margin-top: 0;
    align-self: flex-start; /* anchor to top so header is fully visible */
  }
  .modal-header {
    padding: 10px 14px;
    flex-shrink: 0;
  }
}

@media (max-width: 900px) {
  #previewOverlay { align-items: flex-start; }
}