:root {
    --primary: #10B981;
    --primary-hover: #059669;
    --primary-light: rgba(16, 185, 129, 0.1);

    --bg-body: #050505;
    --bg-card: #0a0a0a;
    --bg-input: #141414;
    --bg-header: rgba(10, 10, 10, 0.85);

    --text-main: #E5E7EB;
    --text-muted: #9CA3AF;
    --text-inverted: #000000;

    --border: #1f1f1f;
    --accent-secondary: #10B981;

    --bubble-user: #10B981;
    --bubble-assistant: #141414;
    --bubble-system: #3F1C1C;
    --text-system: #FCA5A5;

    --radius-lg: 16px;
    --radius-md: 12px;
    --radius-sm: 8px;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .5);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, .5);
    --shadow-lg: 0 10px 30px rgba(0, 0, 0, .7);
    --glow: 0 0 20px rgba(16, 185, 129, .2);

    --font-main: 'Inter', system-ui, sans-serif;
    --font-mono: 'Space Mono', monospace;
}

[data-theme="light"] {
    --bg-body: #F0F4F0;
    --bg-card: #FFFFFF;
    --bg-input: #F3F7F3;
    --bg-header: rgba(255, 255, 255, 0.85);
    --text-main: #111827;
    --text-muted: #6B7280;
    --border: #D1FAE5;
    --bubble-assistant: #F0F4F0;
    --bubble-system: #FEF2F2;
    --text-system: #B91C1C;
    --primary-light: rgba(16, 185, 129, .15);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .05);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, .1);
    --shadow-lg: 0 10px 30px rgba(0, 0, 0, .12);
    --glow: 0 0 20px rgba(16, 185, 129, .1);
}

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

body {
    font-family: var(--font-main);
    background: var(--bg-body);
    color: var(--text-main);
    height: 100vh;
    width: 100vw;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1.5;
    transition: background-color .3s, color .3s;
    position: relative;
    overflow: hidden;
}

/* ── Matrix canvas background ───────────────────────────── */
#matrixCanvas {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 0;
    opacity: 0.04;
}

[data-theme="light"] #matrixCanvas {
    opacity: 0.03;
}

/* ── Scan-line overlay ──────────────────────────────────── */
body::before {
    content: '';
    position: fixed;
    inset: 0;
    background: repeating-linear-gradient(0deg,
            transparent,
            transparent 2px,
            rgba(0, 0, 0, .04) 2px,
            rgba(0, 0, 0, .04) 4px);
    pointer-events: none;
    z-index: 1;
}

/* ── Grid dots bg ───────────────────────────────────────── */
body::after {
    content: '';
    position: fixed;
    inset: 0;
    background-image: radial-gradient(circle, rgba(16, 185, 129, .12) 1px, transparent 1px);
    background-size: 28px 28px;
    pointer-events: none;
    z-index: 0;
}

[data-theme="light"] body::after {
    background-image: radial-gradient(circle, rgba(16, 185, 129, .18) 1px, transparent 1px);
}

/* Wrapper sits above canvas/overlays */
#authBox,
.app {
    position: relative;
    z-index: 10;
}

/* ── Typography & Logo ──────────────────────────────────── */
h1,
h2,
h3,
.logo {
    font-family: var(--font-mono);
}

.logo {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-main);
    text-decoration: none;
    letter-spacing: -0.5px;
    display: inline-block;
    position: relative;
}

/* Glitch effect */
.logo::before,
.logo::after {
    content: attr(data-text);
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    overflow: hidden;
    font-family: var(--font-mono);
    font-weight: 700;
}

.logo::before {
    color: #0ff;
    animation: glitch-1 4s infinite;
    clip-path: polygon(0 20%, 100% 20%, 100% 40%, 0 40%);
}

.logo::after {
    color: var(--primary);
    animation: glitch-2 4s infinite, blink 1.5s ease-in-out infinite;
}

