@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap');

:root {
    --primary: #7c3aed;
    --primary-light: #a78bfa;
    --primary-dark: #5b21b6;
    --accent: #ec4899;
    --accent-light: #f472b6;
    --bg: #0f0f1a;
    --bg-card: #1a1a2e;
    --bg-card-hover: #222240;
    --bg-input: #16162b;
    --bg-nav: #12121f;
    --text: #e2e8f0;
    --text-muted: #94a3b8;
    --text-dim: #64748b;
    --border: #2d2d50;
    --success: #10b981;
    --danger: #ef4444;
    --warning: #f59e0b;
    --radius: 14px;
    --radius-sm: 8px;
    --radius-lg: 20px;
    --nav-height: 64px;
    --header-height: 42px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'Inter', 'Noto Sans SC', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

#app {
    height: 100%;
    display: flex;
    flex-direction: column;
}

/* ── Scrollbar ──────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ── Header ─────────────────────────────────────────────────────────────── */
.app-header {
    height: var(--header-height);
    background: var(--bg-nav);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 10px;
    flex-shrink: 0;
    position: relative;
    z-index: 100;
    backdrop-filter: blur(20px);
}
.app-header .title {
    flex: 1;
    font-size: 15px;
    font-weight: 600;
    text-align: center;
    background: linear-gradient(135deg, var(--primary-light), var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}
.header-btn {
    width: 30px;
    height: 30px;
    border: none;
    background: transparent;
    color: var(--text);
    font-size: 20px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}
.header-btn:hover { background: var(--bg-card); }
.logout-btn {
    color: #c4b5fd;
    font-size: 11px;
    font-weight: 600;
    background: rgba(139,92,246,0.15);
    border: 1px solid rgba(139,92,246,0.3);
    border-radius: 20px;
    padding: 4px 12px;
    cursor: pointer;
}
.restart-chat-btn {
    width: auto;
    padding: 4px 10px;
    font-size: 12px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    gap: 4px;
    color: var(--primary-light);
    white-space: nowrap;
}
.restart-chat-btn:hover {
    background: rgba(124, 58, 237, 0.1);
    border-color: var(--primary);
}

/* ── Main Content ───────────────────────────────────────────────────────── */
.main-content {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    min-height: 0;
}

/* ── Bottom Nav ─────────────────────────────────────────────────────────── */
.bottom-nav {
    height: calc(var(--nav-height) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    background: var(--bg-nav);
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-around;
    flex-shrink: 0;
    position: relative;
    z-index: 100;
    backdrop-filter: blur(20px);
}
.nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    padding: 6px 16px;
    border: none;
    background: transparent;
    color: var(--text-dim);
    font-size: 10px;
    font-weight: 500;
    cursor: pointer;
    transition: color 0.2s;
    position: relative;
}
.nav-item .nav-icon { font-size: 22px; }
.nav-item.active {
    color: var(--primary-light);
}
.nav-item.active::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 20px;
    height: 3px;
    background: var(--primary);
    border-radius: 0 0 3px 3px;
}

/* ── Cards ──────────────────────────────────────────────────────────────── */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    transition: all 0.2s;
}
.card:hover { background: var(--bg-card-hover); }

/* ── Character Filter Bar ───────────────────────────────────────────────── */
.char-filter-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 2px 0 6px;
}
.char-filter-select {
    flex: 1;
    padding: 7px 28px 7px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-card);
    color: var(--text);
    font-size: 13px;
    font-family: inherit;
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' fill='%2394a3b8' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    outline: none;
    transition: border-color 0.2s;
}
.char-filter-select:focus { border-color: var(--primary); }
.char-filter-count {
    font-size: 11px;
    color: var(--text-dim);
    white-space: nowrap;
}

