:root {
    --color-app-bg: #f8fafc;
    --color-surface: #ffffff;
    --color-surface-soft: #f1f5f9;
    --color-brand: #204286;
    --color-brand-dark: #16315f;
    --color-brand-soft: #e8eef8;
    --color-text: #1e293b;
    --color-text-muted: #64748b;
    --color-border: #e2e8f0;
    --color-border-strong: #cbd5e1;
    --color-success-bg: #f0fdf4;
    --color-success-text: #15803d;
    --color-warning-bg: #fffbeb;
    --color-warning-text: #b45309;
    --color-danger-bg: #fef2f2;
    --color-danger-text: #b91c1c;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    --shadow-soft: 0 18px 40px rgba(32, 66, 134, 0.06);
    --radius-sm: 0.5rem;
    --radius-md: 0.875rem;
    --radius-lg: 1.25rem;
    --radius-xl: 1.5rem;
    --transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-normal: 250ms cubic-bezier(0.4, 0, 0.2, 1);
}

* {
    box-sizing: border-box;
}

html {
    font-size: 16px;
}

body {
    margin: 0;
    background: var(--color-app-bg);
    color: var(--color-text);
    font-family: "Figtree", Arial, sans-serif;
    line-height: 1.5;
}

a {
    color: var(--color-brand);
    text-decoration: none;
    transition: color 150ms ease;
}

a:hover,
a:focus {
    color: var(--color-brand-dark);
    outline: none;
}

code,
pre {
    font-family: Consolas, "Courier New", monospace;
}

code {
    display: inline-block;
    border-radius: 0.65rem;
    background: var(--color-brand-soft);
    padding: 0.2rem 0.5rem;
    color: var(--color-brand-dark);
}

button,
.button-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-md);
    border: 1px solid transparent;
    background: linear-gradient(180deg, #2851a3 0%, var(--color-brand) 100%);
    box-shadow: 0 1px 2px rgba(0,0,0,0.1), inset 0 1px 0 rgba(255,255,255,0.1);
    padding: 0.75rem 1.25rem;
    color: #ffffff;
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    cursor: pointer;
    transition: all var(--transition-fast);
}

button:hover,
button:focus,
.button-link:hover,
.button-link:focus {
    background: linear-gradient(180deg, #2a55ab 0%, #1c3a76 100%);
    transform: translateY(-1px);
    box-shadow: 0 4px 6px rgba(32, 66, 134, 0.2), inset 0 1px 0 rgba(255,255,255,0.1);
    outline: none;
    color: #ffffff;
}

button:active,
.button-link:active {
    transform: translateY(0);
    box-shadow: none;
}

button.secondary,
.button-link.secondary {
    border-color: var(--color-border);
    background: var(--color-surface);
    box-shadow: var(--shadow-sm);
    color: var(--color-text);
}

button.secondary:hover,
button.secondary:focus,
.button-link.secondary:hover,
.button-link.secondary:focus {
    border-color: var(--color-border-strong);
    background: #f8fafc;
    color: var(--color-brand);
    box-shadow: var(--shadow-md);
}

button.danger,
button.logout,
.button-link.danger {
    background: var(--color-brand-dark);
}

button.danger:hover,
button.logout:hover,
button.danger:focus,
button.logout:focus,
.button-link.danger:hover,
.button-link.danger:focus {
    background: #102344;
}

label {
    display: block;
    color: var(--color-text);
    font-size: 0.875rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

input,
select,
textarea {
    width: 100%;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    background: #f8fafc;
    padding: 0.75rem 1rem;
    color: var(--color-text);
    font: inherit;
    transition: all var(--transition-fast);
}

input::placeholder,
textarea::placeholder {
    color: #8c9ab0;
}

input:hover,
select:hover,
textarea:hover {
    background: var(--color-surface);
    border-color: var(--color-border-strong);
}

input:focus,
select:focus,
textarea:focus {
    outline: none;
    background: var(--color-surface);
    border-color: var(--color-brand);
    box-shadow: 0 0 0 4px var(--color-brand-soft);
}

textarea {
    min-height: 12rem;
    resize: vertical;
}

.container {
    max-width: 1280px;
    margin: 0 auto;
    padding: 2rem 1.25rem 2.5rem;
}

.stack-sm > * + * {
    margin-top: 0.75rem;
}

.stack-md > * + * {
    margin-top: 1rem;
}

.stack-lg > * + * {
    margin-top: 1.5rem;
}

.surface-card,
.panel,
.card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    transition: box-shadow var(--transition-fast);
}

.surface-card:hover,
.panel:hover,
.card:hover {
    box-shadow: var(--shadow-md);
}

.panel,
.card {
    padding: 1.5rem;
}

.surface-card-padded {
    padding: 2rem;
}

.page-header,
.topbar {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.page-kicker,
.eyebrow-text {
    color: var(--color-text-muted);
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    margin: 0 0 0.5rem;
}

.page-title,
h1 {
    margin: 0;
    color: var(--color-text);
    font-size: clamp(1.8rem, 2.5vw, 2.5rem);
    font-weight: 700;
    line-height: 1.15;
    letter-spacing: -0.02em;
}

.page-subtitle,
.muted {
    color: var(--color-text-muted);
    font-size: 0.95rem;
}

.section-title,
h2,
h3 {
    margin: 0;
    color: var(--color-text);
    font-weight: 700;
    line-height: 1.2;
    letter-spacing: -0.01em;
}

h2 {
    font-size: 1.2rem;
}

h3 {
    font-size: 1rem;
}

.hero-card {
    background:
        radial-gradient(circle at top right, rgba(32, 66, 134, 0.12), transparent 32%),
        linear-gradient(135deg, rgba(255, 255, 255, 0.96), rgba(232, 238, 248, 0.7));
}

.hero-copy {
    max-width: 48rem;
}

.content-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 1.5rem;
}

.editor-split-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 1.5rem;
    align-items: start;
}

