:root {
  --bg: #0e0e15;
  --bg-2: #161622;
  --bg-3: #1e1e2d;
  --border: #2a2a3c;
  --text: #e7e7ef;
  --muted: #9a9ab0;
  --accent: #7c5cff;
  --accent-2: #5b3fd6;
  --danger: #e0607e;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
}

button { font-family: inherit; cursor: pointer; }
input, textarea, select {
  font-family: inherit;
  font-size: 14px;
  background: var(--bg-3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  width: 100%;
}
input:focus, textarea:focus, select:focus { outline: 1px solid var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); margin-top: 8px; min-height: 18px; }

/* ---------- Logowanie ---------- */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
}
.login-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  width: 100%;
  max-width: 360px;
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card p { margin: 0 0 18px; }
.login-card input { margin-bottom: 12px; }
.login-card button {
  width: 100%;
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 8px;
  padding: 11px;
  font-weight: 600;
}
.pw-rules {
  list-style: none;
  padding: 0;
  margin: 2px 0 14px;
  font-size: 12px;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.pw-rules li::before {
  content: '○ ';
  color: var(--muted);
}
.pw-rules li.ok {
  color: #2bb673;
}
.pw-rules li.ok::before {
  content: '● ';
  color: #2bb673;
}

/* ---------- Układ ---------- */
#app { display: flex; flex-direction: column; height: 100vh; }
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
}
.topbar-title { font-size: 16px; margin: 0; flex: 1; }
.user-label { font-size: 12px; color: var(--muted); margin-right: 4px; white-space: nowrap; }

/* Klucze API */
#keys-list { display: flex; flex-direction: column; gap: 10px; margin-top: 6px; }
.key-row { display: flex; align-items: center; gap: 8px; }
.key-row .k-name { width: 120px; flex: none; font-size: 13px; }
.key-row input { flex: 1; }
.key-row .k-status { font-size: 11px; flex: none; width: 84px; text-align: right; }
.key-row .k-status.set { color: var(--accent-2); }
.key-row .k-status.set::before { content: '● '; color: #2bb673; }

/* Klienci */
.client-add { display: flex; gap: 6px; margin: 6px 0 12px; flex-wrap: wrap; }
.client-add input { flex: 1; min-width: 120px; }
.client-add select { max-width: 130px; }
.clients-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.client-item { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 8px; background: var(--bg-3); }
.client-item .c-email { flex: 1; font-size: 13px; word-break: break-all; }
.client-item .c-role { font-size: 10px; padding: 1px 6px; border-radius: 6px; border: 1px solid var(--border); color: var(--muted); flex: none; }
.client-item button { background: transparent; border: 1px solid var(--border); color: var(--text); border-radius: 6px; padding: 3px 8px; font-size: 12px; cursor: pointer; }
.client-item button.c-del { color: var(--danger); border-color: var(--danger); }
.icon-btn {
  background: transparent; border: 0; color: var(--text);
  font-size: 20px; padding: 4px 8px; display: none;
}
.ghost-btn {
  background: transparent; border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; padding: 7px 12px;
}
.layout { display: flex; flex: 1; min-height: 0; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 300px;
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 8px;
}
.panel-head h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 0; }
.small-btn {
  background: var(--bg-3); border: 1px solid var(--border); color: var(--text);
  border-radius: 7px; padding: 4px 9px; font-size: 12px;
}
.thread-list, .agent-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }

.thread-item {
  padding: 9px 10px; border-radius: 8px; border: 1px solid transparent;
  cursor: pointer; background: var(--bg-3);
}
.thread-item { display: flex; align-items: center; gap: 6px; }
.thread-item.active { border-color: var(--accent); }
.thread-info { flex: 1; min-width: 0; cursor: pointer; }
.thread-item .t-title { font-size: 14px; font-weight: 600; }
.thread-item .t-meta { font-size: 11px; color: var(--muted); margin-top: 2px; }
.thread-del { background: transparent; border: 0; color: var(--muted); cursor: pointer; font-size: 13px; padding: 2px 6px; flex: none; border-radius: 6px; }
.thread-del:hover { color: var(--danger); }

