/* One look for desktop and mobile: an app-style header, white cards, roomy controls. */
:root {
    --bg: #eef1f5;
    --surface: #ffffff;
    --surface-2: #f6f8fa;
    --text: #1b2230;
    --muted: #5f6b7d;
    --border: #dde2e9;
    --header: #1d2a3d;
    --header-text: #e9eef6;
    --accent: #1f66d1;
    --accent-text: #ffffff;
    --ok-bg: #e3f4e8;
    --ok-text: #1b5a31;
    --error-bg: #fbe8e8;
    --error-text: #9a1d1d;
    --warn-bg: #fff4dc;
    --notice-edge: #e0a100;
    --input-border: #cdd4dd;
    --accent-hover: #2a74e0;
    --row-hover: #fafbfd;
    --badge-bg: #e7eaf0; --badge-text: #3b4352;
    --st-draft-bg: #eceef2;
    --st-pending-bg: #fff1d6; --st-pending-text: #7a4b00;
    --st-issued-bg: #dfeafe; --st-issued-text: #19427f;
    --st-partial-bg: #e8e1fb; --st-partial-text: #46307f;
    --st-full-bg: #ddf3e4; --st-full-text: #1d5c33;
    --st-closed-bg: #e2e5ea; --st-closed-text: #4b5260;
    --st-cancel-bg: #fbe1e1; --st-cancel-text: #8a1c1c;
    --tab-current-text: #fff;
    --drop-edge: #b9c3d0;
    --drop-over: #eaf2ff;
    --danger-edge: #e3b4b4;
    --logo-bg: #fff;
    --radius: 10px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
}

/* Dark mode (name menu → Dark mode): the same variables, darker values. The title bar is dark in both. */
:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #10151c;
    --surface: #19202a;
    --surface-2: #202935;
    --text: #e3e8f0;
    --muted: #9ba7b9;
    --border: #2d3745;
    --header: #0b1017;
    --header-text: #e9eef6;
    --accent: #3d82ea;
    --accent-text: #ffffff;
    --ok-bg: #15321e;
    --ok-text: #93ddab;
    --error-bg: #3a191c;
    --error-text: #f4a6a6;
    --warn-bg: #382c12;
    --notice-edge: #d39b1c;
    --input-border: #3a4556;
    --accent-hover: #5a96ef;
    --row-hover: #212a36;
    --badge-bg: #2a3340; --badge-text: #c9d1dd;
    --st-draft-bg: #2a3340;
    --st-pending-bg: #3b2f14; --st-pending-text: #f2c76b;
    --st-issued-bg: #172a49; --st-issued-text: #a3c6ff;
    --st-partial-bg: #2a2146; --st-partial-text: #c9b8ff;
    --st-full-bg: #163923; --st-full-text: #93ddab;
    --st-closed-bg: #2a2f37; --st-closed-text: #aeb6c2;
    --st-cancel-bg: #3c1a1d; --st-cancel-text: #f4a6a6;
    --tab-current-text: #fff;
    --drop-edge: #4a5668;
    --drop-over: #1a2b45;
    --danger-edge: #6b2e34;
    --logo-bg: transparent;
    --shadow: 0 1px 2px rgba(0, 0, 0, .35), 0 1px 3px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
}
a { color: var(--accent); }