/* ── Character Card ─────────────────────────────────────────────────────── */
.char-card {
    display: flex;
    gap: 14px;
    align-items: center;
    cursor: pointer;
    padding: 14px;
}
.char-avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 26px;
    flex-shrink: 0;
    overflow: hidden;
    border: 2px solid var(--border);
}
.char-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.char-info { flex: 1; min-width: 0; }
.char-name {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.char-desc {
    font-size: 12px;
    color: var(--text-muted);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.char-tags {
    display: flex;
    gap: 6px;
    margin-top: 6px;
    flex-wrap: wrap;
}
.tag {
    font-size: 10px;
    padding: 2px 8px;
    border-radius: 10px;
    background: rgba(124, 58, 237, 0.15);
    color: var(--primary-light);
    border: 1px solid rgba(124, 58, 237, 0.2);
}

/* ── Buttons ────────────────────────────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 24px;
    border: none;
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
    white-space: nowrap;
}
.btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--accent));
    color: white;
    box-shadow: 0 4px 15px rgba(124, 58, 237, 0.3);
}
.btn-primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 20px rgba(124, 58, 237, 0.4);
}
.btn-primary:active { transform: translateY(0); }
.btn-secondary {
    background: var(--bg-card);
    color: var(--text);
    border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--bg-card-hover); }
.btn-danger {
    background: rgba(239, 68, 68, 0.15);
    color: var(--danger);
    border: 1px solid rgba(239, 68, 68, 0.2);
}
.btn-danger:hover { background: rgba(239, 68, 68, 0.25); }
.btn-sm { padding: 8px 16px; font-size: 12px; }
.btn-full { width: 100%; }
.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none !important;
}

/* ── FAB ────────────────────────────────────────────────────────────────── */
.fab {
    position: fixed;
    bottom: calc(var(--nav-height) + var(--safe-bottom) + 16px);
    right: 16px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    color: white;
    border: none;
    font-size: 28px;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(124, 58, 237, 0.4);
    z-index: 50;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s;
}
.fab:hover { transform: scale(1.1); }
.fab:active { transform: scale(0.95); }

/* ── Form Elements ──────────────────────────────────────────────────────── */
.form-group { margin-bottom: 16px; }
.form-label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-muted);
    margin-bottom: 6px;
}
.form-input,
.form-select,
.form-textarea {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    color: var(--text);
    font-size: 14px;
    font-family: inherit;
    transition: border-color 0.2s;
    outline: none;
}
.form-input:focus,
.form-select:focus,
.form-textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.1);
}
.form-textarea { resize: vertical; min-height: 80px; }
.form-select {
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%2394a3b8' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
}

/* ── Tabs ────────────────────────────────────────────────────────────────── */
.tabs-wrapper {
    position: relative;
    margin-bottom: 20px;
}
.tabs-wrapper::after {
    content: '';
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 30px;
    background: linear-gradient(to right, transparent, var(--bg-input));
    border-radius: 0 var(--radius) var(--radius) 0;
    pointer-events: none;
}
.tabs {
    display: flex;
    gap: 4px;
    padding: 4px;
    background: var(--bg-input);
    border-radius: var(--radius);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.tabs::-webkit-scrollbar { display: none; }
.tab-btn {
    flex: 0 0 auto;
    min-width: max-content;
    padding: 8px 12px;
    border: none;
    border-radius: 10px;
    background: transparent;
    color: var(--text-muted);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
    white-space: nowrap;
}
.tab-btn.active {
    background: var(--primary);
    color: white;
    box-shadow: 0 2px 8px rgba(124, 58, 237, 0.3);
}

/* ── Selection Grid ─────────────────────────────────────────────────────── */
.selection-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
    gap: 8px;
}
.selection-item {
    padding: 10px 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    color: var(--text-muted);
    font-size: 12px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
}
.selection-item:hover { border-color: var(--primary); color: var(--text); }
.selection-item.selected {
    background: rgba(124, 58, 237, 0.15);
    border-color: var(--primary);
    color: var(--primary-light);
}

/* ── Trait Chips ─────────────────────────────────────────────────────────── */
.trait-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}
.trait-chip {
    padding: 6px 14px;
    border: 1px solid var(--border);
    border-radius: 20px;
    background: var(--bg-input);
    color: var(--text-muted);
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
}
.trait-chip:hover { border-color: var(--primary); }
.trait-chip.selected {
    background: rgba(124, 58, 237, 0.2);
    border-color: var(--primary);
    color: var(--primary-light);
}

/* ── Chat ────────────────────────────────────────────────────────────────── */
.chat-container {
    display: flex;
    flex-direction: column;
    height: calc(100vh - var(--header-height) - var(--nav-height) - var(--safe-bottom));
    height: calc(100dvh - var(--header-height) - var(--nav-height) - var(--safe-bottom));
    overflow: hidden;
    position: relative;
}
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    -webkit-overflow-scrolling: touch;
}