.agent-item {
  display: flex; align-items: center; gap: 9px;
  padding: 8px 10px; border-radius: 8px; background: var(--bg-3); cursor: pointer;
}
.agent-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.agent-item .a-name { font-size: 13px; font-weight: 600; }
.agent-item .a-role { font-size: 11px; color: var(--muted); }
.mem-add { display: flex; gap: 6px; margin-bottom: 8px; }
.mem-add input { flex: 1; }
.mem-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 5px; }
.mem-item { display: flex; align-items: flex-start; gap: 8px; padding: 7px 9px; border-radius: 8px; background: var(--bg-3); }
.mem-item .m-text { flex: 1; font-size: 12px; word-break: break-word; }
.mem-item .m-meta { display: block; color: var(--muted); font-size: 10px; margin-top: 2px; }
.mem-item .m-del { background: transparent; border: 0; color: var(--muted); cursor: pointer; flex: none; padding: 0 2px; }
.mem-item .m-del:hover { color: var(--danger); }

.agent-badge {
  font-size: 10px; padding: 1px 6px; border-radius: 6px;
  border: 1px solid var(--border); color: var(--muted); margin-left: auto; flex: none;
}

/* ---------- Główna część ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.thread-header {
  padding: 12px 18px; border-bottom: 1px solid var(--border);
  font-weight: 600; min-height: 20px;
}
.thread-header .th-dir { font-size: 12px; color: var(--muted); font-weight: 400; margin-top: 2px; }
.messages { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 14px; }
.empty-state { color: var(--muted); text-align: center; margin-top: 40px; }

.msg { max-width: 80%; }
.msg .bubble {
  border-radius: var(--radius); padding: 11px 14px; white-space: pre-wrap; word-wrap: break-word;
  border: 1px solid var(--border); background: var(--bg-2);
}
.msg .who { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.msg.user { align-self: flex-end; }
.msg.user .bubble { background: var(--accent-2); border-color: var(--accent); }
.msg.agent { align-self: flex-start; }
.msg.agent .bubble { border-left: 3px solid var(--accent); }
.msg.tool { align-self: center; max-width: 90%; }
.msg.tool .bubble {
  background: transparent; border: 1px dashed var(--border);
  color: var(--muted); font-size: 12px; padding: 6px 12px;
}

/* ---------- Panel plików (prawy) ---------- */
.filepanel {
  width: 320px;
  background: var(--bg-2);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.file-actions { display: flex; gap: 4px; }
.file-breadcrumb { font-size: 12px; color: var(--muted); word-break: break-all; }
.file-breadcrumb .crumb { color: var(--accent); cursor: pointer; }
.file-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.file-row {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 9px; border-radius: 8px; background: var(--bg-3);
}
.file-row .f-icon { flex: none; }
.file-row .f-name { flex: 1; font-size: 13px; cursor: pointer; word-break: break-all; }
.file-row.dir .f-name { font-weight: 600; }
.file-row .f-size { font-size: 11px; color: var(--muted); flex: none; }
.file-row .f-dl { background: transparent; border: 0; color: var(--muted); cursor: pointer; font-size: 14px; flex: none; padding: 2px 4px; }
.file-row .f-dl:hover { color: var(--text); }
.file-row .f-del:hover { color: var(--danger); }
.file-empty { font-size: 12px; color: var(--muted); }

/* Markdown w podglądzie */
.markdown { line-height: 1.55; }
.markdown h1, .markdown h2, .markdown h3, .markdown h4 { margin: .7em 0 .35em; line-height: 1.25; }
.markdown h1 { font-size: 1.5em; } .markdown h2 { font-size: 1.3em; } .markdown h3 { font-size: 1.12em; }
.markdown p { margin: .5em 0; }
.markdown ul, .markdown ol { margin: .4em 0 .4em 1.4em; }
.markdown li { margin: .15em 0; }
.markdown code { background: var(--bg); border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px; font-family: ui-monospace, Consolas, monospace; font-size: .9em; }
.markdown pre { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 12px; overflow: auto; }
.markdown pre code { background: none; border: 0; padding: 0; }
.markdown blockquote { border-left: 3px solid var(--border); margin: .5em 0; padding: .2em 0 .2em 12px; color: var(--muted); }
.markdown a { color: var(--accent); }
.markdown img { max-width: 100%; border-radius: 8px; }
.markdown hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }
.markdown table { border-collapse: collapse; width: 100%; margin: .6em 0; font-size: .92em; display: block; overflow-x: auto; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; vertical-align: top; }
.markdown th { background: var(--bg-3); font-weight: 600; }

