:root {
  --bg: #f4efe2;
  --panel: rgba(255, 255, 255, 0.78);
  --panel-solid: #ffffff;
  --border: rgba(20, 30, 45, 0.10);
  --text: #18212b;
  --dim: #66717e;
  --accent: #0f6bd8;
  --accent-soft: rgba(15, 107, 216, 0.12);
  --shadow: 0 10px 30px -10px rgba(16, 28, 45, 0.35), 0 2px 6px -2px rgba(16, 28, 45, 0.15);
  --radius: 16px;
  --red: #d7262f;
  --ease: cubic-bezier(.2, .8, .2, 1);
  --gutter: 12px;
  color-scheme: light;
}
:root[data-theme="dusk"], :root[data-theme="night"] {
  --bg: #0b1a2a;
  --panel: rgba(16, 24, 34, 0.72);
  --panel-solid: #121b26;
  --border: rgba(255, 255, 255, 0.08);
  --text: #e6ebf1;
  --dim: #8d99a8;
  --accent: #5fb0ff;
  --accent-soft: rgba(95, 176, 255, 0.16);
  --shadow: 0 10px 30px -10px rgba(0, 0, 0, 0.7), 0 2px 6px -2px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}
/* Night: everything through a red filter to preserve dark adaptation (like ECDIS night palettes) */
:root[data-theme="night"] body { filter: grayscale(1) sepia(1) saturate(8) hue-rotate(-45deg) brightness(0.42) contrast(1.15); }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); }
body {
  font: 14px/1.4 Inter, system-ui, -apple-system, Segoe UI, sans-serif;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
#map { position: fixed; inset: 0; }
.mono { font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; letter-spacing: -0.01em; }
.dim { color: var(--dim); }

.panel {
  position: fixed;
  background: var(--panel);
  backdrop-filter: blur(18px) saturate(1.6);
  -webkit-backdrop-filter: blur(18px) saturate(1.6);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 10;
}

/* region jump (top left) */
.brand {
  top: calc(var(--gutter) + env(safe-area-inset-top)); left: var(--gutter);
  display: flex; align-items: center; padding: 6px;
}
.region {
  appearance: none; border: 1px solid var(--border); background: var(--accent-soft); color: var(--text);
  border-radius: 10px; padding: 7px 28px 7px 10px; font: 500 13px Inter, sans-serif; cursor: pointer; min-height: 40px;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 14px) 52%, calc(100% - 9px) 52%; background-size: 5px 5px; background-repeat: no-repeat;
}
.region option { background: var(--panel-solid); }

