:root {
  --bg: #f7f7f5;
  --panel: #ecebe7;
  --surface: #ffffff;
  --text: #1c1c1e;
  --muted: #6b6b70;
  --border: #dcdad4;
  --accent: #c2562b;
  --accent-text: #ffffff;
  --user-bubble: #e7e5df;
  --link: #a8461f;
  --sidebar-w: 280px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1b1d;
    --panel: #141416;
    --surface: #262629;
    --text: #ececee;
    --muted: #9a9aa2;
    --border: #333338;
    --accent: #e0754a;
    --accent-text: #1b1b1d;
    --user-bubble: #2d2d31;
    --link: #f0936b;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- logowanie ---------- */
.login { height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(340px, 100%); display: flex; flex-direction: column; gap: 12px; align-items: center; text-align: center; }
.login-card img { border-radius: 16px; }
.login-card h1 { margin: 4px 0 8px; font-size: 22px; }
.login-card input, .login-card button { width: 100%; padding: 12px 14px; border-radius: 12px; font-size: 16px; }
.login-card input { border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.login-card button { border: 0; background: var(--accent); color: var(--accent-text); font-weight: 600; }
.err { color: #d33; margin: 0; }

/* ---------- układ ---------- */
.app { display: flex; height: 100dvh; overflow: hidden; }
.sidebar {
  width: var(--sidebar-w); flex-shrink: 0; background: var(--panel);
  display: flex; flex-direction: column; padding: 12px 10px;
  padding-top: max(12px, env(safe-area-inset-top));
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}
.new-chat {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); font-weight: 500;
}
.conv-list { flex: 1; overflow-y: auto; margin: 12px 0; display: flex; flex-direction: column; gap: 2px; }
.conv {
  display: flex; align-items: center; border-radius: 8px; padding: 0 4px 0 12px; min-height: 40px;
}
.conv:hover, .conv.active { background: var(--user-bubble); }
.conv a { flex: 1; min-width: 0; color: inherit; text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14.5px; padding: 9px 0; }
.conv .del { opacity: .45; border: 0; background: none; padding: 8px; border-radius: 6px; line-height: 0; }
.conv .del:hover { opacity: 1; }
.kb-info { font-size: 12px; color: var(--muted); padding: 0 6px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 4px; padding: 6px 8px; min-height: 52px;
  padding-top: max(6px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
}
.title { flex: 1; text-align: center; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { border: 0; background: none; padding: 8px; border-radius: 8px; line-height: 0; }
.icon-btn:hover { background: var(--user-bubble); }

.scroller { flex: 1; overflow-y: auto; overscroll-behavior: contain; }
.messages { max-width: 780px; margin: 0 auto; padding: 16px 16px 24px; }

/* ---------- wiadomości ---------- */
.msg { margin: 18px 0; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble); padding: 10px 16px; border-radius: 20px 20px 6px 20px;
  max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.assistant .body { overflow-wrap: anywhere; }
.body p { margin: 0 0 .8em; }
.body h1, .body h2, .body h3 { line-height: 1.3; margin: 1.2em 0 .5em; }
.body h1 { font-size: 1.3em; } .body h2 { font-size: 1.18em; } .body h3 { font-size: 1.05em; }
.body ul, .body ol { padding-left: 1.4em; margin: 0 0 .8em; }
.body li { margin: .25em 0; }
.body a { color: var(--link); text-decoration: none; border-bottom: 1px solid color-mix(in srgb, var(--link) 35%, transparent); }
.body blockquote { margin: .8em 0; padding: .2em 0 .2em 1em; border-left: 3px solid var(--accent); color: var(--muted); }
.body code { background: var(--user-bubble); padding: .1em .35em; border-radius: 4px; font-size: .9em; }
.body table { border-collapse: collapse; display: block; overflow-x: auto; }
.body th, .body td { border: 1px solid var(--border); padding: 6px 10px; }
.err-box { color: #d33; font-size: 14px; }

/* źródła nad odpowiedzią + przypisy w tekście */
.sources {
  display: flex; gap: 10px; overflow-x: auto; margin: 0 -16px 14px; padding: 2px 16px 6px;
  scroll-snap-type: x proximity; scrollbar-width: thin;
}
.src-card {
  flex: 0 0 196px; scroll-snap-align: start; background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px; overflow: hidden; display: flex; flex-direction: column;
}
.src-thumb { display: block; aspect-ratio: 16 / 9; background: var(--user-bubble); }
.src-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.src-title {
  font-size: 13px; line-height: 1.35; padding: 8px 10px 4px; font-weight: 500;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.src-ts { display: flex; flex-wrap: wrap; gap: 4px; padding: 4px 10px 10px; margin-top: auto; }
.src-ts a {
  font-size: 12px; color: var(--muted); text-decoration: none; background: var(--user-bubble);
  border-radius: 6px; padding: 1px 6px;
}
.src-ts a b { color: var(--text); font-weight: 600; margin-right: 2px; }
.body a.cite {
  display: inline-flex; align-items: center; justify-content: center; min-width: 18px; height: 18px;
  padding: 0 5px; margin: 0 1px; border-radius: 9px; border: 0; vertical-align: 2px;
  font-size: 11px; font-weight: 600; line-height: 1; background: var(--user-bubble); color: var(--text);
}
.body a.cite:hover { background: var(--accent); color: var(--accent-text); }

.tools { margin-bottom: 10px; font-size: 13.5px; color: var(--muted); }
.tools summary { cursor: pointer; list-style: none; display: inline-flex; align-items: center; gap: 6px; }
.tools summary::-webkit-details-marker { display: none; }
.tools ul { margin: 6px 0 0; padding-left: 1.2em; }
.spinner { width: 12px; height: 12px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.cursor::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--muted); }
@keyframes blink { 50% { opacity: 0; } }

/* ---------- ekran startowy ---------- */
.empty { max-width: 780px; margin: 0 auto; padding: 12vh 16px 16px; text-align: center; }
.empty h2 { font-size: 24px; font-weight: 600; margin: 0 0 24px; }
.suggestions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.suggestions button {
  text-align: left; padding: 12px 14px; border-radius: 14px; border: 1px solid var(--border);
  background: var(--surface); font-size: 14.5px; line-height: 1.4;
}
.suggestions button:hover { border-color: var(--accent); }

/* ---------- pole wpisywania ---------- */
.composer { padding: 8px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
.composer-box {
  max-width: 780px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 24px; padding: 6px 6px 6px 16px;
}
.composer-box:focus-within { border-color: color-mix(in srgb, var(--accent) 60%, var(--border)); }
textarea {
  flex: 1; border: 0; outline: 0; resize: none; background: transparent; color: var(--text);
  font: inherit; font-size: 16px; /* 16px = brak auto-zoomu na iOS */
  max-height: 180px; padding: 7px 0; line-height: 1.45;
}
.send {
  width: 38px; height: 38px; border-radius: 50%; border: 0; flex-shrink: 0;
  background: var(--accent); color: var(--accent-text); display: grid; place-items: center;
}
.send:disabled { opacity: .3; cursor: default; }

/* ---------- pamięć wniosków ---------- */
.insight {
  margin: 14px 0 4px; padding: 12px 14px; border-radius: 14px;
  border: 1px solid color-mix(in srgb, var(--accent) 45%, var(--border));
  background: color-mix(in srgb, var(--accent) 7%, var(--surface));
}
.insight[data-status="rejected"] { opacity: .55; border-color: var(--border); background: var(--surface); }
.insight-head { font-size: 12.5px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.insight-title { font-weight: 600; margin-bottom: 4px; }
.insight-text { font-size: 14.5px; }
.insight-meta { font-size: 12px; color: var(--muted); margin-top: 6px; }
.insight-actions { display: flex; gap: 8px; margin-top: 10px; }
.insight-actions button, .insight-actions a {
  padding: 7px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface);
  font-size: 14px; text-decoration: none; color: var(--text);
}
.insight-actions .save { background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; }
.muted { color: var(--muted); }

/* ---------- mapa pojęć ---------- */
:root {
  --series-1: #2a78d6; --series-2: #eb6834; --series-3: #1baf7a; --series-4: #eda100; --series-5: #e87ba4;
  --edge: #c3c2b7;
}
@media (prefers-color-scheme: dark) {
  :root { --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500; --series-5: #d55181; --edge: #4a4a46; }
}
.side-link {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px; margin-top: 6px;
  border: 0; background: none; border-radius: 10px; text-align: left;
}
.side-link:hover { background: var(--user-bubble); }
.main { position: relative; }
.map { position: absolute; inset: 0; z-index: 5; background: var(--bg); display: flex; flex-direction: column; }
.map-search { flex: 1; min-width: 0; }
.map-search input {
  width: 100%; padding: 8px 14px; border-radius: 18px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: 16px;
}
.map-canvas { flex: 1; min-height: 0; }
.map-empty { position: absolute; inset: 40% 16px auto; text-align: center; color: var(--muted); }
.map-legend {
  position: absolute; top: calc(max(6px, env(safe-area-inset-top)) + 56px); left: 10px;
  display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12.5px; color: var(--muted);
  background: color-mix(in srgb, var(--bg) 85%, transparent); padding: 6px 10px; border-radius: 10px; max-width: calc(100% - 20px);
}
.map-legend span { display: inline-flex; align-items: center; gap: 5px; }
.map-legend i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.card {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 58%; overflow-y: auto;
  background: var(--surface); border-top: 1px solid var(--border); border-radius: 18px 18px 0 0;
  padding: 16px 18px; padding-bottom: max(16px, env(safe-area-inset-bottom));
  box-shadow: 0 -8px 30px rgba(0,0,0,.12);
}
@media (min-width: 861px) {
  .card { left: auto; top: 70px; right: 14px; bottom: 14px; width: 380px; max-height: none; border-radius: 16px; border: 1px solid var(--border); }
}
.card-close { position: absolute; top: 8px; right: 8px; }
.card h3 { margin: 0 32px 2px 0; font-size: 19px; }
.card .meta { color: var(--muted); font-size: 13px; margin-bottom: 10px; }
.card h4 { margin: 14px 0 6px; font-size: 14px; color: var(--muted); font-weight: 600; }
.card p, .card li { font-size: 14.5px; }
.card ul { padding-left: 1.1em; margin: 0; }
.card li { margin: 5px 0; }
.card a { color: var(--link); text-decoration: none; }
.card .rel { font-weight: 600; }
.card .concept-link { color: var(--text); border-bottom: 1px dashed var(--muted); cursor: pointer; }
.card .dossier summary { cursor: pointer; font-weight: 600; color: var(--muted); font-size: 14px; margin: 6px 0; }
.card .dossier .body { font-size: 14.5px; }
.card .dossier .body h2 { font-size: 15.5px; margin: 1em 0 .4em; }
.card .ask {
  width: 100%; margin-top: 14px; padding: 11px; border-radius: 12px; border: 0;
  background: var(--accent); color: var(--accent-text); font-weight: 600;
}

/* ---------- telefon ---------- */
.scrim { display: none; }
@media (max-width: 860px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 20; width: min(84vw, 320px);
    transform: translateX(-100%); transition: transform .22s ease;
  }
  .app.drawer-open .sidebar { transform: none; box-shadow: 0 0 30px rgba(0,0,0,.2); }
  .app.drawer-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10; }
  .suggestions { grid-template-columns: 1fr; }
  .empty { padding-top: 6vh; }
  .conv .del { opacity: .6; }
}
@media (min-width: 861px) {
  #menu { visibility: hidden; }
  #new-chat-top { visibility: hidden; }
}
