/* Component styles — see docs/rule.md §3–9. Tokens live in tokens.css. */

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--canvas);
  color: var(--ink);
  font: 13px/1.35 "IBM Plex Sans", "Segoe UI", Roboto, Arial, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
a { color: var(--accent); }

/* ---- app shell ---- */
.app-shell {
  width: 100%;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
}
.appbar {
  display: flex; align-items: center; gap: 20px; flex-wrap: wrap;
  padding: 10px 14px; background: var(--menubar); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 600; font-size: 14px; }
.brand .app-name { font-weight: 400; color: var(--muted); margin-left: 6px; }
.module-nav { display: flex; gap: 2px; flex-wrap: wrap; }
.mtab {
  padding: 6px 12px; background: none; border: 1px solid transparent; border-radius: var(--radius);
  color: var(--muted); cursor: pointer; text-decoration: none; display: inline-block;
}
.mtab:hover { background: var(--accent-soft); color: var(--ink); }
.mtab[aria-selected="true"] {
  background: var(--panel); border-color: var(--line-strong); color: var(--ink); font-weight: 600;
}
.global-search { display: flex; gap: 4px; }
.global-search .input { width: 220px; height: var(--ctl-h); }
.app-main { flex: 1; padding: 14px; display: flex; flex-direction: column; gap: 14px; }

/* ---- actions / buttons ---- */
.actions {
  display: flex; align-items: center; gap: 4px; flex-wrap: wrap;
  padding: 2px 0 10px; border-bottom: 1px solid var(--line);
}
.btn {
  height: var(--ctl-h); padding: 0 12px; background: var(--band-2);
  border: 1px solid var(--line-strong); border-radius: var(--radius);
  white-space: nowrap; cursor: pointer; text-decoration: none; color: var(--ink);
  display: inline-flex; align-items: center;
}
.btn:hover { background: var(--accent-soft); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 500; }
.btn.primary:hover { filter: brightness(1.08); }
.spacer { flex: 1; }
.seg-links { display: inline-flex; gap: 4px; }

/* ---- sections ---- */
.section { border: 1px solid var(--line); background: var(--panel); }
.section > .section-title {
  margin: 0; padding: 6px 12px; font-size: 13px; font-weight: 600;
  background: var(--band); border-bottom: 1px solid var(--line);
}

/* ---- fields ---- */
.fields {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 10px 24px; padding: 12px; background: var(--band-2); border-bottom: 1px solid var(--line);
}
.field { display: flex; flex-direction: column; gap: 3px; }
.field > label { color: var(--muted); font-size: 12px; }
.req { color: var(--danger); margin-left: 2px; }
.input, .select {
  height: var(--ctl-h); width: 100%; padding: 0 7px;
  background: var(--panel); border: 1px solid var(--line-strong); border-radius: 0;
}
.input.required, .select.required { background: var(--required); }
.input[readonly] { background: var(--band-2); }
.field-hint { font-size: 11px; color: var(--muted); }

.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: var(--radius); overflow: hidden; }
.seg button {
  height: var(--ctl-h); padding: 0 12px; background: var(--panel); border: 0;
  border-right: 1px solid var(--line-strong); color: var(--muted); cursor: pointer;
}
.seg button:last-child { border-right: 0; }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); font-weight: 500; }

.panel-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px,1fr)); gap: 12px 40px; }
.field-row { display: grid; grid-template-columns: 150px 1fr; align-items: center; gap: 8px; }
.field-row > label { color: var(--muted); font-size: 12px; }
.stack { display: grid; gap: 10px; }
.panel-note { margin: 0 0 12px; color: var(--muted); font-size: 12px; }

/* ---- status chips ---- */
.chip { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 500; white-space: nowrap; }
.chip.open { background: var(--accent-soft); color: var(--accent); }
.chip.progress { background: var(--warn-soft); color: var(--warn); }
.chip.complete { background: var(--ok-soft); color: var(--ok); }

/* ---- tables ---- */
table.grid { width: 100%; border-collapse: separate; border-spacing: 0; }
table.grid th, table.grid td {
  padding: 6px 8px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  text-align: left; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
table.grid th { background: var(--band); font-weight: 600; position: sticky; top: 0; }
table.grid td.num, table.grid th.num { text-align: right; }
table.grid td.ro { background: var(--band-2); color: var(--muted); }
table.grid tbody tr:hover td { background: var(--accent-soft); }
table.grid .chk-col { width: 34px; text-align: center; }
table.grid td .input { width: 100%; min-width: 90px; }
.grid-wrap { max-height: 420px; overflow: auto; border-bottom: 1px solid var(--line); }

/* ---- with-material-only item columns (Rate / GST / Amount) ---- */
#itemsTable .with-material-col { display: none; }
#itemsTable.with-material .with-material-col { display: table-cell; }

.item-toolbar { display: flex; align-items: center; gap: 4px; padding: 6px 8px; background: var(--band-2); border-bottom: 1px solid var(--line); }
.hint { padding: 0 6px; color: var(--muted); font-size: 12px; }

/* ---- stock overview: KPI summary strip ---- */
.summary-strip {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(150px,1fr));
  gap: 1px; background: var(--line); border: 1px solid var(--line); margin-bottom: 12px;
}
.summary-tile { background: var(--panel); padding: 10px 14px; }
.summary-tile .label { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.summary-tile .value { font-size: 20px; font-weight: 600; color: var(--ink); }

/* ---- inline banners ---- */
.banner {
  padding: 8px 12px; background: var(--band-2); border: 1px solid var(--line);
  border-left: 3px solid var(--focus); color: var(--ink); font-size: 12px;
}
.banner b { font-weight: 600; }
.banner.danger { border-left-color: var(--danger); color: var(--danger); }

/* ---- empty state ---- */
.empty-state { padding: 40px 20px; text-align: center; color: var(--muted); }
.empty-state svg { width: 28px; height: 28px; margin-bottom: 8px; opacity: .6; }
.empty-state p { margin: 0; font-size: 12px; }

.block-sep td { border-bottom: 2px solid var(--line-strong); }

/* ---- masters ---- */
.masters-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px,1fr)); gap: 14px; }
.sub-nav { padding-bottom: 8px; border-bottom: 1px solid var(--line); }
.pager { display: flex; align-items: center; gap: 10px; padding: 10px 12px; }
.pager-status { color: var(--muted); font-size: 12px; }
.pager .btn.disabled { pointer-events: none; opacity: .45; }
.inline-form { display: inline; margin-left: 4px; }

