/* ====== DESIGN SYSTEM ====== */
:root {
    --primary: #1751cf;
    --primary-light: #e8effc;
    --primary-dark: #0f3a9e;
    --primary-gradient: linear-gradient(135deg, #1751cf, #6366f1);
    --bg: #f5f7fb;
    --surface: #ffffff;
    --text: #1a1a2e;
    --text-secondary: #6b7280;
    --text-muted: #9ca3af;
    --border: #e5e7eb;
    --success: #10b981;
    --error: #ef4444;
    --warning: #f59e0b;
    --radius: 8px;
    --radius-lg: 16px;
    --radius-xl: 24px;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);
    --transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Lexend', sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
}

#app {
    max-width: 480px;
    margin: 0 auto;
    min-height: 100vh;
    position: relative;
    background: var(--bg);
}

.material-symbols-rounded {
    font-variation-settings: 'FILL' 1, 'wght' 400, 'GRAD' 0, 'opsz' 24;
}

/* ====== PAGE SYSTEM ====== */
.page {
    display: none;
    min-height: 100vh;
}

.page.active {
    display: flex;
    flex-direction: column;
}

/* ====== SETTINGS PAGE ====== */
.settings-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 24px 20px;
    justify-content: center;
}

.settings-header {
    text-align: center;
    margin-bottom: 32px;
}

.logo-circle {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    background: var(--primary-gradient);
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 16px;
    box-shadow: 0 6px 20px rgba(23, 81, 207, 0.35);
    animation: pulse-glow 3s infinite;
}

@keyframes pulse-glow {

    0%,
    100% {
        box-shadow: 0 6px 20px rgba(23, 81, 207, 0.35);
    }

    50% {
        box-shadow: 0 6px 32px rgba(23, 81, 207, 0.55);
    }
}

.logo-circle .material-symbols-rounded {
    color: white;
    font-size: 36px;
}

.settings-header h1 {
    font-size: 1.5rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.subtitle {
    color: var(--text-secondary);
    font-size: 0.85rem;
    margin-top: 6px;
}

/* Settings Card */
.settings-card {
    background: var(--surface);
    border-radius: var(--radius-xl);
    padding: 28px 24px;
    box-shadow: var(--shadow-md);
}

.settings-card h2 {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 24px;
    color: var(--text);
}

.settings-card h2 .material-symbols-rounded {
    color: var(--primary);
    font-size: 22px;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 8px;
}

.model-status {
    font-size: 0.85rem;
    cursor: default;
}

/* Provider Toggle */
.provider-toggle {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.provider-toggle.three-col {
    grid-template-columns: 1fr 1fr 1fr;
}

.toggle-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    font-family: 'Lexend', sans-serif;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-secondary);
    cursor: pointer;
    transition: var(--transition);
}

.toggle-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
}

.toggle-btn.active {
    border-color: var(--primary);
    background: var(--primary-light);
    color: var(--primary);
}

.provider-icon {
    font-size: 1.1rem;
    font-weight: 700;
}

/* Input */
.input-wrapper {
    position: relative;
}

.input-wrapper input,
select {
    width: 100%;
    padding: 12px 44px 12px 14px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    font-family: 'Lexend', sans-serif;
    font-size: 0.9rem;
    color: var(--text);
    transition: var(--transition);
    outline: none;
}

select {
    padding-right: 14px;
    cursor: pointer;
    appearance: auto;
}

.input-wrapper input:focus,
select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(23, 81, 207, 0.1);
}

.input-toggle-eye {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    display: flex;
    padding: 4px;
}

.input-hint {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 8px;
    font-size: 0.75rem;
    color: var(--text-muted);
}

.input-hint .material-symbols-rounded {
    font-size: 16px;
}

.input-hint a {
    color: var(--primary);
    text-decoration: none;
}

.input-hint a:hover {
    text-decoration: underline;
}

/* Primary Button */
.btn-primary {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 14px 24px;
    background: var(--primary-gradient);
    color: white;
    border: none;
    border-radius: var(--radius);
    font-family: 'Lexend', sans-serif;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    box-shadow: 0 4px 12px rgba(23, 81, 207, 0.3);
    margin-top: 8px;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(23, 81, 207, 0.4);
}

.btn-primary:active {
    transform: translateY(0);
}

.btn-primary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.btn-primary .material-symbols-rounded {
    font-size: 20px;
}

