:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --danger: #dc2626;
    --success: #16a34a;
    --warning: #d97706;
    --bg: #f4f6f8;
    --card: #ffffff;
    --text: #1f2937;
    --muted: #6b7280;
    --border: #e5e7eb;
    --sidebar-width: 260px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    font-size: 15px;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.container { max-width: 1200px; margin: 0 auto; padding: 0 20px; }

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 16px;
    border-radius: 8px;
    border: none;
    font-size: 0.95rem;
    cursor: pointer;
    background: var(--primary);
    color: #fff;
}
.btn:hover { background: var(--primary-dark); text-decoration: none; }
.btn-secondary { background: #fff; color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--bg); }
.btn-danger { background: var(--danger); }
.btn-sm { padding: 6px 12px; font-size: 0.85rem; }

/* Sidebar */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-width);
    height: 100vh;
    background: #111827;
    color: #d1d5db;
    overflow-y: auto;
    transform: translateX(-100%);
    transition: transform 0.2s;
    z-index: 1000;
}
.sidebar.open { transform: translateX(0); }
.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px;
    border-bottom: 1px solid #374151;
}
.logo { font-size: 1.25rem; font-weight: 700; color: #fff; }
.logo span { color: var(--primary); }
.sidebar-close {
    background: transparent;
    border: none;
    color: #fff;
    font-size: 1.5rem;
    cursor: pointer;
    display: block;
}
.nav-list { list-style: none; padding: 12px 0; }
.nav-list li a {
    display: block;
    padding: 10px 20px;
    color: #d1d5db;
}
.nav-list li a:hover { background: #1f2937; color: #fff; text-decoration: none; }
.nav-section {
    padding: 12px 20px 6px;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #9ca3af;
    font-weight: 600;
}

/* Submenu navigation */
.nav-item.has-submenu {
    margin: 4px 0;
}

.nav-link {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 20px;
    color: #d1d5db;
    cursor: pointer;
}

.nav-link:hover {
    background: #1f2937;
    color: #fff;
    text-decoration: none;
}

.submenu-arrow {
    font-size: 0.7rem;
    transition: transform 0.2s ease;
}

.submenu-arrow.rotated {
    transform: rotate(180deg);
}

.submenu {
    display: none;
    list-style: none;
    padding: 0;
    background: #1f2937;
}

.submenu.open {
    display: block;
}

.submenu li a {
    display: block;
    padding: 8px 20px 8px 32px;
    color: #9ca3af;
    font-size: 0.9rem;
}

.submenu li a:hover {
    background: #374151;
    color: #fff;
    text-decoration: none;
}

/* Main layout */
.main { margin-left: 0; min-height: 100vh; }
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--card);
    padding: 12px 20px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
}
.menu-toggle {
    background: transparent;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
}
.company-name { font-weight: 600; }
.user-menu { display: flex; align-items: center; gap: 12px; }
.content { padding: 24px 20px; max-width: 1200px; margin: 0 auto; }

/* Cards */
.card {
    background: var(--card);
    border-radius: 12px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.04);
    padding: 24px;
    margin-bottom: 20px;
}
.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 18px;
}
.card-title { font-size: 1.15rem; font-weight: 600; }

/* Tables */
.table-responsive { overflow-x: auto; }
table {
    width: 100%;
    border-collapse: collapse;
    background: var(--card);
    border-radius: 8px;
    overflow: hidden;
}
th, td {
    padding: 12px 16px;
    text-align: left;
    border-bottom: 1px solid var(--border);
}
th {
    background: var(--bg);
    font-weight: 600;
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--muted);
}
tr:last-child td { border-bottom: none; }

/* Forms */
.form-group { margin-bottom: 18px; }
.form-row { display: grid; grid-template-columns: 1fr; gap: 18px; }
@media (min-width: 640px) {
    .form-row { grid-template-columns: repeat(2, 1fr); }
}
label {
    display: block;
    font-weight: 500;
    margin-bottom: 6px;
    font-size: 0.9rem;
}
input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="date"],
input[type="tel"],
select,
textarea {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.95rem;
    font-family: inherit;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}
