/* Mobile-first. Base styles target ~360px phones; wider layouts are added with min-width queries. */
:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #1b1f24;
  --muted: #5b6470;
  --border: #d9dde3;
  --accent: #1f5fbf;
  --accent-text: #ffffff;
  --ok: #1d7a3a;
  --ok-bg: #e3f4e8;
  --warn: #8a5a00;
  --warn-bg: #fff3d6;
  --bad: #b3261e;
  --bad-bg: #fde7e5;
  --info-bg: #e6effc;
  --radius: 10px;
  --gap: 16px;
  --tap: 44px;
  --header-h: 56px;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  line-height: 1.5;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --surface: #1b2027;
    --surface-2: #232a33;
    --text: #e8eaed;
    --muted: #a3acb8;
    --border: #333b46;
    --accent: #6ea3ff;
    --accent-text: #0b1220;
    --ok: #5fd08a;
    --ok-bg: #173323;
    --warn: #f2c464;
    --warn-bg: #3a2e12;
    --bad: #ff8a80;
    --bad-bg: #3d1c1a;
    --info-bg: #1b2a44;
  }
}

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

/* `hidden` must win over layout classes such as .field and .form-row. */
[hidden] {
  display: none !important;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  overflow-wrap: break-word;
}

h1 {
  font-size: 1.5rem;
  margin: 0 0 var(--gap);
  line-height: 1.25;
}

h2 {
  font-size: 1.1rem;
  margin: 0 0 12px;
}

h3 {
  font-size: 1rem;
  margin: 0 0 8px;
}

p {
  margin: 0 0 12px;
}

a {
  color: var(--accent);
}

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.skip-link {
  position: absolute;
  left: -999px;
}

.skip-link:focus {
  left: var(--gap);
  top: var(--gap);
  z-index: 30;
  background: var(--surface);
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
  padding: 8px 12px;
}

/* ---------- Shell ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  height: var(--header-h);
  padding: 0 8px 0 4px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.app-title {
  flex: 1;
  display: inline-flex;
  align-items: center;
  min-height: var(--tap);
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.menu-button {
  min-width: var(--tap);
  min-height: var(--tap);
  font-size: 1.4rem;
  background: none;
  border: 0;
  color: var(--text);
  cursor: pointer;
}

.user-box {
  display: flex;
  align-items: center;
  gap: 4px;
}

.user-name {
  display: none;
  min-height: var(--tap);
  align-items: center;
  color: var(--muted);
  text-decoration: none;
  padding: 0 8px;
}

.layout {
  display: flex;
  min-height: calc(100dvh - var(--header-h));
}

/* Phone: navigation is a slide-in drawer. */
.app-nav {
  position: fixed;
  top: var(--header-h);
  bottom: 0;
  left: 0;
  z-index: 15;
  width: min(280px, 85vw);
  display: flex;
  flex-direction: column;
  padding: 8px 0;
  overflow-y: auto;
  background: var(--surface);
  border-right: 1px solid var(--border);
  transform: translateX(-100%);
  transition: transform 0.2s ease;
  visibility: hidden;
}

.menu-open .app-nav {
  transform: none;
  visibility: visible;
}

.menu-backdrop {
  display: none;
}

.menu-open .menu-backdrop {
  display: block;
  position: fixed;
  inset: var(--header-h) 0 0 0;
  z-index: 14;
  background: rgb(0 0 0 / 35%);
}

.nav-item {
  display: flex;
  align-items: center;
  min-height: var(--tap);
  padding: 8px 20px;
  color: var(--text);
  text-decoration: none;
}

.nav-item[aria-current='page'] {
  color: var(--accent);
  font-weight: 600;
  background: var(--surface-2);
  box-shadow: inset 3px 0 0 var(--accent);
}

.content {
  flex: 1;
  min-width: 0;
  padding: var(--gap);
  max-width: 1100px;
}

.content:focus {
  outline: none;
}

.loading,
.empty {
  color: var(--muted);
}

.lead {
  color: var(--muted);
}

.muted {
  color: var(--muted);
  font-size: 0.9rem;
}

/* ---------- Building blocks ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--gap);
  margin-bottom: var(--gap);
}

.card-head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 8px;
}

.card-head h2 {
  margin: 0;
}

.stack > * + * {
  margin-top: 12px;
}

.grid {
  display: grid;
  gap: var(--gap);
  grid-template-columns: 1fr;
}

.stats {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  margin-bottom: var(--gap);
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  text-decoration: none;
  color: var(--text);
}

.stat-value {
  display: block;
  font-size: 1.6rem;
  font-weight: 700;
  line-height: 1.2;
}

.stat-label {
  color: var(--muted);
  font-size: 0.85rem;
}

.stat-warn {
  border-color: var(--warn);
  background: var(--warn-bg);
}

.stat-bad {
  border-color: var(--bad);
  background: var(--bad-bg);
}

.kv {
  display: grid;
  /* Labels get at least 6em; values never break inside a word or number. */
  grid-template-columns: minmax(6em, 1fr) minmax(min-content, 1.4fr);
  gap: 4px 12px;
  margin: 0 0 12px;
}

