:root {
    --navy-900: #0f1e4f;
    --navy-800: #152861;
    --navy-700: #223874;
    --red-600: #e33b2e;
    --red-500: #f04d3f;
    --sand-100: #f4f0e8;
    --slate-100: #eef2f8;
    --slate-200: #d8dfeb;
    --slate-400: #6d778c;
    --slate-700: #263044;
    --white: #ffffff;
    --shadow-lg: 0 22px 60px rgba(15, 30, 79, 0.18);
    --shadow-md: 0 14px 32px rgba(19, 39, 93, 0.12);
    --radius-xl: 28px;
    --radius-lg: 22px;
    --radius-md: 16px;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    min-height: 100%;
}

html {
    width: 100%;
    overflow-x: hidden;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    width: 100%;
    font-family: "Libre Franklin", sans-serif;
    color: var(--slate-700);
    background:
        radial-gradient(circle at top left, rgba(240, 77, 63, 0.16), transparent 32%),
        radial-gradient(circle at right center, rgba(21, 40, 97, 0.18), transparent 28%),
        linear-gradient(180deg, #f8fafc 0%, #eef3fb 100%);
    overflow-x: hidden;
}

body.modal-open {
    overflow: hidden;
}

a {
    color: inherit;
    text-decoration: none;
}

img {
    display: block;
    max-width: 100%;
}

.guest-shell {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 28px;
}

.login-hero {
    width: 100%;
    max-width: 1240px;
}

.login-panel {
    display: grid;
    grid-template-columns: 1.15fr 0.85fr;
    gap: 28px;
    align-items: stretch;
}

.login-brand,
.auth-card,
.sidebar,
.panel,
.stat-card {
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.65);
}

.login-brand {
    padding: 42px;
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-lg);
    position: relative;
    overflow: hidden;
}

.login-brand::before {
    content: "";
    position: absolute;
    inset: auto -40px -100px auto;
    width: 280px;
    height: 280px;
    border-radius: 50%;
    background: linear-gradient(135deg, rgba(227, 59, 46, 0.2), rgba(15, 30, 79, 0.2));
}

.login-brand img {
    width: min(480px, 100%);
    margin-bottom: 26px;
}

.eyebrow {
    margin: 0 0 10px;
    font-size: 0.78rem;
    font-weight: 800;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--red-600);
}

.login-brand h1,
.auth-card h2,
.panel h2,
.topbar h1 {
    font-family: "Barlow", sans-serif;
    margin: 0;
    color: var(--navy-900);
}

.login-brand h1 {
    font-size: clamp(2.25rem, 4vw, 4.4rem);
    line-height: 0.98;
    max-width: 720px;
}

.support-copy {
    margin: 18px 0 0;
    max-width: 620px;
    font-size: 1.05rem;
    line-height: 1.7;
    color: var(--slate-400);
}

.auth-card {
    padding: 34px;
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-md);
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 18px;
}

.auth-card-header p {
    margin: 10px 0 0;
    color: var(--slate-400);
    line-height: 1.6;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.field span {
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--navy-800);
}

