/* ============================================================
   IAMunicipal — ChatGPT-Style UI
   ============================================================ */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg:           #f7f7f8;
    --surface:      #ffffff;
    --surface2:     #f0f0f0;
    --border:       #e5e5e5;
    --accent:       #10a37f;
    --accent-hover: #0d8a6c;
    --text:         #1a1a1a;
    --text-sec:     #6e6e80;
    --text-muted:   #8e8ea0;
    --user-bg:      #f7f7f8;
    --ai-bg:        #ffffff;
    --danger:       #ef4444;
    --success:      #10a37f;
    --warning:      #f59e0b;
    --radius:       12px;
    --radius-sm:    8px;
    --sidebar-w:    260px;
    --sidebar-bg:   #171717;
    --sidebar-text: #ececf1;
    --sidebar-hover:#2a2b32;
    --sidebar-border:#2a2b32;
    --chat-max-w:   768px;
}

body[data-theme="dark"] {
    --bg:           #212121;
    --surface:      #212121;
    --surface2:     #2f2f2f;
    --border:       #3a3a3a;
    --accent:       #10a37f;
    --accent-hover: #14b88e;
    --text:         #ececf1;
    --text-sec:     #9b9ba4;
    --text-muted:   #6e6e80;
    --user-bg:      #212121;
    --ai-bg:        #2f2f2f;
    --sidebar-bg:   #171717;
    --sidebar-hover:#2a2b32;
    --sidebar-border:#2a2b32;
}

body {
    font-family: -apple-system, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100dvh;
    display: flex;
    overflow: hidden;
}

/* ── Sidebar ── */
#sidebar {
    width: var(--sidebar-w);
    background: var(--sidebar-bg);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    overflow: hidden;
    transition: margin-left .25s ease, opacity .2s;
    z-index: 100;
}
body.sidebar-collapsed #sidebar {
    margin-left: calc(var(--sidebar-w) * -1);
    opacity: 0;
    pointer-events: none;
}

#sidebar-header {
    padding: 16px 16px 8px;
}
#brand-logo, #brand-logo-dark {
    display: block;
    width: 100%;
    max-width: 180px;
    height: auto;
    margin: 0 auto 4px;
    filter: brightness(0) invert(1);
}
.logo-dark { display: none !important; }
body[data-theme="dark"] .logo-light { display: none !important; }
body[data-theme="dark"] .logo-dark  { display: block !important; }
/* In dark sidebar both logos should look white */
body:not([data-theme="dark"]) #brand-logo { filter: brightness(0) invert(1); }

#btn-new-chat {
    margin: 4px 12px 8px;
    padding: 10px 14px;
    background: transparent;
    color: var(--sidebar-text);
    border: 1px solid var(--sidebar-border);
    border-radius: var(--radius);
    cursor: pointer;
    font-size: .875rem;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background .15s;
}
#btn-new-chat:hover { background: var(--sidebar-hover); }
#btn-new-chat svg { flex-shrink: 0; }

#conv-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 8px;
}

.conv-item {
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: .875rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    transition: background .15s;
    margin-bottom: 2px;
    color: var(--sidebar-text);
}
.conv-item:hover  { background: var(--sidebar-hover); }
.conv-item.active { background: var(--sidebar-hover); }
.conv-title  { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-delete {
    color: #8e8ea0;
    background: none;
    border: none;
    cursor: pointer;
    font-size: .875rem;
    padding: 2px 4px;
    border-radius: 4px;
    opacity: 0;
    transition: opacity .15s, color .15s;
}
.conv-item:hover .conv-delete { opacity: 1; }
.conv-delete:hover { color: var(--danger); }

#sidebar-footer {
    padding: 10px 12px;
    border-top: 1px solid var(--sidebar-border);
    display: flex;
    align-items: center;
    gap: 8px;
}
#sidebar-tabs {
    display: flex;
    gap: 4px;
    flex: 1;
}
.sidebar-tab {
    flex: 1;
    padding: 7px 2px;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: #8e8ea0;
    font-size: .7rem;
    cursor: pointer;
    transition: color .15s, background .15s;
    text-align: center;
}
.sidebar-tab:hover { color: var(--sidebar-text); background: var(--sidebar-hover); }
.sidebar-tab.active { color: var(--sidebar-text); background: var(--sidebar-hover); }