.preview-card { max-width: 1320px; width: 90vw; }
.preview-body { max-height: 78vh; }
.preview-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.preview-head h3 { margin: 0; word-break: break-all; }
.preview-body { max-height: 70vh; overflow: auto; }
.preview-body pre {
  margin: 0; white-space: pre-wrap; word-break: break-word; font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 13px; background: var(--bg); padding: 12px; border-radius: 8px; border: 1px solid var(--border);
}
.preview-body img { max-width: 100%; border-radius: 8px; }
a.small-btn { text-decoration: none; display: inline-block; }

/* Strona podglądu Markdown (nowa karta: md.html) */
.md-page { min-height: 100vh; }
.md-topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 20px; background: var(--bg-2); border-bottom: 1px solid var(--border); }
.md-title { font-weight: 600; word-break: break-all; }
.md-container { max-width: 900px; margin: 0 auto; padding: 24px 20px 80px; }

/* ---------- Composer ---------- */
.composer { border-top: 1px solid var(--border); padding: 12px 16px; background: var(--bg-2); }
.composer-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.composer-row:last-child { margin-bottom: 0; }
.composer-label { font-size: 13px; color: var(--muted); flex: none; }
.composer-hint { font-size: 12px; color: var(--muted); }
.composer-hint b { color: var(--text); font-weight: 600; }
.recipient { max-width: 200px; flex: none; }
#input { resize: vertical; }
.send-btn {
  background: var(--accent); color: #fff; border: 0; border-radius: 8px;
  padding: 0 18px; font-weight: 600; flex: none; height: 42px;
}
.send-btn:disabled { opacity: .5; cursor: default; }
.send-btn.stop { background: var(--danger); }
.status { font-size: 12px; color: var(--muted); margin-top: 8px; min-height: 16px; }

/* ---------- Modale ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 50;
}
.modal-card {
  background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 22px; width: 100%; max-width: 440px; max-height: 90vh; overflow-y: auto;
}
.modal-card h3 { margin: 0 0 14px; }
.modal-card label { display: block; font-size: 12px; color: var(--muted); margin: 12px 0 5px; }
.modal-actions { display: flex; align-items: center; gap: 10px; margin-top: 20px; }
.primary-btn { background: var(--accent); color: #fff; border: 0; border-radius: 8px; padding: 9px 16px; font-weight: 600; }
.danger-btn { background: transparent; color: var(--danger); border: 1px solid var(--danger); border-radius: 8px; padding: 9px 14px; }
.new-folder-row { display: flex; gap: 8px; margin-top: 8px; }
.new-folder-row input { flex: 1; }

.typing-cursor { display: inline-block; margin-left: 1px; color: var(--accent); animation: blink 1s steps(2, start) infinite; }
@keyframes blink { to { opacity: 0; } }

.hidden { display: none !important; }

/* ---------- Mobile ---------- */
@media (max-width: 760px) {
  .icon-btn { display: block; }
  .sidebar {
    position: fixed; top: 52px; bottom: 0; left: 0; z-index: 40;
    width: 82%; max-width: 320px; transform: translateX(-100%);
    transition: transform .2s ease;
  }
  .sidebar.open { transform: translateX(0); box-shadow: 0 0 40px rgba(0,0,0,.6); }
  .msg { max-width: 92%; }
  .recipient { max-width: 150px; }

  .filepanel {
    position: fixed; top: 52px; bottom: 0; right: 0; z-index: 40;
    width: 85%; max-width: 340px; transform: translateX(100%);
    transition: transform .2s ease;
  }
  .filepanel.open { transform: translateX(0); box-shadow: 0 0 40px rgba(0,0,0,.6); }
}
