/* Base */
:root {
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;

  /* Chart colours live with the map paint they mirror (js/basemap.js,
     js/route-layers.js); these are the chrome's own palette. */
  --bg: #0b0f12;
  --panel: 15, 20, 24;        /* alpha varies by surface, so keep the channels */
  --accent: 94, 200, 216;
  --text: #dfe6e8;
  --text-dim: #aab4b6;
  --text-faint: #8a9698;
  --border: rgba(255, 255, 255, 0.12);
  --border-input: rgba(255, 255, 255, 0.18);
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.5);

  /* Raised control surfaces, and the destructive/error accent. */
  --control: rgba(255, 255, 255, 0.08);
  --control-hover: rgba(255, 255, 255, 0.14);
  --danger: 255, 106, 92;
  --danger-text: #ffd2cd;
}
html, body { margin: 0; height: 100%; background: var(--bg); font-family: var(--font); }
#map { position: absolute; inset: 0; }

/* Panels */
.panel {
  position: absolute;
  background: rgba(var(--panel), 0.85);
  color: var(--text);
  font: 12px/1.4 var(--font);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
}
.panel .title { font-weight: 600; margin-bottom: 6px; letter-spacing: 0.02em; }
.panel .row { display: flex; align-items: center; gap: 8px; margin: 3px 0; }
.panel .hint { color: var(--text-faint); }

.toast {
  position: fixed;
  z-index: 30;
  left: 50%;
  top: 16px;
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: min(420px, calc(100vw - 32px));
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: rgba(var(--panel), 0.95);
  color: var(--text);
  font: 12px/1.4 var(--font);
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -8px);
  transition: opacity 160ms ease, transform 160ms ease;
}
.toast.open { opacity: 1; transform: translate(-50%, 0); }
.toast.error { border-color: rgba(var(--danger), 0.6); background: rgba(44, 17, 15, 0.97); color: var(--danger-text); }
.toast.busy { border-color: rgba(245, 166, 35, 0.5); color: #f5a623; }
.toast.success { border-color: rgba(31, 174, 74, 0.5); color: #8be3a6; }
.toast .spinner {
  width: 11px;
  height: 11px;
  flex: none;
  border: 2px solid rgba(245, 166, 35, 0.3);
  border-top-color: #f5a623;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .toast { transition: none; }
  .toast .spinner { animation-duration: 2.4s; }
}

/* Disclosures: the panels themselves (#info, #route, #layers, #legend) and #advanced */
.panel summary {
  display: flex;
  align-items: center;
  color: var(--text);
  font-weight: 600;
  letter-spacing: 0.02em;
  cursor: pointer;
  list-style: none;
}
.panel summary::-webkit-details-marker { display: none; }
.panel summary::after {
  content: "+";
  margin-left: auto;
  padding-left: 12px;
  color: var(--text-faint);
  line-height: 1;
}
.panel summary:hover::after { color: var(--text); }
/* Hovering a control inside the summary (repo link, info icon) is not
   hovering the toggle, so the marker stays dim. */
.panel summary:has(a:hover, .info:hover)::after { color: var(--text-faint); }
details[open] > summary { margin-bottom: 6px; }
details[open] > summary::after { content: "\2212"; }

/* Hover notes */
.info {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  margin-left: 6px;
  border: 1px solid rgb(var(--accent));
  border-radius: 50%;
  color: rgb(var(--accent));
  font-size: 10px;
  font-style: italic;
  font-weight: 600;
  cursor: help;
  user-select: none;
  flex: none;
}
.info:hover { background: rgb(var(--accent)); color: var(--bg); }

.note {
  display: none;
  position: absolute;
  z-index: 20;
  width: 260px;
  padding: 9px 11px;
  background: rgba(var(--panel), 0.97);
  border: 1px solid rgba(var(--accent), 0.35);
  border-radius: 4px;
  color: var(--text-dim);
  font: 11px/1.45 var(--font);
  text-align: left;
  cursor: default;
  box-shadow: var(--shadow);
}
.info:hover .note { display: block; }
.note b { display: block; color: var(--text); font-weight: 600; }
.note i { display: block; font-style: normal; margin-top: 1px; }
.note .topic { margin-top: 6px; break-inside: avoid-column; }
.note .topic:first-child { margin-top: 0; }
.note .note-footer { display: block; column-span: all; margin-top: 8px; color: var(--text-faint); }

/* Info */
#info { top: 10px; right: 60px; width: 300px; }
#info > summary { font-size: 14px; }
#info .repo-link { display: inline-flex; margin-left: 8px; color: var(--text-faint); }
#info .repo-link:hover { color: var(--text); }
#info p { margin: 0 0 8px; color: var(--text-dim); }

/* Route */
#route { top: 24px; left: 24px; }
#route .route-body { display: flex; align-items: flex-start; gap: 16px; }
#route .route-main { width: 210px; flex: none; }
#route .row { justify-content: space-between; }
#route .hint { margin-top: 6px; }
#route .note {
  left: 20px;
  top: -6px;
  width: min(940px, calc(100vw - 470px));
  columns: 3;
  column-gap: 20px;
}
#route .coordinate-field { display: block; margin: 5px 0; }
#route .coordinate-field span { display: block; color: var(--text-dim); margin-bottom: 2px; }
#route .coordinate-field small { color: var(--text-faint); font-size: inherit; }
#route input.num,
#route input.coordinate {
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--border-input);
  border-radius: 3px;
  color: var(--text);
}
#route input.num { width: 70px; font: inherit; padding: 2px 5px; }
#route input.coordinate {
  box-sizing: border-box;
  width: 100%;
  font: 12px/1.4 var(--font);
  padding: 3px 5px;
}
#route input.coordinate::placeholder { color: #6f7b7e; }
#route input[type=checkbox] { accent-color: rgb(var(--accent)); cursor: pointer; }
#route label.check { justify-content: flex-start; gap: 6px; cursor: pointer; }