#theme-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    border: none;
    background: transparent;
    color: #8e8ea0;
    cursor: pointer;
    transition: background .15s, color .15s;
    flex-shrink: 0;
}
#theme-toggle:hover { background: var(--sidebar-hover); color: var(--sidebar-text); }
#theme-toggle .icon-moon  { display: none; }
body[data-theme="dark"] #theme-toggle .icon-sun  { display: none; }
body[data-theme="dark"] #theme-toggle .icon-moon { display: block; }

/* ── Sidebar toggle button ── */
#sidebar-toggle {
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 101;
    width: 36px;
    height: 36px;
    border-radius: var(--radius-sm);
    border: none;
    background: var(--surface);
    color: var(--text-sec);
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 6px rgba(0,0,0,.08);
    transition: background .15s;
}
#sidebar-toggle:hover { background: var(--surface2); }
body.sidebar-collapsed #sidebar-toggle { display: flex; }

/* ── Main panel ── */
#main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

/* ── Panel views ── */
.panel { display: none; flex: 1; flex-direction: column; overflow: hidden; }
.panel.active { display: flex; }

/* ── Chat panel — centered layout ── */
#chat-panel {
    display: flex;
    flex-direction: column;
    height: 100%;
}

#messages {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

#welcome-screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: var(--text-sec);
    padding: 40px;
}
#welcome-icon {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
}
#welcome-screen h2 {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text);
}
#welcome-screen p {
    font-size: .95rem;
    color: var(--text-muted);
}

/* ── Message rows: full width stripe, content centered ── */
.message-row {
    width: 100%;
    padding: 24px 20px;
}
.message-row.user {
    background: var(--user-bg);
}
.message-row.assistant {
    background: var(--ai-bg);
    border-bottom: 1px solid var(--border);
}

.message-inner {
    max-width: var(--chat-max-w);
    margin: 0 auto;
    display: flex;
    gap: 16px;
    align-items: flex-start;
}

.msg-avatar {
    width: 30px;
    height: 30px;
    border-radius: 4px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: .8rem;
    font-weight: 700;
    margin-top: 2px;
}
.message-row.user .msg-avatar {
    background: #5436da;
    color: #fff;
}
.message-row.assistant .msg-avatar {
    background: var(--accent);
    color: #fff;
}

.msg-body {
    flex: 1;
    min-width: 0;
}
.msg-sender {
    font-size: .8rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 4px;
}

.msg-content {
    line-height: 1.7;
    font-size: .9375rem;
    color: var(--text);
    word-break: break-word;
}

/* ── System messages ── */
.message-row.system {
    padding: 8px 20px;
    background: transparent;
}
.message-row.system .message-inner {
    justify-content: center;
}
.message-row.system .msg-content {
    font-size: .78rem;
    color: var(--text-muted);
    text-align: center;
    background: var(--surface2);
    padding: 6px 14px;
    border-radius: 20px;
}

/* ── Markdown content ── */
.msg-content strong { font-weight: 600; }
.msg-content em { font-style: italic; }
.msg-content del { text-decoration: line-through; opacity: .6; }

.msg-content .md-heading {
    font-weight: 600;
    color: var(--text);
    margin: 16px 0 8px;
    line-height: 1.3;
}
.msg-content h1.md-heading { font-size: 1.25rem; }
.msg-content h2.md-heading { font-size: 1.1rem; }
.msg-content h3.md-heading { font-size: 1rem; }
.msg-content h4.md-heading { font-size: .95rem; }

.msg-content .md-inline-code {
    background: rgba(0,0,0,.06);
    color: var(--text);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'SFMono-Regular', 'Consolas', 'Monaco', monospace;
    font-size: .85em;
}

