/* SW Maps viewer: icon rail on the left, a drawer of cards beside it, a coordinate
   status bar at the bottom. Theme follows the map style (data-theme on <html>). */
:root {
  --rail-w: 68px;
  --drawer-w: 340px;
  --radius: 10px;
  --radius-sm: 6px;
  --gap: 10px;
  --font: 13px/1.4 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  --mono: 12px/1.3 ui-monospace, "Cascadia Mono", Consolas, "SFMono-Regular", monospace;

  --bg: #eef1f5;
  --surface: #ffffff;
  --surface-2: #f5f7fa;
  --surface-3: #e9edf3;
  --border: #dde3ea;
  --text: #17202b;
  --text-2: #4a5666;
  --text-3: #7a8594;
  --accent: #1a66d2;
  --accent-soft: #e6effc;
  --accent-text: #ffffff;
  --danger: #b42318;
  --warn-bg: #fff4e5;
  --warn-text: #8a4b00;
  --ok: #188038;
  --shadow: 0 1px 2px rgba(16, 24, 40, .08), 0 4px 14px rgba(16, 24, 40, .12);
  --focus: 0 0 0 3px rgba(26, 102, 210, .35);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0d1117;
  --surface: #161b22;
  --surface-2: #1c232c;
  --surface-3: #252e39;
  --border: #2d3742;
  --text: #e6edf3;
  --text-2: #b1bac4;
  --text-3: #8590a0;
  --accent: #4c9aff;
  --accent-soft: #15304f;
  --accent-text: #0d1117;
  --danger: #ff7b72;
  --warn-bg: #3a2a12;
  --warn-text: #f5c27a;
  --ok: #56d364;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 18px rgba(0, 0, 0, .45);
  --focus: 0 0 0 3px rgba(76, 154, 255, .45);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); font: var(--font); }
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: none; box-shadow: var(--focus); }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

#map { position: absolute; top: 0; right: 0; bottom: 0; left: var(--rail-w); }

/* ---- Rail ---------------------------------------------------------------------- */
#rail {
  position: absolute; z-index: 3; top: 0; bottom: 0; left: 0; width: var(--rail-w);
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 0;
  background: var(--surface); border-right: 1px solid var(--border);
}
.brand {
  width: 40px; height: 40px; border-radius: 12px; margin-bottom: 8px;
  display: grid; place-items: center; font-weight: 800; letter-spacing: .02em;
  background: linear-gradient(135deg, #1a66d2, #0d9488); color: #fff; font-size: 14px;
}
.rail-btn {
  width: 58px; padding: 7px 0 6px; border: 0; border-radius: var(--radius); background: transparent;
  display: flex; flex-direction: column; align-items: center; gap: 3px; cursor: pointer; color: var(--text-2);
  font-size: 10.5px; font-weight: 500; position: relative;
}
.rail-btn svg { width: 22px; height: 22px; }
.rail-btn:hover { background: var(--surface-2); color: var(--text); }
.rail-btn[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.rail-btn[aria-pressed="true"]::before {
  content: ""; position: absolute; left: -5px; top: 10px; bottom: 10px; width: 3px; border-radius: 2px; background: var(--accent);
}
.rail-btn .badge {
  position: absolute; top: 3px; right: 8px; min-width: 16px; height: 16px; padding: 0 4px; border-radius: 8px;
  background: var(--accent); color: var(--accent-text); font-size: 10px; line-height: 16px; font-weight: 700;
}
.rail-spacer { flex: 1; }

/* ---- Drawer + cards -------------------------------------------------------------- */
#drawer {
  position: absolute; z-index: 2; top: 0; bottom: 0; left: var(--rail-w); width: var(--drawer-w);
  overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: var(--gap);
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(6px);
  border-right: 1px solid var(--border); box-shadow: var(--shadow);
}
.panel { display: flex; flex-direction: column; gap: var(--gap); }
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 2px 2px 0; }
.panel-head h2 { margin: 0; font-size: 17px; font-weight: 650; }
.icon-btn {
  width: 30px; height: 30px; border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--text-2);
  display: grid; place-items: center; cursor: pointer;
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px; box-shadow: 0 1px 2px rgba(16, 24, 40, .05);
}
.card h3 {
  margin: 0 0 8px; font-size: 11px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--text-3);
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.card p { margin: 0; }
.hint { color: var(--text-3); font-size: 12px; }
.msg { color: var(--text-2); font-size: 12px; margin-top: 6px; }
.msg:empty { display: none; }
.msg.error { color: var(--danger); }
.notice { padding: 8px 10px; border-radius: var(--radius-sm); background: var(--warn-bg); color: var(--warn-text); font-size: 12px; }
.mono { font: var(--mono); }

