:root {
    --bg-primary: #212121;
    --bg-secondary: #171717;
    --bg-tertiary: #2f2f2f;
    --bg-sidebar: #171717;
    --bg-hover: #2f2f2f;
    --bg-input: #2f2f2f;
    --bg-code: #1e1e1e;
    --bg-code-header: #2b2b2b;
    --text-primary: #ececec;
    --text-secondary: #b4b4b4;
    --text-muted: #8e8e8e;
    --text-placeholder: #8e8e8e;
    --border: #383838;
    --border-light: #444;
    --accent: #10a37f;
    --accent-hover: #0e9272;
    --accent-light: rgba(16,163,127,0.15);
    --danger: #ef4444;
    --success: #10a37f;
    --warning: #f59e0b;
    --radius: 12px;
    --radius-lg: 16px;
    --radius-xl: 24px;
    --shadow: 0 2px 6px rgba(0,0,0,0.15);
    --max-width: 48rem;
    --font: 'Söhne', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
}

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

body {
    font-family: var(--font);
    background: var(--bg-primary);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
}

#app {
    height: 100vh;
    display: flex;
    flex-direction: column;
}

/* === Scrollbar === */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.2); }

/* === Login === */
.login-container {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-secondary);
}

.login-card {
    background: var(--bg-primary);
    padding: 48px 40px;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    width: 380px;
    max-width: 90vw;
}

.login-card h1 {
    color: var(--text-primary);
    font-size: 28px;
    text-align: center;
    margin-bottom: 8px;
    font-weight: 600;
}

.login-card .subtitle {
    color: var(--text-muted);
    text-align: center;
    margin-bottom: 32px;
    font-size: 14px;
}

.login-card .form-group {
    margin-bottom: 20px;
}

.login-card label {
    display: block;
    color: var(--text-secondary);
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 6px;
}

.login-card input {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 14px;
    transition: border-color 0.2s;
    color: var(--text-primary);
    background: var(--bg-input);
    font-family: var(--font);
}

.login-card input:focus {
    outline: none;
    border-color: var(--accent);
}

.login-card .btn-login {
    width: 100%;
    padding: 12px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
    font-family: var(--font);
}

.login-card .btn-login:hover { background: var(--accent-hover); }

.login-error {
    color: var(--danger);
    text-align: center;
    font-size: 13px;
    margin-top: 12px;
    min-height: 18px;
}

/* === Main Layout === */
.main-layout {
    display: flex;
    height: 100vh;
}

/* === Sidebar === */
.sidebar {
    width: 260px;
    min-width: 260px;
    background: var(--bg-sidebar);
    display: flex;
    flex-direction: column;
    height: 100vh;
}

.sidebar-header {
    padding: 12px 12px 8px;
}

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

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

.conversations-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 8px;
}

.conv-group-label {
    padding: 8px 12px 4px;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.conv-item {
    padding: 8px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: space-between;
    transition: background 0.1s;
    margin-bottom: 1px;
    line-height: 1.4;
}

.conv-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.conv-item.active { background: var(--bg-hover); color: var(--text-primary); }

.conv-item .conv-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
}

.conv-item .conv-delete {
    opacity: 0;
    color: var(--text-muted);
    cursor: pointer;
    padding: 2px 6px;
    font-size: 16px;
    transition: opacity 0.1s;
    border-radius: 4px;
}

.conv-item:hover .conv-delete { opacity: 1; }
.conv-item .conv-delete:hover { color: var(--danger); background: rgba(239,68,68,0.1); }

.sidebar-footer {
    padding: 8px 12px 12px;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.sidebar-footer button {
    width: 100%;
    padding: 8px 12px;
    background: transparent;
    color: var(--text-secondary);
    border: none;
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
    transition: background 0.1s;
    text-align: left;
    font-family: var(--font);
}

.sidebar-footer button:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

/* === Chat Area === */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100vh;
    min-width: 0;
    background: var(--bg-primary);
}