/* ---- Header ---- */
.topbar { background: var(--header); color: var(--header-text); position: relative; z-index: 20; }
.topbar-inner { position: relative; max-width: 1180px; margin: 0 auto; padding: 0 16px; min-height: 56px; display: flex; align-items: center; gap: 8px 20px; flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 17px; color: #fff; text-decoration: none; padding: 14px 0; flex: 1 0 100%; text-align: center; }
.topbar .mainnav { padding-bottom: 8px; }
.topbar .menus { margin-left: auto; padding-bottom: 8px; }
.mainnav { display: flex; align-items: center; gap: 4px; flex: 1; flex-wrap: wrap; }
.mainnav a { color: var(--header-text); text-decoration: none; padding: 8px 12px; border-radius: 8px; }
.mainnav a:hover { background: rgba(255, 255, 255, .08); }
.mainnav a.current { background: rgba(255, 255, 255, .14); color: #fff; font-weight: 600; }
.mainnav a.navbutton { background: var(--accent); color: #fff; font-weight: 600; margin-left: 6px; }
.mainnav a.navbutton:hover { background: var(--accent-hover); }
.menus { display: flex; gap: 4px; }
.menu { position: relative; }
.menu summary { list-style: none; cursor: pointer; padding: 8px 12px; border-radius: 8px; color: var(--header-text); user-select: none; }
.menu summary::-webkit-details-marker { display: none; }
.menu summary::after { content: " ▾"; font-size: 12px; opacity: .8; }
.menu summary:hover, .menu[open] summary, .menu summary.current { background: rgba(255, 255, 255, .12); }
.menu-items { position: absolute; right: 0; top: calc(100% + 6px); min-width: 200px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 8px 24px rgba(16, 24, 40, .16); padding: 6px; display: flex; flex-direction: column; z-index: 30; }
.menu-items a, .menu-button { display: block; text-align: left; width: 100%; padding: 9px 12px; border-radius: 6px; color: var(--text); text-decoration: none; background: none; border: 0; font: inherit; cursor: pointer; min-height: 0; }
.menu-items a:hover, .menu-button:hover { background: var(--surface-2); }

/* ---- Page ---- */
main { max-width: 1180px; margin: 0 auto; padding: 24px 16px 64px; }
body.mobile main { max-width: 640px; padding: 16px 12px 48px; }
h1 { font-size: 24px; line-height: 1.25; margin: 4px 0 14px; }
h2 { font-size: 17px; margin: 0 0 12px; }
h3 { font-size: 15px; margin: 0 0 8px; }
.pagehead { display: flex; align-items: center; justify-content: space-between; gap: 12px 16px; flex-wrap: wrap; margin-bottom: 8px; }
.pagehead h1 { margin-bottom: 6px; }
.lead { font-size: 17px; margin: 0 0 16px; }
.muted, .hint { color: var(--muted); }
.hint { font-size: 13px; font-weight: normal; }
.warn { color: var(--error-text); }
.mt { margin-top: 14px; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
code { font-size: 13px; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; word-break: break-all; }
pre { white-space: pre-wrap; word-break: break-word; font-size: 12px; margin: 4px 0; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; margin: 0 0 18px; }
.card > :last-child { margin-bottom: 0; }
.narrow { max-width: 420px; margin: 40px auto; }
.narrowform { max-width: 600px; }
.inset { background: var(--surface-2); box-shadow: none; padding: 14px; margin-top: 14px; }
.notice { border-left: 4px solid var(--notice-edge); }
.notice.warn { border-left-color: var(--error-text); color: var(--text); }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px 18px; align-items: start; }
.grid2 > .card { margin: 0; }
.grid2 + .card, .grid2 + section { margin-top: 18px; }

.flash { padding: 12px 16px; border-radius: var(--radius); margin-bottom: 16px; }
.flash-ok { background: var(--ok-bg); color: var(--ok-text); }
.flash-error, .error { background: var(--error-bg); color: var(--error-text); padding: 12px 16px; border-radius: var(--radius); }
.error ul { margin: 6px 0 0; }
.fielderror { color: var(--error-text); font-size: 13px; font-weight: normal; }

/* ---- Forms ---- */
form.stack, .stack { display: flex; flex-direction: column; gap: 14px; }
form.inline { display: inline; }
label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 14px; }
label.check { flex-direction: row; align-items: flex-start; gap: 10px; font-weight: normal; }
label.check input { margin-top: 3px; width: 18px; height: 18px; flex: none; }
label.row { flex-direction: row; align-items: center; gap: 10px; font-weight: normal; margin-bottom: 10px; }
label.row select { width: auto; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; margin: 0; display: flex; flex-direction: column; gap: 10px; }
legend { font-weight: 600; padding: 0 6px; }
input, select, textarea {
    font: inherit; color: var(--text); background: var(--surface);
    border: 1px solid var(--input-border); border-radius: 8px; padding: 9px 12px; width: 100%; min-height: 42px; font-weight: 400;
}
textarea { min-height: 0; }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(31, 102, 209, .35); outline-offset: 0; border-color: var(--accent); }
input[type="checkbox"], input[type="radio"] { width: 18px; height: 18px; min-height: 0; padding: 0; }
input[type="file"] { border-style: dashed; background: var(--surface-2); }

button, .button {
    font: inherit; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    min-height: 42px; padding: 8px 16px; border: 1px solid var(--input-border); border-radius: 8px;
    background: var(--surface); color: var(--text); text-decoration: none; cursor: pointer; white-space: nowrap;
}
button:hover, .button:hover { background: var(--surface-2); }
button.primary, .button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.primary:hover, .button.primary:hover { background: var(--accent-hover); }
.linkbutton { border: 0; padding: 0; min-height: 0; background: none; color: var(--accent); text-decoration: underline; font-weight: normal; }
.block { display: flex; width: 100%; }
.stack > button, .stack > .button { align-self: flex-start; }
.block { align-self: stretch !important; }
.actions { display: flex; align-items: center; gap: 10px 14px; flex-wrap: wrap; }
.filters { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; }
.filters label { min-width: 170px; flex: 1; }
.filters label.check { flex: none; min-width: 0; align-self: center; }
.filters button { flex: none; }

/* ---- Tables ---- */
.tablewrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 18px; }
.card .tablewrap { box-shadow: none; margin-bottom: 0; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
thead th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: var(--row-hover); }
tfoot th { background: var(--surface-2); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.disabled td { color: var(--muted); }
tr.disabled input { opacity: .6; }
tr.rowerror td { background: var(--error-bg); }
tr.justadded td { background: var(--ok-bg); transition: background 2s; }
table.facts th { width: 40%; font-size: 14px; text-transform: none; letter-spacing: 0; color: var(--muted); background: none; }
table.facts td, table.facts th { padding: 8px 4px; }
table.totals { width: auto; min-width: 300px; margin: 14px 0 0 auto; }
table.totals td { padding: 6px 12px; }
table.totals tr.grand td { font-weight: 700; font-size: 16px; border-top: 2px solid var(--text); }
table.lines td, table.receive td { padding: 8px 6px; vertical-align: middle; }
table.lines td:nth-child(2) input { min-width: 240px; }
.w-num { width: 100px; }
.w-part { width: 140px; }

/* ---- Status badges ---- */
.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px; background: var(--badge-bg); color: var(--badge-text); vertical-align: middle; white-space: nowrap; }
.status-draft { background: var(--st-draft-bg); }
.status-pending_approval { background: var(--st-pending-bg); color: var(--st-pending-text); }
.status-issued { background: var(--st-issued-bg); color: var(--st-issued-text); }
.status-partially_received { background: var(--st-partial-bg); color: var(--st-partial-text); }
.status-fully_received { background: var(--st-full-bg); color: var(--st-full-text); }
.status-closed { background: var(--st-closed-bg); color: var(--st-closed-text); }
.status-cancelled, .status-rejected { background: var(--st-cancel-bg); color: var(--st-cancel-text); }

