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

:root {
  --bg: #1a1c2e;
  --surface: #232640;
  --surface2: #2b2e4a;
  --border: #3a3d5c;
  --text: #e0e0f0;
  --text-dim: #8888aa;
  --accent: #4dabf7;
  --green: #51cf66;
  --yellow: #fcc419;
  --red: #ff6b6b;
  --orange: #ff922b;
  --radius: 8px;
}

html { -webkit-text-size-adjust: 100%; }

html, body {
  height: 100%;
  margin: 0;
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  padding-left: env(safe-area-inset-left, 0);
  padding-right: env(safe-area-inset-right, 0);
}

header {
  display: flex;
  align-items: center;
  gap: 0.75rem 1.25rem;
  flex-wrap: nowrap;
  padding: 0.6rem 1.2rem;
  padding-top: max(0.6rem, env(safe-area-inset-top, 0));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

/* Mobile tabs (shown only on narrow screens) */
.mobile-tabs { display: none; }
.tab-btn { display: none; }

header h1 {
  font-size: 1.05rem;
  font-weight: 600;
  flex: 0 1 auto;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

header .hint {
  flex: 1 1 160px;
  min-width: 0;
  font-size: 0.78rem;
  color: var(--text-dim);
  line-height: 1.35;
}

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

/* Sidebar */
#sidebar {
  width: 280px; min-width: 280px;
  overflow-y: auto; padding: 0.8rem;
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 0.6rem;
}
.panel { background: var(--surface2); border-radius: var(--radius); padding: 0.7rem; }
.panel h2 { font-size: 0.85rem; color: var(--accent); margin-bottom: 0.5rem; text-transform: uppercase; letter-spacing: 0.05em; }
.panel h3 { font-size: 0.78rem; color: var(--text-dim); margin-top: 0.5rem; margin-bottom: 0.3rem; }
.panel label {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 0.78rem; color: var(--text-dim); margin-bottom: 0.3rem;
}
.panel label.nodes-show-layer {
  justify-content: flex-start;
  align-items: center;
}
.panel input[type="number"],
.panel input[type="text"] {
  width: 100px; padding: 3px 6px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 4px;
  color: var(--text); font-size: 0.8rem; text-align: right;
}
.panel input[readonly] { color: var(--text-dim); }
.panel select {
  width: 100%; padding: 4px 6px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 4px;
  color: var(--text); font-size: 0.78rem;
}

.coverage-mode-wrap {
  display: block;
  margin: 0.5rem 0 0.2rem;
  font-size: 0.76rem;
  font-weight: 600;
  color: var(--text-dim);
}
.coverage-mode-select {
  width: 100%;
  margin-bottom: 0.35rem;
  padding: 4px 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-size: 0.78rem;
}

/* Results */
.result-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 0.25rem 0.5rem;
  font-size: 0.8rem;
}
.rlabel { color: var(--text-dim); }
.verdict {
  margin-top: 0.5rem; padding: 0.4rem; border-radius: 4px;
  text-align: center; font-weight: 600; font-size: 0.85rem;
}
.verdict.ok { background: rgba(81,207,102,0.15); color: var(--green); }
.verdict.warn { background: rgba(252,196,25,0.15); color: var(--yellow); }
.verdict.fail { background: rgba(255,107,107,0.15); color: var(--red); }

/* Buttons */
.btn-primary, .btn-secondary {
  width: 100%; padding: 0.55rem; border: none; border-radius: var(--radius);
  font-size: 0.82rem; font-weight: 600; cursor: pointer; transition: opacity 0.2s;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-secondary { background: var(--surface2); color: var(--accent); border: 1px solid var(--accent); }
.btn-primary:disabled, .btn-secondary:disabled { opacity: 0.35; cursor: default; }
.btn-primary:hover:not(:disabled) { opacity: 0.85; }
.btn-secondary:hover:not(:disabled) { background: rgba(77,171,247,0.1); }

.btn-row { display: flex; gap: 0.4rem; }
.btn-icon {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 0.35rem;
  padding: 0.45rem 0.5rem; border-radius: var(--radius);
  font-size: 0.75rem; font-weight: 600; cursor: pointer;
  background: var(--surface2); color: var(--green); border: 1px solid var(--green);
  transition: background 0.2s;
}
.btn-icon svg { flex-shrink: 0; }
.btn-icon:disabled { opacity: 0.35; cursor: default; }
.btn-icon:hover:not(:disabled) { background: rgba(81,207,102,0.1); }
.btn-icon-danger { color: var(--red); border-color: var(--red); }
.btn-icon-danger:hover:not(:disabled) { background: rgba(255,107,107,0.1); }

/* Content area */
#content { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
#map-wrap { flex: 1; min-height: 0; position: relative; }
#map { height: 100%; min-height: 0; background: #111; }

.btn-map-full {
  display: none;
}

#bottom-strip {
  display: flex; height: 200px; min-height: 200px;
  background: var(--surface); border-top: 1px solid var(--border);
}
#bottom-strip #panel-results {
  width: 220px; min-width: 220px;
  border-radius: 0; border-right: 1px solid var(--border);
  overflow-y: auto; margin: 0;
}
#profile-container {
  flex: 1; min-width: 0;
  padding: 0.5rem;
}
#profile-container { background: var(--surface); }
#profile-chart { display: block; width: auto !important; }

