:root {
  /* Light green theme. Surfaces are pale green; buttons and links use a deeper
     green so white text on them stays easy to read. */
  --navy: #1c5937; /* dark green: headings, figures, strong text */
  --navy-light: #27784c; /* mid green: links and focus rings */
  --primary: #27784c; /* buttons */
  --primary-dark: #1c5937; /* button hover */
  --tint: #e2f3e8; /* light green: the sidebar */
  --tint-strong: #cfe9d9;
  --bg: #f2f9f4;
  --card: #ffffff;
  --border: #d0e4d7;
  --text: #1c2733;
  --muted: #5b6f62;
  --green: #1e7e34;
  --amber: #b8860b;
  --red: #b02a37;
  --radius: 8px;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: 'Segoe UI', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}
a { color: var(--navy-light); text-decoration: none; }
.shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 210px;
  background: var(--tint);
  color: var(--navy);
  display: flex;
  flex-direction: column;
  padding: 18px 0;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.brand { padding: 0 18px 18px; border-bottom: 1px solid rgba(28,89,55,0.2); margin-bottom: 10px; }
.brand-name { font-size: 22px; font-weight: 700; letter-spacing: 1px; color: var(--navy); }
.brand-sub { font-size: 12px; color: #3f6b52; }
.sidebar a {
  color: #214f36;
  padding: 10px 18px;
  font-size: 14px;
  border-left: 3px solid transparent;
}
.sidebar a:hover { background: var(--tint-strong); }
.sidebar a.active { background: var(--tint-strong); border-left-color: var(--navy-light); color: var(--navy); font-weight: 600; }
.content { flex: 1; padding: 26px 32px; max-width: 1200px; }
h1 { font-size: 22px; margin: 0 0 4px; color: var(--navy); }
h2 { font-size: 17px; color: var(--navy); }
.subtitle { color: var(--muted); margin: 0 0 20px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 18px; gap: 12px; flex-wrap: wrap; }
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 20px;
}
.card h2 { margin-top: 0; }
.grid { display: grid; gap: 14px; }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-5 { grid-template-columns: repeat(5, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.stat {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.stat .num { font-size: 26px; font-weight: 700; color: var(--navy); }
.stat .label { color: var(--muted); font-size: 13px; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; text-transform: uppercase; font-size: 11.5px; letter-spacing: 0.5px; }
tr:hover td { background: #f5fbf7; }
tr[data-href] { cursor: pointer; }
.check-group { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 4px 14px; margin-top: 4px; }
.check-group label { display: flex; align-items: center; gap: 6px; margin: 0; font-weight: normal; color: var(--text); font-size: 13.5px; }
tr.row-today td { background: #eaf6ee; }
tr.row-today td:first-child { font-weight: 600; color: var(--navy); }
tr.row-today:hover td { background: #dcefe3; }
tr.totals-row td { font-weight: 700; border-top: 2px solid var(--border); background: #f5fbf7; }
.right { text-align: right; }
.center { text-align: center; }
.btn {
  display: inline-block;
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  line-height: 1.4;
  text-decoration: none;
}
.btn:hover { background: var(--primary-dark); }
.btn.secondary { background: #fff; color: var(--navy); border: 1px solid var(--navy); }
.btn.small { padding: 4px 10px; font-size: 12.5px; }
.btn.danger { background: var(--red); }
.btn.ghost { background: transparent; color: var(--navy); border: 1px solid var(--border); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
form label { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 4px; margin-top: 12px; font-weight: 600; }
form input[type=text], form input[type=password], form input[type=date], form input[type=number], form input[type=time], form select, form textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13.5px;
  font-family: inherit;
}
form textarea { resize: vertical; }
.field-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.field-row.two { grid-template-columns: repeat(2, 1fr); }
/* Grid items otherwise stretch Add buttons across a whole field column. */
.field-row > .btn { justify-self: start; align-self: end; }
@media (max-width: 700px) {
  .reference-add-form { grid-template-columns: 1fr; }
}
.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 20px;
  font-size: 11.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.badge.draft { background: #e4eee8; color: #3f5a4a; }
.badge.issued { background: #dbeafe; color: #1d4ed8; }
.badge.completed { background: #dcfce7; color: var(--green); }
.badge.invoiced { background: var(--navy); color: #fff; }
.badge.sent { background: #fef3c7; color: var(--amber); }
.badge.accepted { background: #dcfce7; color: var(--green); }
.badge.declined { background: #fde2e1; color: var(--red); }
.badge.expired { background: #e4eee8; color: var(--muted); }
.badge.finalized { background: var(--navy); color: #fff; }
.badge.alert { background: #fde2e1; color: var(--red); margin-left: 5px; }
.tabs { display: flex; gap: 4px; margin-bottom: 16px; border-bottom: 1px solid var(--border); }
.tabs a { padding: 9px 16px; color: var(--muted); border-bottom: 2px solid transparent; font-weight: 600; font-size: 13px; }
.tabs a.active { color: var(--navy); border-bottom-color: var(--navy); }
.flash { background: #dcfce7; border: 1px solid #86efac; color: #14532d; padding: 10px 14px; border-radius: 6px; margin-bottom: 16px; }
.flash-warning { background: #fef3c7; border-color: #fcd34d; color: #7c5800; }
tr.row-alert td { background: #fff7ed; }
tr.row-alert:hover td { background: #fef0e0; }
.muted { color: var(--muted); }
.line-items-table input { padding: 6px 8px; font-size: 13px; }
.line-items-table td { padding: 6px; }
.helpdesk-box { background: #fbfbfd; border: 1px dashed var(--border); border-radius: 6px; padding: 12px; margin-bottom: 6px; }
.two-col { display: grid; grid-template-columns: 1.3fr 1fr; gap: 20px; align-items: start; }
.job-print { display: none; }
@media print {
  .no-print { display: none !important; }
  .content { padding: 0; max-width: none; }
  .job-print { display: block; }
  .job-print-hide { display: none !important; }
  body { font-size: 12px; }
}
.print-sheet { border: 1px solid #000; padding: 24px; max-width: 760px; position: relative; }
.print-sheet.has-letterhead { padding-right: 200px; }
.print-sheet-letterhead { position: absolute; top: 4px; right: 4px; height: calc(100% - 8px); width: 180px; object-fit: cover; }
.rating-boxes { display: flex; gap: 14px; margin-top: 6px; }
.rating-box { display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 11px; }
.rating-box .box { width: 20px; height: 20px; border: 1px solid #333; }
.print-sheet .pcol { display: flex; justify-content: space-between; }
.print-sheet table { margin-top: 14px; }
.print-sheet th, .print-sheet td { border: 1px solid #333; }
.sig-line { margin-top: 22px; border-bottom: 1px solid #333; width: 340px; padding-bottom: 2px; }
.small-note { font-size: 12px; color: var(--muted); margin-top: 4px; }
select[multiple] { height: 120px; }

/* ---- Toasts ---- */
#toast-container {
  position: fixed;
  top: 18px;
  right: 18px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 360px;
}
.toast {
  background: var(--navy);
  color: #fff;
  padding: 12px 16px;
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.18);
  font-size: 13.5px;
  line-height: 1.4;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  cursor: pointer;
  opacity: 0;
  transform: translateX(24px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.toast.show { opacity: 1; transform: translateX(0); }
.toast.toast-success { background: #1e7e34; }
.toast.toast-error { background: var(--red); }
.toast-icon { font-weight: 700; flex-shrink: 0; }
.toast-close { margin-left: auto; opacity: 0.7; flex-shrink: 0; }

/* ---- Confirm modal ---- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 27, 38, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2100;
}
.modal-backdrop.open { display: flex; }
.modal-box {
  background: #fff;
  border-radius: var(--radius);
  padding: 22px 24px;
  max-width: 380px;
  width: 90%;
  box-shadow: 0 12px 32px rgba(0,0,0,0.25);
}
.modal-box h3 { margin: 0 0 10px; color: var(--navy); font-size: 16px; }
.modal-box p { margin: 0 0 18px; color: var(--text); font-size: 13.5px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; }

/* ---- Searchable selects ---- */
.searchable-select { display: flex; flex-direction: column; gap: 4px; }
.searchable-select-input {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
}
.searchable-select-input::placeholder { color: var(--muted); }

/* ---- Stock take autosave feedback ---- */
.stocktake-count-input { transition: background 0.3s ease, border-color 0.3s ease; }
.stocktake-count-input.autosave-ok { background: #dcfce7; border-color: #86efac; }
.stocktake-count-input.autosave-error { background: #fde2e1; border-color: #f5b3b0; }

.table-tools { display: flex; align-items: end; flex-wrap: wrap; gap: 10px; margin: 12px 0; }
.table-search, .table-tools nav { display: flex; align-items: end; flex-wrap: wrap; gap: 8px; }
.table-tools label { display: grid; gap: 4px; margin: 0; font-size: 13px; }
.table-tools input[type=search] { max-width: 100%; width: 240px; padding: 6px 10px; border: 1px solid var(--border); border-radius: 6px; font: inherit; }
.table-tools nav { align-items: center; }
.btn:disabled { opacity: .5; cursor: default; }
@media print {
  .paged-row[hidden] { display: table-row !important; }
  [data-page-section][hidden] { display: block !important; }
}

/* Shared responsive shell: keep wide data inside its own scrollable region. */
[hidden] { display: none !important; }
.content, .card, .grid > *, .field-row > *, .two-col > * { min-width: 0; }
.grid-2, .field-row.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3, .field-row { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-5 { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.two-col { grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr); }
.table-scroll { max-width: 100%; overflow-x: auto; margin-bottom: 10px; }
.table-scroll:focus-visible { outline: 2px solid var(--navy-light); outline-offset: 2px; }
.table-scroll th { white-space: nowrap; }
.table-scroll input:not([type=checkbox]), .table-scroll select { min-width: 100px; }
img, svg { max-width: 100%; }
textarea, input[type=file] { max-width: 100%; }
.helpdesk-box textarea { width: 100%; font: inherit; padding: 10px; }
.mobile-header { display: none; }
.login-page { background: linear-gradient(160deg, var(--tint), var(--tint-strong)); min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 340px; max-width: 100%; }
:focus-visible { outline: 2px solid var(--navy-light); outline-offset: 3px; }
.sidebar :focus-visible { outline-color: var(--navy-light); }

@media screen and (max-width: 1100px) {
  .content { padding: 22px 20px; }
  .grid-4, .grid-5 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .two-col { grid-template-columns: minmax(0, 1fr); }
}

@media screen and (max-width: 900px) {
  .shell { flex-direction: column; }
  .mobile-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px; background: var(--primary); position: sticky; top: 0; z-index: 1000; }
  .mobile-brand { color: white; font-size: 18px; font-weight: 700; }
  .mobile-brand span { display: block; font-size: 11px; font-weight: 400; }
  .sidebar { width: 100%; position: static; height: auto; max-height: 65vh; max-height: 65dvh; padding: 8px 0; }
  .sidebar .brand { display: none; }
  .sidebar a { min-height: 44px; display: block; }
  .content { width: 100%; max-width: none; padding: 20px 16px; }
  .field-row, .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tabs { flex-wrap: wrap; }
  .tabs a { min-height: 44px; display: flex; align-items: center; }
  .btn, .btn.small { min-height: 44px; padding: 10px 12px; display: inline-flex; align-items: center; justify-content: center; }
  input:not([type=checkbox]):not([type=radio]):not([type=hidden]), select, textarea,
  form input[type=text], form input[type=password], form input[type=date], form input[type=number], form input[type=time], form select, form textarea,
  .line-items-table input, .searchable-select-input { font-size: 16px; min-height: 44px; min-width: 0; max-width: 100%; }
  input[type=checkbox], input[type=radio] { width: 20px; height: 20px; vertical-align: middle; }
  .check-group label { min-height: 44px; }
  .page-head > *, .btn-row > *, .table-tools > * { max-width: 100%; min-width: 0; }
  .table-scroll table { min-width: 560px; }
  .table-scroll .record-details { min-width: 0; }
  .two-col td { overflow-wrap: anywhere; }
  #toast-container { top: 76px; right: 12px; left: 12px; max-width: none; }
  .modal-box { max-height: calc(100dvh - 32px); overflow-y: auto; }
  .table-scroll .line-items-table { min-width: 0; }
  .line-items-table, .line-items-table tbody { display: block; width: 100%; }
  .line-items-table thead { display: none; }
  .line-items-table tr { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; padding: 12px; margin-bottom: 12px; border: 1px solid var(--border); border-radius: var(--radius); }
  .line-items-table td { display: block; min-width: 0; padding: 0; border: 0; }
  .line-items-table td::before { content: attr(data-label); display: block; margin-bottom: 4px; color: var(--muted); font-size: 12.5px; font-weight: 600; }
  .line-items-table td[data-label="Remove line"]::before { display: none; }
  .line-items-table tr:hover td { background: transparent; }
}

@media screen and (max-width: 600px) {
  .card { padding: 16px 12px; }
  .field-row, .field-row.two, .grid-2, .grid-3, .grid-5 { grid-template-columns: minmax(0, 1fr); }
  .grid { gap: 10px; }
  .stat { padding: 12px; }
  .stat .num { font-size: 22px; overflow-wrap: anywhere; }
  h1 { font-size: 21px; }
  .page-head, .subtitle, .card p { overflow-wrap: anywhere; }
  .table-search, .table-search label { width: 100%; }
  .table-tools input[type=search] { width: 100%; }
  .table-tools nav { width: 100%; justify-content: space-between; }

}

@media print {
  .shell { display: block; }
  .sidebar, .mobile-header { display: none !important; }
  .table-scroll { overflow: visible; margin: 0; }
  .table-scroll table { min-width: 0; }
}

/* ---- Sidebar footer (name, role, log out) ---- */
.sidebar-user { margin-top: auto; padding: 14px 18px; border-top: 1px solid rgba(28,89,55,0.2); }
.sidebar-user-name { font-size: 13px; color: var(--navy); font-weight: 600; }
.sidebar-user-role { font-size: 11.5px; color: #3f6b52; margin-bottom: 8px; }
.sidebar .sidebar-logout { width: 100%; background: transparent; color: var(--navy); border-color: rgba(28,89,55,0.35); }
.sidebar .sidebar-logout:hover { background: var(--tint-strong); }
