/* ===========================================================
   Theme variables
   =========================================================== */
* { margin: 0; padding: 0; box-sizing: border-box; }

:root,
[data-theme="light"] {
    --bg: #ffffff;
    --bg-elevated: #ffffff;
    --bg-sidebar: #f5f5f5;
    --bg-hover: #ececec;
    --bg-active: #e4e4e4;
    --bg-input: #ffffff;
    --bg-code: #f1f1f1;

    --border: #e5e5e5;
    --border-strong: #d4d4d4;

    --fg: #0a0a0a;
    --fg-muted: #6b6b6b;
    --fg-subtle: #9a9a9a;

    --primary: #0a0a0a;
    --primary-hover: #1f1f1f;
    --primary-fg: #ffffff;

    --accent: #16a373;
    --accent-fg: #ffffff;

    --danger: #d92d20;
    --danger-bg: #fef3f2;
    --danger-fg: #b42318;

    --success-bg: #ecfdf5;
    --success-fg: #047857;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.1);

    --radius: 10px;
    --radius-sm: 6px;
    --radius-lg: 16px;
}

[data-theme="dark"] {
    --bg: #0c0c0d;
    --bg-elevated: #131315;
    --bg-sidebar: #0c0c0d;
    --bg-hover: #1d1d20;
    --bg-active: #26262a;
    --bg-input: #18181b;
    --bg-code: #1a1a1d;

    --border: #2a2a2e;
    --border-strong: #3a3a3f;

    --fg: #f3f3f3;
    --fg-muted: #9c9c9c;
    --fg-subtle: #6b6b6b;

    --primary: #f3f3f3;
    --primary-hover: #ffffff;
    --primary-fg: #0a0a0a;

    --accent: #1fb47e;
    --accent-fg: #ffffff;

    --danger: #f87171;
    --danger-bg: #2a1212;
    --danger-fg: #fca5a5;

    --success-bg: #0c2419;
    --success-fg: #6ee7b7;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.5);
}

html, body { height: 100%; }

body {
    font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--fg);
    height: 100vh;
    overflow: hidden;
}

.muted { color: var(--fg-muted); font-size: 14px; }

/* ===========================================================
   LOGIN
   =========================================================== */
.login-screen {
    display: none;
    align-items: center;
    justify-content: center;
    height: 100vh;
    width: 100%;
    background: var(--bg);
    padding: 20px;
}

.login-screen[style*="flex"], .login-screen.show { display: flex; }

.login-card {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 36px 32px;
    width: 100%;
    max-width: 440px;
    box-shadow: var(--shadow-md);
}

.login-title {
    font-size: 28px;
    font-weight: 800;
    letter-spacing: -0.02em;
    margin-bottom: 6px;
}

.login-form { margin-top: 24px; }

/* ===========================================================
   FORM PRIMITIVES
   =========================================================== */
.field { margin-bottom: 16px; }

.field > label {
    display: block;
    margin-bottom: 6px;
    font-size: 14px;
    font-weight: 600;
    color: var(--fg);
}

input[type="text"],
input[type="password"],
textarea,
select {
    width: 100%;
    padding: 11px 12px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--fg);
    font-family: inherit;
    font-size: 14px;
    transition: border-color 0.15s, box-shadow 0.15s;
}

input[type="text"]::placeholder,
input[type="password"]::placeholder,
textarea::placeholder {
    color: var(--fg-subtle);
}

input[type="text"]:focus,
input[type="password"]:focus,
textarea:focus,
select:focus {
    outline: none;
    border-color: var(--fg);
    box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.05);
}

[data-theme="dark"] input:focus,
[data-theme="dark"] textarea:focus,
[data-theme="dark"] select:focus {
    box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.05);
}

.input-with-icon { position: relative; }

.input-with-icon input { padding-right: 38px; }