/* ---- Dashboard ---- */
.tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 18px; }
.tabs a { padding: 8px 14px; text-decoration: none; color: var(--muted); border-radius: 999px; background: var(--surface); border: 1px solid var(--border); font-weight: 600; font-size: 14px; }
.tabs a.current { color: var(--tab-current-text); background: var(--header); border-color: var(--header); }
.approval { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 10px 16px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.approval:last-child { border-bottom: 0; }
.approval-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.reject-form { display: inline-flex; gap: 6px; }
.reject-form input { width: 210px; }
ul.plain { list-style: none; margin: 0; padding: 0; }
ul.plain li { padding: 10px 0; border-bottom: 1px solid var(--border); }
ul.plain li:last-child { border-bottom: 0; }
ul.polist { list-style: none; padding: 0; margin: 0; }
ul.polist li { padding: 12px 0; border-bottom: 1px solid var(--border); }
ul.polist li:last-child { border-bottom: 0; }

/* ---- PO page ---- */
.sendbox { border-bottom: 1px solid var(--border); padding-bottom: 14px; margin-bottom: 14px; }
.charge { margin-bottom: 10px; flex-wrap: nowrap; }
.charge select { width: auto; }
.charge input:not(.w-num) { flex: 1; }
.totals-live { margin: 10px 0 0; font-size: 16px; }
.receipt { padding: 10px 0; border-bottom: 1px solid var(--border); }
.receipt:last-child { border-bottom: 0; }
.receipt ul { margin: 6px 0; }
.newcontact { background: var(--surface-2); }

/* ---- Job combo box ---- */
.combo { position: relative; font-weight: normal; }
.combo-list { position: absolute; z-index: 10; left: 0; right: 0; margin: 4px 0 0; padding: 4px; list-style: none; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); max-height: 300px; overflow-y: auto; box-shadow: 0 8px 24px rgba(16, 24, 40, .14); }
.combo-list:empty { display: none; }
.combo-list li { padding: 9px 12px; cursor: pointer; border-radius: 6px; }
.combo-list li[role="option"]:hover { background: var(--surface-2); }

