:root {
  --bg: #f4f6f9; --card: #fff; --text: #1d2433; --muted: #6b7486; --border: #dfe3ea;
  --primary: #2558b8; --primary-d: #1c4592; --success: #1f8a4c; --danger: #c0392b; --warning: #c98a0b; --info: #1f7a9a; --dark: #343a40;
  --radius: 6px;
}
* { box-sizing: border-box; }
html { font-size: 14px; }
body { margin: 0; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); line-height: 1.45; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.6rem; margin: 0 0 1rem; font-weight: 600; }
h2 { font-size: 1.15rem; margin: 0 0 .75rem; font-weight: 600; }
h3 { font-size: 1rem; margin: 1rem 0 .4rem; }
code { font-size: .85em; background: #eef1f6; padding: 1px 4px; border-radius: 3px; }
hr { border: 0; border-top: 1px solid var(--border); margin: 1rem 0; }
.muted { color: var(--muted); }
.small { font-size: .88rem; }
.r { text-align: right; }
.nowrap { white-space: nowrap; }
.text-danger { color: var(--danger); }

/* Top bar */
.topbar { display: flex; align-items: center; gap: 1.5rem; background: #1b2a47; color: #fff; padding: 0 1.25rem; height: 52px; position: sticky; top: 0; z-index: 20; }
.topbar a, .topbar button.link { color: #cfd8ea; }
.brand { font-weight: 700; color: #fff !important; font-size: 1.05rem; white-space: nowrap; }
.mainnav { display: flex; align-items: center; gap: .25rem; flex: 1; }
.mainnav > a, .dropdown-toggle { padding: .5rem .75rem; border-radius: var(--radius); color: #cfd8ea; background: none; border: 0; font: inherit; cursor: pointer; }
.mainnav > a.active, .mainnav > a:hover, .dropdown-toggle:hover { background: rgba(255,255,255,.1); color: #fff; text-decoration: none; }
.dropdown { position: relative; }
.dropdown-menu { display: none; position: absolute; top: 100%; left: 0; background: #fff; border: 1px solid var(--border); border-radius: var(--radius); min-width: 200px; box-shadow: 0 6px 20px rgba(0,0,0,.12); padding: .3rem 0; }
.dropdown:hover .dropdown-menu, .dropdown:focus-within .dropdown-menu { display: block; }
.dropdown-menu a { display: block; padding: .45rem 1rem; color: var(--text) !important; }
.dropdown-menu a:hover, .dropdown-menu a.active { background: var(--bg); text-decoration: none; }
.userbox { display: flex; gap: 1rem; align-items: center; white-space: nowrap; }
.nav-toggle { display: none; background: none; border: 0; color: #fff; font-size: 1.4rem; }
button.link { background: none; border: 0; padding: 0; font: inherit; cursor: pointer; }

.container { max-width: 1400px; margin: 0 auto; padding: 1.5rem 1.25rem 3rem; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head h1 { margin: 0; }
.actions { display: flex; gap: .4rem; flex-wrap: wrap; align-items: center; }

/* Cards & layout */
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.1rem 1.25rem; margin-bottom: 1rem; }
.card.narrow { max-width: 640px; }
.grid-2, .grid-3, .grid-4 { display: grid; gap: 0 1rem; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-2 > .card, .grid-3 > .card { margin-bottom: 1rem; }
.span-2 { grid-column: span 2; }

/* Forms */
label { display: block; margin-bottom: .75rem; font-weight: 500; font-size: .92rem; }
label > input, label > select, label > textarea { margin-top: .25rem; }
input[type=text], input:not([type]), input[type=email], input[type=password], input[type=number], input[type=date], select, textarea {
  width: 100%; padding: .45rem .6rem; border: 1px solid #c8cfda; border-radius: var(--radius); font: inherit; background: #fff; color: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(37,88,184,.25); border-color: var(--primary); }
input:disabled, select:disabled, textarea:disabled { background: #f1f3f6; }
textarea.code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .82rem; line-height: 1.4; }
label.check { display: flex; gap: .45rem; align-items: baseline; font-weight: normal; margin-bottom: .45rem; }
label.check input { margin: 0; }
fieldset { border: 0; padding: 0; margin: 0 0 1rem; min-width: 0; }
legend { font-weight: 600; margin-bottom: .5rem; padding: 0; }
.field-error { color: var(--danger); font-size: .85rem; margin: -.5rem 0 .6rem; }
.form-actions { display: flex; gap: .5rem; margin-top: 1rem; flex-wrap: wrap; }
.filters { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; padding: .75rem 1rem; }
.filters input, .filters select { width: auto; min-width: 160px; }
.filters input[name=q] { min-width: 240px; }
.inline-label { display: inline-flex; gap: .4rem; align-items: center; margin: 0; font-weight: normal; }
.inline-label input { width: auto; margin: 0; }
.inline-form { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; margin-bottom: 1rem; }
.inline-form input, .inline-form select { width: auto; flex: 1; min-width: 180px; }
form.inline { display: inline; }
.subform { margin-top: .75rem; padding-top: .75rem; border-top: 1px dashed var(--border); }
details > summary { cursor: pointer; list-style: none; display: inline-block; }
details > summary::-webkit-details-marker { display: none; }

/* Buttons */
.btn { display: inline-block; padding: .45rem .9rem; border-radius: var(--radius); border: 1px solid #c8cfda; background: #fff; color: var(--text); font: inherit; font-weight: 500; cursor: pointer; line-height: 1.3; white-space: nowrap; }
.btn:hover { background: #f0f2f6; text-decoration: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-d); }
.btn-success { background: var(--success); border-color: var(--success); color: #fff; }
.btn-success:hover { background: #18703d; }
.btn-danger { color: var(--danger); border-color: #e3b3ad; }
.btn-danger:hover { background: #fbeeec; }
.btn-sm { padding: .2rem .55rem; font-size: .85rem; }
.btn-block { width: 100%; }
.chip { display: inline-block; padding: .2rem .6rem; border-radius: 999px; background: #eef1f6; border: 1px solid var(--border); font-size: .85rem; color: var(--text); cursor: pointer; margin: 0 .2rem .3rem 0; font-family: inherit; }
.chip:hover { background: #e2e7f0; text-decoration: none; }
.chip-active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* Tables */
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-weight: 600; font-size: .82rem; text-transform: uppercase; letter-spacing: .02em; color: var(--muted); border-bottom: 2px solid var(--border); padding: .5rem .5rem; }
.table th.r { text-align: right; }
.table td { padding: .5rem .5rem; border-bottom: 1px solid #edf0f4; vertical-align: top; }
.table tr.inactive td { color: var(--muted); }
.table tbody tr:hover td { background: #fafbfd; }
.table-scroll { overflow-x: auto; }
.w1 { width: 1%; }
.details { max-width: 420px; word-break: break-word; font-size: .82rem; color: var(--muted); }

/* Badges & alerts */
.badge { display: inline-block; padding: .12rem .5rem; border-radius: 999px; font-size: .75rem; font-weight: 600; vertical-align: middle; white-space: nowrap; }
.badge-primary { background: #dfe8fa; color: var(--primary-d); }
.badge-secondary { background: #eceef2; color: #555d6e; }
.badge-success { background: #dcf3e5; color: #17703d; }
.badge-danger { background: #f9e0dd; color: #a12e22; }
.badge-warning { background: #fbefd4; color: #8a5d00; }
.badge-info { background: #dcf0f6; color: #155f78; }
.badge-dark { background: #3a4150; color: #fff; }
.alert { padding: .7rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; border: 1px solid; }
.alert-success { background: #e7f6ed; border-color: #b7e2c6; color: #155c33; }
.alert-error { background: #fbeceb; border-color: #f0c2bd; color: #8e2a20; }
.alert-warning { background: #fdf5e2; border-color: #f1dca5; color: #74520b; }
.alert-info { background: #e6f3f8; border-color: #b6dcea; color: #1b5a70; }

/* Dashboard */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.stat { background: var(--card); border: 1px solid var(--border); border-left: 4px solid var(--primary); border-radius: var(--radius); padding: .9rem 1rem; color: var(--muted); font-size: .88rem; }
.stat span { display: block; font-size: 1.5rem; font-weight: 700; color: var(--text); }
.stat.warn { border-left-color: var(--warning); }
.stat.ok { border-left-color: var(--success); }
.stat:hover { text-decoration: none; box-shadow: 0 2px 8px rgba(0,0,0,.06); }

.dl { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1rem; margin: 0; }
.dl dt { color: var(--muted); }
.dl dd { margin: 0; }
.history { list-style: none; padding: 0; margin: 0; font-size: .9rem; }
.history li { padding: .3rem 0; border-bottom: 1px solid #f0f2f5; }
.pagination { display: flex; gap: .25rem; flex-wrap: wrap; margin: .5rem 0 1rem; }
.pagination a, .pagination span { padding: .3rem .65rem; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; }
.pagination .current { background: var(--primary); color: #fff; border-color: var(--primary); }
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); margin-bottom: 1rem; flex-wrap: wrap; }
.tabs a { padding: .55rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: var(--radius) var(--radius) 0 0; color: var(--muted); }
.tabs a.active { background: #fff; border-color: var(--border); color: var(--text); margin-bottom: -1px; font-weight: 600; }

/* Roles */
.perm-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 1rem; }
.perm-module { border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem 1rem; }
.perm-module legend { padding: 0 .3rem; }
.perm-module code { font-size: .75rem; }

/* PO form */
.lines-table input { padding: .35rem .45rem; }
.lines-table .num { text-align: right; }
.lines-table td { padding: .3rem .25rem; }
.w-product { width: 18%; } .w-desc { width: 28%; } .w-num { width: 9%; } .w-unit { width: 7%; }
.product-cell { position: relative; }
.ac-list { position: absolute; z-index: 30; left: 0; right: -200px; background: #fff; border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 6px 18px rgba(0,0,0,.12); display: none; max-height: 280px; overflow-y: auto; }
.ac-list.open { display: block; }
.ac-item { padding: .45rem .7rem; cursor: pointer; font-size: .9rem; }
.ac-item:hover, .ac-item.active { background: #eef3fc; }
.ac-item small { color: var(--muted); }
.po-bottom { margin-top: 1rem; align-items: start; }
.totals-table { margin-left: auto; min-width: 300px; border-collapse: collapse; }
.totals-table th { text-align: left; padding: .4rem .75rem; font-weight: 500; }
.totals-table td { text-align: right; padding: .4rem .75rem; }
.totals-table input { width: 120px; text-align: right; }
.totals-table .grand th, .totals-table .grand td { font-size: 1.15rem; font-weight: 700; border-top: 2px solid var(--text); }

/* Invoice review */
.invoice-layout { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(360px, 1fr); gap: 1rem; align-items: start; }
.invoice-pdf { padding: 0; overflow: hidden; position: sticky; top: 64px; }
.invoice-pdf iframe { width: 100%; height: calc(100vh - 100px); border: 0; display: block; }
.match-box { border-left: 4px solid var(--muted); }
.match-matched { border-left-color: var(--success); }
.match-value_mismatch { border-left-color: var(--danger); }
.match-matched_by_value { border-left-color: var(--info); }
.match-po_not_found, .match-no_po_reference { border-left-color: var(--warning); }
.compare { width: 100%; border-collapse: collapse; margin: .5rem 0; }
.compare td, .compare th { padding: .3rem .4rem; border-bottom: 1px solid #edf0f4; }
.notes-list { padding-left: 1.1rem; font-size: .9rem; }
.approve-form { margin-top: .75rem; padding-top: .75rem; border-top: 1px solid var(--border); }
.ocr-text { white-space: pre-wrap; font-size: .78rem; background: #f6f7f9; padding: .75rem; border-radius: var(--radius); max-height: 500px; overflow: auto; }
.dropzone { border: 2px dashed #b9c2d2; border-radius: var(--radius); padding: 2rem; text-align: center; margin-bottom: 1rem; background: #fafbfd; }
.dropzone.drag { border-color: var(--primary); background: #eef3fc; }
.attachment { align-self: center; }

/* Guest */
body.guest { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.guest-box { width: 100%; max-width: 380px; padding: 1rem; }
.brand-lg { text-align: center; color: #1b2a47; }
.error-page { text-align: center; max-width: 640px; margin: 2rem auto; }
.error-page h1 { font-size: 3rem; color: var(--muted); }
.error-message { white-space: pre-wrap; font-family: inherit; text-align: left; display: inline-block; max-width: 100%; overflow-x: auto; }

@media (max-width: 1000px) {
  .grid-3, .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .invoice-layout { grid-template-columns: 1fr; }
  .invoice-pdf { position: static; }
  .invoice-pdf iframe { height: 70vh; }
}
@media (max-width: 760px) {
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .nav-toggle { display: block; margin-left: auto; }
  .mainnav, .userbox { display: none; }
  body.nav-open .topbar { flex-wrap: wrap; height: auto; padding-bottom: .5rem; }
  body.nav-open .mainnav, body.nav-open .userbox { display: flex; flex-direction: column; align-items: flex-start; width: 100%; }
  .dropdown-menu { position: static; display: block; box-shadow: none; background: transparent; border: 0; }
  .dropdown-menu a { color: #cfd8ea !important; }
  .table { font-size: .9rem; }
  .card .table { display: block; overflow-x: auto; }
}

/* Matching / exports */
.tabs .count { display: inline-block; min-width: 1.4em; padding: 0 .35em; border-radius: 999px; background: #eceef2; font-size: .78rem; text-align: center; margin-left: .2rem; }
.bulk-bar { display: flex; flex-wrap: wrap; gap: .6rem 1rem; align-items: center; padding-bottom: .75rem; margin-bottom: .5rem; border-bottom: 1px solid var(--border); }
.bulk-bar .check { margin: 0; }
.notes-cell { max-width: 380px; color: var(--muted); }
.row-changed td { background: #fffbea; }
details.overrides { margin: .5rem 0 1rem; padding: .75rem 1rem; border: 1px dashed var(--border); border-radius: var(--radius); }
details.overrides summary { cursor: pointer; margin-bottom: .5rem; }
.status-dot { display: inline-block; width: .6rem; height: .6rem; border-radius: 50%; margin-right: .3rem; background: var(--muted); }
.status-dot.connected { background: var(--success); }
.status-dot.error { background: var(--danger); }
pre.payload { white-space: pre-wrap; word-break: break-all; font-size: .76rem; background: #f6f7f9; padding: .75rem; border-radius: var(--radius); max-height: 420px; overflow: auto; }
.text-warning { color: var(--warning); }
.provider-card { display: block; color: inherit; } .provider-card:hover { text-decoration: none; border-color: var(--primary); }

/* Installer */
body.installer { background: var(--bg); }
.install-head { background: #1b2a47; color: #fff; padding: 1rem 1.25rem .5rem; }
.install-brand { font-weight: 700; font-size: 1.1rem; }
.install-brand span { font-weight: 400; color: #9fb0cf; font-size: .85rem; margin-left: .4rem; }
.install-steps { list-style: none; display: flex; flex-wrap: wrap; gap: .25rem 1.25rem; padding: 0; margin: .75rem 0 0; font-size: .9rem; }
.install-steps li { color: #8193b5; padding-bottom: .4rem; border-bottom: 3px solid transparent; }
.install-steps li.done, .install-steps li.done a { color: #b9e3c8; }
.install-steps li.current { color: #fff; border-bottom-color: #6ea8ff; font-weight: 600; }
.install-main { max-width: 860px; margin: 0 auto; padding: 1.5rem 1.25rem 3rem; }
.install-main .card.narrow { max-width: none; }
.prep li { margin-bottom: .4rem; }
.checks td { vertical-align: top; }
.check-icon { display: inline-block; width: 1.5rem; height: 1.5rem; line-height: 1.5rem; text-align: center; border-radius: 50%; font-weight: 700; background: #eceef2; color: var(--muted); }
.check-ok .check-icon { background: #dcf3e5; color: #17703d; }
.check-warn .check-icon { background: #fbefd4; color: #8a5d00; }
.check-fail .check-icon { background: #f9e0dd; color: #a12e22; }
.check-help { color: #8a5d00; margin-top: .2rem; }
.check-fail .check-help { color: #a12e22; }
.install-actions { display: flex; flex-wrap: wrap; gap: .75rem; align-items: center; }
.test-results { list-style: none; padding: 0; margin: .5rem 0; font-size: .92rem; }
.test-results li { padding: .15rem 0; }
.text-success { color: var(--success); }

[hidden] { display: none !important; }
.driver-pick { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .5rem; }
.driver-option { border: 1px solid var(--border); border-radius: var(--radius); padding: .6rem .8rem; display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; cursor: pointer; font-weight: normal; margin: 0; }
.driver-option:has(input:checked) { border-color: var(--primary); background: #eef3fc; }
.driver-fields { margin-top: 1rem; }
.card-inset { border: 1px solid var(--border); border-radius: var(--radius); padding: .9rem 1rem; margin: .5rem 0 1rem; background: #fafbfd; }

/* Invoice template mapping */
.map-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 1rem; align-items: start; }
.map-side { position: sticky; top: 64px; }
.map-page { position: relative; margin-bottom: 1rem; border: 1px solid var(--border); line-height: 0; }
.map-page img { width: 100%; height: auto; display: block; user-select: none; }
.map-word { position: absolute; cursor: pointer; border-radius: 2px; background: rgba(37, 88, 184, .06); outline: 1px solid rgba(37, 88, 184, .12); }
.map-word:hover { background: rgba(37, 88, 184, .2); }
.map-word.picked { background: color-mix(in srgb, var(--c) 30%, transparent); outline: 2px solid var(--c); }
.map-text { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .85rem; line-height: 1.9; }
.map-word.inline { position: static; padding: 1px 3px; outline: none; }
.map-field { border: 1px solid var(--border); border-left: 5px solid var(--c); border-radius: var(--radius); padding: .5rem .7rem; margin-bottom: .5rem; }
.map-field.active { background: #f3f6fc; box-shadow: 0 0 0 2px color-mix(in srgb, var(--c) 40%, transparent); }
.map-field-head { display: flex; align-items: center; gap: .4rem; margin: 0; cursor: pointer; }
.map-field-head .clear-field { margin-left: auto; color: var(--muted); }
.swatch { width: .8rem; height: .8rem; border-radius: 2px; background: var(--c); display: inline-block; }
.map-value { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .9rem; margin: .25rem 0 0 1.5rem; word-break: break-all; }
.learned { margin: .4rem 0 0 1.5rem; }
.learned label { font-size: .82rem; margin-bottom: .4rem; }
.tpl-miss { background: #fdf1ef; }
@media (max-width: 1000px) { .map-layout { grid-template-columns: 1fr; } .map-side { position: static; } }
.tpl-notes { background: #eef6f0; border: 1px solid #cfe8d6; border-radius: var(--radius); padding: .5rem .7rem; margin-bottom: .6rem; }
