:root {
  --bg: #f5f6fb; --card: #ffffff; --ink: #0f172a; --ink-2: #334155; --muted: #64748b; --line: #e6e8f0; --line-2: #eef0f6;
  --accent: #4f46e5; --accent-2: #7c3aed; --accent-soft: #eef2ff; --side: #f0f2f8;
  --ok: #ecfdf3; --ok-ink: #067647; --ok-dot: #12b76a;
  --bad: #fef3f2; --bad-ink: #b42318; --bad-dot: #f04438;
  --warn: #fffaeb; --warn-ink: #b54708; --warn-dot: #f79009;
  --dup: #f4f3ff; --dup-ink: #5925dc; --dup-dot: #7a5af8;
  --info: #eff8ff; --info-ink: #175cd3; --info-dot: #2e90fa;
  --radius: 14px; --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 8px 24px -12px rgba(16, 24, 40, .12);
  --shadow-lg: 0 12px 32px -12px rgba(79, 70, 229, .35);
}
* { box-sizing: border-box; }
body { margin: 0; color: var(--ink); background: var(--bg); font: 14px/1.55 Inter, system-ui, -apple-system, "Segoe UI", sans-serif; -webkit-font-smoothing: antialiased; }
a { color: var(--accent); text-decoration: none; } a:hover { text-decoration: underline; }
code { font-size: 12px; background: var(--line-2); padding: 1px 6px; border-radius: 6px; word-break: break-all; }
hr { border: 0; border-top: 1px solid var(--line); margin: 18px 0; }

/* ---- layout: sidebar + content (like Streamlit) */
.layout { display: grid; grid-template-columns: 300px 1fr; min-height: 100vh; }
.side { background: var(--side); border-right: 1px solid var(--line); position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 20px 20px 8px; }
.side-body { padding: 4px 20px 24px; }
.side-toggle { display: block; border: 0; box-shadow: none; background: transparent; font-size: 18px; padding: 6px 10px; cursor: pointer; }
.layout { transition: grid-template-columns .2s ease; }
.layout.collapsed { grid-template-columns: 64px 1fr; }
.layout.collapsed .side-body, .layout.collapsed .brand > div { display: none; }
.layout.collapsed .side-head { flex-direction: column; gap: 10px; padding: 20px 8px; }
.side-h { font-size: 15px; margin: 18px 0 8px; text-transform: none; letter-spacing: 0; color: var(--ink); }
.brand { display: flex; gap: 12px; align-items: center; }
.brand b { font-size: 15px; letter-spacing: -.01em; } .brand small { display: block; color: var(--muted); font-size: 12px; }
.logo { width: 36px; height: 36px; border-radius: 11px; display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 16px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); box-shadow: var(--shadow-lg); flex: none; }
.content { min-width: 0; }
.tabs { position: sticky; top: 0; z-index: 10; display: flex; gap: 2px; overflow-x: auto; padding: 0 32px; background: rgba(245, 246, 251, .85);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-bottom: 1px solid var(--line); }
.tabs button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; box-shadow: none; padding: 14px 14px 12px;
  color: var(--muted); font-weight: 500; white-space: nowrap; }
.tabs button:hover { color: var(--ink); background: none; }
.tabs button.on { color: var(--accent); border-bottom-color: var(--accent); }
main { max-width: 1280px; padding: 8px 32px 40px; }
.tab { display: none; animation: fade .2s ease; } .tab.on { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: none; } }
.foot { color: var(--muted); font-size: 12px; padding: 8px 32px 32px; }

