:root {
    --ink: #17231f;
    --green: #173f32;
    --green-2: #245c49;
    --olive: #86853c;
    --paper: #f7f9f6;
    --surface: #ffffff;
    --line: #dce3dc;
    --muted: #6d766f;
    --danger: #a33f32;
    --warning: #8a6418;
    --success: #216647;
    --shadow: 0 12px 34px rgba(20, 52, 42, .08);
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--paper); }
body {
    min-height: 100vh;
    margin: 0;
    color: var(--ink);
    background: var(--paper);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 15px;
    line-height: 1.5;
}
a { color: var(--green-2); text-underline-offset: 3px; }
button, input, select, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }

.app-shell { display: grid; min-height: 100vh; grid-template-columns: 248px minmax(0, 1fr); }
.sidebar {
    position: sticky;
    top: 0;
    display: flex;
    height: 100vh;
    flex-direction: column;
    padding: 22px 18px;
    border-right: 1px solid rgba(255, 255, 255, .08);
    background: var(--green);
    color: white;
}
.brand { display: block; padding: 4px 10px 28px; }
.brand img { display: block; width: 100%; height: 92px; object-fit: contain; filter: brightness(0) invert(1); }
.main-nav { display: grid; gap: 4px; }
.main-nav a {
    padding: 11px 13px;
    border-radius: 6px;
    color: rgba(255, 255, 255, .74);
    font-weight: 650;
    text-decoration: none;
}
.main-nav a:hover, .main-nav a.active { background: rgba(255, 255, 255, .11); color: white; }
.sidebar-foot { display: grid; gap: 8px; margin-top: auto; padding: 18px 12px 4px; border-top: 1px solid rgba(255, 255, 255, .12); }
.sidebar-foot span { overflow: hidden; color: rgba(255, 255, 255, .65); font-size: 12px; text-overflow: ellipsis; }
.sidebar .link-button { color: white; }
.main-column { min-width: 0; }
.mobile-header { display: none; }
.content { width: min(1180px, calc(100% - 64px)); margin: 0 auto; padding: 48px 0 70px; }

.page-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 24px; margin-bottom: 28px; }
.page-heading h1 { margin: 2px 0 0; color: var(--green); font-size: clamp(28px, 4vw, 42px); line-height: 1.08; }
.eyebrow { margin: 0; color: var(--olive); font-size: 11px; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }
.heading-actions, .form-actions { display: flex; flex-wrap: wrap; gap: 10px; }