/* ---- collapsible section header (chevron + summary), see docs/rule.md §4 ---- */
.section > .collapse-btn {
  width: 100%; display: flex; align-items: center; gap: 6px;
  margin: 0; padding: 6px 12px; font: inherit; font-size: 13px; font-weight: 600;
  background: var(--band); border: 0; border-bottom: 1px solid var(--line);
  color: var(--ink); cursor: pointer; text-align: left;
}
.section > .collapse-btn:hover { background: var(--accent-soft); }
.section > .collapse-btn .chevron { flex: 0 0 auto; transition: transform .12s; }
.section > .collapse-btn[aria-expanded="false"] .chevron { transform: rotate(-90deg); }
.section > .collapse-btn .section-summary {
  font-weight: 400; color: var(--muted); font-size: 12px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
@media (prefers-reduced-motion: reduce) {
  .section > .collapse-btn .chevron { transition: none; }
}

/* ---- tabs (header sub-views), see docs/rule.md §6 ---- */
.tabs {
  display: flex; gap: 2px; padding: 8px 12px 0; overflow-x: auto;
  background: var(--panel); border-bottom: 1px solid var(--line-strong);
}
.tab {
  position: relative; top: 1px; padding: 7px 16px; background: var(--band-2);
  border: 1px solid var(--line); border-bottom: 0; border-radius: 3px 3px 0 0;
  color: var(--muted); white-space: nowrap; cursor: pointer;
}
.tab[aria-selected="true"] {
  background: var(--panel); border-color: var(--line-strong); color: var(--ink);
  font-weight: 600; box-shadow: inset 0 3px 0 var(--accent);
}
.tabpanel { padding: 14px 16px 18px; }
.tabpanel[hidden] { display: none; }

/* ---- invoice modal ("popup" for Start billing) ---- */
.invoice-modal {
  width: min(900px, 92vw); max-height: 86vh; padding: 0;
  border: 1px solid var(--line-strong); border-radius: var(--radius);
  background: var(--panel); color: var(--ink);
}
/* Only the body scrolls; a fixed body max-height guessed the head's height and
   left the dialog itself scrolling by a few pixels (a second scrollbar). */
.invoice-modal[open] { display: flex; flex-direction: column; overflow: hidden; }
.invoice-modal::backdrop { background: rgba(0,0,0,.45); }
.invoice-modal-head {
  display: flex; align-items: center; justify-content: space-between; flex-shrink: 0;
  padding: 8px 12px; background: var(--band); border-bottom: 1px solid var(--line); font-weight: 600;
}
#invoiceModalBody { flex: 1 1 auto; min-height: 0; padding: 12px; overflow: auto; }

/* hidden on screen; only shown for print, see .print-invoice-area below */
#invoicePrintArea { display: none; }

/* ---- printing (invoice document rules: invoice-document.css) ---- */
@media print {
  .no-print { display: none !important; }
  body { background: #fff; color: #000; }
  .app-shell { background: #fff; min-height: 0; }
  .app-main { padding: 0; gap: 10px; }
  .section { border: 1px solid #999; }
  .section > .section-title { background: #eee; color: #000; }
  .grid-wrap { max-height: none; overflow: visible; }
  table.grid th { position: static; background: #eee; color: #000; }

  /* printing from inside the billing popup: show only the copied invoice,
     never the dialog itself (printing an open <dialog> renders unreliably) */
  body.print-invoice-area .app-main > *:not(#invoicePrintArea) { display: none !important; }
  body.print-invoice-area #invoicePrintArea { display: block !important; }
}

/* Authenticated account controls. */
.session-actions { display:flex; align-items:center; flex-wrap:wrap; gap:10px; }
.session-actions form { margin:0; }
.account-link { display:flex; align-items:center; gap:7px; text-decoration:none; font-weight:500; }
.account-role { color:var(--muted); font-size:11px; font-weight:400; padding:2px 5px; border:1px solid var(--line); }