#route .coordinate-field .waypoint-row { display: flex; gap: 4px; margin-bottom: 0; }
#route .waypoint-row input.coordinate { flex: 1; min-width: 0; }
#route .waypoint-remove {
  flex: none;
  width: 20px;
  margin: 0;
  padding: 0;
  background: var(--control);
  border: 1px solid var(--border-input);
  border-radius: 3px;
  color: var(--text-dim);
  font: inherit;
  line-height: 18px;
  cursor: pointer;
}
#route .waypoint-remove:hover { background: rgba(var(--danger), 0.25); border-color: rgba(var(--danger), 0.5); color: var(--danger-text); }
#route .waypoint-add {
  margin: 2px 0 6px;
  padding: 3px 0;
  background: none;
  border: 1px dashed var(--border-input);
  color: var(--text-dim);
}
#route .waypoint-add:hover { background: none; color: var(--text); border-color: var(--text-faint); }

#advanced { width: 215px; flex: none; }
/* Collapsed, it is not a column: the panel stacks and narrows to a single
   column, leaving no empty space beside the route fields. The width is pinned
   because a closed <details> still lets its hidden rows drive the flex
   container's intrinsic width. */
#route .route-body:has(#advanced:not([open])) { flex-direction: column; gap: 6px; }
#route .route-body:has(#advanced:not([open])) #advanced { width: 210px; }
#advanced .row { margin-top: 5px; }

#route button {
  width: 100%;
  margin-top: 8px;
  padding: 4px 0;
  background: var(--control);
  border: 1px solid var(--border-input);
  border-radius: 3px;
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
#route button:hover { background: var(--control-hover); }
#route .route-actions { display: flex; gap: 6px; }
#route .route-actions button { flex: 1; width: auto; }
#route.busy input,
#route.busy button { opacity: 0.45; pointer-events: none; }
#route .distance { margin-top: 8px; font-weight: 600; text-align: center; min-height: 1.4em; }

/* Layers */
#layers { bottom: 44px; right: 10px; }
#layers-body { max-height: calc(100vh - 110px); overflow-y: auto; }
#layers-body:has(.info:hover) { overflow: visible; }
#layers .hint { margin: 2px 0 4px; }
#layers label.row { cursor: pointer; }
#layers input { accent-color: rgb(var(--accent)); cursor: pointer; }
#layers:not(.searching) .search { display: none; }
#layers .title.search {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 10px;
}
#layers .title.search .note { right: 20px; bottom: -6px; width: 290px; }
#layers .search-stage {
  margin: 7px 0 0 8px;
  padding-left: 8px;
  border-left: 1px solid rgba(var(--accent), 0.3);
  color: var(--text-dim);
}
#layers .search-stage > span { display: block; font-weight: 600; }
#layers .search-stage .row { margin-left: 2px; color: var(--text); }
/* Selectors stay disabled until the route returns geometry for them, and most
   requests never run the later passes, so show only what came back. */
#layers label:has(input[data-search-selector]:disabled),
#layers .search-stage:not(:has(input:not(:disabled))),
#layers:not(:has(.search input:not(:disabled))) .title.search { display: none; }

/* Legend */
#legend { bottom: 24px; left: 24px; }
#legend-body {
  display: grid;
  grid-template-columns: repeat(3, max-content);
  gap: 16px;
}
#legend .legend-heading { color: var(--text-dim); font-weight: 600; margin-bottom: 4px; }
#legend .swatch { width: 12px; height: 12px; border-radius: 2px; flex: none; }
#legend .swatch.dot { border-radius: 50%; }

/* Chart colours, so these mirror the map paint rather than the chrome palette:
   depths and buoys from js/basemap.js, routes and search from js/route-layers.js.
   The search swatches name one representative colour; each pass tints its own. */
