:root {
  --bg: #f3f6f8;
  --surface: #ffffff;
  --surface-2: #f7f9fb;
  --text: #11232e;
  --muted: #586a75;
  --line: #dbe3e8;
  --brand: #0b3954;
  --brand-ink: #ffffff;
  --accent: #d7263d;
  --sea: #1769a5;
  --sea-soft: #e3eff8;
  --route: #3d86bf;
  --route-active: #d7263d;
  --port: #0b3954;
  --port-ring: #ffffff;
  --logo-ink: #1c2329;
  --brand-text: #0b3954;
  --focus: #f2a541;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(17 35 46 / 6%), 0 6px 20px rgb(17 35 46 / 7%);
  --header-h: 56px;
  --sidebar-w: 420px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d161c;
    --surface: #142029;
    --surface-2: #182733;
    --text: #e4edf2;
    --muted: #9ab0bd;
    --line: #253744;
    --brand: #0a2a3d;
    --sea: #74b6e6;
    --sea-soft: #1a3446;
    --route: #5aa2d8;
    --route-active: #ff5a6e;
    --port: #f2f6f8;
    --port-ring: #0d161c;
    --logo-ink: #e4edf2;
    --brand-text: #e4edf2;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 6px 20px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}

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

html, body { height: 100%; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--sea); }

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

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

button { font: inherit; color: inherit; }

/* ---------- Topbar ---------- */

.topbar {
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  color: var(--text);
  position: relative;
  z-index: 1000;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--logo-ink);
  text-decoration: none;
}

.logo { display: block; height: 40px; width: auto; }

.brand-name {
  font-size: 1.3rem;
  font-weight: 750;
  letter-spacing: -0.015em;
  color: var(--brand-text);
}

.tagline {
  color: var(--muted);
  font-size: 0.95rem;
  padding-left: 14px;
  border-left: 1px solid var(--line);
}

.topbar-link {
  margin-left: auto;
  background: none;
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 0.9rem;
  cursor: pointer;
}
.topbar-link:hover { border-color: var(--sea); background: var(--surface-2); }

/* ---------- Layout ---------- */

.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: calc(100dvh - var(--header-h));
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
  border-right: 1px solid var(--line);
}

.controls {
  padding: 16px 16px 8px;
  border-bottom: 1px solid var(--line);
}

.results-wrap {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
}

.map-wrap { position: relative; min-height: 0; }
#map { position: absolute; inset: 0; background: var(--sea-soft); }

/* ---------- Search ---------- */

.search-field { position: relative; }

.search-icon {
  position: absolute;
  left: 14px; top: 50%;
  width: 20px; height: 20px;
  transform: translateY(-50%);
  fill: var(--muted);
  pointer-events: none;
}

#q {
  width: 100%;
  padding: 12px 14px 12px 44px;
  font: inherit;
  font-size: 1.05rem;
  color: var(--text);
  background: var(--surface-2);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  transition: border-color .15s, background .15s;
}
#q::placeholder { color: var(--muted); }
#q:focus { outline: none; border-color: var(--sea); background: var(--surface); box-shadow: 0 0 0 4px var(--sea-soft); }

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 12px;
}