.editor-form-pane {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.editor-diagram-pane {
    position: sticky;
    top: 5rem;
    height: calc(100vh - 6.5rem);
    display: flex;
    flex-direction: column;
}

.editor-diagram-sticky {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.editor-diagram-sticky .hydraulic-diagram-shell {
    flex: 1;
    height: 100%;
}

@media (max-width: 1200px) {
    .editor-split-layout {
        grid-template-columns: 1fr;
    }
    .editor-diagram-pane {
        position: static;
        height: 60vh;
        order: -1; /* Puts diagram above form on small screens */
    }
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
}

.field-grid {
    display: grid;
    gap: 1rem;
}

.field-stack {
    display: grid;
    gap: 0.5rem;
}

.field-with-unit {
    position: relative;
    display: block;
    isolation: isolate;
    width: 100%;
}

.field-with-unit > input {
    display: block;
    width: 100%;
    padding-right: 5.75rem;
}

.field-with-unit > .field-unit {
    display: block;
    position: absolute;
    top: 50%;
    right: 1rem;
    transform: translateY(-50%);
    z-index: 1;
    color: var(--color-text-muted);
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    white-space: nowrap;
    pointer-events: none;
}

.inline-note {
    color: var(--color-text-muted);
    font-size: 0.85rem;
}

.checkbox-row {
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
    font-weight: 600;
}

.checkbox-row input[type="checkbox"] {
    width: 1rem;
    height: 1rem;
    margin-top: 0.2rem;
    accent-color: var(--color-brand);
}

.table-shell {
    overflow-x: auto;
    border: 1px solid var(--color-border);
    border-radius: 1rem;
    background: var(--color-surface);
}

.table-shell table {
    margin-top: 0;
}

.table-shell .table-wide {
    min-width: 1500px;
}

.table-shell .table-wide th,
.table-shell .table-wide td {
    white-space: nowrap;
}

.table-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.55rem;
    align-items: center;
}

.table-link {
    color: var(--color-brand);
    font-size: 0.875rem;
    font-weight: 600;
}

.table-link:hover,
.table-link:focus {
    color: var(--color-brand-dark);
}

.metric-value-sm {
    font-size: 1rem;
    word-break: break-word;
}

.actions,
.header-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    align-items: center;
}

.flash,
.error,
.warn,
.status-box {
    border-radius: 1rem;
    padding: 0.9rem 1rem;
    margin-bottom: 1rem;
    border: 1px solid transparent;
    font-size: 0.95rem;
}

.flash {
    background: var(--color-success-bg);
    border-color: #c9e7d4;
    color: var(--color-success-text);
}

.error {
    background: var(--color-danger-bg);
    border-color: #efcaca;
    color: var(--color-danger-text);
}

.warn {
    background: var(--color-warning-bg);
    border-color: #f3d7ab;
    color: var(--color-warning-text);
}

.summary {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.summary .card strong,
.metric-title {
    display: block;
    color: var(--color-text-muted);
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.summary .card div,
.metric-value {
    margin-top: 0.6rem;
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--color-text);
}

table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 1rem;
    background: var(--color-surface);
    overflow: hidden;
}

th,
td {
    border-bottom: 1px solid var(--color-border);
    padding: 1rem;
    text-align: left;
    vertical-align: top;
    font-size: 0.92rem;
    transition: background-color var(--transition-fast);
}

