:root {
  --bg: #f6f9ff;
  --panel: #ffffff;
  --sidebar: #eaf1fe;
  --text: #1b2437;
  --muted: #5d6b85;
  --border: #d6e0f2;
  --accent: #2f6df6;
  --accent-text: #ffffff;
  --user: #dbe8ff;
  --code: #eef3fc;
  --error: #d93a3a;
  --radius: 10px;
  color-scheme: light;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* Motyw: bez wyboru zgodnie z systemem; przełącznik w menu konta ustawia data-theme (theme.js). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151c;
    --panel: #1a1f2a;
    --sidebar: #0f1218;
    --text: #e8edf7;
    --muted: #98a5bd;
    --border: #2b3447;
    --accent: #6c9bff;
    --accent-text: #0b1226;
    --user: #24304a;
    --code: #212838;
    --error: #ff8a80;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
    --bg: #12151c;
    --panel: #1a1f2a;
    --sidebar: #0f1218;
    --text: #e8edf7;
    --muted: #98a5bd;
    --border: #2b3447;
    --accent: #6c9bff;
    --accent-text: #0b1226;
    --user: #24304a;
    --code: #212838;
    --error: #ff8a80;
    color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  display: flex;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.55;
}

button { font: inherit; cursor: pointer; }
.muted { color: var(--muted); font-size: 13px; }

.primary {
  background: var(--accent);
  color: var(--accent-text);
  border: 0;
  border-radius: var(--radius);
  padding: 9px 16px;
  font-weight: 600;
}
.primary:disabled { opacity: .5; cursor: wait; }

.link {
  background: none;
  border: 0;
  padding: 0;
  color: var(--accent);
  text-decoration: underline;
}

.icon { background: none; border: 0; font-size: 20px; color: var(--text); display: none; }

/* Elementy tylko dla telefonu (patrz @media niżej); na komputerze nie zmieniają układu. */
#sidebar-backdrop { display: none; }
.sidebar-head { display: flex; align-items: center; gap: 8px; }
.sidebar-head .primary { flex: 1; }
#close-sidebar { display: none; }
.topbar-tools, .tool-row { display: contents; }
.tool-label { display: none; }

/* --- pasek boczny ---------------------------------------------------------------------------- */

#sidebar {
  width: 290px;
  flex-shrink: 0;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px;
  gap: 12px;
}

#conversations { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }

.conv {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 9px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
}
.conv:hover { background: var(--border); }
.conv.active { background: var(--panel); box-shadow: 0 0 0 1px var(--border); }
.conv .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.conv .cost { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.conv .delete { visibility: hidden; background: none; border: 0; color: var(--muted); padding: 0 2px; }
.conv:hover .delete { visibility: visible; }

#account { position: relative; border-top: 1px solid var(--border); padding-top: 10px; }
.account-btn {
  display: flex; align-items: center; gap: 10px; width: 100%;
  font: inherit; color: var(--text); background: none; border: 0; border-radius: 10px;
  padding: 8px 10px; cursor: pointer; text-align: left;
}
.account-btn:hover, .account-btn[aria-expanded="true"] { background: var(--border); }
.account-btn .avatar {
  flex: 0 0 auto; width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--accent); color: var(--accent-text); font-weight: 700;
}
.account-btn .account-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.account-btn .caret { color: var(--muted); }
.menu {
  position: absolute; left: 0; right: 0; bottom: calc(100% - 4px); z-index: 5;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, .25); padding: 6px;
  display: flex; flex-direction: column;
}
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 4px 2px; }
.menu-item {
  display: block; width: 100%; box-sizing: border-box; font: inherit; color: var(--text); text-decoration: none;
  background: none; border: 0; border-radius: 8px; padding: 9px 12px; text-align: left; cursor: pointer;
}
.menu-item:hover { background: var(--border); }
.menu-item.danger { color: var(--error); }

/* Przyciski paska bocznego i okien */
.side-btn {
  display: block;
  width: 100%;
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-bottom-width: 3px;
  border-radius: 8px;
  padding: 8px 12px;
  text-align: center;
  cursor: pointer;
}
.side-btn:hover { border-color: var(--accent); color: var(--accent); }
.side-btn:active { transform: translateY(1px); border-bottom-width: 2px; }

dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 22px 24px;
  width: min(380px, calc(100vw - 32px));
  box-shadow: 0 10px 40px rgba(0, 0, 0, .35);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog:focus { outline: none; }