/* Message row: portrait + bubble */
.msg-row {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    animation: msgIn 0.3s ease;
}
.msg-row.user { justify-content: flex-end; }
.msg-row.assistant { justify-content: flex-start; }

.msg-portrait {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    overflow: hidden;
    background: var(--bg-card);
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: var(--text-dim);
    margin-top: 2px;
}
.msg-portrait img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.message {
    position: relative;
    max-width: calc(100% - 44px);
    padding: 12px 16px;
    border-radius: var(--radius);
    font-size: 14px;
    line-height: 1.6;
    word-wrap: break-word;
    white-space: pre-wrap;
    min-width: 0;
}
@keyframes msgIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}
.message.user {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    border-bottom-right-radius: 4px;
}
.message.assistant {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}
.message .msg-actions {
    margin-top: 8px;
    display: flex;
    gap: 8px;
}

/* Hover action buttons on messages */
.msg-hover-actions {
    position: absolute;
    bottom: -14px;
    display: flex;
    gap: 4px;
    opacity: 0;
    transition: opacity 0.15s;
    pointer-events: none;
    z-index: 10;
}
.message.user .msg-hover-actions { right: 4px; }
.message.assistant .msg-hover-actions { left: 4px; }
.message:hover .msg-hover-actions,
.message:active .msg-hover-actions,
.message.show-actions .msg-hover-actions {
    opacity: 1;
    pointer-events: auto;
}
@media (hover: none) {
    .msg-hover-actions {
        opacity: 1;
        pointer-events: auto;
        position: static;
        margin-top: 8px;
        gap: 8px;
    }
    .message.user .msg-hover-actions { justify-content: flex-end; }
    .message.assistant .msg-hover-actions { justify-content: flex-start; }
}
.msg-action-btn {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 2px 8px;
    font-size: 12px;
    cursor: pointer;
    color: var(--text-dim);
    font-family: inherit;
    transition: background 0.15s, color 0.15s;
    line-height: 1.4;
    box-shadow: 0 2px 6px rgba(0,0,0,0.25);
}
.msg-action-btn:hover {
    background: var(--bg-card-hover);
    color: var(--text);
}
/* Portrait generation bar */
.portrait-gen-bar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-bottom: 20px;
}
.portrait-style-select {
    padding: 6px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    color: var(--text);
    font-size: 13px;
    font-family: inherit;
    cursor: pointer;
    max-width: 150px;
}
.portrait-style-select:focus {
    border-color: var(--primary);
    outline: none;
}

.msg-action-cg {
    color: var(--accent);
    border-color: rgba(236, 72, 153, 0.3);
}
.msg-action-regen {
    color: var(--primary-light);
    border-color: rgba(124, 58, 237, 0.3);
}

/* Edit mode textarea */
.msg-edit-textarea {
    width: 100%;
    background: var(--bg-input);
    border: 1px solid var(--primary);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 14px;
    font-family: inherit;
    padding: 10px 12px;
    resize: vertical;
    outline: none;
    line-height: 1.6;
    min-height: 60px;
    box-sizing: border-box;
}
.msg-edit-actions {
    display: flex;
    gap: 8px;
    margin-top: 8px;
    justify-content: flex-end;
}
.msg-cg-generating {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 8px;
    font-size: 11px;
    color: var(--text-dim);
}
.chat-input-area {
    padding: 12px 16px;
    background: var(--bg-nav);
    border-top: 1px solid var(--border);
    display: flex;
    gap: 10px;
    align-items: flex-end;
}
.chat-input {
    flex: 1;
    padding: 12px 16px;
    border: 1px solid var(--border);
    border-radius: 24px;
    background: var(--bg-input);
    color: var(--text);
    font-size: 14px;
    font-family: inherit;
    outline: none;
    max-height: 120px;
    resize: none;
    line-height: 1.4;
}
.chat-input:focus { border-color: var(--primary); }
.send-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    color: white;
    border: none;
    font-size: 18px;
    cursor: pointer;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s;
}
.send-btn:hover { transform: scale(1.05); }
.send-btn:disabled { opacity: 0.5; transform: none; }
.cg-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: rgba(236, 72, 153, 0.15);
    color: var(--accent);
    border: 1px solid rgba(236, 72, 153, 0.3);
    font-size: 18px;
    cursor: pointer;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}
