/* ===== Design System | Matches trainingcqy.com ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

:root {
    color-scheme: light dark;
    --bg-color: #f7f9fc;
    --surface-color: #ffffff;
    --text-primary: #1f1f1f;
    --text-secondary: #444746;
    --text-tertiary: #70757a;
    --accent-blue: #c2e7ff;
    --accent-blue-text: #001d35;
    --hover-color: #f0f4f8;
    --active-color: #e0e4e8;
    --border-color: #e0e0e0;
    --chip-border: #c4c7c5;
    --primary-blue: #0b57d0;
    --user-bubble: #d3e3fd;
    --user-bubble-text: #041e49;
    --agent-bubble: var(--surface-color);
    --input-bg: #f0f4f8;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
    --shadow-md: 0 4px 20px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 8px 40px rgba(0, 0, 0, 0.08);
    --radius-sm: 12px;
    --radius-md: 20px;
    --radius-lg: 24px;
    --radius-full: 9999px;
    --transition-fast: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-spring: 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
    --transition-smooth: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg-color: #121212;
        --surface-color: #1e1e1e;
        --text-primary: #e3e3e3;
        --text-secondary: #a0a0a0;
        --text-tertiary: #777;
        --accent-blue: #004a77;
        --accent-blue-text: #c2e7ff;
        --hover-color: #2c2c2c;
        --active-color: #3d3d3d;
        --border-color: #333333;
        --chip-border: #444444;
        --primary-blue: #8ab4f8;
        --user-bubble: #004a77;
        --user-bubble-text: #c2e7ff;
        --agent-bubble: #2a2a2a;
        --input-bg: #2a2a2a;
        --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.2);
        --shadow-md: 0 4px 20px rgba(0, 0, 0, 0.3);
        --shadow-lg: 0 8px 40px rgba(0, 0, 0, 0.4);
    }
}

body {
    font-family: 'Roboto', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    min-height: 100vh;
    -webkit-user-select: none;
    user-select: none;
}

/* ===== Top Navigation ===== */
.topbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 64px;
    z-index: 20;
    display: flex;
    align-items: center;
    padding: 0 24px;
    gap: 8px;
    background-color: var(--bg-color);
}

.logo-area {
    font-size: 20px;
    font-weight: 500;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 10px;
    margin-right: 24px;
    white-space: nowrap;
    text-decoration: none;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 16px;
    height: 40px;
    border-radius: var(--radius-md);
    text-decoration: none;
    color: var(--text-secondary);
    font-size: 14px;
    font-weight: 500;
    transition: background var(--transition-fast), transform var(--transition-fast);
    cursor: pointer;
    white-space: nowrap;
}

@media (hover: hover) and (pointer: fine) {
    .nav-item:hover {
        background-color: var(--hover-color);
    }
}

.nav-item:active {
    background-color: var(--active-color);
    transform: scale(0.97);
}

.nav-item .material-symbols-outlined {
    font-size: 20px;
}

/* ===== Bottom Navigation (Mobile) ===== */
.bottom-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 64px;
    background: var(--surface-color);
    justify-content: space-around;
    align-items: center;
    z-index: 1000;
    padding-bottom: env(safe-area-inset-bottom);
    box-sizing: content-box;
}

.bottom-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 500;
    width: 64px;
    height: 100%;
    gap: 4px;
}

.bottom-nav-item .icon-container {
    width: 64px;
    height: 32px;
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color var(--transition-fast);
}

.bottom-nav-item .material-symbols-outlined {
    font-size: 24px;
}

/* ===== View System ===== */
.view {
    display: none;
    opacity: 0;
    transform: translateY(12px);
    transition: opacity 0.4s ease, transform 0.4s ease;
}

.view.active {
    display: flex;
    opacity: 1;
    transform: translateY(0);
}

/* ===== Auth View ===== */
#auth-view {
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    padding: 80px 20px 80px;
}