.input-icon-btn {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    background: transparent;
    border: none;
    color: var(--fg-muted);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.input-icon-btn:hover { color: var(--fg); }

.checkbox-row {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 500;
    font-size: 14px;
    cursor: pointer;
}

.btn-primary {
    background: var(--primary);
    color: var(--primary-fg);
    border: none;
    border-radius: var(--radius);
    padding: 11px 18px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
}

.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:disabled { opacity: 0.6; cursor: not-allowed; }

.btn-block { width: 100%; }

.error-msg {
    background: var(--danger-bg);
    color: var(--danger-fg);
    border-radius: var(--radius-sm);
    padding: 10px 12px;
    font-size: 13px;
    margin-bottom: 12px;
}

.success-msg {
    background: var(--success-bg);
    color: var(--success-fg);
    border-radius: var(--radius-sm);
    padding: 10px 12px;
    font-size: 13px;
    margin-bottom: 12px;
}

/* ===========================================================
   APP SHELL
   =========================================================== */
.app-shell {
    display: flex;
    height: 100vh;
    width: 100%;
}

/* ---------- Sidebar ---------- */
.sidebar {
    width: 280px;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    padding: 12px;
    transition: width 0.2s, padding 0.2s;
}

.sidebar.collapsed { width: 0; padding: 0; overflow: hidden; border-right: none; }

.sidebar-top {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 8px;
}

.icon-btn {
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    color: var(--fg-muted);
    padding: 8px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.icon-btn:hover {
    background: var(--bg-hover);
    color: var(--fg);
}

.icon-btn.ghost { color: var(--fg-subtle); }
.icon-btn.ghost:hover { color: var(--fg); background: var(--bg-hover); }

.new-chat-btn {
    width: 100%;
    padding: 10px 14px;
    background: transparent;
    color: var(--fg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: background 0.15s, border-color 0.15s;
    margin-bottom: 14px;
}

.new-chat-btn:hover {
    background: var(--bg-hover);
    border-color: var(--border-strong);
}

.sidebar-section-label {
    font-size: 11px;
    color: var(--fg-subtle);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-weight: 600;
    padding: 4px 6px;
    margin-top: 4px;
    margin-bottom: 4px;
}

.chat-list {
    flex: 1;
    overflow-y: auto;
    padding-right: 2px;
}

.chat-item {
    padding: 8px 10px;
    margin-bottom: 2px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background 0.15s;
    display: flex;
    align-items: center;
    gap: 6px;
}

.chat-item:hover { background: var(--bg-hover); }
.chat-item.active { background: var(--bg-active); }

.chat-item-title {
    flex: 1;
    font-size: 13px;
    color: var(--fg);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-item-delete {
    background: transparent;
    border: none;
    color: var(--fg-subtle);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    opacity: 0;
    transition: opacity 0.15s, color 0.15s, background 0.15s;
    flex-shrink: 0;
}

.chat-item:hover .chat-item-delete,
.chat-item.active .chat-item-delete {
    opacity: 1;
}

.chat-item-delete:hover {
    background: var(--danger-bg);
    color: var(--danger);
}

/* ---------- Main column ---------- */
.main-col {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    overflow: hidden;
    min-width: 0;
}

.app-header {
    padding: 14px 20px;
    border-bottom: 1px solid var(--border);
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 12px;
    background: var(--bg);
}

.sidebar-show-btn { grid-column: 1; }

.app-title {
    grid-column: 2;
    text-align: center;
    font-size: 15px;
    font-weight: 600;
    color: var(--fg);
}

.header-right {
    grid-column: 3;
    display: flex;
    align-items: center;
    gap: 6px;
}

/* User menu */
.user-menu { position: relative; }

.user-menu-trigger {
    background: transparent;
    border: 1px solid transparent;
    color: var(--fg);
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    font-weight: 500;
}

.user-menu-trigger:hover { background: var(--bg-hover); }

.user-menu-dropdown {
    position: absolute;
    right: 0;
    top: calc(100% + 6px);
    min-width: 220px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    padding: 6px;
    z-index: 100;
    display: none;
}

.user-menu-dropdown.show { display: block; }

.user-menu-header {
    padding: 10px 12px 8px;
    font-weight: 600;
    font-size: 14px;
    color: var(--fg);
    border-bottom: 1px solid var(--border);
    margin-bottom: 4px;
}

.user-menu-item {
    width: 100%;
    background: transparent;
    border: none;
    color: var(--fg);
    text-align: left;
    padding: 9px 10px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: background 0.1s;
}

.user-menu-item:hover { background: var(--bg-hover); }
.user-menu-item.danger { color: var(--danger); }
.user-menu-item.danger:hover { background: var(--danger-bg); }

/* ---------- Chat area ---------- */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 32px 20px 12px;
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.chat-messages > .empty-state { margin: auto; text-align: center; }

.empty-state h2 {
    font-size: 28px;
    font-weight: 600;
    color: var(--fg);
    margin-bottom: 10px;
    letter-spacing: -0.02em;
}

.empty-state .muted { font-size: 15px; }

/* Conversation turns (no bubble look) */
.message {
    max-width: 820px;
    margin: 0 auto;
    width: 100%;
    animation: fadeUp 0.2s ease-out;
}

@keyframes fadeUp {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

.message-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
    font-size: 13px;
    color: var(--fg-muted);
}

.message-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--border-strong);
    flex-shrink: 0;
}

.message.assistant .message-dot { background: var(--accent); }

.message-author { font-weight: 600; color: var(--fg); }

.message-content {
    color: var(--fg);
    font-size: 15px;
    line-height: 1.65;
    word-wrap: break-word;
    padding-left: 18px;
}

.message-content pre {
    background: var(--bg-code);
    border: 1px solid var(--border);
    padding: 12px;
    border-radius: var(--radius-sm);
    overflow-x: auto;
    margin: 10px 0;
    font-size: 13px;
}

.message-content code {
    background: var(--bg-code);
    padding: 1px 6px;
    border-radius: 4px;
    font-family: ui-monospace, "SF Mono", Menlo, monospace;
    font-size: 13px;
}

.message-content pre code {
    padding: 0;
    background: transparent;
    border-radius: 0;
    font-size: 13px;
    display: block;
}

/* Markdown rendering inside a message bubble */
.message-content > *:first-child { margin-top: 0; }
.message-content > *:last-child  { margin-bottom: 0; }

.message-content h1,
.message-content h2,
.message-content h3,
.message-content h4,
.message-content h5,
.message-content h6 {
    margin: 18px 0 8px;
    font-weight: 700;
    line-height: 1.3;
    color: var(--fg);
    letter-spacing: -0.01em;
}
.message-content h1 { font-size: 1.5em; }
.message-content h2 { font-size: 1.3em; }
.message-content h3 { font-size: 1.15em; }
.message-content h4 { font-size: 1em; }

.message-content p { margin: 8px 0; }

.message-content ul,
.message-content ol {
    margin: 8px 0;
    padding-left: 22px;
}

.message-content ul { list-style: disc; }
.message-content ol { list-style: decimal; }

.message-content li { margin: 4px 0; }

.message-content li > p { margin: 0; }
.message-content li > ul,
.message-content li > ol { margin: 4px 0; }

.message-content hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 18px 0;
}

.message-content blockquote {
    margin: 10px 0;
    padding: 6px 14px;
    border-left: 3px solid var(--border-strong);
    color: var(--fg-muted);
    background: var(--bg-hover);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.message-content a {
    color: var(--accent);
    text-decoration: underline;
    text-underline-offset: 2px;
}

.message-content a:hover { opacity: 0.85; }

.message-content table {
    border-collapse: collapse;
    margin: 12px 0;
    width: 100%;
    font-size: 14px;
}

.message-content th,
.message-content td {
    border: 1px solid var(--border);
    padding: 8px 12px;
    text-align: left;
    vertical-align: top;
}

.message-content th {
    background: var(--bg-hover);
    font-weight: 600;
}

.message-content img {
    max-width: 100%;
    border-radius: var(--radius-sm);
    margin: 8px 0;
}

.streaming-cursor {
    display: inline-block;
    width: 7px;
    height: 1em;
    background: var(--fg-muted);
    vertical-align: text-bottom;
    margin-left: 2px;
    animation: blink 1s infinite;
}

@keyframes blink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0; }
}