.field input {
    width: 100%;
    border: 1px solid var(--slate-200);
    border-radius: 14px;
    padding: 14px 16px;
    font: inherit;
    font-size: 16px;
    color: var(--slate-700);
    background: var(--white);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.field input:focus {
    outline: none;
    border-color: rgba(227, 59, 46, 0.55);
    box-shadow: 0 0 0 4px rgba(227, 59, 46, 0.12);
}

.button {
    appearance: none;
    border: 0;
    border-radius: 14px;
    padding: 14px 18px;
    font: inherit;
    font-weight: 700;
    cursor: pointer;
    transition: transform 0.18s ease, box-shadow 0.18s ease, background 0.18s ease;
}

.button:hover {
    transform: translateY(-1px);
}

.button-primary {
    color: var(--white);
    background: linear-gradient(135deg, var(--red-600), #d62f22);
    box-shadow: 0 16px 30px rgba(227, 59, 46, 0.22);
}

.button-secondary {
    color: var(--navy-800);
    background: #e6edfa;
    box-shadow: inset 0 0 0 1px rgba(34, 56, 116, 0.3);
}

.button-secondary:hover {
    background: #d9e3f6;
}

.button-navy {
    color: var(--white);
    background: linear-gradient(135deg, var(--navy-900), var(--navy-700));
    box-shadow: 0 10px 22px rgba(15, 30, 79, 0.2);
}

.button-danger {
    color: var(--red-600);
    background: rgba(227, 59, 46, 0.1);
    box-shadow: inset 0 0 0 1px rgba(227, 59, 46, 0.32);
}

.button-danger:hover {
    background: rgba(227, 59, 46, 0.16);
}

.button-block {
    width: 100%;
}

.document-cta {
    min-width: 176px;
    justify-content: center;
    text-align: center;
}

.document-cta-proposal {
    color: var(--white);
    background: linear-gradient(135deg, var(--navy-900), var(--navy-700));
    box-shadow: 0 16px 30px rgba(15, 30, 79, 0.2);
}

.document-cta-invoice,
.document-cta-receipt {
    color: var(--white);
    background: linear-gradient(135deg, var(--red-600), #d62f22);
    box-shadow: 0 16px 30px rgba(227, 59, 46, 0.22);
}

.auth-card-footer {
    display: grid;
    gap: 6px;
    padding: 14px 16px;
    border-radius: 16px;
    background: linear-gradient(180deg, rgba(15, 30, 79, 0.04), rgba(15, 30, 79, 0.08));
    color: var(--slate-400);
    font-size: 0.92rem;
}

.auth-card-footer code {
    padding: 2px 8px;
    border-radius: 999px;
    background: rgba(15, 30, 79, 0.08);
    color: var(--navy-900);
    font-family: "Barlow", sans-serif;
    font-size: 0.9rem;
}

.flash {
    position: fixed;
    top: 18px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 50;
    max-width: 560px;
    width: calc(100% - 24px);
    border-radius: 14px;
    padding: 14px 18px;
    font-weight: 600;
    box-shadow: var(--shadow-md);
    opacity: 1;
    transition: opacity 0.28s ease, transform 0.28s ease;
}

.flash.is-hiding {
    opacity: 0;
    transform: translateX(-50%) translateY(-10px);
}

.flash-success {
    background: #e7f7ea;
    color: #17603a;
}

.flash-error {
    background: #ffebea;
    color: #8a2118;
}

.app-shell {
    padding: 20px;
}

.app-frame {
    display: grid;
    grid-template-columns: 290px minmax(0, 1fr);
    gap: 20px;
    min-height: calc(100vh - 40px);
    min-width: 0;
}

.app-frame > * {
    min-width: 0;
}

.sidebar {
    border-radius: var(--radius-xl);
    padding: 24px;
    box-shadow: var(--shadow-md);
    display: flex;
    flex-direction: column;
    gap: 28px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 14px;
}

.brand img {
    width: 82px;
}

.brand strong {
    display: block;
    font-family: "Barlow", sans-serif;
    font-size: 1.18rem;
    color: var(--navy-900);
}

.brand span {
    color: var(--slate-400);
    font-size: 0.92rem;
}

.sidebar-nav {
    display: grid;
    gap: 8px;
}

.nav-link {
    display: flex;
    align-items: center;
    min-height: 48px;
    padding: 0 14px;
    border-radius: 14px;
    font-weight: 700;
    color: var(--navy-900);
    background: rgba(15, 30, 79, 0.04);
}

.nav-link.is-active {
    color: var(--white);
    background: linear-gradient(135deg, var(--navy-900), var(--navy-700));
}

.nav-link:hover {
    background: rgba(15, 30, 79, 0.08);
}

.is-disabled {
    opacity: 0.55;
    pointer-events: none;
}

.sidebar-logout {
    margin-top: auto;
}

.main-panel {
    display: flex;
    flex-direction: column;
    gap: 20px;
    min-width: 0;
}

.topbar,
.panel {
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-md);
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 24px 28px;
    background: linear-gradient(135deg, rgba(15, 30, 79, 0.95), rgba(34, 56, 116, 0.92));
    color: var(--white);
}

.topbar h1 {
    color: var(--white);
    font-size: clamp(1.65rem, 3vw, 2.4rem);
}

.topbar .eyebrow {
    color: rgba(255, 255, 255, 0.75);
}

.topbar-user {
    display: grid;
    justify-items: end;
    gap: 4px;
    text-align: right;
}

.topbar-user span {
    font-weight: 700;
}

.topbar-user small {
    color: rgba(255, 255, 255, 0.72);
}

.topbar-user-link {
    color: rgba(255, 255, 255, 0.9);
    font-size: 0.88rem;
    font-weight: 700;
    text-decoration: underline;
    text-underline-offset: 3px;
}

.content-grid {
    display: grid;
    gap: 20px;
    min-width: 0;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 16px;
}

.stat-card {
    border-radius: var(--radius-lg);
    padding: 24px;
    box-shadow: var(--shadow-md);
}

.stat-label {
    display: inline-block;
    margin-bottom: 10px;
    color: var(--slate-400);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-size: 0.75rem;
}

.stat-card strong {
    display: block;
    font-family: "Barlow", sans-serif;
    font-size: 2.4rem;
    color: var(--navy-900);
}

.stat-card p {
    margin: 10px 0 0;
    color: var(--slate-400);
    line-height: 1.6;
}

.content-columns {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 20px;
    min-width: 0;
}

.panel {
    padding: 24px;
    min-width: 0;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 20px;
}

.panel-header > * {
    min-width: 0;
}

.action-grid,
.roadmap-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
}

.action-card,
.roadmap-card {
    border-radius: 18px;
    padding: 18px;
    background: linear-gradient(180deg, rgba(15, 30, 79, 0.04), rgba(15, 30, 79, 0.07));
    border: 1px solid rgba(21, 40, 97, 0.08);
}

.action-card strong,
.roadmap-card strong {
    display: block;
    margin-bottom: 8px;
    font-family: "Barlow", sans-serif;
    font-size: 1.2rem;
    color: var(--navy-900);
}

.action-card span,
.roadmap-card p {
    color: var(--slate-400);
    line-height: 1.65;
    margin: 0;
}

.recent-activity-cards {
    display: none;
}

.mobile-record-list {
    display: none;
}

.recent-doc-card {
    border-radius: 18px;
    padding: 18px;
    background: linear-gradient(180deg, rgba(15, 30, 79, 0.04), rgba(15, 30, 79, 0.07));
    border: 1px solid rgba(21, 40, 97, 0.08);
}

.recent-doc-top {
    display: flex;
    align-items: start;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 14px;
}

.recent-doc-top strong,
.recent-doc-item strong {
    color: var(--navy-900);
    overflow-wrap: anywhere;
}

.recent-doc-meta {
    display: grid;
    gap: 12px;
}

.recent-doc-item {
    display: grid;
    gap: 4px;
}

.recent-doc-item span {
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--slate-400);
}

