:root {
  --bg: #f6f5fb;
  --panel: #ffffff;
  --side: #eeecf6;
  --text: #1c1b26;
  --muted: #6e6b80;
  --line: #dddaea;
  --accent: #6d5ef5;
  --accent-ink: #ffffff;
  --mine: #6d5ef5;
  --mine-ink: #ffffff;
  --theirs: #ffffff;
  --theirs-ink: #1c1b26;
  --danger: #d1394f;
  --ok: #1f9d6b;
  --shadow: 0 1px 2px rgba(20, 16, 50, .06), 0 4px 16px rgba(20, 16, 50, .06);
  --radius: 18px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14131c;
    --panel: #1c1b27;
    --side: #191823;
    --text: #ecebf5;
    --muted: #9a97ad;
    --line: #2e2c3d;
    --accent: #8b7ef9;
    --mine: #6556e8;
    --theirs: #232232;
    --theirs-ink: #ecebf5;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

.shell { display: flex; height: 100dvh; }

/* ---------- lista rozmów ---------- */
.sidebar {
  width: 290px; flex: none; display: flex; flex-direction: column;
  background: var(--side); border-right: 1px solid var(--line);
  padding: calc(12px + var(--safe-t)) 12px calc(12px + var(--safe-b));
}
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 4px 12px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; }
.brand img { width: 30px; height: 30px; border-radius: 9px; }
.new-btn {
  display: flex; align-items: center; gap: 8px; justify-content: center;
  border: 1px dashed var(--line); background: transparent; border-radius: 12px; padding: 10px; margin-bottom: 10px;
}
.new-btn:hover { border-color: var(--accent); color: var(--accent); }
.conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.conv {
  text-align: left; border: 0; background: transparent; border-radius: 10px; padding: 9px 11px;
  display: flex; flex-direction: column; gap: 1px;
}
.conv:hover { background: color-mix(in srgb, var(--accent) 8%, transparent); }
.conv.active { background: color-mix(in srgb, var(--accent) 16%, transparent); }
.conv b { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv small { color: var(--muted); font-size: 12px; }
.conv .dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); margin-left: 6px; vertical-align: middle; }
.empty-list { color: var(--muted); font-size: 14px; padding: 10px; }
.side-foot { border-top: 1px solid var(--line); padding-top: 8px; }
.link-btn { display: flex; align-items: center; gap: 8px; border: 0; background: transparent; padding: 8px; border-radius: 10px; width: 100%; color: var(--muted); }
.link-btn:hover { color: var(--text); }
.scrim { display: none; }

/* ---------- czat ---------- */
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.topbar {
  display: flex; align-items: center; gap: 6px; position: relative;
  padding: calc(8px + var(--safe-t)) 10px 8px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.title-btn {
  flex: 1; min-width: 0; border: 0; background: transparent; text-align: left; font-weight: 650; font-size: 17px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 6px;
}
.icon-btn { border: 0; background: transparent; width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; }
.icon-btn:hover { background: color-mix(in srgb, var(--text) 7%, transparent); }
.conn { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.conn.on { background: var(--ok); }
.menu {
  position: absolute; right: 10px; top: calc(100% - 4px); z-index: 20; background: var(--panel);
  border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); padding: 4px; display: flex; flex-direction: column; min-width: 180px;
}
.menu button { border: 0; background: transparent; text-align: left; padding: 10px 12px; border-radius: 8px; }
.menu button:hover { background: color-mix(in srgb, var(--text) 7%, transparent); }
.danger { color: var(--danger) !important; }

.messages { flex: 1; overflow-y: auto; padding: 18px 14px 8px; display: flex; flex-direction: column; gap: 4px; overscroll-behavior: contain; }
.messages > * { max-width: 760px; width: 100%; margin-inline: auto; }
.welcome { margin: auto; text-align: center; color: var(--muted); padding: 30px 10px; }
.welcome img { width: 72px; height: 72px; border-radius: 20px; margin-bottom: 10px; }
.welcome h1 { color: var(--text); font-size: 22px; margin: 0 0 6px; }

.msg { display: flex; flex-direction: column; margin-top: 8px; }
.msg.user { align-items: flex-end; }
.msg.assistant { align-items: flex-start; }
.bubble {
  max-width: min(85%, 620px); padding: 10px 14px; border-radius: var(--radius); box-shadow: var(--shadow);
  overflow-wrap: anywhere;
}
.user .bubble { background: var(--mine); color: var(--mine-ink); border-bottom-right-radius: 6px; }
.assistant .bubble { background: var(--theirs); color: var(--theirs-ink); border-bottom-left-radius: 6px; }
.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 4px 0 8px; padding-left: 22px; }
.bubble pre { background: color-mix(in srgb, var(--text) 8%, transparent); padding: 10px; border-radius: 10px; overflow-x: auto; font-size: 13.5px; margin: 6px 0; }
.bubble code { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: .92em; }
.bubble :not(pre) > code { background: color-mix(in srgb, var(--text) 10%, transparent); padding: 1px 5px; border-radius: 5px; }
.user .bubble :not(pre) > code { background: rgba(255, 255, 255, .2); }
.bubble a { color: inherit; }
.voice-tag { display: flex; align-items: center; gap: 6px; font-size: 13px; opacity: .85; margin-bottom: 4px; }
.voice-tag svg { width: 15px; height: 15px; }
.placeholder { font-style: italic; opacity: .8; }