.msg-content .md-code-block {
    background: #1e1e1e;
    color: #d4d4d4;
    padding: 14px 16px;
    border-radius: var(--radius-sm);
    font-family: 'SFMono-Regular', 'Consolas', 'Monaco', monospace;
    font-size: .84rem;
    line-height: 1.55;
    overflow-x: auto;
    margin: 10px 0;
}
.msg-content .md-code-block code {
    background: none;
    color: inherit;
    padding: 0;
    font-size: inherit;
}

.msg-content .md-list {
    margin: 6px 0 6px 20px;
    padding: 0;
    line-height: 1.7;
}
.msg-content .md-list li {
    margin-bottom: 4px;
}

.msg-content .md-hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 14px 0;
}

body[data-theme="dark"] .msg-content .md-inline-code {
    background: rgba(255,255,255,.1);
}
body[data-theme="dark"] .msg-content .md-code-block {
    background: #0d0d0d;
}

/* ── Typing indicator ── */
.typing-indicator .msg-content span {
    display: inline-block;
    width: 7px; height: 7px;
    background: var(--accent);
    border-radius: 50%;
    margin: 0 2px;
    animation: bounce .8s infinite;
}
.typing-indicator .msg-content span:nth-child(2) { animation-delay: .15s; }
.typing-indicator .msg-content span:nth-child(3) { animation-delay: .30s; }
@keyframes bounce {
    0%, 80%, 100% { transform: translateY(0); }
    40%           { transform: translateY(-6px); }
}

/* ── Input area — bottom, centered ── */
#input-area {
    padding: 0 20px 20px;
    background: var(--bg);
    display: flex;
    flex-direction: column;
    align-items: center;
}

#input-wrapper {
    width: 100%;
    max-width: var(--chat-max-w);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    box-shadow: 0 2px 12px rgba(0,0,0,.05);
    overflow: hidden;
    transition: border-color .2s, box-shadow .2s;
}
#input-wrapper:focus-within {
    border-color: var(--accent);
    box-shadow: 0 2px 16px rgba(16,163,127,.12);
}

#input-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 14px 0;
}

#persona-select {
    background: var(--surface2);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 4px 8px;
    border-radius: 6px;
    font-size: .75rem;
    cursor: pointer;
}

#rag-toggle, #db-toggle {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: .75rem;
    color: var(--text-muted);
    cursor: pointer;
}
#rag-toggle input, #db-toggle input { accent-color: var(--accent); width: 14px; height: 14px; }

#input-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 8px 14px 10px;
}

#msg-input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text);
    font-size: .9375rem;
    resize: none;
    max-height: 200px;
    outline: none;
    font-family: inherit;
    line-height: 1.5;
    padding: 4px 0;
}
#msg-input::placeholder { color: var(--text-muted); }

#send-btn {
    width: 36px;
    height: 36px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background .15s, transform .1s;
}
#send-btn:hover    { background: var(--accent-hover); }
#send-btn:active   { transform: scale(.95); }
#send-btn:disabled { opacity: .4; cursor: not-allowed; }

#input-hint {
    font-size: .72rem;
    color: var(--text-muted);
    margin-top: 8px;
}

/* ── Admin panels (docs, personas, dbsources) ── */
#docs-panel, #personas-panel, #dbsources-panel { padding: 24px; gap: 16px; overflow-y: auto; }

.panel-title {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text);
    margin-bottom: 4px;
}

.admin-help-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 16px;
}
.admin-help-title {
    font-size: .9rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 8px;
}
.admin-help-card p {
    font-size: .82rem;
    line-height: 1.55;
    color: var(--text-sec);
    margin-top: 6px;
}

.admin-inline-help {
    font-size: .8rem;
    color: var(--text-sec);
    background: rgba(16,163,127,.06);
    border: 1px dashed rgba(16,163,127,.25);
    border-radius: var(--radius-sm);
    padding: 10px 12px;
}
.admin-inline-help span {
    color: var(--text);
    font-weight: 600;
}