dialog h2 { margin: 0 0 14px; font-size: 18px; }
dialog dl { display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; margin: 0 0 12px; }
dialog dt { color: var(--muted); }
dialog dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; }
dialog label, .login-card label { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; font-size: 14px; color: var(--muted); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.modal-actions .side-btn { width: auto; }

/* --- logowanie ------------------------------------------------------------------------------- */

#login { display: none; flex: 1; align-items: center; justify-content: center; padding: 20px; }
body.login-mode { display: block; }
body.login-mode #login { display: flex; min-height: 100vh; }
body.login-mode > aside, body.login-mode > main { display: none; }
.login-card {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px 26px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .12);
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card .muted { margin: 0 0 18px; }
.login-card .primary { width: 100%; padding: 10px; }
input[type="text"], input[type="password"] {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: 9px 10px;
}
.login-error { color: var(--error); font-size: 14px; margin: 0 0 12px; }

/* --- administracja --------------------------------------------------------------------------- */

body.admin-page { display: block; height: auto; min-height: 100vh; }
.admin-top { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px max(20px, calc((100% - 960px) / 2));
             border-bottom: 1px solid var(--border); background: var(--sidebar); }
.admin-top h1 { margin: 0; font-size: 20px; }
.admin-top-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.side-btn.back { display: inline-block; width: auto; text-decoration: none; }
#admin { max-width: 960px; margin: 0 auto; padding: 24px 20px 48px; }
.admin-table { width: 100%; border-collapse: collapse; margin: 0 0 22px; font-size: 14px; }
.admin-table th, .admin-table td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--border); }
.admin-table th { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
.admin-table td.num, .admin-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.admin-table .actions { display: flex; gap: 6px; justify-content: flex-end; }
.admin-table .side-btn { width: auto; padding: 4px 10px; font-size: 13px; }
.admin-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: end; margin: 0 0 22px; padding: 14px;
              background: var(--panel); border: 1px solid var(--border); border-radius: 10px; }
.admin-form label { margin: 0; }
.admin-form .check { flex-direction: row; align-items: center; gap: 6px; }
.admin-msg { color: var(--error); font-size: 14px; margin: 0 0 12px; }
.admin-body h3 { margin: 18px 0 6px; font-size: 15px; }
.admin-bar { display: flex; flex-wrap: wrap; gap: 12px 20px; align-items: center; margin: 0 0 14px; }
.readonly-banner { background: color-mix(in srgb, var(--accent) 12%, var(--panel)); border: 1px solid var(--accent);
                   border-radius: 8px; padding: 10px 14px; margin: 12px 0 18px; font-size: 14px; }
.readonly-messages .msg { margin-bottom: 18px; }
.admin-table .links { display: inline-flex; gap: 10px; }
.admin-table a { color: var(--accent); }
.status-blocked { color: var(--error); font-weight: 600; }

/* --- główna kolumna -------------------------------------------------------------------------- */

main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
#topbar h1 { font-size: 16px; margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-picker { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.model-picker select {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 8px;
  max-width: 340px;
}

#messages, #library {
  flex: 1;
  overflow-y: auto;
  padding: 24px max(20px, calc((100% - 820px) / 2));
}

.msg { margin-bottom: 22px; }
.msg.user .body {
  background: var(--user);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin-left: 15%;
  white-space: pre-wrap;
}
.msg.assistant .body { overflow-wrap: anywhere; }
.msg.assistant .body :is(h1, h2, h3) { line-height: 1.3; margin: 1.1em 0 .4em; }
.msg.assistant .body h1 { font-size: 20px; }
.msg.assistant .body h2 { font-size: 17px; }
.msg.assistant .body h3 { font-size: 15px; }
.msg.assistant .body table { border-collapse: collapse; margin: .6em 0; }
.msg.assistant .body :is(td, th) { border: 1px solid var(--border); padding: 4px 8px; }
.msg.assistant .body code { background: var(--code); padding: 1px 4px; border-radius: 4px; }
.msg.assistant .body pre { background: var(--code); padding: 10px; border-radius: 8px; overflow-x: auto; }
.msg.assistant .body blockquote { border-left: 3px solid var(--border); margin: .6em 0; padding-left: 12px; color: var(--muted); }
.msg.error .body { color: var(--error); }

.meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 14px;
  margin-top: 8px;
  font-size: 13px;
  color: var(--muted);
}
.meta details summary { cursor: pointer; }
.meta details { flex: 1 1 100%; }
.meta details ul { margin: 4px 0 0; padding-left: 18px; }
.meta details ul.steps, .meta details ul.substeps { padding-left: 0; }
.files {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 14px 0 4px;
  padding: 12px;
  background: color-mix(in srgb, var(--accent) 14%, var(--bg));
  border-radius: calc(var(--radius) + 4px);
}
.files:empty { display: none; }
.proposals { display: flex; flex-direction: column; gap: 10px; margin: 14px 0 4px; }
.proposal-heading { font-size: 11px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--accent); }
.proposal {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  background: var(--panel); border: 2px solid var(--border); border-radius: var(--radius); padding: 12px 16px;
}
.proposal.chosen { border-color: var(--accent); }
.proposal.inactive { opacity: .5; }
.proposal-info { flex: 1 1 220px; min-width: 0; }
.proposal-title { font-weight: 700; font-size: 16px; line-height: 1.3; overflow-wrap: anywhere; }
.proposal-detail, .proposal-topics { font-size: 13px; color: var(--muted); margin-top: 2px; overflow-wrap: anywhere; }
.proposal .btn, .proposal-fresh .btn {
  background: var(--accent); border: 0; border-bottom: 3px solid color-mix(in srgb, var(--accent) 60%, #000);
  border-radius: 8px; padding: 8px 18px; color: var(--accent-text); font-weight: 700; font-size: 14px; line-height: 1.25;
  cursor: pointer; box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
}
.proposal-fresh .btn.secondary { background: var(--panel); color: var(--accent); border: 2px solid var(--accent); box-shadow: none; }
.proposals .btn:disabled { cursor: default; filter: grayscale(.5); opacity: .7; }
.proposal-error { color: #b3261e; font-size: 13px; }
.proposal-error:empty { display: none; }
@media (max-width: 560px) {
  .proposal { padding: 10px; gap: 8px; }
  .proposal .btn { width: 100%; }
}
.file-card {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 14px;
  width: 100%;
  background: var(--panel);
  border: 2px solid var(--accent);
  border-left-width: 8px;
  border-radius: var(--radius);
  padding: 12px 16px;
  flex-wrap: wrap;  /* fiszki mają do pięciu przycisków — na wąskim ekranie schodzą do drugiego wiersza */
}
.file-card .file-icon { font-size: 28px; line-height: 1; flex: 0 0 auto; }
.file-card .file-info { flex: 1 1 auto; min-width: 0; }
.file-card .file-kind { font-size: 11px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--accent); }
.file-card .file-title { font-weight: 700; font-size: 17px; line-height: 1.3; overflow-wrap: anywhere; }
.file-card .file-actions { display: flex; flex: 0 0 auto; flex-wrap: wrap; gap: 8px; margin-left: auto; }
.file-card .btn {
  display: inline-block;
  background: var(--accent);
  border: 0;
  border-bottom: 3px solid color-mix(in srgb, var(--accent) 60%, #000);
  border-radius: 8px;
  padding: 8px 18px;
  color: var(--accent-text);
  text-decoration: none;
  font-weight: 700;
  font-size: 14px;
  line-height: 1.25;  /* ta sama wysokość dla <a> i <button> */
  letter-spacing: .03em;
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
}
.file-card .btn:hover { filter: brightness(1.15); }
.file-card .btn:active { transform: translateY(2px); border-bottom-width: 1px; box-shadow: none; }
@media (max-width: 560px) {
  .file-card { gap: 8px; padding: 10px 10px; }
  .file-card .file-icon { display: none; }
  .file-card .file-title { font-size: 15px; }
  .file-card .btn { padding: 7px 10px; font-size: 13px; }
  .file-card .file-actions { width: 100%; margin-left: 0; }
}
.meta .file {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 10px;
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
}

/* Oś kroków tury: loader na kroku w toku, ✓ przy skończonym, ✕ przy nieudanym. */
.steps.timeline { list-style: none; margin: 0; padding: 0; font-size: 13px; color: var(--muted); }
.meta details .steps.timeline { margin-top: 6px; }
.steps .step { margin: 3px 0; }
.steps .step-row { display: flex; align-items: baseline; gap: 8px; }
.steps .step-text { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.steps .step-time { white-space: nowrap; font-variant-numeric: tabular-nums; opacity: .8; }
.steps .step.running > .step-row .step-text { color: var(--text); font-weight: 600; }
.steps .step.error > .step-row .step-text { color: var(--error); }
.steps .step-note { margin: 1px 0 0 22px; font-size: 12px; opacity: .85; font-style: italic; }
.steps .step-note.error { color: var(--error); font-style: normal; }
.steps .substeps { list-style: none; margin: 2px 0 0 22px; padding: 0; font-size: 12px; }
.step-icon { flex: none; align-self: center; width: 14px; height: 14px; box-sizing: border-box; border-radius: 50%; position: relative; }
.step.running > .step-row > .step-icon, .memory-note.running .step-icon {
  border: 2px solid var(--border); border-top-color: var(--accent); animation: quiz-spin .9s linear infinite;
}
.step.done > .step-row > .step-icon::before, .memory-note:not(.running) .step-icon::before {
  content: "✓"; position: absolute; inset: -2px 0 0; text-align: center; font-size: 12px; line-height: 16px; color: var(--accent);
}
.step.error > .step-row > .step-icon::before { content: "✕"; position: absolute; inset: -2px 0 0; text-align: center; font-size: 12px; line-height: 16px; color: var(--error); }
.memory-note { display: flex; align-items: center; gap: 8px; margin-top: 8px; font-size: 13px; color: var(--muted); }
.memory-note.running { font-style: italic; }
@media (prefers-reduced-motion: reduce) { .step.running > .step-row > .step-icon, .memory-note.running .step-icon { animation-duration: 3s; } }

.empty { text-align: center; margin-top: 8vh; }
.empty h2 { font-size: 22px; margin-bottom: 4px; }
.suggestions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 18px; }
.suggestions button {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 14px;
}
.suggestions button:hover { border-color: var(--accent); }

/* --- biblioteka ------------------------------------------------------------------------------ */

.lib-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.lib-head h2 { margin: 0 0 10px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 12px; flex-wrap: wrap; }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; padding: 8px 12px; color: var(--muted); }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.lib-filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.lib-filters :is(select, input) { font: inherit; padding: 5px 8px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--panel); color: var(--text); max-width: 100%; }
.lib-filters label { display: flex; align-items: center; gap: 6px; font-size: 14px; }
.lib-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 12px; min-height: 0; }
.lib-actions:empty { display: none; }
.lib-actions input { font: inherit; padding: 5px 8px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--panel); color: var(--text); }
.lib-list { list-style: none; padding: 0; margin: 0; }
.lib-list > li { display: flex; gap: 10px; align-items: flex-start; padding: 10px 0; border-bottom: 1px solid var(--border); }
.lib-list .main { flex: 1; min-width: 0; }
.lib-list .q { font-weight: 600; overflow-wrap: anywhere; }
.lib-list .a { color: var(--muted); overflow-wrap: anywhere; }
.lib-list .tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; font-size: 12px; color: var(--muted); }
.lib-list .links { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 4px; font-size: 14px; }
.lib-list a { color: var(--accent); }
.lib-list .delete { background: none; border: 0; color: var(--muted); font-size: 18px; line-height: 1; padding: 0 4px; }
.lib-list .delete:hover { color: var(--error); }
.badge { display: inline-block; padding: 0 7px; border-radius: 999px; border: 1px solid var(--border); font-size: 12px; }
.badge.sprawdzona { border-color: #2e7d32; color: #2e7d32; }
.badge.poprawiona { border-color: #1565c0; color: #1565c0; }
.badge.uwagi { border-color: var(--error); color: var(--error); }
.epoch > summary { cursor: pointer; font-weight: 600; padding: 8px 0; }
.epoch .lib-list { margin-left: 4px; }

#composer {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  padding: 12px max(20px, calc((100% - 820px) / 2)) 18px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}
#composer textarea {
  flex: 1;
  resize: vertical;
  min-height: 48px;
  max-height: 40vh;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
}
#composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* --- telefon (≤ 760 px): przypięte paski, przewija się tylko wnętrze czatu ------------------------ */

@media (max-width: 760px) {
  html:has(body.chat-page) { overflow: hidden; overscroll-behavior: none; }
  /* --app-h / --app-top ustawia common.js z visualViewport: na iOS klawiatura nie zmienia dvh, a Safari przewija
     stronę, żeby pokazać pole tekstowe — bez tego górny pasek uciekałby poza ekran, a klawiatura zasłaniała pola
     logowania. To samo dotyczy ekranu logowania (karta ma się mieścić nad klawiaturą) i okien (dialog niżej).
     Bez skryptu zostaje 100dvh. */
  body.chat-page {
    position: fixed; top: var(--app-top, 0px); left: 0; width: 100%;
    height: var(--app-h, 100dvh); overflow: hidden; overscroll-behavior: none;
  }
  body.login-mode { overflow-y: auto; }  /* niski ekran (np. poziomo z klawiaturą): logowanie można przewinąć */
  body.login-mode #login { min-height: 100%; }
  main { height: 100%; }

  .icon { display: block; min-width: 44px; min-height: 44px; font-size: 22px; padding: 0; }
  button, .menu-item, .side-btn, select { min-height: 44px; }
  .theme-switch, .link, .conv .tag { min-height: 0; }

  #topbar {
    position: relative; flex-wrap: nowrap; gap: 6px; min-height: 52px;
    padding: calc(4px + env(safe-area-inset-top)) calc(4px + env(safe-area-inset-right)) 4px calc(4px + env(safe-area-inset-left));
  }
  #topbar h1 { min-width: 0; }
  #subject-badge { flex: 0 0 auto; }
  #toggle-sidebar { flex: 0 0 auto; }
  #topbar-more { flex: 0 0 auto; }
  .topbar-tools {
    display: none; position: absolute; z-index: 6; top: 100%; right: 8px; width: min(320px, calc(100vw - 16px));
    flex-direction: column; align-items: stretch; gap: 10px; padding: 12px;
    background: var(--panel); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 8px 28px rgba(0, 0, 0, .25);
  }
  #topbar.menu-open .topbar-tools { display: flex; }
  .tool-row { display: flex; align-items: center; justify-content: space-between; min-height: 44px; }
  .tool-label { display: inline; }
  .topbar-tools .link { text-align: left; min-height: 44px; font-size: 15px; }
  .model-picker { flex-direction: column; align-items: stretch; gap: 4px; }
  .model-picker select { max-width: none; width: 100%; font-size: 16px; }
  select, input, textarea, #composer textarea, input[type="text"], input[type="password"] { font-size: 16px; }
  .admin-table { display: block; overflow-x: auto; max-width: 100%; }
  .admin-form { flex-direction: column; align-items: stretch; }
  .admin-form label { display: flex; flex-direction: column; gap: 4px; }
  .admin-form .check { flex-direction: row; min-height: 44px; }
  .admin-form input[type="checkbox"] { width: 22px; height: 22px; }
  #admin { padding-left: 14px; padding-right: 14px; }

  #messages, #library { padding: 16px calc(14px + env(safe-area-inset-right)) 16px calc(14px + env(safe-area-inset-left)); min-height: 0; overscroll-behavior: contain; }
  #composer {
    gap: 8px; align-items: flex-end;
    padding: 8px calc(10px + env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) calc(10px + env(safe-area-inset-left));
  }
  #composer textarea { resize: none; min-height: 44px; height: 44px; max-height: 30vh; padding: 10px 12px; }
  #send { min-height: 44px; min-width: 64px; }
  .msg.user .body { margin-left: 8%; }

  #sidebar {
    position: fixed; top: var(--app-top, 0px); left: 0; height: var(--app-h, 100dvh); width: min(320px, 86vw); z-index: 10;
    transform: translateX(-100%); visibility: hidden; transition: transform .2s, visibility 0s .2s;
    padding-top: calc(14px + env(safe-area-inset-top)); padding-bottom: calc(14px + env(safe-area-inset-bottom));
  }
  body.sidebar-open #sidebar { transform: none; visibility: visible; transition: transform .2s, visibility 0s; box-shadow: 0 0 30px rgba(0, 0, 0, .3); }
  #sidebar-backdrop { position: fixed; inset: 0; z-index: 9; background: rgba(0, 0, 0, .45); }
  body.sidebar-open #sidebar-backdrop { display: block; }
  #close-sidebar { display: block; flex: 0 0 auto; }
  .conv { min-height: 44px; }
  .conv .delete { visibility: visible; min-width: 44px; min-height: 44px; font-size: 20px; }
  .menu-item { padding: 12px; }
  .tabs { overflow-x: auto; flex-wrap: nowrap; }
  .tabs button { white-space: nowrap; min-height: 44px; }
  /* Okna przy górze widocznego obszaru (nie na środku pełnego ekranu), żeby klawiatura nie zasłaniała pól. */
  dialog { max-width: calc(100vw - 24px); margin-top: calc(var(--app-top, 0px) + 12px); margin-bottom: auto;
           max-height: calc(var(--app-h, 100dvh) - 24px); overflow-y: auto; }
}