.button {
    display: inline-flex;
    min-height: 42px;
    align-items: center;
    justify-content: center;
    padding: 9px 16px;
    border: 1px solid transparent;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 750;
    text-decoration: none;
}
.button-primary { border-color: var(--green); background: var(--green); color: white; }
.button-primary:hover { border-color: var(--green-2); background: var(--green-2); }
.button-secondary { border-color: var(--line); background: var(--surface); color: var(--green); }
.button-secondary:hover { border-color: #bdcbbf; background: #fbfcfa; }
.button-small { min-height: 34px; padding: 6px 11px; font-size: 13px; }
.button-full { width: 100%; }
.link-button { padding: 0; border: 0; background: transparent; color: var(--green-2); cursor: pointer; font-weight: 700; text-align: left; text-decoration: underline; text-underline-offset: 3px; }
.danger { color: var(--danger); }

.stat-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; margin-bottom: 24px; }
.stat { min-width: 0; padding: 20px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.stat span { display: block; margin-bottom: 10px; color: var(--muted); font-size: 12px; font-weight: 700; text-transform: uppercase; }
.stat strong { display: block; overflow: hidden; color: var(--green); font-size: 27px; line-height: 1.1; text-overflow: ellipsis; }
.stat-accent { border-color: var(--green); background: var(--green); }
.stat-accent span { color: rgba(255, 255, 255, .66); }
.stat-accent strong { color: white; }

.panel { margin-bottom: 18px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); box-shadow: var(--shadow); }
.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 18px; padding: 20px 22px; border-bottom: 1px solid var(--line); }
.panel-heading h2, .form-section h2, .action-panel h2 { margin: 0; color: var(--green); font-size: 18px; }
.table-wrap { width: 100%; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th { padding: 12px 16px; border-bottom: 1px solid var(--line); color: var(--muted); font-size: 11px; text-align: left; text-transform: uppercase; white-space: nowrap; }
td { padding: 15px 16px; border-bottom: 1px solid #edf0ec; vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fbfcfb; }
td small { display: block; margin-top: 3px; color: var(--muted); }
.num { text-align: right; white-space: nowrap; }
.strong-link { color: var(--green); font-weight: 800; text-decoration: none; }
.actions-cell { text-align: right; white-space: nowrap; }
.empty-state { padding: 56px 24px; text-align: center; }
.empty-state h3, .empty-state h1 { margin: 0 0 8px; color: var(--green); }
.empty-state p { margin: 0; color: var(--muted); }

.badge { display: inline-flex; padding: 4px 8px; border-radius: 999px; background: #edf1ed; color: #566059; font-size: 11px; font-weight: 800; white-space: nowrap; }
.badge-paid, .badge-accepted { background: #e4f2e9; color: var(--success); }
.badge-sent { background: #e8eff4; color: #315b73; }
.badge-cancelled { background: #f5e7e4; color: var(--danger); }
.badge-draft { background: #f2efe1; color: #786818; }

.alert { margin-bottom: 18px; padding: 13px 15px; border: 1px solid; border-radius: 6px; }
.alert-success { border-color: #b9dcc7; background: #eaf6ee; color: var(--success); }
.alert-error { border-color: #e2b8b1; background: #fbefed; color: var(--danger); }
.alert-warning { border-color: #dfd09f; background: #fbf6e6; color: var(--warning); }

.toolbar { display: flex; justify-content: space-between; margin-bottom: 16px; }
.search-form { display: flex; width: min(560px, 100%); gap: 8px; }
.search-form input { flex: 1; }
.tabs { display: flex; gap: 5px; margin: -8px 0 18px; }
.tabs a { padding: 8px 13px; border-radius: 6px; color: var(--muted); font-weight: 750; text-decoration: none; }
.tabs a.active { background: #e8eee9; color: var(--green); }

.form-layout, .document-form { display: grid; gap: 0; }
.form-section { padding: 22px; }
.form-section > h2 { margin-bottom: 18px; }
.section-intro { margin: -9px 0 20px; color: var(--muted); }
.field-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 17px; }
.span-2 { grid-column: 1 / -1; }
label { display: grid; gap: 7px; color: #34423b; font-size: 13px; font-weight: 700; }
input, select, textarea {
    width: 100%;
    border: 1px solid #cbd5cd;
    border-radius: 6px;
    outline: none;
    background: white;
    color: var(--ink);
}
input, select { min-height: 43px; padding: 9px 11px; }
textarea { min-height: 86px; padding: 10px 11px; resize: vertical; }
.optional { color: var(--muted); font-size: 12px; font-weight: 500; }
input:focus, select:focus, textarea:focus { border-color: var(--green-2); box-shadow: 0 0 0 3px rgba(36, 92, 73, .11); }
.form-actions { justify-content: flex-end; padding-top: 4px; }
.danger-zone { margin-top: 26px; padding-top: 20px; border-top: 1px solid var(--line); }

.line-items { display: grid; gap: 12px; padding: 20px 22px; }
.line-item { position: relative; display: grid; grid-template-columns: minmax(220px, 2fr) 100px 145px 130px 28px; gap: 10px; align-items: end; padding-bottom: 12px; border-bottom: 1px solid #edf0ec; }
.line-item:last-child { border-bottom: 0; }
.line-total { display: grid; gap: 7px; min-height: 43px; align-content: center; text-align: right; }
.line-total span { color: #34423b; font-size: 13px; font-weight: 700; }
.line-total strong { font-size: 14px; }
.remove-line { width: 28px; height: 43px; border: 0; background: transparent; color: var(--danger); cursor: pointer; font-size: 24px; }
.document-totals { display: flex; align-items: end; justify-content: flex-end; gap: 34px; padding: 18px 22px; border-top: 1px solid var(--line); background: #fafbf9; }
.document-totals label { width: 110px; }
.document-totals > div { display: grid; gap: 2px; text-align: right; }
.document-totals span { color: var(--muted); font-size: 12px; }
.document-totals strong { color: var(--green); font-size: 25px; }

.document-summary { display: grid; grid-template-columns: minmax(0, 1fr) 260px; gap: 18px; align-items: start; }
.summary-main { overflow: hidden; }
.summary-head { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 22px; border-bottom: 1px solid var(--line); }
.summary-head > div { display: grid; gap: 3px; }
.summary-head span, .document-meta dt { color: var(--muted); font-size: 11px; font-weight: 750; text-transform: uppercase; }
.summary-head strong { color: var(--green); font-size: 18px; }
.document-meta { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); margin: 0; padding: 18px 22px; border-bottom: 1px solid var(--line); }
.document-meta div { padding-right: 16px; }
.document-meta dt { margin-bottom: 4px; }
.document-meta dd { margin: 0; }
.view-totals { display: grid; width: min(360px, 100%); gap: 7px; margin: 20px 22px 22px auto; padding: 0 22px; }
.view-totals > div { display: flex; justify-content: space-between; gap: 20px; }
.view-totals .grand { margin-top: 4px; padding-top: 10px; border-top: 2px solid var(--olive); color: var(--green); font-size: 18px; }
.notes-block { margin: 0 22px 22px; padding: 14px; border-left: 3px solid var(--olive); background: #f3f6f2; }
.notes-block p { margin: 5px 0 0; }
.action-panel { padding: 20px; }
.action-panel h2 { margin-bottom: 10px; }
.action-panel p { color: var(--muted); font-size: 13px; }
.action-form { margin-top: 14px; }
.send-actions { display: grid; gap: 10px; }
.action-warning { margin: 3px 0 0; padding: 10px 11px; border-left: 3px solid var(--olive); background: #f5f6ed; color: #5f6043 !important; }
.action-link { color: var(--green); font-size: 13px; font-weight: 750; }
.action-divider { margin: 20px 0; border-top: 1px solid var(--line); }
.success-text { color: var(--success) !important; font-weight: 800; }

.login-page { display: grid; min-height: 100vh; place-items: center; padding: 28px; background: #edf2ee; }
.login-panel { width: min(430px, 100%); }
.login-logo { display: block; width: 250px; height: 130px; margin: 0 auto 20px; object-fit: contain; }
.login-card { padding: 30px; border: 1px solid var(--line); border-radius: 8px; background: white; box-shadow: var(--shadow); }
.login-card h1 { margin: 4px 0 24px; color: var(--green); font-size: 29px; }
.stack-form { display: grid; gap: 17px; }

@media (max-width: 980px) {
    .app-shell { grid-template-columns: 1fr; }
    .sidebar { position: fixed; z-index: 20; width: min(300px, 85vw); transform: translateX(-102%); transition: transform .2s ease; box-shadow: 18px 0 45px rgba(0, 0, 0, .18); }
    .menu-open .sidebar { transform: translateX(0); }
    .mobile-header { position: sticky; z-index: 15; top: 0; display: flex; height: 68px; align-items: center; justify-content: space-between; padding: 7px 20px; border-bottom: 1px solid var(--line); background: rgba(255, 255, 255, .96); }
    .mobile-brand img { display: block; width: 118px; height: 52px; object-fit: contain; }
    .menu-toggle { padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; background: white; color: var(--green); font-weight: 750; }
    .content { width: min(100% - 36px, 900px); padding-top: 32px; }
    .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .document-summary { grid-template-columns: 1fr; }
    .action-panel { order: -1; }
    .line-item { grid-template-columns: minmax(180px, 1fr) 90px 130px 115px 28px; }
}

@media (max-width: 720px) {
    .content { width: min(100% - 24px, 680px); padding: 24px 0 46px; }
    .page-heading { align-items: stretch; flex-direction: column; gap: 17px; }
    .page-heading h1 { font-size: 31px; }
    .heading-actions { display: grid; grid-template-columns: 1fr 1fr; }
    .heading-actions .button:only-child { grid-column: 1 / -1; }
    .stat-grid { gap: 10px; }
    .stat { padding: 16px; }
    .stat strong { font-size: 22px; }
    .field-grid { grid-template-columns: 1fr; }
    .span-2 { grid-column: auto; }
    .form-section { padding: 18px; }
    .search-form { display: grid; grid-template-columns: 1fr auto; }
    .line-item { grid-template-columns: 1fr 1fr 28px; padding: 15px; border: 1px solid var(--line); border-radius: 6px; }
    .item-description { grid-column: 1 / -1; }
    .line-total { grid-column: 1 / 3; text-align: left; }
    .remove-line { grid-column: 3; grid-row: 2; }
    .document-totals { align-items: stretch; flex-direction: column; }
    .document-totals label { width: 100%; }
    .document-totals > div { text-align: left; }
    .document-meta { grid-template-columns: 1fr; gap: 13px; }
    .panel-heading { align-items: flex-start; padding: 16px 18px; }
    th, td { padding: 12px; }
    .login-page { padding: 18px; }
    .login-card { padding: 23px; }
}

@media (max-width: 430px) {
    .heading-actions { grid-template-columns: 1fr; }
    .stat-grid { grid-template-columns: 1fr 1fr; }
    .stat span { font-size: 10px; }
    .tabs { overflow-x: auto; }
    .tabs a { white-space: nowrap; }
}
