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

:root {
    --bg: #0f1117;
    --surface: #1a1d27;
    --surface2: #242836;
    --border: #2e3345;
    --text: #e4e6ed;
    --text2: #8b8fa3;
    --primary: #6366f1;
    --primary-hover: #5558e6;
    --danger: #ef4444;
    --success: #22c55e;
    --warning: #f59e0b;
    --info: #3b82f6;
    --radius: 10px;
    --radius-sm: 6px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans SC", sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
}

a { color: var(--primary); text-decoration: none; }

/* navbar */
.navbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0 24px;
}
.nav-inner {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 56px;
}
.nav-brand {
    font-size: 18px;
    font-weight: 700;
    color: var(--text);
    letter-spacing: -.5px;
}
.nav-user {
    margin-right: 12px;
    color: var(--text2);
    font-size: 14px;
}

/* container */
.container {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 24px;
}

/* login card */
.login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 40px 36px;
    width: 100%;
    max-width: 400px;
}
.logo {
    text-align: center;
    font-size: 28px;
    font-weight: 800;
    letter-spacing: -1px;
    margin-bottom: 4px;
}
.subtitle {
    text-align: center;
    color: var(--text2);
    font-size: 14px;
    margin-bottom: 28px;
}

/* forms */
.login-form { }
.form-group { margin-bottom: 16px; }
.form-group label {
    display: block;
    margin-bottom: 6px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text2);
}
.form-group input,
.form-group textarea,
.form-group select {
    width: 100%;
    padding: 10px 14px;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 14px;
    font-family: inherit;
    outline: none;
    transition: border-color .2s;
}
.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
    border-color: var(--primary);
}
.form-group textarea { resize: vertical; min-height: 70px; }

/* buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 20px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: all .2s;
    gap: 6px;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-outline:hover:not(:disabled) { background: var(--surface2); border-color: var(--text2); }
.btn-danger { background: var(--danger); color: #fff; border: none; }
.btn-danger:hover:not(:disabled) { opacity: 0.85; }
.btn-full { width: 100%; }
.btn-sm { padding: 6px 14px; font-size: 13px; }

/* alerts */
.alert {
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    margin-bottom: 16px;
    font-size: 13px;
}
.alert-danger { background: rgba(239, 68, 68, .12); color: var(--danger); border: 1px solid rgba(239, 68, 68, .3); }
.alert-success { background: rgba(34, 197, 94, .12); color: var(--success); border: 1px solid rgba(34, 197, 94, .3); }
.alert-warning { background: rgba(245, 158, 11, .12); color: var(--warning); border: 1px solid rgba(245, 158, 11, .3); }
.alert-info { background: rgba(59, 130, 246, .12); color: var(--info); border: 1px solid rgba(59, 130, 246, .3); }

/* flash */
.flash-container {
    max-width: 1200px;
    margin: 12px auto;
    padding: 0 24px;
}

/* main */
.main-content {
    max-width: 1200px;
    margin: 0 auto;
    padding: 24px;
}

/* card */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
    height: 100%;
}
.card h2 {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 6px;
}
.hint {
    font-size: 13px;
    color: var(--text2);
    margin-bottom: 18px;
}

/* token section */
.token-section { margin-bottom: 20px; }
.token-row {
    display: flex;
    gap: 8px;
    align-items: center;
}
.input-token {
    flex: 1;
    padding: 10px 14px;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 14px;
    font-family: monospace;
    outline: none;
}
.input-token:focus { border-color: var(--primary); }
.token-options {
    display: flex;
    align-items: center;
    gap: 14px;
    margin-top: 10px;
}
.checkbox-label {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--text2);
    cursor: pointer;
    user-select: none;
}
.checkbox-label input[type="checkbox"] {
    width: 15px;
    height: 15px;
    accent-color: var(--primary);
    cursor: pointer;
}
.status-text { font-size: 13px; color: var(--success); margin-top: 8px; display: block; }
.status-text.error { color: var(--danger); }
.status-text.warning { color: var(--warning); }

/* workspace */
.workspace {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 20px;
}
@media (max-width: 768px) {
    .workspace { grid-template-columns: 1fr; }
}

/* file upload */
.file-upload-area {
    position: relative;
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    padding: 30px;
    text-align: center;
    cursor: pointer;
    transition: border-color .2s, background .2s;
    overflow: hidden;
}
.file-upload-area:hover,
.file-upload-area.dragover {
    border-color: var(--primary);
    background: rgba(99, 102, 241, .05);
}
.file-upload-area input[type="file"] {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
}
.drop-text { color: var(--text2); font-size: 13px; }

.preview-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 8px;
    margin-top: 12px;
}
.preview-item {
    position: relative;
    border-radius: var(--radius-sm);
    overflow: hidden;
    aspect-ratio: 1;
    background: var(--surface2);
    border: 1px solid var(--border);
}
.preview-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}
.preview-index {
    position: absolute;
    top: 4px;
    left: 4px;
    background: var(--primary);
    color: #fff;
    font-size: 11px;
    font-weight: 600;
    padding: 1px 6px;
    border-radius: 3px;
    z-index: 2;
}
.preview-remove {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 22px;
    height: 22px;
    border: none;
    background: rgba(0,0,0,.55);
    color: #fff;
    border-radius: 50%;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}
.preview-remove:hover {
    background: rgba(220,38,38,.85);
}

/* result */
.result-section { margin-bottom: 20px; }
.result-area {
    text-align: center;
    min-height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--surface2);
    border-radius: var(--radius-sm);
    margin-bottom: 16px;
    overflow: hidden;
}
.result-area img {
    max-width: 100%;
    max-height: 600px;
    object-fit: contain;
}
.placeholder-text { color: var(--text2); font-size: 14px; }
.result-actions { display: flex; gap: 10px; justify-content: center; }

