:root {
    --bg: #f6f6f7;
    --surface: #ffffff;
    --border: #e4e4e7;
    --text: #18181b;
    --muted: #71717a;
    --accent: #e4572e;
    --accent-hover: #c9461f;
    --radius: 8px;
    --success-bg: #ecfdf5; --success: #047857;
    --error-bg: #fef2f2;   --error: #b91c1c;
    --warning-bg: #fffbeb; --warning: #b45309;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: 15px;
    color: var(--text);
    background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; }
a { color: var(--accent); }
h1, h2 { font-weight: 600; margin: 0 0 16px; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }

/* górny pasek */
.topbar {
    display: flex; align-items: center; justify-content: space-between; gap: 16px;
    padding: 0 20px; height: 52px;
    background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 700; color: var(--accent); text-decoration: none; font-size: 1.1rem; }
.topbar nav { display: flex; gap: 18px; flex-wrap: wrap; }
.topbar nav a { color: var(--muted); text-decoration: none; }
.topbar nav a:hover { color: var(--text); }

.page { padding: 24px; max-width: 1100px; margin: 0 auto; }
.page.narrow { max-width: 480px; }

/* formularze */
form label { display: block; margin: 14px 0 6px; font-weight: 500; }
form label.checkbox { display: flex; align-items: center; gap: 8px; font-weight: 400; }
input[type=email], input[type=password], input[type=text], textarea {
    width: 100%; padding: 10px 12px; font: inherit;
    border: 1px solid var(--border); border-radius: var(--radius); background: #fff;
}
input:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 35%, transparent); border-color: var(--accent); }
form ul { margin: 6px 0 0; padding-left: 18px; color: var(--error); font-size: .9em; }
.help-text, form .form-help { color: var(--muted); font-size: .85em; }

.btn {
    display: inline-block; padding: 10px 18px; margin-top: 18px;
    border: 1px solid var(--border); border-radius: var(--radius);
    background: var(--surface); color: var(--text); font: inherit; cursor: pointer; text-decoration: none;
}
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn:disabled { opacity: .6; cursor: default; }

/* komunikaty */
.flash { padding: 10px 14px; border-radius: var(--radius); margin: 12px 0; }
.flash-success { background: var(--success-bg); color: var(--success); }
.flash-error, .flash-danger { background: var(--error-bg); color: var(--error); }
.flash-warning { background: var(--warning-bg); color: var(--warning); }
body > .flash { margin: 12px 20px; }

/* ekrany logowania */
body.auth { display: grid; place-items: center; padding: 24px; }
.auth-card {
    width: 100%; max-width: 400px; padding: 32px;
    background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
}
.auth-brand { color: var(--accent); text-align: center; }
.auth-card .btn { width: 100%; }
.auth-links { text-align: center; margin: 18px 0 0; font-size: .9em; }

/* czat */
body.chat-page { height: 100vh; display: flex; flex-direction: column; overflow: hidden; }
body.chat-page > .flash { flex: none; }
.chat-layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 260px 1fr; }
.chat-sidebar {
    display: flex; flex-direction: column; gap: 12px; min-height: 0;
    padding: 16px 12px; background: var(--surface); border-right: 1px solid var(--border);
}
.chat-new { margin: 0; text-align: center; }
.chat-history { overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.chat-history a {
    display: block; padding: 7px 10px; border-radius: 6px; color: var(--text); text-decoration: none;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: .92em;
}
.chat-history a:hover { background: var(--bg); }
.chat-history a.active { background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent-hover); }
.chat-history-empty { color: var(--muted); font-size: .9em; padding: 0 10px; }

.chat-main { display: flex; flex-direction: column; min-height: 0; }
.chat-messages { flex: 1; overflow-y: auto; padding: 24px max(24px, calc((100% - 860px) / 2)); }
.chat-empty { margin-top: 8vh; text-align: center; color: var(--muted); }
.chat-empty h1 { color: var(--text); }
.chat-examples { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 20px; }
.chat-example {
    padding: 8px 14px; border: 1px solid var(--border); border-radius: 18px; background: var(--surface);
    color: var(--text); font: inherit; font-size: .9em; cursor: pointer;
}
.chat-example:hover { border-color: var(--accent); }

.msg { margin: 0 0 18px; }
.msg-user { display: flex; justify-content: flex-end; }
.msg-user .msg-body {
    max-width: 80%; padding: 10px 14px; border-radius: 14px 14px 4px 14px;
    background: color-mix(in srgb, var(--accent) 10%, var(--surface)); white-space: pre-wrap;
}
.msg-assistant {
    padding: 14px 18px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
}
.msg-status { color: var(--muted); font-size: .9em; }
.msg-status::before { content: ''; display: inline-block; width: 8px; height: 8px; margin-right: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1s infinite alternate; }
@keyframes pulse { from { opacity: .25; } to { opacity: 1; } }
.msg-meta { margin-top: 8px; color: var(--muted); font-size: .8em; }
.msg-sources { margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--border); font-size: .85em; color: var(--muted); }
.msg-sources .source { display: inline-block; margin: 2px 10px 2px 0; }
.msg-sources .attachment { color: var(--muted); }
.msg-sources .attachment::before { content: '📎 '; }
.msg .flash { margin: 10px 0 0; }

.markdown { line-height: 1.55; overflow-wrap: anywhere; }
.markdown > :first-child { margin-top: 0; }
.markdown > :last-child { margin-bottom: 0; }
.markdown p, .markdown ul, .markdown ol { margin: 0 0 10px; }
.markdown h1, .markdown h2, .markdown h3 { font-size: 1.05rem; margin: 16px 0 8px; }
.markdown code { background: var(--bg); padding: 1px 4px; border-radius: 4px; font-size: .9em; }
.markdown .table-wrap { overflow-x: auto; margin: 0 0 10px; }
.markdown table { border-collapse: collapse; font-size: .9em; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 5px 8px; text-align: left; vertical-align: top; }
.markdown th { background: var(--bg); }

.chat-form {
    display: flex; gap: 10px; align-items: flex-end;
    padding: 12px max(24px, calc((100% - 860px) / 2)) 4px;
}
.chat-form textarea { resize: none; min-height: 48px; max-height: 200px; field-sizing: content; }
.chat-form .btn { margin: 0; height: 48px; }
.chat-delete { padding: 0 max(24px, calc((100% - 860px) / 2)) 10px; text-align: right; }
.link-button { background: none; border: 0; padding: 0; color: var(--muted); font: inherit; font-size: .8em; cursor: pointer; text-decoration: underline; }
.link-button:hover { color: var(--error); }

@media (max-width: 760px) {
    .chat-layout { grid-template-columns: 1fr; }
    .chat-sidebar { display: none; }
}
