*, *::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, body { height: 100%; font-family: 'Segoe UI', system-ui, sans-serif; background: var(--bg); color: var(--text); }

header {
  display: flex; align-items: center; gap: 1.5rem;
  padding: 0.6rem 1.2rem;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
header h1 { font-size: 1.1rem; font-weight: 600; white-space: nowrap; }
header .hint { font-size: 0.78rem; color: var(--text-dim); }

#main {
  display: flex; height: calc(100vh - 44px);
}

/* 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 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;
}

/* 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, .btn-danger {
  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-danger { background: transparent; color: var(--red); border: 1px solid var(--red); }
.btn-share {
  width: 100%; padding: 0.55rem; border-radius: var(--radius);
  font-size: 0.82rem; font-weight: 600; cursor: pointer; transition: opacity 0.2s;
  background: var(--surface2); color: var(--green); border: 1px solid var(--green);
}
.btn-share:disabled { opacity: 0.35; cursor: default; }
.btn-share:hover:not(:disabled) { background: rgba(81,207,102,0.1); }
.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-danger:hover { background: rgba(255,107,107,0.1); }

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

#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-chart { width: 100% !important; height: 100% !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;
  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;
  background: rgba(0,0,0,0.65);
  display: flex; align-items: center; justify-content: center;
}
.modal {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  width: 640px; max-width: 92vw; max-height: 85vh; overflow-y: auto;
  padding: 1.5rem 2rem; position: relative; color: var(--text);
}
.modal h2 { font-size: 1.15rem; color: var(--accent); margin-bottom: 1rem; }
.modal-close {
  position: absolute; top: 0.8rem; right: 1rem;
  background: none; border: none; color: var(--text-dim);
  font-size: 1.5rem; cursor: pointer; line-height: 1;
}
.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-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; }

/* 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); } }

/* Responsive */
@media (max-width: 800px) {
  #main { flex-direction: column; }
  #sidebar { width: 100%; min-width: unset; max-height: 40vh; flex-direction: row; flex-wrap: wrap; }
  .panel { flex: 1; min-width: 220px; }
}
