/* SAGE - serwis modelowania ksztaltow planetoid.
   Uklad: waski pasek gorny + jedna kolumna tresci (max 1180px); panele danych w siatce,
   ktora na telefonie przechodzi w jedna kolumne. */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --ink: #18202d;
  --muted: #5d6676;
  --line: #dde1e8;
  --accent: #2c56c8;
  --accent-ink: #ffffff;
  --accent-soft: #e6ecfa;
  --good: #1f7a4d;
  --warn: #a86400;
  --bad: #b3261e;
  --plot-obs: #18202d;
  --plot-model: #d0341f;
  --plot-grid: #e8ebf0;
  --font-display: "Bricolage Grotesque", "Segoe UI", system-ui, sans-serif;
  --font-body: "Source Sans 3", "Segoe UI", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, "SFMono-Regular", Consolas, monospace;
  --step--1: 0.8125rem;
  --step-0: 0.9375rem;
  --step-1: 1.125rem;
  --step-2: 1.5rem;
  --step-3: 2rem;
  --radius: 6px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141c;
    --surface: #161d28;
    --ink: #e6eaf1;
    --muted: #97a1b2;
    --line: #2a3443;
    --accent: #7c9cf0;
    --accent-ink: #0f141c;
    --accent-soft: #1d2840;
    --good: #5cc28d;
    --warn: #e0a54a;
    --bad: #f0857c;
    --plot-obs: #e6eaf1;
    --plot-model: #ff7b5e;
    --plot-grid: #222b38;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f141c;
  --surface: #161d28;
  --ink: #e6eaf1;
  --muted: #97a1b2;
  --line: #2a3443;
  --accent: #7c9cf0;
  --accent-ink: #0f141c;
  --accent-soft: #1d2840;
  --good: #5cc28d;
  --warn: #e0a54a;
  --bad: #f0857c;
  --plot-obs: #e6eaf1;
  --plot-model: #ff7b5e;
  --plot-grid: #222b38;
  color-scheme: dark;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: var(--step-0);
  line-height: 1.5;
}
h1, h2, h3 { font-family: var(--font-display); font-weight: 600; line-height: 1.2; text-wrap: balance; margin: 0; }
h1 { font-size: var(--step-3); letter-spacing: -0.01em; }
h2 { font-size: var(--step-2); }
h3 { font-size: var(--step-1); }
a { color: var(--accent); }
code, .mono, td.num, .num { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.num { font-size: 0.92em; }
.muted { color: var(--muted); }
.small { font-size: var(--step--1); }

body::before { content: ""; position: fixed; inset: 0 0 auto; height: env(safe-area-inset-top, 0px); z-index: 10; background: var(--surface); }
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 5;
  background: var(--surface); border-bottom: 1px solid var(--line);
  padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.topbar-in { max-width: 1180px; margin: 0 auto; display: flex; align-items: center; gap: 16px; min-height: 52px; flex-wrap: wrap; }
.brand { font-family: var(--font-display); font-weight: 700; font-size: var(--step-1); letter-spacing: 0.02em; text-decoration: none; color: var(--ink); }
.brand span { color: var(--accent); }
.crumbs { display: flex; gap: 6px; flex-wrap: wrap; color: var(--muted); font-size: var(--step--1); min-width: 0; }
.crumbs a { color: var(--muted); text-decoration: none; }
.crumbs a:hover { color: var(--accent); }
.topbar .spacer { flex: 1; }
.user { font-size: var(--step--1); color: var(--muted); display: flex; gap: 10px; align-items: center; }

main {
  max-width: 1180px; margin: 0 auto;
  padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-block: 24px 64px;
  display: flex; flex-direction: column; gap: 24px;
}
.head { display: flex; gap: 16px; align-items: flex-end; flex-wrap: wrap; justify-content: space-between; }
.head .sub { color: var(--muted); margin-top: 4px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; min-width: 0; display: flex; flex-direction: column; gap: 12px; }
.panel > h3 { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.panel > h3 .muted { font-family: var(--font-body); font-weight: 400; font-size: var(--step--1); }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
@media (max-width: 860px) { .grid2, .grid3 { grid-template-columns: minmax(0, 1fr); } }

/* przelaczniki widokow */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; padding: 8px 12px; font: inherit; color: var(--muted); cursor: pointer; }
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }

