:root {
  --primary: #0F4C81; --primary-600: #0b3d68; --primary-50: #eef4fb;
  --accent: #C8A24A;
  --bg: #f4f6f9; --surface: #ffffff; --border: #e3e7ed; --border-strong: #cfd5de;
  --text: #1d2939; --muted: #667085; --faint: #98a2b3;
  --ok: #067647; --ok-bg: #ecfdf3; --warn: #b54708; --warn-bg: #fffaeb; --bad: #b42318; --bad-bg: #fef3f2;
  --info: #175cd3; --info-bg: #eff8ff;
  --radius: 10px; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.45; }
a { color: var(--primary); }
#app { display: flex; min-height: 100vh; }

/* sidebar */
#sidebar { width: 228px; flex: 0 0 228px; background: #0c2340; color: #cdd8e6; display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { display: flex; gap: 10px; align-items: center; padding: 18px 16px 14px; border-bottom: 1px solid rgba(255,255,255,.08); }
.brand-mark { width: 36px; height: 36px; border-radius: 9px; background: var(--accent); color: #0c2340; font-weight: 800;
  display: grid; place-items: center; }
.brand-name { color: #fff; font-weight: 650; max-width: 150px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-sub { font-size: 11.5px; color: #8da2bd; }
nav { padding: 10px 8px; display: flex; flex-direction: column; gap: 2px; }
nav a { color: #cdd8e6; text-decoration: none; padding: 9px 10px; border-radius: 8px; display: flex; gap: 10px; align-items: center; }
nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
nav a.active { background: rgba(255,255,255,.12); color: #fff; font-weight: 600; }
nav .ic { width: 20px; text-align: center; }
.nav-sep { height: 1px; background: rgba(255,255,255,.08); margin: 8px 6px; }
.side-foot { margin-top: auto; padding: 14px 16px; font-size: 11.5px; color: #8da2bd; }

#main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#topbar { display: flex; align-items: center; gap: 12px; padding: 14px 24px; background: var(--surface);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20; flex-wrap: wrap; }
#topbar h1 { font-size: 19px; margin: 0; font-weight: 650; flex: 1; min-width: 160px; }
#pageActions { display: flex; gap: 8px; flex-wrap: wrap; }
#menuBtn { display: none; }
#view { padding: 22px 24px 60px; max-width: 1500px; width: 100%; }

/* basics */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 16px; }
.card h2 { font-size: 15px; margin: 0 0 12px; font-weight: 650; display: flex; align-items: center; gap: 8px; }
.card h2 .sub { font-weight: 400; color: var(--muted); font-size: 12.5px; }
.card.internal { border: 1.5px dashed #f04438; background: #fffbfa; }
.muted { color: var(--muted); } .small { font-size: 12.5px; } .right { text-align: right; } .nowrap { white-space: nowrap; }
.grid { display: grid; gap: 12px 14px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); } .g6 { grid-template-columns: repeat(6, minmax(0, 1fr)); }
.span2 { grid-column: span 2; } .span3 { grid-column: span 3; } .spanall { grid-column: 1 / -1; }
label.f, div.f { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); font-weight: 500; min-width: 0; }
label.f.req > span:first-child::after { content: " *"; color: var(--bad); }
input, select, textarea { font: inherit; font-size: 13.5px; color: var(--text); background: #fff; border: 1px solid var(--border-strong);
  border-radius: 7px; padding: 7px 9px; width: 100%; min-width: 0; }
textarea { resize: vertical; min-height: 64px; }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(15,76,129,.25); border-color: var(--primary); }
input:disabled, select:disabled, textarea:disabled { background: #f7f8fa; color: #475467; }
input.num { text-align: right; font-variant-numeric: tabular-nums; }
.invalid { border-color: var(--bad) !important; background: var(--bad-bg) !important; }
input[type=checkbox] { width: auto; }
.check { display: flex; gap: 8px; align-items: center; font-size: 13.5px; color: var(--text); }
.check > input[type=checkbox] { flex: none; }

.btn { font: inherit; font-size: 13.5px; border: 1px solid var(--border-strong); background: #fff; color: var(--text); border-radius: 8px;
  padding: 7px 13px; cursor: pointer; display: inline-flex; gap: 6px; align-items: center; text-decoration: none; white-space: nowrap; }
.btn:hover { background: #f7f8fa; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-600); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.danger { color: var(--bad); border-color: #fda29b; }
.btn.danger:hover { background: var(--bad-bg); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { padding: 4px 9px; font-size: 12.5px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { border: none; background: none; cursor: pointer; padding: 4px 6px; border-radius: 6px; color: var(--muted); font-size: 14px; }
.icon-btn:hover { background: #eef1f5; color: var(--text); }
.icon-btn.del:hover { background: var(--bad-bg); color: var(--bad); }

/* tables */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; }
table.data th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600;
  padding: 9px 10px; border-bottom: 1px solid var(--border); background: #fafbfc; white-space: nowrap; user-select: none; }
table.data th.sortable { cursor: pointer; } table.data th.sortable:hover { color: var(--text); }
table.data td { padding: 10px; border-bottom: 1px solid #eef0f3; vertical-align: top; }
table.data tr.click { cursor: pointer; } table.data tr.click:hover td { background: #f8fafc; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
.empty { padding: 40px 10px; text-align: center; color: var(--muted); }

.badge { display: inline-block; font-size: 11.5px; font-weight: 600; padding: 2px 9px; border-radius: 99px; background: #f2f4f7; color: #344054; white-space: nowrap; }
.badge.draft { background: #f2f4f7; color: #475467; } .badge.issued, .badge.sent { background: var(--info-bg); color: var(--info); }
.badge.paid, .badge.accepted, .badge.converted { background: var(--ok-bg); color: var(--ok); }
.badge.partially_paid, .badge.expired { background: var(--warn-bg); color: var(--warn); }
.badge.overdue, .badge.cancelled, .badge.declined, .badge.void { background: var(--bad-bg); color: var(--bad); }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; align-items: end; }
.toolbar .grow { flex: 1; min-width: 200px; }
.toolbar label.f { min-width: 130px; }

/* dashboard */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat .l { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.stat .v { font-size: 21px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; word-break: break-word; }
.stat .s { font-size: 12px; color: var(--muted); margin-top: 2px; }
.stat.hl { border-left: 4px solid var(--primary); } .stat.warn { border-left: 4px solid var(--warn); } .stat.bad { border-left: 4px solid var(--bad); } .stat.ok { border-left: 4px solid var(--ok); }
.cur-head { font-weight: 650; margin: 6px 0 8px; color: var(--primary); }
.two-col { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }
.activity { list-style: none; padding: 0; margin: 0; }
.activity li { padding: 7px 0; border-bottom: 1px solid #f0f2f5; font-size: 13px; display: flex; gap: 10px; }
.activity .t { color: var(--faint); white-space: nowrap; font-size: 12px; min-width: 112px; }
.banner { padding: 11px 14px; border-radius: 9px; margin-bottom: 14px; font-size: 13.5px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.banner.warn { background: var(--warn-bg); border: 1px solid #fedf89; color: #7a2e0e; }
.banner.info { background: var(--info-bg); border: 1px solid #b2ddff; color: #194185; }
.banner.bad { background: var(--bad-bg); border: 1px solid #fecdca; color: #7a271a; }
.banner.ok { background: var(--ok-bg); border: 1px solid #abefc6; color: #054f31; }

/* editor */
.editor { display: grid; grid-template-columns: minmax(0, 1fr) 330px; gap: 16px; align-items: start; }
.summary { position: sticky; top: 76px; }
.summary .row { display: flex; justify-content: space-between; gap: 10px; padding: 5px 0; font-size: 13.5px; font-variant-numeric: tabular-nums; }
.summary .row.total { background: var(--primary); color: #fff; margin: 6px -8px; padding: 9px 8px; border-radius: 7px; font-weight: 700; font-size: 15px; }
.summary .row.balance { background: #fef0c7; margin: 6px -8px; padding: 8px; border-radius: 7px; font-weight: 700; border: 1px solid var(--accent); }
.summary .row.balance.zero { background: var(--ok-bg); border-color: #abefc6; }
.summary .row.sub { color: var(--muted); font-size: 12.5px; }
.summary .actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.summary .actions .btn { justify-content: center; }
.summary .actions .wide { grid-column: span 2; }
.errors { background: var(--bad-bg); border: 1px solid #fecdca; color: #7a271a; border-radius: 8px; padding: 9px 12px; font-size: 12.5px; margin-top: 10px; }
.errors ul { margin: 4px 0 0; padding-left: 18px; }

.items { display: flex; flex-direction: column; gap: 10px; }
.item { border: 1px solid var(--border); border-radius: 9px; padding: 10px; background: #fcfcfd; }
.item-row { display: grid; gap: 8px; grid-template-columns: 26px minmax(110px, 1.1fr) minmax(170px, 2.4fr) minmax(95px, 1fr) 70px 70px 110px 120px; align-items: end; }
.item-row2 { display: grid; gap: 8px; grid-template-columns: 26px minmax(150px, 2.5fr) 82px 92px minmax(90px, 1fr) 72px 84px auto; align-items: end; margin-top: 8px; }
.tpl-card { border: 1px solid var(--border); border-radius: 10px; padding: 10px; background: #fff; cursor: pointer; display: flex; flex-direction: column; gap: 8px; }
.tpl-card.active { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary); }
.tpl-mock { border: 1px solid #e4e7ec; border-radius: 6px; overflow: hidden; background: #fff; height: 128px; }
.tpl-card .btn { align-self: flex-start; }
.item .idx { font-weight: 700; color: var(--muted); padding-bottom: 8px; text-align: center; }
.item .line-total { text-align: right; font-weight: 700; padding: 7px 4px; font-variant-numeric: tabular-nums; }
.item .line-meta { font-size: 11.5px; color: var(--muted); text-align: right; }
.item .tools { display: flex; gap: 2px; justify-content: flex-end; padding-bottom: 4px; }
.item textarea { min-height: 36px; height: 36px; }
.rows { display: flex; flex-direction: column; gap: 8px; }
.mini-row { display: grid; gap: 8px; align-items: end; }
.add-row { margin-top: 10px; display: flex; gap: 8px; flex-wrap: wrap; }
details.sect > summary { cursor: pointer; font-weight: 650; font-size: 15px; list-style: none; display: flex; gap: 8px; align-items: center; }
details.sect > summary::before { content: "▸"; color: var(--muted); }
details.sect[open] > summary::before { content: "▾"; }
details.sect > summary .sub { font-weight: 400; color: var(--muted); font-size: 12.5px; }
details.sect > .body { margin-top: 12px; }
.locked-note { display: flex; gap: 10px; align-items: center; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; font-size: 13.5px; }
.kv .k { color: var(--muted); }
.img-slot { border: 1px dashed var(--border-strong); border-radius: 9px; padding: 10px; display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.img-slot .prev { height: 80px; display: flex; align-items: center; }
.img-slot img { max-height: 80px; max-width: 220px; object-fit: contain; background: repeating-conic-gradient(#f1f3f6 0% 25%, #fff 0% 50%) 50% / 14px 14px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; flex-wrap: wrap; }
.tabs button { border: none; background: none; padding: 9px 14px; cursor: pointer; font: inherit; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

/* modal & toasts */
.modal-bg { position: fixed; inset: 0; background: rgba(16,24,40,.45); z-index: 100; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal { background: #fff; border-radius: 12px; width: min(560px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.modal.wide { width: min(1100px, 100%); height: calc(100vh - 32px); }
.modal-h { padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 10px; }
.modal-h h3 { margin: 0; font-size: 16px; flex: 1; }
.modal-b { padding: 16px 18px; overflow: auto; flex: 1; }
.modal-f { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.modal iframe { width: 100%; height: 100%; border: 0; background: #525659; }
.modal.wide .modal-b { padding: 0; }
#toasts { position: fixed; right: 18px; bottom: 18px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast { background: #1d2939; color: #fff; padding: 10px 14px; border-radius: 9px; box-shadow: var(--shadow); max-width: 380px; font-size: 13.5px; }
.toast.err { background: var(--bad); } .toast.ok { background: var(--ok); }
.pick-list { max-height: 360px; overflow: auto; border: 1px solid var(--border); border-radius: 8px; }
.pick-list .pi { padding: 9px 12px; border-bottom: 1px solid #f0f2f5; cursor: pointer; }
.pick-list .pi:hover { background: var(--primary-50); }

@media (max-width: 1180px) {
  .editor { grid-template-columns: minmax(0, 1fr); }
  .summary { position: static; }
  .item-row { grid-template-columns: 26px minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1fr); }
  .item-row > :nth-child(3) { grid-column: span 2; }
  .item-row2 { grid-template-columns: 26px minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1fr); }
  .item-row2 > :nth-child(2) { grid-column: span 3; }
}
@media (max-width: 860px) {
  #sidebar { position: fixed; left: -240px; z-index: 50; transition: left .2s; }
  #sidebar.open { left: 0; }
  #menuBtn { display: inline-flex; }
  #view { padding: 16px; }
  #topbar { padding: 10px 16px; }
  .g3, .g4, .g6 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .two-col { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 520px) {
  .g2, .g3, .g4, .g6 { grid-template-columns: minmax(0, 1fr); }
  .span2, .span3 { grid-column: auto; }
  .item-row, .item-row2 { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .item-row > :nth-child(3), .item-row2 > :nth-child(2) { grid-column: span 2; }
  .item .idx { display: none; }
}