/* When used as cursor, only apply blink on the ::after of .logo-cursor */
.logo-cursor::after {
    content: "▸";
    font-style: normal;
    margin-left: 4px;
    animation: blink 1.5s ease-in-out infinite;
    color: var(--accent-secondary);
}

.logo-cursor::before {
    display: none;
}

@keyframes blink {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0;
    }
}

@keyframes glitch-1 {

    0%,
    90%,
    100% {
        transform: none;
        opacity: 0;
    }

    91% {
        transform: translateX(-3px);
        opacity: .7;
        clip-path: polygon(0 10%, 100% 10%, 100% 30%, 0 30%);
    }

    93% {
        transform: translateX(3px);
        opacity: .7;
        clip-path: polygon(0 60%, 100% 60%, 100% 80%, 0 80%);
    }

    95% {
        transform: translateX(-2px);
        opacity: .7;
    }

    97% {
        transform: none;
        opacity: 0;
    }
}

@keyframes glitch-2 {

    0%,
    88%,
    100% {
        transform: none;
        opacity: 0;
    }

    89% {
        transform: translateX(3px);
        opacity: .8;
        clip-path: polygon(0 50%, 100% 50%, 100% 70%, 0 70%);
    }

    91% {
        transform: translateX(-2px);
        opacity: .6;
    }

    93% {
        transform: none;
        opacity: 0;
    }
}

/* ── Auth box ───────────────────────────────────────────── */
.auth-box {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg), var(--glow);
    padding: 2.5rem;
    max-width: 420px;
    width: 100%;
    animation: slideUp 0.4s ease-out;
    transition: background-color .3s, border-color .3s;
    /* Corner accents */
    position: relative;
    overflow: hidden;
}

/* Animated corner line accent */
.auth-box::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: linear-gradient(90deg, transparent, var(--primary), transparent);
    animation: scan-h 3s ease-in-out infinite;
}

@keyframes scan-h {
    0% {
        background-position: -200% 0;
        opacity: 0;
    }

    20% {
        opacity: 1;
    }

    80% {
        opacity: 1;
    }

    100% {
        background-position: 200% 0;
        opacity: 0;
    }
}

.form-group {
    margin-bottom: 1.25rem;
}

.form-group label {
    display: block;
    margin-bottom: .5rem;
    font-size: .8rem;
    font-weight: 500;
    color: var(--primary);
    font-family: var(--font-mono);
    letter-spacing: 1px;
    text-transform: uppercase;
}

.form-group input {
    width: 100%;
    padding: .75rem 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: .9rem;
    background: var(--bg-input);
    color: var(--text-main);
    font-family: var(--font-mono);
    transition: all .2s;
}

.form-group input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 1px var(--primary), 0 0 12px rgba(16, 185, 129, .2);
}

.btn {
    border: none;
    padding: .75rem 1.5rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-weight: 700;
    font-size: .85rem;
    transition: all .2s;
    width: 100%;
    margin-bottom: .5rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-family: var(--font-mono);
    text-transform: uppercase;
    letter-spacing: 1px;
    position: relative;
    overflow: hidden;
}

/* Shimmer effect on primary btn */
.btn-primary {
    background: var(--primary);
    color: #000;
}

.btn-primary::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 60%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .3), transparent);
    transition: left .4s;
}

.btn-primary:hover::before {
    left: 140%;
}

.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 16px rgba(16, 185, 129, .4);
}

.btn-ghost {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
}

.btn-ghost:hover {
    background: var(--bg-input);
    color: var(--text-main);
    border-color: var(--primary);
}

.alert {
    padding: .75rem 1rem;
    border-radius: var(--radius-sm);
    margin-bottom: 1rem;
    font-size: .85rem;
    display: flex;
    align-items: center;
    gap: .5rem;
    font-family: var(--font-mono);
}

.alert-error {
    background: rgba(220, 38, 38, .1);
    color: #F87171;
    border: 1px solid #7F1D1D;
}

