*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 13px;
  background: #f5f6fa;
  color: #222;
}

/* ── Login screen ────────────────────────────────────── */
#login-screen {
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  gap: 24px;
  background: linear-gradient(135deg, #1a2340 0%, #253058 60%, #2d3a5e 100%);
  color: #fff;
  text-align: center;
  padding: 40px;
}
#login-screen h1 { font-size: 22px; font-weight: 600; }
#login-screen p  { font-size: 13px; opacity: 0.8; max-width: 360px; line-height: 1.5; }

.btn-ms-login {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 28px;
  background: #fff;
  color: #1a2340;
  border: none;
  border-radius: 6px;
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0,0,0,0.3);
  transition: transform 0.15s, box-shadow 0.15s;
}
.btn-ms-login:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(0,0,0,0.35); }

/* ── App shell ───────────────────────────────────────── */
#app-shell { display: none; }

/* ── Header ─────────────────────────────────────────── */
header {
  background: #1a2340;
  color: #fff;
  padding: 10px 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}
header h1 { font-size: 16px; font-weight: 600; flex: 1; }

/* .hdr-controls holds three explicit toolbar groups — filtering, billing
   actions, help — separated by a divider. Spacing between groups and
   between buttons within a group both come from flex gap, not margins,
   so nothing has to be unpicked if a button is added or removed. */
.hdr-controls { display: flex; align-items: center; gap: 18px; flex-wrap: wrap; }
.hdr-group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.hdr-divider { width: 1px; align-self: stretch; min-height: 20px; background: rgba(255,255,255,0.16); }
@media (max-width: 900px) {
  .hdr-divider { display: none; }
}
.hdr-controls label { color: #ccd; font-size: 12px; }
.hdr-controls select {
  padding: 4px 8px; border-radius: 4px; border: none;
  background: #2d3a5e; color: #fff; cursor: pointer;
}
#period-lock-badge {
  background: #fef3c7; color: #92400e; padding: 4px 10px;
  border-radius: 4px; font-size: 11px; font-weight: 700;
  cursor: default;
}

/* ── User info in header ─────────────────────────────── */
#user-info {
  display: none;
  align-items: center;
  gap: 10px;
  background: rgba(255,255,255,0.08);
  padding: 5px 12px;
  border-radius: 4px;
}
#user-email { font-size: 12px; color: #aabbd0; }
.btn-signout {
  padding: 4px 10px;
  background: rgba(255,255,255,0.12);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.25);
  border-radius: 4px;
  font-size: 11px;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-signout:hover { background: rgba(255,255,255,0.22); }