/* Layer control */
.leaflet-control-layers {
  background: var(--surface) !important;
  border: 1px solid var(--border) !important;
  border-radius: var(--radius) !important;
  color: var(--text) !important;
}
.leaflet-control-layers-toggle {
  background-color: var(--surface2) !important;
  width: 40px !important;
  height: 40px !important;
  background-size: 24px 24px !important;
}
.leaflet-control-zoom a {
  width: 36px !important;
  line-height: 34px !important;
  font-size: 20px !important;
}
.leaflet-control-layers label { color: var(--text) !important; }

/* Leaflet popup overrides */
.leaflet-popup-content-wrapper { background: var(--surface2); color: var(--text); border-radius: var(--radius); }
.leaflet-popup-tip { background: var(--surface2); }

.dist-tooltip {
  background: rgba(26,28,46,0.85); color: #4dabf7; border: 1px solid #4dabf7;
  border-radius: 4px; padding: 2px 8px; font-size: 0.8rem; font-weight: 600;
  box-shadow: none;
}
.dist-tooltip::before { display: none; }

/* Help button */
.btn-help {
  margin-left: auto;
  flex-shrink: 0;
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--accent); font-size: 1rem; font-weight: 700;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: background 0.2s;
}
.btn-help:hover { background: rgba(77,171,247,0.15); }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0; z-index: 10000;
  padding: max(0.75rem, env(safe-area-inset-top)) max(0.75rem, env(safe-area-inset-right)) max(0.75rem, env(safe-area-inset-bottom)) max(0.75rem, env(safe-area-inset-left));
  background: rgba(0,0,0,0.65);
  display: flex; align-items: center; justify-content: center;
  -webkit-overflow-scrolling: touch;
}
.modal {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  width: 640px; max-width: 100%; max-height: min(85vh, 100dvh - 1.5rem); overflow-y: auto;
  padding: 1.25rem 1.25rem 1.5rem; position: relative; color: var(--text);
}
.modal h2 { font-size: 1.15rem; color: var(--accent); margin-bottom: 1rem; }
.modal-close {
  position: absolute; top: 0.5rem; right: 0.6rem;
  min-width: 44px; min-height: 44px;
  display: flex; align-items: center; justify-content: center;
  background: none; border: none; color: var(--text-dim);
  font-size: 1.5rem; cursor: pointer; line-height: 1;
  padding: 0;
}
.modal-close:hover { color: var(--text); }

.help-section { margin-bottom: 1.2rem; }
.help-section h3 { font-size: 0.9rem; color: var(--accent); margin-bottom: 0.4rem; }
.help-section p { font-size: 0.82rem; color: var(--text-dim); margin-bottom: 0.3rem; line-height: 1.5; }
.help-section ul.help-list {
  font-size: 0.82rem;
  color: var(--text-dim);
  margin: 0 0 0.5rem 0;
  padding-left: 1.15rem;
  line-height: 1.5;
}
.help-section ul.help-list li { margin-bottom: 0.45rem; }
.help-section ul.help-list li:last-child { margin-bottom: 0.25rem; }

.help-table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0 -0.25rem;
  padding: 0 0.25rem;
}
.help-table-wrap .help-table { min-width: 520px; }
.help-table { width: 100%; border-collapse: collapse; font-size: 0.8rem; }
.help-table td { padding: 0.35rem 0.5rem; border-bottom: 1px solid var(--border); vertical-align: top; }
.help-table .ht-field { color: var(--text); font-weight: 600; white-space: nowrap; width: 140px; }
.help-table td:last-child { color: var(--text-dim); line-height: 1.45; }

