:root {
    --bg: #f4f6f8;
    --surface: #ffffff;
    --surface-soft: #f8fafc;
    --text: #1f2937;
    --muted: #667085;
    --border: #dfe5ec;
    --shadow: 0 8px 22px rgba(16, 24, 40, .06);
    --radius: 8px;
    --sidebar-width: 270px;
}

[data-theme="dark"] {
    --bg: #111827;
    --surface: #18202d;
    --surface-soft: #202a39;
    --text: #f3f4f6;
    --muted: #a7b0bf;
    --border: #303b4d;
    --shadow: 0 10px 26px rgba(0, 0, 0, .18);
}

* { box-sizing: border-box; }

html,
body {
    min-width: 320px;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 15px;
    line-height: 1.45;
}

a { text-decoration: none; }

.app-shell {
    display: grid;
    grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
    min-height: 100vh;
}

.sidebar {
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    padding: 16px 12px;
    background: #101828;
    color: #fff;
}

.sidebar-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 0 4px 14px;
    border-bottom: 1px solid rgba(255,255,255,.09);
    margin-bottom: 10px;
}

.brand {
    display: flex;
    align-items: center;
    min-width: 0;
    gap: 10px;
    color: #fff;
    font-weight: 800;
}

.brand:hover { color: #fff; }

.brand span:last-child {
    display: grid;
    min-width: 0;
}

.brand small {
    color: rgba(255,255,255,.62);
    font-size: .74rem;
    font-weight: 600;
}

.brand-mark {
    display: grid;
    flex: 0 0 38px;
    width: 38px;
    height: 38px;
    place-items: center;
    background: var(--primary);
    color: #fff;
    border-radius: var(--radius);
}

.sidebar-nav {
    gap: 2px;
}

.nav-section {
    margin: 14px 10px 5px;
    color: rgba(255,255,255,.48);
    font-size: .68rem;
    font-weight: 800;
    letter-spacing: .06em;
    text-transform: uppercase;
}

.sidebar .nav-link {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 38px;
    padding: 8px 10px;
    color: rgba(255,255,255,.78);
    border-radius: var(--radius);
    font-size: .9rem;
    font-weight: 650;
}

.sidebar .nav-link i {
    width: 21px;
    text-align: center;
    color: rgba(255,255,255,.6);
}

.sidebar .nav-link span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar .nav-link:hover,
.sidebar .nav-link.active {
    background: rgba(255,255,255,.1);
    color: #fff;
}

.sidebar .nav-link.active {
    border-left: 3px solid var(--primary);
    padding-left: 7px;
}

.main {
    min-width: 0;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    min-height: 68px;
    padding: 12px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.page-title {
    min-width: 0;
}

.eyebrow {
    color: var(--muted);
    font-size: .76rem;
    font-weight: 700;
}

.topbar h1 {
    margin: 0;
    color: var(--text);
    font-size: 1.35rem;
    font-weight: 800;
    line-height: 1.2;
    letter-spacing: 0;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 0 0 auto;
}

.user-chip {
    display: flex;
    align-items: center;
    gap: 8px;
    max-width: 210px;
    padding: 7px 10px;
    background: var(--surface-soft);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.user-chip span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.content {
    width: 100%;
    max-width: 1500px;
    margin: 0 auto;
    padding: 24px;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    min-height: 38px;
    border-radius: var(--radius);
    font-weight: 700;
}

.btn-sm { min-height: 32px; }

.btn-primary,
.btn-primary:hover {
    background: var(--primary);
    border-color: var(--primary);
}

.btn-outline-primary {
    color: var(--primary);
    border-color: var(--primary);
}

.btn-outline-primary:hover {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}

.btn-outline-secondary {
    color: var(--text);
    background: var(--surface);
    border-color: var(--border);
}

.btn-outline-secondary:hover {
    color: var(--text);
    background: var(--surface-soft);
    border-color: var(--border);
}

.btn-icon {
    width: 40px;
    height: 40px;
    min-height: 40px;
    padding: 0;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
}

.sidebar .btn-icon {
    color: #fff;
    background: rgba(255,255,255,.08);
    border-color: rgba(255,255,255,.16);
}

.panel,
.metric-card,
.kpi-strip {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.panel {
    padding: 20px;
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 14px;
    margin-bottom: 16px;
}

.panel-header h2,
.section-title {
    margin: 0;
    color: var(--text);
    font-size: 1.08rem;
    font-weight: 800;
    letter-spacing: 0;
}

.panel-header p {
    margin: 4px 0 0;
    color: var(--muted);
    font-size: .9rem;
}

.metrics-grid {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 14px;
}

.metric-card {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    min-height: 108px;
    padding: 16px;
}

.metric-card span,
.kpi-strip span {
    color: var(--muted);
    font-size: .84rem;
    font-weight: 700;
}

.metric-card strong {
    display: block;
    margin-top: 8px;
    color: var(--text);
    font-size: 1.35rem;
    line-height: 1.15;
    word-break: break-word;
}

.metric-card i {
    display: grid;
    flex: 0 0 40px;
    width: 40px;
    height: 40px;
    place-items: center;
    color: var(--primary);
    background: #eef4ff;
    border-radius: var(--radius);
}

[data-theme="dark"] .metric-card i {
    background: #23324d;
}

.kpi-strip {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 74px;
    padding: 15px 16px;
}

.kpi-strip strong {
    color: var(--secondary);
    font-size: 1.3rem;
}

.table-toolbar {
    display: flex;
    gap: 8px;
    margin-bottom: 14px;
    max-width: 560px;
}

.table-toolbar .form-control {
    min-width: 0;
}

.table-responsive {
    width: 100%;
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.clean-table {
    min-width: 760px;
    margin: 0;
    color: var(--text);
}

.clean-table thead th {
    padding: 12px 14px;
    background: var(--surface-soft);
    color: var(--muted);
    border-bottom: 1px solid var(--border);
    font-size: .74rem;
    font-weight: 800;
    letter-spacing: .03em;
    text-transform: uppercase;
    white-space: nowrap;
}

.clean-table tbody td {
    padding: 12px 14px;
    color: var(--text);
    border-color: var(--border);
    vertical-align: middle;
}

.clean-table tbody tr:hover {
    background: var(--surface-soft);
}

.clean-table .btn {
    width: 34px;
    min-height: 32px;
    padding: 0;
}

.form-control,
.form-select {
    min-height: 42px;
    color: var(--text);
    background: var(--surface);
    border-color: var(--border);
    border-radius: var(--radius);
}

.form-control:focus,
.form-select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 .18rem rgba(37, 99, 235, .13);
}

textarea.form-control {
    min-height: 110px;
}

.form-label {
    width: 100%;
    color: var(--muted);
    font-size: .86rem;
    font-weight: 750;
}

.form-label > .form-control,
.form-label > .form-select {
    margin-top: 7px;
}

.details-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 12px;
}

.detail-item {
    min-width: 0;
    padding: 13px;
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.detail-item span {
    display: block;
    margin-bottom: 6px;
    color: var(--muted);
    font-size: .72rem;
    font-weight: 800;
    letter-spacing: .04em;
    text-transform: uppercase;
}

.detail-item strong {
    display: block;
    color: var(--text);
    font-size: .95rem;
    font-weight: 750;
    word-break: break-word;
}

.event-list {
    display: grid;
    gap: 10px;
}

.event-item {
    padding: 12px;
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.event-item strong,
.event-item span {
    display: block;
}

.event-item span {
    margin-top: 5px;
    color: var(--muted);
    font-size: .86rem;
}

.chat-panel {
    display: flex;
    flex-direction: column;
    min-height: 620px;
}

.chat-thread {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 12px;
    max-height: 420px;
    overflow-y: auto;
    padding: 14px;
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.chat-message {
    max-width: 84%;
    padding: 12px 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.chat-message.user {
    align-self: flex-end;
    color: #fff;
    background: var(--primary);
    border-color: var(--primary);
}

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

.chat-input {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 10px;
    margin-top: 14px;
    align-items: end;
}

.hint-box {
    display: grid;
    gap: 6px;
    margin-top: 14px;
    padding: 12px;
    color: var(--muted);
    border: 1px dashed var(--border);
    border-radius: var(--radius);
    font-size: .86rem;
}

.hint-box strong {
    color: var(--text);
}

.budget-summary {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.alert {
    border-radius: var(--radius);
}

.login-page {
    display: grid;
    min-height: 100vh;
    place-items: center;
    padding: 20px;
    background: var(--bg);
}

.login-card {
    width: min(420px, 100%);
    padding: 28px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.login-card .brand,
.login-card .brand:hover {
    color: var(--text);
}

.login-card .brand small {
    color: var(--muted);
}

.login-card h1 {
    font-size: 1.5rem;
    font-weight: 800;
    letter-spacing: 0;
}

.sidebar-backdrop {
    display: none;
}

@media (max-width: 1320px) {
    .metrics-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }
}

@media (max-width: 1100px) {
    .details-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 991px) {
    .app-shell {
        grid-template-columns: 1fr;
    }

    .sidebar {
        position: fixed;
        left: calc(var(--sidebar-width) * -1);
        z-index: 40;
        width: var(--sidebar-width);
        transition: left .2s ease;
        box-shadow: 18px 0 38px rgba(0,0,0,.25);
    }

    .sidebar.is-open {
        left: 0;
    }

    .sidebar-backdrop {
        position: fixed;
        inset: 0;
        z-index: 35;
        background: rgba(15, 23, 42, .46);
    }

    .sidebar-backdrop.is-open {
        display: block;
    }

    body.sidebar-open {
        overflow: hidden;
    }

    .topbar {
        min-height: 64px;
        padding: 10px 16px;
    }

    .content {
        padding: 18px;
    }
}

@media (max-width: 720px) {
    body {
        font-size: 14px;
    }

    .metrics-grid,
    .details-grid,
    .budget-summary {
        grid-template-columns: 1fr;
    }

    .metric-card {
        min-height: 88px;
    }

    .topbar {
        gap: 10px;
    }

    .eyebrow,
    .user-chip,
    .logout-btn span {
        display: none;
    }

    .topbar h1 {
        font-size: 1.08rem;
        white-space: normal;
    }

    .topbar-actions {
        gap: 6px;
    }

    .content {
        padding: 14px;
    }

    .panel {
        padding: 15px;
    }

    .panel-header {
        flex-direction: column;
        align-items: stretch;
    }

    .panel-header .d-flex,
    .panel-header .btn {
        width: 100%;
    }

    .panel-header .d-flex .btn {
        flex: 1 1 auto;
    }

    .table-toolbar {
        max-width: none;
    }

    .clean-table {
        min-width: 700px;
    }

    .chat-panel {
        min-height: auto;
    }

    .chat-input {
        grid-template-columns: 1fr;
    }

    .chat-input .btn {
        width: 100%;
    }

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

@media (max-width: 430px) {
    .topbar {
        padding: 9px 12px;
    }

    .btn-icon {
        width: 38px;
        height: 38px;
        min-height: 38px;
    }

    .table-toolbar {
        flex-direction: column;
    }

    .table-toolbar .btn {
        width: 100%;
    }
}
