:root {
    --bg: #0e1117;
    --surface: #161b22;
    --surface-2: #1c2330;
    --border: #2a313c;
    --text: #e6edf3;
    --muted: #8b949e;
    --accent: #4c8bf5;
    --accent-soft: #1f6feb33;
    --danger: #f85149;
    --ok: #3fb950;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
}

/* ----- App layout ----- */
.layout { display: flex; min-height: 100vh; }

.sidebar {
    width: 260px;
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 1rem 0.75rem;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 700;
    font-size: 1.2rem;
    padding: 0.5rem 0.75rem 1rem;
}

.sidebar-nav { flex: 1; }
.nav-section { margin-bottom: 1.5rem; }
.nav-section-title {
    text-transform: uppercase;
    font-size: 0.7rem;
    letter-spacing: 0.06em;
    color: var(--muted);
    margin: 0 0.75rem 0.4rem;
}
.nav-section ul { list-style: none; margin: 0; padding: 0; }

.nav-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-radius: 8px;
    margin-bottom: 2px;
}
.nav-item a, .nav-item > span {
    display: block;
    width: 100%;
    padding: 0.5rem 0.75rem;
    color: var(--text);
    text-decoration: none;
    border-radius: 8px;
}
.nav-item a:hover { background: var(--surface-2); }
.nav-item.active a { background: var(--accent-soft); color: #fff; font-weight: 600; }
.nav-item.disabled { color: var(--muted); }
.nav-item.disabled > span:first-child { color: var(--muted); cursor: default; }
.nav-tag {
    font-size: 0.6rem;
    color: var(--muted);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 1px 6px;
    margin-right: 0.5rem;
}

.sidebar-footer {
    border-top: 1px solid var(--border);
    padding: 0.75rem;
    font-size: 0.8rem;
}
.user-email { color: var(--muted); margin: 0 0 0.5rem; word-break: break-all; }
.logout-link { color: var(--accent); text-decoration: none; }
.logout-link:hover { text-decoration: underline; }

/* ----- Content ----- */
.content { flex: 1; padding: 1.5rem 2rem; min-width: 0; }
.page-header h1 { margin: 0 0 1rem; font-size: 1.5rem; }
.muted { color: var(--muted); }
.placeholder {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 2rem;
}

/* ----- Messages ----- */
.messages { list-style: none; padding: 0; margin: 0 0 1rem; }
.message { padding: 0.6rem 0.9rem; border-radius: 8px; margin-bottom: 0.5rem; }
.message.error { background: #f8514922; border: 1px solid var(--danger); }
.message.success { background: #3fb95022; border: 1px solid var(--ok); }
.message.info { background: var(--surface-2); border: 1px solid var(--border); }

/* ----- Login ----- */
.login-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
}
.login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 2rem;
    width: 360px;
    max-width: 90vw;
}
.login-brand { font-size: 1.4rem; font-weight: 700; margin-bottom: 1.5rem; }
.login-card h1 { font-size: 1.2rem; margin: 0 0 1rem; }
.login-card label { display: block; margin: 0.75rem 0 0.25rem; color: var(--muted); }
.login-card input {
    width: 100%;
    padding: 0.6rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
}
.login-card button, .content button {
    margin-top: 1rem;
    width: 100%;
    padding: 0.6rem;
    border: none;
    border-radius: 8px;
    background: var(--accent);
    color: #fff;
    font-weight: 600;
    cursor: pointer;
}
.login-card button:hover, .content button:hover { filter: brightness(1.1); }

/* ----- Metrics ----- */
.metrics { display: flex; gap: 0.75rem; flex-wrap: wrap; margin-bottom: 1.25rem; }
.metric {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.75rem 1rem;
    min-width: 140px;
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}
.metric-value { font-size: 1.5rem; font-weight: 700; }
.metric-label { font-size: 0.75rem; color: var(--muted); }

/* ----- Collapsible panels & forms ----- */
.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.5rem 1rem;
    margin-bottom: 0.75rem;
}
.panel > summary { cursor: pointer; font-weight: 600; padding: 0.25rem 0; }
.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem 1rem;
    padding: 0.75rem 0;
}
.form-grid label { display: flex; flex-direction: column; gap: 0.25rem; color: var(--muted); font-size: 0.85rem; }
.form-grid label.full { grid-column: 1 / -1; }
.form-grid label.checkbox { flex-direction: row; align-items: center; gap: 0.4rem; }
.form-grid .full { grid-column: 1 / -1; }
.form-grid input, .form-grid select, .form-grid textarea {
    padding: 0.5rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    font-family: inherit;
}
.form-grid button { width: auto; padding: 0.55rem 1.2rem; }