.btn {
  padding: 5px 14px; border-radius: 4px; border: none;
  cursor: pointer; font-size: 12px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 5px;
  text-decoration: none; line-height: 1.4;
}
.btn-primary { background: #3b7ddd; color: #fff; }
.btn-secondary { background: #4a5568; color: #fff; }
.btn-danger { background: #e53e3e; color: #fff; }
.btn-sm { padding: 3px 10px; font-size: 11px; }
.btn:hover { opacity: 0.88; }
.btn:visited { color: inherit; }

/* ── Nav ─────────────────────────────────────────────── */
nav {
  background: #253058;
  padding: 0 20px;
  display: flex;
  gap: 0;
}
.nav-link {
  padding: 9px 16px;
  color: #8899bb;
  font-size: 12px;
  font-weight: 600;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  user-select: none;
  text-transform: uppercase;
  letter-spacing: .4px;
}
.nav-link:hover { color: #c8d4f0; }
.nav-link.active { color: #fff; border-bottom-color: #3b7ddd; }

/* ── Views ───────────────────────────────────────────── */
.view { display: none; }
.view.active { display: block; }

/* ── Summary bar ─────────────────────────────────────── */
#summary-bar {
  background: #fff;
  border-bottom: 1px solid #e2e8f0;
  padding: 8px 20px;
  display: flex;
  gap: 32px;
  font-size: 12px;
}
.sum-item { display: flex; flex-direction: column; gap: 2px; }
.sum-label { color: #718096; font-size: 11px; }
.sum-value { font-weight: 700; font-size: 14px; }
.pos { color: #276749; }
.neg { color: #c53030; }

/* ── Row-selection totals bar ────────────────────────── */
/* Shown only once a row is selected within the (possibly filtered) view -
   lets a subset of the current view be totalled separately from the
   summary bar above, e.g. for a supplier invoice recon. */
#selection-bar {
  background: #ebf4ff;
  border-bottom: 1px solid #bfdbfe;
  padding: 8px 20px;
  display: flex;
  align-items: center;
  gap: 32px;
  font-size: 12px;
}
#selection-bar .sel-label { color: #1e40af; }
#selection-bar .btn { margin-left: auto; }
#selection-bar .btn + .btn { margin-left: 0; }

/* ── View padding ────────────────────────────────────── */
#main, #main-customers, #main-products, #main-categories, #main-suppliers { padding: 16px 20px; }

/* ── View toolbar (search / filter row) ─────────────── */
.view-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.view-toolbar h2 { font-size: 14px; font-weight: 600; color: #2d3a5e; flex: 1; }
.search-input {
  padding: 5px 10px; border: 1px solid #cbd5e0; border-radius: 4px;
  font-size: 12px; width: 220px; background: #fff;
}
.search-input:focus { outline: none; border-color: #3b7ddd; }
.filter-select {
  padding: 5px 8px; border: 1px solid #cbd5e0; border-radius: 4px;
  font-size: 12px; background: #fff; cursor: pointer;
}

/* ── Tables ──────────────────────────────────────────── */
.table-wrap { overflow-x: auto; border-radius: 6px; box-shadow: 0 1px 3px rgba(0,0,0,.1); }

table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  min-width: 600px;
}
thead th {
  background: #2d3a5e;
  color: #e2e8f0;
  padding: 8px 10px;
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
  user-select: none;
  position: sticky;
  top: 0;
  z-index: 2;
}
thead th.num { text-align: right; }
thead th.sortable { cursor: pointer; }
thead th.sortable:hover { background: #374873; }
thead th .sort-arrow { margin-left: 4px; font-size: 9px; opacity: 0.85; }
thead .filter-row th {
  top: 34px;
  background: #fff;
  padding: 4px 6px;
  z-index: 2;
  border-bottom: 1px solid #e2e8f0;
}
.col-filter {
  width: 100%;
  padding: 3px 6px;
  border: 1px solid #cbd5e0;
  border-radius: 3px;
  background: #e2e8f0;
  color: #2d3a5e;
  font-size: 11px;
  font-family: inherit;
}
.col-filter::placeholder { color: #a0aec0; }
.col-filter:focus { outline: none; border-color: #3b7ddd; background: #fff; }
select.col-filter { cursor: pointer; }
#view-billing .table-wrap {
  max-height: calc(100vh - 165px);
  overflow-y: auto;
}
tbody tr.data-row {
  cursor: pointer;
  border-bottom: 1px solid #edf2f7;
  transition: background 0.1s;
}
tbody tr.data-row:hover { background: #ebf4ff; }
tbody tr.data-row.selected { background: #dbeafe; }
tbody td { padding: 7px 10px; vertical-align: middle; }
tbody td.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody td.dim { color: #a0aec0; font-size: 11px; }
.margin-pos { color: #276749; font-weight: 600; }
.margin-neg { color: #c53030; font-weight: 600; }
.expiry-soon    { color: #92400e; font-weight: 600; }
.expiry-expired { color: #c53030; font-weight: 600; }
tfoot td {
  padding: 8px 10px;
  font-weight: 700;
  background: #f7fafc;
  border-top: 2px solid #cbd5e0;
}
tfoot td.num { text-align: right; }
.row-count { font-size: 11px; color: #718096; }

/* ── Edit panel row ──────────────────────────────────── */
tr.edit-row td { padding: 0; }
.edit-panel {
  background: #f0f4ff;
  border-left: 3px solid #3b7ddd;
  padding: 16px 20px;
}
.edit-panel h4 { margin-bottom: 12px; color: #2d3a5e; font-size: 13px; }
.field-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px 16px;
}
.field { display: flex; flex-direction: column; gap: 3px; }
.field label { font-size: 11px; color: #4a5568; font-weight: 500; }
.field input, .field select, .field textarea {
  padding: 5px 8px; border: 1px solid #cbd5e0; border-radius: 4px;
  font-size: 12px; background: #fff;
}
.field input:focus, .field select:focus {
  outline: none; border-color: #3b7ddd;
}
.field input[readonly] { background: #f7fafc; color: #718096; }
.edit-actions { margin-top: 12px; display: flex; gap: 8px; align-items: center; }
.edit-actions .delete-link {
  margin-left: auto; color: #c53030; font-size: 11px; cursor: pointer;
  text-decoration: underline; background: none; border: none; padding: 0;
}
.computed-row {
  display: flex; gap: 24px; margin-top: 10px;
  padding: 8px 12px; background: #e8f0fe; border-radius: 4px;
  font-size: 12px;
}
.computed-row span { color: #4a5568; }
.computed-row strong { color: #1a2340; }

/* ── Modal ───────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal-overlay.hidden { display: none; }
.modal {
  background: #fff;
  border-radius: 8px;
  padding: 24px;
  width: 640px;
  max-width: 95vw;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.modal h3 { margin-bottom: 16px; color: #1a2340; font-size: 15px; }
.modal .section-title {
  font-size: 11px; font-weight: 600; color: #718096;
  text-transform: uppercase; letter-spacing: .5px;
  margin: 14px 0 8px;
  border-bottom: 1px solid #e2e8f0; padding-bottom: 4px;
}
.modal-actions { margin-top: 18px; display: flex; gap: 8px; justify-content: flex-end; }

/* ── Misc ────────────────────────────────────────────── */
.hidden { display: none !important; }
#toast {
  position: fixed; bottom: 20px; right: 20px;
  background: #1a2340; color: #fff;
  padding: 10px 18px; border-radius: 6px;
  font-size: 12px; z-index: 200;
  transition: opacity .3s;
}
#toast.hidden { opacity: 0; pointer-events: none; }
.badge {
  display: inline-block; padding: 1px 6px; border-radius: 3px;
  font-size: 10px; font-weight: 600; text-transform: uppercase;
}
.badge-invoiced { background: #ebf8ff; color: #2b6cb0; }
.badge-cost_only { background: #fef9c3; color: #92400e; }
.badge-internal { background: #f0fff4; color: #276749; }
.badge-insert { background: #f0fff4; color: #276749; }
.badge-update { background: #ebf8ff; color: #2b6cb0; }
.badge-delete { background: #fff5f5; color: #c53030; }
.chip-active   { background: #f0fff4; color: #276749; border-radius: 3px; padding: 1px 7px; font-size: 11px; }
.chip-inactive { background: #fff5f5; color: #c53030; border-radius: 3px; padding: 1px 7px; font-size: 11px; }
.chip-confirmed    { background: #f0fff4; color: #276749; border-radius: 3px; padding: 1px 7px; font-size: 11px; }
.chip-carried-over { background: #fef9c3; color: #92400e; border-radius: 3px; padding: 1px 7px; font-size: 11px; }
td.confirm-cell { cursor: pointer; }

/* ── Role-based visibility ───────────────────────────── */
body.viewer-mode .fc { display: none !important; }
body.viewer-mode #view-billing tbody tr.data-row { cursor: default; }

/* ── Cost-changed indicator ──────────────────────────── */
tbody tr.data-row td.cost-changed          { background-color: #ebf8ff; }
tbody tr.data-row:hover td.cost-changed    { background-color: #dbeafe; }
tbody tr.data-row.selected td.cost-changed { background-color: #bfdbfe; }

/* ── Marked-for-removal indicator ────────────────────── */
tbody tr.data-row.marked-for-removal              { background-color: #fff5f5; }
tbody tr.data-row.marked-for-removal:hover         { background-color: #fed7d7; }
tbody tr.data-row.marked-for-removal.selected      { background-color: #feb2b2; }
td.tag-removal { color: #c53030; font-weight: 600; font-size: 12px; }
