:root {
  --bg: #f6f7f9; --panel: #ffffff; --side: #eef1f5; --line: #e2e6ec; --line-strong: #cfd5de;
  --text: #1c2430; --muted: #6a7585; --faint: #98a2b0;
  --accent: #1d5bd8; --accent-soft: #e8effd; --accent-text: #fff;
  --ai: #6b3fd4; --ai-soft: #f1ecfd; --ai-line: #dccff8;
  --warn: #b25e00; --warn-soft: #fff4e5; --danger: #c62828; --ok: #1e7e3e; --ok-soft: #e7f6ec;
  --unread: #1d5bd8; --star: #e8a400; --sel: #e3ebfb; --hover: #f1f4f8;
  --shadow: 0 10px 40px rgba(20, 30, 50, .18);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c; --panel: #1b1f26; --side: #171a20; --line: #2a303a; --line-strong: #3a414d;
    --text: #e4e8ee; --muted: #9aa4b2; --faint: #6c7684;
    --accent: #5b8ff5; --accent-soft: #1f2c45; --accent-text: #0d1320;
    --ai: #a888f5; --ai-soft: #251f3a; --ai-line: #3b3060;
    --warn: #f0a74a; --warn-soft: #33281a; --danger: #ef6b6b; --ok: #5fcf85; --ok-soft: #1a2e22;
    --unread: #7aa6ff; --sel: #22304a; --hover: #20252e;
    --shadow: 0 10px 40px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Arial, sans-serif;
  background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

/* --- przyciski i pola --- */
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line-strong); background: var(--panel);
  border-radius: 8px; padding: 6px 12px; cursor: pointer; white-space: nowrap; line-height: 1.3; }
.btn:hover { background: var(--hover); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ai { background: var(--ai); border-color: var(--ai); color: #fff; font-weight: 600; }
.btn.ai:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--hover); }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 3px 9px; font-size: 13px; }
.icon-btn { border: 0; background: transparent; cursor: pointer; padding: 6px 8px; border-radius: 7px; color: var(--muted);
  font-size: 15px; line-height: 1; }
.icon-btn:hover { background: var(--hover); color: var(--text); }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=search], select, textarea {
  border: 1px solid var(--line-strong); background: var(--panel); border-radius: 8px; padding: 7px 10px; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { resize: vertical; }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 4px; font-weight: 500; }