/* ── Upload form ── */
#upload-form {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: flex-end;
}
#upload-form input[type="file"] {
    flex: 1; min-width: 180px;
    background: var(--surface2); border: 1px solid var(--border);
    border-radius: var(--radius-sm); padding: 8px 10px;
    color: var(--text); font-size: .83rem;
}
#upload-form input[type="text"],
#upload-form select,
#upload-form textarea {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius-sm); padding: 8px 10px;
    color: var(--text); font-size: .83rem; width: 160px;
}
#upload-form #doc-type { min-width: 160px; }
#upload-form textarea {
    width: 100%; min-height: 140px; resize: vertical;
    font-family: inherit; line-height: 1.5;
}
#upload-btn {
    background: var(--accent); color: #fff; border: none;
    border-radius: var(--radius-sm); padding: 9px 18px;
    cursor: pointer; font-weight: 600; font-size: .85rem;
    transition: background .15s;
}
#upload-btn:hover { background: var(--accent-hover); }

/* ── Ingest result ── */
.ingest-result {
    background: rgba(16,163,127,.08);
    border: 1px solid rgba(16,163,127,.25);
    border-radius: var(--radius);
    padding: 14px 16px;
}
.ingest-result-title { font-size: .88rem; font-weight: 700; color: var(--accent); margin-bottom: 10px; }
.ingest-result-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; }
.ingest-result-grid span { display: block; font-size: .72rem; color: var(--text-muted); margin-bottom: 2px; }
.ingest-result-grid strong { font-size: .86rem; color: var(--text); word-break: break-word; }
.ingest-result-error {
    margin-top: 12px; padding: 10px 12px; border-radius: var(--radius-sm);
    background: rgba(239,68,68,.08); border: 1px solid rgba(239,68,68,.2);
    color: #dc2626; font-size: .8rem; line-height: 1.5;
}

/* ── Document cards ── */
#doc-list { display: flex; flex-direction: column; gap: 8px; }
.doc-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
}
.doc-icon { font-size: 1.5rem; }
.doc-info { flex: 1; }
.doc-name  { font-size: .88rem; font-weight: 600; }
.doc-meta  { font-size: .75rem; color: var(--text-muted); margin-top: 2px; }
.doc-error { font-size: .75rem; color: #dc2626; margin-top: 6px; line-height: 1.45; }
.doc-status {
    font-size: .72rem; padding: 2px 8px; border-radius: 12px; font-weight: 600;
}
.doc-status.ready      { background: rgba(16,163,127,.12); color: var(--success); }
.doc-status.processing { background: rgba(245,158,11,.12); color: var(--warning); }
.doc-status.pending    { background: rgba(245,158,11,.12); color: var(--warning); }
.doc-status.error      { background: rgba(239,68,68,.1); color: var(--danger); }
.doc-del {
    background: none; border: none; color: var(--text-muted);
    cursor: pointer; font-size: 1.1rem; padding: 4px;
    border-radius: 6px; transition: color .15s;
}
.doc-del:hover { color: var(--danger); }

/* ── Personas panel ── */
#persona-form {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.form-row { display: flex; gap: 10px; flex-wrap: wrap; }
.form-group { display: flex; flex-direction: column; gap: 4px; flex: 1; min-width: 160px; }
.form-group label { font-size: .78rem; color: var(--text-sec); }
.field-help { font-size: .72rem; line-height: 1.45; color: var(--text-muted); }
.form-group input,
.form-group select,
.form-group textarea {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 8px 10px;
    color: var(--text);
    font-size: .85rem;
    font-family: inherit;
    outline: none;
    transition: border-color .2s;
}
.form-group textarea { resize: vertical; min-height: 80px; }
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus { border-color: var(--accent); }

.form-actions { display: flex; gap: 8px; justify-content: flex-end; }
.btn-save {
    background: var(--accent); color: #fff; border: none;
    border-radius: var(--radius-sm); padding: 9px 20px;
    cursor: pointer; font-weight: 600; font-size: .85rem;
    transition: background .15s;
}
.btn-save:hover { background: var(--accent-hover); }
.btn-cancel {
    background: var(--surface2); color: var(--text-sec);
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    padding: 9px 16px; cursor: pointer; font-size: .85rem;
    transition: background .15s;
}
.btn-cancel:hover { background: var(--border); }

#persona-list { display: flex; flex-direction: column; gap: 8px; }
.persona-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 16px;
    display: flex; align-items: flex-start; gap: 12px;
}
.persona-card.is-default { border-color: var(--accent); }
.persona-info { flex: 1; }
.persona-name { font-weight: 600; font-size: .92rem; }
.persona-model { font-size: .75rem; color: var(--text-muted); margin-top: 2px; }
.persona-prompt { font-size: .78rem; color: var(--text-muted); margin-top: 6px; white-space: pre-wrap; word-break: break-word; }
.default-badge {
    font-size: .68rem; padding: 2px 8px;
    background: var(--accent); color: #fff;
    border-radius: 10px; margin-left: 6px;
}
.persona-actions { display: flex; gap: 6px; }
.btn-edit {
    background: var(--surface2); border: 1px solid var(--border);
    color: var(--text-sec); border-radius: 6px; padding: 5px 10px;
    cursor: pointer; font-size: .78rem; transition: color .15s;
}
.btn-edit:hover { color: var(--accent); border-color: var(--accent); }

