:root {
  --bg: #f7f9fa;
  --surface: #ffffff;
  --text: #13232b;
  --muted: #5b6b73;
  --line: #dde4e8;
  --accent: #0e7490;
  --accent-text: #ffffff;
  --warn-bg: #fff4d6;
  --warn-text: #6b4e00;
  --error: #b42318;
  --ok: #147a3d;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1418; --surface: #152027; --text: #e3ecef; --muted: #93a4ab;
    --line: #26363f; --accent: #38bdf8; --accent-text: #052433;
    --warn-bg: #3a2f10; --warn-text: #f5d58a; --error: #ff8a80; --ok: #6fd69a;
  }
}
:root[data-theme="dark"] {
  --bg: #0d1418; --surface: #152027; --text: #e3ecef; --muted: #93a4ab;
  --line: #26363f; --accent: #38bdf8; --accent-text: #052433;
  --warn-bg: #3a2f10; --warn-text: #f5d58a; --error: #ff8a80; --ok: #6fd69a;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column;
}
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 8px 16px;
  background: var(--surface); border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.brand { font-weight: 700; color: var(--accent); font-size: 17px; }
nav { display: flex; gap: 4px; flex-wrap: wrap; }
nav button { background: none; color: var(--text); border: 0; padding: 6px 10px; border-radius: 6px; }
nav button.active { background: var(--accent); color: var(--accent-text); }
.who { margin-left: auto; color: var(--muted); font-size: 13px; display: flex; gap: 8px; align-items: center; }
.disclaimer { background: var(--warn-bg); color: var(--warn-text); padding: 6px 16px; font-size: 13px; }

main { flex: 1; position: relative; min-height: 0; display: flex; flex-direction: column; }
.mapview { flex: 1; position: relative; min-height: 420px; }
#map { position: absolute; inset: 0; }
.legend, .mapinfo {
  position: absolute; background: var(--surface); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; font-size: 12px; box-shadow: 0 1px 4px rgb(0 0 0 / .12);
}
.legend { left: 12px; bottom: 28px; }
.legend .sw { display: inline-block; width: 12px; height: 12px; border-radius: 2px; margin-right: 4px; vertical-align: -2px; }
.mapinfo { right: 12px; top: 12px; max-width: 260px; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  margin: 16px auto; padding: 16px 20px; width: min(860px, calc(100% - 32px)); overflow-x: auto;
}
.card.narrow { width: min(420px, calc(100% - 32px)); }
.card.wide { width: min(1200px, calc(100% - 32px)); }
h1 { font-size: 20px; margin: 0 0 8px; }
h2 { font-size: 16px; margin: 20px 0 8px; }
.hint, .opt { color: var(--muted); font-size: 13px; }