.tag { display: inline-block; width: 20px; height: 20px; border-radius: 50%; text-align: center; line-height: 20px; font-size: 0.7rem; font-weight: 700; color: #fff; }
.tag-ap { background: #4dabf7; }
.tag-st { background: #51cf66; }

/* План сети — виртуальные передатчики */
.network-plan-hint {
  font-size: 0.72rem;
  color: var(--text-dim);
  line-height: 1.35;
  margin: 0.35rem 0 0.5rem;
}
.network-plan-controls label {
  margin-top: 0.35rem;
}
.btn-network {
  width: 100%;
  margin-top: 0.4rem;
  font-size: 0.8rem;
  padding: 0.4rem 0.5rem;
}
.network-plan-btn-row {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.4rem;
}
.network-plan-btn-row > .btn-secondary {
  flex: 1;
  width: auto;
  min-width: 0;
  margin-top: 0;
}

/* Nodes panel */
.panel label.nodes-show-layer {
  gap: 0.45rem;
}
.nodes-show-layer {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.82rem;
  margin-bottom: 0.5rem;
  cursor: pointer;
  user-select: none;
}
.nodes-show-layer input[type="checkbox"] {
  width: 1.125rem;
  height: 1.125rem;
  flex-shrink: 0;
  margin: 0;
  accent-color: var(--accent, #4dabf7);
}

.nodes-info {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 0.78rem; margin-bottom: 0.4rem;
}
.nodes-info .text-dim { color: var(--text-dim); font-size: 0.7rem; }
.btn-nodes {
  width: 100%; padding: 0.45rem; border-radius: var(--radius);
  font-size: 0.78rem; font-weight: 600; cursor: pointer;
  background: var(--surface2); color: var(--accent); border: 1px solid var(--border);
  transition: background 0.2s;
}
.btn-nodes:hover { background: rgba(77,171,247,0.1); }
.btn-nodes:disabled { opacity: 0.4; cursor: default; }

/* Marker clusters — dark theme */
.marker-cluster div {
  background: rgba(77, 171, 247, 0.6);
  border-radius: 50%; width: 30px; height: 30px;
  display: flex; align-items: center; justify-content: center;
}
.marker-cluster span {
  color: #fff; font-size: 0.75rem; font-weight: 700;
}
.marker-cluster-small { background: rgba(77, 171, 247, 0.25); }
.marker-cluster-small div { background: rgba(77, 171, 247, 0.5); }
.marker-cluster-medium { background: rgba(252, 196, 25, 0.25); }
.marker-cluster-medium div { background: rgba(252, 196, 25, 0.5); }
.marker-cluster-large { background: rgba(255, 107, 107, 0.25); }
.marker-cluster-large div { background: rgba(255, 107, 107, 0.5); }

.marker-cluster-meshcore.marker-cluster-small { background: rgba(132, 94, 247, 0.25); }
.marker-cluster-meshcore.marker-cluster-small div { background: rgba(132, 94, 247, 0.55); }
.marker-cluster-meshcore.marker-cluster-medium { background: rgba(250, 176, 5, 0.22); }
.marker-cluster-meshcore.marker-cluster-medium div { background: rgba(250, 176, 5, 0.5); }
.marker-cluster-meshcore.marker-cluster-large { background: rgba(255, 107, 107, 0.22); }
.marker-cluster-meshcore.marker-cluster-large div { background: rgba(255, 107, 107, 0.48); }

/* Node triangle markers */
.node-marker { background: none !important; border: none !important; }
.node-online svg { filter: drop-shadow(0 0 3px rgba(81,207,102,0.5)); }
.node-offline svg { opacity: 0.6; }
.meshcore-marker.meshcore-online svg { filter: drop-shadow(0 0 3px rgba(81,207,102,0.45)); }
.meshcore-marker.meshcore-offline svg { opacity: 0.65; }

/* Node popup */
.node-popup { font-size: 0.8rem; line-height: 1.5; }
.node-popup code { background: var(--bg); padding: 1px 4px; border-radius: 3px; font-size: 0.75rem; }

/* Loading indicator */
.loading { position: relative; }
.loading::after {
  content: ''; position: absolute; top: 50%; left: 50%;
  width: 24px; height: 24px; margin: -12px 0 0 -12px;
  border: 3px solid var(--border); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Tablets & phones */
@media (max-width: 900px) {
  .mobile-tabs {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
    padding: 0.5rem 0.85rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
  }

  .tab-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border);
    background: var(--surface2);
    color: var(--text-dim);
    border-radius: var(--radius);
    min-height: 32px;
    font-size: 0.85rem;
    font-weight: 700;
    cursor: pointer;
  }

  .tab-btn[aria-selected="true"] {
    background: rgba(77,171,247,0.18);
    border-color: rgba(77,171,247,0.7);
    color: var(--text);
  }

  header {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "title help"
      "hint hint";
    align-items: start;
    gap: 0rem 0.6rem;
    padding: 0.3rem 0.85rem;
  }
  header h1 {
    grid-area: title;
    white-space: normal;
    font-size: 0.82rem;
    line-height: 1.25;
    overflow: visible;
    text-overflow: unset;
  }
  header .hint { display: none; }
  .btn-help {
    grid-area: help;
    margin-left: 0;
    align-self: start;
    justify-self: end;
  }

  #main {
    flex-direction: column;
  }

  /* Layout: Map (in #content) -> Tabs -> Content below */
  #content { order: 0; }
  #sidebar { order: 2; }

  /* Tabs: map is always visible; switch params vs results below map */
  body.mobile-tab-params #sidebar { display: flex; }
  body.mobile-tab-params #bottom-strip { display: none; }

  body.mobile-tab-results #sidebar { display: none; }
  body.mobile-tab-results #bottom-strip { display: flex; }

  #content {
    order: 0;
    flex: 0 0 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
  }

  /* Map always visible on mobile tabs */
  #map-wrap { display: block; }

  #map-wrap {
    flex: 0 0 auto;
    min-height: 32vh;
  }

  /* Mobile-only button to expand/collapse map */
  .btn-map-full {
    display: inline-flex;
    position: absolute;
    right: max(0.75rem, env(safe-area-inset-right, 0));
    bottom: max(0.75rem, env(safe-area-inset-bottom, 0));
    z-index: 500;
    align-items: center;
    justify-content: center;
    padding: 0.45rem 0.6rem;
    border-radius: 999px;
    border: 1px solid rgba(255,255,255,0.18);
    background: rgba(35, 38, 64, 0.85);
    color: var(--text);
    font-size: 0.78rem;
    font-weight: 700;
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    cursor: pointer;
  }

  #bottom-strip {
    flex-direction: column;
    height: auto;
    min-height: 0;
    flex: 0 0 auto;
    max-height: 55vh;
    overflow: hidden;
  }

  #bottom-strip #panel-results {
    width: 100%;
    min-width: 0;
    max-height: none;
    border-right: none;
    border-bottom: 1px solid var(--border);
    overflow: visible;
  }

  #profile-container {
    flex: 0 0 auto;
    height: clamp(240px, 34vh, 420px);
    min-height: 240px;
    max-height: 420px;
  }

  #profile-chart {
    height: 100% !important;
  }

  /* Hide chart area until we have data (reduces scrolling) */
  body:not(.has-profile) #profile-container {
    display: none;
  }

  #sidebar {
    width: 100%;
    min-width: 0;
    max-height: none;
    flex: 1 1 auto;
    flex-direction: column;
    flex-wrap: nowrap;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    border-right: none;
    border-top: 1px solid var(--border);
    padding-bottom: max(1rem, env(safe-area-inset-bottom, 0));
  }

  .panel {
    flex: none;
    min-width: 0;
  }

  .panel label {
    flex-direction: column;
    align-items: stretch;
    gap: 0.35rem;
  }

  .panel label.nodes-show-layer {
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    gap: 0.5rem;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  .panel label.nodes-show-layer input[type="checkbox"] {
    width: 1.25rem;
    height: 1.25rem;
    flex: 0 0 auto;
    align-self: center;
    margin: 0;
  }

  .panel input[type="number"],
  .panel input[type="text"] {
    width: 100%;
    max-width: 100%;
    padding: 0.5rem 0.65rem;
    font-size: 16px;
    text-align: left;
  }

  .panel select {
    width: 100%;
    padding: 0.5rem 0.65rem;
    font-size: 16px;
  }

  .btn-primary,
  .btn-secondary {
    min-height: 36px;
    padding: 0.45rem 0.75rem;
    font-size: 0.88rem;
  }

  .btn-icon {
    min-height: 36px;
    padding: 0.45rem 0.75rem;
    font-size: 0.8rem;
  }

  .btn-nodes {
    min-height: 36px;
    padding: 0.45rem 0.75rem;
    font-size: 0.82rem;
  }

  .btn-help {
    width: 32px;
    height: 32px;
    font-size: 0.95rem;
  }

  .result-grid {
    font-size: 0.78rem;
    grid-template-columns: auto 1fr auto 1fr;
    column-gap: 0.6rem;
    row-gap: 0.25rem;
  }

  .modal {
    padding: 1rem 1rem 1.25rem;
  }
  .help-table .ht-field { white-space: normal; }
}

@media (max-width: 380px) {
  .result-grid {
    grid-template-columns: auto 1fr;
    gap: 0.2rem 0.35rem;
  }
}

/* Map fullscreen mode (mobile) */
body.map-fullscreen {
  overflow: hidden;
  touch-action: none;
}

body.map-fullscreen header,
body.map-fullscreen #sidebar,
body.map-fullscreen #bottom-strip {
  display: none !important;
}

body.map-fullscreen #main {
  height: 100dvh;
}

body.map-fullscreen #content {
  position: fixed;
  inset: 0;
  z-index: 20000;
}

body.map-fullscreen #map-wrap {
  height: 100%;
}

body.map-fullscreen #map {
  min-height: 0;
  height: 100%;
}

body.map-fullscreen .btn-map-full {
  bottom: max(0.75rem, env(safe-area-inset-bottom, 0));
  right: max(0.75rem, env(safe-area-inset-right, 0));
}