.cg-btn:hover { background: rgba(236, 72, 153, 0.25); }
.cg-inline {
    position: relative;
    margin-top: 8px;
    border-radius: var(--radius-sm);
    overflow: hidden;
    max-width: 280px;
}
.cg-inline img {
    width: 100%;
    display: block;
    cursor: pointer;
}
.cg-remove-btn {
    position: absolute;
    top: 6px;
    right: 6px;
    padding: 2px 8px;
    border-radius: var(--radius-sm);
    border: 1px solid rgba(239,68,68,0.4);
    background: rgba(239,68,68,0.8);
    color: white;
    font-size: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0.85;
    transition: opacity 0.15s;
    line-height: 1;
    font-size: 10px;
    font-weight: 600;
}
.cg-remove-btn:active { opacity: 1; background: rgba(239,68,68,1); }

/* ── Gallery ────────────────────────────────────────────────────────────── */
.gallery-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 10px;
    padding: 16px;
}
.gallery-item {
    aspect-ratio: 1;
    border-radius: var(--radius-sm);
    overflow: hidden;
    position: relative;
    cursor: pointer;
    border: 1px solid var(--border);
}
.gallery-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s;
}
.gallery-item:hover img { transform: scale(1.05); }
.gallery-item .overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 6px 8px;
    background: linear-gradient(transparent, rgba(0,0,0,0.8));
    font-size: 10px;
    color: white;
}
.gallery-char-section {
    padding: 16px 16px 8px;
}
.gallery-char-name {
    font-size: 16px;
    font-weight: 600;
    color: var(--primary-light);
}

/* ── Lightbox ───────────────────────────────────────────────────────────── */
.lightbox {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.95);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.2s;
    padding: 20px;
}
.lightbox img {
    max-width: 100%;
    max-height: 90vh;
    border-radius: var(--radius);
    object-fit: contain;
}
.lightbox-close {
    position: absolute;
    top: 16px;
    right: 16px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255,255,255,0.1);
    color: white;
    border: none;
    font-size: 24px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}
.lightbox-actions {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 12px;
}

/* ── Settings ───────────────────────────────────────────────────────────── */
.settings-section {
    padding: 16px;
}
.settings-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 0;
    border-bottom: 1px solid var(--border);
}
.settings-item:last-child { border-bottom: none; }
.lang-toggle {
    display: flex;
    gap: 4px;
    padding: 3px;
    background: var(--bg-input);
    border-radius: var(--radius-sm);
}
.lang-btn {
    padding: 6px 14px;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--text-muted);
    font-size: 13px;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.2s;
}
.lang-btn.active {
    background: var(--primary);
    color: white;
}

/* ── Empty State ────────────────────────────────────────────────────────── */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    text-align: center;
}
.empty-state .icon {
    font-size: 64px;
    margin-bottom: 16px;
    opacity: 0.3;
}
.empty-state h3 {
    font-size: 18px;
    margin-bottom: 8px;
    color: var(--text-muted);
}
.empty-state p {
    font-size: 14px;
    color: var(--text-dim);
    margin-bottom: 24px;
}

/* ── Toast ──────────────────────────────────────────────────────────────── */
.toast {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    padding: 12px 24px;
    border-radius: var(--radius);
    color: white;
    font-size: 14px;
    z-index: 2000;
    animation: slideDown 0.3s ease;
    max-width: 90%;
    text-align: center;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
.toast.info { background: var(--primary); }
.toast.warning { background: #e6a700; color: #1a1a1a; }
@keyframes slideDown {
    from { opacity: 0; transform: translate(-50%, -20px); }
    to { opacity: 1; transform: translate(-50%, 0); }
}

/* ── Loading ────────────────────────────────────────────────────────────── */
.spinner {
    width: 24px;
    height: 24px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 15, 26, 0.8);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 500;
    gap: 16px;
}
.loading-overlay .spinner {
    width: 40px;
    height: 40px;
}

/* ── Confirm Dialog ─────────────────────────────────────────────────────── */
.dialog-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 20px;
    animation: fadeIn 0.2s;
}
.dialog {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 24px;
    max-width: 340px;
    width: 100%;
    text-align: center;
}
.dialog h3 { margin-bottom: 12px; font-size: 18px; }
.dialog p { color: var(--text-muted); margin-bottom: 20px; font-size: 14px; }
.dialog-buttons { display: flex; gap: 10px; justify-content: center; }