tbody tr:hover td {
    background-color: var(--color-surface-soft);
}

th {
    background: #f8fafc;
    color: var(--color-text-muted);
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    border-bottom: 2px solid var(--color-border);
}

tr:last-child td {
    border-bottom: none;
}

.badge {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    padding: 0.35rem 0.8rem;
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.04em;
}

.badge.ok,
.ok {
    background: var(--color-success-bg);
    color: var(--color-success-text);
}

.badge.off,
.rd {
    background: var(--color-danger-bg);
    color: var(--color-danger-text);
}

.links code {
    display: block;
    margin-bottom: 0.75rem;
    padding: 0.9rem 1rem;
    background: var(--color-surface-soft);
    border: 1px solid var(--color-border);
}

.hint {
    color: var(--color-text-muted);
    font-size: 0.85rem;
    margin-top: 0.5rem;
}

.inline-form {
    display: inline;
}

.link-button {
    background: transparent;
    border: none;
    padding: 0;
    color: var(--color-brand);
    font-size: 0.875rem;
    font-weight: 600;
    letter-spacing: normal;
    text-transform: none;
    box-shadow: none;
}

.link-button:hover,
.link-button:focus {
    background: transparent;
    color: var(--color-brand-dark);
}

.auth-shell {
    width: 100%;
    min-height: 100vh;
}

.auth-panel-copy {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 1.5rem;
    padding: 2.5rem;
}

.auth-brand {
    display: inline-flex;
    align-items: center;
    gap: 0.9rem;
    font-weight: 700;
    color: var(--color-text);
}

.brand-logo {
    display: block;
    width: auto;
    height: 2.75rem;
    max-width: 11rem;
    object-fit: contain;
}

.auth-brand-mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 3rem;
    height: 3rem;
    border-radius: 999px;
    background: var(--color-brand-soft);
    color: var(--color-brand);
    font-size: 1.15rem;
    font-weight: 700;
}

.auth-copy-content {
    max-width: 44rem;
    display: grid;
    gap: 1.5rem;
}

.auth-title-accent {
    display: block;
    color: var(--color-brand);
}

.auth-copy-lead {
    max-width: 38rem;
    font-size: 1rem;
    line-height: 1.9;
}

.auth-form-intro {
    margin-bottom: 1.5rem;
    display: grid;
    gap: 0.5rem;
}

.auth-form-title {
    font-size: 1.65rem;
}

.auth-panel-login {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 2rem 1.5rem;
    background-image: linear-gradient(rgba(18, 34, 67, 0.58), rgba(18, 34, 67, 0.22)), url("./auth-background.svg");
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
}

.auth-panel-login::before {
    content: "";
    position: absolute;
    inset: 1.5rem;
    border-radius: 1.5rem;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.12), rgba(255, 255, 255, 0.03));
    border: 1px solid rgba(255, 255, 255, 0.24);
}

.auth-panel-login::after {
    content: "";
    position: absolute;
    inset: 0;
    background:
        radial-gradient(circle at 22% 18%, rgba(32, 66, 134, 0.42), transparent 28%),
        radial-gradient(circle at 76% 76%, rgba(245, 247, 242, 0.22), transparent 24%),
        linear-gradient(135deg, rgba(255, 255, 255, 0.08), transparent 38%);
    pointer-events: none;
}

.auth-card {
    position: relative;
    z-index: 1;
    width: min(100%, 26rem);
    padding: 2rem;
}

.auth-card h1 {
    font-size: 1.7rem;
}

.auth-card .field {
    margin-bottom: 1rem;
}

.app-nav {
    border-bottom: 1px solid rgba(226, 232, 240, 0.8);
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    position: sticky;
    top: 0;
    z-index: 40;
}

.app-nav-inner {
    max-width: 1280px;
    margin: 0 auto;
    padding: 1rem 1.25rem;
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    align-items: center;
}

.app-nav-main {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 1rem;
    margin-left: auto;
}

.app-nav-links {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    align-items: center;
}

.app-nav-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
}

.nav-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.25rem 0;
    border: none;
    background: transparent;
    color: var(--color-text);
    font-size: 0.875rem;
    font-weight: 600;
}

.nav-chip:hover,
.nav-chip:focus {
    color: var(--color-brand);
}

.nav-logout-form {
    margin: 0;
}

.nav-logout-button {
    min-width: 7rem;
}

.section-stack > * + * {
    margin-top: 1.5rem;
}