/* ---------- Composer ---------- */
.composer-wrap {
    padding: 8px 20px 18px;
    border-top: 1px solid var(--border);
    background: var(--bg);
}

.composer {
    max-width: 820px;
    margin: 0 auto;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 18px;
    padding: 10px 12px;
    display: flex;
    align-items: flex-end;
    gap: 6px;
    box-shadow: var(--shadow-sm);
    transition: border-color 0.15s;
}

.composer:focus-within { border-color: var(--border-strong); }

#userInput {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--fg);
    font-size: 15px;
    resize: none;
    max-height: 200px;
    padding: 6px 4px;
    outline: none;
    box-shadow: none;
}

#userInput:focus { box-shadow: none; }

.composer-actions {
    display: flex;
    align-items: center;
    gap: 4px;
}

.icon-btn-circle {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, transform 0.05s;
}

.icon-btn-circle.send {
    background: var(--primary);
    color: var(--primary-fg);
}

.icon-btn-circle.send:hover { background: var(--primary-hover); }
.icon-btn-circle.send:disabled { opacity: 0.5; cursor: not-allowed; }

.icon-btn-circle.stop {
    background: var(--danger);
    color: white;
}

.composer-footnote {
    text-align: center;
    margin-top: 8px;
    font-size: 12px;
}