/* wybór przedmiotu */
.tiles { display: flex; flex-wrap: wrap; justify-content: center; gap: 14px; margin-top: 22px; }
.tile {
  display: flex; flex-direction: column; gap: 6px; align-items: center; justify-content: center;
  width: 190px; min-height: 110px; padding: 16px;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 14px; font-size: 14px;
}
.tile strong { font-size: 20px; }
.tile:hover { box-shadow: 0 0 0 2px var(--border); }
.conv .tag { font-size: 11px; color: var(--muted); border: 1px solid var(--border); border-radius: 6px; padding: 0 4px; }
#subject-badge { white-space: nowrap; }

/* Przełącznik motywu (górny pasek, panel admina): mały suwak ze słońcem i księżycem */
.theme-switch {
  position: relative; flex: 0 0 auto; width: 44px; height: 24px; padding: 0;
  border: 1px solid var(--border); border-radius: 999px; background: var(--code); cursor: pointer;
  font-size: 12px; line-height: 1;
}
.theme-switch::before, .theme-switch::after { position: absolute; top: 5px; opacity: .7; }
.theme-switch::before { content: "☀"; left: 6px; }
.theme-switch::after { content: "☾"; right: 6px; }
.theme-switch .knob {
  position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; border-radius: 50%;
  background: var(--accent); transition: transform .15s; z-index: 1;
}
.theme-switch[aria-checked="true"] .knob { transform: translateX(20px); }
.theme-switch:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* --- test interaktywny (web/quiz.js, web/quizresult.js) --- */
.file-card button.btn { font-family: inherit; cursor: pointer; }  /* rozmiar i grubość z .btn, jak u linków */
.quiz-head h2 { margin: 0 0 4px; font-size: 20px; }
.quiz-counter-label { color: var(--muted); font-size: 14px; margin-bottom: 10px; }
.quiz-dots { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 18px; }
.quiz-dot { width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--border); background: var(--panel); color: var(--muted); font-size: 12px; padding: 0; }
.quiz-dot.done { background: color-mix(in srgb, var(--accent) 22%, var(--panel)); border-color: var(--accent); color: var(--text); }
.quiz-dot.current { border-color: var(--text); color: var(--text); font-weight: 700; }
.quiz-slide { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; margin-bottom: 16px; }
.quiz-question { font-size: 18px; font-weight: 600; line-height: 1.45; margin: 0 0 16px; overflow-wrap: anywhere; }
.quiz-options { display: grid; gap: 10px; }
.quiz-option { display: flex; align-items: center; gap: 12px; text-align: left; padding: 14px 16px; border-radius: var(--radius); border: 2px solid var(--border); background: var(--bg); color: var(--text); font-size: 16px; cursor: pointer; min-height: 52px; }
.quiz-option:hover { border-color: var(--accent); }
.quiz-option.selected { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 16%, var(--panel)); }
.quiz-letter { flex: 0 0 auto; width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; background: var(--accent); color: var(--accent-text); font-weight: 700; font-size: 14px; }
.quiz-tf { justify-content: center; font-weight: 700; }
.quiz-open { width: 100%; box-sizing: border-box; padding: 12px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--bg); color: var(--text); font: inherit; resize: vertical; }
.quiz-counter { color: var(--muted); font-size: 12px; text-align: right; margin-top: 4px; }
.quiz-order { list-style: none; padding: 0; display: grid; gap: 8px; margin: 0; }
.quiz-order li { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg); }
.quiz-order-btns { display: flex; gap: 6px; }
.quiz-order-btns .side-btn { width: auto; padding: 4px 12px; }
.quiz-match { display: grid; gap: 10px; }
.quiz-match-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: center; margin: 0; color: var(--text); font-size: 16px; }
.quiz-match-row select { padding: 10px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--bg); color: var(--text); font: inherit; }
.quiz-nav { display: flex; justify-content: space-between; gap: 10px; margin-top: 8px; }
.quiz-nav .side-btn, .quiz-nav .primary { width: auto; min-width: 130px; padding: 10px 16px; }
.quiz-finish { margin-top: 12px; padding: 12px 24px; }
.quiz-missing { list-style: none; padding: 0; margin: 0 0 8px; }
.quiz-missing .link { text-align: left; }
.quiz-wait { display: grid; place-items: center; gap: 12px; padding: 60px 0; color: var(--muted); }
.quiz-spinner { width: 36px; height: 36px; border-radius: 50%; border: 4px solid var(--border); border-top-color: var(--accent); animation: quiz-spin 1s linear infinite; }
@keyframes quiz-spin { to { transform: rotate(360deg); } }
.quiz-warn { background: color-mix(in srgb, #d99a00 18%, var(--panel)); border: 1px solid #d99a00; border-radius: var(--radius); padding: 10px 12px; }
.quiz-warn .side-btn { width: auto; margin-left: 8px; }
.quiz-score { display: flex; align-items: baseline; gap: 14px; margin: 6px 0 18px; }
.quiz-score-points { font-size: 40px; font-weight: 800; font-variant-numeric: tabular-nums; }
.quiz-score-percent { font-size: 22px; color: var(--muted); }
.quiz-answers { list-style: none; padding: 0; margin: 0 0 16px; display: grid; gap: 8px; }
.quiz-row { border: 1px solid var(--border); border-left-width: 6px; border-radius: var(--radius); background: var(--panel); }
.quiz-row.ok { border-left-color: #1f9d55; background: color-mix(in srgb, #1f9d55 12%, var(--panel)); }
.quiz-row.part { border-left-color: #d99a00; background: color-mix(in srgb, #d99a00 14%, var(--panel)); }
.quiz-row.bad { border-left-color: var(--error); background: color-mix(in srgb, #d93a3a 12%, var(--panel)); }
.quiz-row summary { display: flex; gap: 10px; padding: 10px 12px; cursor: pointer; align-items: baseline; }
.quiz-row-nr { font-weight: 700; }
.quiz-row-text { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.quiz-row-pts { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.quiz-detail { padding: 0 14px 12px; }
.quiz-detail p { margin: 6px 0; overflow-wrap: anywhere; }
.quiz-note { color: var(--muted); font-style: italic; }
.lib-list .side-btn { width: auto; }
@media (max-width: 560px) {
  .quiz-slide { padding: 14px; }
  .quiz-match-row { grid-template-columns: 1fr; gap: 4px; }
  .quiz-nav .side-btn, .quiz-nav .primary { min-width: 0; flex: 1; }
}

/* --- karta testu (web/test.html): okno wypełnione jak slajd prezentacji --- */
body.quiz-page { display: block; height: auto; min-height: 100vh; }
.quiz-page #quiz { box-sizing: border-box; min-height: 100vh; display: flex; flex-direction: column;
  padding: 0 max(20px, calc((100% - 1100px) / 2)); }
.quiz-theme { position: fixed; top: 12px; right: 16px; z-index: 3; }
/* „Wróć” tylko w zainstalowanej aplikacji (bez paska przeglądarki i kart); w karcie zwykłej przeglądarki karta po prostu się zamyka. */
.quiz-back { display: none; position: fixed; top: 8px; left: 12px; z-index: 3; width: auto; }
.standalone .quiz-back { display: inline-block; }
.standalone .quiz-page #quiz { padding-top: 58px; }
.quiz-page .quiz-head { position: sticky; top: 0; z-index: 2; background: var(--bg); padding: 14px 64px 4px 0; }
.quiz-progress { height: 6px; border-radius: 3px; background: var(--border); overflow: hidden; margin: 0 0 12px; }
.quiz-progress > div { height: 100%; background: var(--accent); transition: width .3s; }
.quiz-title-row { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.quiz-title-row h2 { margin: 0; }
.quiz-page .quiz-slide { flex: 1; display: flex; flex-direction: column; justify-content: center;
  padding: clamp(20px, 4vw, 56px); margin: 8px 0 12px; }
.quiz-page .quiz-question { font-size: clamp(22px, 3.2vw, 38px); line-height: 1.35; margin-bottom: clamp(18px, 3vw, 36px); }
.quiz-page .quiz-option { font-size: clamp(17px, 2vw, 24px); min-height: 64px; padding: 16px 20px; }
.quiz-page .quiz-letter { width: 36px; height: 36px; font-size: 18px; }
.quiz-page .quiz-open { font-size: clamp(16px, 1.8vw, 22px); }
.quiz-page .quiz-order li, .quiz-page .quiz-match-row { font-size: clamp(16px, 1.9vw, 22px); }
.quiz-page .quiz-nav { position: sticky; bottom: 0; z-index: 2; background: var(--bg); padding: 12px 0 18px; margin: 0; align-items: center; }
.quiz-page .quiz-nav .side-btn, .quiz-page .quiz-nav .primary { font-size: 17px; padding: 12px 22px; text-decoration: none; text-align: center; }
.quiz-page .quiz-result, .quiz-page .quiz-warn { margin-top: 12px; }

/* --- panel „Moja nauka” (web/nauka.html) --- */
.study-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 20px; }
.study-tile { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.study-tile-value { font-size: 30px; font-weight: 800; font-variant-numeric: tabular-nums; }
.study-tile-label { color: var(--muted); font-size: 13px; margin-bottom: 4px; }
.study-review .primary { width: auto; margin-top: 6px; padding: 6px 14px; }
.lib-list .side-btn, .lib-list .study-go { width: auto; flex: 0 0 auto; text-decoration: none; padding: 6px 14px; }
.study-bars { display: grid; gap: 8px; margin: 8px 0 22px; }
.study-bar-row { display: grid; grid-template-columns: minmax(120px, 220px) 1fr 48px; gap: 10px; align-items: center; font-size: 14px; }
.study-bar { height: 14px; border-radius: 7px; background: var(--border); overflow: hidden; }
.study-bar > div { height: 100%; background: var(--accent); }
.study-bar-value { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
@media (max-width: 600px) { .study-bar-row { grid-template-columns: 1fr 48px; } .study-bar-label { grid-column: 1 / -1; } .study-bar { grid-column: 1; } }

/* --- test na telefonie (po regułach bazowych testu, żeby je nadpisać) --- */
@media (max-width: 760px) {
  /* Kółka z numerami pytań: bez min-height (rozciągało je do elips); większe pod palec. */
  .quiz-dot { min-height: 0; width: 38px; height: 38px; flex: 0 0 auto; font-size: 14px; }
  /* Test: jak czat — przypięty do widocznej części ekranu (--app-h z common.js), przewija się tylko #quiz; dzięki temu klawiatura
     ekranowa nie zasłania pola odpowiedzi, a Wstecz/Dalej zostają tuż nad nią. Nagłówek przewija się razem z treścią (przy
     klawiaturze nie zabiera miejsca). */
  html:has(body.quiz-page) { overflow: hidden; overscroll-behavior: none; }
  body.quiz-page { position: fixed; top: var(--app-top, 0px); left: 0; width: 100%; height: var(--app-h, 100dvh); min-height: 0; overflow: hidden; }
  .quiz-page #quiz { height: 100%; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
    padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }
  .quiz-page .quiz-head { position: static; }
  .quiz-page .quiz-nav { padding-bottom: calc(14px + env(safe-area-inset-bottom)); }
  .quiz-page .quiz-nav .side-btn, .quiz-page .quiz-nav .primary { font-size: 16px; padding: 12px 8px; white-space: nowrap; }
  /* Pole odpowiedzi niskie: nad klawiaturą ma się zmieścić razem z pytaniem i przyciskami; dłuższy tekst przewija się w polu. */
  .quiz-page .quiz-open { height: 130px; resize: none; }
}

/* --- nauka z fiszek (web/fiszki.html, web/fiszki.js) --- */
.fiszki { max-width: 680px; margin: 0 auto; padding: 24px 20px 48px; }
.fiszki-section { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; margin-bottom: 16px; }
.fiszki-section h2 { margin: 0 0 10px; font-size: 18px; }
.fiszki-today-count { font-size: 24px; font-weight: 800; margin-bottom: 12px; font-variant-numeric: tabular-nums; }
.fiszki-filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 8px; margin-bottom: 10px; }
.fiszki select { width: 100%; padding: 9px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel); color: var(--text); font: inherit; }
.fiszki-pool-info { color: var(--muted); margin: 0 0 12px; font-variant-numeric: tabular-nums; }
.fiszki-start-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.fiszki-start-row select { width: auto; }
.fiszki-meta { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 14px; margin-bottom: 10px; }
.fiszki-progress { font-weight: 700; font-variant-numeric: tabular-nums; }
.fiszki-card { text-align: center; min-height: 200px; display: flex; flex-direction: column; justify-content: center; gap: 16px; padding: 20px 6px; }
.fiszki-question { font-size: 22px; font-weight: 700; line-height: 1.35; overflow-wrap: anywhere; }
.fiszki-answer { font-size: 18px; line-height: 1.45; border-top: 1px dashed var(--border); padding-top: 14px; overflow-wrap: anywhere; white-space: pre-wrap; }
.fiszki-answer[hidden], .fiszki-rate[hidden], .fiszki-check[hidden] { display: none; }
.fiszki-check { width: 100%; padding: 14px; font-size: 17px; }
.fiszki-rate { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.fiszki-btn { min-height: 60px; padding: 8px 6px; border: 2px solid var(--border); border-radius: 12px; background: var(--panel); color: var(--text);
  font: inherit; font-weight: 700; cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; }
.fiszki-btn small { font-weight: 400; font-size: 12px; color: var(--muted); }
.fiszki-btn:active { transform: translateY(1px); }
.fiszki-btn.nie_wiem { border-color: #d9534f; background: color-mix(in srgb, #d9534f 10%, var(--panel)); }
.fiszki-btn.tak_sobie { border-color: #e0a526; background: color-mix(in srgb, #e0a526 10%, var(--panel)); }
.fiszki-btn.wiem { border-color: #3b9d5d; background: color-mix(in srgb, #3b9d5d 10%, var(--panel)); }
.fiszki-dots { display: inline-flex; gap: 4px; }
.fiszki-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--border); }
.fiszki-dot.wiem { background: #3b9d5d; }
.fiszki-dot.tak_sobie { background: #e0a526; }
.fiszki-dot.nie_wiem { background: #d9534f; }
.fiszki-keys { text-align: center; font-size: 13px; }
@media (max-width: 560px) {
  .fiszki { padding: 16px 12px 40px; }
  .fiszki-question { font-size: 19px; }
  .fiszki-answer { font-size: 16px; }
  .fiszki-keys { display: none; }  /* skróty klawiszowe nie mają sensu na telefonie */
  .fiszki-filters { grid-template-columns: 1fr 1fr; }
}
.fiszki-page .admin-top { flex-wrap: wrap; row-gap: 8px; }
@media (max-width: 560px) {
  .fiszki-page .admin-top { padding: 12px; }
  .fiszki-page .admin-top-actions { width: 100%; }
  .fiszki-page .admin-top-actions .side-btn.back { flex: 1 1 auto; text-align: center; }
}
.fiszki-gen { margin: 4px 0 12px; display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.fiszki-gen p { margin: 0; }
.fiszki-gen [hidden], .fiszki-gen p:empty { display: none; }
.fiszki-notice { font-weight: 700; }

/* wzory (KaTeX, web/math.js): długi wzór blokowy przewija się poziomo zamiast rozpychać dymek */
.katex-display { overflow-x: auto; overflow-y: hidden; padding: .15em 0; }
.msg.assistant .body .katex { font-size: 1.08em; }

/* „Moja nauka”: wybór przedmiotu nad kafelkami */
.study-subject { margin-bottom: 12px; }
.study-subject select { font: inherit; padding: 8px 12px; border-radius: 8px; max-width: 100%; }