/* ---- CSV drop ---- */
.dropzone { border: 2px dashed var(--drop-edge); border-radius: var(--radius); padding: 22px; display: flex; flex-wrap: wrap; align-items: center; gap: 14px; background: var(--surface-2); }
.dropzone.over { border-color: var(--accent); background: var(--drop-over); }
.dropzone p { margin: 0; font-weight: 600; }
.dropzone input[type="file"] { width: auto; flex: 1; min-width: 200px; }

/* ---- Quick PO lines (mobile) ---- */
.mline { display: flex; flex-direction: column; gap: 8px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.mline-row { display: grid; grid-template-columns: 1fr 90px 1fr; gap: 8px; }

@media (max-width: 720px) {
    .topbar-inner { gap: 4px 12px; }
    .brand { padding: 10px 0 6px; font-size: 15px; line-height: 1.3; }
    .mainnav { order: 3; flex: 1 0 100%; }
    .topbar .menus { padding-bottom: 0; }
    .reject-form input { width: 150px; }
    .charge { flex-wrap: wrap; }
    .charge input:not(.w-num) { flex: 1 1 100%; order: 3; }
    table.lines td:nth-child(2) input { min-width: 180px; }
    .card { padding: 16px; }
    h1 { font-size: 21px; }
}

/* ---- Modal question (GOODS RECEIVED?) ---- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(16, 24, 40, .45); z-index: 40; }
dialog.modal { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); margin: 0; z-index: 50;
    width: min(440px, calc(100vw - 32px)); border: 0; border-radius: 14px; padding: 28px; box-shadow: 0 20px 50px rgba(16, 24, 40, .3); background: var(--surface); color: var(--text); }
.modal-question { font-size: 26px; text-align: center; margin: 0; letter-spacing: .02em; }
dialog.modal .muted, dialog.modal .hint { text-align: center; }
.modal-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.modal-actions button { min-height: 52px; font-size: 18px; }

/* ---- Destructive actions ---- */
button.danger { background: var(--surface); border-color: var(--danger-edge); color: var(--error-text); }
button.danger:hover { background: var(--error-bg); }
details.danger summary { color: var(--error-text); cursor: pointer; font-weight: 600; }
.datelabel { display: block; font-size: 12px; margin-bottom: 4px; }

/* ---- Phones: tables marked .cards turn each row into a stacked card ---- */
@media (max-width: 720px) {
    table.cards, table.cards tbody, table.cards tr, table.cards td { display: block; width: 100%; }
    table.cards thead { display: none; }
    .tablewrap:has(> table.cards) { overflow-x: visible; background: none; border: 0; box-shadow: none; }
    table.cards tbody tr { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 10px 14px; margin-bottom: 12px; }
    table.cards tbody tr:hover td { background: none; }
    table.cards td { display: grid; grid-template-columns: 42% 1fr; gap: 10px; align-items: center; border: 0; padding: 5px 0; text-align: left !important; }
    table.cards td::before { content: attr(data-label); color: var(--muted); font-size: 13px; font-weight: 600; }
    /* A cell with no label is the card's title. */
    table.cards td:not([data-label]):not(.cardplain):not(.datecell) { display: block; font-weight: 600; padding-bottom: 8px; border-bottom: 1px solid var(--border); margin-bottom: 4px; }
    table.cards td:not([data-label])::before { content: none; }
    table.cards td.hide-card { display: none !important; }
    table.cards td.cardplain { display: block; padding: 5px 0; }
    table.cards td.datecell .datelabel { color: var(--muted); font-size: 13px; font-weight: 600; margin: 0; }
    table.cards td:empty { display: none; }
    table.cards td input:not([type="checkbox"]):not([type="radio"]), table.cards td select { width: 100%; min-width: 0 !important; }
    table.cards tfoot { display: block; }
    /* Draft and amend line editor: Qty, Unit and Price side by side, as on Quick PO. */
    table.cards.lines tbody tr { display: grid; grid-template-columns: repeat(3, 1fr); column-gap: 8px; }
    table.cards.lines td { grid-column: 1 / -1; }
    table.cards.lines td.trio { grid-column: auto; display: flex; flex-direction: column; align-items: stretch; gap: 4px; }
    table.cards.lines td.trio::before { font-size: 12px; }
    table.cards.lines td[data-label="Description"], table.cards.lines td[data-label="Part no."] { display: flex; flex-direction: column; align-items: stretch; gap: 4px; }
}
.show-card { display: none; }
@media (max-width: 720px) { table.cards .show-card { display: inline; } }

/* ---- PO page: details and actions run across the page ---- */
.factgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px 24px; margin: 0; }
.factgrid > div { min-width: 0; }
.factgrid > .wide { grid-column: 1 / -1; }
.factgrid dt { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; margin-bottom: 3px; }
.factgrid dd { margin: 0; overflow-wrap: anywhere; }
.actiongrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 20px 28px; align-items: start; }
.actiongrid > div + div { border-left: 1px solid var(--border); padding-left: 28px; }
.actiongrid .inset { margin-top: 12px; }
select.w-auto { width: auto; }
@media (max-width: 720px) {
    .actiongrid > div + div { border-left: 0; padding-left: 0; border-top: 1px solid var(--border); padding-top: 16px; }
}