/* === Top Bar (model selector) === */
.chat-topbar {
    padding: 8px 16px;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
    height: 48px;
}

.model-dropdown {
    position: relative;
}

.model-dropdown-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: transparent;
    color: var(--text-primary);
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    font-family: var(--font);
    transition: background 0.1s;
}

.model-dropdown-btn:hover { background: var(--bg-hover); }

.model-dropdown-btn .chevron {
    font-size: 10px;
    color: var(--text-muted);
    transition: transform 0.15s;
}

.model-dropdown-btn.open .chevron { transform: rotate(180deg); }

.model-dropdown-menu {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    min-width: 280px;
    max-height: 400px;
    overflow-y: auto;
    z-index: 100;
    box-shadow: 0 8px 32px rgba(0,0,0,0.4);
    padding: 4px;
}

.model-dropdown-menu.open { display: block; }

.model-dropdown-category {
    padding: 8px 12px 4px;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.model-dropdown-item {
    padding: 8px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background 0.1s;
}

.model-dropdown-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.model-dropdown-item.active { background: var(--accent-light); color: var(--accent); }

.model-dropdown-item .model-check {
    width: 16px;
    text-align: center;
    font-size: 12px;
    color: var(--accent);
}

/* Hide old tabs and selector */
.category-tabs { display: none; }
.model-selector { display: none; }

/* === Messages === */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 0;
}

.message {
    padding: 24px 0;
    max-width: var(--max-width);
    margin: 0 auto;
    width: 100%;
    padding-left: 24px;
    padding-right: 24px;
}

.message-avatar {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 600;
    flex-shrink: 0;
}

.message-avatar.user {
    background: #9c5aff;
    color: white;
}

.message-avatar.assistant {
    background: var(--accent);
    color: white;
}

.message-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
}

.message-role {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
}

.message-body {
    flex: 1;
    min-width: 0;
}

.message-content {
    font-size: 15px;
    line-height: 1.7;
    color: var(--text-primary);
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.message-content p { margin-bottom: 12px; }
.message-content p:last-child { margin-bottom: 0; }

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

.message-content li { margin-bottom: 4px; line-height: 1.7; }

.message-content strong { font-weight: 600; }
.message-content em { font-style: italic; }

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

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

/* Code blocks */
.message-content pre {
    background: var(--bg-code);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0;
    margin: 16px 0;
    overflow: hidden;
}

.code-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    background: var(--bg-code-header);
    font-size: 12px;
    color: var(--text-muted);
}

.code-header .btn-copy {
    padding: 4px 10px;
    background: transparent;
    color: var(--text-muted);
    border: none;
    border-radius: 6px;
    font-size: 12px;
    cursor: pointer;
    transition: all 0.15s;
    font-family: var(--font);
    display: flex;
    align-items: center;
    gap: 4px;
}

.code-header .btn-copy:hover {
    background: rgba(255,255,255,0.1);
    color: var(--text-primary);
}

.message-content pre code {
    display: block;
    padding: 16px;
    overflow-x: auto;
    font-family: 'Söhne Mono', 'Fira Code', 'Consolas', 'Monaco', monospace;
    font-size: 13px;
    line-height: 1.6;
    color: #e6edf3;
}

.message-content code {
    background: var(--bg-code);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Söhne Mono', 'Fira Code', 'Consolas', monospace;
    font-size: 13px;
    color: #e6edf3;
}

.message-content pre code { background: none; padding: 0; border-radius: 0; }

.message-content blockquote {
    border-left: 3px solid var(--border-light);
    padding-left: 16px;
    margin: 12px 0;
    color: var(--text-secondary);
}

.message-content table {
    border-collapse: collapse;
    margin: 16px 0;
    width: 100%;
}

.message-content th, .message-content td {
    border: 1px solid var(--border);
    padding: 10px 14px;
    text-align: left;
    font-size: 14px;
}

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

