:root {
  --green: #0a8f5a;
  --green-dark: #08734a;
  --green-soft: #eaf6f0;
  --red: #d7191f;
  --ink: #1d2327;
  --muted: #64707a;
  --line: #dde3e7;
  --bg: #f4f6f7;
  --card: #fff;
  --warn: #9a6700;
  --warn-bg: #fff6dc;
  --radius: 10px;
  font-size: 16px;
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; color: var(--ink); background: var(--bg); }
a { color: var(--green-dark); }
h1 { font-size: 1.55rem; margin: .3rem 0 1rem; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3 { font-size: .95rem; margin: .9rem 0 .4rem; color: var(--green-dark); }
code { font-size: .85em; background: #eef1f3; padding: 0 .25em; border-radius: 3px; }
[hidden] { display: none !important; }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .5rem 1.25rem; background: #fff; border-bottom: 3px solid var(--green); position: sticky; top: 0; z-index: 20; }
.brand { display: flex; align-items: center; gap: .6rem; text-decoration: none; color: var(--ink); font-weight: 600; }
.brand img { height: 34px; }
.topbar nav { display: flex; align-items: center; gap: 1rem; }
.topbar nav a { text-decoration: none; color: var(--ink); padding: .3rem .1rem; border-bottom: 2px solid transparent; }
.topbar nav a[aria-current] { border-color: var(--red); font-weight: 600; }

.container { max-width: 1100px; margin: 0 auto; padding: 1rem 1.25rem 4rem; }
.container.wide { max-width: 1240px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.15rem; margin-bottom: 1rem; box-shadow: 0 1px 2px rgba(0,0,0,.03); }
.card.narrow { max-width: 640px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: .75rem; flex-wrap: wrap; margin-bottom: .5rem; }
.card-head h2 { margin: 0; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hint { display: block; font-size: .8rem; color: var(--muted); margin-top: .2rem; font-weight: normal; }
.error-text { color: var(--red); display: block; }
.inline { display: inline; }
.inline-form { display: flex; gap: .5rem; flex-wrap: wrap; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: .3rem; min-height: 38px; padding: .4rem .9rem; border: 1px solid #c9d1d6; border-radius: 8px; background: #fff; color: var(--ink); font: inherit; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { border-color: var(--green); }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--green-dark); }
.btn.danger { color: var(--red); border-color: #efb9bb; }
.btn.small { min-height: 30px; padding: .2rem .6rem; font-size: .85rem; }
.btn.icon { min-width: 30px; padding: .2rem .45rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
button.link { background: none; border: 0; color: var(--ink); cursor: pointer; font: inherit; padding: 0; }

input, select, textarea { font: inherit; padding: .45rem .55rem; border: 1px solid #c9d1d6; border-radius: 7px; background: #fff; color: var(--ink); width: 100%; min-height: 38px; }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(10,143,90,.35); border-color: var(--green); }
input.invalid, input.server-invalid { border-color: var(--red); background: #fff3f3; }
input[type=checkbox], input[type=radio] { width: auto; min-height: 0; }
input.tiny { width: 5rem; }
label { display: block; font-weight: 600; font-size: .88rem; }
label > input, label > select, label > textarea { margin-top: .25rem; font-weight: normal; }
label.check { display: flex; align-items: center; gap: .45rem; font-weight: normal; margin: .3rem 0; }
fieldset { border: 1px solid var(--line); border-radius: 8px; padding: .6rem .8rem .8rem; margin: .5rem 0; }
legend { font-weight: 600; font-size: .9rem; padding: 0 .3rem; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .8rem 1rem; }
.form-grid > .span-2 { grid-column: span 2; }
.side-pair fieldset { margin: 0; }
.side-pair fieldset label { margin-bottom: .5rem; }
.form-actions { margin-top: 1rem; display: flex; gap: .5rem; flex-wrap: wrap; }
.form-actions.sticky { position: sticky; bottom: 0; background: var(--bg); padding: .6rem 0; }
form.card > label, .card.narrow label { margin-bottom: .8rem; }

.messages { margin-bottom: 1rem; }
.msg { padding: .6rem .85rem; border-radius: 8px; margin-bottom: .6rem; border: 1px solid; }
.msg ul { margin: .3rem 0 0 1.1rem; padding: 0; }
.msg-success { background: var(--green-soft); border-color: #b5dcc8; }
.msg-info { background: #eef5fb; border-color: #c6dcef; }
.msg-warning { background: var(--warn-bg); border-color: #ecd28a; color: #5b4300; }
.msg-error { background: #fdeeee; border-color: #f1b9bb; color: #8a1014; }
.msg summary { cursor: pointer; }

.badge { display: inline-block; font-size: .72rem; font-weight: 600; padding: .1rem .45rem; border-radius: 999px; background: #eef1f3; color: #3e4a52; vertical-align: middle; }
.badge.ok, .badge.state-submitted { background: var(--green-soft); color: var(--green-dark); }
.badge.warn, .badge.state-queued, .badge.state-sending { background: var(--warn-bg); color: var(--warn); }
.badge.state-failed { background: #fdeeee; color: var(--red); }
.badge.state-unknown { background: #f3e8ff; color: #6b21a8; }
.badge.edited { background: #e8f0fe; color: #1a56c4; }
.chip { display: inline-block; background: #eef1f3; border-radius: 999px; padding: .1rem .55rem; margin: 0 .25rem .25rem 0; }

/* Dashboard */
.toolbar { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: 1rem; align-items: center; }
.toolbar input[type=search] { flex: 1 1 260px; width: auto; }
.toolbar select { width: auto; }
.segmented { display: inline-flex; border: 1px solid #c9d1d6; border-radius: 8px; overflow: hidden; }
.segmented a, .segmented button { padding: .45rem .9rem; text-decoration: none; color: var(--ink); background: #fff; border: 0; font: inherit; cursor: pointer; }
.segmented .active { background: var(--green); color: #fff; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 1rem; }
.listing-card { display: flex; flex-direction: column; margin: 0; position: relative; }
.listing-card h2 { font-size: 1.05rem; margin-bottom: .5rem; }
.listing-card a.stretched { text-decoration: none; color: var(--ink); }
.listing-card dl { display: grid; grid-template-columns: auto 1fr; gap: .15rem .7rem; margin: 0 0 .75rem; font-size: .9rem; }
.listing-card dt { color: var(--muted); }
.listing-card dd { margin: 0; }
.listing-card footer { margin-top: auto; display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.listing-card footer form { margin-left: auto; }
.empty { text-align: center; color: var(--muted); padding: 2rem; }
.table-wrap { overflow-x: auto; }
table.simple, table.listing-table { width: 100%; border-collapse: collapse; }
table.simple th, table.simple td, table.listing-table th, table.listing-table td { text-align: left; padding: .5rem .55rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
td.num, th.num { text-align: right; }
td.actions { white-space: nowrap; text-align: right; }
tr.inactive td { opacity: .6; }
.settings-grid .link-card { text-decoration: none; color: var(--ink); }
.settings-grid .link-card:hover { border-color: var(--green); }
.settings-grid p { margin: 0; color: var(--muted); }

/* Login */
.login-page main { display: grid; place-items: center; min-height: 100vh; }
.login-card { width: min(380px, 92vw); text-align: center; }
.login-card label { text-align: left; margin-bottom: .8rem; }
.login-logo { width: 200px; margin: .5rem auto 0; }
.login-card h1 { font-size: 1.2rem; }

/* Editor */
.editor-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; flex-wrap: wrap; position: sticky; top: 53px; z-index: 10; background: var(--bg); padding: .4rem 0 .6rem; }
.editor-head h1 { margin: 0; }
.editor-actions { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.save-status { font-size: .85rem; padding: .2rem .6rem; border-radius: 999px; background: #eef1f3; }
.save-status.ok { background: var(--green-soft); color: var(--green-dark); }
.save-status.ok::before { content: "✓ "; }
.save-status.pending { background: var(--warn-bg); color: var(--warn); }
.save-status.error, .save-status.error-soft { background: #fdeeee; color: var(--red); font-weight: 600; }
.warnings .msg { font-size: .9rem; }
.names .label { display: block; font-weight: 600; font-size: .88rem; margin-bottom: .25rem; }
.name-row { display: flex; gap: .35rem; margin-bottom: .35rem; }

.fee-header, .fee-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr) minmax(0, 1fr); gap: .6rem; align-items: start; }
.fee-header { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: .2rem .4rem; border-bottom: 2px solid var(--green); }
.fee-header span:nth-child(2) { text-align: center; }
.fee-header span:nth-child(3) { text-align: right; }
.fee-section h3 { text-align: center; background: var(--green-soft); padding: .3rem; border-radius: 6px; margin: .9rem 0 .3rem; }
.fee-row { padding: .35rem .4rem; border-bottom: 1px solid var(--line); }
.fee-row.provisional { background: #fffaf0; }
.fee-row.provisional .fee-name::after { content: "Provisional (transaction undecided)"; font-size: .72rem; color: var(--warn); }
.fee-name { display: flex; flex-direction: column; gap: .2rem; }
.fee-name .fee-label { text-align: center; font-weight: 600; border-color: transparent; background: transparent; }
.fee-name .fee-label:hover, .fee-name .fee-label:focus { border-color: #c9d1d6; background: #fff; }
.fee-meta { display: flex; justify-content: center; align-items: center; gap: .3rem; flex-wrap: wrap; }
.fee-side { display: flex; flex-direction: column; }
.fee-side input { text-align: right; font-variant-numeric: tabular-nums; }
.fee-side .side-label { display: none; }
.side-method { background: none; border: 0; padding: .1rem 0; font-size: .74rem; color: var(--muted); cursor: pointer; text-align: right; text-decoration: underline dotted; }
.fee-side.is-override input { background: #f2f7ff; border-color: #9cbcf0; }
.fee-side.is-override .side-method { color: #1a56c4; }
.fee-side[data-status="incomplete"] input { border-color: #e2b13c; background: var(--warn-bg); }
.side-seller input, .side-seller .side-method { text-align: left; }

.totals { margin-top: .8rem; border-top: 2px solid var(--ink); }
.tline { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr) minmax(0, 1fr); gap: .6rem; align-items: center; padding: .35rem .4rem; border-bottom: 1px solid var(--line); }
.tline > span { text-align: center; font-weight: 600; }
.tline output { font-weight: 600; font-variant-numeric: tabular-nums; }
.tcell .side-label { display: none; }
.t-buyer, .t-buyer input, .t-buyer output { text-align: right; }
.tline.grand { border-bottom: 3px double var(--ink); font-size: 1.05rem; }

.adj-group { border-top: 1px solid var(--line); margin-top: .7rem; }
.adj-row { display: grid; grid-template-columns: minmax(0, 1.3fr) minmax(0, 2fr) minmax(0, 1fr) auto; gap: .4rem; margin-bottom: .5rem; }
.adj-row input:nth-of-type(2) { text-align: right; }
#adjustments { margin: .9rem 0 .6rem; }

.results .result-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.result { border: 1px solid var(--line); border-radius: 8px; padding: .8rem 1rem; display: flex; flex-direction: column; gap: .25rem; }
.result .rlabel { color: var(--muted); font-size: .9rem; }
.result strong { font-size: 1.6rem; font-variant-numeric: tabular-nums; }
.result strong.negative { color: var(--red); }
.result strong.incomplete { font-size: 1rem; color: var(--warn); }
.result .badge { align-self: flex-start; }
.disclaimer { font-size: .8rem; color: var(--muted); margin: .8rem 0 0; }

dialog.dialog { border: 0; border-radius: 12px; padding: 1.2rem 1.3rem; width: min(520px, 94vw); max-height: 90vh; overflow: auto; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
dialog.dialog.wide { width: min(780px, 96vw); }
dialog::backdrop { background: rgba(20,30,35,.45); }
dialog label { margin-bottom: .7rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: .5rem; flex-wrap: wrap; margin-top: 1rem; }
.dialog-actions.stack { flex-direction: column; align-items: stretch; }
.tabs { margin-bottom: .8rem; }
.rec-group { margin-bottom: .6rem; }
.rec-item { align-items: flex-start !important; padding: .35rem .2rem; border-bottom: 1px solid var(--line); }
.rec-item.edited { background: #fffaf0; }
.rec-item input { margin-top: .25rem; }
.diff ul { padding-left: 1.1rem; max-height: 40vh; overflow: auto; }
.checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
#company-checklist ul, #company-checklist div { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
#company-checklist label { font-weight: normal; display: flex; gap: .4rem; align-items: center; }
.override-company { border-top: 1px solid var(--line); padding-top: .4rem; }
.override legend label { margin: 0; font-weight: 600; }
.section-form { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.section-form .section-name { width: 14rem; font-weight: 600; }
.preview-list { display: grid; grid-template-columns: auto 1fr; gap: .5rem 1rem; }
.preview-list dt { font-weight: 600; }
.preview-list dd { margin: 0; }
.body-preview { white-space: pre-wrap; font: inherit; margin: 0; background: #f7f9fa; padding: .6rem; border-radius: 6px; }

@media (max-width: 760px) {
  .container { padding: .75rem .75rem 4rem; }
  .topbar { padding: .45rem .75rem; }
  .brand span { display: none; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid > .span-2 { grid-column: auto; }
  .editor-head { position: static; }
  .editor-actions { width: 100%; }
  .editor-actions .btn { flex: 1 1 auto; }
  .fee-header { display: none; }
  .fee-row { grid-template-columns: 1fr 1fr; grid-template-areas: "name name" "seller buyer"; border: 1px solid var(--line); border-radius: 8px; margin-bottom: .5rem; background: #fff; }
  .fee-row .fee-name { grid-area: name; }
  .fee-row .fee-label { justify-content: flex-start; padding-left: 0; }
  .fee-row .side-seller { grid-area: seller; }
  .fee-row .side-buyer { grid-area: buyer; }
  .fee-side .side-label { display: block; font-size: .75rem; font-weight: 600; color: var(--muted); }
  .side-seller input, .side-seller .side-method { text-align: right; }
  .tline { grid-template-columns: 1fr 1fr; grid-template-areas: "label label" "seller buyer"; }
  .tline > span { grid-area: label; }
  .tline .t-seller { grid-area: seller; }
  .tline .t-buyer { grid-area: buyer; }
  .tcell .side-label { display: block; font-size: .75rem; color: var(--muted); font-weight: 600; }
  .t-seller, .t-seller input { text-align: right; }
  .results .result-grid { grid-template-columns: 1fr; }
  .adj-row { grid-template-columns: 1fr 7rem auto; }
  .adj-row select { grid-column: 1 / -1; }
  .question { flex-direction: column; align-items: stretch; }
  .question .segmented { display: flex; }
  .question .segmented button { flex: 1; }
  .dialog-actions.split { flex-direction: column; }
  table.simple thead, table.listing-table thead { display: none; }
  table.simple tr, table.listing-table tr { display: block; border-bottom: 1px solid var(--line); padding: .4rem 0; }
  table.simple td, table.listing-table td { display: flex; justify-content: space-between; gap: 1rem; border: 0; padding: .2rem .3rem; text-align: right; }
  table.simple td[data-label]::before, table.listing-table td[data-label]::before { content: attr(data-label); color: var(--muted); font-size: .8rem; text-align: left; }
  td.actions { justify-content: flex-end; }
  .preview-list { grid-template-columns: 1fr; }
}
.fee-badges { display: inline-flex; gap: .3rem; flex-wrap: wrap; justify-content: center; }

/* Simplified editor */
.linklike { background: none; border: 0; padding: 0; color: var(--green-dark); text-decoration: underline; cursor: pointer; font: inherit; }
.fee-help { margin: -.2rem 0 .6rem; }
.fee-name { justify-content: center; }
.fee-label { display: flex; align-items: center; justify-content: center; gap: .35rem; width: 100%; min-height: 38px; background: none; border: 1px solid transparent; border-radius: 7px; font: inherit; font-weight: 600; color: var(--ink); cursor: pointer; padding: .3rem .5rem; }
.fee-label:hover, .fee-label:focus-visible { border-color: #c9d1d6; background: #fff; }
.fee-label .edit-icon { color: var(--muted); font-weight: normal; font-size: .85rem; opacity: .55; }
.fee-label:hover .edit-icon { opacity: 1; }
.side-hint { font-size: .74rem; color: var(--muted); min-height: 1em; padding-top: .1rem; }
.side-seller .side-hint { text-align: left; }
.side-buyer .side-hint { text-align: right; }
.fee-row.provisional .fee-name::after { content: "Only if financed"; font-size: .72rem; color: var(--warn); text-align: center; }
.todo { display: flex; flex-wrap: wrap; align-items: center; gap: .35rem; }
.chip.linklike { background: #fff; border: 1px solid #ecd28a; border-radius: 999px; padding: .1rem .6rem; text-decoration: none; color: #5b4300; }
.chip.linklike:hover { border-color: var(--warn); }
.questions { display: flex; flex-direction: column; gap: .6rem; }
.question { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; }
.answer button { min-width: 4.5rem; }
.answer button.active { background: var(--green); color: #fff; }
.archive-row { margin: 0 0 1rem; text-align: right; }
.dialog-actions.split { justify-content: space-between; }
.dialog-actions.split > span { display: flex; gap: .4rem; flex-wrap: wrap; }
.per-unit { display: flex; align-items: center; gap: .35rem; flex-wrap: wrap; margin-bottom: .5rem; font-weight: 600; font-size: .88rem; }
.per-unit input.short { width: 6rem; }
.per-unit .hint { flex-basis: 100%; }
.side-pair fieldset select { margin-bottom: .5rem; }