.settings-footer {
    text-align: center;
    margin-top: 20px;
    font-size: 0.72rem;
    color: var(--text-muted);
}

/* History Section */
.history-section {
    margin-top: 24px;
}

.history-section h3 {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 12px;
}

.history-section h3 .material-symbols-rounded {
    color: var(--primary);
    font-size: 20px;
}

.history-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.history-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 14px 16px;
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    transition: var(--transition);
    cursor: pointer;
}

.history-item:hover {
    box-shadow: var(--shadow-md);
    transform: translateY(-1px);
}

.history-item-main {
    flex: 1;
    min-width: 0;
}

.history-topic {
    font-size: 0.88rem;
    font-weight: 600;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-meta {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.72rem;
    color: var(--text-muted);
    margin-top: 4px;
}

.history-delete {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition);
    flex-shrink: 0;
}

.history-delete:hover {
    background: #fef2f2;
    color: var(--error);
}

.history-delete .material-symbols-rounded {
    font-size: 18px;
}

/* ====== CHAT PAGE ====== */
#page-chat {
    height: 100vh;
    height: 100dvh;
}

/* Chat Header */
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    z-index: 10;
}

.chat-header-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.chat-header-right {
    display: flex;
    gap: 4px;
}

.icon-btn {
    width: 38px;
    height: 38px;
    border: none;
    border-radius: 50%;
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-secondary);
    transition: var(--transition);
}

.icon-btn:hover {
    background: var(--primary-light);
    color: var(--primary);
}

.chat-header-info h2 {
    font-size: 1rem;
    font-weight: 600;
    line-height: 1.2;
}

.chat-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.72rem;
    color: var(--text-muted);
}

.status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--success);
    animation: dot-blink 2s infinite;
}

@keyframes dot-blink {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.3;
    }
}

/* Chat Messages Area */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
}

/* Message Bubbles */
.message {
    display: flex;
    gap: 10px;
    max-width: 92%;
    animation: msgIn 0.3s ease;
}

@keyframes msgIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.ai {
    align-self: flex-start;
}

.message.user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 16px;
}

.message.ai .message-avatar {
    background: var(--primary-gradient);
    color: white;
}

.message.user .message-avatar {
    background: #e5e7eb;
    color: var(--text-secondary);
}

.message-bubble {
    padding: 14px 16px;
    border-radius: var(--radius-lg);
    line-height: 1.7;
    font-size: 0.88rem;
    word-break: break-word;
}

.message.ai .message-bubble {
    background: var(--surface);
    color: var(--text);
    border-bottom-left-radius: 4px;
    box-shadow: var(--shadow-sm);
}

.message.user .message-bubble {
    background: var(--primary);
    color: white;
    border-bottom-right-radius: 4px;
}

/* Typing Indicator */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 4px 0;
}

.typing-indicator span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--primary);
    opacity: 0.4;
    animation: typing-bounce 1.4s infinite ease-in-out;
}

.typing-indicator span:nth-child(1) {
    animation-delay: 0s;
}

.typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing-bounce {

    0%,
    80%,
    100% {
        transform: scale(0.6);
        opacity: 0.3;
    }

    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* ====== MARKDOWN RENDERING IN BUBBLES ====== */
.message-bubble h1,
.message-bubble h2,
.message-bubble h3,
.message-bubble h4 {
    margin: 16px 0 8px 0;
    font-weight: 600;
    line-height: 1.4;
}

.message-bubble h1:first-child,
.message-bubble h2:first-child,
.message-bubble h3:first-child {
    margin-top: 0;
}

.message-bubble h1 {
    font-size: 1.15rem;
}

.message-bubble h2 {
    font-size: 1.05rem;
}

.message-bubble h3 {
    font-size: 0.95rem;
    color: var(--primary-dark);
}

.message-bubble h4 {
    font-size: 0.9rem;
}

.message-bubble p {
    margin: 8px 0;
}

.message-bubble p:first-child {
    margin-top: 0;
}

.message-bubble p:last-child {
    margin-bottom: 0;
}

.message-bubble ul,
.message-bubble ol {
    padding-left: 20px;
    margin: 8px 0;
}

.message-bubble li {
    margin: 4px 0;
    line-height: 1.6;
}

.message-bubble strong {
    font-weight: 600;
    color: var(--primary-dark);
}

.message.user .message-bubble strong {
    color: white;
}

.message-bubble em {
    font-style: italic;
    opacity: 0.85;
}

.message-bubble blockquote {
    border-left: 3px solid var(--primary);
    padding: 8px 12px;
    margin: 10px 0;
    background: var(--primary-light);
    border-radius: 0 var(--radius) var(--radius) 0;
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.message-bubble code {
    font-family: 'JetBrains Mono', monospace;
    background: rgba(23, 81, 207, 0.08);
    color: var(--primary-dark);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.82em;
}

.message.user .message-bubble code {
    background: rgba(255, 255, 255, 0.15);
    color: white;
}

.message-bubble pre {
    background: #1e1e2e;
    border-radius: var(--radius);
    padding: 14px 16px;
    margin: 10px 0;
    overflow-x: auto;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.2);
}

.message-bubble pre code {
    background: none;
    color: #a6e3a1;
    padding: 0;
    font-size: 0.82rem;
    line-height: 1.7;
}

.message-bubble hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 14px 0;
}