/* ── Page Padding ───────────────────────────────────────────────────────── */
.page-pad { padding: 16px; padding-bottom: 24px; }

/* ── Avatar Placeholder ─────────────────────────────────────────────────── */
.avatar-upload {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background: var(--bg-input);
    border: 2px dashed var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 36px;
    color: var(--text-dim);
    cursor: pointer;
    margin: 0 auto 20px;
    overflow: hidden;
    transition: border-color 0.2s;
}
.avatar-upload:hover { border-color: var(--primary); }
.avatar-upload img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* ── Color Picker Grid ──────────────────────────────────────────────────── */
.color-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}
.color-swatch {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    cursor: pointer;
    border: 3px solid transparent;
    transition: all 0.2s;
    position: relative;
}
.color-swatch:hover { transform: scale(1.1); }
.color-swatch.selected {
    border-color: white;
    box-shadow: 0 0 0 2px var(--primary);
}

/* ── Mobile optimizations ───────────────────────────────────────────────── */
@media (max-width: 400px) {
    .selection-grid { grid-template-columns: repeat(3, 1fr); }
    .gallery-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 768px) {
    .gallery-grid { grid-template-columns: repeat(4, 1fr); }
}

/* ── Transitions ────────────────────────────────────────────────────────── */
.page-enter { animation: pageIn 0.25s ease; }
@keyframes pageIn {
    from { opacity: 0; transform: translateX(12px); }
    to { opacity: 1; transform: translateX(0); }
}

/* ── Filter tabs (Gallery) ──────────────────────────────────────────────── */
.filter-bar {
    display: flex;
    gap: 8px;
    padding: 12px 16px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.filter-bar::-webkit-scrollbar { display: none; }
.filter-chip {
    padding: 6px 16px;
    border-radius: 20px;
    border: 1px solid var(--border);
    background: var(--bg-input);
    color: var(--text-muted);
    font-size: 13px;
    cursor: pointer;
    white-space: nowrap;
    font-family: inherit;
    transition: all 0.2s;
}
.filter-chip.active {
    background: var(--primary);
    border-color: var(--primary);
    color: white;
}

/* ── Delete button on char card ─────────────────────────────────────────── */
.char-delete {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: transparent;
    border: none;
    color: var(--text-dim);
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}
.char-delete:hover {
    background: rgba(239, 68, 68, 0.15);
    color: var(--danger);
}

/* ── Model Selector ─────────────────────────────────────────────────────── */
.model-option {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
    text-align: left;
    width: 100%;
    color: var(--text);
}
.model-option:hover { border-color: var(--primary); }
.model-option.selected {
    border-color: var(--primary);
    background: rgba(124, 58, 237, 0.1);
}
.model-radio {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: 2px solid var(--border);
    flex-shrink: 0;
    position: relative;
    transition: border-color 0.2s;
}
.model-radio.checked {
    border-color: var(--primary);
}
.model-radio.checked::after {
    content: '';
    position: absolute;
    top: 3px;
    left: 3px;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--primary);
}
.model-info {
    display: flex;
    flex-direction: column;
    gap: 1px;
}
.model-name {
    font-size: 14px;
    font-weight: 500;
}
.model-desc {
    font-size: 11px;
    color: var(--text-dim);
}

/* ── Chat Style Select ──────────────────────────────────────────────────── */
.chat-style-select {
    padding: 4px 28px 4px 8px;
    border: 1px solid var(--border);
    border-radius: 16px;
    background: var(--bg-card);
    color: var(--text);
    font-size: 12px;
    font-family: inherit;
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' fill='%2394a3b8' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    outline: none;
    transition: border-color 0.2s;
}
.chat-style-select:hover,
.chat-style-select:focus { border-color: var(--primary); }

