:root {
  color-scheme: light;
  --page: #ffffff;
  --surface: #ffffff;
  --subtle: #f5f5f4;
  --subtle-2: #ececea;
  --ink: #111111;
  --ink-2: #5f5e5a;
  --muted: #8f8e89;
  --grid: #e8e7e3;
  --axis: #c9c8c1;
  --border: #e7e7e4;
  --strong: #111111;
  --on-strong: #ffffff;
  --accent: #2a78d6;
  --critical: #d03b3b;
  --warning-bg: #fdf3dc;
  --s-stt: #2a78d6;
  --s-llm: #eb6834;
  --s-tts: #1baf7a;
  --bubble-me: #f1f1ef;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.06);
  --radius: 14px;
  --tap: 44px;
  --nav-h: 64px;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #161616; --subtle: #1d1d1c; --subtle-2: #262625;
    --ink: #f2f2f0; --ink-2: #b9b8b1; --muted: #85847e; --grid: #2a2a28; --axis: #3a3a37;
    --border: #2a2a28; --strong: #f2f2f0; --on-strong: #0d0d0d; --accent: #3987e5;
    --warning-bg: #3a2f14; --s-stt: #3987e5; --s-llm: #d95926; --s-tts: #199e70;
    --bubble-me: #222221; --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #161616; --subtle: #1d1d1c; --subtle-2: #262625;
  --ink: #f2f2f0; --ink-2: #b9b8b1; --muted: #85847e; --grid: #2a2a28; --axis: #3a3a37;
  --border: #2a2a28; --strong: #f2f2f0; --on-strong: #0d0d0d; --accent: #3987e5;
  --warning-bg: #3a2f14; --s-stt: #3987e5; --s-llm: #d95926; --s-tts: #199e70;
  --bubble-me: #222221; --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--page); color: var(--ink); font-size: 15px; line-height: 1.45;
  min-height: 100vh; min-height: 100dvh; -webkit-text-size-adjust: 100%;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
}
button, input, select, textarea { font: inherit; color: inherit; }
button { -webkit-tap-highlight-color: transparent; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
h2 { font-size: 14px; font-weight: 600; margin: 0 0 10px; }
.page-title { font-size: 16px; margin: 4px 0 12px; }

/* ---------- naglowek ---------- */
.top {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--page);
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px); -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1; }
.brand strong { font-size: 15px; white-space: nowrap; }
.version { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; opacity: .8; }
.version:empty { display: none; }
@media (max-width: 420px) { .version .code { display: none; } }
.chip { font-size: 12px; padding: 3px 8px; border-radius: 999px; background: var(--subtle); color: var(--ink-2); white-space: nowrap; }
.chip-soft:empty { display: none; }
.banner { margin: 8px 16px 0; background: var(--warning-bg); border-radius: 10px; padding: 8px 12px; font-size: 13px; }
.alert {
  margin: 8px 16px 0; display: flex; gap: 8px; align-items: flex-start; justify-content: space-between;
  background: var(--critical); color: #fff; border-radius: 12px; padding: 10px 8px 10px 14px; font-size: 14px;
}
.alert span { display: block; }
.alert-help { opacity: 0.9; font-size: 12px; margin-top: 2px; }
.alert .icon-btn.ghost { color: #fff; }
.chip.ok { background: rgba(12, 163, 12, 0.18); color: var(--ink); }
.chip.bad { background: var(--critical); color: #fff; }

.session-pill {
  display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 12px;
  border-radius: 999px; border: 1px solid var(--border); background: var(--surface);
  cursor: pointer; font-variant-numeric: tabular-nums; font-size: 13px; font-weight: 600; white-space: nowrap;
}
.session-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--axis); }
.session-pill.live .dot { background: var(--critical); animation: blink 1.6s infinite; }
.pill-sub { color: var(--ink-2); font-weight: 500; }
.pill-sub:empty { display: none; }
.pill-end { color: var(--ink-2); font-weight: 500; padding-left: 8px; border-left: 1px solid var(--border); }
.icon-btn.danger svg { width: 16px; height: 16px; fill: none; stroke: currentColor; }
@keyframes blink { 50% { opacity: 0.35; } }