/* ── App ────────────────────────────────────────────────── */
.app {
    background: var(--bg-card);
    border: none;
    border-radius: 0;
    box-shadow: none;
    width: 100vw;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: fadeIn .4s ease-out;
    transition: background-color .3s;
}

.app-header {
    background: var(--bg-header);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
    padding: 1rem 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
    z-index: 10;
    transition: border-color .3s;
    position: relative;
}

/* Top border light sweep on header */
.app-header::after {
    content: '';
    position: absolute;
    bottom: -1px;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--primary), transparent);
    animation: scan-h 4s ease-in-out infinite;
}

.header-right {
    display: flex;
    gap: .75rem;
    align-items: center;
}

.username {
    font-size: .8rem;
    color: var(--text-muted);
    font-family: var(--font-mono);
}

.btn-sm {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
    padding: .35rem .75rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: .75rem;
    font-weight: 600;
    transition: all .2s;
    font-family: var(--font-mono);
    text-transform: uppercase;
    letter-spacing: .5px;
}

.btn-sm:hover {
    border-color: var(--primary);
    color: var(--primary);
    box-shadow: 0 0 8px rgba(16, 185, 129, .2);
}

/* ── Chat messages ──────────────────────────────────────── */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    background: var(--bg-card);
    scroll-behavior: smooth;
    transition: background-color .3s;
}

.message {
    display: flex;
    gap: 1rem;
    max-width: 85%;
    animation: slideIn .3s ease-out;
}

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

.message.assistant {
    align-self: flex-start;
}

.avatar {
    width: 32px;
    height: 32px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    background: var(--bg-input);
    border: 1px solid var(--border);
    color: var(--text-muted);
    transition: background-color .3s, border-color .3s;
}

.message.user .avatar {
    background: var(--primary-light);
    color: var(--primary);
    border-color: rgba(16, 185, 129, .3);
}

.message.assistant .avatar {
    background: var(--bg-input);
    color: var(--primary);
}

.message.system .avatar {
    background: rgba(220, 38, 38, .1);
    color: #F87171;
}

.bubble {
    padding: 1rem 1.25rem;
    border-radius: var(--radius-md);
    line-height: 1.6;
    font-size: .95rem;
    position: relative;
    transition: background-color .3s, border-color .3s, color .3s;
}

.message.user .bubble {
    background: var(--primary-light);
    color: var(--primary);
    border: 1px solid rgba(16, 185, 129, .2);
    border-bottom-right-radius: 2px;
    font-family: var(--font-mono);
    font-size: .85rem;
}

.message.assistant .bubble {
    background: var(--bg-input);
    color: var(--text-main);
    border: 1px solid var(--border);
    border-bottom-left-radius: 2px;
}

.message.system .bubble {
    background: rgba(63, 28, 28, .5);
    color: var(--text-system);
    border: 1px solid #7F1D1D;
    width: 100%;
    font-family: var(--font-mono);
    font-size: .85rem;
}

/* SQL Block */
.sql-block {
    background: #000;
    color: #6EE7B7;
    padding: 1rem;
    border-radius: var(--radius-sm);
    margin-top: 1rem;
    font-family: var(--font-mono);
    font-size: .8rem;
    overflow-x: auto;
    white-space: pre;
    border: 1px solid rgba(16, 185, 129, .3);
    box-shadow: inset 0 0 20px rgba(16, 185, 129, .05), 0 0 10px rgba(16, 185, 129, .1);
    position: relative;
}

.sql-block::before {
    content: 'SQL';
    position: absolute;
    top: .35rem;
    right: .75rem;
    font-size: .65rem;
    color: rgba(16, 185, 129, .5);
    letter-spacing: 2px;
}

/* Table */
.results-table {
    margin-top: 1rem;
    overflow-x: auto;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--bg-input);
    transition: background-color .3s, border-color .3s;
}

.results-table table {
    width: 100%;
    border-collapse: collapse;
    font-size: .82rem;
    font-family: var(--font-mono);
}

