:root {
  --color-bg: #f5f6f8;
  --color-surface: #ffffff;
  --color-border: #e2e5ea;
  --color-text: #1c2430;
  --color-text-muted: #6b7280;
  --color-primary: #1f4e79;
  --color-primary-dark: #163a5c;
  --color-danger: #b3261e;
  --color-danger-bg: #fdecea;
  --color-success: #1e7a3c;
  --color-warning: #92620d;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
}

* {
  box-sizing: border-box;
}

/* Classes like .btn set display, which would otherwise override the hidden attribute. */
[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  overflow-x: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
}

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

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  padding: 14px 24px;
}

.topbar h1 {
  font-size: 18px;
  margin: 0;
  font-weight: 600;
}

.topbar .subtitle {
  font-size: 13px;
  color: var(--color-text-muted);
}

.topbar-right {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px 14px;
  font-size: 14px;
}

.badge {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 8px;
  border-radius: 999px;
  background: #eef2f7;
  color: var(--color-primary-dark);
  font-weight: 600;
}

/* ---------- Layout ---------- */
.container {
  max-width: 1280px;
  margin: 0 auto;
  padding: 24px;
}

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* ---------- Dashboard summary ---------- */
.summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}

.summary-tile {
  padding: 16px;
}

.summary-tile .label {
  font-size: 12px;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 6px;
}

.summary-tile .value {
  font-size: 20px;
  font-weight: 600;
}

.summary-tile .value-sub {
  font-size: 13px;
  color: var(--color-text-muted);
  margin-top: 2px;
}

.summary-tile.grand-total {
  border-color: var(--color-primary);
  background: #f3f7fb;
}

.summary-tile.paid-total .value {
  color: var(--color-success);
}

.summary-tile.remaining-total .value {
  color: var(--color-warning);
}

.tile-split {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 2px 8px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--color-border);
  font-size: 12px;
  color: var(--color-text-muted);
}

/* Project-wide paid / remaining / grand total — sits above the per-category tiles. */
.weight-missing {
  color: var(--color-warning);
}

.project-totals {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 12px;
  margin-bottom: 12px;
}

.project-totals .summary-tile .value {
  font-size: 22px;
}

/* Five tiles get cramped on tablets / small laptops: money on one row, weight + volume on the next. */
@media (max-width: 1100px) and (min-width: 641px) {
  .project-totals {
    grid-template-columns: repeat(6, 1fr);
  }

  .project-totals .summary-tile {
    grid-column: span 2;
  }

  .project-totals .weight-total,
  .project-totals .volume-total {
    grid-column: span 3;
  }
}

/* ---------- Controls row ---------- */
.controls-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.controls-left {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.filter-chip {
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
  color: var(--color-text);
}

.filter-chip.active {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
}

/* ---------- Buttons ---------- */
button {
  font-family: inherit;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text);
  padding: 8px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  cursor: pointer;
}

.btn:hover {
  background: #f0f2f5;
}

.btn-primary {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--color-primary-dark);
}

.btn-danger {
  color: var(--color-danger);
  border-color: #f0c6c3;
  background: var(--color-danger-bg);
}

.btn-icon {
  padding: 6px 8px;
  font-size: 13px;
}

.btn-small {
  padding: 5px 10px;
  font-size: 13px;
}

/* ---------- Table ---------- */
.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

thead th {
  text-align: left;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--color-text-muted);
  padding: 10px 16px;
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
  vertical-align: bottom;
}

tbody td {
  padding: 10px 16px;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
}

tbody tr:last-child td {
  border-bottom: none;
}

td.item-name {
  min-width: 180px;
}

/* Admins click the item name to edit it. */
.item-name-link {
  padding: 0;
  border: none;
  background: none;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.item-name-link:hover {
  color: var(--color-primary);
  text-decoration: underline;
}

/* The Edit column stays pinned to the right edge while the wide table scrolls sideways. */
.actions-cell {
  position: sticky;
  right: 0;
  background: var(--color-surface);
  box-shadow: inset 1px 0 0 var(--color-border);
}

thead th.actions-cell,
tbody td.actions-cell {
  padding-left: 10px;
  padding-right: 10px;
}

tr.group-row td.actions-cell {
  background: #f7f8fa;
}

tr.subtotal-row td.actions-cell {
  background: #fbfbfc;
}

tr.total-row td.actions-cell {
  background: #f3f7fb;
}

th.num,
td.num {
  text-align: right;
  white-space: nowrap;
}

tr.group-row td {
  background: #f7f8fa;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-primary-dark);
  padding-top: 8px;
  padding-bottom: 8px;
}

tr.subtotal-row td {
  font-weight: 600;
  background: #fbfbfc;
  border-bottom: 2px solid var(--color-border);
}

tr.total-row td {
  font-weight: 700;
  font-size: 15px;
  padding: 14px 16px;
  background: #f3f7fb;
  border-top: 2px solid var(--color-primary);
}

/* "📎 2 invoices / receipts" link under the item name */
.doc-link {
  display: block;
  margin-top: 4px;
  padding: 0;
  border: none;
  background: none;
  color: var(--color-primary);
  font-size: 12px;
  cursor: pointer;
  text-align: left;
}

.doc-link:hover {
  text-decoration: underline;
}

.doc-link-muted {
  color: var(--color-text-muted);
}

/* ---------- Invoices / receipts modal ---------- */
.docs-item-name {
  margin-top: -8px;
}

.doc-list {
  list-style: none;
  margin: 0 0 16px;
  padding: 0;
}

.doc-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--color-border);
}