/* rail */
.rail {
  top: calc(var(--gutter) + env(safe-area-inset-top)); right: var(--gutter);
  display: flex; flex-direction: column; padding: 6px; gap: 2px;
}
.btn {
  width: 40px; height: 40px; border: 0; border-radius: 11px; background: transparent; color: var(--text);
  display: grid; place-items: center; cursor: pointer;
  transition: background .18s var(--ease), transform .12s var(--ease), color .18s;
}
.btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.btn svg .fill-red { fill: var(--red); stroke: var(--red); }
.btn svg #compass, #compass { transition: transform .25s var(--ease); }
.btn:hover { background: var(--accent-soft); }
.btn:active { transform: scale(0.9); }
.btn.on { background: var(--accent); color: #fff; }
.btn.on svg .fill-red { fill: #fff; stroke: #fff; }
.sep { height: 1px; background: var(--border); margin: 4px 6px; }

/* settings sheet */
.sheet {
  top: calc(var(--gutter) + env(safe-area-inset-top)); right: calc(var(--gutter) + 62px);
  width: 300px; padding: 16px; transform-origin: top right;
  animation: pop .22s var(--ease);
}
.sheet h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--dim); margin: 0 0 12px; font-weight: 600; }
@keyframes pop { from { opacity: 0; transform: scale(.96) translateY(-4px); } to { opacity: 1; transform: none; } }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--accent-soft); padding: 3px; border-radius: 11px; gap: 2px; }
.seg button { border: 0; background: transparent; color: var(--text); font: 500 13px Inter, sans-serif; padding: 7px 0; border-radius: 9px; cursor: pointer; transition: background .2s var(--ease), box-shadow .2s; }
.seg button[aria-checked="true"] { background: var(--panel-solid); box-shadow: 0 1px 4px rgba(0,0,0,.18); font-weight: 600; }
.field { margin: 16px 0 6px; }
.field label { display: flex; justify-content: space-between; font-weight: 500; margin-bottom: 8px; }
.field output { font-family: 'JetBrains Mono', monospace; color: var(--accent); font-size: 13px; }
#settings input[type=range]#rose-size { background: linear-gradient(90deg, var(--border), var(--accent)); }
input[type=range] { width: 100%; appearance: none; height: 6px; border-radius: 3px; background: linear-gradient(90deg, #6fa9dc, #d6e9f8); outline: none; }
input[type=range]::-webkit-slider-thumb { appearance: none; width: 22px; height: 22px; border-radius: 50%; background: var(--panel-solid); border: 2px solid var(--accent); box-shadow: 0 2px 6px rgba(0,0,0,.25); cursor: grab; transition: transform .15s var(--ease); }
input[type=range]::-webkit-slider-thumb:active { transform: scale(1.15); cursor: grabbing; }
input[type=range]::-moz-range-thumb { width: 20px; height: 20px; border-radius: 50%; background: var(--panel-solid); border: 2px solid var(--accent); }
.legend { display: flex; height: 18px; margin-top: 10px; border-radius: 6px; overflow: hidden; border: 1px solid var(--border); }
.legend div { flex: 1; font: 500 9px 'JetBrains Mono', monospace; display: grid; place-items: center; color: rgba(0,0,0,.6); }
:root[data-theme="dusk"] .legend div, :root[data-theme="night"] .legend div { color: rgba(255,255,255,.7); }
.toggles { display: grid; gap: 2px; margin: 14px 0 6px; }
.tgl { display: flex; align-items: center; gap: 10px; padding: 7px 4px; cursor: pointer; border-radius: 8px; user-select: none; }
.tgl:hover { background: var(--accent-soft); }
.tgl input { display: none; }
.tgl span { width: 34px; height: 20px; border-radius: 10px; background: var(--border); position: relative; transition: background .2s var(--ease); flex: none; }
.tgl span::after { content: ''; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .22s var(--ease); }
.tgl input:checked + span { background: var(--accent); }
.tgl input:checked + span::after { transform: translateX(14px); }
.fine { font-size: 11px; color: var(--dim); margin: 10px 0 0; line-height: 1.45; }

.sheet h2.sub { margin-top: 18px; }
.text { width: 100%; margin-top: 8px; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--border); background: var(--accent-soft); color: var(--text); font: 12px 'JetBrains Mono', monospace; }
.row { display: flex; align-items: center; justify-content: space-between; margin-top: 12px; gap: 8px; }
.row label { font-weight: 500; }
.row.btns { justify-content: flex-start; }
.mini { border: 1px solid var(--border); background: var(--accent-soft); color: var(--text); border-radius: 8px; padding: 5px 8px; font: 500 13px Inter, sans-serif; }
.chip { border: 1px solid var(--border); background: var(--accent-soft); color: var(--text); border-radius: 999px; padding: 9px 16px; min-height: 40px; font: 500 12px Inter, sans-serif; cursor: pointer; transition: transform .12s var(--ease); }
.chip:active { transform: scale(.95); }
.chip:disabled { opacity: .45; cursor: default; transform: none; }
.sheet { max-height: calc(100% - 2 * var(--gutter) - 60px); overflow: auto; }

/* instruments */
.inst {
  left: 50%; transform: translateX(-50%); bottom: calc(var(--gutter) + env(safe-area-inset-bottom) + 50px);
  padding: 10px 12px 8px; min-width: 380px; max-width: calc(100% - 2 * var(--gutter)); animation: rise .25s var(--ease);
}
@keyframes rise { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }
.inst-grid { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(70px, 1fr); gap: 4px; }
.cell { display: grid; grid-template-columns: auto auto; grid-template-rows: auto auto; align-items: baseline; justify-content: start; column-gap: 3px; padding: 2px 8px; border-radius: 10px; }
.cell + .cell { border-left: 1px solid var(--border); border-radius: 0; }
.cell .lbl { grid-column: 1 / 3; font-size: 10px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--dim); }
.cell .val { font: 600 24px/1.1 'JetBrains Mono', monospace; letter-spacing: -0.03em; font-variant-numeric: tabular-nums; }
.cell.big .val { font-size: 30px; color: var(--accent); }
.cell .unit { font-size: 11px; color: var(--dim); }
.cell.warn .val { color: var(--red); }
.inst-foot { display: flex; align-items: center; gap: 10px; margin-top: 6px; padding-top: 6px; border-top: 1px solid var(--border); }
.inst-foot .grow { flex: 1; }
.mode { width: 32px; height: 32px; border-radius: 9px; border: 0; background: transparent; color: var(--text); display: grid; place-items: center; cursor: pointer; transition: background .18s, transform .12s; }
.mode svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; stroke-linecap: round; }
.mode[aria-pressed="true"] { background: var(--accent); color: #fff; }
.mode:active { transform: scale(.9); }
.mode:hover:not([aria-pressed="true"]) { background: var(--accent-soft); }
.alarm { margin: -2px -4px 8px; padding: 6px 10px; border-radius: 10px; background: var(--red); color: #fff; font-weight: 600; font-size: 13px; text-align: center; animation: blink 1.2s infinite; }
@keyframes blink { 50% { opacity: .6; } }
.toast { left: 50%; transform: translateX(-50%); top: calc(var(--gutter) + 64px + env(safe-area-inset-top)); padding: 8px 14px; font-size: 13px; animation: pop .2s var(--ease); }

/* compass rose */
.rose { position: fixed; z-index: 5; left: calc(var(--gutter) + 4px); bottom: calc(var(--gutter) + 64px + env(safe-area-inset-bottom)); width: var(--rose-size, 170px); height: var(--rose-size, 170px); pointer-events: none; --rose: #c0167a; }
:root[data-theme="dusk"] .rose, :root[data-theme="night"] .rose { --rose: #ff6fbf; }
.rose-svg { width: 100%; height: 100%; overflow: visible; }
.rose-line { stroke: var(--rose); stroke-width: 0.8; fill: none; }
.rose-line.thin { stroke-width: 0.5; stroke-dasharray: 2 2; }
.rose-fill { fill: var(--rose); }
.rose-text { fill: var(--rose); font: 500 8px Inter, sans-serif; text-anchor: middle; dominant-baseline: middle; paint-order: stroke; stroke: var(--bg); stroke-width: 2.4px; stroke-linejoin: round; }
.rose-text.note { font-size: 7.5px; font-weight: 600; }

.sheet-head { display: flex; align-items: center; justify-content: space-between; margin: -6px -6px 6px 0; }
.sheet-head h2 { margin: 0; }
.close { width: 40px; height: 40px; border-radius: 10px; border: 0; background: transparent; color: var(--dim); font-size: 24px; line-height: 1; cursor: pointer; display: grid; place-items: center; }
.close:hover, .close:active { background: var(--accent-soft); color: var(--text); }
.m-btns { display: flex; gap: 8px; justify-content: center; margin-top: 8px; }
.chip.primary { background: var(--accent); color: #fff; border-color: transparent; }

/* info card */
.card {
  left: var(--gutter); top: calc(var(--gutter) + 72px + env(safe-area-inset-top));
  width: 320px; max-height: calc(100% - 160px); overflow: auto; padding: 14px 16px 12px;
  animation: slide .25s var(--ease);
}
@keyframes slide { from { opacity: 0; transform: translateX(-8px); } to { opacity: 1; transform: none; } }
.card .close { position: absolute; top: 6px; right: 6px; }
.card .close:hover { background: var(--accent-soft); color: var(--text); }
.card-head { display: flex; gap: 12px; align-items: center; padding-right: 24px; }
.card-head img { width: 32px; height: 44px; flex: none; }
.card-head img[src=""] { display: none; }
.card-kind { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--dim); font-weight: 600; }
.card h3 { margin: 2px 0 0; font-size: 17px; letter-spacing: -0.01em; }
.card-light { margin-top: 10px; font: 500 14px 'JetBrains Mono', monospace; color: var(--accent); }
.card-light:empty { display: none; }
.card dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 12px 0 0; font-size: 12.5px; }
.card dt { color: var(--dim); }
.card dd { margin: 0; word-break: break-word; }

/* light characteristic: scene + timing strips */
.lc { margin-top: 10px; }
.lc-scene { display: block; width: 100%; height: 96px; border-radius: 12px; background: #02060d; }
.lc-rows { display: grid; gap: 2px; margin-top: 6px; }
.lc-row { display: grid; grid-template-columns: 14px auto 1fr; align-items: center; gap: 10px; min-height: 44px; padding: 4px 6px; border: 0; border-radius: 10px; background: transparent; color: var(--text); text-align: left; cursor: pointer; font: 12px Inter, sans-serif; }
.lc-row[aria-pressed="true"] { background: var(--accent-soft); }
.lc-lamp { width: 12px; height: 12px; border-radius: 50%; background: #1a2230; border: 1px solid var(--border); }
.lc-txt { display: flex; flex-direction: column; line-height: 1.2; white-space: nowrap; }
.lc-txt .dim { font-size: 11px; }
.lc-track { position: relative; height: 24px; min-width: 0; }
.lc-strip { width: 100%; height: 24px; display: block; }
.lc-bg { fill: #0b1320; }
.lc-ticks line { stroke: var(--dim); stroke-width: 2; vector-effect: non-scaling-stroke; }
.lc-head { position: absolute; top: 0; height: 20px; width: 2px; margin-left: -1px; background: var(--accent); border-radius: 1px; box-shadow: 0 0 4px var(--accent); }

/* measure */
.measure { left: 50%; transform: translateX(-50%); top: calc(var(--gutter) + env(safe-area-inset-top)); padding: 10px 16px; text-align: center; animation: pop .2s var(--ease); }
.m-main { display: flex; gap: 16px; justify-content: center; font: 600 18px 'JetBrains Mono', monospace; }
.m-main #m-brg { color: var(--accent); }
.m-hint { font-size: 11px; color: var(--dim); margin-top: 2px; }
.measure-label { font: 600 11px 'JetBrains Mono', monospace; background: var(--panel-solid); color: var(--text); padding: 2px 6px; border-radius: 6px; box-shadow: var(--shadow); white-space: nowrap; pointer-events: none; }

/* status bar */
.status {
  left: var(--gutter); bottom: calc(var(--gutter) + env(safe-area-inset-bottom));
  display: flex; align-items: center; gap: 10px; padding: 7px 12px; border-radius: 12px; min-width: 260px;
}
.status .grow { flex: 1; }
.depth-chip { font: 600 12px 'JetBrains Mono', monospace; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.depth-chip.shoal { background: rgba(215, 38, 47, .14); color: var(--red); }

.net-chip { display: inline-flex; align-items: center; gap: 5px; font: 600 12px Inter, sans-serif; padding: 2px 9px 2px 7px; border-radius: 999px; background: rgba(214, 140, 0, .16); color: #b26b00; white-space: nowrap; }
:root[data-theme="dusk"] .net-chip, :root[data-theme="night"] .net-chip { color: #ffb84d; }
.net-chip svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }

/* offline regions */
.off .row label { flex: none; }
.off .mini { min-height: 44px; flex: 1; max-width: 190px; }
.off .chip { min-height: 44px; }
.off-est { margin-top: 10px; padding: 8px 10px; border-radius: 10px; background: var(--accent-soft); color: var(--text); }
.off-est.warn { background: rgba(215, 38, 47, .12); color: var(--red); font-family: Inter, sans-serif; }
.off-progress { margin-top: 10px; display: grid; gap: 6px; }
.off-track { height: 8px; border-radius: 4px; background: var(--border); overflow: hidden; }
.off-track i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 4px; transition: width .2s linear; }
.off-progress span { color: var(--dim); }
.off-list { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 2px; }
.off-list:empty { display: none; }
.off-list li { display: flex; align-items: center; gap: 4px; border-radius: 10px; }
.off-list li:hover { background: var(--accent-soft); }
.off-go { flex: 1; min-width: 0; min-height: 44px; display: flex; flex-direction: column; align-items: flex-start; justify-content: center; gap: 1px; border: 0; background: transparent; color: var(--text); text-align: left; padding: 4px 6px; cursor: pointer; font: 13px Inter, sans-serif; }
.off-go strong { font-weight: 600; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.off-go span { font-size: 11.5px; }
.off-del { flex: none; width: 44px; height: 44px; }
.off-upd { flex: none; min-height: 40px; padding: 6px 12px; }
.off-old { color: var(--red); font-weight: 600; }
.off-del svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.off-del:hover { color: var(--red); }

/* maplibre chrome */
.maplibregl-ctrl-bottom-right { bottom: calc(var(--gutter) + env(safe-area-inset-bottom)); right: var(--gutter); }
.maplibregl-ctrl-scale { background: var(--panel) !important; color: var(--text) !important; border-color: var(--text) !important; font: 500 11px Inter, sans-serif; backdrop-filter: blur(10px); border-radius: 0 0 4px 4px; }
.maplibregl-ctrl-attrib { background: var(--panel) !important; color: var(--dim); border-radius: 10px !important; backdrop-filter: blur(10px); font-size: 11px; }
.maplibregl-ctrl-attrib, .maplibregl-ctrl-attrib-inner { color: var(--dim) !important; }
.maplibregl-ctrl-attrib a { color: var(--dim) !important; }
.maplibregl-ctrl-attrib-button { filter: invert(var(--inv, 0)); }
/* while the compass rose shows, the credits box keeps clear of it: page width minus rose and gutters */
body:not(.navigating):has(#rose:not([hidden])) .maplibregl-ctrl-attrib { box-sizing: border-box; max-width: calc(100vw - var(--rose-size, 170px) - 3 * var(--gutter) - 8px); }
:root[data-theme="dusk"], :root[data-theme="night"] { --inv: 1; }
.maplibregl-ctrl-group { display: none; }
.maplibregl-user-location-dot, .maplibregl-user-location-dot::before { background-color: var(--accent); }
.maplibregl-canvas { cursor: default; }
.measuring .maplibregl-canvas { cursor: crosshair; }

@media (max-width: 640px) {
  :root { --gutter: 10px; }
  .brand { right: calc(var(--gutter) + 64px); }
  .region { flex: 1; min-width: 0; }
  .sheet { left: var(--gutter); right: var(--gutter); width: auto; top: auto; bottom: calc(var(--gutter) + 52px + env(safe-area-inset-bottom)); transform-origin: bottom center; max-height: 70%; overflow: auto; }
  .card { left: var(--gutter); right: var(--gutter); width: auto; top: auto; bottom: calc(var(--gutter) + 52px + env(safe-area-inset-bottom)); max-height: 45%; animation: up .25s var(--ease); }
  @keyframes up { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
  .status { right: calc(var(--gutter) + 0px); min-width: 0; }
  .status #zoom { display: none; }
  .measure { top: calc(var(--gutter) + 64px + env(safe-area-inset-top)); left: var(--gutter); right: calc(var(--gutter) + 64px); transform: none; }
  .rose { bottom: calc(var(--gutter) + 50px + env(safe-area-inset-bottom)); }
  body.navigating .rose { display: none; }
  .inst { min-width: 0; width: calc(100% - 2 * var(--gutter)); bottom: calc(var(--gutter) + 46px + env(safe-area-inset-bottom)); }
  .cell .val { font-size: 20px; } .cell.big .val { font-size: 26px; }
  .inst-grid { grid-auto-columns: minmax(58px, 1fr); }
  .maplibregl-ctrl-bottom-right { bottom: calc(var(--gutter) + 52px + env(safe-area-inset-bottom)); }
}
@media (pointer: coarse) {
  .btn { width: 46px; height: 46px; }
  .mode { width: 44px; height: 44px; }
  .mode svg { width: 22px; height: 22px; }
  .tgl { padding: 10px 4px; }
  .seg button { padding: 11px 0; font-size: 14px; }
  .mini, .region { min-height: 44px; font-size: 15px; }
  input[type=range] { height: 8px; }
  input[type=range]::-webkit-slider-thumb { width: 28px; height: 28px; }
  .text { font-size: 15px; padding: 10px; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
.fine.credit { margin-top: 6px; font-weight: 600; color: var(--text); opacity: .7; }