.row { display: flex; gap: 10px; align-items: center; }
.row > * { min-width: 0; }
.grow { flex: 1; }
.spinner { width: 14px; height: 14px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%;
  display: inline-block; animation: spin .7s linear infinite; vertical-align: -2px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* --- układ --- */
.app { display: grid; grid-template-columns: 220px 400px 1fr; height: 100vh; height: 100dvh; }
.app.wide { grid-template-columns: 220px 1fr; }
.app.wide .list { display: none; }
.side { background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.brand { padding: 16px 16px 10px; font-weight: 700; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.brand .addr { font-weight: 400; font-size: 12px; color: var(--muted); display: block; overflow: hidden; text-overflow: ellipsis; }
.side .compose-btn { margin: 4px 12px 12px; justify-content: center; padding: 9px; }
.nav { overflow-y: auto; flex: 1; padding: 0 8px; }
.nav-title { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--faint); margin: 14px 10px 4px; }
.nav a { display: flex; align-items: center; gap: 9px; padding: 6px 10px; border-radius: 8px; color: var(--text);
  text-decoration: none; cursor: pointer; }
.nav a:hover { background: var(--hover); }
.nav a.active { background: var(--sel); font-weight: 600; }
.nav a .ico { width: 18px; text-align: center; opacity: .8; }
.nav a .lbl { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav a .cnt { font-size: 12px; font-weight: 600; color: var(--unread); }
.nav a .cnt.pending { color: var(--ai); }
.side-foot { padding: 10px 14px; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); }
.side-foot .row { justify-content: space-between; }

.list { min-width: 0; border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.list-head { padding: 10px 12px; border-bottom: 1px solid var(--line); }
.list-head h2 { font-size: 16px; margin: 0 0 8px; display: flex; align-items: center; gap: 8px; }
.list-head h2 .count { font-size: 12px; font-weight: 400; color: var(--muted); }
.filters { display: flex; gap: 6px; margin-top: 8px; }
.chip { border: 1px solid var(--line-strong); border-radius: 99px; padding: 2px 10px; font-size: 12.5px; cursor: pointer;
  background: transparent; }
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.items { overflow-y: auto; flex: 1; }
.item { padding: 10px 14px 10px 46px; border-bottom: 1px solid var(--line); cursor: pointer; position: relative; }
.item:hover { background: var(--hover); }
.item.sel { background: var(--sel); }
.item.unread::before { content: ""; position: absolute; left: 33px; top: 16px; width: 7px; height: 7px; border-radius: 50%;
  background: var(--unread); }
.item .l1 { display: flex; gap: 8px; align-items: baseline; }
.item .who { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item.unread .who, .item.unread .subj { font-weight: 650; }
.item .date { font-size: 12px; color: var(--muted); white-space: nowrap; }
.item .subj { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 1px; }
.item .snip { color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 1px; }
.item .tags { display: inline-flex; gap: 4px; font-size: 12px; }
.tag-ai { color: var(--ai); font-weight: 600; font-size: 11.5px; }
.star { color: var(--star); }
.more { padding: 14px; text-align: center; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }

.main { min-width: 0; min-height: 0; overflow-y: auto; background: var(--panel); }
.reader { max-width: 980px; margin: 0 auto; padding: 18px 26px 60px; }
.reader-bar { display: flex; gap: 4px; flex-wrap: wrap; align-items: center; position: sticky; top: 0; z-index: 2;
  background: var(--panel); padding: 10px 0; border-bottom: 1px solid var(--line); margin: -18px 0 14px; }
.reader h1 { font-size: 20px; margin: 4px 0 14px; line-height: 1.3; overflow-wrap: anywhere; }
.back-btn { display: none; }
.msg { border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 10px; background: var(--panel); }
.msg-head { display: flex; gap: 10px; padding: 10px 14px; align-items: flex-start; cursor: pointer; }
.avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--accent-soft); color: var(--accent);
  display: grid; place-items: center; font-weight: 700; font-size: 13px; flex: none; }
.avatar.me { background: var(--ok-soft); color: var(--ok); }
.msg-head .who { font-weight: 600; }
.msg-head .addr { color: var(--muted); font-size: 12.5px; }
.msg-head .to { color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; }
.msg-head .when { margin-left: auto; color: var(--muted); font-size: 12.5px; white-space: nowrap; }
.msg-body { padding: 0 14px 14px 58px; }
.msg.collapsed .msg-body { display: none; }
.msg.collapsed .msg-head .preview { display: block; }
.msg-head .preview { display: none; color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-body pre { white-space: pre-wrap; font: inherit; margin: 0; overflow-wrap: anywhere; }
.msg-body iframe { width: 100%; border: 0; min-height: 60px; display: block; background: #fff; border-radius: 6px; }
.banner { background: var(--warn-soft); color: var(--warn); border-radius: 8px; padding: 7px 12px; font-size: 13px;
  margin-bottom: 10px; display: flex; gap: 10px; align-items: center; }
.atts { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.att { display: inline-flex; gap: 8px; align-items: center; border: 1px solid var(--line-strong); border-radius: 8px;
  padding: 6px 10px; text-decoration: none; color: var(--text); font-size: 13px; max-width: 280px; }
.att:hover { background: var(--hover); }
.att .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att .sz { color: var(--muted); font-size: 12px; }
.quote-toggle { color: var(--muted); font-size: 12.5px; cursor: pointer; border: 1px solid var(--line); border-radius: 6px;
  padding: 0 8px; display: inline-block; margin-top: 8px; background: transparent; }

/* --- karta asystenta --- */
.ai-card { border: 1px solid var(--ai-line); background: var(--ai-soft); border-radius: var(--radius); padding: 12px 14px;
  margin-bottom: 14px; }
.ai-card .ai-title { font-weight: 650; color: var(--ai); display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.ai-card .ai-title .grow { flex: 1; }
.ai-card textarea { min-height: 38px; max-height: 240px; background: var(--panel); resize: none; overflow-y: auto; line-height: 1.45; display: block; }
.ai-draft-text { white-space: pre-wrap; background: var(--panel); border: 1px solid var(--ai-line); border-radius: 8px;
  padding: 10px 12px; max-height: 260px; overflow: auto; margin: 6px 0 8px; }
.conf { font-size: 12px; padding: 1px 8px; border-radius: 99px; font-weight: 600; }
.conf.wysoka { background: var(--ok-soft); color: var(--ok); }
.conf.srednia { background: var(--warn-soft); color: var(--warn); }
.conf.niska { background: #fde8e8; color: var(--danger); }
.ai-notes { font-size: 13px; color: var(--muted); }
.ai-notes ul { margin: 4px 0; padding-left: 18px; }
.placeholder-mark { background: #ffe58a; color: #5a4300; border-radius: 3px; padding: 0 2px; }

/* --- okno pisania --- */
.compose { position: fixed; right: 24px; bottom: 0; width: min(720px, calc(100vw - 48px)); height: min(86vh, 860px);
  background: var(--panel); border: 1px solid var(--line-strong); border-radius: 12px 12px 0 0; box-shadow: var(--shadow);
  display: flex; flex-direction: column; z-index: 50; }
.compose.min { height: 44px; overflow: hidden; }
.compose-head { display: flex; align-items: center; gap: 8px; padding: 8px 10px 8px 16px; border-bottom: 1px solid var(--line);
  font-weight: 600; cursor: pointer; }
.compose-head .grow { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.compose-fields { padding: 4px 16px; border-bottom: 1px solid var(--line); }
.cf { display: flex; align-items: center; gap: 8px; border-bottom: 1px solid var(--line); position: relative; }
.cf:last-child { border-bottom: 0; }
.cf > span { color: var(--muted); width: 52px; font-size: 13px; }
.cf input { border: 0; padding: 8px 0; border-radius: 0; }
.cf input:focus { outline: none; }
.suggest { position: absolute; left: 60px; top: 100%; z-index: 5; background: var(--panel); border: 1px solid var(--line-strong);
  border-radius: 8px; box-shadow: var(--shadow); min-width: 300px; overflow: hidden; }
.suggest div { padding: 7px 12px; cursor: pointer; }
.suggest div:hover, .suggest div.on { background: var(--sel); }
.compose-ai { padding: 8px 16px; background: var(--ai-soft); border-bottom: 1px solid var(--ai-line); }
.compose-ai input { background: var(--panel); }
.compose-ai .ai-info { font-size: 12.5px; margin-top: 6px; color: var(--muted); }
.compose textarea.body { flex: 1; border: 0; border-radius: 0; padding: 12px 16px; resize: none; line-height: 1.55; min-height: 120px; }
.compose textarea.body:focus { outline: none; }
.compose-foot { display: flex; gap: 8px; align-items: center; padding: 10px 16px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.files { display: flex; gap: 6px; flex-wrap: wrap; padding: 0 16px 6px; }
.file-chip { font-size: 12.5px; border: 1px solid var(--line-strong); border-radius: 6px; padding: 2px 8px; display: inline-flex; gap: 6px; }
.file-chip button { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 0; }

/* --- strony: wiedza, asystent, ustawienia --- */
.page { max-width: 980px; margin: 0 auto; padding: 24px 28px 80px; }
.page h1 { font-size: 22px; margin: 0 0 4px; }
.page .lead { color: var(--muted); margin: 0 0 20px; }
.card { border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; margin-bottom: 16px; background: var(--panel); }
.card h3 { margin: 0 0 10px; font-size: 15px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 16px; }
.stat { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.stat b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: 12.5px; }
.kb-item { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px; }
.kb-item.pending { border-color: var(--ai-line); background: var(--ai-soft); }
.kb-item .kb-head { display: flex; gap: 8px; align-items: center; }
.kb-item .kb-title { font-weight: 650; flex: 1; }
.kb-item .kb-cat { font-size: 12px; color: var(--muted); border: 1px solid var(--line-strong); border-radius: 99px; padding: 0 8px; }
.kb-item .kb-content { white-space: pre-wrap; margin-top: 6px; color: var(--text); max-height: 7.5em; overflow: hidden;
  position: relative; font-size: 13.5px; }
.kb-item.open .kb-content { max-height: none; }
.kb-src { font-size: 11.5px; color: var(--faint); }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; }
.tabs button { border: 0; background: none; padding: 8px 12px; cursor: pointer; border-bottom: 2px solid transparent; color: var(--muted); }
.tabs button.on { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.progress { height: 6px; border-radius: 99px; background: var(--line); overflow: hidden; margin: 8px 0; }
.progress > div { height: 100%; background: var(--ai); transition: width .4s; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.pair { border-bottom: 1px solid var(--line); padding: 10px 0; font-size: 13px; }
.pair .q { color: var(--muted); }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #222b38; color: #fff;
  padding: 10px 16px; border-radius: 9px; box-shadow: var(--shadow); z-index: 100; max-width: 90vw; font-size: 13.5px; }
.toast.err { background: var(--danger); }
.modal-bg { position: fixed; inset: 0; background: rgba(10, 15, 25, .45); z-index: 80; display: grid; place-items: center; padding: 16px; }
.modal { background: var(--panel); border-radius: 12px; box-shadow: var(--shadow); width: min(640px, 100%); max-height: 90vh;
  overflow: auto; padding: 18px 20px; }
.modal h3 { margin: 0 0 12px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.menu { position: absolute; background: var(--panel); border: 1px solid var(--line-strong); border-radius: 8px; box-shadow: var(--shadow);
  z-index: 60; min-width: 200px; max-height: 360px; overflow: auto; padding: 4px; }
.menu div { padding: 7px 12px; border-radius: 6px; cursor: pointer; }
.menu div:hover { background: var(--hover); }
kbd { font: 11px ui-monospace, monospace; border: 1px solid var(--line-strong); border-radius: 4px; padding: 0 4px; }

/* --- telefon --- */
@media (max-width: 1100px) { .app { grid-template-columns: 200px 340px 1fr; } }
@media (max-width: 860px) {
  .app, .app.wide { grid-template-columns: minmax(0, 1fr); }
  .side { position: fixed; inset: 0 30% 0 0; z-index: 70; transform: translateX(-102%); transition: transform .2s; box-shadow: var(--shadow); }
  .app.nav-open .side { transform: none; }
  .app .main { display: none; }
  .app.reading .list, .app.wide .list { display: none; }
  .app.reading .main, .app.wide .main { display: block; }
  .menu-btn { display: inline-block !important; }
  .back-btn { display: inline-flex; }
  .reader { padding: 14px 16px 60px; }
  .msg-body { padding: 0 12px 12px; }
  .page { padding: 16px 16px 60px; }
  .compose { right: 0; width: 100vw; height: 100dvh; border-radius: 0; }
  .grid2 { grid-template-columns: 1fr; }
}
.menu-btn { display: none; }
input[type=checkbox] { flex: none; width: 16px; height: 16px; accent-color: var(--accent); }

.ai-questions { background: var(--panel); border: 1px solid var(--ai-line); border-radius: 8px; padding: 12px 14px; margin-bottom: 4px; }
.ai-q-head { font-weight: 650; margin-bottom: 8px; }
.ai-q { margin-bottom: 12px; }
.ai-q:last-of-type { margin-bottom: 0; }
.ai-q-text { margin-bottom: 5px; }
.ai-q-props { display: flex; flex-wrap: wrap; gap: 5px; margin-bottom: 6px; }
.ai-q-props .chip { text-align: left; }
.ai-q textarea { min-height: 34px; }
.ai-card .ai-title + textarea { margin-bottom: 2px; }
.compose-ai .ai-questions { margin-top: 8px; max-height: 40vh; overflow: auto; }

.sig-frame { width: 100%; border: 0; display: block; background: #fff; border-radius: 6px; min-height: 30px; }
.sig-preview { border: 1px dashed var(--line-strong); border-radius: 8px; padding: 10px; background: #fff; }
.compose-sig { padding: 6px 16px 8px; border-top: 1px dashed var(--line); max-height: 200px; overflow: auto; flex: none; }
textarea.mono { font: 12.5px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; }

.compose-main { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; }
.compose-main textarea.body { flex: 1 0 220px; }
.compose-ai { flex: none; }
.ai-bar { display: flex; gap: 8px; align-items: center; }
.ai-bar input { flex: 1; min-width: 0; width: auto; }
.ai-bar .btn { flex: none; }
.ai-quick { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 7px; }
.ai-quick .chip { background: var(--panel); font-size: 12px; }
.ai-quick .chip:disabled { opacity: .5; cursor: default; }
.ai-details { font-size: 12.5px; margin-top: 6px; }
.ai-details summary { cursor: pointer; color: var(--ai); }
.compose-sig { max-height: 150px; }
.ai-refine { margin-top: 10px; }
.ai-refine input { background: var(--panel); }
.tcount { display: inline-block; margin-left: 6px; font-size: 11.5px; font-weight: 600; color: var(--muted);
  border: 1px solid var(--line-strong); border-radius: 99px; padding: 0 6px; line-height: 1.5; vertical-align: 1px; }
.filters { flex-wrap: wrap; }

.item .check { position: absolute; left: 6px; top: 6px; width: 26px; height: 30px; display: grid; place-items: center; cursor: pointer; }
.item .check input { pointer-events: none; opacity: .35; }
.item:hover .check input, .item.checked .check input, .list.selecting .check input { opacity: 1; }
.item.checked { background: var(--accent-soft); }
.bulk { display: flex; align-items: center; gap: 2px; margin-top: 8px; min-height: 30px; }
.bulk .check { display: grid; place-items: center; width: 26px; height: 28px; cursor: pointer; margin-left: -4px; }
.bulk .check input { pointer-events: none; }
.bulk-n { font-size: 13px; margin: 0 8px 0 2px; white-space: nowrap; }