/* ---------- przyciski ---------- */
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-width: 36px; height: 36px; padding: 0 10px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-weight: 600; font-size: 13px;
  touch-action: manipulation;
}
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn.ghost { border-color: transparent; background: transparent; color: var(--ink-2); padding: 0; width: 36px; }
.icon-btn.ghost:hover { background: var(--subtle); color: var(--ink); }
.icon-btn.danger { background: var(--critical); border-color: transparent; color: #fff; }
.icon-btn.danger svg { fill: currentColor; stroke: none; width: 14px; height: 14px; }
.icon-btn.danger.idle { background: transparent; border-color: var(--critical); color: var(--critical); }
.btn {
  min-height: var(--tap); padding: 0 16px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
  text-decoration: none; font-weight: 600; font-size: 14px;
}
.btn-primary { background: var(--strong); color: var(--on-strong); border-color: transparent; }
:is(.btn, .icon-btn, .seg button, .tabs button, .send-btn, .call-btn, .line-btn, .session-pill):focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

.seg { display: inline-flex; background: var(--subtle); border-radius: 999px; padding: 3px; gap: 2px; max-width: 100%; }
.seg button {
  min-height: 34px; padding: 0 14px; border: 0; border-radius: 999px; background: transparent;
  cursor: pointer; color: var(--ink-2); white-space: nowrap; font-size: 13px; font-weight: 500;
}
.seg button.on, .seg button[aria-checked="true"] { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }
.seg-full { display: flex; width: 100%; }
.seg-full button { flex: 1; }

/* ---------- uklad ---------- */
main { padding: 12px 16px 16px; max-width: 1100px; margin: 0 auto; }
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--page);
  background: color-mix(in srgb, var(--page) 92%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  height: var(--nav-h); border: 0; background: transparent; cursor: pointer; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; font-size: 11px; font-weight: 500;
}
.tabs button[aria-current="page"] { color: var(--ink); }
.tabs button[aria-current="page"] svg { stroke-width: 2.2; }

/* ---------- rozmowa ---------- */
.tab-chat {
  display: flex; flex-direction: column; align-items: center;
  min-height: calc(100vh - var(--nav-h) - 90px - env(safe-area-inset-bottom));
  min-height: calc(100dvh - var(--nav-h) - 90px - env(safe-area-inset-bottom));
}
.view-switch {
  align-self: center; margin-bottom: 4px;
  position: sticky; top: calc(var(--sticky-top, 57px) + 8px); z-index: 15;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
}
.stage { flex: 1; width: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px; padding: 16px 0; }