.is-invalid { border-color: var(--danger); }
.invalid-feedback { color: var(--danger); font-size: 0.85rem; margin-top: 4px; }

/* Public pages */
.public { background: #fff; }
.public-nav { border-bottom: 1px solid var(--border); }
.public-nav .container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
}
.public-nav .nav-links { display: flex; gap: 16px; align-items: center; }
.public-content { min-height: calc(100vh - 120px); }
.public-footer { border-top: 1px solid var(--border); padding: 24px 0; text-align: center; color: var(--muted); }

.hero {
    text-align: center;
    padding: 80px 20px;
    background: linear-gradient(180deg, #eff6ff 0%, #fff 100%);
}
.hero h1 { font-size: 2.25rem; margin-bottom: 16px; }
.hero p { font-size: 1.1rem; color: var(--muted); max-width: 600px; margin: 0 auto 28px; }

.auth-card { max-width: 420px; margin: 60px auto; }

/* Dashboard widgets */
.widgets {
    display: grid;
    grid-template-columns: repeat(1, 1fr);
    gap: 16px;
    margin-bottom: 24px;
}
@media (min-width: 640px) { .widgets { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 1024px) { .widgets { grid-template-columns: repeat(4, 1fr); } }
.widget {
    background: var(--card);
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.04);
}
.widget-label { font-size: 0.85rem; color: var(--muted); margin-bottom: 6px; }
.widget-value { font-size: 1.5rem; font-weight: 700; }
.widget-sub { font-size: 0.8rem; color: var(--muted); margin-top: 6px; }
.widget-alert { border-left: 4px solid var(--danger); }
.widget-alert .widget-value { color: var(--danger); }
.widget-positive { border-left: 4px solid var(--success); }
.widget-positive .widget-value { color: var(--success); }

/* Dashboard header */
.dash-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 16px;
    margin-bottom: 24px;
}
.dash-title { font-size: 1.5rem; font-weight: 700; margin-bottom: 4px; }