/* ── Self Persona Picker ────────────────────────────────────────────────── */
.self-picker-btn {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    border: 1px solid var(--border);
    border-radius: 16px;
    background: var(--bg-card);
    color: var(--text);
    font-size: 12px;
    font-family: inherit;
    cursor: pointer;
    transition: border-color 0.2s;
}
.self-picker-btn:hover { border-color: var(--primary); }
.self-mini-avatar {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    overflow: hidden;
    flex-shrink: 0;
    color: white;
}
.self-mini-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.self-picker-dropdown {
    position: absolute;
    left: 16px;
    right: 16px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    z-index: 200;
    box-shadow: 0 8px 24px rgba(0,0,0,0.4);
    max-height: 200px;
    overflow-y: auto;
}
.self-picker-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 10px 14px;
    border: none;
    background: transparent;
    color: var(--text);
    font-size: 13px;
    font-family: inherit;
    cursor: pointer;
    transition: background 0.15s;
    text-align: left;
}
.self-picker-item:hover { background: var(--bg-card-hover); }
.self-picker-item.active {
    background: rgba(124, 58, 237, 0.1);
    color: var(--primary-light);
}
.self-picker-item + .self-picker-item {
    border-top: 1px solid var(--border);
}

/* ── Character Tabs ───────────────────────────────────────────── */
.char-tabs-bar {
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.char-tabs-scroll {
    display: flex;
    overflow-x: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
    padding: 0 4px;
    gap: 2px;
}
.char-tabs-scroll::-webkit-scrollbar { display: none; }
.char-tab {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 8px;
    font-size: 12px;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    color: var(--text-dim);
    transition: all 0.15s;
    position: relative;
    max-width: 140px;
}
.char-tab:hover {
    background: var(--bg-card);
    color: var(--text);
}
.char-tab.active {
    color: var(--text);
    border-bottom-color: var(--primary);
    background: var(--bg-card);
}
.char-tab-avatar {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    overflow: hidden;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--border);
    font-size: 10px;
}
.char-tab-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.char-tab-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 70px;
}
.char-tab-close {
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 10px;
    cursor: pointer;
    padding: 0 2px;
    line-height: 1;
    opacity: 0.5;
    transition: opacity 0.15s;
    flex-shrink: 0;
}
.char-tab-close:hover { opacity: 1; color: var(--danger); }

/* ── Session Tabs ─────────────────────────────────────────────── */
.session-tabs-bar {
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.session-tabs-scroll {
    display: flex;
    overflow-x: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
    padding: 0 8px;
}
.session-tabs-scroll::-webkit-scrollbar { display: none; }
.session-tab {
    position: relative;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
    font-size: 11px;
    color: var(--text-dim);
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    cursor: pointer;
    font-family: inherit;
    white-space: nowrap;
    transition: color 0.15s, border-color 0.15s;
}
.session-tab:hover { color: var(--text); }
.session-tab.active {
    color: var(--primary-light);
    border-bottom-color: var(--primary);
}
.session-tab-title {
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
}
.session-tab-menu {
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    font-size: 14px;
    padding: 0 2px;
    line-height: 1;
    font-family: inherit;
    opacity: 0;
    transition: opacity 0.15s;
}
.session-tab:hover .session-tab-menu,
.session-tab.active .session-tab-menu { opacity: 1; }
.session-tab-new {
    color: var(--text-dim);
    font-size: 16px;
    padding: 8px 12px;
    border-bottom: 2px solid transparent;
}
.session-tab-new:hover { color: var(--primary-light); }

.session-ctx-menu {
    position: absolute;
    top: 100%;
    left: 0;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: 0 8px 24px rgba(0,0,0,0.4);
    z-index: 300;
    min-width: 130px;
    overflow: hidden;
}
.session-ctx-menu button {
    display: block;
    width: 100%;
    padding: 10px 14px;
    border: none;
    background: transparent;
    color: var(--text);
    font-size: 13px;
    font-family: inherit;
    cursor: pointer;
    text-align: left;
    transition: background 0.15s;
}
.session-ctx-menu button:hover { background: var(--bg-card-hover); }
.session-ctx-menu button.danger { color: #ef4444; }
.session-ctx-menu button + button { border-top: 1px solid var(--border); }

.session-rename-input {
    background: var(--bg-input);
    border: 1px solid var(--primary);
    border-radius: 4px;
    color: var(--text);
    font-size: 12px;
    font-family: inherit;
    padding: 2px 6px;
    width: 80px;
    outline: none;
}

/* ── Image Style Picker ────────────────────────────────────────── */
.image-style-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
}
.img-style-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 10px 4px;
    border-radius: var(--radius-sm);
    border: 2px solid var(--border);
    background: transparent;
    cursor: pointer;
    font-family: inherit;
    color: var(--text-dim);
    transition: border-color 0.2s, background 0.2s, color 0.2s;
}
.img-style-btn:hover {
    border-color: var(--text-muted);
    color: var(--text);
}
.img-style-btn.active {
    border-color: var(--primary);
    background: rgba(124, 58, 237, 0.08);
    color: var(--text);
}
.img-style-icon { font-size: 20px; }
.img-style-label {
    font-size: 10px;
    font-weight: 600;
    text-align: center;
    line-height: 1.2;
}
.img-style-btn.active .img-style-label { color: var(--primary-light); }

