:root {
    --bg: #f4f7fb;
    --panel: #ffffff;
    --panel-2: #f8fbff;
    --line: #e5ecf4;
    --text: #142033;
    --muted: #61708a;
    --primary: #3b82f6;
    --primary-deep: #1d4ed8;
    --success: #1f9d67;
    --warning: #d97706;
    --danger: #dc2626;
    --shadow: 0 18px 50px rgba(15, 23, 42, 0.08);
    --radius: 18px;
    --radius-sm: 12px;
    --sidebar: #0f172a;
    --sidebar-text: #cbd5e1;
    --sidebar-muted: #94a3b8;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; color: var(--text); background: var(--bg); }
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
input, select, textarea {
    width: 100%;
    border: 1px solid var(--line);
    background: #fff;
    border-radius: 14px;
    padding: 0.85rem 1rem;
    color: var(--text);
    outline: none;
    transition: border-color .2s ease, box-shadow .2s ease, transform .2s ease;
}
input:focus, select:focus, textarea:focus {
    border-color: rgba(59,130,246,.45);
    box-shadow: 0 0 0 4px rgba(59,130,246,.12);
}
label { display: block; font-size: .9rem; font-weight: 600; margin-bottom: .5rem; }
textarea { resize: vertical; min-height: 110px; }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

.app-shell { min-height: 100vh; }
.app-grid { display: grid; grid-template-columns: 280px minmax(0, 1fr); min-height: 100vh; }
.app-main { min-width: 0; display: flex; flex-direction: column; }
.page { padding: 1.5rem 1.5rem 2rem; }
.mobile-only { display: none; }

.sidebar {
    background: linear-gradient(180deg, #0f172a 0%, #111b33 100%);
    color: var(--sidebar-text);
    padding: 1.35rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    min-height: 100vh;
    position: sticky;
    top: 0;
}
.brand-block { display: flex; align-items: center; gap: .9rem; }
.brand-mark {
    width: 42px; height: 42px; border-radius: 14px;
    display: grid; place-items: center;
    font-weight: 800; color: #fff;
    background: linear-gradient(135deg, #60a5fa 0%, #2563eb 100%);
    box-shadow: 0 14px 30px rgba(37,99,235,.3);
}
.sidebar-nav { display: grid; gap: .35rem; }
.nav-link {
    display: flex; align-items: center; justify-content: space-between;
    padding: .92rem 1rem;
    border-radius: 14px;
    color: var(--sidebar-text);
    transition: background .2s ease, transform .2s ease, color .2s ease;
}
.nav-link:hover, .nav-link.is-active {
    background: rgba(148, 163, 184, 0.14);
    color: #fff;
    transform: translateX(2px);
}
.sidebar-foot { margin-top: auto; display: grid; gap: .9rem; }
.context-card, .user-card {
    border: 1px solid rgba(148, 163, 184, .16);
    background: rgba(15, 23, 42, .35);
    padding: .95rem 1rem;
    border-radius: 16px;
}

.topbar {
    display: flex; align-items: center; justify-content: space-between;
    gap: 1rem; padding: 1rem 1.5rem; background: rgba(244,247,251,.84); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 30;
}
.topbar-left { display: flex; align-items: center; gap: 1rem; }
.topbar h1 { margin: .15rem 0 0; font-size: 1.55rem; }
.topbar-actions { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; }

.eyebrow { text-transform: uppercase; letter-spacing: .12em; font-size: .72rem; color: var(--muted); font-weight: 800; }
.small { font-size: .84rem; }
.muted { color: var(--muted); }
.muted-light { color: rgba(255,255,255,.78); }
.line-clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.card {
    background: var(--panel);
    border: 1px solid rgba(229, 236, 244, .9);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.card-head, .section-head {
    display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem;
}
.card-head { margin-bottom: 1.25rem; }
.hero, .form-card, .table-card, .chart-card, .list-card, .settings-card, .notes-card, .install-card, .token-card { padding: 1.25rem; }
.hero {
    display: flex; align-items: center; justify-content: space-between; gap: 1rem;
}
.hero.compact { padding: 1.2rem 1.25rem; }
.hero h2, .card-head h1, .card-head h2, .section-head h3 { margin: .2rem 0; }
.hero-actions { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; }
.hero-meta { text-align: right; }
.card-gradient {
    background: radial-gradient(circle at top right, rgba(147,197,253,.5), rgba(59,130,246,.18) 35%, #10213f 100%);
    color: #fff;
    border: 0;
}
.card-gradient .eyebrow { color: rgba(255,255,255,.7); }
.card-gradient h1, .card-gradient h2, .card-gradient p { color: #fff; }

.btn {
    border: 0; border-radius: 14px; padding: .82rem 1rem; font-weight: 700;
    display: inline-flex; align-items: center; justify-content: center; gap: .5rem;
    transition: transform .16s ease, box-shadow .16s ease, background .16s ease, color .16s ease;
}
.btn:hover { transform: translateY(-1px); }
.btn-block { width: 100%; }
.btn-sm { padding: .55rem .85rem; border-radius: 12px; font-size: .9rem; }
.btn-primary { background: linear-gradient(135deg, var(--primary) 0%, var(--primary-deep) 100%); color: #fff; box-shadow: 0 14px 30px rgba(29,78,216,.2); }
.btn-ghost { background: #eef4fb; color: var(--text); }
.btn-light { background: #fff; color: var(--primary-deep); }
.btn-ghost-light { background: rgba(255,255,255,.12); color: #fff; border: 1px solid rgba(255,255,255,.2); }
.icon-btn { border: 0; background: #eef4fb; width: 42px; height: 42px; border-radius: 13px; }
.text-link { color: var(--primary-deep); font-weight: 700; }
.pill { background: #eef4fb; color: var(--text); padding: .55rem .85rem; border-radius: 999px; font-size: .9rem; }

.badge {
    display: inline-flex; align-items: center; justify-content: center;
    min-width: 78px; padding: .44rem .68rem; border-radius: 999px; font-size: .78rem; font-weight: 800;
}
.badge-primary { background: rgba(59,130,246,.12); color: var(--primary-deep); }
.badge-success { background: rgba(31,157,103,.12); color: var(--success); }
.badge-warning { background: rgba(217,119,6,.12); color: var(--warning); }
.badge-danger { background: rgba(220,38,38,.12); color: var(--danger); }
.badge-muted { background: rgba(100,116,139,.14); color: #475569; }
.badge-row { display: flex; gap: .55rem; flex-wrap: wrap; }

.flash-stack { display: grid; gap: .75rem; margin-bottom: 1rem; }
.flash {
    padding: .95rem 1rem; border-radius: 14px; font-weight: 600; border: 1px solid transparent;
}
.flash-success { background: rgba(31,157,103,.1); color: #126647; border-color: rgba(31,157,103,.18); }
.flash-error { background: rgba(220,38,38,.08); color: #a61d24; border-color: rgba(220,38,38,.16); }
.flash-warning { background: rgba(217,119,6,.12); color: #9a5800; border-color: rgba(217,119,6,.16); }

.stats-grid { display: grid; gap: 1rem; }
.stats-grid.four { grid-template-columns: repeat(4, minmax(0,1fr)); }
.stats-grid.five { grid-template-columns: repeat(5, minmax(0,1fr)); }
.stat-card { padding: 1.15rem 1.2rem; }
.stat-card span { display: block; color: var(--muted); font-size: .9rem; margin-bottom: .55rem; }
.stat-card strong { font-size: 1.95rem; line-height: 1; }

.dashboard-grid { display: grid; gap: 1rem; grid-template-columns: 1.55fr 1fr; }
.dashboard-grid.admin-two-col { grid-template-columns: 1fr 1fr; }
.report-sections { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }

.simple-chart { display: flex; align-items: flex-end; gap: 1rem; min-height: 230px; padding: 1rem 0 .25rem; overflow-x: auto; }
.simple-chart.tall { min-height: 290px; }
.chart-col { min-width: 46px; display: flex; flex-direction: column; align-items: center; gap: .6rem; }
.chart-bar {
    width: 36px; border-radius: 999px 999px 10px 10px;
    background: linear-gradient(180deg, #7dd3fc 0%, #3b82f6 85%);
    box-shadow: 0 12px 25px rgba(59,130,246,.18);
}
.chart-col span { color: var(--muted); font-size: .8rem; white-space: nowrap; }

.list-stack { display: grid; gap: .75rem; }
.list-item {
    display: flex; align-items: center; justify-content: space-between; gap: .9rem;
    padding: .95rem 1rem; border: 1px solid var(--line); border-radius: 14px;
    transition: background .16s ease, transform .16s ease, border-color .16s ease;
}
.list-item:hover { background: var(--panel-2); border-color: #dbe7f4; transform: translateY(-1px); }
.list-item.static { cursor: default; }

.table-wrap { overflow: auto; }
table { width: 100%; border-collapse: collapse; min-width: 820px; }
th, td { padding: .95rem .9rem; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: .82rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.actions-cell { display: flex; gap: .55rem; align-items: center; }

.page-actions, .search-row, .filter-actions, .form-actions { display: flex; gap: .75rem; align-items: center; }
.page-actions { justify-content: space-between; flex-wrap: wrap; }
.search-row { flex: 1; max-width: 480px; }
.filter-card { padding: 1rem; }
.filter-card .form-grid { align-items: end; }
.form-grid { display: grid; gap: 1rem; }
.form-grid.two-col { grid-template-columns: repeat(2, minmax(0,1fr)); }
.form-grid.four-col { grid-template-columns: 2fr 1fr 1fr auto; }
.col-span-2 { grid-column: span 2; }
.stack-sm > * + *, .stack-md > * + *, .stack-lg > * + * { margin-top: 0; }
.stack-sm { display: grid; gap: .7rem; }
.stack-md { display: grid; gap: 1rem; }
.stack-lg { display: grid; gap: 1.2rem; }
.narrow { max-width: 920px; }
.divider { height: 1px; background: var(--line); }

.chat-layout { display: grid; grid-template-columns: minmax(0, 1.7fr) 380px; gap: 1rem; align-items: start; }
.chat-thread { padding: 1.1rem; display: grid; gap: 1rem; }
.thread-head { border-bottom: 1px solid var(--line); padding-bottom: 1rem; }
.message-list {
    display: flex; flex-direction: column; gap: .9rem; max-height: 68vh; overflow: auto; padding-right: .2rem;
}
.message-bubble {
    max-width: min(78%, 640px); padding: .9rem 1rem; border-radius: 18px; border: 1px solid var(--line); background: #fff;
}
.message-bubble.inbound { align-self: flex-start; background: #fff; }
.message-bubble.outbound { align-self: flex-end; background: #eff6ff; border-color: #dbeafe; }
.message-bubble.ai { box-shadow: inset 0 0 0 1px rgba(59,130,246,.08); }
.message-bubble.human { box-shadow: inset 0 0 0 1px rgba(16,185,129,.08); }
.message-meta {
    display: flex; align-items: center; gap: .5rem; flex-wrap: wrap;
    font-size: .76rem; color: var(--muted); margin-bottom: .55rem;
}
.message-content { white-space: pre-wrap; line-height: 1.6; }
.composer { border-top: 1px solid var(--line); padding-top: 1rem; display: grid; gap: .85rem; }
.composer-actions { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.chat-sidebar { position: sticky; top: 92px; }
.note-card { border: 1px solid var(--line); border-radius: 14px; padding: .9rem; background: var(--panel-2); }
.notes-list { display: grid; gap: .8rem; }

.empty-state {
    padding: 1.4rem; text-align: center; color: var(--muted);
    border: 1px dashed #d8e2ee; border-radius: 14px; background: #fbfdff;
}
.empty-state.compact { padding: .95rem; }

.auth-shell {
    min-height: 100vh; display: grid; place-items: center; padding: 2rem;
    background:
        radial-gradient(circle at top left, rgba(96,165,250,.24), transparent 34%),
        radial-gradient(circle at bottom right, rgba(14,165,233,.24), transparent 30%),
        linear-gradient(180deg, #f7fbff 0%, #edf4fb 100%);
}
.auth-card-wrap {
    width: min(1120px, 100%); display: grid; grid-template-columns: 1.15fr 420px; gap: 1rem; align-items: stretch;
}
.auth-brand-panel, .auth-card { padding: 1.5rem; }
.auth-brand-panel h1 { font-size: 2.2rem; line-height: 1.08; margin: .3rem 0 1rem; max-width: 14ch; }
.feature-list { display: flex; gap: .75rem; flex-wrap: wrap; margin-top: 1.5rem; }
.feature-chip {
    background: rgba(255,255,255,.12); color: #fff; border: 1px solid rgba(255,255,255,.18);
    border-radius: 999px; padding: .6rem .9rem; font-weight: 700;
}

.install-shell { min-height: 100vh; display: grid; place-items: center; padding: 2rem; background: linear-gradient(180deg, #f7fbff 0%, #edf4fb 100%); }
.install-wrap { width: min(960px, 100%); }
.install-card { padding: 1.5rem; }

.token-output {
    display: block; white-space: pre-wrap; word-break: break-all;
    background: #0f172a; color: #dbeafe; padding: 1rem; border-radius: 16px; font-size: .95rem;
}
.report-json {
    margin: 0; max-height: 360px; overflow: auto; padding: 1rem; border-radius: 16px; background: #0f172a; color: #d9e7ff;
}

@media (max-width: 1200px) {
    .stats-grid.five { grid-template-columns: repeat(3, minmax(0,1fr)); }
    .dashboard-grid { grid-template-columns: 1fr; }
    .chat-layout { grid-template-columns: 1fr; }
    .chat-sidebar { position: static; }
}

@media (max-width: 992px) {
    .app-grid { grid-template-columns: 1fr; }
    .sidebar {
        position: fixed; inset: 0 auto 0 0; width: 300px; transform: translateX(-102%); z-index: 50; transition: transform .25s ease;
    }
    .app-grid.sidebar-open .sidebar { transform: translateX(0); }
    .mobile-only { display: inline-flex; }
    .stats-grid.four, .stats-grid.five, .form-grid.two-col, .dashboard-grid.admin-two-col { grid-template-columns: repeat(2, minmax(0,1fr)); }
    .form-grid.four-col { grid-template-columns: repeat(2, minmax(0,1fr)); }
    .filter-actions { grid-column: span 2; }
    .auth-card-wrap { grid-template-columns: 1fr; }
}

@media (max-width: 720px) {
    .page, .topbar { padding-inline: 1rem; }
    .hero, .page-actions, .composer-actions, .topbar, .topbar-left { flex-direction: column; align-items: stretch; }
    .topbar-actions { justify-content: stretch; }
    .stats-grid.four, .stats-grid.five, .form-grid.two-col, .form-grid.four-col, .dashboard-grid.admin-two-col { grid-template-columns: 1fr; }
    .search-row { max-width: none; flex-direction: column; align-items: stretch; }
    .chat-thread, .settings-card, .notes-card, .hero, .form-card, .table-card { padding: 1rem; }
    .message-bubble { max-width: 92%; }
    .col-span-2, .filter-actions { grid-column: auto; }
    table { min-width: 680px; }
}