/* Inputs */
.field { position: relative; }
.field svg { position: absolute; left: 10px; top: 50%; translate: 0 -50%; width: 17px; height: 17px; color: var(--text-3); pointer-events: none; }
input[type=search], input[type=text], select {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.field input { padding-left: 34px; height: 40px; font-size: 14px; }
input:focus, select:focus { border-color: var(--accent); background: var(--surface); outline: none; box-shadow: var(--focus); }
label.k { display: block; color: var(--text-2); font-size: 12px; margin: 8px 0 4px; }

/* Buttons */
.btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: var(--radius-sm);
  padding: 6px 10px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; font-weight: 500;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn svg { width: 15px; height: 15px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 6px; }

/* Segmented control */
.seg { display: flex; padding: 3px; gap: 2px; border-radius: 8px; background: var(--surface-3); }
.seg button {
  flex: 1; border: 0; background: transparent; border-radius: 6px; padding: 5px 6px; cursor: pointer; color: var(--text-2);
  font-size: 12px; font-weight: 550; white-space: nowrap;
}
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.15); }

/* Switch rows */
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 6px 0; cursor: pointer; }
.switch-row + .switch-row { border-top: 1px solid var(--border); }
.switch-row .t { font-weight: 550; }
.switch-row .d { color: var(--text-3); font-size: 12px; }
.switch { appearance: none; flex: none; width: 36px; height: 20px; border-radius: 10px; background: var(--surface-3);
  border: 1px solid var(--border); position: relative; cursor: pointer; transition: background .15s; margin: 0; }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%;
  background: var(--text-3); transition: transform .15s, background .15s; }
.switch:checked { background: var(--accent); border-color: var(--accent); }
.switch:checked::after { transform: translateX(16px); background: #fff; }

/* Result and list cards */
.card-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.item {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 9px 11px;
  cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px;
}
.item:hover, .item:focus-visible { border-color: var(--accent); }
.item .name { font-weight: 600; }
.item .label { grid-column: 1 / -1; color: var(--text-2); font-size: 12px; }
.item .kind { color: var(--text-3); font-size: 11px; text-transform: capitalize; }
.item.position { border-style: dashed; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--border); background: var(--surface-2); border-radius: 999px; padding: 4px 10px; cursor: pointer; font-size: 12px; }
.chip:hover { border-color: var(--accent); color: var(--accent); }

/* Directions */
.ends { display: grid; grid-template-columns: 22px 1fr auto; gap: 6px 8px; align-items: center; }
.ends .dot { width: 22px; height: 22px; border-radius: 50%; color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 11px; }
.ends .dot.a { background: #188038; } .ends .dot.b { background: #d93025; }
.ends .end { padding: 7px 9px; border-radius: var(--radius-sm); background: var(--surface-2); border: 1px solid var(--border);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-height: 34px; }
.ends .end.empty { color: var(--text-3); font-style: italic; }
#route-summary { font-size: 15px; font-weight: 650; }
#route-summary:empty { display: none; }
#route-steps { margin: 8px 0 0; padding-left: 20px; font-size: 12.5px; max-height: 260px; overflow: auto; color: var(--text-2); }
#route-steps:empty { display: none; }
#route-steps li { padding: 3px 0; }

/* Legends */
.legend { list-style: none; margin: 8px 0 0; padding: 0; font-size: 12px; display: grid; gap: 3px; }
.legend i { display: inline-block; width: 12px; height: 12px; margin-right: 6px; vertical-align: -2px; border-radius: 3px; }

/* Sectors */
.sector {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--c); border-radius: var(--radius);
  padding: 9px 10px; display: grid; grid-template-columns: 1fr auto; gap: 4px 8px; cursor: pointer;
}
.sector.selected { border-color: var(--c); box-shadow: 0 0 0 2px color-mix(in srgb, var(--c) 35%, transparent); }
.sector input.name { border: 0; background: transparent; padding: 0; font-weight: 650; font-size: 14px; width: 100%; }
.sector input.name:focus { box-shadow: none; background: var(--surface-2); border-radius: 4px; }
.sector .meta { grid-column: 1 / -1; color: var(--text-2); font-size: 12px; }
.sector .refs { grid-column: 1 / -1; font: var(--mono); color: var(--text); word-break: break-word; }
.sector .acts { display: flex; gap: 2px; }
.sector .acts .icon-btn { width: 26px; height: 26px; }
.empty-state { text-align: center; color: var(--text-3); padding: 14px 6px; border: 1px dashed var(--border); border-radius: var(--radius); font-size: 12.5px; }
.mode-banner {
  position: absolute; z-index: 2; top: 12px; left: 50%; translate: -50% 0; margin-left: calc(var(--rail-w) / 2);
  background: var(--accent); color: var(--accent-text); border-radius: 999px; padding: 6px 8px 6px 14px; font-weight: 600;
  box-shadow: var(--shadow); display: flex; gap: 10px; align-items: center; font-size: 12.5px;
}
.mode-banner button { border: 0; border-radius: 999px; padding: 3px 10px; cursor: pointer; background: rgba(255,255,255,.22); color: inherit; font-weight: 600; }
body.drawer-open .mode-banner { margin-left: calc((var(--rail-w) + var(--drawer-w)) / 2); }
body.sector-mode #map .maplibregl-canvas { cursor: crosshair; }

