:root {
    --page-max: 1440px;
    --page-gutter: 1.5rem;
    color-scheme: light;
    --navy: #152c6b;
    --navy-2: #0e204f;
    --ink: #172033;
    --muted: #667085;
    --line: #d8deea;
    --surface: #ffffff;
    --surface-soft: #f5f7fb;
    --accent: #2f65d5;
    --danger: #b42318;
    --success: #18794e;
    --shadow: 0 14px 40px rgba(21, 44, 107, .10);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
* { box-sizing: border-box; }
html, body { width: 100%; max-width: 100%; overflow-x: hidden; }
body { margin: 0; color: var(--ink); background: #eef2f8; }
button, input, textarea, select { font: inherit; min-width: 0; max-width: 100%; }
.app-header {
    display: flex; justify-content: space-between; gap: 2rem; align-items: end;
    width: 100%;
    padding-block: 2.25rem;
    padding-inline: max(var(--page-gutter), calc((100vw - var(--page-max)) / 2 + var(--page-gutter)));
    color: white;
    background: linear-gradient(125deg, var(--navy-2), var(--navy));
}
.app-header h1 { margin: .2rem 0 .45rem; font-size: clamp(2rem, 4vw, 3.15rem); letter-spacing: -.035em; }
.app-header p { margin: 0; max-width: 780px; color: #dbe5ff; line-height: 1.55; }
.eyebrow { text-transform: uppercase; letter-spacing: .18em; font-size: .78rem; font-weight: 800; }
.app-header > div, .app-header > nav { min-width: 0; max-width: 100%; }
.header-actions { display: flex; gap: .75rem; flex-wrap: wrap; }
.layout { display: grid; grid-template-columns: minmax(0, 1fr) 330px; gap: 1.5rem; width: 100%; max-width: var(--page-max); margin: 0 auto; padding: 1.5rem var(--page-gutter); align-items: start; }
.layout > *, form, .card, .summary-panel, .summary-card { min-width: 0; max-width: 100%; }
form { display: grid; gap: 1rem; min-width: 0; }
.card, .summary-card { background: var(--surface); border: 1px solid rgba(21,44,107,.10); border-radius: 18px; box-shadow: var(--shadow); }
.card { padding: 1.35rem; }
.section-heading { display: flex; gap: .9rem; align-items: center; margin-bottom: 1.15rem; }
.section-heading > span { display: grid; place-items: center; width: 2.25rem; height: 2.25rem; flex: 0 0 auto; border-radius: 50%; background: var(--navy); color: white; font-weight: 800; }
.section-heading h2 { margin: 0 0 .2rem; font-size: 1.25rem; }
.section-heading p { margin: 0; color: var(--muted); font-size: .92rem; }
.form-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .9rem; min-width: 0; }
.form-grid > *, .split-grid > *, .service-row > *, .check-grid > * { min-width: 0; }
.form-grid.cols-5 { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.form-grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.span-2 { grid-column: span 2; }
.split-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin: 1rem 0; }
label { display: grid; gap: .38rem; font-size: .86rem; font-weight: 700; color: #344054; }
input, textarea, select {
    width: 100%; border: 1px solid #cbd3e1; border-radius: 10px; padding: .72rem .78rem;
    background: #fff; color: var(--ink); outline: none; transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(47,101,213,.13); }
textarea { resize: vertical; min-height: 150px; line-height: 1.5; }
fieldset { border: 1px solid var(--line); border-radius: 14px; padding: 1rem; margin: 1rem 0 0; min-width: 0; }
legend { padding: 0 .45rem; color: var(--navy); font-weight: 800; }
.check-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .7rem .9rem; margin-bottom: .9rem; }
.check-grid.wide { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.check-grid.inline { display: flex; flex-wrap: wrap; }
.check-grid label, .toggle { display: flex; flex-direction: row; align-items: center; gap: .5rem; font-weight: 600; }
.check-grid input, .toggle input { width: 1.05rem; height: 1.05rem; padding: 0; accent-color: var(--navy); flex: 0 0 auto; }

.service-toolbar { display: flex; justify-content: space-between; align-items: end; gap: 1rem; margin-bottom: .85rem; }
.service-toolbar h3 { margin: 0 0 .2rem; font-size: 1rem; color: var(--navy); }
.service-toolbar p { margin: 0; color: var(--muted); font-size: .85rem; }
.service-rows { display: grid; gap: .7rem; }
.service-add-row { display: flex; justify-content: flex-start; margin-top: .85rem; }
.service-row { display: grid; grid-template-columns: minmax(240px, 1.55fr) minmax(180px, 1fr) minmax(120px, .55fr) auto; gap: .7rem; align-items: end; padding: .8rem; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-soft); }
.service-row label { min-width: 0; }
.service-row select, .service-row input { background: #fff; }
.remove-service { align-self: end; min-height: 43px; color: var(--danger); background: #fff; border: 1px solid #efc8c4; }
.service-footer { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-top: .9rem; padding: .85rem 1rem; border-radius: 12px; background: #eef3ff; }
.service-total { display: flex; align-items: baseline; gap: .65rem; color: var(--navy); font-weight: 800; }
.service-total output { font-size: 1.18rem; }
.service-total-note { margin: 0; max-width: 34rem; color: var(--muted); font-size: .86rem; font-weight: 500; line-height: 1.45; }
.field-help { color: var(--muted); font-size: .75rem; font-weight: 500; line-height: 1.35; }
.scope-controls { display: flex; justify-content: space-between; align-items: center; gap: .75rem; flex-wrap: wrap; margin: 1rem 0 .8rem; }
.compact-button { padding: .5rem .75rem; }
.scope-status { display: flex; justify-content: space-between; gap: 1rem; align-items: start; }
.scope-fit-warning { color: var(--muted); font-size: .8rem; line-height: 1.4; }
.scope-fit-warning.warning { color: var(--danger); font-weight: 700; }

.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 12px; }
.measurement-add-row { display: flex; align-items: center; gap: .75rem; margin: .85rem 0; }
table { border-collapse: collapse; width: 100%; }
th, td { padding: .55rem; border-bottom: 1px solid var(--line); text-align: left; }
th { background: var(--surface-soft); font-size: .8rem; color: #475467; }
td:first-child, th:first-child { width: 42px; text-align: center; }
td input { min-width: 90px; padding: .5rem .6rem; }
tfoot th, tfoot td { border-bottom: 0; font-weight: 800; }
.character-count { margin-top: .45rem; text-align: right; color: var(--muted); font-size: .8rem; }
.calculation-bar { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; margin-top: 1rem; padding-top: 1rem; border-top: 1px solid var(--line); }
.warnings { color: var(--danger); font-size: .85rem; font-weight: 700; }
.button { display: inline-flex; align-items: center; justify-content: center; max-width: 100%; border: 0; border-radius: 10px; padding: .72rem 1rem; font-weight: 800; text-align: center; white-space: normal; cursor: pointer; text-decoration: none; transition: transform .12s, opacity .12s; }
.button:hover { transform: translateY(-1px); }
.button:disabled { opacity: .55; cursor: wait; transform: none; }
.button.primary { color: #fff; background: var(--navy); }
.button.secondary { color: var(--navy); background: #e7edff; }
.button.ghost { color: inherit; background: transparent; border: 1px solid currentColor; }
.button.danger { color: #fff; background: var(--danger); }
.app-header .button.secondary { color: white; background: rgba(255,255,255,.14); }
.app-header .button.ghost { color: white; }
.sticky-actions { position: sticky; bottom: 1rem; z-index: 5; display: flex; justify-content: flex-end; gap: .7rem; padding: .8rem; border: 1px solid rgba(21,44,107,.15); border-radius: 14px; background: rgba(255,255,255,.93); backdrop-filter: blur(12px); box-shadow: var(--shadow); }
.summary-panel { position: sticky; top: 1rem; display: grid; gap: 1rem; }
.summary-card { padding: 1.25rem; }
.summary-card h2, .summary-card h3 { margin-top: 0; }
.summary-card dl { margin: 0; }
.summary-card dl div { display: flex; justify-content: space-between; gap: 1rem; padding: .72rem 0; border-bottom: 1px solid var(--line); }
.summary-card dt { color: var(--muted); }
.summary-card dd { min-width: 0; margin: 0; font-weight: 800; text-align: right; overflow-wrap: anywhere; }
.summary-card.muted { background: #f7f9fd; }
.privacy-note, .summary-card p { color: var(--muted); font-size: .85rem; line-height: 1.55; }
dialog { width: min(980px, 96vw); height: min(920px, 92vh); padding: 0; border: 0; border-radius: 16px; box-shadow: 0 40px 100px rgba(0,0,0,.30); }
dialog::backdrop { background: rgba(3, 10, 28, .72); }
.dialog-header { display: flex; justify-content: space-between; align-items: center; padding: .8rem 1rem; border-bottom: 1px solid var(--line); }
.dialog-header h2 { margin: 0; font-size: 1rem; }
.icon-button { border: 0; background: transparent; font-size: 1.8rem; cursor: pointer; }
#pdfPreview { width: 100%; height: calc(100% - 58px); border: 0; background: #667085; }
.toast { position: fixed; right: 1rem; bottom: 1rem; z-index: 20; max-width: 420px; padding: .85rem 1rem; border-radius: 12px; color: white; background: var(--success); box-shadow: var(--shadow); opacity: 0; transform: translateY(12px); pointer-events: none; transition: .2s; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--danger); }
.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; }
@media (max-width: 1100px) {
    .layout { grid-template-columns: 1fr; }
    .summary-panel { position: static; grid-template-columns: 1fr 1fr; }
}
@media (max-width: 800px) {
    :root { --page-gutter: .8rem; }
    .app-header {
        align-items: flex-start;
        flex-direction: column;
        padding-block: 1.5rem;
        padding-inline: var(--page-gutter);
    }
    .layout { padding: .8rem var(--page-gutter); }
    .form-grid, .form-grid.cols-5, .form-grid.cols-3, .split-grid { grid-template-columns: 1fr 1fr; }
    .service-row { grid-template-columns: 1fr 1fr; }
    .remove-service { justify-self: start; }
    .check-grid.wide { grid-template-columns: 1fr 1fr; }
    .span-2 { grid-column: span 2; }
    .sticky-actions { flex-wrap: wrap; }
}
@media (max-width: 520px) {
    .form-grid, .form-grid.cols-5, .form-grid.cols-3, .split-grid, .summary-panel, .check-grid, .check-grid.wide, .service-row { grid-template-columns: minmax(0, 1fr); }
    .card, .summary-card { padding: 1rem; }
    .service-toolbar, .service-footer, .scope-status { align-items: stretch; flex-direction: column; }
    .service-add-row .button, .measurement-add-row .button { width: 100%; }
    .measurement-add-row { align-items: stretch; flex-direction: column; }
    .span-2 { grid-column: auto; }
    .sticky-actions .button { flex: 1 1 100%; }
}

.archive-layout { grid-template-columns: 1fr; max-width: 1200px; }
.archive-actions { display: flex; gap: .55rem; align-items: center; flex-wrap: wrap; }
.archive-actions form { display: inline; margin: 0; }
.status-badge { display: inline-flex; align-items: center; border-radius: 999px; padding: .28rem .62rem; font-size: .75rem; font-weight: 800; }
.status-badge.draft { color: #854a0e; background: #fff1d6; border: 1px solid #f4d08b; }
.status-badge.completed { color: #166a46; background: #dcf7e9; border: 1px solid #a9e2c7; }
.archive-message { margin-top: 0; padding: .8rem 1rem; border-radius: 10px; color: var(--success); background: #e8f7ef; font-weight: 700; }

/* Authentication */
.logout-form { display: inline-flex; margin: 0; }
.header-actions .logout-form { gap: 0; }
.auth-page { min-height: 100vh; display: grid; place-items: center; padding: 1rem; background: linear-gradient(145deg, #0e204f, #1d3f91); }
.auth-shell { width: min(460px, 100%); }
.auth-card { background: var(--surface); border-radius: 20px; padding: clamp(1.4rem, 5vw, 2.2rem); box-shadow: 0 28px 80px rgba(0,0,0,.28); }
.auth-card h1 { margin: .2rem 0 .45rem; color: var(--navy); font-size: 2rem; }
.auth-eyebrow { margin: 0; color: var(--accent); font-size: .76rem; font-weight: 900; letter-spacing: .14em; text-transform: uppercase; }
.auth-intro, .auth-security-note, .account-copy { color: var(--muted); line-height: 1.55; }
.auth-form, .account-form { display: grid; gap: 1rem; margin-top: 1.25rem; }
.auth-submit { width: 100%; margin-top: .2rem; }
.auth-error { margin: 1rem 0; padding: .8rem 1rem; border-radius: 10px; color: #8a1c13; background: #fde9e7; border: 1px solid #f6c8c3; font-weight: 700; }
.auth-security-note { margin: 1.15rem 0 0; font-size: .78rem; }
.account-layout { grid-template-columns: minmax(0, 720px); max-width: 900px; }
.account-card h2 { margin-top: 0; }
.account-form .button { justify-self: start; }
@media (max-width: 520px) {
    .header-actions, .header-actions .button, .header-actions .logout-form, .header-actions .logout-form .button { width: 100%; }
    .header-actions { display: grid; }
    .account-form .button { width: 100%; }
}