.auth-card {
    background: var(--surface-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    padding: 48px 40px;
    max-width: 420px;
    width: 100%;
    text-align: center;
    animation: cardEntrance 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes cardEntrance {
    from {
        opacity: 0;
        transform: translateY(30px) scale(0.96);
    }

    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.auth-icon {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    background: var(--accent-blue);
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 20px;
    animation: iconPulse 2s ease-in-out infinite;
}

@keyframes iconPulse {

    0%,
    100% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.05);
    }
}

.auth-icon .material-symbols-outlined {
    font-size: 36px;
    color: var(--accent-blue-text);
}

.auth-title {
    font-size: 24px;
    font-weight: 700;
    margin-bottom: 6px;
}

.auth-subtitle {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 28px;
}

.auth-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.input-group {
    position: relative;
    display: flex;
    align-items: center;
}

.input-icon {
    position: absolute;
    left: 14px;
    font-size: 20px;
    color: var(--text-tertiary);
    transition: color var(--transition-fast);
    pointer-events: none;
}

.input-group input {
    width: 100%;
    height: 48px;
    padding: 0 16px 0 44px;
    border: 1.5px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-color);
    color: var(--text-primary);
    font-size: 14px;
    font-family: inherit;
    outline: none;
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.input-group input:focus {
    border-color: var(--primary-blue);
    box-shadow: 0 0 0 3px rgba(11, 87, 208, 0.15);
}

.input-group input:focus+.input-icon,
.input-group input:focus~.input-icon {
    color: var(--primary-blue);
}

/* fix: icon is before input in DOM, use has() */
.input-group:has(input:focus) .input-icon {
    color: var(--primary-blue);
}

.primary-btn,
.secondary-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: 48px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 15px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: background var(--transition-fast), transform var(--transition-fast),
        box-shadow var(--transition-fast);
}

.primary-btn {
    background: var(--primary-blue);
    color: #fff;
}

.primary-btn:hover {
    box-shadow: 0 2px 8px rgba(11, 87, 208, 0.3);
    transform: translateY(-1px);
}

.primary-btn:active {
    transform: translateY(0) scale(0.98);
}

.primary-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.primary-btn .material-symbols-outlined,
.secondary-btn .material-symbols-outlined {
    font-size: 20px;
}

.secondary-btn {
    background: var(--hover-color);
    color: var(--primary-blue);
    border: 1.5px solid var(--border-color);
}

.secondary-btn:hover {
    background: var(--active-color);
    transform: translateY(-1px);
}

.secondary-btn:active {
    transform: translateY(0) scale(0.98);
}

.auth-divider {
    display: flex;
    align-items: center;
    gap: 16px;
    margin: 24px 0;
    color: var(--text-tertiary);
    font-size: 13px;
}

.auth-divider::before,
.auth-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border-color);
}

.status-msg {
    font-size: 13px;
    min-height: 20px;
    margin-top: 4px;
    transition: opacity var(--transition-fast);
}

.status-msg.success {
    color: #1e8e3e;
}

.status-msg.error {
    color: #d93025;
}

.status-msg.info {
    color: var(--primary-blue);
}

/* ===== Chat View ===== */
#chat-view {
    padding-top: 64px;
    height: 100vh;
}

.chat-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    max-width: 860px;
    margin: 0 auto;
}

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.chat-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.header-icon {
    font-size: 28px;
    color: var(--primary-blue);
}

.chat-title {
    font-size: 16px;
    font-weight: 600;
}

.quota-badge {
    font-size: 12px;
    color: var(--text-tertiary);
    background: var(--hover-color);
    padding: 2px 10px;
    border-radius: var(--radius-full);
    transition: background var(--transition-fast), color var(--transition-fast);
}

.quota-badge.low {
    background: #fce8e6;
    color: #c5221f;
}

@media (prefers-color-scheme: dark) {
    .quota-badge.low {
        background: #3c1f1e;
        color: #f28b82;
    }
}

.chat-header-actions {
    display: flex;
    gap: 4px;
}

.icon-btn {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background var(--transition-fast), transform var(--transition-fast);
}

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

.icon-btn:active {
    transform: scale(0.9);
}

.icon-btn .material-symbols-outlined {
    font-size: 22px;
}

/* ===== Messages Area ===== */
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
    position: relative;
}

.welcome-msg {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-secondary);
    animation: fadeUp 0.5s ease;
}

.welcome-icon {
    font-size: 48px;
    color: var(--primary-blue);
    display: block;
    margin-bottom: 12px;
    animation: wave 1.2s ease-in-out 2;
    transform-origin: 70% 70%;
}

@keyframes wave {
    0% {
        transform: rotate(0deg);
    }

    10% {
        transform: rotate(14deg);
    }

    20% {
        transform: rotate(-8deg);
    }

    30% {
        transform: rotate(14deg);
    }

    40% {
        transform: rotate(-4deg);
    }

    50% {
        transform: rotate(10deg);
    }

    60% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(0deg);
    }
}

.welcome-hint {
    font-size: 13px;
    color: var(--text-tertiary);
    margin-top: 8px;
}

/* Message Bubbles */
.msg {
    display: flex;
    gap: 10px;
    max-width: 85%;
    animation: msgSlideIn 0.35s var(--transition-spring);
}

