:root {
    --bg-color: #f6faff;
    --card-bg: rgba(255, 255, 255, 0.88);
    --primary: #1261ff;
    --primary-strong: #0838e9;
    --primary-glow: rgba(18, 97, 255, 0.24);
    --text-main: #10234d;
    --text-muted: #62708f;
    --accent: #00b8e8;
    --accent-soft: #dff7ff;
    --glass: rgba(255, 255, 255, 0.74);
    --border: rgba(18, 97, 255, 0.14);
    --success: #16e978;
    --success-glow: rgba(22, 233, 120, 0.34);
    --danger: #e0556d;
    --danger-glow: rgba(224, 85, 109, 0.24);
    --surface-shadow: rgba(16, 35, 77, 0.1);
}

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

body {
    font-family: 'Plus Jakarta Sans', sans-serif;
    background: var(--bg-color);
    background-image:
        radial-gradient(circle at 8% 8%, rgba(18, 97, 255, 0.16) 0%, transparent 34%),
        radial-gradient(circle at 88% 12%, rgba(0, 184, 232, 0.14) 0%, transparent 32%),
        radial-gradient(circle at 76% 96%, rgba(22, 233, 120, 0.12) 0%, transparent 38%),
        linear-gradient(135deg, #fbfdff 0%, #eef6ff 48%, #f9fffb 100%);
    color: var(--text-main);
    min-height: 100vh;
    overflow: hidden;
}

body.auth-pending .app-container,
body.auth-required .app-container,
body.auth-ready .auth-screen {
    display: none;
}

.auth-screen {
    width: 100vw;
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
}

.auth-panel {
    width: min(100%, 420px);
    padding: 32px;
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 24px 60px var(--surface-shadow);
    backdrop-filter: blur(18px);
}

.auth-logo {
    margin-bottom: 34px;
}

.auth-copy {
    margin-bottom: 28px;
}

.auth-copy h2 {
    font-family: 'Outfit', sans-serif;
    font-size: 2rem;
    font-weight: 600;
    margin-bottom: 8px;
    color: var(--text-main);
}

.auth-copy p {
    color: var(--text-muted);
    font-size: 0.95rem;
    line-height: 1.5;
}

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

.auth-form label {
    color: var(--text-main);
    font-size: 0.78rem;
    font-weight: 600;
}

.auth-form input {
    width: 100%;
    height: 48px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: rgba(246, 250, 255, 0.72);
    color: var(--text-main);
    font: inherit;
    padding: 0 14px;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
}

.auth-form input:focus {
    background: #ffffff;
    border-color: rgba(18, 97, 255, 0.42);
    box-shadow: 0 0 0 4px rgba(18, 97, 255, 0.1);
}

.auth-error {
    min-height: 20px;
    color: var(--danger);
    font-size: 0.8rem;
    line-height: 1.4;
}

.auth-submit {
    width: 100%;
    height: 48px;
    border-radius: 8px;
    margin-top: 2px;
    font-weight: 600;
}

.app-container {
    width: 100vw;
    height: 100vh;
    background: transparent;
    backdrop-filter: none;
    border: none;
    border-radius: 0;
    display: flex;
    flex-direction: column;
    position: relative;
}

header {
    padding: 24px 32px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}

.logo {
    display: flex;
    align-items: center;
    gap: 12px;
}

.logo-icon {
    width: 32px;
    height: 32px;
    background:
        linear-gradient(135deg, #0838e9 0%, #1261ff 40%, #00b8e8 68%, #16e978 100%);
    border-radius: 8px;
    box-shadow: 0 14px 30px rgba(18, 97, 255, 0.18);
}

h1 {
    font-size: 1.2rem;
    font-weight: 600;
    letter-spacing: -0.5px;
    font-family: 'Outfit', sans-serif;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 14px;
}

.instance-badge {
    display: flex;
    align-items: center;
    color: var(--text-muted);
    font-size: 0.75rem;
    background: var(--glass);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 10px 14px;
}

.instance-badge strong {
    color: var(--text-main);
    font-size: 0.95rem;
    font-weight: 600;
}

.status-badge {
    background: var(--glass);
    padding: 6px 12px;
    border-radius: 100px;
    font-size: 0.75rem;
    display: flex;
    align-items: center;
    gap: 8px;
    border: 1px solid var(--border);
    color: var(--text-muted);
}

.status-badge.is-active {
    color: var(--text-main);
}

.status-badge.is-inactive {
    color: var(--danger);
}

.status-dot {
    width: 8px;
    height: 8px;
    background: var(--accent-soft);
    border-radius: 50%;
    box-shadow: 0 0 8px rgba(0, 184, 232, 0.28);
}

.status-badge.is-active .status-dot {
    background: var(--success);
    box-shadow: 0 0 8px var(--success-glow);
}

.status-badge.is-inactive .status-dot {
    background: var(--danger);
    box-shadow: 0 0 8px var(--danger-glow);
}

main {
    flex: 1;
    overflow-y: auto;
    padding: 24px 32px 0;
    display: flex;
    flex-direction: column;
    gap: 24px;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
    min-height: 0;
}

main::-webkit-scrollbar {
    width: 6px;
}

main::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 10px;
}

.message-row {
    width: 100%;
    max-width: 880px;
    margin: 0 auto;
    display: flex;
}

.message-row.user {
    justify-content: flex-end;
}

.message-row.agent {
    justify-content: flex-start;
}

.welcome-message {
    text-align: center;
    width: min(100%, 720px);
    margin: 0 auto;
}

.welcome-message h2 {
    font-size: 2rem;
    margin-bottom: 8px;
    background: linear-gradient(100deg, var(--text-main), var(--primary), var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.message {
    max-width: min(80%, 820px);
    padding: 16px 20px;
    border-radius: 18px;
    line-height: 1.6;
    font-size: 0.95rem;
    position: relative;
    animation: fadeIn 0.3s ease-out;
}

.message.agent.has-chart {
    width: min(100%, 820px);
    max-width: min(100%, 820px);
}

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

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

.message.user {
    background: linear-gradient(135deg, var(--primary), var(--primary-strong));
    color: #ffffff;
    border-bottom-right-radius: 4px;
}

.message.agent {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.message.agent p {
    margin-bottom: 12px;
}

.message.agent p:last-child {
    margin-bottom: 0;
}

.message.agent ul,
.message.agent ol {
    margin-left: 20px;
    margin-bottom: 12px;
}

.message.agent li {
    margin-bottom: 6px;
}

.message.agent strong {
    color: var(--primary);
    font-weight: 600;
}

.message.agent code {
    background: rgba(0, 184, 232, 0.1);
    color: #0d418f;
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Courier New', Courier, monospace;
    font-size: 0.9em;
}

.message-chart {
    margin-top: 14px;
    min-height: 280px;
    width: 100%;
    max-width: 100%;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: rgba(246, 250, 255, 0.84);
    overflow-x: auto;
    overflow-y: hidden;
}

.message-chart .vega-embed,
.message-chart .vega-embed > div {
    width: 100%;
    min-width: 0;
}

.message-chart svg,
.message-chart canvas {
    display: block;
    max-width: 100%;
}

.message-chart .vega-embed summary {
    display: none;
}

footer {
    padding: 24px 32px;
    flex-shrink: 0;
}

#chat-form {
    display: flex;
    align-items: center;
    gap: 16px;
    width: 100%;
    max-width: 880px;
    margin: 0 auto;
    padding: 10px;
    background: rgba(255, 255, 255, 0.82);
    border: 1px solid var(--border);
    border-radius: 24px;
    box-shadow: 0 18px 45px var(--surface-shadow);
    backdrop-filter: blur(18px);
}

textarea {
    flex: 1;
    background: transparent;
    border: none;
    padding: 14px 20px;
    border-radius: 16px;
    color: var(--text-main);
    font-family: inherit;
    font-size: 0.95rem;
    line-height: 1.45;
    max-height: 180px;
    min-height: 48px;
    overflow-y: hidden;
    resize: none;
    transition: all 0.3s ease;
}

textarea::placeholder {
    color: var(--text-muted);
}

textarea:focus {
    outline: none;
    box-shadow: none;
}

button {
    background: linear-gradient(135deg, var(--primary), var(--primary-strong));
    color: #ffffff;
    border: none;
    width: 48px;
    height: 48px;
    border-radius: 999px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

button:hover {
    transform: scale(1.05);
    box-shadow: 0 0 20px var(--primary-glow);
    background: linear-gradient(135deg, #1a93ff, var(--primary-strong));
}

button:disabled {
    cursor: wait;
    opacity: 0.68;
    transform: none;
    box-shadow: none;
}

button svg {
    width: 20px;
    height: 20px;
}

.logout-button {
    width: 36px;
    height: 36px;
    background: rgba(255, 255, 255, 0.78);
    color: var(--text-muted);
    border: 1px solid var(--border);
    box-shadow: none;
}

.logout-button:hover {
    color: var(--danger);
    background: rgba(255, 255, 255, 0.95);
    box-shadow: 0 10px 24px rgba(224, 85, 109, 0.14);
}

.logout-button svg {
    width: 18px;
    height: 18px;
}

.app-container.is-empty main {
    justify-content: center;
    padding: 0 32px;
}

.app-container.is-empty footer {
    background: transparent;
    padding-top: 0;
    padding-bottom: 56px;
}

.app-container.is-empty #chat-form {
    max-width: 760px;
}

.app-container.is-empty textarea {
    padding-top: 18px;
    padding-bottom: 18px;
    font-size: 1rem;
    min-height: 60px;
}

@media (max-width: 840px) {
    body {
        min-height: 100vh;
    }

    .app-container {
        width: 100%;
        height: 100vh;
    }

    .auth-panel {
        padding: 26px;
    }

    .auth-copy h2 {
        font-size: 1.65rem;
    }

    header {
        align-items: flex-start;
        flex-direction: column;
        gap: 16px;
    }

    .header-actions {
        width: 100%;
        justify-content: space-between;
    }

    .message,
    .message.agent.has-chart {
        max-width: 100%;
        width: 100%;
    }

    .message-row {
        max-width: 100%;
    }

    main {
        padding: 20px 16px 0;
    }

    footer {
        padding: 20px 16px 28px;
    }

    #chat-form {
        gap: 12px;
        padding: 8px;
    }

    .app-container.is-empty main {
        padding: 0 16px;
    }

    .app-container.is-empty footer {
        padding: 0 16px 32px;
    }
}

.loading-dots {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 0;
    min-height: 1.4em;
    flex-shrink: 0;
}

.message.loading {
    display: inline-flex;
    align-items: center;
    gap: 12px;
    padding: 14px 18px;
}

.loading-text {
    color: var(--text-main);
    font-size: 0.9rem;
    line-height: 1.4;
}

.dot {
    width: 8px;
    height: 8px;
    background: var(--accent);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out both;
}

.dot:nth-child(1) {
    animation-delay: -0.32s;
}

.dot:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes bounce {
    0%,
    80%,
    100% {
        transform: scale(0);
    }

    40% {
        transform: scale(1);
    }
}