/* ── Home Tabs ─────────────────────────────────────────────────── */
.home-tabs {
    display: flex;
    gap: 0;
    padding: 8px 16px;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
}
.home-tab-btn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px;
    border: none;
    background: transparent;
    color: var(--text-dim);
    font-size: 14px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: color 0.2s, border-color 0.2s;
}
.home-tab-btn:hover { color: var(--text); }
.home-tab-btn.active {
    color: var(--primary-light);
    border-bottom-color: var(--primary);
}

/* ── Auth Page ─────────────────────────────────────────────────── */
.auth-container {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    min-height: 100vh;
    background: linear-gradient(135deg, var(--bg) 0%, #1a1030 50%, var(--bg) 100%);
}
.auth-card {
    width: 100%;
    max-width: 380px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 36px 28px;
    text-align: center;
    animation: fadeIn 0.4s ease;
}
.auth-logo {
    font-size: 56px;
    margin-bottom: 12px;
}
.auth-title {
    font-size: 26px;
    font-weight: 700;
    margin-bottom: 6px;
    background: linear-gradient(135deg, var(--primary-light), var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}
.auth-subtitle {
    font-size: 14px;
    color: var(--text-muted);
    margin-bottom: 24px;
}
.auth-error {
    background: rgba(239, 68, 68, 0.12);
    border: 1px solid rgba(239, 68, 68, 0.25);
    color: var(--danger);
    font-size: 13px;
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    margin-bottom: 16px;
}
.auth-switch {
    margin-top: 20px;
    font-size: 13px;
    color: var(--text-dim);
}
.auth-switch-btn {
    background: none;
    border: none;
    color: var(--primary-light);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    margin-left: 4px;
}
.auth-switch-btn:hover {
    text-decoration: underline;
}

/* ── World Avatar ──────────────────────────────────────────────── */
.world-avatar {
    background: linear-gradient(135deg, #10b981, #3b82f6) !important;
}

/* ── Reply Length Picker ───────────────────────────────────────── */
.reply-length-picker {
    display: flex;
    gap: 8px;
}
.reply-len-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 12px 8px;
    border-radius: var(--radius-sm);
    border: 2px solid var(--border);
    background: transparent;
    cursor: pointer;
    font-family: inherit;
    color: var(--text-dim);
    transition: border-color 0.2s, background 0.2s, color 0.2s;
}
.reply-len-btn:hover {
    border-color: var(--text-muted);
    color: var(--text);
}
.reply-len-btn.active {
    border-color: var(--primary);
    background: rgba(124, 58, 237, 0.08);
    color: var(--text);
}
.reply-len-icon { font-size: 20px; }
.reply-len-label {
    font-size: 13px;
    font-weight: 600;
}
.reply-len-desc {
    font-size: 10px;
    color: var(--text-dim);
}
.reply-len-btn.active .reply-len-desc { color: var(--primary-light); }

/* ── Toggle Switch ────────────────────────────────────────────── */
.toggle-switch {
    position: relative;
    width: 44px;
    height: 24px;
    border-radius: 12px;
    border: none;
    background: var(--border);
    cursor: pointer;
    padding: 0;
    flex-shrink: 0;
    transition: background 0.25s;
}
.toggle-switch.active {
    background: var(--primary);
}
.toggle-knob {
    position: absolute;
    top: 2px;
    left: 2px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: white;
    transition: transform 0.25s;
    box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.toggle-switch.active .toggle-knob {
    transform: translateX(20px);
}

.scroll-btns {
    position: absolute;
    right: 8px;
    bottom: 72px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.scroll-btn {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    background: rgba(255,255,255,0.1);
    color: rgba(255,255,255,0.5);
    border: 1px solid rgba(255,255,255,0.15);
    font-size: 11px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}
.scroll-btn:active {
    background: rgba(255,255,255,0.25);
}