.meta { font-size: 12px; color: var(--muted); margin: 4px 6px 0; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.meta .err { color: var(--danger); }
.meta button { border: 0; background: transparent; color: var(--accent); padding: 0; font-size: 12px; font-weight: 600; }

.play {
  display: inline-flex; align-items: center; gap: 8px; border: 1px solid var(--line); background: var(--panel);
  border-radius: 999px; padding: 4px 12px 4px 4px; font-size: 13px; color: var(--text);
}
.user .play { background: rgba(255, 255, 255, .16); border-color: rgba(255, 255, 255, .3); color: var(--mine-ink); margin-top: 6px; }
.play .knob { width: 28px; height: 28px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; }
.user .play .knob { background: #fff; color: var(--mine); }
.play .knob svg { width: 14px; height: 14px; fill: currentColor; stroke: none; }
.play .bar { width: 90px; height: 4px; border-radius: 4px; background: color-mix(in srgb, currentColor 22%, transparent); overflow: hidden; }
.play .bar i { display: block; height: 100%; width: calc(var(--p, 0) * 100%); background: currentColor; }
.assistant .play { margin: 6px 0 0 2px; }

.typing { display: inline-flex; gap: 4px; padding: 14px 16px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite both; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }
.spinner { width: 12px; height: 12px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; display: inline-block; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- pole wiadomości ---------- */
.composer { padding: 8px 12px calc(10px + var(--safe-b)); background: var(--bg); }
.compose-row {
  max-width: 760px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 26px; padding: 6px 6px 6px 16px; box-shadow: var(--shadow);
}
.compose-row:focus-within { border-color: color-mix(in srgb, var(--accent) 60%, var(--line)); }
textarea {
  flex: 1; border: 0; outline: 0; resize: none; background: transparent; color: var(--text);
  font: inherit; padding: 9px 0; max-height: 40vh; min-height: 40px;
}
.round-btn {
  width: 42px; height: 42px; border-radius: 50%; border: 0; display: grid; place-items: center; flex: none;
  background: color-mix(in srgb, var(--text) 8%, transparent);
}
.round-btn.primary { background: var(--accent); color: var(--accent-ink); }
.round-btn:disabled { opacity: .5; cursor: default; }
.recording { align-items: center; padding-left: 6px; }
.rec-info { flex: 1; display: flex; align-items: center; gap: 10px; min-width: 0; font-variant-numeric: tabular-nums; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; flex: none; }
@keyframes pulse { 50% { opacity: .3; } }
#recWave { flex: 1; min-width: 0; width: 100%; height: 36px; color: var(--accent); }

/* ---------- ustawienia, komunikaty ---------- */
dialog { border: 0; padding: 0; border-radius: 20px; background: var(--panel); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, .3); width: min(420px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(10, 8, 25, .45); }
.sheet { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.sheet h2 { margin: 0; font-size: 20px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
select { font: inherit; padding: 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }
.check { display: flex; gap: 10px; align-items: center; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.note { font-size: 13px; color: var(--muted); margin: 0; }
.sheet-actions { display: flex; justify-content: space-between; gap: 10px; }
.sheet-actions button { border: 0; padding: 10px 18px; border-radius: 12px; }
.sheet-actions .primary { background: var(--accent); color: var(--accent-ink); }
.sheet-actions .ghost { background: transparent; color: var(--danger); }
.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; max-width: calc(100vw - 32px);
}

.only-narrow { display: none; }
@media (max-width: 860px) {
  .only-narrow { display: grid; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 40; width: min(300px, 84vw);
    transform: translateX(-102%); transition: transform .22s ease; box-shadow: var(--shadow);
  }
  .shell.side-open .sidebar { transform: none; }
  .shell.side-open .scrim { display: block; position: fixed; inset: 0; z-index: 30; background: rgba(10, 8, 25, .4); }
  .bubble { max-width: 88%; }
}
@media (min-width: 861px) { #closeSide { display: none; } }