/* Company logo at the left end of the title bar; the title stays centred. */
.brandlogo { position: absolute; left: 16px; top: 8px; height: 40px; max-width: 180px; object-fit: contain; background: var(--logo-bg); border-radius: 6px; padding: 3px 6px; }
@media (max-width: 720px) { .brandlogo { height: 30px; max-width: 110px; top: 6px; left: 12px; } .brand { padding-left: 110px; padding-right: 0; } }

/* Mobile pages: the nav link(s) sit on the same line as the Menu dropdown, left-aligned. */
@media (max-width: 720px) {
    body.mobile .topbar .mainnav { order: 2; flex: 1 1 auto; width: auto; padding-bottom: 6px; }
    body.mobile .topbar .menus { order: 3; margin-left: auto; padding-bottom: 6px; }
}
.scrollbox { max-height: 420px; overflow-y: auto; }
.quotelist { list-style: none; padding: 0; margin: 0 0 12px; }
.quotelist li { display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: baseline; padding: 6px 0; border-bottom: 1px solid var(--border); }
[data-quotes].busy { opacity: .6; pointer-events: none; }
.mb { margin-bottom: 18px; }
.orphan { border-bottom: 1px solid var(--border); padding-bottom: 10px; }
.orphan .hint { margin: 2px 0 4px 26px; }
.orphan details { margin-left: 26px; }
.orphanlist { margin: 6px 0 0; padding-left: 18px; font-size: 14px; word-break: break-all; }
.cardhead { display: flex; align-items: center; justify-content: space-between; gap: 8px 16px; flex-wrap: wrap; }
.cardhead h2 { margin: 0; }
.button.small { padding: 4px 10px; font-size: 13px; min-height: 0; }
.checkgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 4px 16px; margin: 6px 0 0 28px; }
.checkgrid.off { opacity: .45; pointer-events: none; }
.logopreview { display: flex; align-items: center; justify-content: center; min-height: 70px; border-radius: 8px; padding: 10px; margin-bottom: 10px; border: 1px solid var(--border); }
.logopreview img { max-height: 56px; max-width: 100%; }
.logobox-light .logopreview { background: #ffffff; }
.logobox-dark .logopreview { background: #0b1017; }
.logobox h3 { margin-top: 0; }
.raterow { border-top: 1px solid var(--border); padding-top: 10px; margin-top: 10px; }
.raterow p { margin: 0 0 6px; }
.inline-details { display: inline-block; margin-left: 10px; font-size: 14px; }
.tslines td { vertical-align: top; }
.tslines .jobcell { min-width: 240px; }
.w-hrs { width: 74px; text-align: right; }
.tstotals { margin: 12px 0 0; }
.combo-list li.combo-wo { font-weight: 600; border-bottom: 1px solid var(--border); border-radius: 0; }
.ts-draft { background: var(--st-draft-bg); }
.ts-submitted { background: var(--st-issued-bg); color: var(--st-issued-text); }
.ts-returned { background: var(--st-pending-bg); color: var(--st-pending-text); }
.ts-approved { background: var(--st-full-bg); color: var(--st-full-text); }
.tslines tr.xdesc td { border-top: 0; padding-top: 0; }
.tslines tr.xdesc input { width: 100%; }
.tslines tbody[data-xline] + tbody[data-xline] tr:first-child td { border-top: 1px solid var(--border); }