form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
input, select, button { font: inherit; }
input, select {
  padding: 7px 9px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--bg); color: var(--text); min-width: 0;
}
button {
  padding: 7px 14px; border: 0; border-radius: 6px; background: var(--accent);
  color: var(--accent-text); cursor: pointer;
}
button.small { padding: 3px 9px; font-size: 13px; }
button.link { background: none; color: var(--accent); padding: 0; }
button.danger { background: none; color: var(--error); border: 1px solid var(--line); }
button:disabled { opacity: .5; cursor: default; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.grid .actions { grid-column: 1 / -1; display: flex; gap: 12px; align-items: center; }
.inline { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.inline input[type=number] { width: 80px; }
form + form, #signin-form button, #redeem-form button { margin-top: 10px; }

table { width: 100%; border-collapse: collapse; font-size: 13px; margin: 6px 0; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 600; }
td code { font-size: 12px; }
.msg { min-height: 1.2em; font-size: 13px; }
.msg.error { color: var(--error); }
.msg.ok { color: var(--ok); }
.secret {
  margin-top: 10px; padding: 10px; border: 1px dashed var(--accent); border-radius: 6px;
  font-size: 13px; word-break: break-all;
}
.badge { display: inline-block; padding: 1px 7px; border-radius: 10px; font-size: 12px; background: var(--bg); border: 1px solid var(--line); }
.badge.processed, .badge.active { color: var(--ok); }
.badge.failed, .badge.suspended { color: var(--error); }

#toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--text);
  color: var(--bg); padding: 8px 14px; border-radius: 6px; font-size: 13px; opacity: 0;
  transition: opacity .2s; pointer-events: none; max-width: calc(100% - 32px);
}
#toast.show { opacity: .95; }

@media (max-width: 600px) {
  .topbar { gap: 8px; }
  .who { margin-left: 0; width: 100%; }
  .mapinfo { left: 56px; right: 12px; max-width: none; top: 12px; }
  .legend { font-size: 11px; padding: 6px 8px; display: flex; flex-wrap: wrap; gap: 2px 8px; right: 12px; bottom: 44px; }
  .legend > div:first-child, .legend .hint { width: 100%; }
}

.legend { max-width: 290px; }
.legend summary { cursor: pointer; }
.legend-body { margin-top: 6px; display: flex; flex-direction: column; gap: 3px; }
.legend .toggle { display: flex; align-items: center; gap: 6px; flex-direction: row; color: var(--text); font-size: 12px; }
.legend .toggle input { margin: 0; }
.legend .exc-key { margin: 2px 0 0 22px; font-size: 11px; color: var(--muted); }
.legend p.hint { margin: 6px 0 0; font-size: 11px; }

.ownship {
  position: absolute; left: 56px; top: 12px; background: var(--surface); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; font-size: 12px;
  box-shadow: 0 1px 4px rgb(0 0 0 / .12); min-width: 150px;
}
.ownship .own-row { display: flex; gap: 14px; font-size: 13px; }
.ownship .own-row strong { font-size: 20px; font-variant-numeric: tabular-nums; }
.ownship.stale { opacity: .55; }
@media (max-width: 600px) { .ownship { top: auto; bottom: 120px; left: 12px; } }
.own-arrow { pointer-events: none; }
.own-arrow svg { display: block; filter: drop-shadow(0 1px 2px rgb(0 0 0 / .35)); }

.route-panel {
  position: absolute; right: 12px; bottom: 36px; width: 320px; max-height: calc(100% - 140px); overflow-y: auto;
  background: var(--surface); color: var(--text); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; font-size: 13px; box-shadow: 0 2px 10px rgb(0 0 0 / .18); display: flex; flex-direction: column; gap: 8px;
}
.route-head { display: flex; justify-content: space-between; align-items: center; }
.route-hint { font-weight: 600; color: var(--accent); }
.route-stats { display: flex; gap: 14px; flex-wrap: wrap; }
.route-stats strong { font-size: 20px; font-variant-numeric: tabular-nums; }
.route-section { display: flex; flex-direction: column; gap: 4px; }
.route-item {
  display: flex; flex-direction: column; align-items: flex-start; gap: 1px; text-align: left; width: 100%;
  background: var(--bg); color: var(--text); border: 1px solid var(--line); border-left: 4px solid #2563eb; padding: 6px 8px;
}
.route-item.shallower { border-left-color: #dc2626; }
.route-item.land_afloat { border-left-color: #9333ea; }
.route-item .where { font-weight: 600; }
.route-settings { display: flex; gap: 10px; flex-wrap: wrap; }
.route-settings label { flex-direction: row; align-items: center; gap: 4px; display: flex; font-size: 12px; }
.route-num { width: 64px; padding: 3px 5px; }
.route-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.route-btn svg { display: block; margin: auto; }
@media (max-width: 600px) {
  .route-panel { left: 8px; right: 8px; width: auto; bottom: 8px; max-height: 55%; }
}
body.route-open .mapinfo { display: none; }
.route-panel { top: 12px; bottom: auto; max-height: calc(100% - 60px); }
@media (max-width: 600px) { .route-panel { top: auto; bottom: 8px; max-height: 55%; } }
.row-actions { display: inline-flex; gap: 6px; }