.orb-wrap { --level: 0; position: relative; width: min(62vw, 260px); aspect-ratio: 1; }
.orb {
  position: absolute; inset: 0; border-radius: 50%; overflow: hidden;
  transform: scale(calc(0.92 + var(--level) * 0.16));
  transition: transform 90ms linear, filter 400ms ease;
  box-shadow: 0 20px 60px -20px rgba(42, 120, 214, 0.45);
}
.orb-core {
  position: absolute; inset: -25%;
  background: conic-gradient(from 0deg, #0b4fc4, #4fc3dc, #e9f8fc, #1d6fe0, #8fe0ec, #0a3a9c, #5cc8e0, #e6f6fb, #0b4fc4);
  filter: blur(14px);
  animation: spin 14s linear infinite;
}
.orb-gloss {
  position: absolute; inset: 0; border-radius: 50%;
  background: radial-gradient(circle at 32% 28%, rgba(255, 255, 255, 0.55), transparent 42%),
              radial-gradient(circle at 70% 80%, rgba(10, 40, 120, 0.25), transparent 55%);
}
.orb-wrap[data-state="idle"] .orb { filter: saturate(0.85); }
.orb-wrap[data-state="listening"] .orb-core { animation-duration: 8s; }
.orb-wrap[data-state="thinking"] .orb-core { animation-duration: 2.2s; }
.orb-wrap[data-state="thinking"] .orb { animation: breathe 1.2s ease-in-out infinite; }
.orb-wrap[data-state="speaking"] .orb-core { animation-duration: 4s; }
.orb-wrap[data-state="speaking"].remote .orb { animation: breathe 0.9s ease-in-out infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes breathe { 50% { transform: scale(1.04); } }

.call-btn {
  position: absolute; left: 50%; bottom: -26px; transform: translateX(-50%);
  width: 56px; height: 56px; border-radius: 50%; border: 4px solid var(--page);
  background: var(--strong); color: var(--on-strong); cursor: pointer;
  display: grid; place-items: center; box-shadow: var(--shadow); transition: background 0.2s;
}
.call-btn svg { width: 22px; height: 22px; fill: currentColor; stroke: none; transition: transform 0.25s; }
.call-btn.active { background: var(--critical); color: #fff; }
.call-btn.active svg { transform: rotate(135deg); }

.status { margin: 26px 0 0; font-size: 14px; color: var(--ink-2); text-align: center; min-height: 20px; }

/* w trakcie rozmowy: mala kula u gory, pod nia zapis na zywo */
.orb-wrap { transition: width 0.45s cubic-bezier(0.2, 0.8, 0.2, 1); }
.live-chat { display: none; }
.stage.compact { justify-content: flex-start; gap: 10px; padding-top: 8px; }
.stage.compact .orb-wrap { width: 112px; }
.stage.compact .orb { box-shadow: 0 10px 30px -12px rgba(42, 120, 214, 0.5); }
.stage.compact .call-btn { width: 44px; height: 44px; bottom: -18px; border-width: 3px; }
.stage.compact .call-btn svg { width: 18px; height: 18px; }
.stage.compact .status { margin-top: 18px; font-size: 13px; }
.stage.compact .caption { display: none; }
.stage.compact .live-chat {
  display: flex; flex: none; width: 100%; max-width: 720px; overflow-y: auto; overscroll-behavior: contain;
  height: 240px; /* nadpisywane z JS: reszta ekranu nad polem wiadomosci */
  padding: 8px 2px 12px; border-top: 1px solid var(--border);
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 16px);
  mask-image: linear-gradient(to bottom, transparent 0, #000 16px);
}
.stage.compact { flex: none; }
.caption { width: 100%; max-width: 560px; text-align: center; min-height: 72px; }
.cap-heard { margin: 0 0 6px; color: var(--muted); font-size: 14px; }
.cap-reply { margin: 0; font-size: 17px; line-height: 1.45; font-weight: 500; }
.cap-reply .code { font-size: 12px; font-weight: 700; color: var(--muted); margin-right: 6px; letter-spacing: 0.02em; }

.chat { flex: 1; width: 100%; max-width: 720px; display: flex; flex-direction: column; gap: 10px; padding: 12px 0; }
.msg { max-width: 88%; padding: 10px 14px; border-radius: 18px; background: var(--surface); border: 1px solid var(--border); }
.msg.me { align-self: flex-end; background: var(--bubble-me); border-color: transparent; border-bottom-right-radius: 6px; }
.msg.robot { align-self: flex-start; border-bottom-left-radius: 6px; }
.msg.sys { align-self: center; background: transparent; border: 0; color: var(--muted); font-size: 12px; padding: 2px; }
.msg .code { font-weight: 700; font-size: 12px; color: var(--muted); margin-right: 6px; }
.msg .meta { color: var(--muted); font-size: 11px; margin-top: 4px; font-variant-numeric: tabular-nums; }
.msg .replay { margin-top: 8px; height: 30px; font-size: 12px; padding: 0 10px; }
.msg .replay svg { width: 12px; height: 12px; fill: currentColor; stroke: none; }
.empty { color: var(--muted); text-align: center; padding: 24px 8px; font-size: 14px; }

.pill {
  position: sticky; bottom: calc(var(--nav-h) + 10px + env(safe-area-inset-bottom));
  width: 100%; max-width: 720px; margin-top: 8px;
  border: 1px solid var(--strong); border-radius: 22px; background: var(--surface);
  padding: 10px 10px 8px 16px; box-shadow: var(--shadow);
}
.pill textarea {
  width: 100%; border: 0; outline: 0; resize: none; background: transparent; font-size: 15px;
  max-height: 120px; padding: 2px 0 6px;
}
.pill textarea::placeholder { color: var(--muted); }
.pill-row { display: flex; align-items: center; gap: 2px; margin-left: -8px; }
.pill-hint { flex: 1; font-size: 12px; color: var(--muted); padding-left: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.send-btn {
  width: 36px; height: 36px; border-radius: 50%; border: 0; background: var(--strong); color: var(--on-strong);
  display: grid; place-items: center; cursor: pointer;
}
.send-btn svg { width: 18px; height: 18px; stroke-width: 2.2; }
#pttBtn.recording { background: var(--critical); color: #fff; box-shadow: 0 0 0 6px rgba(208, 59, 59, 0.25); }

/* ---------- ustawienia (panel od dolu) ---------- */
.sheet {
  border: 1px solid var(--border); border-radius: 20px; background: var(--surface); color: var(--ink);
  padding: 16px; width: min(480px, calc(100vw - 24px)); box-shadow: var(--shadow);
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.35); }
@media (max-width: 759px) {
  .sheet { margin: auto 0 0; width: 100vw; max-width: 100vw; border-radius: 20px 20px 0 0; padding-bottom: calc(20px + env(safe-area-inset-bottom)); }
}
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.field { display: grid; gap: 8px; margin-bottom: 18px; }
.field-label { font-weight: 600; font-size: 14px; }
.field-help { font-size: 12px; color: var(--muted); display: block; }
.field input[type="password"] { min-height: var(--tap); padding: 0 12px; border: 1px solid var(--border); border-radius: 12px; background: var(--page); }
.switch-row { grid-template-columns: 1fr auto; align-items: center; cursor: pointer; }
.switch { appearance: none; -webkit-appearance: none; width: 44px; height: 26px; border-radius: 999px; background: var(--subtle-2); position: relative; cursor: pointer; margin: 0; transition: background 0.2s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); transition: transform 0.2s; }
.switch:checked { background: var(--strong); }
.switch:checked::after { transform: translateX(18px); background: var(--on-strong); }
input[type="range"] { width: 100%; accent-color: var(--strong); }

/* ---------- przyciski kwestii ---------- */
.group { margin-bottom: 22px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.line-btn {
  min-height: 76px; text-align: left; padding: 12px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--surface); cursor: pointer;
  display: flex; flex-direction: column; gap: 2px; touch-action: manipulation; transition: border-color 0.15s, transform 0.1s;
}
.line-btn:hover { border-color: var(--axis); }
.line-btn:active { transform: scale(0.98); }
.line-btn.playing { border-color: var(--strong); box-shadow: 0 0 0 1px var(--strong); }
.line-btn .lc { font-weight: 700; font-size: 14px; }
.line-btn .ll { color: var(--ink-2); font-size: 12px; }
.line-btn .ls { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; margin-top: auto; }

/* ---------- koszty ---------- */
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 12px; }
.tile, .card, .sess { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.tile { padding: 12px; }
.tile .tl { font-size: 12px; color: var(--ink-2); }
.tile .tv { font-size: 22px; font-weight: 600; margin-top: 2px; }
.tile .ts { font-size: 11px; color: var(--muted); margin-top: 2px; }
.meter { height: 6px; border-radius: 3px; background: var(--subtle-2); margin-top: 8px; overflow: hidden; }
.meter > span { display: block; height: 100%; border-radius: 3px; background: var(--accent); }
.meter.warn > span { background: #fab219; }
.meter.over > span { background: var(--critical); }
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.card { padding: 14px; margin-bottom: 12px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2); margin-bottom: 6px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; stroke: none; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; stroke: none; }
.chart .hit { fill: transparent; cursor: pointer; }
.chart .hit:hover, .chart .hit.active { fill: var(--subtle); }
.chart path, .chart rect:not(.hit), .chart text { pointer-events: none; }
.tooltip {
  position: fixed; z-index: 30; pointer-events: none; background: var(--surface); color: var(--ink);
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; font-size: 12px;
  box-shadow: var(--shadow); min-width: 150px; font-variant-numeric: tabular-nums;
}
.tooltip .row { display: flex; justify-content: space-between; gap: 12px; }
.tooltip i { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 6px; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: 12px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.wrap { white-space: normal; min-width: 160px; }
.note { color: var(--muted); font-size: 12px; }

/* ---------- historia rozmow ---------- */
.sess-list { display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); margin-top: 12px; }
.sess { padding: 14px; }
button.sess { text-align: left; width: 100%; cursor: pointer; font: inherit; color: inherit; transition: border-color 0.15s; }
button.sess:hover { border-color: var(--axis); }
.sess-sub { color: var(--muted); font-size: 12px; margin-top: 6px; }
.sess-line { font-variant-numeric: tabular-nums; font-size: 14px; }
button.sess .sess-head { margin-bottom: 4px; }
.back-btn { gap: 4px; padding: 0 14px 0 8px; margin-bottom: 12px; min-height: 36px; font-size: 13px; }
.back-btn svg { width: 18px; height: 18px; }
#historyMeta { margin-bottom: 8px; }
.msg.sep { align-self: stretch; text-align: center; color: var(--muted); font-size: 12px; background: transparent; border: 0; border-top: 1px solid var(--border); border-radius: 0; padding: 8px 0 0; margin-top: 6px; max-width: none; }
.sess-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 10px; }
.sess-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; margin: 0; }
.sess-stats dt { font-size: 11px; color: var(--muted); }
.sess-stats dd { margin: 0 0 4px; font-variant-numeric: tabular-nums; font-weight: 500; }

/* ---------- szerokie ekrany ---------- */
@media (min-width: 760px) {
  body { padding-bottom: 0; }
  .tabs {
    position: sticky; top: var(--header-h, 57px); bottom: auto; display: flex; justify-content: center; gap: 4px;
    border-top: 0; border-bottom: 1px solid var(--border); padding: 6px 0;
  }
  .tabs button { height: 36px; flex-direction: row; gap: 8px; font-size: 13px; padding: 0 14px; border-radius: 999px; }
  .tabs button svg { width: 16px; height: 16px; }
  .tabs button[aria-current="page"] { background: var(--subtle); }
  body { display: flex; flex-direction: column; }
  .top { order: 0; } .banner { order: 1; } .tabs { order: 2; } main { order: 3; width: 100%; }
  .tab-chat { min-height: calc(100vh - 120px); min-height: calc(100dvh - 120px); }
  .pill { bottom: 16px; }
  .tiles { grid-template-columns: repeat(4, 1fr); }
}

@media (prefers-reduced-motion: reduce) {
  .orb-core, .orb, .session-pill.live .dot { animation: none !important; }
}