.preformatted {
    white-space: pre-wrap;
    word-break: break-word;
    background: #152645;
    color: #edf2fb;
    border-radius: 1rem;
    padding: 1.25rem;
    overflow-x: auto;
    border: 1px solid #24477e;
}

.balance {
    margin-top: 0.5rem;
    color: var(--color-text-muted);
    font-size: 0.8rem;
    line-height: 1.5;
}

.hydraulic-diagram-viewer {
    display: grid;
    gap: 1rem;
}

.hydraulic-diagram-toolbar {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 0.75rem;
    align-items: center;
}

.hydraulic-diagram-toolbar-group {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
}

.diagram-control-button {
    min-width: 2.75rem;
    padding: 0.55rem 0.8rem;
    border-radius: 0.75rem;
    border: 1px solid var(--color-border);
    background: var(--color-surface);
    color: var(--color-text);
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.diagram-control-button:hover,
.diagram-control-button:focus {
    border-color: var(--color-brand);
    background: var(--color-brand-soft);
    color: var(--color-brand);
}

.diagram-control-button.is-active {
    border-color: var(--color-brand);
    background: var(--color-brand);
    color: #ffffff;
}

.hydraulic-diagram-state {
    color: var(--color-text-muted);
    font-size: 0.8rem;
    font-weight: 600;
}

.hydraulic-diagram-stage {
    overflow: hidden;
    border: 1px solid var(--color-border);
    border-radius: 1rem;
    background:
        radial-gradient(circle at top right, rgba(32, 66, 134, 0.08), transparent 25%),
        linear-gradient(180deg, #ffffff, #f7f9fd);
    min-height: 320px;
    cursor: grab;
}

.hydraulic-diagram-stage.is-grabbing {
    cursor: grabbing;
}

.hydraulic-diagram-stage svg {
    display: block;
}

.hydraulic-diagram-empty {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 320px;
    color: var(--color-text-muted);
    font-size: 0.95rem;
}

.hydraulic-diagram-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    align-items: center;
}

.hydraulic-diagram-legend-item {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    color: var(--color-text-muted);
    font-size: 0.8rem;
    font-weight: 600;
}

.hydraulic-diagram-legend-dot {
    width: 0.75rem;
    height: 0.75rem;
    border-radius: 999px;
    display: inline-block;
}

.hydraulic-diagram-legend-dot.water {
    background: #1a5fd4;
}

.hydraulic-diagram-legend-dot.slurry {
    background: #b45309;
}

.hydraulic-diagram-legend-dot.node {
    background: #15803d;
}

@media (min-width: 1024px) {
    .auth-shell {
        display: grid;
        grid-template-columns: minmax(0, 60%) minmax(360px, 40%);
    }

    .auth-panel-copy {
        padding: 4rem 5rem;
    }
}

@media (max-width: 900px) {
    .container {
        padding: 1.25rem 1rem 2rem;
    }

    .content-grid,
    .content-grid-wide {
        grid-template-columns: 1fr;
    }

    .page-header,
    .topbar,
    .app-nav-inner,
    .app-nav-main {
        flex-direction: column;
        align-items: stretch;
    }

    .app-nav-actions {
        justify-content: flex-start;
    }
}

@media (max-width: 1100px) {
}

@media print {
    body {
        background: #fff;
        color: #000;
        font-size: 10pt;
    }
    
    .app-nav, 
    .actions, 
    .editor-tabs, 
    .editor-tabs-wrapper,
    .hint, 
    .button-link, 
    button, 
    .nav-logout-form {
        display: none !important;
    }
    
    .container {
        max-width: 100%;
        margin: 0;
        padding: 0;
    }
    
    .surface-card, 
    .panel, 
    .table-shell {
        box-shadow: none !important;
        border: 1px solid #ccc !important;
        margin-bottom: 1rem;
        page-break-inside: avoid;
    }
    
    .hero-card {
        background: none !important;
        border: none !important;
        padding: 0 0 1rem 0 !important;
    }
    
    .page-title {
        font-size: 18pt !important;
    }

    .table-shell {
        overflow: visible !important;
    }

    table {
        width: 100% !important;
        border-collapse: collapse !important;
    }
    
    th, td {
        border-bottom: 1px solid #ddd !important;
        padding: 0.4rem !important;
        font-size: 9pt !important;
    }

    th {
        background: #f1f5f9 !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    tr {
        page-break-inside: avoid;
    }
    
    .hydraulic-diagram-shell {
        border: 1px solid #ccc !important;
        height: auto !important;
        min-height: 400px;
        page-break-inside: avoid;
    }
    
    .badge {
        border: 1px solid #aaa !important;
        background: none !important;
        color: #000 !important;
    }
}