/* spinner */
.spinner {
    display: inline-block;
    width: 18px;
    height: 18px;
    border: 2px solid rgba(255,255,255,.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin .6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* credits badge */
.credits-badge {
    display: inline-flex;
    align-items: center;
    padding: 4px 12px;
    background: rgba(99, 102, 241, .15);
    border: 1px solid rgba(99, 102, 241, .3);
    border-radius: 20px;
    font-size: 13px;
    font-weight: 500;
    color: var(--primary);
    margin-right: 12px;
}
.credits-badge.credits-low {
    background: rgba(239, 68, 68, .12);
    border-color: rgba(239, 68, 68, .3);
    color: var(--danger);
}

/* credit request */
.credit-request-section { margin-bottom: 20px; }

/* form footer */
.form-footer {
    text-align: center;
    margin-top: 18px;
    font-size: 13px;
    color: var(--text2);
}

/* page title */
.page-title {
    font-size: 22px;
    font-weight: 700;
    margin-bottom: 20px;
}

/* admin tabs */
.admin-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 20px;
    border-bottom: 1px solid var(--border);
    padding-bottom: 0;
}
.tab-btn {
    position: relative;
    padding: 10px 20px;
    background: none;
    border: none;
    color: var(--text2);
    font-size: 14px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
    transition: all .2s;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}
.tab-btn .badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    background: var(--danger);
    color: #fff;
    border-radius: 9px;
    font-size: 11px;
    margin-left: 6px;
}

/* tables */
.table-wrap {
    overflow-x: auto;
}
.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}
.data-table th {
    text-align: left;
    padding: 10px 14px;
    color: var(--text2);
    font-weight: 500;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
.data-table td {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
.data-table tbody tr:hover {
    background: var(--surface2);
}
.data-table .loading {
    text-align: center;
    color: var(--text2);
    padding: 30px;
}

/* role tags */
.role-tag {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
}
.role-admin { background: rgba(99, 102, 241, .15); color: var(--primary); }
.role-user { background: rgba(139, 143, 163, .12); color: var(--text2); }

/* delta colors */
.delta-positive { color: var(--success); font-weight: 600; }
.delta-negative { color: var(--danger); font-weight: 600; }

/* admin panel */
.admin-panel { }

/* responsive */
@media (max-width: 768px) {
    .admin-tabs { flex-wrap: wrap; }
    .nav-inner { flex-wrap: wrap; gap: 8px; height: auto; padding: 10px 0; }
}

/* ===== Chat Section ===== */
.chat-section { margin-bottom: 20px; }

.chat-messages {
    max-height: 500px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px;
    margin-bottom: 14px;
    background: var(--surface2);
    border-radius: var(--radius-sm);
    min-height: 120px;
    scroll-behavior: smooth;
}

.chat-empty-hint {
    color: var(--text2);
    font-size: 14px;
    text-align: center;
    padding: 40px 0;
}

.chat-message {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.65;
    white-space: pre-wrap;
    word-break: break-word;
    animation: msgIn .2s ease-out;
}

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

.chat-message.user {
    align-self: flex-end;
    background: var(--primary);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.chat-message.assistant {
    align-self: flex-start;
    background: var(--surface);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.chat-message .msg-role {
    font-size: 11px;
    font-weight: 600;
    margin-bottom: 4px;
    opacity: .7;
    text-transform: uppercase;
    letter-spacing: .5px;
}

.chat-input-row {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

.chat-input-row textarea {
    flex: 1;
    resize: none;
}

.chat-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 8px;
}

/* History */
.history-section {
    margin-top: 20px;
}

.history-section .badge {
    background: var(--primary);
    color: #fff;
    font-size: 12px;
    padding: 2px 8px;
    border-radius: 10px;
    vertical-align: middle;
}

.history-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 12px;
}

.history-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    transition: border-color .2s, transform .2s;
}

.history-card:hover {
    border-color: var(--primary);
    transform: translateY(-2px);
}

.history-card-thumb {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
    background: var(--bg);
    display: block;
}

.history-card-body {
    padding: 8px;
}

.history-card-type {
    font-size: 11px;
    color: var(--primary);
    font-weight: 600;
    text-transform: uppercase;
    margin-bottom: 4px;
}

.history-card-prompt {
    font-size: 12px;
    color: var(--text);
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.history-card-time {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 4px;
}

.history-card-status {
    display: inline-block;
    font-size: 10px;
    padding: 2px 6px;
    border-radius: 3px;
    margin-top: 4px;
}
.history-card-status.status-done    { background: #e8f5e9; color: #2e7d32; }
.history-card-status.status-failed  { background: #ffebee; color: #c62828; }
.history-card-status.status-running { background: #e3f2fd; color: #1565c0; }
.history-card-status.status-pending { background: #fff3e0; color: #e65100; }

.history-actions {
    margin-top: 12px;
    text-align: center;
}

/* History modal */
.history-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .7);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.history-modal {
    background: var(--card-bg);
    border-radius: 12px;
    max-width: 90vw;
    max-height: 90vh;
    overflow: auto;
    padding: 20px;
    position: relative;
}

.history-modal img {
    max-width: 100%;
    max-height: 70vh;
    display: block;
    border-radius: 6px;
}

.history-modal-prompt {
    margin-top: 12px;
    font-size: 14px;
    color: var(--text);
    line-height: 1.5;
}

.history-modal-close {
    position: absolute;
    top: 10px;
    right: 10px;
    background: var(--bg);
    border: none;
    color: var(--text);
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.history-modal-close:hover {
    background: var(--border);
}
