:root {
    --bg: #f5f7fb;
    --surface: #ffffff;
    --surface-soft: #f9fbfd;
    --text: #172033;
    --muted: #667085;
    --line: #e5e9f0;
    --brand: #1769e0;
    --brand-dark: #1055b8;
    --green: #0f9f6e;
    --orange: #d97706;
    --red: #dc2626;
    --purple: #7c3aed;
    --shadow: 0 16px 40px rgba(17, 24, 39, .08);
    --radius: 8px;
    --sidebar: 272px;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html { background: var(--bg); color: var(--text); }
body { margin: 0; min-height: 100vh; font-size: 14px; }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
a { color: inherit; text-decoration: none; }

.app-shell { display: grid; grid-template-columns: var(--sidebar) 1fr; min-height: 100vh; }
.sidebar {
    position: sticky;
    top: 0;
    height: 100vh;
    padding: 20px 16px;
    background: #111827;
    color: #f8fafc;
    overflow-y: auto;
}
.brand { display: flex; gap: 12px; align-items: center; min-width: 0; }
.brand-mark {
    width: 42px;
    height: 42px;
    display: grid;
    place-items: center;
    border-radius: var(--radius);
    background: var(--brand);
    color: #fff;
    font-weight: 800;
    letter-spacing: 0;
    flex: 0 0 auto;
}
.brand strong { display: block; font-size: 16px; line-height: 1.2; }
.brand small { display: block; color: #aab4c5; margin-top: 2px; }
.side-nav { display: grid; gap: 6px; margin-top: 28px; }
.nav-item {
    width: 100%;
    border: 0;
    border-radius: var(--radius);
    padding: 11px 12px;
    text-align: left;
    color: #cbd5e1;
    background: transparent;
}
.nav-item:hover, .nav-item.active { color: #fff; background: rgba(255,255,255,.1); }
.main { min-width: 0; }
.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    min-height: 76px;
    padding: 16px 24px;
    background: rgba(245, 247, 251, .9);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--line);
}
.topbar h1 { margin: 0; font-size: 24px; line-height: 1.2; letter-spacing: 0; }
.topbar p { margin: 3px 0 0; color: var(--muted); }
.top-actions { display: flex; align-items: center; gap: 10px; min-width: 0; }
.global-search input { width: min(32vw, 360px); }
.user-chip {
    display: grid;
    gap: 1px;
    padding: 7px 10px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    min-width: 140px;
}
.user-chip span { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-chip small { color: var(--muted); }
.content { padding: 24px; }
.view { display: none; }
.view.active { display: block; }

.button, .icon-button {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    min-height: 38px;
    padding: 9px 14px;
    font-weight: 700;
    transition: .18s ease;
}
.button:hover, .icon-button:hover { transform: translateY(-1px); box-shadow: 0 8px 18px rgba(17, 24, 39, .08); }
.button.primary { color: #fff; background: var(--brand); border-color: var(--brand); }
.button.primary:hover { background: var(--brand-dark); }
.button.ghost { background: transparent; }
.button.danger { color: #fff; background: var(--red); border-color: var(--red); }
.button.full { width: 100%; justify-content: center; }
.icon-button { width: 38px; padding: 0; display: grid; place-items: center; font-size: 20px; }
.mobile-only { display: none; }

input, select, textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #fff;
    padding: 10px 12px;
    color: var(--text);
    outline: none;
}
textarea { min-height: 110px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(23,105,224,.12); }
label { display: grid; gap: 6px; font-weight: 700; color: #344054; }

.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
}
.filter-row, .split-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.filter-row input, .filter-row select { width: auto; min-width: 180px; }

.kpi-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 16px; }
.kpi-card {
    display: grid;
    gap: 8px;
    padding: 18px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.kpi-card span { color: var(--muted); }
.kpi-card strong { font-size: 28px; letter-spacing: 0; }
.kpi-card.blue { border-top: 4px solid var(--brand); }
.kpi-card.green { border-top: 4px solid var(--green); }
.kpi-card.orange { border-top: 4px solid var(--orange); }
.kpi-card.purple { border-top: 4px solid var(--purple); }

.dashboard-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.span-2 { grid-column: span 2; }
.panel {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    min-width: 0;
}
.panel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 16px 18px;
    border-bottom: 1px solid var(--line);
}
.panel-head h2 { margin: 0; font-size: 16px; letter-spacing: 0; }
.panel-head p { margin: 4px 0 0; color: var(--muted); }
.chart-row { padding: 16px; min-height: 300px; }

.stack-list { display: grid; gap: 0; }
.stack-item {
    display: grid;
    gap: 4px;
    padding: 13px 18px;
    border-bottom: 1px solid var(--line);
}
.stack-item:last-child { border-bottom: 0; }
.stack-item strong { line-height: 1.35; }
.stack-item span, .muted { color: var(--muted); }

.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 14px; margin-bottom: 16px; }
.entity-card {
    display: grid;
    gap: 10px;
    padding: 16px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.entity-card h3 { margin: 0; font-size: 16px; }
.entity-meta { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; color: var(--muted); }
.badge {
    display: inline-flex;
    align-items: center;
    min-height: 24px;
    border-radius: 999px;
    padding: 3px 9px;
    background: #edf4ff;
    color: #1454b8;
    font-weight: 700;
    font-size: 12px;
}
.badge.green { background: #e7f8f1; color: #087455; }
.badge.orange { background: #fff4e5; color: #b45309; }
.badge.red { background: #fee2e2; color: #b91c1c; }
.badge.gray { background: #eef2f7; color: #475467; }

.table-panel { overflow: auto; }
.data-table { width: 100%; border-collapse: collapse; min-width: 760px; }
.data-table th, .data-table td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; }
.data-table th { background: var(--surface-soft); color: #475467; font-size: 12px; text-transform: uppercase; letter-spacing: 0; user-select: none; }
.data-table tr:hover td { background: #fbfdff; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.row-actions .button { min-height: 30px; padding: 6px 9px; font-size: 12px; }

.modal {
    position: fixed;
    inset: 0;
    z-index: 60;
    display: none;
    align-items: flex-start;
    justify-content: center;
    padding: 40px 16px;
    background: rgba(15, 23, 42, .45);
    overflow-y: auto;
}
.modal.open { display: flex; }
.modal-panel {
    width: min(1120px, 100%);
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: 0 30px 80px rgba(0,0,0,.25);
    overflow: hidden;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 18px; }
#modalBody { padding: 18px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; grid-column: 1 / -1; padding-top: 6px; }

.template-builder {
    display: grid;
    grid-template-columns: 280px minmax(0, 1fr);
    gap: 14px;
    align-items: start;
}
.builder-fields, .builder-editor {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface-soft);
}
.builder-fields { max-height: 650px; overflow: auto; padding: 12px; }
.builder-title { font-weight: 800; margin-bottom: 10px; }
.field-search { margin-bottom: 10px; }
.field-group { border-top: 1px solid var(--line); padding: 8px 0; }
.field-group:first-of-type { border-top: 0; }
.field-group summary {
    font-weight: 800;
    color: #344054;
    cursor: pointer;
    margin-bottom: 8px;
}
.field-chip-wrap { display: flex; flex-wrap: wrap; gap: 6px; }
.field-chip {
    border: 1px solid #c9d7eb;
    border-radius: 999px;
    background: #fff;
    color: #1454b8;
    padding: 5px 8px;
    font-size: 12px;
    font-weight: 700;
}
.field-chip:hover { background: #edf4ff; }
.builder-editor { padding: 12px; }
.editor-toolbar {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 6px;
    margin: 8px 0 10px;
}
.editor-toolbar button, .image-tool {
    min-height: 32px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #fff;
    padding: 6px 9px;
    font-weight: 800;
    color: var(--text);
}
.image-tool { display: inline-flex; align-items: center; cursor: pointer; }
.image-tool input { display: none; }
.rich-editor {
    min-height: 520px;
    padding: 28px;
    border: 1px solid #cbd5e1;
    border-radius: var(--radius);
    background: #fff;
    line-height: 1.7;
    outline: none;
    overflow: auto;
}
.rich-editor:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(23,105,224,.12); }
.rich-editor table, .document-preview table {
    width: 100%;
    border-collapse: collapse;
    margin: 12px 0;
}
.rich-editor td, .rich-editor th, .document-preview td, .document-preview th {
    border: 1px solid #cbd5e1;
    padding: 8px;
}
.rich-editor img, .document-preview img {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 12px 0;
}
.editor-value {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
    pointer-events: none;
}

.toast-wrap { position: fixed; right: 18px; bottom: 18px; z-index: 90; display: grid; gap: 10px; }
.toast {
    min-width: min(360px, calc(100vw - 36px));
    padding: 12px 14px;
    border-radius: var(--radius);
    background: #111827;
    color: #fff;
    box-shadow: var(--shadow);
}
.toast.success { background: #087455; }
.toast.error { background: #b42318; }

.calendar-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 8px; padding: 16px; }
.calendar-day { min-height: 74px; padding: 8px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface-soft); }
.metric-large { padding: 18px; font-size: 32px; font-weight: 800; }
.mini-stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; padding: 16px; }
.mini-stat { padding: 12px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface-soft); }
.document-preview { padding: 28px; min-height: 420px; line-height: 1.7; }
.report-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 14px; }
.report-card {
    text-align: left;
    display: grid;
    gap: 8px;
    padding: 18px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
}
.report-card span { color: var(--muted); }
.empty-state {
    padding: 32px;
    border: 1px dashed #b8c2d4;
    border-radius: var(--radius);
    background: #fff;
    color: var(--muted);
    text-align: center;
}

.login-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 20px;
    background: linear-gradient(135deg, #e7f0ff 0%, #f9fbfd 42%, #eaf8f2 100%);
}
.login-card {
    width: min(430px, 100%);
    padding: 28px;
    border-radius: var(--radius);
    background: #fff;
    border: 1px solid var(--line);
    box-shadow: var(--shadow);
}
.login-brand { margin-bottom: 24px; color: var(--text); }

@media (max-width: 1100px) {
    .app-shell { grid-template-columns: 1fr; }
    .sidebar { position: fixed; z-index: 50; transform: translateX(-105%); transition: .2s ease; width: var(--sidebar); }
    .sidebar.open { transform: translateX(0); }
    .mobile-only { display: grid; }
    .dashboard-grid, .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .span-2 { grid-column: span 2; }
    .topbar { align-items: flex-start; }
    .top-actions { flex-wrap: wrap; justify-content: flex-end; }
}

@media (max-width: 720px) {
    body { font-size: 13px; }
    .topbar { padding: 14px; flex-wrap: wrap; }
    .content { padding: 14px; }
    .topbar h1 { font-size: 20px; }
    .global-search, .global-search input, .user-chip { width: 100%; }
    .top-actions { width: 100%; }
    .toolbar { align-items: stretch; flex-direction: column; }
    .filter-row input, .filter-row select, .split-actions, .split-actions .button { width: 100%; }
    .kpi-grid, .dashboard-grid, .form-grid, .mini-stats { grid-template-columns: 1fr; }
    .template-builder { grid-template-columns: 1fr; }
    .builder-fields { max-height: 280px; }
    .rich-editor { min-height: 420px; padding: 18px; }
    .span-2 { grid-column: span 1; }
    .data-table { display: none; }
    .mobile-cards { display: grid; }
    .modal { padding: 12px; }
    .calendar-grid { grid-template-columns: repeat(2, 1fr); }
}

@media print {
    body { background: #fff; }
    .sidebar, .topbar, .toolbar, .panel-head .button, .toast-wrap { display: none !important; }
    .app-shell { display: block; }
    .content { padding: 0; }
    .panel { border: 0; box-shadow: none; }
    .print-surface { display: block !important; }
    .document-preview { padding: 0; }
}