/* ===========================================================
   MODALS
   =========================================================== */
.modal {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1000;
    background: rgba(0, 0, 0, 0.45);
    align-items: center;
    justify-content: center;
    padding: 20px;
    animation: fadeIn 0.15s ease-out;
}

[data-theme="dark"] .modal { background: rgba(0, 0, 0, 0.65); }

.modal.show { display: flex; }

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
    background: var(--bg-elevated);
    color: var(--fg);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 500px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
    animation: slideUp 0.2s ease-out;
}

.modal-content.modal-wide { max-width: 760px; }

@keyframes slideUp {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}

.modal-header {
    padding: 18px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-header h2 {
    font-size: 17px;
    font-weight: 700;
    color: var(--fg);
}

.close-btn {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--fg-muted);
    line-height: 1;
    padding: 0 4px;
}

.close-btn:hover { color: var(--fg); }

.modal-body { padding: 20px; }

.modal-footer {
    padding: 16px 20px;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
}

/* ===========================================================
   USERS TABLE (Admin Panel)
   =========================================================== */
.section-heading {
    font-size: 13px;
    font-weight: 600;
    color: var(--fg);
    margin: 0 0 10px 0;
    padding-bottom: 6px;
    border-bottom: 1px solid var(--border);
}

.section-heading:not(:first-child) { margin-top: 24px; }

.add-user-form {
    display: grid;
    grid-template-columns: 1.5fr 1.5fr 1fr auto;
    gap: 8px;
}

.add-user-form input,
.add-user-form select {
    padding: 9px 10px;
    font-size: 13px;
}

.add-user-form button {
    padding: 9px 16px;
    font-size: 13px;
}

.users-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

.users-table th,
.users-table td {
    padding: 10px 8px;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

.users-table th {
    color: var(--fg-muted);
    font-weight: 600;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.users-table td { color: var(--fg); vertical-align: middle; }

.users-table select {
    padding: 4px 8px;
    font-size: 12px;
    width: auto;
}

.row-actions { display: flex; gap: 6px; }

.btn-small {
    padding: 5px 10px;
    font-size: 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    border: 1px solid var(--border);
    background: transparent;
    color: var(--fg);
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.btn-small:hover {
    background: var(--bg-hover);
    border-color: var(--border-strong);
}

.btn-small.danger { color: var(--danger); }

.btn-small.danger:hover {
    background: var(--danger);
    border-color: var(--danger);
    color: white;
}

.btn-small:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ===========================================================
   SCROLLBARS
   =========================================================== */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 6px;
    border: 2px solid transparent;
    background-clip: padding-box;
}
::-webkit-scrollbar-thumb:hover { background-color: var(--border-strong); background-clip: padding-box; }

/* ===========================================================
   RESPONSIVE
   =========================================================== */
@media (max-width: 768px) {
    .sidebar { width: 240px; }
    .app-title { font-size: 13px; }
}

@media (max-width: 640px) {
    .sidebar {
        position: absolute;
        left: -300px;
        z-index: 200;
        height: 100vh;
        transition: left 0.2s;
    }
    .sidebar.open { left: 0; box-shadow: var(--shadow-lg); }
    .app-title { display: none; }
}