/* ---- text */
h1 { font-size: 26px; line-height: 1.2; margin: 24px 0 16px; letter-spacing: -.02em; font-weight: 700; }
h2 { font-size: 17px; margin: 28px 0 10px; letter-spacing: -.01em; }
.card h2:first-child { margin-top: 0; }
.caption, .muted { color: var(--muted); } .caption { font-size: 12.5px; margin: 6px 0; }
.small { font-size: 12px; } .nowrap { white-space: nowrap; } .wide-cell { min-width: 260px; }
.label { font-size: 13px; font-weight: 500; margin-bottom: 6px; }
ul.list { margin: 8px 0; padding-left: 20px; } ul.list li { margin: 3px 0; }
pre.code { background: #f7f8fb; border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; font-size: 12px; overflow-x: auto; margin: 8px 0; }

/* ---- controls */
button, select, input { font: inherit; color: var(--ink); }
button { border: 1px solid var(--line); background: #fff; padding: 8px 14px; border-radius: 10px; cursor: pointer; font-weight: 500;
  transition: all .15s; box-shadow: 0 1px 2px rgba(16, 24, 40, .05); }
button:hover { border-color: #cfd4e2; background: #fafbff; }
button.primary { color: #fff; border: 0; background: linear-gradient(135deg, var(--accent), var(--accent-2)); box-shadow: var(--shadow-lg); padding: 9px 18px; }
button.primary:hover { filter: brightness(1.06); }
button:disabled { opacity: .5; cursor: default; filter: none; }
.small-btn { padding: 4px 10px; font-size: 12px; }
.wide-btn { width: 100%; margin-top: 14px; }
input[type=text], input[type=number], input[type=date], select { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; background: #fff; width: 100%; }
input[type=file] { font-size: 12px; max-width: 100%; }
select.wide { max-width: 680px; }
input:focus, select:focus, button:focus-visible { outline: 3px solid #c7d2fe; outline-offset: 1px; }
.field { display: block; font-size: 13px; font-weight: 500; margin: 12px 0; max-width: 680px; }
.field > input, .field > select, .field > .seg, .field > .row { margin-top: 6px; }
.field > small { display: block; font-weight: 400; margin-top: 6px; font-size: 12px; }
.field .row input { flex: 1; min-width: 200px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; margin: 10px 0; cursor: pointer; }
.seg { display: flex; padding: 3px; background: #fff; border: 1px solid var(--line); border-radius: 10px; gap: 3px; }
.seg button { flex: 1; border: 0; box-shadow: none; padding: 6px 8px; font-size: 12.5px; border-radius: 8px; color: var(--muted); }
.seg button.on { background: var(--accent-soft); color: var(--accent); }
.seg button:disabled { opacity: 1; cursor: default; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.cols2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 16px; margin: 12px 0; }
.cols3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; margin: 14px 0; padding: 4px 18px;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.drop { display: flex; flex-direction: column; align-items: center; gap: 6px; border: 1.5px dashed #c7cbe0; border-radius: 14px; padding: 26px;
  text-align: center; color: var(--muted); cursor: pointer; margin: 12px 0; background: linear-gradient(180deg, #fafbff, #f6f7fd); transition: all .15s; }
.drop:hover, .drop.over { border-color: var(--accent); background: var(--accent-soft); }
.drop b { color: var(--ink); } .drop input { display: none; }
.drop-icon { width: 28px; height: 28px; fill: none; stroke: var(--accent); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
  background: #fff; border-radius: 10px; padding: 6px; box-sizing: content-box; box-shadow: var(--shadow); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border-radius: 99px; padding: 4px 12px; font-size: 12px; box-shadow: none; color: var(--muted); }
.chip.on { background: var(--accent-soft); color: var(--accent); border-color: #c7d2fe; }
.file-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--line-2); }
.file-row:last-child { border-bottom: 0; }
.export-list { margin-top: 16px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 18px; box-shadow: var(--shadow); }
.export-list .file-row { padding: 12px 0; }
.doc-buttons { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.doc-buttons p { word-break: break-all; }

/* ---- cards, alerts, expanders */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; margin: 14px 0; box-shadow: var(--shadow); }
.alert { border-radius: 10px; padding: 11px 14px; margin: 10px 0; border: 1px solid; font-size: 13.5px; }
.alert-success { background: var(--ok); color: var(--ok-ink); border-color: #abefc6; }
.alert-error { background: var(--bad); color: var(--bad-ink); border-color: #fecdca; }
.alert-warning { background: var(--warn); color: var(--warn-ink); border-color: #fedf89; }
.alert-info { background: var(--info); color: var(--info-ink); border-color: #b2ddff; }
details.exp { background: var(--card); border: 1px solid var(--line); border-radius: 12px; margin: 10px 0; box-shadow: 0 1px 2px rgba(16, 24, 40, .04); }
details.exp > summary { cursor: pointer; padding: 11px 16px; font-weight: 500; list-style: none; display: flex; align-items: center; gap: 8px; }
details.exp > summary::before { content: "›"; display: inline-block; transition: transform .15s; color: var(--muted); font-size: 18px; line-height: 1; }
details.exp[open] > summary::before { transform: rotate(90deg); }
details.exp > summary::-webkit-details-marker { display: none; }
.exp-body { padding: 0 16px 14px; }

/* ---- tables */
.tablewrap { overflow: auto; background: var(--card); border: 1px solid var(--line); border-radius: 12px; margin: 8px 0; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line-2); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
th { font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; background: #fafbfd; position: sticky; top: 0; z-index: 1; white-space: nowrap; }
td.num, th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
table.oneline td { white-space: nowrap; max-width: 340px; overflow: hidden; text-overflow: ellipsis; }
table.oneline td .badge { vertical-align: middle; }
table.clickable tr[data-case] { cursor: pointer; } table.clickable tr[data-case]:hover td { background: #f8f8ff; }
td input[type=number], td input:not([type]) { padding: 5px 8px; border: 1px solid var(--line); border-radius: 8px; width: 100%; min-width: 80px; }
td select { padding: 5px 8px; }

/* ---- badges */
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 99px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .8; }
.s-matched, .r-PASS, .st-extracted { background: var(--ok); color: var(--ok-ink); }
.s-mismatch, .r-FAIL, .st-error { background: var(--bad); color: var(--bad-ink); }
.s-review, .r-REVIEW, .st-ready, .st-detected { background: var(--warn); color: var(--warn-ink); }
.s-duplicate, .r-DUPLICATE { background: var(--dup); color: var(--dup-ink); }
.r-INFO, .r-SKIP, .st-uploaded { background: var(--info); color: var(--info-ink); }
.tag-new { display: inline-block; padding: 1px 7px; border-radius: 99px; font-size: 11px; font-weight: 600; background: var(--accent-soft); color: var(--accent); }

/* ---- invoice detail: document comparison */
table.compare td { vertical-align: top; font-size: 13px; }
table.compare td.cmp-field { min-width: 140px; max-width: 200px; }
table.compare tr.row-bad td { background: var(--bad); }
table.compare tr.row-warn td { background: var(--warn); }
/* ---- overview filters, extracted data per document */
.filters { display: grid; grid-template-columns: 1.6fr 1.3fr 1.3fr 1fr 1fr 1fr; gap: 12px; margin-top: 8px; }
.filters .field { margin: 0 0 12px; max-width: none; }
.extracted-doc h3 { font-size: 14px; margin: 14px 0 6px; } .extracted-doc h3 .caption { font-weight: 400; }

/* ---- metrics, bars, progress */
.metrics { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin: 12px 0; }
.metric { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); position: relative; overflow: hidden; }
.metric::after { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: linear-gradient(var(--accent), var(--accent-2)); }
.m6 .metric:nth-child(2)::after { background: var(--ok-dot); } .m6 .metric:nth-child(3)::after { background: var(--bad-dot); }
.m6 .metric:nth-child(4)::after { background: var(--warn-dot); } .m6 .metric:nth-child(5)::after { background: var(--dup-dot); }
.m6 .metric:nth-child(6)::after { background: var(--info-dot); }
.metric span { color: var(--muted); font-size: 12px; font-weight: 500; display: block; }
.metric b { font-size: 22px; letter-spacing: -.02em; display: block; margin-top: 2px; font-variant-numeric: tabular-nums; word-break: break-word; }
.metric small { display: block; color: var(--muted); font-size: 12px; }
.bars { display: flex; flex-direction: column; gap: 10px; }
.bar-row { display: grid; grid-template-columns: 130px 1fr 92px; gap: 10px; align-items: center; font-size: 13px; }
.bar-row b { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
.bar-track { height: 14px; background: var(--line-2); border-radius: 99px; overflow: hidden; }
.bar-track i { display: block; height: 100%; border-radius: 99px; background: linear-gradient(90deg, var(--accent), var(--accent-2)); }
.progress { margin: 12px 0; } .progress .bar { height: 8px; background: var(--line-2); border-radius: 99px; overflow: hidden; }
.progress i { display: block; height: 100%; border-radius: 99px; transition: width .4s ease;
  background: linear-gradient(90deg, var(--accent), var(--accent-2), var(--accent)); background-size: 200% 100%; animation: flow 1.6s linear infinite; }
@keyframes flow { to { background-position: -200% 0; } }
.progress span { display: block; margin-top: 6px; font-size: 12px; color: var(--muted); }

#toast { position: fixed; right: 24px; bottom: 24px; max-width: 440px; background: rgba(15, 23, 42, .94); color: #fff; padding: 14px 18px;
  border-radius: 14px; box-shadow: 0 20px 40px -12px rgba(15, 23, 42, .45); z-index: 30; animation: fade .2s ease; }
#toast.err { background: rgba(180, 35, 24, .95); }
[hidden] { display: none !important; }

/* ---- small screens: sidebar folds into a panel above the tabs */
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .side { position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--line); }
  .layout.collapsed { grid-template-columns: 1fr; }
  .layout.collapsed .brand > div { display: block; }
  .layout.collapsed .side-head { flex-direction: row; padding: 20px 20px 8px; }
  .side-body { display: none; } .side.open .side-body { display: block; }
  .tabs { padding: 0 12px; } main { padding: 4px 16px 32px; } .foot { padding: 8px 16px 24px; }
  h1 { font-size: 22px; } .bar-row { grid-template-columns: 100px 1fr 80px; }
  .filters { grid-template-columns: 1fr; }
}

/* ---- bundles: where an invoice came from, what happened to each document of its file */
.origin { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; display: grid; gap: 4px; }
.origin-step { padding-left: 18px; } .origin-step2 { padding-left: 36px; }
.o-used, .o-read { background: var(--ok); color: var(--ok-ink); }
.o-unlinked, .o-ready { background: var(--info); color: var(--info-ink); }
.o-held { background: var(--line-2); color: var(--ink-2); }
.o-review { background: var(--warn); color: var(--warn-ink); }
/* how legible a document is (scan quality / fields Gemini could not read) */
.q-poor { background: var(--info); color: var(--info-ink); }
.q-unreadable { background: var(--warn); color: var(--warn-ink); }

/* ---- overview: 3-way amounts (PO / Contract, GR, invoice) */
td.diff-ok { color: var(--ok-ink); } td.diff-over { color: var(--bad-ink); font-weight: 600; } td.diff-under { color: var(--warn-ink); font-weight: 600; }
