:root {
    color-scheme: light;
    --bg: #f5f7fb;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --border: #dfe5ef;
    --text: #172033;
    --muted: #667085;
    --primary: #3659d9;
    --primary-dark: #2845b8;
    --danger: #b42318;
    --danger-bg: #fff4f2;
    --success: #067647;
    --success-bg: #ecfdf3;
    --sidebar: #111827;
    --sidebar-text: #d7deea;
    --shadow: 0 10px 30px rgba(16, 24, 40, .06);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body { font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 270px 1fr; }
.sidebar { position: sticky; top: 0; height: 100vh; background: var(--sidebar); color: var(--sidebar-text); padding: 22px 16px; display: flex; flex-direction: column; overflow-y: auto; }
.brand { display: flex; gap: 12px; align-items: center; color: white; padding: 4px 8px 24px; }
.brand:hover { text-decoration: none; }
.brand-mark { width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center; background: #4169e1; font-weight: 800; }
.brand strong, .brand small { display: block; }
.brand small { color: #9ba8bd; margin-top: 2px; }
.sidebar-nav { display: flex; flex-direction: column; gap: 4px; }
.sidebar-nav > a, .module-nav-item > a:first-child { color: var(--sidebar-text); padding: 9px 10px; border-radius: 7px; }
.sidebar-nav > a:hover, .module-nav-item > a:first-child:hover { background: rgba(255,255,255,.08); text-decoration: none; color: white; }
.nav-label { margin: 20px 10px 6px; color: #8794aa; font-size: 11px; font-weight: 700; letter-spacing: .09em; text-transform: uppercase; }
.nav-label-row { display: flex; justify-content: space-between; align-items: center; }
.nav-icon-link { color: #b9c4d5; width: 26px; height: 26px; border-radius: 6px; display: grid; place-items: center; font-size: 18px; }
.nav-icon-link:hover { background: rgba(255,255,255,.1); color: white; text-decoration: none; }
.module-nav-item { display: grid; grid-template-columns: 1fr 28px; align-items: center; }
.nav-empty { color: #8794aa; padding: 8px 10px; font-size: 13px; }
.manage-link { margin-top: 8px; border-top: 1px solid rgba(255,255,255,.08); padding-top: 16px !important; }
.sidebar-footer { margin-top: auto; border-top: 1px solid rgba(255,255,255,.08); padding: 16px 8px 0; display: grid; gap: 5px; color: #8794aa; font-size: 11px; }

.main-content { min-width: 0; }
.topbar { min-height: 58px; background: rgba(255,255,255,.9); backdrop-filter: blur(10px); border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 12px; padding: 0 28px; color: var(--muted); font-size: 13px; position: sticky; top: 0; z-index: 5; }
.sidebar-toggle { display: none; border: 0; background: transparent; font-size: 21px; }
.page-container { max-width: 1480px; margin: 0 auto; padding: 34px; }
.page-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; margin-bottom: 26px; }
.page-header.compact { max-width: 900px; }
h1 { font-size: 32px; line-height: 1.15; margin: 2px 0 8px; letter-spacing: -.025em; }
h2 { font-size: 20px; margin: 0 0 8px; }
h3 { margin: 0; }
p { line-height: 1.55; }
.eyebrow { margin: 0; color: var(--primary); font-size: 12px; font-weight: 750; letter-spacing: .09em; text-transform: uppercase; }
.muted { color: var(--muted); margin: 0; }

.button { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 38px; padding: 8px 14px; border: 1px solid var(--border); border-radius: 7px; background: white; color: var(--text); font-weight: 650; font-size: 14px; }
.button:hover { text-decoration: none; background: var(--surface-2); }
.button.primary { background: var(--primary); border-color: var(--primary); color: white; }
.button.primary:hover { background: var(--primary-dark); }
.button.subtle { background: transparent; }
.button.danger { background: var(--danger); border-color: var(--danger); color: white; }
.button-row, .form-actions, .inline-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 9px; }

.metric-grid { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 16px; margin-bottom: 32px; }
.metric-card { background: var(--surface); border: 1px solid var(--border); border-radius: 11px; padding: 20px; box-shadow: var(--shadow); }
.metric-card span { display: block; color: var(--muted); font-size: 13px; }
.metric-card strong { display: block; font-size: 30px; margin-top: 6px; }
.section-block { margin-top: 26px; }
.section-heading { display: flex; justify-content: space-between; align-items: flex-start; gap: 18px; margin-bottom: 16px; }
.section-heading p { color: var(--muted); margin: 0; font-size: 14px; }
.card-grid { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 16px; }
.module-card { background: white; border: 1px solid var(--border); border-radius: 11px; padding: 20px; box-shadow: var(--shadow); }
.module-card-header { display: flex; justify-content: space-between; gap: 12px; }
.module-card p { color: var(--muted); min-height: 48px; }
.module-stats { display: flex; gap: 18px; border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); padding: 12px 0; margin: 16px 0; color: var(--muted); font-size: 13px; }
.module-stats strong { color: var(--text); }

.panel { background: white; border: 1px solid var(--border); border-radius: 11px; box-shadow: var(--shadow); }
.form-panel { padding: 24px; }
.form-panel.narrow { max-width: 760px; }
.table-panel { overflow: hidden; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; background: var(--surface-2); color: var(--muted); font-size: 12px; letter-spacing: .04em; text-transform: uppercase; }
th, td { padding: 14px 18px; border-bottom: 1px solid var(--border); vertical-align: top; }
tbody tr:last-child td { border-bottom: 0; }
td small { display: block; color: var(--muted); margin-top: 5px; max-width: 700px; }
.actions-cell { text-align: right; white-space: nowrap; }
.actions-cell a { margin-left: 12px; }
.status-pill { display: inline-flex; align-items: center; min-height: 24px; padding: 3px 9px; border-radius: 999px; background: #eef2ff; color: #3730a3; font-size: 12px; font-weight: 700; }
.status-pill.active { background: var(--success-bg); color: var(--success); }
.status-pill.inactive { background: #f2f4f7; color: #667085; }

.form-group { margin-bottom: 19px; }
.form-group label:not(.choice-row):not(.checkbox-row) { display: block; font-weight: 700; margin-bottom: 7px; }
input[type="text"], input[type="search"], input[type="date"], input:not([type]), select, textarea { width: 100%; border: 1px solid #cdd5df; border-radius: 7px; padding: 10px 11px; background: white; color: var(--text); }
input:focus, select:focus, textarea:focus { outline: 3px solid rgba(54,89,217,.14); border-color: var(--primary); }
textarea { resize: vertical; }
.form-group small { display: block; margin-top: 6px; color: var(--muted); }
.checkbox-row, .choice-row { display: flex; gap: 9px; align-items: center; }
.checkbox-row input, .choice-row input { width: 16px; height: 16px; }
.choice-list { display: grid; gap: 8px; }
.field-validation, .validation-summary { color: var(--danger); font-size: 13px; }
.validation-summary ul { margin: 0 0 16px; padding-left: 20px; }
.required-mark { font-size: 10px; text-transform: uppercase; color: var(--danger); margin-left: 5px; }

.two-column-layout { display: grid; grid-template-columns: minmax(300px,.75fr) minmax(440px,1.25fr); gap: 18px; align-items: start; }
.field-list { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.field-list-item { display: flex; justify-content: space-between; align-items: center; gap: 14px; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.field-list-item:last-child { border-bottom: 0; }
.field-list-item span { display: block; color: var(--muted); font-size: 12px; margin-top: 3px; }
.inline-actions form { display: inline; margin: 0; }
.icon-button, .link-button { border: 0; background: transparent; color: var(--primary); padding: 3px 5px; }
.icon-button:disabled { opacity: .25; cursor: default; }
.link-button.danger { color: var(--danger); }
.danger-zone { margin-top: 28px; border: 1px solid #fecdca; background: var(--danger-bg); border-radius: 11px; padding: 22px; }
.danger-zone p { color: #7a271a; }

.search-bar { padding: 14px; display: flex; gap: 9px; align-items: center; border-bottom: 1px solid var(--border); }
.search-bar input { max-width: 480px; }
.editor-layout { display: grid; grid-template-columns: minmax(0, 1fr) 390px; gap: 18px; align-items: start; }
.editor-layout > div { display: grid; gap: 18px; }
.relationship-panel { position: sticky; top: 78px; max-height: calc(100vh - 100px); display: flex; flex-direction: column; }
.lookup-list { overflow-y: auto; margin-top: 14px; padding-right: 3px; }
.lookup-group { margin-bottom: 18px; }
.lookup-group h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 7px; }
.lookup-option { display: flex; gap: 10px; align-items: flex-start; padding: 9px; border-radius: 7px; }
.lookup-option:hover { background: var(--surface-2); }
.lookup-option input { margin-top: 3px; }
.lookup-option span { min-width: 0; }
.lookup-option strong, .lookup-option small { display: block; }
.lookup-option small { color: var(--muted); margin-top: 2px; }
.sticky-actions { position: sticky; bottom: 0; background: rgba(245,247,251,.94); backdrop-filter: blur(10px); border-top: 1px solid var(--border); padding: 16px 0; display: flex; gap: 9px; margin-top: 18px; z-index: 4; }

.details-layout { display: grid; grid-template-columns: minmax(0,1fr) 360px; gap: 18px; align-items: start; }
.details-layout > div { display: grid; gap: 18px; }
.details-panel { padding: 22px; }
.detail-row { display: grid; grid-template-columns: 180px 1fr; gap: 18px; padding: 13px 0; border-bottom: 1px solid var(--border); }
.detail-row:last-child { border-bottom: 0; }
.detail-row > span { color: var(--muted); font-size: 13px; }
.pre-wrap { white-space: pre-wrap; }
.related-card-list { display: grid; gap: 9px; }
.related-card { display: block; border: 1px solid var(--border); border-radius: 8px; padding: 12px; color: var(--text); }
.related-card:hover { text-decoration: none; border-color: var(--primary); }
.related-card span, .related-card small { display: block; color: var(--muted); font-size: 12px; }
.related-card strong { display: block; margin: 3px 0; }

.alert { border-radius: 8px; padding: 12px 14px; margin-bottom: 20px; }
.alert-success { background: var(--success-bg); color: var(--success); border: 1px solid #abefc6; }
.empty-state { text-align: center; padding: 52px 24px; color: var(--muted); }
.empty-state.compact { padding: 24px; }

@media (max-width: 1100px) {
    .card-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
    .editor-layout, .details-layout { grid-template-columns: 1fr; }
    .relationship-panel { position: static; max-height: none; }
}

@media (max-width: 820px) {
    .app-shell { display: block; }
    .sidebar { position: fixed; z-index: 20; width: 270px; transform: translateX(-100%); transition: transform .2s ease; }
    body.sidebar-open .sidebar { transform: translateX(0); }
    .sidebar-toggle { display: inline-block; }
    .page-container { padding: 24px 18px; }
    .page-header { display: block; }
    .page-header .button-row, .page-header > .button { margin-top: 16px; }
    .metric-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
    .two-column-layout { grid-template-columns: 1fr; }
    .topbar { padding: 0 18px; }
}

@media (max-width: 560px) {
    .card-grid, .metric-grid { grid-template-columns: 1fr; }
    .detail-row { grid-template-columns: 1fr; gap: 6px; }
    .field-list-item { align-items: flex-start; flex-direction: column; }
    .search-bar { align-items: stretch; flex-direction: column; }
}

.module-title-link { color: var(--text); }
.module-title-link:hover { color: var(--primary); }
.active-button { border-color: var(--primary); color: var(--primary); background: #eef2ff; }
.record-name-link { color: var(--text); }
.record-name-link:hover { color: var(--primary); }
.cell-value { display: block; min-width: 90px; max-width: 520px; white-space: pre-wrap; overflow-wrap: anywhere; }

.active-filter-bar { display: flex; justify-content: space-between; align-items: center; gap: 16px; margin: -8px 0 18px; padding: 12px 14px; border: 1px solid #c7d7fe; border-radius: 8px; background: #f5f8ff; }
.active-filter-bar strong { display: block; font-size: 12px; color: var(--primary); text-transform: uppercase; letter-spacing: .05em; margin-bottom: 4px; }
.active-filter-bar code { white-space: pre-wrap; overflow-wrap: anywhere; }
.alert-error { background: var(--danger-bg); color: #7a271a; border: 1px solid #fecdca; }
.alert-error strong, .alert-error span { display: block; }
.alert-error span { margin-top: 4px; }
.compact-alert { margin: 14px 0; }

.app-dialog { width: min(680px, calc(100vw - 32px)); max-height: calc(100vh - 48px); padding: 0; border: 0; border-radius: 12px; color: var(--text); box-shadow: 0 24px 70px rgba(16, 24, 40, .24); overflow: auto; }
.app-dialog::backdrop { background: rgba(17, 24, 39, .55); backdrop-filter: blur(2px); }
.app-dialog > * { margin-left: 24px; margin-right: 24px; }
.app-dialog > .dialog-header { margin: 0; padding: 22px 24px 14px; border-bottom: 1px solid var(--border); }
.app-dialog > p { margin-top: 18px; }
.app-dialog > form { margin-bottom: 0; padding-bottom: 22px; }
.dialog-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; }
.dialog-header h2 { font-size: 24px; }
.dialog-close { border: 0; background: transparent; color: var(--muted); font-size: 28px; line-height: 1; padding: 0 3px; }
.dialog-close:hover { color: var(--text); }
.dialog-actions { display: flex; justify-content: flex-end; align-items: center; gap: 9px; border-top: 1px solid var(--border); margin-top: 22px; padding-top: 18px; }
.column-choice-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; margin-top: 18px; }
.column-choice { display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); }
.column-choice:hover { border-color: #b8c5db; }
.column-choice input { width: 17px; height: 17px; }
.filter-reference { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 16px 0 20px; }
.filter-reference > div { border: 1px solid var(--border); border-radius: 8px; padding: 12px; background: var(--surface-2); }
.filter-reference strong, .filter-reference span { display: block; }
.filter-reference span { color: var(--muted); margin-top: 4px; font-size: 13px; }
.field-chip-list { display: flex; flex-wrap: wrap; gap: 7px; }
.field-chip-list code { border: 1px solid var(--border); border-radius: 6px; background: var(--surface-2); padding: 5px 7px; font-size: 12px; }

@media (max-width: 560px) {
    .active-filter-bar { align-items: flex-start; flex-direction: column; }
    .column-choice-grid, .filter-reference { grid-template-columns: 1fr; }
    .dialog-actions { align-items: stretch; flex-direction: column-reverse; }
    .dialog-actions .button { width: 100%; }
}