.results-table th {
    background: rgba(16, 185, 129, .07);
    padding: .6rem 1rem;
    text-align: left;
    font-weight: 700;
    color: var(--primary);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
    letter-spacing: .5px;
    text-transform: uppercase;
    font-size: .75rem;
}

.results-table td {
    padding: .6rem 1rem;
    border-bottom: 1px solid var(--border);
    color: var(--text-main);
}

.results-table tr:last-child td {
    border-bottom: none;
}

.results-table tr:hover td {
    background: rgba(16, 185, 129, .04);
}

.row-count {
    margin-top: .5rem;
    font-size: .75rem;
    color: var(--primary);
    font-family: var(--font-mono);
    text-align: right;
    opacity: .6;
}

/* Typing indicator */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: .75rem 1rem;
    background: var(--bg-input);
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    width: fit-content;
    border-bottom-left-radius: 2px;
}

.typing-indicator span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--primary);
    animation: typingBounce .9s ease-in-out infinite;
    opacity: .3;
}

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

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

@keyframes typingBounce {

    0%,
    80%,
    100% {
        transform: scale(1);
        opacity: .3;
    }

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

/* Empty state */
.empty-chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    text-align: center;
    padding: 2rem;
    gap: 1rem;
    opacity: .6;
}

.empty-chat h3 {
    font-family: var(--font-mono);
    font-size: 1rem;
    color: var(--primary);
    letter-spacing: 2px;
}

.empty-chat p {
    font-family: var(--font-mono);
    font-size: .8rem;
}

/* Status tag */
.status-tag {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-family: var(--font-mono);
    font-size: .7rem;
    color: var(--primary);
    letter-spacing: 1px;
    text-transform: uppercase;
}

.status-tag::before {
    content: '';
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--primary);
    box-shadow: 0 0 6px var(--primary);
    animation: pulse-dot 2s ease-in-out infinite;
}

@keyframes pulse-dot {

    0%,
    100% {
        box-shadow: 0 0 4px var(--primary);
    }

    50% {
        box-shadow: 0 0 12px var(--primary), 0 0 4px var(--primary);
    }
}

/* ── Input area ─────────────────────────────────────────── */
.chat-input-area {
    padding: 1.25rem 1.5rem;
    border-top: 1px solid var(--border);
    background: var(--bg-card);
    flex-shrink: 0;
    transition: background-color .3s, border-color .3s;
}

.input-row {
    display: flex;
    gap: .75rem;
    align-items: flex-end;
    background: var(--bg-input);
    padding: .5rem;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    transition: border-color .2s, box-shadow .2s;
}

.input-row:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 1px rgba(16, 185, 129, .3), 0 0 16px rgba(16, 185, 129, .1);
}

textarea.chat-input {
    flex: 1;
    padding: .5rem .75rem;
    border: none;
    font-size: .9rem;
    resize: none;
    font-family: var(--font-mono);
    background: transparent;
    color: var(--text-main);
    min-height: 40px;
    max-height: 150px;
    line-height: 1.5;
    caret-color: var(--primary);
}

textarea.chat-input:focus {
    outline: none;
}

textarea.chat-input::placeholder {
    color: var(--text-muted);
    opacity: .5;
}

.send-btn {
    background: var(--primary);
    color: #000;
    border: none;
    width: 36px;
    height: 36px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all .2s;
    flex-shrink: 0;
}

.send-btn:hover:not(:disabled) {
    background: var(--primary-hover);
    transform: scale(1.08);
    box-shadow: 0 0 12px rgba(16, 185, 129, .4);
}

.send-btn:disabled {
    background: var(--border);
    color: var(--text-muted);
    cursor: not-allowed;
}