/* ---- My location ------------------------------------------------------------------ */
.loc-state { display: flex; align-items: center; gap: 8px; }
.loc-state .hint { margin-left: auto; }
.live-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--text-3); flex: none; }
.live-dot[data-state="searching"] { background: #f59f00; animation: pulse 1.2s infinite; }
.live-dot[data-state="on"] { background: var(--ok); }
.live-dot[data-state="stale"] { background: var(--danger); }
.loc-big { font-size: 17px; font-weight: 600; margin: 8px 0 4px; letter-spacing: .01em; }
.card .coords { display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; font-size: 12px; }
.coords .k { color: var(--text-3); font-size: 10.5px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; padding-top: 1px; }
.coords .v { font: var(--mono); }
.rail-btn .badge.live { background: var(--ok); color: #fff; font-size: 8px; min-width: 12px; height: 12px; line-height: 12px; padding: 0; top: 5px; right: 12px; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(26, 102, 210, .55); } 70% { box-shadow: 0 0 0 12px rgba(26, 102, 210, 0); } 100% { box-shadow: 0 0 0 0 rgba(26, 102, 210, 0); } }

/* The "me" marker: dot, pulse while live, heading cone when moving */
.me { width: 22px; height: 22px; position: relative; }
.me .core { position: absolute; inset: 3px; border-radius: 50%; background: #1a73e8; border: 3px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,.45); animation: pulse 2s infinite; }
.me.stale .core { background: #8590a0; animation: none; }
.me .cone { position: absolute; left: 50%; top: 50%; width: 0; height: 0; display: none;
  border-left: 9px solid transparent; border-right: 9px solid transparent; border-bottom: 22px solid rgba(26,115,232,.55);
  transform-origin: 50% 100%; translate: -50% -100%; }
.me.heading .cone { display: block; }

/* Locate button in the map controls */
.maplibregl-ctrl button.sw-locate { display: grid; place-items: center; color: var(--text-2); }
.maplibregl-ctrl button.sw-locate svg { width: 19px; height: 19px; }
.maplibregl-ctrl button.sw-locate[data-state="searching"] { color: #f59f00; }
.maplibregl-ctrl button.sw-locate[data-state="on"] { color: var(--accent); }
.maplibregl-ctrl button.sw-locate[data-state="follow"] { color: #fff; background: var(--accent) !important; }

/* ---- Status bar -------------------------------------------------------------------- */
#statusbar {
  position: absolute; z-index: 2; bottom: 10px; left: calc(var(--rail-w) + 10px); right: 10px; margin: 0 auto;
  width: max-content; max-width: calc(100% - var(--rail-w) - 20px);
  display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 2px; padding: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow);
}
body.drawer-open #statusbar { left: calc(var(--rail-w) + var(--drawer-w) + 10px); max-width: calc(100% - var(--rail-w) - var(--drawer-w) - 20px); }
#statusbar .seg-val {
  display: flex; align-items: baseline; gap: 6px; padding: 4px 10px; border-radius: 999px; border: 0; background: transparent; cursor: pointer;
  white-space: nowrap;
}
#statusbar .seg-val:hover { background: var(--surface-2); }
#statusbar .k { color: var(--text-3); font-size: 10.5px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; }
#statusbar .v { font: var(--mono); font-size: 12.5px; }
#statusbar .sep { width: 1px; height: 18px; background: var(--border); }
#statusbar .copied { color: var(--ok); }

/* ---- Map popups as cards ---------------------------------------------------------- */
.maplibregl-popup-content {
  background: var(--surface); color: var(--text); border-radius: var(--radius); box-shadow: var(--shadow);
  border: 1px solid var(--border); padding: 12px 14px; font: var(--font);
}
.maplibregl-popup-tip { border-top-color: var(--surface) !important; border-bottom-color: var(--surface) !important; }
.maplibregl-popup-close-button { color: var(--text-3); font-size: 18px; width: 26px; height: 26px; border-radius: 6px; right: 4px; top: 4px; }
.maplibregl-popup-close-button:hover { background: var(--surface-2); }
.popup { min-width: 230px; max-width: 280px; display: grid; gap: 4px; }
.popup .title { font-weight: 650; font-size: 14px; padding-right: 20px; }
.popup .label { color: var(--text-2); font-size: 12px; }
.popup .coords { display: grid; grid-template-columns: auto 1fr; gap: 2px 8px; margin-top: 4px; font-size: 12px; }
.popup .coords .k { color: var(--text-3); font-size: 10.5px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; padding-top: 1px; }
.popup .coords .v { font: var(--mono); }
.popup .depth { color: #1d4e89; font-size: 12px; }
:root[data-theme="dark"] .popup .depth { color: #8ab4f8; }
.popup .actions { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
.popup .actions .btn { padding: 4px 8px; font-size: 12px; }

.pin { width: 24px; height: 24px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg);
  border: 2px solid #fff; box-shadow: 0 1px 3px rgba(0,0,0,.4); }
.pin span { display: block; transform: rotate(45deg); color: #fff; font: 700 11px/20px system-ui; text-align: center; }

/* MapLibre controls in the theme */
.maplibregl-ctrl-group { background: var(--surface) !important; border: 1px solid var(--border); border-radius: var(--radius) !important; box-shadow: var(--shadow) !important; }
.maplibregl-ctrl-group button + button { border-top-color: var(--border) !important; }
:root[data-theme="dark"] .maplibregl-ctrl button .maplibregl-ctrl-icon { filter: invert(1) hue-rotate(180deg); }
.maplibregl-ctrl-scale { background: color-mix(in srgb, var(--surface) 85%, transparent); color: var(--text); border-color: var(--text-2); }
.maplibregl-ctrl-attrib { background: color-mix(in srgb, var(--surface) 80%, transparent) !important; color: var(--text-2); }
.maplibregl-ctrl-attrib a { color: var(--text-2); }
.maplibregl-ctrl-bottom-left { bottom: 52px; }

/* ---- Small screens: rail along the bottom, drawer as a bottom sheet ----------------- */
@media (max-width: 720px) {
  :root { --rail-w: 0px; --drawer-w: 100%; }
  #map { left: 0; bottom: 62px; }
  #rail { top: auto; right: 0; height: 62px; width: auto; flex-direction: row; justify-content: space-around; padding: 4px 6px;
    border-right: 0; border-top: 1px solid var(--border); }
  .brand, .rail-spacer { display: none; }
  .rail-btn { width: auto; flex: 1; }
  .rail-btn[aria-pressed="true"]::before { left: 18%; right: 18%; top: -5px; bottom: auto; width: auto; height: 3px; }
  #drawer { top: auto; left: 0; right: 0; bottom: 62px; width: auto; max-height: 58vh; border-radius: 16px 16px 0 0;
    border-right: 0; border-top: 1px solid var(--border); }
  /* Status bar moves to the top (the bottom has the rail and the attribution). */
  #statusbar, body.drawer-open #statusbar {
    top: 8px; bottom: auto; left: 8px; right: 52px; width: auto; max-width: none; margin: 0;
    flex-wrap: wrap; justify-content: center; border-radius: 14px; gap: 0;
  }
  #statusbar .seg-val { padding: 3px 8px; }
  #statusbar .v { font-size: 11.5px; }
  #statusbar .zoom-val, #statusbar .sep { display: none !important; }
  .mode-banner { top: auto; bottom: 12px; }
  .mode-banner, body.drawer-open .mode-banner { margin-left: 0; }
  .maplibregl-ctrl-bottom-left { bottom: 48px; }
}