.chip {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 0.875rem;
  cursor: pointer;
  color: var(--muted);
}
.chip:hover { border-color: var(--sea); color: var(--text); }
.chip[aria-pressed="true"] {
  background: var(--brand);
  border-color: var(--brand);
  color: var(--brand-ink);
}
@media (prefers-color-scheme: dark) {
  .chip[aria-pressed="true"] { background: var(--sea); border-color: var(--sea); color: #0d161c; }
}

.count {
  margin: 12px 2px 4px;
  font-size: 0.85rem;
  color: var(--muted);
}

.view-toggle { display: none; }

/* ---------- Results ---------- */

.results { padding: 0 8px 8px; }

.group-title {
  position: sticky;
  top: 0;
  z-index: 1;
  margin: 0 -8px;
  padding: 14px 22px 6px;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  background: linear-gradient(var(--surface) 80%, transparent);
}
.group-count { font-weight: 500; opacity: .7; margin-left: 4px; }

.route-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.route {
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 4px;
  border: 1px solid transparent;
  transition: background .12s, border-color .12s;
}
.route:hover { background: var(--surface-2); }
.route[aria-current="true"] {
  background: var(--sea-soft);
  border-color: color-mix(in srgb, var(--sea) 35%, transparent);
}

.route-main {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}

.route-title {
  flex: 1;
  font-weight: 650;
  font-size: 1.02rem;
  line-height: 1.3;
}
.route-title .port { display: inline-block; max-width: 100%; }
.route-title .area { font-weight: 400; color: var(--muted); }
.route-title .country {
  font-weight: 500;
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
  margin-left: 2px;
  vertical-align: 2px;
}
.route-title .sep { color: var(--accent); margin: 0 0.3em; font-weight: 700; }

.pin {
  flex: none;
  width: 18px; height: 18px;
  margin-top: 2px;
  fill: var(--muted);
  opacity: .55;
}
.route-main:hover .pin, .route[aria-current="true"] .pin { opacity: 1; fill: var(--accent); }

.badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 1px 7px;
  border-radius: 999px;
  background: #fff3d6;
  color: #7a4d00;
  margin-left: 6px;
  vertical-align: 2px;
}
@media (prefers-color-scheme: dark) { .badge { background: #3d2f12; color: #f5c56b; } }

.ops { margin: 6px 0 0; padding: 0; list-style: none; }
.op { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; font-size: 0.92rem; margin-top: 3px; }
.op-name { color: var(--muted); }
.op a { text-decoration: none; white-space: nowrap; }
.op a:hover { text-decoration: underline; }
.note { font-size: 0.85rem; color: var(--muted); margin: 4px 0 0; }

.empty { padding: 24px 20px; color: var(--muted); }
.empty strong { color: var(--text); }

.sidebar-footer {
  padding: 16px 20px 24px;
  font-size: 0.82rem;
  color: var(--muted);
  border-top: 1px solid var(--line);
  margin-top: 8px;
}
.sidebar-footer p { margin: 0 0 6px; }

.inline-link {
  background: none;
  border: 0;
  padding: 0;
  color: var(--sea);
  text-decoration: underline;
  cursor: pointer;
  font-size: inherit;
}

/* ---------- Map ---------- */

.leaflet-container { font: inherit; }

@media (prefers-color-scheme: dark) {
  .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(0.85) contrast(0.9); }
  .leaflet-control-zoom a, .leaflet-control-attribution { background: var(--surface) !important; color: var(--text) !important; }
  .leaflet-control-attribution a { color: var(--sea) !important; }
  .leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--text); }
}

.leaflet-popup-content { margin: 12px 14px; min-width: 200px; }
.port-popup h3 { margin: 0 0 2px; font-size: 1rem; }
.port-popup .area { color: var(--muted); font-size: 0.85rem; margin: 0 0 8px; }
.port-popup ul { list-style: none; margin: 0; padding: 0; }
.port-popup li { margin: 4px 0; }
.port-popup button {
  background: none;
  border: 0;
  padding: 0;
  color: var(--sea);
  cursor: pointer;
  text-align: left;
  font-size: 0.92rem;
}
.port-popup button:hover { text-decoration: underline; }
.port-popup .op-hint { color: var(--muted); font-size: 0.8rem; display: block; }

.leaflet-tooltip.port-label {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  font-weight: 600;
}

/* ---------- Dialog ---------- */

dialog {
  width: min(520px, calc(100vw - 32px));
  border: 0;
  border-radius: 16px;
  padding: 28px 28px 20px;
  color: var(--text);
  background: var(--surface);
  box-shadow: 0 20px 60px rgb(0 0 0 / 30%);
}
dialog::backdrop { background: rgb(11 57 84 / 45%); backdrop-filter: blur(2px); }
dialog h2 { margin: 0 0 12px; }
.dialog-close-form { position: absolute; top: 10px; right: 12px; margin: 0; }
.dialog-close {
  border: 0; background: none; font-size: 1.6rem; line-height: 1;
  width: 36px; height: 36px; border-radius: 50%; cursor: pointer; color: var(--muted);
}
.dialog-close:hover { background: var(--surface-2); color: var(--text); }
.signature { font-style: italic; }
.fineprint { font-size: 0.82rem; color: var(--muted); border-top: 1px solid var(--line); padding-top: 12px; margin-top: 18px; }

.noscript { padding: 24px; text-align: center; }

/* ---------- Mobile ---------- */

@media (max-width: 799px) {
  :root { --header-h: 52px; }
  .topbar { padding: 0 12px; gap: 10px; }
  .tagline { display: none; }

  .layout {
    display: flex;
    flex-direction: column;
  }
  .sidebar { border-right: 0; flex: 1; min-width: 0; }
  .controls { padding: 12px 12px 6px; }

  .chips {
    flex-wrap: nowrap;
    overflow-x: auto;
    margin: 12px -12px 0;
    padding: 0 12px 2px;
    scrollbar-width: none;
  }
  .chips::-webkit-scrollbar { display: none; }
  .chip { flex: none; }

  .view-toggle {
    display: flex;
    margin-top: 12px;
    background: var(--surface-2);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 3px;
  }
  .view-toggle button {
    flex: 1;
    border: 0;
    background: none;
    padding: 7px;
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.92rem;
    color: var(--muted);
    cursor: pointer;
  }
  .view-toggle button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

  .layout[data-view="list"] .map-wrap { display: none; }
  .layout[data-view="map"] .results-wrap { display: none; }
  .layout[data-view="map"] .sidebar { flex: none; }
  .layout[data-view="map"] .map-wrap { flex: 1; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; scroll-behavior: auto !important; }
}
