:root {
  --ink: #1f2430; --muted: #6b7280; --line: #d9dde5; --bg: #f4f5f8; --card: #fff;
  --brand: #8a6a1f; --accent: #1f4fd8; --danger: #b42318; --ok: #157347;
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.45 "Segoe UI", Calibri, Arial, sans-serif; color: var(--ink); background: var(--bg); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 14px; }
h1 small { font-weight: 400; color: var(--muted); font-size: 16px; }
h2 { font-size: 15px; margin: 0 0 10px; }
main { max-width: 1180px; margin: 0 auto; padding: 20px 16px 60px; }

.topbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 22px; padding: 8px 20px; background: #fff; border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--ink); white-space: nowrap; }
.brand img { height: 36px; }
.topbar nav { display: flex; gap: 16px; flex: 1; }
.topbar nav a { color: var(--ink); padding: 4px 2px; }
.new-menu { display: flex; flex-wrap: wrap; gap: 8px; }

.btn { display: inline-block; white-space: nowrap; padding: 7px 13px; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--ink); font: inherit; cursor: pointer; }
.btn:hover { background: #f0f2f7; text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #1a43b8; }
.btn.danger { color: var(--danger); border-color: #f0c0bb; }
.btn.small { padding: 3px 9px; font-size: 13px; }
.icon-btn { border: 0; background: none; font-size: 20px; line-height: 1; color: var(--muted); cursor: pointer; padding: 2px 6px; }
.icon-btn:hover { color: var(--danger); }

.flash { padding: 10px 14px; border-radius: 6px; margin-bottom: 14px; border: 1px solid; }
.flash.ok { background: #e8f5ee; border-color: #b7dfc7; color: var(--ok); }
.flash.error { background: #fdecea; border-color: #f4c2bd; color: var(--danger); }
.flash ul { margin: 0; padding-left: 18px; }

.cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px; }
.card span { display: block; color: var(--muted); font-size: 12px; }
.card strong { font-size: 18px; }
.hint { color: var(--muted); font-size: 12px; margin: 6px 0 12px; }

.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.filters label { display: flex; align-items: center; gap: 4px; color: var(--muted); }

input, select, textarea { font: inherit; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--ink); max-width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid #b9c8f7; border-color: var(--accent); }
label { display: block; margin-bottom: 10px; color: var(--muted); font-size: 12px; }
label input, label select, label textarea { display: block; width: 100%; margin-top: 3px; color: var(--ink); font-size: 14px; }
label.check { display: flex; gap: 6px; align-items: center; }
label.check input { width: auto; margin: 0; }
.filters label input { display: inline; width: auto; margin: 0; }
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.row > label { flex: 1 1 150px; }
.row > label.grow { flex: 3 1 300px; }

.panel { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 14px 16px; margin-bottom: 14px; }
.actions { display: flex; gap: 8px; margin: 14px 0; }

table.grid { width: 100%; border-collapse: collapse; background: var(--card); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
table.grid th, table.grid td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
table.grid th { background: #eef1f7; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
.num { text-align: right !important; font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 24px !important; }
.nowrap { white-space: nowrap; }
form.inline { display: inline; }
form.user-box { display: flex; align-items: center; gap: 8px; }
.panel.narrow { max-width: 420px; margin: 40px auto; }
tr.inactive td { opacity: .55; }
.tag { background: #eef1f7; border-radius: 4px; padding: 2px 7px; font-size: 12px; }
.badge { border-radius: 999px; padding: 2px 10px; font-size: 12px; text-transform: capitalize; }
.badge.draft { background: #eceff3; color: #4b5563; }
.badge.sent { background: #e0ebff; color: #1d4ed8; }
.badge.paid { background: #dcf3e5; color: var(--ok); }
.badge.cancelled { background: #fde4e1; color: var(--danger); }

table.items td { vertical-align: middle; padding: 5px 6px; }
table.items td input { width: 100%; }
table.items .no { width: 34px; color: var(--muted); text-align: center; }
table.items .qty { width: 80px; }
table.items .price { width: 130px; }
table.items .amt { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
table.items .c { text-align: center; }
table.items .wht-cb { width: auto; }
#add-row { margin-top: 10px; }
.totals-wrap { margin-top: 16px; justify-content: space-between; align-items: flex-start; }
.totals-wrap > div { flex: 1 1 320px; }
table.totals { border-collapse: collapse; min-width: 300px; margin-left: auto; }
table.totals th { text-align: left; padding: 4px 14px 4px 0; color: var(--muted); font-weight: 600; }
table.totals td { text-align: right; padding: 4px 0; font-variant-numeric: tabular-nums; min-width: 110px; }
table.totals tr.grand th, table.totals tr.grand td { border-top: 2px solid var(--ink); color: var(--ink); font-weight: 700; font-size: 16px; }

.toolbar { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.toolbar-actions { display: flex; gap: 8px; }
.side-panels { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 12px; }
.files { list-style: none; padding: 0; margin: 0 0 10px; }
.files li { display: flex; gap: 10px; align-items: center; padding: 4px 0; border-bottom: 1px dashed var(--line); }
.files li form { margin-left: auto; }
.share-panel .share-link { margin: 4px 0 12px; }
.share-msgs { display: grid; gap: 4px; margin-bottom: 12px; }
.share-msgs label textarea { min-height: 74px; resize: vertical; }
.share-msgs .btn { justify-self: start; margin-bottom: 10px; }
.actions form { margin: 0; }

/* Invoice sheet (screen + print), follows the Excel layout */
.sheet { background: #fff; color: #000; border: 2px solid #000; padding: 18px 22px; margin: 14px auto 0; max-width: 1060px; font-family: Calibri, "Segoe UI", Arial, sans-serif; font-size: 13px; }
.sheet-head { display: flex; justify-content: space-between; gap: 20px; }
.from { display: flex; gap: 12px; align-items: flex-start; flex: 1; }
.logo { width: 95px; height: 95px; object-fit: contain; }
.from-text { flex: 1; padding-top: 18px; }
.from-name { font-weight: 700; font-size: 15px; border-bottom: 3px solid #000; padding-bottom: 2px; margin-bottom: 4px; }
.meta { width: 340px; }
.title-box { border: 2px solid #000; text-align: center; font-size: 22px; font-weight: 700; padding: 6px; margin-bottom: 6px; }
.note-box { border: 2px solid #000; padding: 6px 8px; margin-top: 8px; min-height: 48px; white-space: pre-wrap; }
.to-block { margin: 12px 0; border-top: 1px solid #000; border-bottom: 1px solid #000; padding: 6px 0; }
.to-addr { white-space: pre-wrap; }
.sheet .lbl { font-weight: 600; }
.sheet-items { width: 100%; border-collapse: collapse; margin-bottom: 8px; }
.sheet-items th, .sheet-items td { border: 1px solid #000; padding: 6px 8px; vertical-align: top; }
.sheet-items thead th { background: #d9e2f3; text-align: center; }
.sheet-items .w-no { width: 40px; }
.sheet-items .w-qty { width: 90px; }
.sheet-items td.c { text-align: center; }
.sheet-items td.r { text-align: right; white-space: nowrap; }
.sheet-items tfoot th { text-align: right; }
.sheet-items tfoot .noborder { border: 0; }
.sheet-items tfoot tr.grand th, .sheet-items tfoot tr.grand td { font-weight: 700; }
.words { border-top: 1px solid #000; border-bottom: 1px solid #000; padding: 6px 0; margin-bottom: 12px; }
.sheet-foot { display: flex; justify-content: space-between; gap: 20px; align-items: flex-start; }
.pay table td { padding: 1px 10px 1px 0; }
.sign { width: 260px; text-align: center; }
.sign-co { font-weight: 700; }
.sign-img { height: 56px; display: block; margin: 4px auto; }
.sign-space { height: 70px; }
.sign-name { border-top: 1px solid #000; padding-top: 2px; }
.terms { margin-top: 16px; border-top: 1px solid #000; padding-top: 8px; font-size: 12px; }
.terms ol { margin: 4px 0 0 18px; padding: 0; }

@page { size: A4 landscape; margin: 8mm; }
@media print {
  body { background: #fff; }
  .no-print { display: none !important; }
  main { padding: 0; max-width: none; }
  .sheet { margin: 0; max-width: none; border-width: 2px; page-break-inside: avoid; }
  .sheet-items thead th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
@media (max-width: 820px) {
  .cards { grid-template-columns: 1fr 1fr; }
  .side-panels { grid-template-columns: 1fr; }
  .sheet { overflow-x: auto; }
  .sheet-head, .sheet-foot { flex-direction: column; }
  .meta, .sign { width: 100%; }
}

.topmenu { display: contents; }
.menu-btn { display: none; }
.wht-label { display: inline-flex; align-items: center; gap: 8px; margin: 0; color: var(--ink); font-size: 14px; }
.wht-label input { display: inline-block; width: auto; margin: 0; }
.wht-text { display: none; }

@media (max-width: 760px) {
  body { font-size: 15px; }
  main { padding: 14px 12px 80px; }
  h1 { font-size: 20px; }

  .topbar { padding: 8px 12px; gap: 8px; justify-content: space-between; }
  .menu-btn { display: inline-flex; align-items: center; min-height: 44px; padding: 0 18px; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--ink); font: inherit; font-weight: 600; cursor: pointer; }
  .topmenu { display: none; flex-direction: column; gap: 10px; width: 100%; padding: 4px 0 10px; }
  .topmenu.open { display: flex; }
  .topbar nav { flex-direction: column; gap: 0; flex: none; }
  .topbar nav a { padding: 12px 4px; border-bottom: 1px solid var(--line); font-size: 16px; }
  .new-menu { display: grid; grid-template-columns: 1fr; }
  form.user-box { justify-content: space-between; }

  input, select, textarea { font-size: 16px; min-height: 44px; }
  textarea { min-height: 72px; }
  input[type=checkbox] { min-height: 0; width: 22px; height: 22px; }
  .btn { display: inline-flex; align-items: center; justify-content: center; min-height: 44px; padding: 10px 16px; }
  .btn.small { min-height: 40px; padding: 6px 12px; }
  .icon-btn { min-width: 44px; min-height: 44px; font-size: 26px; }

  .card strong { font-size: 16px; }
  .filters > input[type=search] { flex: 1 1 100%; }
  .filters > select { flex: 1 1 45%; min-width: 0; }
  .filters label { flex: 1 1 45%; }
  .filters label input { flex: 1; min-width: 0; }
  .row > label, .row > label.grow { flex: 1 1 100%; }
  .toolbar-actions { flex-wrap: wrap; width: 100%; }
  .toolbar-actions .btn { flex: 1 1 auto; }
  .actions { flex-wrap: wrap; }

  #invoice-form > .actions { position: sticky; bottom: 0; z-index: 4; background: var(--bg); margin: 14px -12px 0; padding: 10px 12px; border-top: 1px solid var(--line); }
  #invoice-form > .actions .btn { flex: 1; }

  table.grid:not(.items), table.grid:not(.items) tbody { display: block; border: 0; background: none; }
  table.grid:not(.items) thead { display: none; }
  table.grid:not(.items) tr { display: block; background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; margin-bottom: 10px; }
  table.grid:not(.items) td { display: flex; justify-content: space-between; gap: 14px; border: 0; padding: 4px 0; text-align: right; overflow-wrap: anywhere; }
  table.grid:not(.items) td[data-label]::before { content: attr(data-label); flex: none; padding-top: 2px; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; text-align: left; }
  table.grid:not(.items) td.num { text-align: right !important; }
  table.grid:not(.items) td:empty { display: none; }
  table.grid:not(.items) td:not([data-label]) { justify-content: flex-start; flex-wrap: wrap; text-align: left; }
  table.grid:not(.items) td:not([data-label]):last-child { padding-top: 10px; margin-top: 4px; border-top: 1px solid var(--line); }
  table.grid:not(.items) td.nowrap { white-space: normal; }
  table.grid:not(.items) td.empty { display: block; text-align: center; }

  table.items, table.items tbody { display: block; border: 0; background: none; }
  table.items thead { display: none; }
  table.items tr.item-row { display: grid; grid-template-columns: repeat(6, 1fr); grid-template-areas: "no no no rm rm rm" "nm nm nm nm nm nm" "qty qty qty prc prc prc" "sub sub tax tax tot tot" "wht wht wht wht wht wht"; gap: 8px; padding: 12px; margin-bottom: 12px; background: var(--card); border: 1px solid var(--line); border-radius: 10px; }
  table.items td { display: block; padding: 0; border: 0; text-align: left; width: auto; }
  table.items td[data-label]::before { content: attr(data-label); display: block; font-size: 12px; color: var(--muted); }
  table.items td.no { grid-area: no; align-self: center; width: auto; text-align: left; font-weight: 700; color: var(--muted); }
  table.items td.no::before { content: "Item "; }
  table.items td.c-name { grid-area: nm; }
  table.items td.c-qty { grid-area: qty; }
  table.items td.c-price { grid-area: prc; }
  table.items td.sub { grid-area: sub; }
  table.items td.tax { grid-area: tax; }
  table.items td.tot { grid-area: tot; font-weight: 700; }
  table.items td.amt, table.items td.c { text-align: left; }
  table.items td.c-wht { grid-area: wht; }
  table.items td.c-rm { grid-area: rm; text-align: right; }
  table.items .qty, table.items .price { width: 100%; }
  .wht-text { display: inline; }
  #add-row { width: 100%; }

  table.totals { min-width: 0; width: 100%; }
  .totals-wrap > div { flex-basis: 100%; }
  .files li { flex-wrap: wrap; }
}
