:root {
    --bg: #f3efe7;
    --ink: #1c1915;
    --muted: #6d655c;
    --line: #e3dcd0;
    --card: #fffdfb;
    --green: #1f4636;
    --green-2: #2a5a46;
    --gold: #8d6a3a;
    --due: #8d2f2b;
    --credit: #1d4e79;
    --clear: #1f4636;
    --side: #17241e;
    --serif: Georgia, "Times New Roman", serif;
    --sans: "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    color: var(--ink);
    font-family: var(--sans);
    background:
        radial-gradient(circle at top left, rgba(255, 250, 240, 0.9), transparent 42%),
        var(--bg);
}

h1, h2, h3 { font-family: var(--serif); font-weight: 500; }
h2 { margin: 0; font-size: 1.35rem; }
h3 { margin: 0 0 8px; font-size: 0.95rem; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); }
p { margin: 0; }
a { color: var(--green); }

.app { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }

.side {
    background: var(--side);
    color: #f4efe6;
    padding: 22px 16px;
    display: flex;
    flex-direction: column;
    gap: 22px;
}

.brand { color: inherit; text-decoration: none; display: grid; gap: 4px; padding: 0 8px; }
.brand span { font-family: var(--serif); font-size: 1.7rem; }
.brand small { color: #c9c0b2; }

.side nav { display: flex; flex-direction: column; gap: 4px; }
.side nav a {
    color: #ddd6c8;
    text-decoration: none;
    padding: 10px 12px;
    border-radius: 10px;
}
.side nav a.active,
.side nav a:hover { background: #234536; color: white; }

.side form { margin-top: auto; }

.content { padding: 28px 32px 56px; display: grid; align-content: start; gap: 16px; }
.invoice-form { display: grid; gap: 16px; }

.page-head { display: flex; justify-content: space-between; gap: 16px; align-items: flex-end; }
.page-head h1 { margin: 0; font-size: 2.1rem; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.eyebrow {
    margin: 0 0 4px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-size: 0.72rem;
    color: var(--gold);
}

.button, button.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border: 0;
    border-radius: 10px;
    padding: 10px 14px;
    background: var(--green);
    color: white;
    text-decoration: none;
    font: inherit;
    cursor: pointer;
}
.button:hover { background: var(--green-2); }
.button.ghost { background: white; color: var(--green); border: 1px solid #c9d5ce; }
.button.ghost:hover { background: #f3f7f4; }
.side .button.ghost { width: 100%; color: #f4efe6; background: transparent; border-color: #4d6156; }
.side .button.ghost:hover { background: #234536; }
button:disabled { opacity: 0.65; cursor: wait; }

.card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 16px;
    padding: 18px;
}
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.stat p { color: var(--muted); }
.stat strong { display: block; margin-top: 6px; font-family: var(--serif); font-size: 1.9rem; font-weight: 500; }

.section-head { display: flex; justify-content: space-between; gap: 12px; align-items: center; margin-bottom: 12px; }
.search { display: flex; gap: 8px; }
.search input { width: 240px; }

label { display: grid; gap: 6px; color: var(--muted); font-size: 0.92rem; }
input, textarea, select {
    width: 100%;
    border: 1px solid var(--line);
    background: white;
    color: var(--ink);
    border-radius: 10px;
    padding: 10px 12px;
    font: inherit;
}
textarea { min-height: 90px; resize: vertical; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--green-2); border-color: var(--green-2); }
#courier-field[hidden] { display: none; }
.date-field { max-width: 220px; }
.fields { display: grid; gap: 14px; }
.row-2 { display: grid; grid-template-columns: 1.4fr 0.8fr; gap: 14px; }
.hint { color: var(--muted); font-size: 0.92rem; }

.banner {
    background: #f8f3e8;
    border: 1px solid #eadfcd;
    border-radius: 12px;
    padding: 12px 14px;
}

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 12px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 0.75rem; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table-wrap { overflow-x: auto; }
.lines input { min-width: 0; }
.lines .qty, .lines .rate { width: 110px; }
.product-cell, .name-cell { position: relative; min-width: 0; }
.product-cell { min-width: 180px; }
.name-cell { display: block; }
.suggest {
    position: fixed;
    z-index: 80;
    display: grid;
    gap: 2px;
    margin: 0;
    padding: 4px;
    background: white;
    border: 1px solid var(--line);
    border-radius: 10px;
    box-shadow: 0 12px 30px rgba(40, 30, 15, 0.12);
    max-height: 240px;
    overflow: auto;
}
.suggest[hidden] { display: none; }
.suggest-item {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    border: 0;
    background: transparent;
    color: var(--ink);
    text-align: left;
    padding: 8px 10px;
    border-radius: 8px;
    font: inherit;
    cursor: pointer;
}
.suggest-item span { min-width: 0; }
.suggest-item small { color: var(--muted); white-space: nowrap; }
.suggest-item:hover,
.suggest-item.active { background: #f3f7f4; }
.price-field { max-width: 220px; }
.charge-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-top: 16px; }

.bill {
    --bill-pad-y: 28px;
    --bill-pad-x: 36px;
    max-width: 880px;
    color: #171717;
    background: #fff;
    border: 1px solid #eceae7;
    border-radius: 20px;
    box-shadow: 0 10px 30px rgba(20, 20, 20, 0.04);
    padding: var(--bill-pad-y) var(--bill-pad-x);
    display: flex;
    flex-direction: column;
}
.bill-top {
    margin: calc(var(--bill-pad-y) * -1) calc(var(--bill-pad-x) * -1) 22px;
}
.bill-banner {
    display: flex;
    align-items: center;
    gap: 16px;
    min-height: 108px;
    padding: 14px 28px 18px 18px;
    color: #fff;
    background:
        linear-gradient(180deg, rgba(255, 236, 220, 0.28), rgba(255, 236, 220, 0) 46%),
        linear-gradient(102deg, #e7b48e 0%, #c4846a 46%, #9a5344 100%);
    border-radius: 20px 20px 64px 0;
}
.bill-titlebar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-top: 0;
    padding: 0 28px 2px 0;
    position: relative;
}
.bill-nameplate {
    margin: -18px 0 0;
    background: #14110f;
    color: #f8f1e7;
    padding: 14px 52px 14px 20px;
    border-radius: 0 42px 42px 0;
    font-size: 1.02rem;
    font-weight: 700;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    line-height: 1;
}
.bill-logo {
    flex: 0 0 auto;
    background: #fff;
    padding: 5px;
    border-radius: 16px;
    line-height: 0;
    box-shadow: 0 6px 16px rgba(70, 28, 16, 0.22);
}
.bill-logo.is-empty, .scanner.is-empty {
    border: 1px dashed #ddd;
    color: #888;
    font-size: 0.78rem;
    min-width: 88px;
    min-height: 88px;
    display: grid;
    place-items: center;
}
.bill-logo img {
    display: block;
    width: 86px;
    height: 86px;
    max-width: 86px;
    max-height: 86px;
    border: 0;
    border-radius: 0;
    background: transparent;
    object-fit: contain;
}
.bill-contacts {
    display: flex;
    flex: 1;
    flex-direction: column;
    align-items: stretch;
    justify-content: center;
    gap: 8px;
    min-width: 0;
}
.bill-contact-row {
    display: flex;
    align-items: center;
    justify-content: space-evenly;
    gap: 8px;
    min-width: 0;
}
.bill-address-line {
    justify-content: center;
    text-align: center;
}
.bill .bill-contacts p,
.bill .bill-doc,
.bill .bill-number {
    margin: 0;
}
.bill .bill-nameplate { margin: -18px 0 0; }
.bill .bill-number { margin-top: 6px; }
.bill-contacts p {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 0;
    min-width: 0;
    color: #fff;
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.03em;
    line-height: 1.25;
    text-transform: uppercase;
}
.bill-contacts p + p {
    padding-left: 10px;
    border-left: 1px solid rgba(255, 255, 255, 0.45);
}
.bill-contacts svg {
    width: 18px;
    height: 18px;
    flex: none;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.7;
    stroke-linecap: round;
    stroke-linejoin: round;
}
.bill-contacts span { overflow-wrap: anywhere; }
.bill-id {
    text-align: right;
    padding: 10px 4px 0 20px;
}
.bill-doc {
    margin: 0;
    font-size: 1.65rem;
    font-weight: 700;
    letter-spacing: -0.03em;
    line-height: 1;
    color: #1c1612;
}
.bill .bill-number {
    margin: 6px 0 0;
    font-size: 0.82rem;
    font-weight: 600;
    letter-spacing: 0.14em;
    line-height: 1;
    color: #8a8178;
}
.bill-shop {
    margin: 0;
    display: grid;
    gap: 4px;
    color: #3a3836;
    font-size: 0.86rem;
    line-height: 1.35;
}
.bill-shop p {
    margin: 0;
    display: grid;
    grid-template-columns: 68px minmax(0, 1fr);
    gap: 8px;
    align-items: baseline;
}
.bill-shop span {
    color: #a07848;
    font-size: 0.62rem;
    letter-spacing: 0.12em;
    text-transform: uppercase;
}
.bill-meta span, .words span {
    color: #8a8178;
    font-size: 0.68rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}
.bill-parties, .bill-foot { display: grid; gap: 22px; }
.bill-parties { grid-template-columns: 1.4fr 0.8fr; padding: 8px 0 22px; }
.bill-panel {
    background: linear-gradient(180deg, #fffaf4 0%, #fafafa 100%);
    border-radius: 16px;
    border: 1px solid #f0e4d4;
    padding: 16px 18px;
}
.bill-panel h2 {
    margin: 0 0 10px;
    font-size: 0.68rem;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: #a07848;
    font-family: var(--sans);
}
.bill p { margin: 0 0 4px; }
.party-name { font-size: 1.25rem; font-weight: 600; letter-spacing: -0.02em; margin-bottom: 6px; }
.party-name a { color: inherit; text-decoration: none; }
.bill-address { white-space: pre-wrap; color: #3a3836; }
.bill-meta p { display: flex; justify-content: space-between; gap: 16px; align-items: baseline; margin-bottom: 10px; }
.bill-meta strong { font-weight: 600; }
.bill-lines { border-collapse: collapse; }
.bill-lines th {
    background: #1c1612;
    color: #f8f1e7;
    border: 0;
    padding: 10px 12px;
    font-size: 0.68rem;
    letter-spacing: 0.1em;
}
.bill-lines td {
    padding: 14px 12px;
    border-bottom: 1px solid #f3ebe3;
    vertical-align: middle;
}
.bill-lines td.num:last-child { color: #6e4e32; font-weight: 600; }
.bill-lines .sr { color: #8a8178; width: 48px; }
.bill .table-wrap { flex: 0 0 auto; }
.bill-lines tbody td { background: #fff; }
.bill-foot {
    grid-template-columns: 1.05fr 0.95fr;
    align-items: stretch;
    gap: 28px 40px;
    margin-top: auto;
    padding-top: 20px;
    border-top: 1px solid #eadfce;
}
.bill-sums { display: flex; flex-direction: column; }
.words {
    margin: 0 0 16px;
    padding: 10px 12px;
    background: #fffaf4;
    border-left: 3px solid #c4a27a;
    border-radius: 0 10px 10px 0;
    font-size: 1rem;
    line-height: 1.5;
}
.words span { display: block; margin-bottom: 4px; }
.scanner {
    width: 128px;
    margin: 0;
    padding: 6px;
    border: 1px solid #f0e4d4;
    border-radius: 12px;
    background: #fff;
}
.scanner img { width: 116px; height: 116px; object-fit: contain; display: block; }
.scan-note {
    margin: 8px 0 0;
    width: 128px;
    text-align: center;
    color: #8a8178;
    font-size: 0.72rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
}
.thanks {
    grid-column: 1 / -1;
    margin: 0;
    padding-top: 14px;
    border-top: 1px solid #f0e6da;
    text-align: center;
    font-size: 0.98rem;
    letter-spacing: 0.01em;
    color: #6e4e32;
}
.bill-sums p { display: flex; justify-content: space-between; gap: 16px; margin: 0 0 8px; color: #3a3836; }
.bill-sums strong { font-variant-numeric: tabular-nums; font-weight: 600; }
.sum-sheet {
    border: 1px solid #f0e4d4;
    border-radius: 14px;
    background: #fffaf6;
    padding: 12px 14px 6px;
}
.bill-sums p.sum-line {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 0.9em auto;
    align-items: baseline;
    column-gap: 8px;
    margin: 0 0 7px;
}
.sum-sign {
    text-align: center;
    color: #6e4e32;
    font-weight: 700;
}
.bill-sums .bill-total {
    margin-top: 2px;
    padding-top: 8px;
    border-top: 1px solid #e7d9c8;
    color: #1c1612;
    font-weight: 600;
}
.bill-sums .sum-previous {
    margin-top: 4px;
    padding-top: 8px;
    border-top: 1px dashed #e7d9c8;
}
.current-balance {
    margin: 12px 0 0;
    padding: 12px 14px;
    background: #1c1612;
    color: #f8f1e7;
    border: 0;
    border-radius: 12px;
}
.current-balance span { color: #e6d3bf; font-weight: 600; }
.current-balance strong { font-size: 1.45rem; font-weight: 600; letter-spacing: -0.03em; color: #fff; }
.sign-block {
    margin-top: auto;
    padding-top: 22px;
    margin-left: auto;
    width: fit-content;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
}
.bill-sums .sign-shop,
.bill-sums .sign-name {
    display: block;
    width: 100%;
    text-align: center;
    justify-content: center;
}
.sign-shop {
    margin: 0 0 2px;
    color: #6e4e32;
    font-size: 0.78rem;
    font-weight: 600;
    letter-spacing: 0.04em;
}
.sign-image {
    display: block;
    width: auto;
    max-width: 108px;
    max-height: 44px;
    height: auto;
    margin: 0 auto;
    object-fit: contain;
}
.sign-name {
    margin: 2px 0 0;
    padding: 0;
    border: 0;
    font-size: 0.78rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    color: #6e4e32;
}
.icon-btn { border: 0; background: transparent; color: var(--due); cursor: pointer; padding: 8px; font: inherit; }
.links { display: flex; gap: 12px; white-space: nowrap; }

.total-row {
    display: flex;
    justify-content: flex-end;
    align-items: baseline;
    gap: 16px;
    margin: 16px 0;
    font-size: 1.05rem;
}
.total-row strong { font-family: var(--serif); font-size: 1.6rem; font-weight: 500; }

.due { color: var(--due); }
.credit { color: var(--credit); }
.clear { color: var(--clear); }
.muted { color: var(--muted); }
.empty { color: var(--muted); margin: 8px 0; }

.flash { padding: 12px 14px; border-radius: 10px; }
.flash.ok { background: #e7f2ea; color: #1d4a32; }
.flash.error { background: #f8e8e6; color: #8a2b24; }

.split { display: grid; grid-template-columns: 1.3fr 0.7fr; gap: 16px; }
.hero-balance { display: flex; justify-content: space-between; gap: 18px; align-items: flex-start; }
.figure { font-family: var(--serif); font-size: 2.5rem; margin: 4px 0 8px; }
.address { white-space: pre-wrap; margin-top: 8px; }
.breakdown { margin: 0; display: grid; gap: 10px; min-width: 140px; }
.breakdown div { display: flex; justify-content: space-between; gap: 16px; }
.breakdown dt { color: var(--muted); }
.breakdown dd { margin: 0; font-variant-numeric: tabular-nums; }

.sheet { max-width: 860px; }
.sheet-head, .bill-to { display: flex; justify-content: space-between; gap: 16px; margin-bottom: 22px; }
.sheet-meta { text-align: right; }
.sheet-meta p:first-child { font-family: var(--serif); font-size: 1.3rem; }
.outstanding-line { margin-top: 8px; }

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(440px, 100%); background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 32px; box-shadow: 0 24px 50px rgba(40, 30, 15, 0.08); display: grid; gap: 14px; }
.login-card h1 { margin: 0; font-size: 2.2rem; }
.lede { color: var(--muted); }
.login-card form { display: grid; gap: 14px; }
.login-card .button { margin-top: 4px; }

@media (max-width: 860px) {
    .app { display: block; }
    .side { padding: 14px; }
    .side nav { flex-direction: row; flex-wrap: wrap; }
    .side form { margin-top: 8px; }
    .content { padding: 18px 16px 40px; }
    .page-head, .section-head, .hero-balance, .sheet-head { flex-direction: column; align-items: flex-start; }
    .stats, .row-2, .split, .charge-row, .bill-parties, .bill-foot { grid-template-columns: 1fr; }
    .bill { --bill-pad-y: 20px; --bill-pad-x: 16px; padding: var(--bill-pad-y) var(--bill-pad-x) 24px; }
    .bill-banner { flex-wrap: wrap; border-radius: 20px 20px 48px 0; padding-right: 16px; }
    .bill-contact-row { flex-wrap: wrap; justify-content: flex-start; }
    .bill-contacts p + p { border-left: 0; padding-left: 0; }
    .bill-address-line { justify-content: flex-start; text-align: left; }
    .bill-titlebar { flex-wrap: wrap; padding-right: 12px; }
    .bill-id { text-align: left; }
    .search, .search input { width: 100%; }
    .sheet-meta { text-align: left; }
}

@media print {
    @page { margin: 12mm; }
    body { background: white; }
    .side, .page-head, .flash, .no-print { display: none !important; }
    .app { display: block; }
    .content { padding: 0; display: block; }
    .sheet, .card { border: none; box-shadow: none; max-width: none; }
    .bill {
        border: 0;
        border-radius: 0;
        box-shadow: none;
        --bill-pad-y: 0px;
        --bill-pad-x: 0px;
        padding: 0;
        min-height: calc(297mm - 24mm);
        overflow: visible;
    }
    .bill-banner { border-radius: 0 0 72px 0; }
    .bill .table-wrap {
        flex: 1 1 auto;
        background-image: repeating-linear-gradient(
            to bottom,
            transparent 0,
            transparent 47px,
            #f3ebe3 47px,
            #f3ebe3 48px
        );
    }
    .bill-top { margin-bottom: 16px; }
    .bill-banner, .bill-nameplate { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    .bill-parties, .bill-foot { grid-template-columns: 1.15fr 0.85fr !important; }
    .bill-lines th, .current-balance, .bill-panel, .sum-sheet, .words, .bill-banner, .bill-nameplate, .table-wrap, .scanner { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    a { color: inherit; text-decoration: none; }
}