.kv dt {
  color: var(--muted);
}

.kv dd {
  margin: 0;
  text-align: right;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.badges {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.status {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: capitalize;
  background: var(--surface-2);
  color: var(--muted);
  white-space: nowrap;
}

.status-active,
.status-approved,
.status-closed,
.status-ok,
.status-received,
.status-balanced,
.status-completed {
  background: var(--ok-bg);
  color: var(--ok);
}

.status-draft,
.status-pending,
.status-open,
.status-released,
.status-in_progress,
.status-partially_received,
.status-temporary_password,
.status-awaiting_qa {
  background: var(--info-bg);
  color: var(--accent);
}

.status-due,
.status-locked,
.status-over_allowance,
.status-excess,
.status-variance {
  background: var(--warn-bg);
  color: var(--warn);
}

.status-rejected,
.status-inactive,
.status-cancelled,
.status-retired,
.status-unbalanced {
  background: var(--bad-bg);
  color: var(--bad);
}

.tabs {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  margin-bottom: var(--gap);
  border-bottom: 1px solid var(--border);
}

.tab {
  min-height: var(--tap);
  padding: 8px 14px;
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  white-space: nowrap;
  cursor: pointer;
  border-bottom: 3px solid transparent;
}

.tab[aria-selected='true'] {
  color: var(--accent);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

/* ---------- Forms ---------- */
.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 12px;
}

.field label,
legend {
  font-weight: 600;
  font-size: 0.95rem;
}

.hint {
  color: var(--muted);
  font-size: 0.85rem;
}

input:not([type='checkbox']):not([type='radio']),
select,
textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 8px 12px;
  font: inherit;
  font-size: 16px; /* prevents iOS zoom on focus */
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}

textarea {
  min-height: 80px;
}

input:user-invalid,
select:user-invalid {
  border-color: var(--bad);
}

.checks {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  margin: 0 0 12px;
}

.check {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: var(--tap);
}

.check input {
  width: 22px;
  height: 22px;
}

.form-row {
  display: grid;
  gap: 0 12px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.form-error,
.form-info {
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 12px;
  background: var(--bad-bg);
  color: var(--bad);
}

.form-error.form-info,
.form-info {
  background: var(--info-bg);
  color: var(--text);
}

.form-error p:last-child,
.form-error ul {
  margin-bottom: 0;
}

.notice {
  border-left: 4px solid var(--warn);
  background: var(--warn-bg);
  padding: 12px;
  border-radius: 8px;
  margin-bottom: var(--gap);
}

.secret {
  display: inline-block;
  font-size: 1.1rem;
  padding: 4px 8px;
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: 6px;
  user-select: all;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.button,
.button-secondary,
.button-danger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap);
  padding: 10px 16px;
  font: inherit;
  font-weight: 600;
  border-radius: var(--radius);
  cursor: pointer;
  text-decoration: none;
  flex: 1 1 auto;
}

.button {
  color: var(--accent-text);
  background: var(--accent);
  border: 1px solid var(--accent);
}

.button-secondary {
  color: var(--accent);
  background: var(--surface);
  border: 1px solid var(--accent);
}

.button-danger {
  color: #fff;
  background: var(--bad);
  border: 1px solid var(--bad);
}

.button-link {
  min-height: var(--tap);
  padding: 0 8px;
  background: none;
  border: 0;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
}

button:disabled {
  opacity: 0.55;
  cursor: progress;
}

/* ---------- Tables: stacked cards on phones ---------- */
.table-wrap {
  margin-bottom: 12px;
}

table.data {
  width: 100%;
  border-collapse: collapse;
}

table.data caption {
  text-align: left;
  font-weight: 600;
  padding-bottom: 8px;
}

table.data thead {
  display: none;
}

table.data tr {
  display: block;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  margin-bottom: 8px;
  background: var(--surface);
}

table.data td {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 0;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

table.data td::before {
  content: attr(data-label);
  color: var(--muted);
  text-align: left;
  font-weight: 500;
}

table.data td[data-label='']::before {
  content: none;
}

table.data tr.row-warn {
  border-color: var(--warn);
  background: var(--warn-bg);
}

table.data tr.row-bad {
  border-color: var(--bad);
  background: var(--bad-bg);
}

.list-link {
  display: block;
  color: inherit;
  text-decoration: none;
}

/* ---------- Toasts ---------- */
.toasts {
  position: fixed;
  left: var(--gap);
  right: var(--gap);
  bottom: var(--gap);
  z-index: 40;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--text);
  color: var(--bg);
  box-shadow: 0 4px 16px rgb(0 0 0 / 25%);
}

.toast-error {
  background: var(--bad);
  color: #fff;
}

/* ---------- Sign-in page ---------- */
.auth-page {
  display: flex;
  justify-content: center;
  padding: var(--gap);
}

.auth-card {
  width: 100%;
  max-width: 420px;
  margin-top: 8vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px var(--gap);
}

.auth-card h1 {
  font-size: 1.25rem;
}

.auth-card .button {
  width: 100%;
  margin: 8px 0 12px;
}

/* ---------- Larger screens ---------- */
@media (min-width: 640px) {
  .stats {
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  }

  .grid {
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  }

  .button,
  .button-secondary,
  .button-danger {
    flex: 0 0 auto;
  }

  .user-name {
    display: inline-flex;
  }

  table.data thead {
    display: table-header-group;
  }

  table.data tr {
    display: table-row;
    border: 0;
    border-bottom: 1px solid var(--border);
    border-radius: 0;
    padding: 0;
    margin: 0;
    background: none;
  }

  table.data th {
    text-align: left;
    color: var(--muted);
    font-weight: 600;
    font-size: 0.85rem;
    padding: 8px;
    border-bottom: 2px solid var(--border);
  }

  table.data td {
    display: table-cell;
    padding: 8px;
    text-align: left;
    vertical-align: middle;
  }

  table.data td::before {
    content: none;
  }

  .table-wrap {
    overflow-x: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
  }
}

@media (min-width: 900px) {
  .menu-button,
  .menu-backdrop,
  .menu-open .menu-backdrop {
    display: none;
  }

  .app-header {
    padding-left: var(--gap);
  }

  .app-nav {
    position: sticky;
    top: var(--header-h);
    height: calc(100dvh - var(--header-h));
    width: 220px;
    flex: 0 0 220px;
    transform: none;
    visibility: visible;
    transition: none;
  }

  .content {
    padding: 24px 32px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .app-nav {
    transition: none;
  }
}

.grid > .card {
  margin-bottom: 0;
}

.grid {
  margin-bottom: var(--gap);
}

details > summary {
  display: inline-flex;
  align-items: center;
  min-height: var(--tap);
  cursor: pointer;
  list-style-position: inside;
}

details[open] > summary {
  margin-bottom: 8px;
}

details form {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.back-link {
  display: inline-flex;
  align-items: center;
  min-height: var(--tap);
  margin-bottom: 4px;
}

a.tab {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}

.danger-zone {
  margin-top: 32px;
  padding-top: var(--gap);
  border-top: 1px solid var(--border);
}

/* ---------- Dashboard ---------- */
.pill {
  display: inline-block;
  padding: 8px 14px;
  border-radius: var(--radius);
  font-weight: 600;
  margin-bottom: var(--gap);
}

.pill-ok {
  background: var(--ok-bg);
  color: var(--ok);
}

.pill-bad {
  background: var(--bad-bg);
  color: var(--bad);
}

.bars {
  list-style: none;
  margin: 0;
  padding: 0;
}

.bar-row {
  display: grid;
  grid-template-columns: minmax(80px, 30%) 1fr auto;
  align-items: center;
  gap: 8px;
  min-height: 32px;
}

.bar-label {
  color: var(--muted);
  font-size: 0.9rem;
}

.bar-track {
  height: 12px;
}

.bar-fill {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 0 4px 4px 0;
}

.bar-row:hover .bar-fill {
  filter: brightness(1.15);
}

.bar-value {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.size-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0 12px;
}

@media (min-width: 640px) {
  .size-grid {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }
}

.bad {
  color: var(--bad);
}

.stat-label {
  display: block;
}

.kv dd a,
.notice li a {
  display: inline-flex;
  align-items: center;
  min-height: var(--tap);
  min-width: var(--tap);
}

.reconciliation {
  margin-bottom: var(--gap);
}

/* ---------- Printing: reports are saved as PDF from the browser ---------- */
@page {
  size: A4 landscape;
  margin: 12mm;
}

@media print {
  * {
    color: #000 !important;
    background: transparent !important;
    box-shadow: none !important;
  }

  :root {
    color-scheme: light;
  }

  html,
  body {
    background: #fff !important;
  }

  body {
    font-size: 10pt;
  }

  .skip-link,
  .app-header,
  .app-nav,
  .menu-backdrop,
  .toasts,
  .no-print {
    display: none !important;
  }

  .content {
    padding: 0;
  }

  .table-wrap {
    overflow: visible;
    border: 0;
  }

  table.data thead {
    display: table-header-group;
  }

  table.data tr {
    display: table-row;
    border: 0;
    border-bottom: 1px solid #999;
    border-radius: 0;
    padding: 0;
    margin: 0;
    break-inside: avoid;
  }

  table.data th,
  table.data td {
    display: table-cell;
    padding: 3px 4px;
    text-align: left;
    font-size: 8.5pt;
  }

  table.data td::before {
    content: none;
  }
}
