:root {
    --navy: #0d1b3a;
    --blue: #2454c6;
    --blue-dark: #19419f;
    --blue-soft: #e8efff;
    --ink: #17233f;
    --muted: #667085;
    --line: #d9dee8;
    --surface: #ffffff;
    --canvas: #f4f6f9;
    --green: #16835f;
    --green-soft: #e5f6ef;
    --amber: #a46108;
    --amber-soft: #fff4dc;
    --red: #b42318;
    --red-soft: #feeceb;
    --sidebar-width: 248px;
    --topbar-height: 74px;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    color: var(--ink);
    background: var(--canvas);
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--canvas); }
body { margin: 0; min-height: 100vh; background: var(--canvas); color: var(--ink); font-size: 15px; letter-spacing: 0; }
button, input, select, textarea { font: inherit; letter-spacing: 0; }
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; color: var(--blue); font-size: 2rem; line-height: 1.2; font-weight: 650; }
h2 { margin-bottom: 0; font-size: 1.15rem; line-height: 1.3; }

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
.topbar { position: fixed; z-index: 30; inset: 0 0 auto 0; height: var(--topbar-height); background: var(--navy); color: white; display: flex; align-items: center; padding: 0 28px; }
.brand, .auth-brand { display: inline-flex; align-items: center; gap: 12px; color: white; font-size: 1.35rem; font-weight: 650; }
.brand:hover { text-decoration: none; }
.brand-mark { display: grid; place-items: center; min-width: 46px; height: 32px; border: 2px solid currentColor; border-radius: 5px; font-size: 0.68rem; font-weight: 800; letter-spacing: 0; }
.topbar-context { margin-left: auto; display: flex; align-items: center; gap: 15px; }
.user-name { font-weight: 550; }
.avatar { width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; background: #9bb6f3; color: var(--blue-dark); font-weight: 800; }
.link-button { border: 0; background: transparent; color: #d8e2ff; cursor: pointer; padding: 7px; }
.link-button:hover { color: white; text-decoration: underline; }
.mode-badge, .engine-badge { display: inline-flex; align-items: center; min-height: 28px; padding: 4px 9px; border-radius: 4px; font-size: 0.78rem; font-weight: 700; text-transform: uppercase; }
.mode-simulation { color: #bff4df; border: 1px solid #357f69; background: #173d3b; }
.mode-data_entry { color: #ffe1a6; border: 1px solid #8b651d; background: #453518; }
.engine-badge { color: var(--blue-dark); background: var(--blue-soft); }

.sidebar { position: fixed; z-index: 25; left: 0; top: var(--topbar-height); bottom: 0; width: var(--sidebar-width); background: var(--blue); color: white; padding: 28px 22px 18px; overflow-y: auto; display: flex; flex-direction: column; }
.sidebar nav { display: flex; flex-direction: column; }
.nav-section { margin: 22px 10px 6px; color: #a9c0f4; font-size: 0.7rem; text-transform: uppercase; font-weight: 700; }
.nav-section:first-child { margin-top: 0; }
.nav-link { position: relative; display: block; color: white; padding: 10px 12px; border-radius: 4px; font-weight: 550; }
.nav-link:hover { background: rgba(255,255,255,0.11); text-decoration: none; }
.nav-link.active { background: white; color: var(--blue-dark); font-weight: 750; }
.nav-link.active::before { content: ""; position: absolute; left: -22px; top: 0; bottom: 0; width: 4px; background: var(--navy); }
.nav-settings { margin-top: auto; border-top: 1px solid rgba(255,255,255,0.22); padding-top: 18px; }
.mobile-nav-toggle { display: none; margin-right: 10px; }
.icon-button { width: 40px; height: 40px; border: 1px solid rgba(255,255,255,0.3); background: transparent; color: white; cursor: pointer; }

.app-main { margin-left: var(--sidebar-width); padding: calc(var(--topbar-height) + 38px) 42px 55px; min-height: 100vh; }
.page-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 24px; margin-bottom: 26px; }
.eyebrow { margin-bottom: 6px; color: var(--muted); font-size: 0.72rem; line-height: 1.2; font-weight: 800; text-transform: uppercase; }
.toolbar-band { min-height: 56px; padding: 12px 18px; margin-bottom: 20px; background: var(--navy); color: white; display: flex; align-items: center; gap: 30px; }
.toolbar-date { font-weight: 700; }
.toolbar-count { margin-left: auto; color: #c8d6f4; }
.filter-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin: 0 0 18px; }
.filter-row input, .filter-row select { min-width: 220px; }
.filter-grid { display: grid; grid-template-columns: minmax(250px, 1fr) 180px 190px auto auto; }
.filter-grid input, .filter-grid select { width: 100%; min-width: 0; }

.button { min-height: 40px; border: 1px solid transparent; border-radius: 4px; padding: 9px 15px; display: inline-flex; align-items: center; justify-content: center; gap: 7px; cursor: pointer; font-weight: 700; line-height: 1.2; white-space: nowrap; }
.button:hover { text-decoration: none; }
.button-primary { background: var(--blue); color: white; }
.button-primary:hover { background: var(--blue-dark); }
.button-secondary { border-color: #b8c0cf; color: var(--ink); background: white; }
.button-secondary:hover { border-color: var(--blue); color: var(--blue); }
.button-quiet { color: var(--muted); background: transparent; }
.button-small { min-height: 34px; padding: 6px 11px; }
.button-block { width: 100%; }
.button-group { display: flex; gap: 10px; flex-wrap: wrap; }
.button:disabled { opacity: 0.5; cursor: not-allowed; }

input, select, textarea { width: 100%; min-height: 42px; border: 1px solid #bfc6d2; border-radius: 3px; background: white; color: var(--ink); padding: 9px 11px; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible { outline: 3px solid rgba(36,84,198,0.24); outline-offset: 2px; border-color: var(--blue); }
.field { display: grid; gap: 6px; min-width: 0; }
.field > span { font-size: 0.82rem; font-weight: 700; color: #344054; }
.field-wide { grid-column: 1 / -1; }

.table-wrap { width: 100%; overflow-x: auto; background: var(--surface); border: 1px solid var(--line); box-shadow: 0 3px 12px rgba(13,27,58,0.05); }
table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
th { height: 48px; padding: 10px 13px; background: #eef1f5; color: #4c566a; text-align: left; font-size: 0.75rem; text-transform: uppercase; white-space: nowrap; }
td { min-height: 58px; padding: 14px 13px; border-top: 1px solid #e5e8ee; vertical-align: middle; }
tbody tr:hover { background: #fafbfc; }
.actions-column { width: 150px; }
.row-actions { display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.row-actions form { margin: 0; }
.icon-action { border: 0; background: transparent; color: var(--blue); padding: 6px; cursor: pointer; font-weight: 700; }
.icon-action:hover { text-decoration: underline; }
.icon-action.danger { color: var(--red); }
.empty-cell { padding: 36px; color: var(--muted); text-align: center; }
.compact-table table { min-width: 680px; }

.status, .priority { display: inline-flex; align-items: center; min-height: 26px; padding: 4px 8px; border-radius: 3px; font-size: 0.78rem; font-weight: 750; white-space: nowrap; }
.status-active, .status-completed, .status-assigned { background: var(--green-soft); color: var(--green); }
.status-inactive, .status-cancelled, .status-archived, .status-failed { background: var(--red-soft); color: var(--red); }
.status-maintenance, .status-pending, .status-in_progress, .status-constraint_warning { background: var(--amber-soft); color: var(--amber); }
.status-scheduled, .status-running { background: var(--blue-soft); color: var(--blue-dark); }
.status-unassigned { background: var(--red-soft); color: var(--red); }
.priority-urgent { background: var(--red-soft); color: var(--red); }
.priority-high { background: var(--amber-soft); color: var(--amber); }
.priority-normal { background: var(--blue-soft); color: var(--blue-dark); }
.priority-low { background: #eef1f5; color: #4c566a; }

.kpi-grid { display: grid; grid-template-columns: repeat(5, minmax(150px, 1fr)); gap: 16px; margin-bottom: 34px; }
.kpi { min-height: 136px; padding: 20px; border: 1px solid var(--line); border-top: 4px solid var(--blue); background: white; display: flex; flex-direction: column; justify-content: center; }
.kpi strong { margin: 6px 0 2px; color: var(--blue-dark); font-size: 2.15rem; line-height: 1; }
.kpi > span:last-child { color: var(--muted); font-size: 0.8rem; }
.kpi-label { color: #344054; font-size: 0.76rem; font-weight: 800; text-transform: uppercase; }
.kpi-attention { border-top-color: var(--amber); }
.kpi-attention strong { color: var(--amber); }
.dashboard-grid { display: grid; grid-template-columns: minmax(0, 1.65fr) minmax(280px, 0.75fr); gap: 24px; }
.section-heading { min-height: 52px; margin-bottom: 10px; display: flex; justify-content: space-between; align-items: center; gap: 16px; }
.activity-panel { min-width: 0; }
.activity-list { margin: 0; padding: 0; list-style: none; border-top: 1px solid var(--line); }
.activity-list li { position: relative; display: grid; grid-template-columns: 14px 1fr; gap: 10px; padding: 14px 4px; border-bottom: 1px solid var(--line); }
.activity-list p { margin: 3px 0; color: var(--muted); font-size: 0.8rem; }
.activity-list time { color: #8a93a3; font-size: 0.74rem; }
.activity-dot { width: 8px; height: 8px; margin-top: 6px; border-radius: 50%; background: var(--green); }

.entity-dialog { width: min(980px, calc(100vw - 30px)); max-height: calc(100vh - 40px); padding: 0; border: 0; border-radius: 5px; box-shadow: 0 24px 80px rgba(13,27,58,0.35); }
.entity-dialog::backdrop { background: rgba(13,27,58,0.78); }
.dialog-header { min-height: 72px; padding: 18px 24px; color: white; background: var(--blue); display: flex; align-items: center; justify-content: space-between; }
.dialog-header h2 { font-size: 1.4rem; }
.dialog-close { width: 38px; height: 38px; border: 0; border-radius: 50%; color: var(--navy); background: white; cursor: pointer; font-size: 1.55rem; line-height: 1; }
.entity-form { padding: 26px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px 24px; }
.dialog-actions { display: flex; gap: 10px; margin-top: 26px; }

.simulation-controls { margin-bottom: 24px; padding: 18px; background: white; border: 1px solid var(--line); display: grid; grid-template-columns: repeat(6, minmax(125px, 1fr)) auto; align-items: end; gap: 12px; }
.run-summary { padding: 10px 14px; margin-bottom: 16px; color: #475467; background: #e9edf3; display: flex; flex-wrap: wrap; gap: 10px 24px; font-size: 0.8rem; }
.warning-panel { margin-bottom: 24px; padding: 17px 20px; border-left: 4px solid var(--amber); background: var(--amber-soft); }
.warning-panel ul { margin: 9px 0 0; padding-left: 20px; }
.rule-list { width: min(420px, 70vw); margin: 8px 0; padding-left: 18px; line-height: 1.5; }
details summary { color: var(--blue); cursor: pointer; font-weight: 700; }
details pre { max-width: 520px; max-height: 260px; overflow: auto; padding: 12px; color: #dce5f8; background: var(--navy); font-size: 0.72rem; white-space: pre-wrap; }
.empty-state { min-height: 250px; padding: 50px 24px; border: 1px dashed #b8c0cf; background: white; text-align: center; display: grid; place-content: center; }
.empty-state p { margin: 8px auto 0; color: var(--muted); max-width: 620px; }

.report-controls { grid-template-columns: repeat(4, minmax(160px, 1fr)) auto; }
.report-summary { margin-bottom: 18px; padding: 13px 18px; background: var(--navy); color: white; display: flex; align-items: baseline; gap: 8px 22px; }
.report-summary strong { color: #a9c0f4; font-size: 1.15rem; }
.settings-section { margin-bottom: 32px; }
.mode-control { display: grid; grid-template-columns: repeat(2, minmax(220px, 1fr)) auto; gap: 14px; align-items: stretch; }
.mode-control > label { padding: 16px; border: 1px solid var(--line); background: white; display: flex; gap: 12px; cursor: pointer; }
.mode-control input { width: 20px; min-height: 20px; }
.mode-control span { display: grid; gap: 4px; }
.mode-control small { color: var(--muted); }
.password-form { display: grid; grid-template-columns: repeat(3, minmax(180px, 1fr)) auto; gap: 14px; align-items: end; }

.flash { margin: 0 0 18px; padding: 13px 16px; border-left: 4px solid; background: white; }
.flash-success { border-color: var(--green); color: #0c6044; }
.flash-error { border-color: var(--red); color: var(--red); }
.flash-warning { border-color: var(--amber); color: var(--amber); }

.auth-page { background: var(--navy); }
.auth-shell { min-height: 100vh; padding: 28px; display: grid; place-items: center; background: linear-gradient(115deg, var(--navy) 0 52%, #15336f 52% 100%); }
.auth-panel { width: min(430px, 100%); padding: 38px; background: white; border-top: 5px solid var(--blue); box-shadow: 0 22px 70px rgba(0,0,0,0.3); }
.auth-brand { margin-bottom: 40px; color: var(--navy); }
.auth-copy { margin: 10px 0 24px; color: var(--muted); }
.auth-form { display: grid; gap: 18px; }
.security-note { margin: 20px 0 0; color: #8a93a3; font-size: 0.75rem; text-align: center; }

@media (max-width: 1280px) {
    .kpi-grid { grid-template-columns: repeat(3, 1fr); }
    .simulation-controls { grid-template-columns: repeat(3, 1fr); }
    .report-controls { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 900px) {
    :root { --topbar-height: 64px; }
    .mobile-nav-toggle { display: inline-grid; place-items: center; }
    .sidebar { transform: translateX(-100%); transition: transform 180ms ease; box-shadow: 10px 0 30px rgba(13,27,58,0.25); }
    body.nav-open .sidebar { transform: translateX(0); }
    .app-main { margin-left: 0; padding: calc(var(--topbar-height) + 28px) 22px 42px; }
    .topbar { padding: 0 16px; }
    .topbar .brand span:last-child { display: none; }
    .user-name, .mode-badge { display: none; }
    .dashboard-grid { grid-template-columns: 1fr; }
    .filter-grid { grid-template-columns: 1fr 1fr; }
    .mode-control, .password-form { grid-template-columns: 1fr; }
}

@media (max-width: 620px) {
    h1 { font-size: 1.6rem; }
    .app-main { padding-left: 14px; padding-right: 14px; }
    .page-heading { align-items: flex-start; flex-direction: column; }
    .page-heading .button, .page-heading .button-group { width: 100%; }
    .button-group .button { flex: 1; }
    .toolbar-band { align-items: flex-start; flex-direction: column; gap: 4px; }
    .toolbar-count { margin-left: 0; }
    .kpi-grid, .simulation-controls, .report-controls, .form-grid, .filter-grid { grid-template-columns: 1fr; }
    .kpi { min-height: 112px; }
    .entity-form { padding: 18px; }
    .field-wide { grid-column: auto; }
    .report-summary { align-items: flex-start; flex-direction: column; }
    .auth-shell { padding: 14px; }
    .auth-panel { padding: 28px 22px; }
    .topbar-context { gap: 8px; }
    .avatar { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; }
}