.message-bubble table {
    width: 100%;
    border-collapse: collapse;
    margin: 10px 0;
    font-size: 0.82rem;
}

.message-bubble th,
.message-bubble td {
    padding: 8px 10px;
    border: 1px solid var(--border);
    text-align: left;
}

.message-bubble th {
    background: var(--primary-light);
    font-weight: 600;
}

/* Level markers */
.message-bubble .level-marker {
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

/* ====== CHAT INPUT ====== */
.chat-input-bar {
    padding: 12px 16px;
    background: var(--surface);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.chat-input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--bg);
    border: 2px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 4px 4px 4px 14px;
    transition: var(--transition);
}

.chat-input-wrapper:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(23, 81, 207, 0.08);
}

#chat-input {
    flex: 1;
    border: none;
    outline: none;
    background: transparent;
    font-family: 'Lexend', sans-serif;
    font-size: 0.9rem;
    color: var(--text);
    resize: none;
    max-height: 120px;
    padding: 8px 0;
    line-height: 1.5;
}

#chat-input::placeholder {
    color: var(--text-muted);
}

.send-btn {
    width: 38px;
    height: 38px;
    border: none;
    border-radius: 50%;
    background: var(--primary-gradient);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition);
    flex-shrink: 0;
}

.send-btn:hover {
    transform: scale(1.05);
}

.send-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
    transform: none;
}

.send-btn .material-symbols-rounded {
    font-size: 20px;
}

/* ====== ERROR TOAST ====== */
.toast {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%) translateY(-100px);
    max-width: 400px;
    width: calc(100% - 40px);
    padding: 14px 18px;
    border-radius: var(--radius-lg);
    font-size: 0.85rem;
    line-height: 1.4;
    z-index: 999;
    transition: transform 0.3s ease;
    display: flex;
    align-items: center;
    gap: 10px;
}

.toast.show {
    transform: translateX(-50%) translateY(0);
}

.toast.error {
    background: #fef2f2;
    color: #991b1b;
    border: 1px solid #fecaca;
}

.toast .material-symbols-rounded {
    font-size: 20px;
    flex-shrink: 0;
}

/* ====== PHASE SEPARATOR ====== */
.phase-separator {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 8px 0;
    animation: msgIn 0.3s ease;
}

.separator-line {
    flex: 1;
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--primary), transparent);
}

.separator-text {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--primary);
    white-space: nowrap;
    padding: 4px 12px;
    background: var(--primary-light);
    border-radius: 20px;
}

/* Admin Panel */
.admin-panel {
    margin-top: 20px;
    display: flex;
    gap: 8px;
    justify-content: center;
    align-items: center;
    opacity: 0.1;
    transition: opacity 0.3s;
}

.admin-panel:hover,
.admin-panel:focus-within {
    opacity: 1;
}

#admin-pass {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 6px 12px;
    font-size: 0.85rem;
    color: var(--text);
    width: 120px;
}

#btn-unlock {
    background: transparent;
    border: none;
    cursor: pointer;
    font-size: 1.2rem;
}

/* ====== RESPONSIVE ====== */
@media (min-width: 481px) {
    body {
        background: #0f0f1a;
    }

    #app {
        box-shadow: 0 0 60px rgba(23, 81, 207, 0.15);
        border-left: 1px solid #1e1e30;
        border-right: 1px solid #1e1e30;
    }
}