:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --ink: #14202b;
  --muted: #5b6b78;
  --line: #dde3e8;
  --brand: #0b4f6c;
  --brand-ink: #ffffff;
  --accent: #0f7c8c;
  --green: #1d7a46;
  --green-bg: #e3f3ea;
  --amber: #8a5a00;
  --amber-bg: #fdf0d5;
  --red: #a52a2a;
  --red-bg: #fbe4e2;
  --blue: #1f5fa8;
  --blue-bg: #e2ecf8;
  --grey-bg: #eceff2;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 32, 48, .06), 0 2px 8px rgba(16, 32, 48, .05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f151b; --surface: #17212a; --ink: #e6edf3; --muted: #9aabb8; --line: #2a3843;
    --brand: #0e5c7d; --accent: #3bb3c3;
    --green: #6fd19a; --green-bg: #183528; --amber: #f0c05a; --amber-bg: #3a2e12;
    --red: #ff8f86; --red-bg: #3d1d1b; --blue: #8ab8ff; --blue-bg: #1b2c44; --grey-bg: #26323c;
    --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; }
a { color: var(--accent); }
h1 { font-size: 1.55rem; margin: 0 0 .25rem; line-height: 1.25; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3.subhead { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 1rem 0 .4rem; }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .85em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hint { color: var(--muted); font-weight: 400; font-size: .8rem; }

/* top bar */
.topbar { display: flex; align-items: center; gap: 1.5rem; padding: .6rem 1.25rem; background: var(--brand); color: var(--brand-ink); flex-wrap: wrap; }
.brand { color: var(--brand-ink); text-decoration: none; font-weight: 700; display: flex; align-items: center; gap: .55rem; letter-spacing: .01em; }
.brand-mark { width: 22px; height: 22px; border-radius: 6px; background: linear-gradient(180deg, #7fd6e0 0 45%, #ffffff 45% 55%, #2a8fa0 55%); display: inline-block; }
.mainnav { display: flex; gap: .25rem; flex-wrap: wrap; flex: 1; }
.mainnav a { color: rgba(255,255,255,.85); text-decoration: none; padding: .35rem .7rem; border-radius: 7px; }
.mainnav a:hover { background: rgba(255,255,255,.12); color: #fff; }
.mainnav a.active { background: rgba(255,255,255,.2); color: #fff; font-weight: 600; }
.userbox { display: flex; align-items: center; gap: .75rem; }
.userbox .username { color: #fff; text-decoration: none; font-size: .9rem; opacity: .9; }
.linkbtn { background: none; border: 1px solid rgba(255,255,255,.4); color: #fff; padding: .25rem .6rem; border-radius: 6px; cursor: pointer; font: inherit; font-size: .85rem; }
.linkbtn:hover { background: rgba(255,255,255,.12); }

.page { max-width: 1280px; margin: 0 auto; padding: 1.25rem 1.25rem 3rem; }
.foot { text-align: center; color: var(--muted); font-size: .8rem; padding: 1rem 1rem 2rem; }
.pagehead { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap; }
.crumbs { margin: 0 0 .2rem; font-size: .85rem; }
.actions, .actions-bar { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.actions form { margin: 0; }
.actions-bar { margin: 0 0 1rem; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem 1.25rem; margin-bottom: 1rem; box-shadow: var(--shadow); }
.card.narrow { max-width: 520px; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; align-items: start; }
.grid2 > .card, .grid2 > .stack > .card { margin-bottom: 0; }
.grid2 { margin-bottom: 1rem; }
.stack { display: flex; flex-direction: column; gap: .85rem; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: .85rem; }
@media (max-width: 900px) { .grid2, .row2 { grid-template-columns: 1fr; } }

/* stats */
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1rem; margin-bottom: 1rem; }
@media (max-width: 800px) { .stats { grid-template-columns: repeat(2, 1fr); } }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.1rem; text-decoration: none; color: var(--ink); box-shadow: var(--shadow); display: flex; flex-direction: column; }
.stat:hover { border-color: var(--accent); }
.stat-num { font-size: 1.9rem; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat-label { color: var(--muted); font-size: .85rem; }
.stat-alert .stat-num { color: var(--amber); }

/* forms */
label { display: flex; flex-direction: column; gap: .3rem; font-weight: 600; font-size: .88rem; }
.lbl { display: flex; align-items: center; gap: .4rem; flex-wrap: wrap; }
label.check { flex-direction: row; align-items: center; gap: .5rem; font-weight: 400; }
input, select, textarea { font: inherit; font-weight: 400; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 7px; padding: .5rem .6rem; min-width: 0; }
input:not([type=checkbox]):not([type=radio]):not([type=file]), select, textarea { width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .4rem; padding: .5rem .95rem; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); text-decoration: none; font: inherit; font-weight: 600; font-size: .9rem; cursor: pointer; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn.small { padding: .25rem .6rem; font-size: .8rem; }
.btn[disabled] { opacity: .6; cursor: wait; }
form.inline { display: inline; }
.fieldgroup { border: 1px solid var(--line); border-radius: 8px; padding: .75rem 1rem 1rem; margin: 0; }
.fieldgroup legend { padding: 0 .4rem; font-weight: 700; font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.fieldgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: .75rem 1rem; }
.fieldgrid.four { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 700px) { .fieldgrid.four { grid-template-columns: 1fr 1fr; } }
.field.parsed input { border-color: var(--green); background: var(--green-bg); }
.field.missing input { border-style: dashed; }
.srcline { font-weight: 400; font-size: .72rem; color: var(--muted); font-family: ui-monospace, Menlo, Consolas, monospace; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.filters { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: .75rem 1rem; align-items: end; }
.filters .wide { grid-column: 1 / -1; }
.filter-actions { display: flex; gap: .5rem; }

.dropzone { border: 2px dashed var(--line); border-radius: var(--radius); padding: 1.5rem; text-align: center; align-items: center; cursor: pointer; }
.dropzone:hover { border-color: var(--accent); }
.dz-title { font-size: 1.05rem; }

/* tables */
.tablewrap { overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; font-size: .9rem; }
.tbl th { text-align: left; font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); border-bottom: 1px solid var(--line); padding: .45rem .5rem; white-space: nowrap; }
.tbl td { border-bottom: 1px solid var(--line); padding: .5rem; vertical-align: top; }
.tbl tbody tr:hover { background: color-mix(in srgb, var(--accent) 5%, transparent); }
.tbl.compact td { padding: .4rem .5rem; }
.tbl .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tbl form { margin: 0; }

/* badges & flags */
.badge { display: inline-block; padding: .08rem .5rem; border-radius: 999px; font-size: .75rem; font-weight: 700; text-decoration: none; white-space: nowrap; vertical-align: middle; }
.badge.big { font-size: .85rem; padding: .15rem .7rem; }
.badge-green { background: var(--green-bg); color: var(--green); }
.badge-amber { background: var(--amber-bg); color: var(--amber); }
.badge-red { background: var(--red-bg); color: var(--red); }
.badge-blue { background: var(--blue-bg); color: var(--blue); }
.badge-grey { background: var(--grey-bg); color: var(--muted); }
.flaglist { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: .5rem; }
.flaglist li { display: flex; gap: .5rem; align-items: baseline; flex-wrap: wrap; }
.flags { list-style: none; padding: 0; margin: 0 0 .5rem; display: flex; flex-direction: column; gap: .4rem; }
.flags li { display: flex; gap: .5rem; align-items: baseline; }
.flags .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; transform: translateY(-1px); }
.flag-red .dot { background: var(--red); }
.flag-amber .dot { background: #d99a00; }
.flag-info .dot { background: var(--muted); }
.links { margin: 0 0 1rem; padding-left: 1.1rem; }

.kv { display: grid; grid-template-columns: minmax(140px, 40%) 1fr; gap: .3rem 1rem; margin: 0; }
.kv dt { color: var(--muted); font-size: .88rem; }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; }
.editbox { margin-top: 1rem; }
.editbox > summary { list-style: none; display: inline-flex; }
.editbox[open] > summary { margin-bottom: 1rem; }
.editbox > summary::-webkit-details-marker { display: none; }
details.danger { margin: 1rem 0; color: var(--muted); font-size: .9rem; }
details.danger summary { cursor: pointer; }
details.danger form { margin-top: .5rem; }
.rawtext pre { white-space: pre-wrap; max-height: 420px; overflow: auto; background: var(--bg); padding: .75rem; border-radius: 8px; }
.rawtext summary { cursor: pointer; font-weight: 600; }
pre.cmd { background: var(--bg); padding: .6rem .75rem; border-radius: 8px; overflow-x: auto; }

.tabs { display: flex; gap: .25rem; margin-bottom: .75rem; }
.tabs a { padding: .35rem .8rem; border-radius: 999px; text-decoration: none; color: var(--muted); border: 1px solid transparent; }
.tabs a.active { background: var(--surface); border-color: var(--line); color: var(--ink); font-weight: 600; }

/* flash */
.flash { padding: .65rem .9rem; border-radius: 8px; margin-bottom: 1rem; border: 1px solid transparent; }
.flash-success { background: var(--green-bg); color: var(--green); }
.flash-error { background: var(--red-bg); color: var(--red); }
.flash-info { background: var(--blue-bg); color: var(--blue); }

/* auth */
body.bare { display: flex; min-height: 100vh; align-items: center; justify-content: center; background: radial-gradient(1200px 600px at 50% -10%, color-mix(in srgb, var(--brand) 25%, var(--bg)), var(--bg)); }
.auth-wrap { width: 100%; max-width: 400px; padding: 1.25rem; }
.auth-brand { display: flex; align-items: center; gap: .55rem; justify-content: center; font-weight: 700; font-size: 1.15rem; margin-bottom: 1rem; }
.auth-card h1 { font-size: 1.3rem; margin-bottom: 1rem; }

/* map */
.mapcard { padding: 0; overflow: hidden; }
.bigmap { height: calc(100vh - 250px); min-height: 420px; }
.minimap { height: 260px; border-radius: 8px; margin-top: .75rem; }
.maplegend { display: flex; gap: 1.25rem; flex-wrap: wrap; padding: .6rem 1rem; border-top: 1px solid var(--line); }
.lg { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: .35rem; vertical-align: -1px; }
.lg-ours { background: #e8590c; }
.lg-tanker { background: #0f7c8c; }
.lg-other { background: #98a6b3; }
.inline-controls { display: flex; gap: 1rem; align-items: flex-end; flex-wrap: wrap; }
.leaflet-popup-content { font: 13px/1.45 system-ui, sans-serif; }
.leaflet-popup-content .pop-name { font-weight: 700; font-size: 14px; }
.leaflet-container { background: #aad3df; }

@media (max-width: 640px) {
  .topbar { gap: .5rem 1rem; padding: .6rem .9rem; }
  .userbox { margin-left: auto; }
  .page { padding: 1rem .9rem 2.5rem; }
  .card { padding: 1rem; }
  .kv { grid-template-columns: 1fr 1fr; }
  .bigmap { height: 70vh; }
}