.mobile-record-card {
    border-radius: 18px;
    padding: 18px;
    background: linear-gradient(180deg, rgba(15, 30, 79, 0.04), rgba(15, 30, 79, 0.07));
    border: 1px solid rgba(21, 40, 97, 0.08);
}

.mobile-record-top {
    display: flex;
    align-items: start;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 14px;
}

.mobile-record-title {
    color: var(--navy-900);
    font-family: "Barlow", sans-serif;
    font-size: 1.15rem;
    overflow-wrap: anywhere;
}

.mobile-record-meta {
    display: grid;
    gap: 12px;
}

.mobile-record-item {
    display: grid;
    gap: 4px;
}

.mobile-record-item span {
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--slate-400);
}

.mobile-record-item strong {
    overflow-wrap: anywhere;
}

.mobile-record-actions {
    display: grid;
    gap: 10px;
    margin-top: 16px;
}

.table-wrap {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    min-width: 760px;
}

.data-table th,
.data-table td {
    padding: 14px 16px;
    border-bottom: 1px solid var(--slate-200);
    text-align: left;
    font-size: 0.95rem;
}

.data-table th {
    color: var(--navy-900);
    font-family: "Barlow", sans-serif;
    font-size: 0.96rem;
}

.status-pill {
    display: inline-flex;
    align-items: center;
    min-height: 34px;
    padding: 0 12px;
    border-radius: 999px;
    background: rgba(227, 59, 46, 0.1);
    color: var(--red-600);
    font-size: 0.84rem;
    font-weight: 700;
}

