:root {
  --bg: #EEF1F5;
  --surface: #FFFFFF;
  --ink: #172033;
  --muted: #5D6778;
  --line: #D6DCE4;
  --accent: #2A4A96;
  --on-accent: #FFFFFF;
  --gain: #0A7A4A;
  --loss: #C0382C;
  --gain-fill: rgba(10, 122, 74, .16);
  --loss-fill: rgba(192, 56, 44, .16);
  --focus: #E8A317;
  --font: "Public Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0C121E;
    --surface: #141C2B;
    --ink: #E5E9F0;
    --muted: #95A1B5;
    --line: #243046;
    --accent: #7C9BE6;
    --on-accent: #0C121E;
    --gain: #3CC98A;
    --loss: #F07A6E;
    --gain-fill: rgba(60, 201, 138, .16);
    --loss-fill: rgba(240, 122, 110, .16);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 16px/1.45 var(--font);
  font-variant-numeric: tabular-nums;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }

.gain { color: var(--gain); }
.loss { color: var(--loss); }

/* ---------- pasek ---------- */
.bar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 2px;
  min-height: 56px; padding: env(safe-area-inset-top, 0px) 4px 0;
  background: var(--bg);
}
.bar h1 { flex: 1; margin: 0; padding: 0 8px; font-size: 18px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn {
  width: 44px; height: 44px; flex: none; display: grid; place-items: center;
  border: 0; border-radius: 50%; background: transparent; cursor: pointer;
}
.icon-btn:active { background: var(--line); }
.icon-btn svg { width: 22px; height: 22px; }
.spin svg { animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

main { max-width: 720px; margin: 0 auto; padding: 0 14px calc(96px + env(safe-area-inset-bottom, 0px)); }

/* ---------- podsumowanie ---------- */
.hero { padding: 8px 2px 4px; }
.hero .label { font-size: 14px; color: var(--muted); }
.hero .total { font-size: clamp(36px, 11vw, 52px); font-weight: 700; letter-spacing: -0.02em; line-height: 1.1; margin: 2px 0 6px; }
.hero .result { font-size: 18px; font-weight: 700; }
.hero .result small { font-size: 15px; font-weight: 600; margin-left: 6px; }
.hero .session { margin-top: 2px; font-size: 14px; color: var(--muted); }
.hero .session b { font-weight: 600; }
.stamp { margin-top: 6px; font-size: 13px; color: var(--muted); }

/* ---------- wykres ---------- */
.chart { margin: 14px -14px 0; padding: 0 14px; }
.chart svg { display: block; width: 100%; height: 190px; touch-action: pan-y; }
.chart .readout { min-height: 40px; font-size: 14px; color: var(--muted); padding: 4px 2px 0; }
.chart .readout b { color: var(--ink); font-weight: 700; }
.chart .legend { display: flex; gap: 16px; font-size: 13px; color: var(--muted); padding: 2px 2px 0; }
.chart .legend i { display: inline-block; width: 16px; height: 0; vertical-align: middle; margin-right: 6px; border-top: 2.5px solid var(--ink); }
.chart .legend i.dash { border-top: 2px dashed var(--muted); }
.ranges { display: flex; gap: 6px; margin: 8px 0 0; }
.ranges button {
  flex: 1; min-height: 38px; border: 1px solid var(--line); border-radius: 10px;
  background: transparent; font-size: 14px; font-weight: 600; color: var(--muted); cursor: pointer;
}
.ranges button[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.chart-empty { height: 120px; display: grid; place-items: center; color: var(--muted); font-size: 14px; border: 1px dashed var(--line); border-radius: 12px; margin-top: 12px; text-align: center; padding: 0 16px; }

/* ---------- pozycje ---------- */
h2.sec { margin: 26px 2px 8px; font-size: 15px; font-weight: 700; color: var(--muted); display: flex; justify-content: space-between; }
.positions { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: 14px; border: 1px solid var(--line); }
.positions li + li { border-top: 1px solid var(--line); }
.positions a { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; padding: 12px 14px; color: inherit; text-decoration: none; }
.positions .n { font-weight: 700; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.positions .v { font-weight: 700; text-align: right; }
.positions .s { font-size: 13px; color: var(--muted); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.positions .r { font-size: 13px; font-weight: 600; text-align: right; }
.positions .closed .n, .positions .closed .v { font-weight: 500; color: var(--muted); }

/* ---------- przyciski ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 20px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface);
  font-weight: 700; font-size: 15px; cursor: pointer; text-decoration: none; color: var(--ink);
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.danger { color: var(--loss); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.fab {
  position: fixed; z-index: 10; right: 16px; left: 16px;
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  max-width: 688px; margin: 0 auto;
  box-shadow: 0 6px 20px rgba(20, 30, 60, .25);
}
.stack { display: grid; gap: 10px; margin-top: 18px; }

/* ---------- formularz ---------- */
.field { margin: 14px 0 0; }
.field > label, .field > .lab { display: block; font-size: 14px; font-weight: 600; margin: 0 2px 6px; }
.field input[type="text"], .field input[type="date"], .field input[type="search"] {
  width: 100%; min-height: 50px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface);
  font-size: 17px;
}
.field input:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.field .help { margin: 6px 2px 0; font-size: 13px; color: var(--muted); }
.help button { border: 0; background: none; color: var(--accent); font-weight: 700; padding: 4px 2px; cursor: pointer; font-size: 13px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.two .field { margin-top: 14px; }

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 0; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--surface); }
.seg button { min-height: 46px; border: 0; background: transparent; font-weight: 700; cursor: pointer; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--bg); }

.results { list-style: none; margin: 6px 0 0; padding: 0; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); overflow: hidden; }
.results li + li { border-top: 1px solid var(--line); }
.results button { display: block; width: 100%; text-align: left; padding: 10px 14px; border: 0; background: transparent; cursor: pointer; }
.results b { display: block; }
.results span { font-size: 13px; color: var(--muted); }
.chosen { display: flex; align-items: center; gap: 10px; padding: 10px 8px 10px 14px; border: 1px solid var(--accent); border-radius: 12px; background: var(--surface); }
.chosen div { flex: 1; min-width: 0; }
.chosen b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chosen span { font-size: 13px; color: var(--muted); }
.chosen button { border: 0; background: none; color: var(--accent); font-weight: 700; padding: 8px; cursor: pointer; }
.summary { margin: 18px 2px 0; padding: 12px 14px; border-radius: 12px; background: var(--surface); border: 1px solid var(--line); font-size: 15px; }
.warn { margin: 12px 2px 0; padding: 10px 14px; border-radius: 12px; border: 1px solid var(--loss); color: var(--loss); font-size: 14px; }

/* ---------- szczegóły pozycji ---------- */
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; margin-top: 16px; }
.stats div { background: var(--surface); padding: 10px 12px; }
.stats div:last-child:nth-child(odd) { grid-column: span 2; }
.stats dt { font-size: 13px; color: var(--muted); }
.stats dd { margin: 2px 0 0; font-weight: 700; }
.txs { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; }
.txs li + li { border-top: 1px solid var(--line); }
.txs a { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; padding: 12px 14px; color: inherit; text-decoration: none; }
.txs .k { font-weight: 700; }
.txs .d, .txs .p { font-size: 13px; color: var(--muted); }
.txs .p { text-align: right; }
.txs .t { text-align: right; font-weight: 600; }
.tag { display: inline-block; font-size: 12px; font-weight: 700; padding: 1px 8px; border-radius: 8px; margin-right: 6px; vertical-align: 1px; }
.tag.buy { background: var(--gain-fill); color: var(--gain); }
.tag.sell { background: var(--loss-fill); color: var(--loss); }

.empty { margin: 40px 2px; text-align: center; }
.empty h2 { font-size: 22px; margin: 0 0 8px; }
.empty p { color: var(--muted); margin: 0 0 20px; }
.error { margin: 12px 0; padding: 12px 14px; border: 1px solid var(--loss); border-radius: 12px; font-size: 14px; background: var(--surface); }
.note { font-size: 14px; color: var(--muted); margin: 10px 2px; }

dialog { width: min(92vw, 400px); border: 0; border-radius: 16px; padding: 20px; background: var(--surface); color: var(--ink); }
dialog::backdrop { background: rgba(8, 14, 28, .55); }
dialog h3 { margin: 0 0 8px; font-size: 18px; }
dialog p { margin: 0 0 16px; color: var(--muted); }
dialog .row { display: flex; gap: 8px; justify-content: flex-end; }

.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom, 0px)); z-index: 30;
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  padding: 10px 16px; border-radius: 12px; background: var(--ink); color: var(--bg); font-size: 15px;
  transition: transform .2s, opacity .2s; max-width: calc(100vw - 32px);
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
