/* Paikkatietokeskus /data — workbench */
:root {
  --bg: #f5f7fa; --panel: #ffffff; --panel-2: #f7f9fc; --line: #e2e7ee; --line-2: #ccd5e0;
  --text: #16202c; --muted: #5d6b7b; --faint: #8994a3;
  --accent: #0f6fd1; --accent-soft: #e7f1fd; --good: #1a7f37; --warn: #b35900; --bad: #c62828;
  --shadow: 0 1px 2px rgba(16,24,40,.05), 0 6px 18px rgba(16,24,40,.06);
  --radius: 12px;
  color-scheme: light;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1319; --panel: #161d25; --panel-2: #1c242e; --line: #26313d; --line-2: #35414f;
    --text: #e7eef5; --muted: #9aa7b6; --faint: #6f7c8b;
    --accent: #4c9dff; --accent-soft: #14294a; --good: #3fb950; --warn: #f0a44b; --bad: #ff6b6b;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.45; }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.spacer { flex: 1; }
h1 { font-size: 20px; margin: 0 0 4px; }
h2 { font-size: 15px; margin: 0; }
a { color: var(--accent); }

/* login */
.login-shell { min-height: 100vh; display: grid; place-items: center; padding: 20px;
  background: radial-gradient(1000px 500px at 20% -10%, var(--accent-soft), transparent 60%), var(--bg); }
.login-card { width: min(430px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 26px; box-shadow: var(--shadow); display: grid; gap: 8px; }
.login-card h1 { font-size: 22px; margin: 10px 0 2px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 800; letter-spacing: .04em; font-size: 13px; text-decoration: none; color: var(--text); }
.brand small { font-weight: 600; color: var(--muted); }
.brand-glyph { display: grid; place-items: center; width: 24px; height: 24px; border-radius: 7px; background: var(--accent); color: #fff; font-size: 13px; }
.login-card label { font-size: 12px; font-weight: 600; color: var(--muted); margin-top: 6px; }
.password-field { display: flex; gap: 6px; }
.password-field input { flex: 1; }
.password-field button { width: 40px; border-radius: 8px; border: 1px solid var(--line-2); background: var(--panel-2); cursor: pointer; }
.form-error { color: var(--bad); min-height: 18px; font-size: 13px; margin: 4px 0 0; }
input, select, textarea, .input { padding: 8px 10px; border-radius: 8px; border: 1px solid var(--line-2); background: var(--panel); width: 100%; }
.input.sm { padding: 5px 8px; width: auto; }

/* shell */
.app { min-height: 100vh; display: flex; flex-direction: column; }
.topbar { position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: 14px; padding: 8px 16px;
  background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tab { padding: 6px 12px; border-radius: 8px; border: 1px solid transparent; background: none; cursor: pointer; color: var(--muted); font-weight: 600; }
.tab:hover { background: var(--panel-2); color: var(--text); }
.tab.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.icon-btn { width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); cursor: pointer; color: var(--muted); }
.health { display: flex; gap: 10px; font-size: 12px; color: var(--muted); }
.hdot { display: inline-flex; align-items: center; gap: 5px; }
.hdot i { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); }
.hdot.ok i { background: var(--good); } .hdot.err i { background: var(--bad); }
.workspace { padding: 18px; max-width: 1380px; width: 100%; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }
.head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 14px; flex-wrap: wrap; }
.head p { margin: 0; max-width: 78ch; }
.head-actions { display: flex; gap: 8px; align-items: center; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); margin-bottom: 14px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.card-head.tight { margin-bottom: 6px; }
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 13px; border-radius: 8px; border: 1px solid var(--line-2);
  background: var(--panel); cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:disabled { opacity: .6; cursor: progress; }
.btn.wide { width: 100%; justify-content: center; margin-top: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 5px 11px; border-radius: 999px; border: 1px solid var(--line-2); background: var(--panel); cursor: pointer; font-size: 12.5px; }
.chip:hover { border-color: var(--accent); }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.check, .inline { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.inline input, .inline select { width: auto; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; }
.field { display: grid; gap: 5px; align-content: start; }
.field > label { font-size: 12px; font-weight: 600; color: var(--muted); }
.span2 { grid-column: 1 / -1; }
.actions { display: flex; align-items: center; gap: 12px; }

.metrics { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 12px; margin-bottom: 14px; }
.metric { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.metric span { font-size: 12px; color: var(--muted); }
.metric b { display: block; font-size: 26px; line-height: 1.15; font-variant-numeric: tabular-nums; }
.metric small { display: block; font-size: 12px; color: var(--muted); }
.metric em { display: block; font-size: 11.5px; color: var(--faint); font-style: normal; margin-top: 4px; }
.result-head { display: flex; justify-content: space-between; gap: 14px; align-items: flex-start; flex-wrap: wrap; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.chart-wrap { width: 100%; }
.chart { width: 100%; height: 230px; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 10px; }
.chart .ln { fill: none; stroke-width: 2; }
polyline.ln { fill: none; }
polyline.c0 { stroke: #1565c0; } circle.c0 { fill: #1565c0; }
polyline.c1 { stroke: #ef6c00; } circle.c1 { fill: #ef6c00; }
polyline.c2 { stroke: #2e7d32; } circle.c2 { fill: #2e7d32; }
polyline.c3 { stroke: #6a1b9a; } circle.c3 { fill: #6a1b9a; }
polyline.c4 { stroke: #00838f; } circle.c4 { fill: #00838f; }
polyline.c5 { stroke: #c62828; } circle.c5 { fill: #c62828; }
.legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: 12px; color: var(--muted); margin-top: 4px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; }
.legend .c0 i { background: #1565c0; } .legend .c1 i { background: #ef6c00; } .legend .c2 i { background: #2e7d32; }
.legend .c3 i { background: #6a1b9a; } .legend .c4 i { background: #00838f; } .legend .c5 i { background: #c62828; }

.table-scroll { overflow: auto; max-height: 460px; border: 1px solid var(--line); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { position: sticky; top: 0; background: var(--panel-2); text-align: left; font-size: 12px; color: var(--muted); z-index: 1; }
th, td { padding: 6px 9px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td.n { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--panel-2); }

.notes ul { margin: 6px 0 0; padding-left: 18px; color: var(--muted); font-size: 13px; }
.log .log-lines { margin: 8px 0 0; max-height: 180px; overflow: auto; font-family: ui-monospace, Consolas, monospace;
  font-size: 11.5px; color: var(--muted); background: var(--panel-2); border-radius: 8px; padding: 8px 10px; white-space: pre-wrap; }
.progress { height: 5px; border-radius: 999px; background: var(--line); overflow: hidden; }
.progress i { display: block; height: 100%; background: var(--accent); transition: width .3s; }
.pill { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--panel-2); color: var(--muted); }
.pill.done { background: rgba(26,127,55,.14); color: var(--good); }
.pill.err { background: rgba(198,40,40,.14); color: var(--bad); }
.loading { display: flex; align-items: center; gap: 10px; color: var(--muted); }
.err { border-color: var(--bad); color: var(--bad); }
.empty { color: var(--muted); padding: 10px 0; }
.spinner { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--line-2); border-top-color: var(--accent);
  animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

/* marine */
.marine-toolbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.seg { display: inline-flex; border: 1px solid var(--line-2); border-radius: 8px; overflow: hidden; }
.seg button { padding: 6px 12px; border: 0; background: var(--panel); cursor: pointer; border-right: 1px solid var(--line); }
.seg button:last-child { border-right: 0; }
.seg button.active { background: var(--accent); color: #fff; }
.stat { display: inline-flex; flex-direction: column; line-height: 1.15; }
.stat b { font-size: 16px; font-variant-numeric: tabular-nums; }
.stat small { color: var(--muted); font-size: 11.5px; }
.map-wrap { position: relative; padding: 0; overflow: hidden; }
.map { height: min(58vh, 560px); width: 100%; }
.map-legend { position: absolute; left: 12px; bottom: 12px; display: flex; gap: 12px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; padding: 6px 10px; font-size: 12px; color: var(--muted); box-shadow: var(--shadow); }
.map-legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; }
.sw.fast { background: #1565c0; } .sw.slow { background: #90a4ae; } .sw.extra { background: #7b1fa2; }
.sw.heat { background: linear-gradient(90deg, #4fc3f7, #ffca28, #c62828); border-radius: 2px; width: 22px; }
.map-status { position: absolute; right: 12px; top: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  padding: 5px 10px; font-size: 12px; color: var(--muted); box-shadow: var(--shadow); max-width: 60%; }
.maplibregl-popup-content { background: var(--panel); color: var(--text); border-radius: 10px; border: 1px solid var(--line); }
.pop { display: grid; gap: 3px; font-size: 12.5px; max-height: 260px; overflow: auto; }
.pop div { display: flex; justify-content: space-between; gap: 10px; }
.pop span { color: var(--muted); }

/* statfin */
.variables { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.variable { display: grid; gap: 4px; font-size: 12px; font-weight: 600; color: var(--muted); }
.variable em { font-style: normal; color: var(--accent); }
.variable select { font-weight: 400; color: var(--text); }
.variable small { font-weight: 400; }

/* saved */
.saved-list { display: grid; gap: 10px; }
.saved { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin: 0; }
.saved p { margin: 2px 0 0; }
.saved-heading { margin: 26px 0 12px; font-size: 17px; }
.dashboard-toolbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.dashboard-toolbar select { min-width: 110px; }
.dashboard-area-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 6px; max-height: 230px; overflow: auto; margin-top: 10px; }
.dashboard-area-option { display: flex; align-items: center; gap: 8px; padding: 7px 9px; border-radius: 8px; cursor: pointer; background: var(--panel-2); }
.dashboard-area-option input { width: auto; margin: 0; }
.dashboard-selected { margin: 8px 0; }
#dashboardAreaSearch { max-width: 420px; margin-top: 6px; display: block; }
.dash-kpis { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-bottom: 14px; }
.dash-kpis .metric { min-width: 0; }
.dash-kpis .metric b { font-size: clamp(18px, 2vw, 27px); overflow-wrap: anywhere; }
.dash-kpis .metric b.metric-name { font-size: 21px; }
.dashboard-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.dashboard-grid .card { min-width: 0; margin-bottom: 0; }
.dashboard-grid .card-head { flex-wrap: wrap; }
.dashboard-detail { margin-top: 14px; }
.donut-layout { display: flex; align-items: center; gap: 18px; min-height: 280px; }
.donut { width: min(230px, 48%); aspect-ratio: 1; border-radius: 50%; display: grid; place-items: center; flex: none; }
.donut > div { width: 64%; aspect-ratio: 1; border-radius: 50%; background: var(--panel); display: flex; flex-direction: column;
  align-items: center; justify-content: center; text-align: center; box-shadow: 0 0 0 1px var(--line); }
.donut b { font-size: clamp(15px, 1.7vw, 23px); font-variant-numeric: tabular-nums; }
.donut small { font-size: 11px; color: var(--muted); }
.donut-legend { flex: 1; list-style: none; margin: 0; padding: 0; min-width: 0; }
.donut-legend li { display: flex; align-items: center; gap: 7px; font-size: 12px; padding: 5px 0; border-bottom: 1px solid var(--line); }
.donut-legend i { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.donut-legend span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.donut-legend b { font-variant-numeric: tabular-nums; }
.dash-bars { display: grid; gap: 8px; }
.dash-bar-label { display: flex; justify-content: space-between; gap: 10px; font-size: 12px; }
.dash-bar-label span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dash-bar-label b { flex: none; font-variant-numeric: tabular-nums; }
.dash-bar-track { height: 9px; background: var(--panel-2); border-radius: 999px; overflow: hidden; margin-top: 3px; }
.dash-bar-track i { display: block; height: 100%; border-radius: inherit; background: #1876b6; }
.dash-bar small { color: var(--muted); font-size: 11px; }

.toast { position: fixed; left: 50%; bottom: 22px; transform: translate(-50%, 16px); opacity: 0; pointer-events: none; z-index: 90;
  background: #16202c; color: #fff; padding: 10px 16px; border-radius: 9px; box-shadow: var(--shadow); transition: .2s; max-width: min(620px, 92vw); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.ok { background: #1a7f37; } .toast.error { background: #b42318; }

@media (max-width: 860px) {
  .form-grid { grid-template-columns: 1fr; }
  .workspace { padding: 12px; }
  .topbar { gap: 8px; }
  .health { display: none; }
  .dash-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .dashboard-grid { grid-template-columns: 1fr; }
}
@media (max-width: 520px) {
  .dash-kpis { grid-template-columns: 1fr; }
  .donut-layout { flex-direction: column; }
  .donut { width: min(230px, 75%); }
  .donut-legend { width: 100%; }
}

/* ---------- Rail map: network, region and painted selection ---------- */
.rail-map-card { padding: 12px; margin-bottom: 14px; }
.rail-map-tools { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 10px; }
.rail-map-tools .inline { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.rail-map-tools .inline .input { width: auto; min-width: 170px; }
.rail-map { height: 460px; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); }
.rail-map.painting { cursor: crosshair; }
.rail-map-legend { margin: 8px 2px 0; }
.rail-selection { border: 1px solid var(--line); border-radius: 9px; padding: 10px 12px; background: var(--panel-2);
  color: var(--muted); font-size: 13px; }
.rail-selected-heading { display: flex; justify-content: space-between; align-items: baseline; flex-wrap: wrap; gap: 4px 12px; color: var(--text); }
.rail-selected-heading span { color: var(--muted); font-size: 12px; }
.rail-selection p { margin: 5px 0 8px; }
.rail-selected-columns { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.rail-selected-columns > div { min-width: 0; }
.rail-selected-columns b { color: var(--text); font-size: 12px; }
.rail-selected-list { display: flex; flex-wrap: wrap; align-content: flex-start; gap: 5px; max-height: 170px; overflow: auto;
  padding: 6px 1px 2px; }
.rail-selected-item { width: auto; padding: 4px 8px; border: 1px solid #e5b851; border-radius: 6px;
  background: #fff4d8; color: #694200; font-size: 12px; cursor: pointer; text-align: left; }
.rail-selected-item:hover, .rail-selected-item:focus-visible { border-color: #a86600; outline-color: #a86600; }
@media (prefers-color-scheme: dark) {
  .rail-selected-item { background: #463410; border-color: #946f28; color: #ffe8ae; }
}
@media (max-width: 640px) { .rail-selected-columns { grid-template-columns: 1fr; } }
.checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 6px 14px; margin-top: 8px; }
.checks label { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }
.checks input { width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer; }
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }

.prefetch { grid-column: 1 / -1; border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; font-size: 13px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.metrics.tight { gap: 8px; }

.map.painting, #marineMap.painting { cursor: crosshair; }
.two-col { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px 24px; margin: 10px 0; }
.two-col b { font-size: 12px; }
ul.plain { list-style: none; margin: 6px 0 0; padding: 0; font-size: 13px; }
ul.plain li { padding: 3px 0; border-bottom: 1px solid var(--line); }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: rgba(47,129,247,.08); }

ul.rank { list-style: none; margin: 8px 0 0; padding: 0; font-size: 13px; }
ul.rank li { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; padding: 5px 0;
  border-bottom: 1px solid var(--line); align-items: baseline; }
ul.rank li span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
ul.rank li b { font-variant-numeric: tabular-nums; }
ul.rank li b.up { color: #1a7f37; }
ul.rank li b.down { color: #b42318; }
ul.rank li em { grid-column: 1 / -1; font-style: normal; font-size: 11px; color: var(--muted); }
.card.tight { padding: 10px 12px; }