@keyframes msgSlideIn {
    from {
        opacity: 0;
        transform: translateY(16px) scale(0.97);
    }

    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

@keyframes fadeUp {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.msg.user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.msg.agent {
    align-self: flex-start;
}

.msg-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 18px;
}

.msg.agent .msg-avatar {
    background: var(--accent-blue);
    color: var(--accent-blue-text);
}

.msg.user .msg-avatar {
    background: var(--user-bubble);
    color: var(--user-bubble-text);
}

.msg-content {
    padding: 12px 16px;
    border-radius: var(--radius-md);
    font-size: 14px;
    line-height: 1.7;
    user-select: text;
    word-break: break-word;
}

.msg.agent .msg-content {
    background: var(--agent-bubble);
    border: 1px solid var(--border-color);
    border-bottom-left-radius: 4px;
}

.msg.user .msg-content {
    background: var(--user-bubble);
    color: var(--user-bubble-text);
    border-bottom-right-radius: 4px;
}

/* Markdown rendering inside agent messages */
.msg-content h1,
.msg-content h2,
.msg-content h3 {
    font-size: 15px;
    font-weight: 700;
    margin: 12px 0 6px;
    color: var(--primary-blue);
}

.msg-content h1:first-child,
.msg-content h2:first-child,
.msg-content h3:first-child {
    margin-top: 0;
}

.msg-content p {
    margin: 6px 0;
}

.msg-content ul,
.msg-content ol {
    padding-left: 20px;
    margin: 6px 0;
}

.msg-content li {
    margin: 3px 0;
}

.msg-content strong {
    font-weight: 600;
}

.msg-content code {
    background: var(--hover-color);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
}

.msg-content pre {
    background: var(--hover-color);
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
}

.msg-content hr {
    border: none;
    border-top: 1px solid var(--border-color);
    margin: 12px 0;
}

/* Typing Indicator */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 16px 20px;
}

.typing-indicator .dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-tertiary);
    animation: typingBounce 1.2s ease-in-out infinite;
}

.typing-indicator .dot:nth-child(2) {
    animation-delay: 0.15s;
}

.typing-indicator .dot:nth-child(3) {
    animation-delay: 0.3s;
}

@keyframes typingBounce {

    0%,
    60%,
    100% {
        transform: translateY(0);
        opacity: 0.4;
    }

    30% {
        transform: translateY(-8px);
        opacity: 1;
    }
}

/* ===== Quota Exhausted Overlay ===== */
.quota-overlay {
    display: none;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 24px;
    background: var(--surface-color);
    border-top: 1px solid var(--border-color);
    text-align: center;
    animation: slideUp 0.4s ease;
    z-index: 10;
}

.quota-overlay.visible {
    display: block;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.quota-overlay-icon {
    font-size: 40px;
    color: var(--text-tertiary);
    margin-bottom: 8px;
}

.quota-overlay-content p {
    font-size: 15px;
    font-weight: 500;
}

.quota-overlay-hint {
    font-size: 13px;
    color: var(--text-tertiary);
    margin-top: 4px;
    font-weight: 400 !important;
}

/* ===== Input Bar ===== */
.input-bar {
    padding: 12px 20px 16px;
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
    background: var(--bg-color);
}

.input-bar-inner {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--input-bg);
    border: 1.5px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 6px 6px 6px 18px;
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.input-bar-inner:focus-within {
    border-color: var(--primary-blue);
    box-shadow: 0 0 0 3px rgba(11, 87, 208, 0.12);
}

#chat-input {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 14px;
    font-family: inherit;
    line-height: 1.5;
    resize: none;
    outline: none;
    max-height: 120px;
    padding: 8px 0;
}

#chat-input::placeholder {
    color: var(--text-tertiary);
}

.send-btn {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    background: var(--primary-blue);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background var(--transition-fast), transform var(--transition-spring),
        opacity var(--transition-fast);
}

.send-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: scale(0.9);
}

.send-btn:not(:disabled):hover {
    transform: scale(1.08);
}

.send-btn:not(:disabled):active {
    transform: scale(0.92);
}

.send-btn .material-symbols-outlined {
    font-size: 22px;
}

/* ===== Loading spinner for buttons ===== */
.btn-loading {
    pointer-events: none;
}

.btn-loading .material-symbols-outlined {
    display: none;
}

.btn-loading::after {
    content: '';
    width: 18px;
    height: 18px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

.secondary-btn.btn-loading::after {
    border-color: rgba(11, 87, 208, 0.2);
    border-top-color: var(--primary-blue);
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .topbar {
        display: none;
    }

    .bottom-nav {
        display: flex;
    }

    #auth-view {
        padding: 20px 16px 80px;
    }

    #chat-view {
        padding-top: 0;
        padding-bottom: 64px;
    }

    .auth-card {
        padding: 32px 24px;
    }

    .chat-container {
        max-width: 100%;
    }

    .msg {
        max-width: 92%;
    }

    .messages {
        padding: 16px;
    }
}