/* ----- Scope filter chips ----- */
.scope-filter { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-bottom: 1rem; }
.chip {
    padding: 0.35rem 0.8rem;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    text-decoration: none;
    font-size: 0.85rem;
}
.chip:hover { background: var(--surface-2); }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ----- Table + detail layout ----- */
.table-detail { display: grid; grid-template-columns: 3fr 2fr; gap: 1.25rem; align-items: start; }
.grid { height: 620px; width: 100%; }
.detail-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1rem 1.25rem;
    max-height: 70vh;
    overflow-y: auto;
}
.empty { color: var(--muted); padding: 2rem; text-align: center; }

/* ----- Detail card ----- */
.detail-card h2 { font-size: 1.05rem; margin: 0; }
.detail-head { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.5rem; flex-wrap: wrap; }
.badge { font-size: 0.7rem; padding: 2px 8px; border-radius: 6px; }
.badge.danger { background: #f8514922; border: 1px solid var(--danger); color: var(--danger); }
.btn-link { color: var(--accent); text-decoration: none; }
.btn-link:hover { text-decoration: underline; }
.detail-card code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }

/* ----- Conversation ----- */
.conversation { display: flex; flex-direction: column; gap: 0.4rem; }
.msg { display: flex; }
.msg-user { justify-content: flex-start; }
.msg-assistant { justify-content: flex-end; }
.bubble {
    max-width: 85%;
    padding: 0.5rem 0.75rem;
    border-radius: 12px;
    background: var(--surface-2);
    border: 1px solid var(--border);
}
.msg-assistant .bubble { background: var(--accent-soft); }
.msg-warning { color: #f0b429; font-size: 0.85rem; }
.msg-time { font-size: 0.7rem; color: var(--muted); margin: 0 0 0.4rem; }
.msg-details { font-size: 0.8rem; color: var(--muted); }
.msg-details .details-title { font-weight: 600; margin: 0.4rem 0 0.2rem; color: var(--text); }
.msg-details ul { margin: 0; padding-left: 1.2rem; }

@media (max-width: 1100px) {
    .table-detail { grid-template-columns: 1fr; }
}

/* ----- Analytics ----- */
.filter-bar {
    display: flex;
    align-items: flex-end;
    gap: 1rem;
    flex-wrap: wrap;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.9rem 1rem;
    margin-bottom: 1.5rem;
}
.filter-bar label { display: flex; flex-direction: column; gap: 0.25rem; color: var(--muted); font-size: 0.8rem; }
.filter-bar input, .filter-bar select {
    padding: 0.5rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    font-family: inherit;
}
.filter-bar button { margin-top: 0; width: auto; padding: 0.55rem 1.2rem; }

.analytics-section {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1.25rem 1.5rem;
    margin-bottom: 1.5rem;
}
.analytics-section h2 { font-size: 1.1rem; margin: 0 0 1rem; }

.metric-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.75rem;
    margin-bottom: 1rem;
}
.metric-card {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.75rem 1rem;
}
.metric-card .metric-value { font-size: 1.5rem; font-weight: 700; }
.metric-card .metric-label { font-size: 0.75rem; color: var(--muted); }
.metric-card .metric-delta { font-size: 0.75rem; color: var(--accent); margin: 0.2rem 0 0; }

.chart-block { margin-top: 1rem; }
.chart-title { font-weight: 600; margin: 0 0 0.2rem; }
.chart-caption { font-size: 0.8rem; margin: 0 0 0.4rem; }
.plotly-chart { width: 100%; min-height: 60px; }

@media (max-width: 900px) {
    .metric-grid { grid-template-columns: 1fr 1fr; }
}

/* ----- Escalation analysis ----- */
.metric-grid-6 { grid-template-columns: repeat(6, 1fr); }
.bordered-card {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.75rem 1rem;
    margin-bottom: 0.75rem;
}
.bordered-card p { margin: 0.25rem 0; }
.bordered-card code, .group-title + .bordered-card code { background: var(--bg); padding: 1px 5px; border-radius: 5px; }
.group-title { font-size: 1rem; margin: 1.25rem 0 0.5rem; }
.chart-pair {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    margin-bottom: 0.5rem;
}
.case-layout { grid-template-columns: 1fr 2fr; margin-top: 1rem; }
.btn-download {
    display: inline-block;
    padding: 0.55rem 1.2rem;
    border-radius: 8px;
    background: var(--accent);
    color: #fff;
    text-decoration: none;
    font-weight: 600;
}
.btn-download:hover { filter: brightness(1.1); }

@media (max-width: 1100px) {
    .chart-pair { grid-template-columns: 1fr; }
    .metric-grid-6 { grid-template-columns: repeat(3, 1fr); }
}