.new-session-row {
    margin-top: .75rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.new-session-link {
    font-size: .7rem;
    color: var(--text-muted);
    cursor: pointer;
    background: none;
    border: none;
    display: inline-flex;
    align-items: center;
    gap: .4rem;
    padding: .35rem .7rem;
    border-radius: var(--radius-sm);
    font-family: var(--font-mono);
    text-transform: uppercase;
    letter-spacing: .5px;
    transition: all .2s;
}

.new-session-link:hover {
    color: var(--primary);
    background: rgba(16, 185, 129, .06);
}

.powered-by {
    font-family: var(--font-mono);
    font-size: .65rem;
    color: var(--text-muted);
    opacity: .5;
    text-align: right;
    letter-spacing: 1px;
    text-transform: uppercase;
}

.hidden {
    display: none !important;
}

/* ── Animations ─────────────────────────────────────────── */
@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

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

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

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(12px);
    }

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

/* ── Mobile ─────────────────────────────────────────────── */
@media (max-width: 640px) {
    .auth-box {
        border: none;
        padding: 1.5rem;
        background: transparent;
        box-shadow: none;
    }

    .chat-messages {
        padding: 1rem;
    }
}

/* ── BI Components ──────────────────────────────────────── */

/* Insight text: styled differently from the old plain text */
.insight-text {
    font-size: .93rem;
    line-height: 1.65;
    color: var(--text-main);
    margin-bottom: .25rem;
}

/* Metric cards row */
.metric-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
    gap: .6rem;
    margin-top: 1rem;
}

.metric-card {
    background: rgba(16, 185, 129, .06);
    border: 1px solid rgba(16, 185, 129, .18);
    border-radius: var(--radius-sm);
    padding: .65rem .9rem;
    text-align: center;
    transition: background .2s, border-color .2s;
}

.metric-card:hover {
    background: rgba(16, 185, 129, .12);
    border-color: rgba(16, 185, 129, .35);
}

.metric-label {
    font-family: var(--font-mono);
    font-size: .65rem;
    color: var(--primary);
    letter-spacing: 1px;
    text-transform: uppercase;
    margin-bottom: .3rem;
    opacity: .7;
}

.metric-value {
    font-family: var(--font-mono);
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-main);
    letter-spacing: -.5px;
}

/* Chart toggle buttons */
.chart-toggle-row {
    display: flex;
    gap: .4rem;
    margin-top: 1rem;
    margin-bottom: .5rem;
}

.chart-toggle-btn {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-muted);
    font-family: var(--font-mono);
    font-size: .72rem;
    font-weight: 600;
    letter-spacing: .5px;
    padding: .3rem .8rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all .2s;
    text-transform: uppercase;
}

.chart-toggle-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
}

.chart-toggle-btn.active {
    background: rgba(16, 185, 129, .12);
    border-color: var(--primary);
    color: var(--primary);
    box-shadow: 0 0 8px rgba(16, 185, 129, .15);
}

/* Chart canvas wrapper */
.chart-container {
    margin-top: .5rem;
    padding: 1rem;
    background: rgba(0, 0, 0, .25);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    max-height: 340px;
    transition: background .3s, border-color .3s;
}

[data-theme="light"] .chart-container {
    background: rgba(255, 255, 255, .6);
}

.chart-container canvas {
    max-height: 300px;
}

/* ── Suggestion chips ───────────────────────────────────── */
.suggestion-chips {
    display: flex;
    flex-wrap: wrap;
    gap: .6rem;
    justify-content: center;
    margin-top: 1.25rem;
    max-width: 620px;
}

.chip {
    background: rgba(16, 185, 129, .06);
    border: 1px solid rgba(16, 185, 129, .2);
    color: var(--text-muted);
    font-family: var(--font-mono);
    font-size: .75rem;
    padding: .5rem 1rem;
    border-radius: 999px;
    cursor: pointer;
    transition: all .2s;
    letter-spacing: .3px;
}

.chip:hover {
    background: rgba(16, 185, 129, .15);
    border-color: var(--primary);
    color: var(--primary);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(16, 185, 129, .2);
}