.doc-row:last-child {
  border-bottom: none;
}

.doc-thumb {
  display: block;
  width: 48px;
  height: 48px;
  border-radius: 6px;
  border: 1px solid var(--color-border);
  object-fit: cover;
  background: #eef1f4;
  flex-shrink: 0;
}

.doc-thumb-pdf {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: var(--color-danger);
  background: var(--color-danger-bg);
}

.doc-meta {
  flex: 1;
  min-width: 0;
}

.doc-name {
  display: block;
  font-size: 14px;
  overflow-wrap: anywhere;
}

.doc-empty {
  padding: 20px 0;
}

.doc-upload {
  padding-top: 12px;
  border-top: 1px solid var(--color-border);
}

.paid-in-full {
  font-size: 12px;
  font-weight: 600;
  color: var(--color-success);
}

.hint.hint-error {
  color: var(--color-danger);
}

.item-thumb {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  object-fit: cover;
  background: #eef1f4;
  display: block;
}

.item-thumb-placeholder {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  background: #eef1f4;
}

.status-pill {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 999px;
  font-weight: 600;
  display: inline-block;
}

.status-Needed { background: #f2f2f2; color: #555; }
.status-Ordered { background: #fff4e0; color: #92620d; }
.status-Purchased { background: #e3f0ff; color: #1f4e79; }
.status-Shipped { background: #eee5ff; color: #5a2ea6; }
.status-Received { background: #e4f7e9; color: #1e7a3c; }


.empty-state {
  padding: 40px 16px;
  text-align: center;
  color: var(--color-text-muted);
}

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(16, 24, 40, 0.45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  overflow-y: auto;
  z-index: 50;
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: var(--color-surface);
  border-radius: var(--radius);
  width: 100%;
  max-width: 480px;
  padding: 24px;
  box-shadow: 0 10px 30px rgba(16, 24, 40, 0.2);
}

.modal h2 {
  margin-top: 0;
  font-size: 17px;
}

.field {
  margin-bottom: 14px;
}

.field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 4px;
}

.field input[type="text"],
.field input[type="number"],
.field input[type="email"],
.field input[type="password"],
.field select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
}

.field-row {
  display: flex;
  gap: 12px;
}

.field-row .field {
  flex: 1;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
}

.modal-actions-left {
  margin-right: auto;
}

.form-error {
  color: var(--color-danger);
  font-size: 13px;
  margin-top: 8px;
  min-height: 16px;
}

/* ---------- Login page ---------- */
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.auth-card {
  width: 100%;
  max-width: 380px;
  padding: 32px;
}

.auth-card h1 {
  font-size: 20px;
  margin: 0 0 4px;
}

.auth-card .subtitle {
  color: var(--color-text-muted);
  font-size: 14px;
  margin-bottom: 20px;
}

.auth-toggle {
  display: flex;
  gap: 16px;
  margin-top: 16px;
  font-size: 13px;
}

.auth-toggle button {
  background: none;
  border: none;
  padding: 0;
  color: var(--color-primary);
  cursor: pointer;
  font-size: 13px;
  text-decoration: underline;
}

.hint {
  font-size: 12px;
  color: var(--color-text-muted);
  margin-top: 4px;
}

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

/* ---------- Mobile ---------- */
@media (max-width: 640px) {
  .topbar {
    padding: 12px 16px;
  }

  .topbar h1 {
    font-size: 16px;
  }

  .topbar-right {
    font-size: 13px;
    gap: 8px 10px;
  }

  .container {
    padding: 16px;
  }

  .summary-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }

  .project-totals {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-bottom: 8px;
  }

  .project-totals .grand-total {
    grid-column: 1 / -1;
    order: -1;
  }

  .project-totals .summary-tile .value {
    font-size: 18px;
  }

  tr.total-row td {
    padding: 10px;
  }

  .summary-tile {
    padding: 12px;
  }

  .summary-tile .value {
    font-size: 17px;
  }

  .controls-row {
    flex-direction: column;
    align-items: stretch;
  }

  #addItemBtn {
    width: 100%;
  }

  thead th,
  tbody td {
    padding: 8px 10px;
    white-space: nowrap;
  }

  .modal-overlay {
    padding: 0;
    align-items: stretch;
  }

  .modal {
    max-width: none;
    width: 100%;
    min-height: 100%;
    border-radius: 0;
  }

  .field-row {
    flex-direction: column;
    gap: 0;
  }

  .auth-card {
    padding: 24px;
  }
}