/* stany */
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; font-size: var(--step--1); background: var(--surface); white-space: nowrap; }
.chip b { font-family: var(--font-mono); font-weight: 500; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.st-active .dot, .st-evolve .dot { background: var(--good); }
.st-screen .dot { background: var(--accent); }
.st-dormant .dot, .st-paused .dot { background: var(--warn); }
.st-merged .dot, .st-dead .dot, .st-finished .dot { background: var(--muted); }
.stage { font-family: var(--font-mono); font-size: var(--step--1); border: 1px solid var(--line); border-radius: 4px; padding: 0 5px; }

/* liczby glowne */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat { border-left: 2px solid var(--line); padding: 2px 0 2px 12px; }
.stat .k { font-size: var(--step--1); color: var(--muted); letter-spacing: 0.02em; }
.stat .v { font-family: var(--font-mono); font-size: var(--step-1); font-variant-numeric: tabular-nums; }

/* tabele */
.tablewrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: var(--step--1); }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; letter-spacing: 0.03em; }
td.num, th.num { text-align: right; }
tr.sel td { background: var(--accent-soft); }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--accent-soft); }

/* formularze */
form.f { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; align-items: end; }
label { display: flex; flex-direction: column; gap: 4px; font-size: var(--step--1); color: var(--muted); }
input, select, textarea { font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 4px; padding: 6px 8px; min-width: 0; }
input[type="checkbox"] { width: 16px; height: 16px; }
button.btn { font: inherit; font-weight: 600; border-radius: 4px; padding: 7px 14px; border: 1px solid var(--accent); background: var(--accent); color: var(--accent-ink); cursor: pointer; }
button.btn.ghost { background: transparent; color: var(--accent); }
button.btn:disabled { opacity: 0.5; cursor: progress; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.msg { border-radius: 4px; padding: 8px 12px; font-size: var(--step--1); white-space: pre-wrap; }
.msg.err { background: color-mix(in srgb, var(--bad) 12%, var(--surface)); color: var(--bad); }
.msg.ok { background: color-mix(in srgb, var(--good) 12%, var(--surface)); color: var(--good); }
.note { font-size: var(--step--1); color: var(--muted); }

/* wykresy */
svg.plot { width: 100%; height: auto; display: block; }
svg.plot text { fill: var(--muted); font-family: var(--font-mono); font-size: 10px; }
svg.plot .ax { stroke: var(--line); fill: none; }
svg.plot .gr { stroke: var(--plot-grid); fill: none; }
svg.plot .obs { fill: var(--plot-obs); }
svg.plot .err { stroke: var(--plot-obs); opacity: 0.35; }
svg.plot .model { stroke: var(--plot-model); fill: none; stroke-width: 1.6; }
svg.plot .modelpt { fill: var(--plot-model); }
svg.plot .acc { fill: var(--accent); }
svg.plot .accline { stroke: var(--accent); fill: none; stroke-width: 1.5; }
svg.plot .lbl { fill: var(--ink); font-family: var(--font-body); font-size: 11px; }
.fits { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.fit { border: 1px solid var(--line); border-radius: 4px; padding: 8px; min-width: 0; }
.fit .t { font-family: var(--font-mono); font-size: 11px; color: var(--muted); display: flex; justify-content: space-between; gap: 8px; }

/* mapa biegunow i model 3D */
.mapwrap { position: relative; }
.mapwrap .scr { fill: none; stroke: var(--muted); stroke-width: 0.9; opacity: 0.55; }
.legend { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; font-size: var(--step--1); color: var(--muted); }
.ramp { width: 120px; height: 8px; border-radius: 2px; background: linear-gradient(90deg, #2c56c8, #4aa3a2, #e2c044, #d4572e); }
.m3dpair { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 12px; }
.m3dpair figure { margin: 0; min-width: 0; }
.m3dpair .steps { display: flex; flex-wrap: wrap; gap: 2px 8px; margin-top: 4px; font-variant-numeric: tabular-nums; }
.m3dpair .steps span { display: inline-flex; align-items: center; gap: 3px; }
.m3dpair .steps i { width: 12px; height: 12px; border-radius: 2px; display: inline-block; }
.m3dpair figcaption { font-size: var(--step--1); margin-top: 4px; }
canvas.model3d { width: 100%; aspect-ratio: 1 / 1; max-width: 100%; display: block; cursor: grab; background: var(--bg); border-radius: 4px; touch-action: none; }
.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 4px 16px; font-size: var(--step--1); }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.loading { color: var(--muted); font-size: var(--step--1); }
.empty { color: var(--muted); padding: 12px 0; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
#tabc { display: flex; flex-direction: column; gap: 24px; }

/* wagi krzywych niszy */
.wts { columns: 300px; column-gap: 28px; }
.wrow { break-inside: avoid; margin-bottom: 3px; display: grid; grid-template-columns: minmax(0, 7.5em) 1fr 3em; gap: 8px; align-items: center; font-size: var(--step--1); }
.wrow .wn { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--font-mono); }
.wrow .wv { text-align: right; font-variant-numeric: tabular-nums; }
.wbar { position: relative; height: 10px; border-radius: 2px; background: linear-gradient(to right, transparent calc(25% - 0.5px), var(--muted) calc(25% - 0.5px), var(--muted) calc(25% + 0.5px), transparent calc(25% + 0.5px), transparent calc(75% - 0.5px), var(--muted) calc(75% - 0.5px), var(--muted) calc(75% + 0.5px), transparent calc(75% + 0.5px)), var(--line); }
.wbar i { position: absolute; top: 0; bottom: 0; border-radius: 2px; }
.wbar i.up { background: #d4572e; }
.wbar i.dn { background: #608cdc; }
.wbar b { position: absolute; left: 50%; top: -2px; bottom: -2px; width: 1px; background: var(--muted); }

/* edytor krzywej */
svg.plot circle.rej { fill: none; stroke: var(--plot-model); stroke-width: 1.4; }
.chips label.inl { display: inline-flex; flex-direction: row; align-items: center; gap: 6px; white-space: nowrap; }
.cedit { align-items: center; margin: 8px 0; }

/* rozklad obserwacji */
.appwrap { display: grid; grid-template-columns: minmax(0, 440px) minmax(0, 1fr); gap: 20px; align-items: start; }
.appwrap svg { max-width: 440px; }
.applist { display: flex; flex-direction: column; gap: 4px; font-size: var(--step--1); }
.applist i { width: 12px; height: 12px; border-radius: 50%; display: inline-block; margin-right: 6px; vertical-align: -1px; }
@media (max-width: 720px) { .appwrap { grid-template-columns: 1fr; } }

/* edytor krzywej w wierszu tabeli */
tr.cedrow > td { background: var(--bg); padding: 12px 14px 16px; white-space: normal; }
.cedbox { max-width: 1100px; }
.cedbox svg.plot { max-width: 720px; }
.cedbox .grid2 svg.plot { max-width: 100%; }
a.cv.on { font-weight: 600; }

/* etapy i niepewnosci */
.ucard { border-top: 1px solid var(--line); padding-top: 14px; margin-top: 16px; }
.uhead { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: baseline; justify-content: space-between; }
.uhead h4 { margin: 0; font-size: 1rem; }
.ubar { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin: 8px 0 4px; max-width: 520px; }
.ubar i { display: block; height: 100%; background: var(--accent); }
.btn.small { padding: 4px 10px; font-size: var(--step--1); }
svg.plot .pin { fill: #3f9a52; opacity: 0.75; }
svg.plot .pout { fill: var(--muted); opacity: 0.4; }
svg.plot .pref { fill: var(--plot-model); }
svg.plot .pbound { fill: none; stroke: var(--plot-model); stroke-width: 1.5; }
[hidden] { display: none !important; }
.uform { border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px; margin: 10px 0; }
.uform .note { margin: 4px 0; }
.grid2.ures { align-items: start; }
.grid2.ures svg.plot { max-width: 360px; }
.ucard .m3dpair { max-width: 760px; }