#legend .swatch.depth-0-2 { background: #ff6a5c; }
#legend .swatch.depth-2-5 { background: #ffb347; }
#legend .swatch.depth-5-10 { background: #5ec8d8; }
#legend .swatch.depth-10-20 { background: #2f7fa8; }
#legend .swatch.depth-20-50 { background: #4ba3bf; }
#legend .swatch.depth-50-plus { background: #7cc4d6; }
#legend .swatch.port { background: #1fae4a; }
#legend .swatch.starboard { background: #e4232f; }
#legend .swatch.preferred { background: #f5a623; }
#legend .swatch.route-centerline { background: #b06ef5; }
#legend .swatch.route-channel { background: #ffa94d; }
#legend .swatch.route-open { background: #63b3ed; }
#legend .swatch.raw-route { background: #ffd166; }
#legend .swatch.expanded-cell { background: #5ec8d8; }
#legend .swatch.expanded-centerline { background: #b06ef5; }

/* Right-click menu */
#menu {
  display: none;
  z-index: 10;
  padding: 4px;
  min-width: 150px;
}
#menu button {
  display: block;
  width: 100%;
  padding: 5px 8px;
  background: none;
  border: 0;
  border-radius: 3px;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
#menu button:hover { background: rgba(var(--accent), 0.22); }

/* Harbor popup */
.harbor-map-popup .maplibregl-popup-content {
  padding: 10px;
  background: rgba(var(--panel), 0.98);
  border: 1px solid rgba(var(--accent), 0.35);
  border-radius: 5px;
  box-shadow: var(--shadow);
}
.harbor-map-popup.maplibregl-popup-anchor-top .maplibregl-popup-tip { border-bottom-color: #0f1418; }
.harbor-map-popup.maplibregl-popup-anchor-bottom .maplibregl-popup-tip { border-top-color: #0f1418; }
.harbor-map-popup.maplibregl-popup-anchor-left .maplibregl-popup-tip { border-right-color: #0f1418; }
.harbor-map-popup.maplibregl-popup-anchor-right .maplibregl-popup-tip { border-left-color: #0f1418; }
.harbor-map-popup .maplibregl-popup-close-button { color: var(--text-dim); font-size: 16px; }
.harbor-map-popup .maplibregl-popup-close-button:hover { color: var(--text); background: transparent; }
.harbor-popup { color: var(--text); font: 12px/1.4 var(--font); }
.harbor-popup .name { font-weight: 600; }
.harbor-popup .meta { color: var(--text-dim); margin-top: 2px; }
.harbor-popup .actions { display: flex; gap: 6px; margin-top: 8px; }
.harbor-popup button {
  flex: 1;
  padding: 3px 7px;
  border: 1px solid rgba(var(--accent), 0.45);
  border-radius: 3px;
  background: rgba(var(--accent), 0.12);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
.harbor-popup button:hover { background: rgba(var(--accent), 0.25); }

/* Responsive */
@media (max-width: 1000px) {
  #route .note { width: calc(100vw - 300px); columns: 2; }
}
@media (max-width: 720px) {
  .info:focus-within .note { display: block; }
  .note,
  #route .note,
  #layers .title.search .note {
    position: fixed;
    inset: auto 8px 8px 8px;
    width: auto;
    max-height: 60vh;
    overflow-y: auto;
    columns: 1;
  }

  /* Leave the right edge to the map's zoom control. */
  #route {
    inset: 8px 50px auto 8px;
    padding: 8px 10px;
    max-height: 40vh;
    max-height: 40dvh;
    overflow-y: auto;
  }
  #route .route-body { flex-direction: column; gap: 6px; }
  #route .route-body:has(#advanced:not([open])) #advanced { width: auto; }
  #route .route-main,
  #advanced { width: auto; }
  #route .hint { display: none; }
  #route .coordinate-field { margin: 3px 0; }

  /* Fields span the panel; Draft and the buttons share a row. */
  #route .route-main { display: grid; grid-template-columns: 1fr 1fr; column-gap: 8px; }
  #route .route-main > * { grid-column: 1 / -1; }
  #route .route-main > .row,
  #route .route-main > .route-actions { grid-column: auto; }
  #route .route-actions button { margin-top: 0; }

  #info { inset: auto auto 88px 8px; width: auto; max-width: calc(100vw - 16px); }
  #layers { inset: auto 8px 32px auto; max-width: calc(100vw - 16px); }
  #legend { inset: auto auto 32px 8px; max-width: calc(100vw - 16px); }
  #info[open], #layers[open], #legend[open] { z-index: 5; }
  #layers-body { max-height: 50vh; max-height: 50dvh; }
  #legend-body { grid-template-columns: 1fr; gap: 8px; max-height: 50vh; overflow-y: auto; }
}
@media (pointer: coarse) {
  #route button,
  #menu button { min-height: 36px; }
  #route .waypoint-remove { width: 36px; }
  #route input.num,
  #route input.coordinate { font-size: 16px; padding: 6px 8px; }
  .panel .row { min-height: 28px; }
  .info { width: 22px; height: 22px; }
}
