:root {
  --navy: #0b2545;
  --navy-2: #13315c;
  --blue: #1b6ec2;
  --sky: #8da9c4;
  --sand: #eef4ed;
  --danger: #c1121f;
  --radius: 14px;
  --shadow: 0 2px 14px rgba(11, 37, 69, 0.25);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; height: 100%; overscroll-behavior: none; }
body { background: var(--navy); }

#map {
  position: fixed;
  inset: 0;
  background: #cfe0ec;
}

/* Top bar: brand + search */
.topbar {
  position: fixed;
  top: calc(10px + var(--safe-top));
  left: 10px;
  right: 70px;
  z-index: 1000;
  display: flex;
  align-items: flex-start;
  gap: 8px;
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }
.brand {
  display: flex;
  align-items: center;
  gap: 7px;
  background: var(--navy);
  color: #fff;
  padding: 8px 14px 8px 10px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.2px;
  box-shadow: var(--shadow);
  user-select: none;
  flex-shrink: 0;
  cursor: pointer;
}

/* Search */
.search {
  position: relative;
  flex: 1;
  max-width: 340px;
}
.search-ico {
  position: absolute;
  left: 11px;
  top: 9px;
  width: 18px;
  height: 18px;
  color: #5b768d;
  pointer-events: none;
}
.search input {
  width: 100%;
  padding: 9px 34px 9px 36px;
  border: none;
  border-radius: 999px;
  background: #fff;
  color: var(--navy);
  font-size: 15px;
  box-shadow: var(--shadow);
}
.search input:focus { outline: 2px solid var(--blue); }
.search input::-webkit-search-cancel-button { display: none; }
.search-clear {
  position: absolute;
  right: 6px;
  top: 5px;
  width: 26px;
  height: 26px;
  border: none;
  border-radius: 50%;
  background: var(--sand);
  color: var(--navy);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
}
.search-results {
  position: absolute;
  top: 42px;
  left: 0;
  right: 0;
  background: #fff;
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
  max-height: 50vh;
  overflow-y: auto;
}
.search-item {
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid #eef2f6;
}
.search-item:last-child { border-bottom: none; }
.search-item:hover, .search-item.active { background: var(--sand); }
.search-name { font-size: 14.5px; font-weight: 600; color: var(--navy); }
.search-sub { font-size: 12px; color: #5b768d; margin-top: 1px; }
.search-empty { padding: 12px 14px; font-size: 13px; color: #5b768d; }

/* Chart loading chip */
.chart-loading {
  position: fixed;
  top: calc(58px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 999;
  background: rgba(11, 37, 69, 0.88);
  color: #fff;
  font-size: 12.5px;
  padding: 6px 14px;
  border-radius: 999px;
  box-shadow: var(--shadow);
}

/* Tide / current station markers */
.st-icon {
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 1px 5px rgba(11, 37, 69, 0.45);
  color: #fff;
  font-size: 11px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
}
.st-icon.st-tide { background: #1b6ec2; }
.st-icon.st-curr { background: #d97706; }
.st-chip {
  display: inline-flex;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  color: #fff;
  font-size: 10px;
  font-weight: 800;
  align-items: center;
  justify-content: center;
  vertical-align: -3px;
}
.st-chip.st-tide { background: #1b6ec2; }
.st-chip.st-curr { background: #d97706; }
.tide-rows { margin-top: 6px; }
.tide-row { display: flex; gap: 8px; align-items: baseline; padding: 3px 0; font-variant-numeric: tabular-nums; }
.tide-kind { width: 64px; font-weight: 700; }
.tide-kind.hi, .tide-kind.flood { color: #1a7a3a; }
.tide-kind.lo, .tide-kind.ebb { color: #c1121f; }
.tide-kind.slack { color: #5b768d; }
.tide-when { flex: 1; }
.tide-val { color: #5b768d; }

/* GPS HUD */
.hud {
  position: fixed;
  top: calc(58px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  gap: 14px;
  background: rgba(11, 37, 69, 0.92);
  color: #fff;
  padding: 6px 16px;
  border-radius: 999px;
  box-shadow: var(--shadow);
}
.hud-item { display: flex; flex-direction: column; align-items: center; min-width: 52px; }
.hud-val { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; }
.hud-lbl { font-size: 10px; opacity: 0.75; text-transform: uppercase; letter-spacing: 0.5px; }

/* Floating action buttons */
.fabs {
  position: fixed;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.fab {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  background: #fff;
  color: var(--navy);
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.fab svg { width: 24px; height: 24px; fill: currentColor; }
.fab:active { transform: scale(0.94); }
.fab.active { background: var(--blue); color: #fff; }
.fab.tracking { background: var(--blue); color: #fff; animation: pulse 2s infinite; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(27, 110, 194, 0.5); }
  70% { box-shadow: 0 0 0 12px rgba(27, 110, 194, 0); }
  100% { box-shadow: 0 0 0 0 rgba(27, 110, 194, 0); }
}

/* Route toolbar */
.route-bar {
  position: fixed;
  bottom: calc(14px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 1001;
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(11, 37, 69, 0.94);
  color: #fff;
  padding: 8px 10px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  max-width: calc(100vw - 20px);
}
.route-hint { font-size: 13px; padding: 0 6px; white-space: nowrap; }
.chip {
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: transparent;
  color: inherit;
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.chip-primary { background: var(--blue); border-color: var(--blue); }

/* Panels */
.panel {
  position: fixed;
  z-index: 1002;
  background: #fff;
  color: var(--navy);
  box-shadow: 0 -4px 24px rgba(11, 37, 69, 0.3);
  left: 0;
  right: 0;
  bottom: 0;
  border-radius: 18px 18px 0 0;
  max-height: 78vh;
  display: flex;
  flex-direction: column;
  padding-bottom: var(--safe-bottom);
  animation: slideup 0.22s ease-out;
}
@keyframes slideup { from { transform: translateY(30px); } to { transform: none; } }
@media (min-width: 720px) {
  .panel {
    left: auto;
    right: 70px;
    bottom: auto;
    top: 70px;
    width: 360px;
    border-radius: var(--radius);
    max-height: calc(100vh - 120px);
  }
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px 8px;
}
.panel-head h2 { margin: 0; font-size: 17px; }
.panel-close {
  border: none;
  background: var(--sand);
  color: var(--navy);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.panel-body { padding: 4px 18px 18px; overflow-y: auto; }

fieldset {
  border: 1px solid #dde6ee;
  border-radius: 10px;
  margin: 0 0 12px;
  padding: 8px 12px 10px;
}
legend { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px; color: var(--navy-2); padding: 0 4px; }
.row { display: flex; align-items: center; gap: 10px; padding: 8px 2px; font-size: 15px; cursor: pointer; }
.row input[type="checkbox"], .row input[type="radio"] { width: 18px; height: 18px; accent-color: var(--blue); }
.slider-row { justify-content: space-between; font-size: 13px; color: var(--navy-2); }
.slider-row input[type="range"] { flex: 1; max-width: 55%; accent-color: var(--blue); }

.note { font-size: 12.5px; color: #5b768d; line-height: 1.45; margin: 8px 0 0; }

.chart-links { display: flex; flex-wrap: wrap; gap: 6px; padding: 4px 0; }
.chart-links a {
  background: var(--sand);
  color: var(--navy-2);
  font-size: 13px;
  font-weight: 600;
  padding: 5px 11px;
  border-radius: 999px;
  text-decoration: none;
}
.chart-links a:hover { background: #dce8f2; }

.field { display: block; font-size: 13px; font-weight: 600; color: var(--navy-2); margin-bottom: 12px; }
.field input, .field select {
  display: block;
  width: 100%;
  margin-top: 5px;
  padding: 10px 12px;
  font-size: 16px;
  border: 1px solid #cdd9e5;
  border-radius: 10px;
  background: #fff;
  color: var(--navy);
}
.field input:focus, .field select:focus { outline: 2px solid var(--blue); border-color: var(--blue); }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }

.btn-primary {
  width: 100%;
  border: none;
  background: var(--blue);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  padding: 12px;
  border-radius: 10px;
  cursor: pointer;
}
.btn-row { display: flex; gap: 8px; margin-top: 12px; }
.btn-row .chip { border-color: #cdd9e5; color: var(--navy); }

/* Route stats */
.route-stats { display: flex; gap: 8px; margin-bottom: 4px; }
.stat {
  flex: 1;
  background: var(--sand);
  border-radius: 10px;
  padding: 10px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.stat-val { font-size: 19px; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat-lbl { font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.4px; color: #5b768d; }

.legs { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 13.5px; }
.legs th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: 0.4px; color: #5b768d; padding: 4px 6px; border-bottom: 1px solid #dde6ee; }
.legs td { padding: 7px 6px; border-bottom: 1px solid #eef2f6; font-variant-numeric: tabular-nums; }

/* Waypoint markers */
.wpt-icon {
  background: var(--blue);
  border: 2.5px solid #fff;
  border-radius: 50%;
  box-shadow: 0 1px 6px rgba(11, 37, 69, 0.5);
  color: #fff;
  font-size: 11px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
}
.wpt-icon.wpt-start { background: #1a7a3a; }
.wpt-icon.wpt-end { background: var(--danger); }

/* Position marker */
.pos-icon svg { filter: drop-shadow(0 1px 4px rgba(11, 37, 69, 0.6)); }

/* Toast */
.toast {
  position: fixed;
  bottom: calc(80px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 1100;
  background: rgba(11, 37, 69, 0.95);
  color: #fff;
  font-size: 14px;
  padding: 10px 18px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  max-width: 86vw;
  text-align: center;
}

/* Disclaimer */
.disclaimer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 1200;
  background: var(--navy);
  color: #dce8f2;
  font-size: 13px;
  line-height: 1.5;
  padding: 12px 16px calc(12px + var(--safe-bottom));
  display: flex;
  align-items: center;
  gap: 14px;
  justify-content: center;
  flex-wrap: wrap;
}
.disclaimer button {
  border: none;
  background: var(--blue);
  color: #fff;
  font-weight: 700;
  padding: 7px 22px;
  border-radius: 999px;
  cursor: pointer;
}

/* Chart feature popup */
.enc-popup .leaflet-popup-content-wrapper {
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.enc-popup .leaflet-popup-content { margin: 12px 16px; font-size: 13.5px; color: var(--navy); }
.enc-title { font-weight: 800; font-size: 14px; }
.enc-sub { font-size: 11.5px; color: #5b768d; text-transform: uppercase; letter-spacing: 0.3px; margin-bottom: 2px; }
.enc-line { margin-top: 3px; line-height: 1.4; }
.enc-hr { border: none; border-top: 1px solid #dde6ee; margin: 9px 0; }

/* Leaflet tweaks */
.leaflet-control-attribution { font-size: 10px; background: rgba(255, 255, 255, 0.75); }
.leaflet-control-zoom { display: none; }
@media (min-width: 720px) and (hover: hover) {
  .leaflet-control-zoom { display: block; }
  .leaflet-left .leaflet-control-zoom { margin-top: 64px; }
}
.leaflet-control-scale-line { background: rgba(255, 255, 255, 0.7); }