/* ── Toast ── */
#toast-container {
    position: fixed; bottom: 24px; right: 24px; z-index: 9999;
    display: flex; flex-direction: column; gap: 8px;
}
.toast {
    padding: 12px 18px; border-radius: var(--radius-sm);
    font-size: .85rem; font-weight: 500; color: #fff;
    box-shadow: 0 4px 16px rgba(0,0,0,.2);
    animation: toastIn .3s ease;
}
.toast.success { background: #0d8a6c; }
.toast.error   { background: #dc2626; }
@keyframes toastIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

/* ── Scrollbar ── */
::-webkit-scrollbar       { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(0,0,0,.15); border-radius: 3px; }
body[data-theme="dark"] ::-webkit-scrollbar-thumb { background: rgba(255,255,255,.12); }
#sidebar ::-webkit-scrollbar-thumb { background: rgba(255,255,255,.12); }

/* ── Dark mode overrides ── */
body[data-theme="dark"] #upload-form input[type="file"],
body[data-theme="dark"] #upload-form input[type="text"],
body[data-theme="dark"] #upload-form select,
body[data-theme="dark"] #upload-form textarea,
body[data-theme="dark"] .form-group input,
body[data-theme="dark"] .form-group select,
body[data-theme="dark"] .form-group textarea,
body[data-theme="dark"] #msg-input,
body[data-theme="dark"] #persona-select {
    background: var(--surface2);
    color: var(--text);
    border-color: var(--border);
}

body[data-theme="dark"] #input-wrapper {
    background: var(--surface2);
    border-color: var(--border);
}

body[data-theme="dark"] #sidebar-toggle {
    background: var(--surface2);
    border: 1px solid var(--border);
}

body[data-theme="dark"] .doc-status.ready      { background: rgba(16,163,127,.15); }
body[data-theme="dark"] .doc-status.processing { background: rgba(245,158,11,.15); }
body[data-theme="dark"] .doc-status.pending    { background: rgba(245,158,11,.15); }

/* ── Responsive ── */
@media (max-width: 768px) {
    #sidebar { position: fixed; left: 0; top: 0; bottom: 0; }
    body:not(.sidebar-collapsed) #sidebar-toggle { display: flex; left: calc(var(--sidebar-w) + 8px); }
    .message-inner { gap: 10px; }
    .msg-avatar { width: 26px; height: 26px; font-size: .7rem; }
    #input-wrapper { border-radius: 12px; }
}
@media (max-width: 480px) {
    #sidebar { width: 85vw; }
    body.sidebar-collapsed #sidebar { margin-left: -85vw; }
}
body[data-theme="dark"] .doc-status.error      { background: #351e1e; }