/* Alerts */
.alert {
    padding: 12px 16px;
    border-radius: 8px;
    margin-bottom: 20px;
}
.alert-success { background: #d1fae5; color: #065f46; }
.alert-danger { background: #fee2e2; color: #991b1b; }
.alert-warning { background: #fef3c7; color: #92400e; }
.alert-info { background: #dbeafe; color: #1e40af; }

/* Status badges */
.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 0.8rem;
    font-weight: 500;
}
.badge-draft { background: #f3f4f6; color: #4b5563; }
.badge-sent { background: #dbeafe; color: #1e40af; }
.badge-paid { background: #d1fae5; color: #065f46; }
.badge-partial { background: #fef3c7; color: #92400e; }
.badge-overdue { background: #fee2e2; color: #991b1b; }
.badge-active { background: #d1fae5; color: #065f46; }
.badge-suspended { background: #fee2e2; color: #991b1b; }
.badge-trial { background: #fef3c7; color: #92400e; }
.badge-trialing { background: #fef3c7; color: #92400e; }
.badge-inactive { background: #f3f4f6; color: #4b5563; }
.badge-cancelled { background: #fee2e2; color: #991b1b; }
.badge-past_due { background: #fee2e2; color: #991b1b; }
.badge-submitted { background: #dbeafe; color: #1e40af; }
.badge-approved { background: #d1fae5; color: #065f46; }
.badge-rejected { background: #fee2e2; color: #991b1b; }
.badge-accepted { background: #d1fae5; color: #065f46; }
.badge-declined { background: #fee2e2; color: #991b1b; }
.badge-expired { background: #f3f4f6; color: #4b5563; }
.badge-converted { background: #ede9fe; color: #5b21b6; }
.badge-confirmed { background: #dbeafe; color: #1e40af; }
.badge-shipped { background: #e0f2fe; color: #075985; }
.badge-delivered { background: #d1fae5; color: #065f46; }
.badge-invoiced { background: #ede9fe; color: #5b21b6; }
.badge-received { background: #d1fae5; color: #065f46; }
.badge-closed { background: #f3f4f6; color: #4b5563; }
.badge-write_off { background: #f3f4f6; color: #4b5563; }

/* Utility */
.text-right { text-align: right; }
.text-center { text-align: center; }
.text-muted { color: var(--muted); }
.mb-0 { margin-bottom: 0; }
.mb-2 { margin-bottom: 12px; }
.mt-2 { margin-top: 12px; }
.flex { display: flex; }
.justify-between { justify-content: space-between; }
.gap-2 { gap: 12px; }

@media (min-width: 1024px) {
    .sidebar { transform: translateX(0); }
    .main { margin-left: var(--sidebar-width); }
    .menu-toggle, .sidebar-close { display: none; }
}

/* Print */
@media print {
    .sidebar, .topbar, .menu-toggle, .sidebar-close { display: none; }
    .main { margin-left: 0; }
}

.document-page {
    max-width: 1080px;
    margin: 0 auto;
}
.document-heading {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 24px;
    margin-bottom: 28px;
}
.back-link {
    display: inline-flex;
    margin-bottom: 8px;
    color: var(--muted);
    font-size: 0.88rem;
    font-weight: 600;
}
.document-heading h1 {
    color: #111827;
    font-size: clamp(1.65rem, 3vw, 2.15rem);
    letter-spacing: -0.03em;
    line-height: 1.2;
}
.document-heading p {
    color: var(--muted);
    margin-top: 8px;
}
.document-badge {
    background: #eff6ff;
    border: 1px solid #bfdbfe;
    border-radius: 999px;
    color: #1d4ed8;
    font-size: 0.78rem;
    font-weight: 700;
    padding: 6px 12px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
}
.document-heading-side {
    align-items: flex-end;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.document-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}
.card-header .document-actions {
    display: inline-flex;
}
.statement-summary {
    margin: 22px 0 0 auto;
    max-width: 280px;
}
.btn-disabled {
    cursor: not-allowed;
    opacity: 0.5;
    pointer-events: none;
}
.document-form {
    display: grid;
    gap: 20px;
}
.form-section {
    background: var(--card);
    border: 1px solid #e7ebf2;
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.04);
    padding: clamp(20px, 3vw, 30px);
}
.section-heading {
    margin-bottom: 24px;
}
.section-heading-with-action {
    align-items: flex-start;
    display: flex;
    justify-content: space-between;
    gap: 18px;
}
.section-kicker {
    color: var(--primary);
    display: block;
    font-size: 0.73rem;
    font-weight: 800;
    letter-spacing: 0.1em;
    margin-bottom: 5px;
    text-transform: uppercase;
}
.section-heading h2 {
    color: #111827;
    font-size: 1.15rem;
    letter-spacing: -0.015em;
}
.section-heading p {
    color: var(--muted);
    font-size: 0.9rem;
    margin-top: 4px;
}
.form-grid {
    display: grid;
    gap: 18px;
    grid-template-columns: repeat(2, minmax(0, 1fr));
}
.form-grid-primary {
    grid-template-columns: minmax(0, 1.5fr) minmax(150px, 0.5fr);
}
.form-grid-dates {
    margin-top: 2px;
}
.form-group-wide {
    grid-column: 1 / -1;
}
.document-form .form-group {
    margin-bottom: 0;
}
.document-form label {
    color: #374151;
    font-size: 0.8rem;
    font-weight: 700;
    margin-bottom: 7px;
}
.required-mark {
    color: var(--danger);
}
.field-help {
    color: #8a94a6;
    display: block;
    font-size: 0.78rem;
    line-height: 1.45;
    margin-top: 6px;
}
.document-form input,
.document-form select,
.document-form textarea {
    background: #fff;
    border-color: #d9e0ea;
    min-height: 44px;
    transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}
.document-form textarea {
    min-height: 92px;
    resize: vertical;
}
.document-form input:hover,
.document-form select:hover,
.document-form textarea:hover {
    border-color: #b9c5d5;
}
.document-form input:focus,
.document-form select:focus,
.document-form textarea:focus {
    background: #fff;
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.11);
}
.line-items-section {
    padding-bottom: 22px;
}
.line-items-table {
    border: 1px solid #e4e9f0;
    border-radius: 12px;
    overflow: hidden;
}
.line-items-header,
.line-item {
    display: grid;
    grid-template-columns: minmax(250px, 2.2fr) 78px 120px 145px 105px 34px;
    gap: 12px;
}
.line-items-header {
    align-items: center;
    background: #f7f9fc;
    border-bottom: 1px solid #e4e9f0;
    color: #7a8494;
    font-size: 0.7rem;
    font-weight: 800;
    letter-spacing: 0.07em;
    padding: 11px 14px;
    text-transform: uppercase;
}
.line-items-header span:nth-child(n+2) {
    text-align: right;
}
.line-item {
    align-items: end;
    background: #fff;
    border-bottom: 1px solid #eef1f5;
    padding: 16px 14px;
}
.line-item:last-child {
    border-bottom: 0;
}
.line-item-main {
    display: grid;
    gap: 10px;
    grid-template-columns: minmax(115px, 0.9fr) minmax(120px, 1.1fr);
}
.line-item-fields {
    display: contents;
}
.line-item .form-group {
    min-width: 0;
}
.line-item .form-group label {
    display: none;
}
.line-item input,
.line-item select {
    font-size: 0.86rem;
    min-height: 40px;
    padding: 8px 9px;
}
.line-total {
    align-self: center;
    min-width: 0;
    text-align: right;
}
.line-total-label {
    display: none;
}
.line-total-value {
    color: #172033;
    font-size: 0.9rem;
    white-space: nowrap;
}
.line-remove {
    align-items: center;
    align-self: center;
    background: transparent;
    border: 0;
    border-radius: 7px;
    color: #9aa4b2;
    cursor: pointer;
    display: inline-flex;
    font-size: 1.45rem;
    height: 32px;
    justify-content: center;
    line-height: 1;
    width: 32px;
}
.line-remove:hover:not(:disabled) {
    background: #fef2f2;
    color: var(--danger);
}
.line-remove:disabled {
    cursor: not-allowed;
    opacity: 0.35;
}
.line-items-footer {
    align-items: flex-start;
    display: flex;
    justify-content: space-between;
    gap: 24px;
    margin-top: 18px;
}
.line-items-footer > .field-help {
    max-width: 360px;
}
.document-summary {
    color: #667085;
    min-width: 250px;
}
.document-summary > div {
    display: flex;
    font-size: 0.84rem;
    justify-content: space-between;
    gap: 32px;
    padding: 4px 0;
}
.document-summary strong {
    color: #344054;
    font-weight: 700;
}
.document-summary .summary-total {
    border-top: 1px solid #e4e9f0;
    color: #111827;
    font-size: 1rem;
    margin-top: 6px;
    padding-top: 10px;
}
.document-summary .summary-total strong {
    color: var(--primary-dark);
    font-size: 1.12rem;
}
.form-actions {
    align-items: center;
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    padding: 4px 0 18px;
}
.document-form .btn {
    min-height: 44px;
    padding-left: 18px;
    padding-right: 18px;
}
.document-form .btn-secondary {
    background: #fff;
}

.form-page {
    max-width: 1080px;
    margin: 0 auto;
}
.form-page-header {
    align-items: flex-start;
    display: flex;
    justify-content: space-between;
    gap: 24px;
    margin-bottom: 28px;
}
.form-page-header h1 {
    color: #111827;
    font-size: clamp(1.65rem, 3vw, 2.15rem);
    letter-spacing: -0.03em;
    line-height: 1.2;
}
.form-page-header p {
    color: var(--muted);
    margin-top: 8px;
}
.form-page-form {
    display: grid;
    gap: 20px;
}
.form-page-form .form-section {
    margin: 0;
}
.form-page-form .form-row:not(.line-item) {
    display: grid;
    gap: 18px;
    grid-template-columns: repeat(2, minmax(0, 1fr));
}
.form-page-form .form-group {
    margin-bottom: 0;
}
.form-page-form label {
    color: #374151;
    font-size: 0.8rem;
    font-weight: 700;
    margin-bottom: 7px;
}
.form-page-form input,
.form-page-form select,
.form-page-form textarea {
    background: #fff;
    border-color: #d9e0ea;
    min-height: 44px;
    transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}
.form-page-form textarea {
    min-height: 92px;
    resize: vertical;
}
.form-page-form input:hover,
.form-page-form select:hover,
.form-page-form textarea:hover {
    border-color: #b9c5d5;
}
.form-page-form input:focus,
.form-page-form select:focus,
.form-page-form textarea:focus {
    background: #fff;
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.11);
}
.form-page-form input[type="checkbox"] {
    min-height: auto;
    width: auto;
}
.form-page-form .field-help {
    color: #8a94a6;
    display: block;
    font-size: 0.78rem;
    line-height: 1.45;
    margin-top: 6px;
}
.form-page-form .form-group-wide {
    grid-column: 1 / -1;
}
.form-page-form .form-actions {
    padding: 4px 0 18px;
}
.form-page-form .form-actions .btn {
    min-height: 44px;
    padding-left: 18px;
    padding-right: 18px;
}
.form-page-form .form-actions .btn-secondary {
    background: #fff;
}
.form-checkboxes {
    display: flex;
    flex-wrap: wrap;
    gap: 12px 24px;
}
.form-checkbox {
    align-items: center;
    background: #f7f9fc;
    border: 1px solid #e4e9f0;
    border-radius: 10px;
    color: #374151;
    display: inline-flex;
    gap: 9px;
    min-height: 44px;
    padding: 8px 12px;
}
.form-checkbox label {
    margin: 0;
}
.form-preview {
    align-items: center;
    background: #f7f9fc;
    border: 1px solid #e4e9f0;
    border-radius: 10px;
    display: inline-flex;
    margin-top: 10px;
    min-height: 84px;
    padding: 10px;
}
.form-preview img {
    max-height: 60px;
    max-width: 180px;
}

@media (max-width: 900px) {
    .line-items-header {
        display: none;
    }
    .line-item {
        display: grid;
        gap: 14px;
        grid-template-columns: 1fr 32px;
    }
    .line-item-main {
        grid-column: 1 / -1;
    }
    .line-item-fields {
        align-items: end;
        display: grid;
        gap: 10px;
        grid-column: 1 / -1;
        grid-template-columns: repeat(3, minmax(0, 1fr)) 100px;
    }
    .line-item .form-group label,
    .line-total-label {
        display: block;
    }
    .line-total {
        align-self: end;
        text-align: left;
    }
    .line-remove {
        grid-column: 2;
        grid-row: 1;
    }
}

@media (max-width: 640px) {
    .document-heading,
    .section-heading-with-action,
    .line-items-footer {
        align-items: stretch;
        flex-direction: column;
    }
    .document-heading-side {
        align-items: flex-start;
    }
    .document-badge {
        align-self: flex-start;
    }
    .form-grid,
    .form-grid-primary {
        grid-template-columns: 1fr;
    }
    .line-item-main,
    .line-item-fields {
        grid-template-columns: 1fr 1fr;
    }
    .line-item-main .form-group:first-child,
    .line-item-main .form-group:nth-child(2) {
        grid-column: 1 / -1;
    }
    .line-total {
        grid-column: 1;
    }
    .line-remove {
        grid-column: 2;
        grid-row: auto;
        justify-self: end;
    }
    .document-summary {
        min-width: 0;
        width: 100%;
    }
    .form-actions {
        flex-direction: column-reverse;
    }
    .form-actions .btn {
        width: 100%;
    }
    .form-page-header {
        flex-direction: column;
    }
    .form-page-form .form-row:not(.line-item) {
        grid-template-columns: 1fr;
    }
    .form-page-form .form-actions {
        flex-direction: column-reverse;
    }
}