/* Streaming cursor */
.streaming-cursor::after {
    content: '';
    display: inline-block;
    width: 7px;
    height: 18px;
    background: var(--text-primary);
    margin-left: 2px;
    vertical-align: text-bottom;
    animation: blink 1s infinite;
}

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

/* Welcome screen */
.welcome {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    min-height: 60vh;
}

.welcome h2 {
    font-size: 28px;
    color: var(--text-primary);
    margin-bottom: 12px;
    font-weight: 600;
}

.welcome p {
    color: var(--text-muted);
    font-size: 16px;
    max-width: 400px;
    line-height: 1.5;
}

/* === Input Area === */
.input-area {
    padding: 0 24px 24px;
    flex-shrink: 0;
}

.input-wrapper {
    max-width: var(--max-width);
    margin: 0 auto;
    position: relative;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    display: flex;
    align-items: flex-end;
    padding: 4px 4px 4px 16px;
    transition: border-color 0.15s;
}

.input-wrapper:focus-within {
    border-color: var(--border-light);
}

.input-wrapper textarea {
    flex: 1;
    padding: 10px 0;
    background: transparent;
    color: var(--text-primary);
    border: none;
    font-size: 15px;
    font-family: var(--font);
    resize: none;
    min-height: 24px;
    max-height: 200px;
    line-height: 1.5;
}

.input-wrapper textarea::placeholder {
    color: var(--text-placeholder);
}

.input-wrapper textarea:focus {
    outline: none;
}

.btn-send {
    width: 36px;
    height: 36px;
    background: white;
    color: var(--bg-primary);
    border: none;
    border-radius: 50%;
    font-size: 16px;
    cursor: pointer;
    transition: opacity 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.btn-send:hover { opacity: 0.85; }
.btn-send:disabled { opacity: 0.3; cursor: not-allowed; }

.btn-stop {
    width: 36px;
    height: 36px;
    background: white;
    color: var(--bg-primary);
    border: none;
    border-radius: 50%;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.btn-stop:hover { opacity: 0.85; }

.input-footer {
    text-align: center;
    padding-top: 8px;
    font-size: 12px;
    color: var(--text-muted);
    max-width: var(--max-width);
    margin: 0 auto;
}

/* === Admin Panel === */
.admin-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    background: var(--bg-primary);
}

.admin-header {
    padding: 16px 24px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.admin-header h1 {
    font-size: 18px;
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
}

.admin-tabs {
    display: flex;
    gap: 0;
    padding: 0 24px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
}

.admin-tab {
    padding: 10px 20px;
    background: transparent;
    color: var(--text-secondary);
    border: none;
    border-bottom: 2px solid transparent;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.1s;
    font-family: var(--font);
}

.admin-tab:hover { color: var(--text-primary); }
.admin-tab.active { color: var(--text-primary); border-bottom-color: var(--text-primary); }

.admin-content {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
}

.admin-section { display: none; }
.admin-section.active { display: block; }

/* Tables */
.data-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 16px;
}

.data-table th, .data-table td {
    padding: 12px 16px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    font-size: 14px;
}

.data-table th {
    color: var(--text-muted);
    font-weight: 600;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.data-table tr:hover { background: var(--bg-hover); }

/* Buttons */
.btn {
    padding: 8px 16px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.1s;
    font-family: var(--font);
}

.btn:hover { background: var(--bg-hover); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-danger { color: var(--danger); border-color: transparent; background: rgba(239,68,68,0.1); }
.btn-danger:hover { background: rgba(239,68,68,0.2); }
.btn-sm { padding: 4px 10px; font-size: 12px; }

/* Forms */
.form-row {
    display: flex;
    gap: 12px;
    margin-bottom: 12px;
    align-items: flex-end;
}

.form-row .form-group { flex: 1; }

.form-group label {
    display: block;
    font-size: 13px;
    color: var(--text-muted);
    font-weight: 500;
    margin-bottom: 4px;
}

.form-group input, .form-group select {
    width: 100%;
    padding: 8px 12px;
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 14px;
    font-family: var(--font);
}

.form-group input:focus, .form-group select:focus {
    outline: none;
    border-color: var(--accent);
}

/* Modal */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    backdrop-filter: blur(2px);
}

.modal {
    background: var(--bg-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 24px;
    min-width: 400px;
    max-width: 90vw;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}

.modal h3 {
    margin-bottom: 16px;
    font-size: 18px;
    font-weight: 600;
}

.modal-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    margin-top: 20px;
}

/* Checkbox grid */
.checkbox-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 8px;
    margin-top: 12px;
}

.checkbox-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    background: var(--bg-secondary);
    border-radius: 8px;
    font-size: 13px;
    cursor: pointer;
}

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