/* Semantic status colors so proposals/invoices read at a glance. */
.status-pill.status-draft { background: rgba(109, 119, 140, 0.14); color: #4a5568; }
.status-pill.status-sent { background: rgba(37, 99, 235, 0.12); color: #1d4ed8; }
.status-pill.status-approved { background: rgba(22, 163, 74, 0.14); color: #15803d; }
.status-pill.status-paid { background: rgba(13, 148, 136, 0.16); color: #0f766e; }
.status-pill.status-cancelled { background: rgba(227, 59, 46, 0.1); color: var(--red-600); }
.status-pill.status-converted { background: rgba(124, 58, 237, 0.12); color: #6d28d9; }

.empty-state {
    text-align: center;
    color: var(--slate-400);
}

.header-actions,
.table-actions,
.filter-actions,
.inline-form {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.inline-form {
    margin: 0;
}

.filter-bar {
    display: grid;
    grid-template-columns: 2fr repeat(2, minmax(180px, 1fr)) auto;
    gap: 14px;
    margin-bottom: 18px;
}

.filter-bar > * {
    min-width: 0;
}

.table-subtext {
    margin-top: 4px;
    color: var(--slate-400);
    font-size: 0.84rem;
    overflow-wrap: anywhere;
}

.button-small {
    min-height: 38px;
    padding: 10px 14px;
    font-size: 0.88rem;
}

.button,
.nav-link,
.modal-close {
    touch-action: manipulation;
}

.status-pill-muted {
    background: rgba(109, 119, 140, 0.12);
    color: var(--slate-400);
}

.form-panel {
    display: grid;
    gap: 24px;
    min-width: 0;
}

.form-grid {
    display: grid;
    gap: 16px;
}

.field {
    min-width: 0;
}

.two-columns {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.three-columns {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.compact-grid {
    align-items: end;
}

.field-full {
    grid-column: 1 / -1;
}

.field select,
.field textarea {
    width: 100%;
    border: 1px solid var(--slate-200);
    border-radius: 14px;
    padding: 14px 16px;
    font: inherit;
    font-size: 16px;
    color: var(--slate-700);
    background: var(--white);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.inline-field-action {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 10px;
    align-items: center;
}

.inline-field-action > * {
    min-width: 0;
}

.field textarea {
    resize: vertical;
}

.field-help {
    min-height: 52px;
    padding: 14px 16px;
    border-radius: 14px;
    background: rgba(15, 30, 79, 0.05);
    color: var(--slate-400);
    line-height: 1.6;
}

.field select:focus,
.field textarea:focus {
    outline: none;
    border-color: rgba(227, 59, 46, 0.55);
    box-shadow: 0 0 0 4px rgba(227, 59, 46, 0.12);
}

.field-checkbox {
    justify-content: center;
}

.field-checkbox input {
    width: 22px;
    height: 22px;
    margin: 0;
}

.work-type-card {
    display: grid;
    gap: 12px;
    padding: 16px;
    border: 1px solid var(--slate-200);
    border-radius: 16px;
    background: rgba(15, 30, 79, 0.03);
}

.work-type-options {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.work-type-option {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    min-height: 44px;
    padding: 0 14px;
    border: 1px solid rgba(15, 30, 79, 0.14);
    border-radius: 999px;
    background: #ffffff;
    color: var(--navy-900);
    font-weight: 700;
}

.work-type-option input {
    width: 18px;
    height: 18px;
    margin: 0;
}

.document-shell {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 320px;
    gap: 20px;
    min-width: 0;
}

.document-panel {
    overflow: hidden;
    min-width: 0;
}

.panel-section {
    display: grid;
    gap: 16px;
}

.section-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

.section-head h3 {
    margin: 0;
    font-family: "Barlow", sans-serif;
    color: var(--navy-900);
    font-size: 1.35rem;
}

.line-items-table .table-input,
.line-items-table select {
    width: 100%;
    min-width: 100px;
    padding: 10px 12px;
    border: 1px solid var(--slate-200);
    border-radius: 12px;
    font: inherit;
    color: var(--slate-700);
    background: var(--white);
}

.line-items-table textarea.table-input {
    min-height: calc(5.6em + 22px); /* ~4 lines by default; auto-grow expands beyond */
    line-height: 1.4;
    resize: vertical;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.line-total {
    display: inline-flex;
    align-items: center;
    min-height: 42px;
    font-weight: 700;
    color: var(--navy-900);
}

.totals-layout {
    align-items: start;
}

.total-card,
.side-note {
    border-radius: 18px;
    background: linear-gradient(180deg, rgba(15, 30, 79, 0.04), rgba(15, 30, 79, 0.07));
    border: 1px solid rgba(21, 40, 97, 0.08);
    padding: 18px;
}

.summary-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px solid rgba(21, 40, 97, 0.08);
}

.summary-row strong {
    font-family: "Barlow", sans-serif;
    font-size: 1.12rem;
    color: var(--navy-900);
}

.summary-total {
    border-bottom: 0;
}

.summary-total strong {
    font-size: 1.8rem;
}

.side-panel {
    display: grid;
    align-content: start;
    gap: 18px;
    min-width: 0;
}

.stack-form {
    display: grid;
    gap: 14px;
}

.side-note p {
    margin: 8px 0 0;
    color: var(--slate-400);
    line-height: 1.6;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 80;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: rgba(15, 30, 79, 0.52);
    backdrop-filter: blur(6px);
}

.modal-backdrop.is-hidden {
    display: none;
}

.save-prompt-card {
    width: min(440px, calc(100vw - 40px));
}

.save-prompt-lead {
    margin: 6px 0 22px;
    color: var(--slate-400);
    line-height: 1.5;
}

.modal-card {
    width: min(860px, calc(100vw - 40px));
    max-width: 100%;
    max-height: calc(100vh - 40px);
    overflow: auto;
    overflow-x: hidden;
    border-radius: 24px;
    background: var(--white);
    box-shadow: var(--shadow-lg);
    padding: 24px;
    min-width: 0;
    overscroll-behavior: contain;
}

.modal-card > *,
.modal-card form,
.modal-card .form-grid,
.modal-header > *,
.modal-actions > * {
    min-width: 0;
}

.modal-card input,
.modal-card select,
.modal-card textarea {
    font-size: 16px;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: start;
    gap: 16px;
    margin-bottom: 18px;
}

.modal-header h3 {
    margin: 0;
    font-family: "Barlow", sans-serif;
    color: var(--navy-900);
    font-size: 1.5rem;
    overflow-wrap: anywhere;
}

.modal-close {
    border: 0;
    background: transparent;
    color: var(--slate-400);
    font-size: 2rem;
    line-height: 1;
    cursor: pointer;
    flex: 0 0 auto;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
}

.modal-feedback {
    border-radius: 14px;
    padding: 12px 14px;
    font-weight: 600;
}

.modal-feedback.is-hidden {
    display: none;
}

.modal-feedback.is-error {
    background: #ffebea;
    color: #8a2118;
}

.modal-feedback.is-success {
    background: #e7f7ea;
    color: #17603a;
}

/* ===== Mobile navigation (hidden on desktop; activated <= 1080px) ===== */
.mobile-topbar {
    display: none;
}

.nav-overlay {
    display: none;
}

.hamburger {
    display: inline-flex;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
    width: 44px;
    height: 44px;
    padding: 0 9px;
    border: 0;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.14);
    cursor: pointer;
    flex: 0 0 auto;
}

.hamburger span {
    display: block;
    height: 2.5px;
    width: 100%;
    border-radius: 2px;
    background: #fff;
    transition: transform 0.25s ease, opacity 0.2s ease;
}

@media (max-width: 1280px) {
    .data-table {
        min-width: 680px;
    }

    .line-items-table {
        min-width: 980px;
    }
}

@media (max-width: 1440px) {
    .login-panel,
    .app-frame,
    .stats-grid {
        grid-template-columns: 1fr;
    }

    .sidebar {
        gap: 20px;
    }

    .document-shell,
    .two-columns,
    .three-columns,
    .filter-bar,
    .inline-field-action {
        grid-template-columns: 1fr;
    }

    .sidebar-nav {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .stats-grid,
    .action-grid,
    .roadmap-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .filter-bar {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .filter-actions {
        grid-column: 1 / -1;
        justify-content: flex-end;
    }
}

@media (max-width: 820px) {
    .guest-shell,
    .app-shell {
        padding: 14px;
    }

    .login-brand,
    .auth-card,
    .topbar,
    .panel,
    .sidebar,
    .stat-card {
        border-radius: 20px;
    }

    .login-brand,
    .auth-card,
    .panel,
    .sidebar,
    .stat-card {
        padding: 20px;
    }

    .topbar {
        align-items: flex-start;
        flex-direction: column;
        gap: 12px;
        padding: 20px;
    }

    .topbar-user {
        width: 100%;
        justify-items: start;
        text-align: left;
    }

    .brand {
        align-items: flex-start;
    }

    .sidebar-nav {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .nav-link {
        justify-content: center;
        text-align: center;
    }

    .panel-header,
    .section-head,
    .modal-header,
    .modal-actions,
    .header-actions,
    .table-actions,
    .filter-actions,
    .inline-form {
        flex-direction: column;
        align-items: stretch;
    }

    .header-actions .button,
    .table-actions .button,
    .filter-actions .button,
    .inline-form .button,
    .modal-actions .button {
        width: 100%;
    }

    .header-actions,
    .table-actions {
        flex-wrap: wrap;
    }

    .panel-header {
        margin-bottom: 16px;
    }

    .filter-bar,
    .inline-field-action {
        grid-template-columns: 1fr;
    }

    .filter-bar {
        gap: 12px;
    }

    .form-grid,
    .content-grid,
    .main-panel,
    .content-columns {
        gap: 16px;
    }

    .field input,
    .field select,
    .field textarea {
        font-size: 16px;
    }

    .recent-activity-table {
        display: none;
    }

    .recent-activity-cards {
        display: grid;
        gap: 14px;
    }

    .desktop-table-only {
        display: none;
    }

    .mobile-record-list {
        display: grid;
        gap: 14px;
    }

    .work-type-options {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .work-type-option {
        width: 100%;
        justify-content: center;
    }

    .table-wrap {
        margin: 0 -20px;
        padding: 0 20px 6px;
    }

    .line-items-wrap {
        margin: 0;
        padding: 0;
        overflow: visible;
    }

    .document-shell,
    .document-panel,
    .side-panel,
    .panel,
    .main-panel,
    .content-grid,
    .content-columns,
    .form-panel {
        width: 100%;
        min-width: 0;
    }

    .table-wrap-stackable {
        margin: 0;
        padding: 0;
        overflow: visible;
    }

    .table-wrap-stackable .data-table {
        min-width: 0;
        border-collapse: separate;
    }

    .table-wrap-stackable .data-table,
    .table-wrap-stackable .data-table tbody,
    .table-wrap-stackable .data-table tr,
    .table-wrap-stackable .data-table td {
        display: block;
        width: 100%;
    }

    .table-wrap-stackable .data-table thead {
        display: none;
    }

    .table-wrap-stackable .data-table tbody {
        display: grid;
        gap: 14px;
    }

    .table-wrap-stackable .data-table tr {
        padding: 16px;
        border: 1px solid rgba(21, 40, 97, 0.1);
        border-radius: 18px;
        background: linear-gradient(180deg, rgba(15, 30, 79, 0.03), rgba(15, 30, 79, 0.06));
    }

    .table-wrap-stackable .data-table td {
        display: grid;
        grid-template-columns: minmax(88px, 108px) minmax(0, 1fr);
        gap: 10px;
        align-items: start;
        padding: 10px 0;
        border-bottom: 1px solid rgba(21, 40, 97, 0.08);
        white-space: normal;
    }

    .table-wrap-stackable .data-table td::before {
        content: attr(data-label);
        font-family: "Barlow", sans-serif;
        font-size: 0.84rem;
        font-weight: 700;
        color: var(--navy-900);
    }

    .table-wrap-stackable .data-table td:last-child {
        border-bottom: 0;
        padding-bottom: 0;
    }

    .table-wrap-stackable .data-table td.empty-state {
        display: block;
        padding: 0;
        border-bottom: 0;
    }

    .table-wrap-stackable .data-table td.empty-state::before {
        content: none;
    }

    .data-table {
        min-width: 640px;
    }

    .line-items-table {
        min-width: 0;
        border-collapse: separate;
    }

    .line-items-table,
    .line-items-table tbody,
    .line-items-table tr,
    .line-items-table td {
        display: block;
        width: 100%;
    }

    .line-items-table thead {
        display: none;
    }

    .line-items-table tbody {
        display: grid;
        gap: 14px;
    }

    .line-items-table tr {
        padding: 16px;
        border: 1px solid rgba(21, 40, 97, 0.1);
        border-radius: 18px;
        background: linear-gradient(180deg, rgba(15, 30, 79, 0.03), rgba(15, 30, 79, 0.06));
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
        gap: 12px 10px;
        align-items: end;
    }

    .line-items-table td {
        display: grid;
        gap: 6px;
        padding: 0;
        border-bottom: 0;
        white-space: normal;
        min-width: 0;
    }

    /* Service and Description keep their own full-width line; the small
       number fields (Code/Qty/Unit/Unit Price) sit compactly side by side. */
    .line-items-table td[data-label="Service"],
    .line-items-table td[data-label="Description"],
    .line-items-table td[data-label="Line Total"],
    .line-items-table td[data-label="Actions"] {
        grid-column: 1 / -1;
    }
    .line-items-table td[data-label="Service"] { order: 1; }
    .line-items-table td[data-label="Description"] { order: 2; }
    .line-items-table td[data-label="Code"] { order: 3; }
    .line-items-table td[data-label="Qty"] { order: 4; }
    .line-items-table td[data-label="Unit"] { order: 5; }
    .line-items-table td[data-label="Unit Price"] { order: 6; }
    .line-items-table td[data-label="Line Total"] { order: 7; }
    .line-items-table td[data-label="Actions"] { order: 8; }

    .line-items-table td::before {
        content: attr(data-label);
        font-family: "Barlow", sans-serif;
        font-size: 0.84rem;
        font-weight: 700;
        color: var(--navy-900);
    }

    .line-items-table .table-input,
    .line-items-table select,
    .line-items-table .button-small {
        min-width: 0;
        width: 100%;
    }

    .line-items-table .line-total {
        min-height: 0;
        padding: 12px 14px;
        border-radius: 12px;
        border: 1px solid var(--slate-200);
        background: #ffffff;
    }

    .data-table th,
    .data-table td {
        padding: 12px 12px;
        white-space: nowrap;
    }

    .modal-backdrop {
        padding: 10px;
        align-items: flex-end;
    }

    .modal-card {
        width: calc(100vw - 20px);
        max-width: calc(100vw - 20px);
        max-height: min(88vh, 100%);
        border-radius: 22px 22px 0 0;
        padding: 20px;
    }
}

@media (max-width: 640px) {
    .guest-shell,
    .app-shell {
        padding: 10px;
    }

    .app-frame {
        gap: 12px;
        min-height: auto;
    }

    .sidebar,
    .panel,
    .topbar,
    .stat-card,
    .login-brand,
    .auth-card {
        padding: 16px;
        border-radius: 18px;
    }

    .brand {
        gap: 10px;
    }

    .brand img {
        width: 62px;
    }

    .brand strong {
        font-size: 1rem;
    }

    .topbar h1 {
        font-size: 1.95rem;
        line-height: 1;
    }

    .eyebrow {
        font-size: 0.72rem;
        letter-spacing: 0.14em;
    }

    .stats-grid,
    .action-grid,
    .roadmap-grid,
    .work-type-options {
        grid-template-columns: 1fr;
    }

    .sidebar-nav {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .recent-doc-card {
        padding: 16px;
        border-radius: 16px;
    }

    .recent-doc-top {
        flex-direction: column;
        align-items: stretch;
    }

    .mobile-record-card {
        padding: 16px;
        border-radius: 16px;
    }

    .mobile-record-top {
        flex-direction: column;
        align-items: stretch;
    }

    .nav-link,
    .button {
        min-height: 46px;
    }

    .button {
        padding: 13px 16px;
    }

    .table-wrap {
        margin: 0 -16px;
        padding: 0 16px 6px;
    }

    .line-items-wrap {
        margin: 0;
        padding: 0;
    }

    .table-wrap-stackable {
        margin: 0;
        padding: 0;
    }

    .table-wrap-stackable .data-table tr {
        padding: 14px;
        border-radius: 16px;
    }

    .table-wrap-stackable .data-table td {
        grid-template-columns: 1fr;
        gap: 6px;
    }

    .data-table {
        min-width: 580px;
    }

    .line-items-table {
        min-width: 0;
    }

    .line-items-table tr {
        padding: 14px;
        border-radius: 16px;
    }

    .modal-card {
        width: calc(100vw - 12px);
        max-width: calc(100vw - 12px);
        padding: 16px 16px calc(16px + env(safe-area-inset-bottom, 0px));
    }

    .flash {
        top: 10px;
        width: calc(100% - 16px);
    }
}

/* ===== Fixed header + hamburger drawer (phones & tablets) ===== */
@media (max-width: 1440px) {
    /* Make room for the fixed compact header */
    .app-shell {
        padding-top: calc(72px + env(safe-area-inset-top, 0px));
    }

    .mobile-topbar {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        z-index: 60;
        height: calc(58px + env(safe-area-inset-top, 0px));
        padding: env(safe-area-inset-top, 0px) 14px 0;
        background: linear-gradient(135deg, var(--navy-900), var(--navy-700));
        box-shadow: var(--shadow-md);
    }

    .mobile-topbar-brand {
        display: flex;
        align-items: center;
        gap: 10px;
        min-width: 0;
    }

    .mobile-topbar-brand img {
        width: 34px;
        height: 34px;
        object-fit: contain;
        background: #fff;
        border-radius: 8px;
        padding: 3px;
        flex: 0 0 auto;
    }

    .mobile-topbar-brand strong {
        color: #fff;
        font-family: "Barlow", sans-serif;
        font-size: 1.05rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* Sidebar becomes an off-canvas drawer */
    .app-frame {
        display: block;
    }

    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        z-index: 70;
        width: min(84vw, 320px);
        height: 100dvh;
        border-radius: 0;
        transform: translateX(-100%);
        transition: transform 0.28s ease;
        overflow-y: auto;
        padding: calc(22px + env(safe-area-inset-top, 0px)) 20px calc(22px + env(safe-area-inset-bottom, 0px));
        gap: 22px;
    }

    body.nav-open .sidebar {
        transform: translateX(0);
        box-shadow: 0 24px 70px rgba(0, 0, 0, 0.45);
    }

    /* Drawer nav = vertical list (override the desktop-ish 2-col grid) */
    .sidebar-nav {
        grid-template-columns: 1fr;
    }

    .nav-link {
        justify-content: flex-start;
        text-align: left;
    }

    /* Backdrop */
    .nav-overlay {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 65;
        background: rgba(9, 16, 40, 0.55);
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.28s ease, visibility 0.28s ease;
    }

    body.nav-open .nav-overlay {
        opacity: 1;
        visibility: visible;
    }

    body.nav-open {
        overflow: hidden;
    }

    /* Hamburger morphs into an X while open */
    body.nav-open .hamburger span:nth-child(1) {
        transform: translateY(7.5px) rotate(45deg);
    }

    body.nav-open .hamburger span:nth-child(2) {
        opacity: 0;
    }

    body.nav-open .hamburger span:nth-child(3) {
        transform: translateY(-7.5px) rotate(-45deg);
    }
}

/* ===== Document wizard (mobile/tablet steps; desktop stays the full form) ===== */
.wiz-stepper,
.wiz-actionbar,
.wiz-review { display: none; }

body[data-wiz] .wiz-stepper {
    display: flex;
    align-items: center;
    gap: 5px;
    margin-bottom: 20px;
    padding: 13px 15px;
    background: linear-gradient(135deg, var(--navy-900), var(--navy-700));
    border-radius: 16px;
    box-shadow: var(--shadow-md);
}
body[data-wiz] .wiz-node { flex: 1 1 0; min-width: 0; display: flex; flex-direction: column; gap: 7px; cursor: pointer; }
body[data-wiz] .wiz-bar { height: 5px; border-radius: 999px; background: rgba(255, 255, 255, 0.22); transition: background .25s; }
body[data-wiz] .wiz-node.is-done .wiz-bar { background: rgba(255, 255, 255, 0.7); }
body[data-wiz] .wiz-node.is-active .wiz-bar { background: var(--red-500); }
body[data-wiz] .wiz-lbl { font-size: 0.62rem; font-weight: 700; color: rgba(255, 255, 255, 0.6); display: flex; align-items: center; gap: 5px; white-space: nowrap; }
body[data-wiz] .wiz-node.is-active .wiz-lbl { color: #fff; }
body[data-wiz] .wiz-num { width: 16px; height: 16px; border-radius: 50%; background: rgba(255, 255, 255, 0.18); display: grid; place-items: center; font-size: 0.6rem; font-weight: 800; flex: 0 0 auto; }
body[data-wiz] .wiz-node.is-active .wiz-num { background: var(--red-600); }
body[data-wiz] .wiz-node.is-done .wiz-num { background: #1f9d63; }

body[data-wiz] .wiz-hide,
body[data-wiz] .document-panel .header-actions,
body[data-wiz] .side-panel { display: none; }

body[data-wiz][data-wiz-step="1"] [data-wstep]:not([data-wstep="1"]),
body[data-wiz][data-wiz-step="2"] [data-wstep]:not([data-wstep="2"]),
body[data-wiz][data-wiz-step="3"] [data-wstep]:not([data-wstep="3"]),
body[data-wiz][data-wiz-step="4"] [data-wstep]:not([data-wstep="4"]) { display: none !important; }

body[data-wiz][data-wiz-step="4"] .wiz-review { display: block; }

body[data-wiz] .document-panel { padding-bottom: 96px; backdrop-filter: none; }

body[data-wiz] .wiz-actionbar {
    display: flex;
    align-items: center;
    gap: 10px;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 55;
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px));
    background: var(--white);
    border-top: 1px solid var(--slate-200);
    box-shadow: 0 -8px 24px rgba(15, 30, 79, 0.08);
}
body[data-wiz] .wiz-actionbar .wiz-total { flex: 1 1 auto; }
body[data-wiz] .wiz-actionbar .wiz-total span { display: block; font-size: 0.62rem; letter-spacing: 0.1em; text-transform: uppercase; color: var(--slate-400); font-weight: 800; }
body[data-wiz] .wiz-actionbar .wiz-total strong { font-size: 1.15rem; color: var(--navy-900); font-variant-numeric: tabular-nums; }
body[data-wiz] .wiz-actionbar .button { width: auto; margin: 0; flex: 0 0 auto; }
body[data-wiz] .wiz-back[hidden] { visibility: hidden; }

/* Review (step 4) */
body[data-wiz] .wiz-rev-card { background: var(--white); border: 1.5px solid var(--slate-200); border-radius: 16px; padding: 14px 16px; margin-bottom: 14px; }
body[data-wiz] .wiz-rev-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
body[data-wiz] .wiz-rev-head span { font-size: 0.7rem; font-weight: 800; letter-spacing: 0.1em; text-transform: uppercase; color: var(--navy-700); }
body[data-wiz] .wiz-rev-edit { border: none; background: none; color: var(--red-600); font-weight: 800; font-size: 0.8rem; cursor: pointer; }
body[data-wiz] .wiz-rev-client { display: flex; align-items: center; gap: 12px; }
body[data-wiz] .wiz-rev-avatar { width: 44px; height: 44px; border-radius: 12px; background: var(--navy-900); color: #fff; display: grid; place-items: center; font-weight: 800; flex: 0 0 auto; }
body[data-wiz] .wiz-rev-client b { display: block; font-size: 1rem; color: var(--navy-900); }
body[data-wiz] .wiz-rev-client small { font-size: 0.85rem; color: var(--slate-400); }
body[data-wiz] .wiz-rev-row { display: flex; justify-content: space-between; gap: 14px; padding: 7px 0; font-size: 0.92rem; border-bottom: 1px dashed var(--slate-100); }
body[data-wiz] .wiz-rev-row:last-child { border-bottom: none; }
body[data-wiz] .wiz-rev-row .k { color: var(--slate-400); }
body[data-wiz] .wiz-rev-row .v { color: var(--navy-900); font-weight: 600; text-align: right; }
body[data-wiz] .wiz-rev-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
body[data-wiz] .wiz-rev-chip { font-size: 0.78rem; font-weight: 700; background: #fff5f4; color: var(--navy-900); border: 1px solid #f3cdc9; padding: 4px 10px; border-radius: 999px; }
body[data-wiz] .wiz-rev-item { padding: 9px 0; border-bottom: 1px dashed var(--slate-100); }
body[data-wiz] .wiz-rev-item:last-child { border-bottom: none; }
body[data-wiz] .wiz-rev-item .top { display: flex; justify-content: space-between; gap: 12px; }
body[data-wiz] .wiz-rev-item .desc { font-weight: 600; color: var(--navy-900); }
body[data-wiz] .wiz-rev-item .amt { font-weight: 700; font-variant-numeric: tabular-nums; color: var(--navy-900); }
body[data-wiz] .wiz-rev-item .meta { font-size: 0.82rem; color: var(--slate-400); margin-top: 2px; }
body[data-wiz] .wiz-rev-total { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; padding-top: 12px; border-top: 2px solid var(--slate-200); }
body[data-wiz] .wiz-rev-total span { font-size: 0.72rem; font-weight: 800; letter-spacing: 0.1em; text-transform: uppercase; color: var(--slate-400); }
body[data-wiz] .wiz-rev-total strong { font-size: 1.35rem; color: var(--navy-900); font-variant-numeric: tabular-nums; }

body[data-wiz] .wiz-msg {
    position: fixed;
    left: 16px;
    right: 16px;
    bottom: calc(88px + env(safe-area-inset-bottom, 0px));
    z-index: 56;
    background: var(--red-600);
    color: #fff;
    padding: 12px 14px;
    border-radius: 12px;
    font-weight: 700;
    font-size: 0.9rem;
    box-shadow: var(--shadow-md);
    opacity: 0;
    transform: translateY(8px);
    transition: opacity .25s ease, transform .25s ease;
    pointer-events: none;
}
body[data-wiz] .wiz-msg.show { opacity: 1; transform: none; }

/* ===== Sticky Save bar on the simple forms (phone / tablet portrait) =====
   Keeps the primary action in reach without scrolling back to the top.
   Scoped to .form-panel so the document builder (.document-panel + wizard) is untouched. */
@media (max-width: 820px) {
    .form-panel {
        padding-bottom: 88px;
        /* backdrop-filter creates a containing block that would break the fixed bar */
        backdrop-filter: none;
    }
    .form-panel .header-actions {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 55;
        flex-direction: row;
        align-items: center;
        gap: 10px;
        margin: 0;
        padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px));
        background: var(--white);
        border-top: 1px solid var(--slate-200);
        box-shadow: 0 -8px 24px rgba(15, 30, 79, 0.08);
    }
    .form-panel .header-actions .button {
        width: auto;
        margin: 0;
    }
    .form-panel .header-actions .button-primary {
        flex: 1 1 auto;
    }
    .form-panel .header-actions .button-secondary {
        flex: 0 0 auto;
    }
}

/* ===== Line items as cards on desktop/tablet-landscape (>820px) =====
   Fields inline on the top row, the Description textarea on its own full width row.
   The <=820px stacked layout is untouched. */
@media (min-width: 821px) {
    .line-items-table {
        min-width: 0;
        border-collapse: separate;
    }
    .line-items-table thead {
        display: none;
    }
    .line-items-table tbody {
        display: grid;
        gap: 14px;
    }
    .line-items-table tr {
        display: grid;
        /* Service ~25%; the rest share the row proportionally so nothing overlaps */
        grid-template-columns: minmax(0, 1.4fr) minmax(0, 0.85fr) minmax(0, 0.6fr) minmax(0, 0.8fr) minmax(0, 1.05fr) minmax(0, 1.05fr) auto;
        gap: 10px 12px;
        align-items: end;
        padding: 16px 18px;
        border: 1px solid rgba(21, 40, 97, 0.1);
        border-radius: 16px;
        background: linear-gradient(180deg, rgba(15, 30, 79, 0.03), rgba(15, 30, 79, 0.05));
    }
    /* Inputs have a base min-width:100px that overflowed the narrow columns
       and overlapped neighbors — let them shrink to their grid cell. */
    .line-items-table .table-input,
    .line-items-table select {
        min-width: 0;
    }
    .line-items-table td {
        display: grid;
        gap: 6px;
        min-width: 0;
        padding: 0;
        border: 0;
        white-space: normal;
    }
    .line-items-table td::before {
        content: attr(data-label);
        font-family: "Barlow", sans-serif;
        font-size: 0.74rem;
        font-weight: 700;
        color: var(--navy-900);
    }
    .line-items-table td[data-label="Description"] {
        order: 1;
        grid-column: 1 / -1;
    }
    .line-items-table td[data-label="Actions"] {
        align-self: end;
    }
    .line-items-table td[data-label="Actions"]::before {
        content: "";
    }
    .line-items-table .line-total {
        width: 100%;
    }
}