.checkbox-item input[type="checkbox"] {
    accent-color: var(--accent);
}

/* Status badge */
.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 500;
}

.badge-active { background: rgba(16,163,127,0.15); color: var(--success); }
.badge-inactive { background: rgba(239,68,68,0.15); color: var(--danger); }
.badge-admin { background: rgba(156,90,255,0.15); color: #9c5aff; }

/* Permission matrix */
.perm-matrix { margin-top: 16px; }

.perm-row {
    display: flex;
    align-items: center;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    gap: 16px;
}

.perm-row .perm-user {
    width: 150px;
    font-weight: 500;
    font-size: 14px;
}

.perm-row .perm-cats {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

/* === ImageGen Tabs === */
.ig-tab {
    padding: 8px 20px;
    background: transparent;
    color: var(--text-secondary);
    border: none;
    border-bottom: 2px solid transparent;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.1s;
    font-family: var(--font);
}

.ig-tab:hover { color: var(--text-primary); }
.ig-tab.active { color: var(--text-primary); border-bottom-color: var(--text-primary); }

/* === Options Toggle Button === */
.btn-options-toggle {
    padding: 6px 8px;
    background: transparent;
    color: var(--text-muted);
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.1s;
    display: flex;
    align-items: center;
    margin-left: auto;
}

.btn-options-toggle:hover { background: var(--bg-hover); color: var(--text-primary); }
.btn-options-toggle.active { color: var(--text-primary); background: var(--bg-hover); }

/* === Options Panel === */
.options-panel {
    padding: 12px 24px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
    flex-shrink: 0;
    animation: slideDown 0.15s ease-out;
}

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

.options-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 16px;
    max-width: var(--max-width);
    margin: 0 auto;
}

.option-item {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.option-label {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 13px;
    color: var(--text-secondary);
    font-weight: 500;
}

.option-value {
    font-size: 13px;
    color: var(--text-primary);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    min-width: 40px;
    text-align: right;
}

.option-range {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 4px;
    background: var(--border);
    border-radius: 2px;
    outline: none;
    cursor: pointer;
}

.option-range::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--text-primary);
    cursor: pointer;
    border: none;
    box-shadow: 0 1px 3px rgba(0,0,0,0.3);
    transition: transform 0.1s;
}

.option-range::-webkit-slider-thumb:hover {
    transform: scale(1.15);
}

.option-range::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--text-primary);
    cursor: pointer;
    border: none;
    box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

.option-hint {
    font-size: 11px;
    color: var(--text-muted);
    line-height: 1.3;
}

.btn-reset-options {
    padding: 6px 14px;
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 12px;
    cursor: pointer;
    transition: all 0.1s;
    font-family: var(--font);
    white-space: nowrap;
}

.btn-reset-options:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

/* Responsive */
@media (max-width: 768px) {
    .sidebar { display: none; }
    .sidebar.open {
        display: flex;
        position: fixed;
        z-index: 100;
        width: 100%;
        top: 0;
        left: 0;
    }
    .message {
        padding-left: 16px;
        padding-right: 16px;
    }
    .input-area {
        padding: 0 12px 16px;
    }
}
