:root {
  --navy: #0F1729;
  --navy-light: #1B2A4A;
  --orange: #E8772E;
  --orange-dark: #D4651E;
  --bg: #F4F5F7;
  --card-bg: #FFFFFF;
  --border: #E3E5E9;
  --text: #1A1D29;
  --text-muted: #6B7280;
  --text-faint: #9CA3AF;
  --green: #1E8E5A;
  --red-bg: #FDEDEE;
  --red-text: #C0392B;
  --amber-bg: #FFF6E0;
  --radius: 8px;
  --mono: 'SF Mono', 'Consolas', 'Menlo', monospace;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

a { color: inherit; text-decoration: none; }

/* ---------- Top nav ---------- */
.topnav {
  background: var(--navy);
  border-bottom: 1px solid #000;
}
.topnav-inner {
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 52px;
}
.brand {
  font-weight: 800;
  font-size: 17px;
  letter-spacing: 0.5px;
  color: #fff;
}
.brand-accent { color: var(--orange); }
.nav-links {
  display: flex;
  align-items: center;
  gap: 4px;
}
.nav-link {
  color: #C7CCD9;
  font-size: 13.5px;
  font-weight: 500;
  padding: 7px 14px;
  border-radius: 6px;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
}
.nav-link:hover { color: #fff; background: rgba(255,255,255,0.06); }
.nav-link.active {
  background: var(--orange);
  color: #fff;
}
.nav-disabled { opacity: 0.35; cursor: default; pointer-events: none; }
.nav-muted { color: #6B7280; }
.nav-sep {
  width: 1px;
  height: 22px;
  background: rgba(255,255,255,0.12);
  margin: 0 6px;
}

/* ---------- Page ---------- */
.page {
  max-width: 1400px;
  margin: 0 auto;
  padding: 32px 24px 80px;
}
.page-title {
  font-size: 26px;
  font-weight: 800;
  margin: 0 0 2px;
  color: var(--text);
}
.page-subtitle {
  color: var(--text-muted);
  font-size: 14px;
  margin: 0 0 24px;
}

/* ---------- Card / filter bar ---------- */
.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 20px;
}
.filter-bar {
  padding: 20px 24px;
  display: flex;
  gap: 20px;
  align-items: flex-end;
  flex-wrap: wrap;
}
.filter-group { display: flex; flex-direction: column; gap: 6px; }
.filter-group.grow { flex: 1; min-width: 260px; }
.filter-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.6px;
  color: var(--text-faint);
  text-transform: uppercase;
}
.input, .select {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 9px 12px;
  font-size: 13.5px;
  background: #fff;
  color: var(--text);
  font-family: inherit;
}
.input:focus, .select:focus { outline: 2px solid var(--orange); outline-offset: -1px; }
.input { width: 100%; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover { background: #F7F8FA; }
.btn-primary {
  background: var(--orange);
  border-color: var(--orange);
  color: #fff;
}
.btn-primary:hover { background: var(--orange-dark); border-color: var(--orange-dark); }
.btn-sm { padding: 5px 12px; font-size: 12.5px; }
.btn-outline-block { width: 100%; justify-content: center; }

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 13px;
  color: var(--text-muted);
  padding-bottom: 9px;
}
.checkbox-row input { width: 15px; height: 15px; accent-color: var(--orange); }

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; }
table.price-table {
  width: 100%;
  border-collapse: collapse;
}
.price-table thead th {
  text-align: left;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.6px;
  color: var(--text-faint);
  text-transform: uppercase;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.price-table thead th.num { text-align: right; }
.price-table tbody td {
  padding: 13px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
  vertical-align: middle;
}
.price-table tbody td.num {
  text-align: right;
  font-family: var(--mono);
  font-size: 13px;
}
.price-table tbody tr.product-row { cursor: pointer; transition: background 0.12s; }
.price-table tbody tr.product-row:hover { background: #FAFBFC; }
.price-table tbody tr.alert-row { background: var(--red-bg); }
.price-table tbody tr.alert-row:hover { background: #FBE2E4; }

.product-name { font-weight: 600; }
.product-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 12px;
  color: var(--text-muted);
  margin-right: 8px;
  transition: transform 0.15s;
}
tr.expanded .product-toggle { transform: rotate(90deg); }

.unit-tag {
  font-size: 11px;
  color: var(--text-faint);
  font-style: italic;
}
.pill {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 100px;
  font-size: 11.5px;
  font-weight: 700;
}
.pill-alert { background: #FBE2E4; color: var(--red-text); }
.pill-flag { background: var(--amber-bg); color: #92660C; margin-left: 6px; }
.pct-up { color: var(--red-text); font-weight: 700; }
.pct-down { color: var(--green); font-weight: 700; }
.pct-flat { color: var(--text-faint); }

.count-badge {
  font-size: 12px;
  color: var(--text-muted);
  background: #F0F1F4;
  padding: 2px 9px;
  border-radius: 100px;
  font-weight: 600;
}

/* ---------- Expanded history row ---------- */
.history-row td {
  padding: 0;
  background: #FAFAFB;
}
.history-panel { padding: 4px 14px 16px 46px; }
.history-table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}
.history-table th {
  text-align: left;
  font-size: 10.5px;
  font-weight: 700;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 8px 12px;
  background: #F7F8FA;
  border-bottom: 1px solid var(--border);
}
.history-table th.num { text-align: right; }
.history-table td {
  padding: 8px 12px;
  font-size: 12.5px;
  border-bottom: 1px solid #F0F1F3;
}
.history-table td.num { text-align: right; font-family: var(--mono); }
.history-table tr:last-child td { border-bottom: none; }
.history-loading { padding: 16px; color: var(--text-muted); font-size: 13px; }
.flag-note { color: var(--red-text); font-size: 11.5px; font-style: italic; }

/* ---------- Empty state ---------- */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}
.empty-state h3 { color: var(--text); margin-bottom: 6px; }

/* ---------- Form page ---------- */
.form-card { max-width: 560px; padding: 28px; }
.form-row { margin-bottom: 16px; }
.form-row label {
  display: block;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
.error-banner {
  background: var(--red-bg);
  color: var(--red-text);
  padding: 10px 14px;
  border-radius: 6px;
  font-size: 13px;
  margin-bottom: 16px;
}
.hint { font-size: 12px; color: var(--text-faint); margin-top: 4px; }

/* ---------- Responsive ---------- */
@media (max-width: 720px) {
  .topnav-inner { padding: 0 12px; }
  .nav-links { gap: 0; }
  .nav-link { padding: 6px 9px; font-size: 12px; }
  .page { padding: 20px 12px 60px; }
  .form-grid { grid-template-columns: 1fr; }
  .price-table thead th:nth-child(n+5),
  .price-table tbody td:nth-child(n+5) { display: none; }
}

/* ---------- Login ---------- */
.login-body {
  background: var(--navy);
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}
.login-card {
  background: #fff;
  border-radius: var(--radius);
  padding: 36px 32px;
  width: 360px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.35);
}
.login-brand { font-weight: 800; font-size: 20px; color: var(--navy); text-align: center; }
.login-sub { text-align: center; color: var(--text-muted); font-size: 13px; margin: 4px 0 22px; }

/* ---------- Flash messages ---------- */
.flash-stack { margin-bottom: 18px; display: flex; flex-direction: column; gap: 8px; }
.flash {
  padding: 11px 16px;
  border-radius: 6px;
  font-size: 13.5px;
  font-weight: 500;
}
.flash-success { background: #E6F4EC; color: var(--green); }
.flash-error { background: var(--red-bg); color: var(--red-text); }

.nav-user {
  color: #9aa3b5;
  font-size: 12.5px;
  font-weight: 600;
  padding: 0 4px;
}

/* ---------- Suppliers page ---------- */
.supplier-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.supplier-card:last-child { border-bottom: none; }
.supplier-name { font-weight: 700; font-size: 14.5px; }
.supplier-meta { color: var(--text-muted); font-size: 12.5px; margin-top: 2px; }
.supplier-stats { display: flex; gap: 22px; align-items: center; }
.stat-block { text-align: right; }
.stat-num { font-size: 17px; font-weight: 700; font-family: var(--mono); }
.stat-label { font-size: 10.5px; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.4px; }

.detail-grid {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 20px;
}
.detail-side { display: flex; flex-direction: column; gap: 16px; }
.kv-row { display: flex; justify-content: space-between; padding: 9px 0; border-bottom: 1px solid #F0F1F3; font-size: 13px; }
.kv-row:last-child { border-bottom: none; }
.kv-label { color: var(--text-muted); }

/* ---------- Costing builder ---------- */
.costing-grid { display: grid; grid-template-columns: 1fr 360px; gap: 20px; }
.ingredient-row {
  display: grid;
  grid-template-columns: 1fr 110px 100px 36px;
  gap: 10px;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #F0F1F3;
}
.ingredient-row .line-cost { font-family: var(--mono); font-weight: 600; text-align: right; }
.remove-btn {
  background: none;
  border: none;
  color: var(--text-faint);
  font-size: 16px;
  cursor: pointer;
  width: 28px; height: 28px;
  border-radius: 6px;
}
.remove-btn:hover { background: var(--red-bg); color: var(--red-text); }
.costing-summary {
  background: var(--navy);
  color: #fff;
  border-radius: var(--radius);
  padding: 22px;
  position: sticky;
  top: 20px;
}
.costing-summary .total-label { font-size: 11px; letter-spacing: 0.6px; text-transform: uppercase; color: #9aa3b5; }
.costing-summary .total-value { font-size: 32px; font-weight: 800; font-family: var(--mono); color: var(--orange); margin: 4px 0 18px; }
.costing-summary .sub-line { display: flex; justify-content: space-between; font-size: 12.5px; padding: 5px 0; color: #C7CCD9; }

/* ---------- Admin ---------- */
.stat-cards { display: grid; grid-template-columns: repeat(6, 1fr); gap: 14px; margin-bottom: 24px; }
.stat-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
}
.stat-card .big-num { font-size: 26px; font-weight: 800; color: var(--navy); font-family: var(--mono); }
.stat-card .label { font-size: 11px; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.4px; margin-top: 4px; }
.stat-card.warn .big-num { color: var(--red-text); }

.admin-section-title {
  font-size: 15px;
  font-weight: 700;
  margin: 28px 0 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.activity-row { display: flex; gap: 14px; padding: 10px 14px; border-bottom: 1px solid #F0F1F3; font-size: 12.5px; align-items: baseline; }
.activity-row .time { color: var(--text-faint); width: 140px; flex-shrink: 0; font-family: var(--mono); }
.activity-row .who { font-weight: 600; width: 90px; flex-shrink: 0; }
.activity-row .what { color: var(--text-muted); }
.tag-action {
  display: inline-block;
  font-size: 10.5px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 100px;
  background: #EEF1F8;
  color: var(--navy-light);
  text-transform: uppercase;
}

.admin-nav { display: flex; gap: 6px; margin-bottom: 20px; }
.admin-nav a {
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}
.admin-nav a.active { background: var(--navy); color: #fff; }
.admin-nav a:hover:not(.active) { background: #F0F1F4; }

.role-badge {
  font-size: 10.5px;
  font-weight: 700;
  padding: 2px 9px;
  border-radius: 100px;
  text-transform: uppercase;
}
.role-admin { background: #FDEEDD; color: var(--orange-dark); }
.role-user { background: #EEF1F8; color: var(--navy-light); }
.status-dot { display:inline-block; width:8px; height:8px; border-radius:50%; margin-right:6px; }
.status-active { background: var(--green); }
.status-inactive { background: #C7CCD9; }

.inline-form { display: inline; }
.modal-overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(15,23,41,0.5);
  z-index: 100;
  align-items: center;
  justify-content: center;
}
.modal-overlay.open { display: flex; }
.modal-box {
  background: #fff;
  border-radius: var(--radius);
  padding: 24px;
  width: 380px;
}
.modal-box h3 { margin: 0 0 16px; font-size: 16px; }

@media (max-width: 900px) {
  .stat-cards { grid-template-columns: repeat(3, 1fr); }
  .costing-grid { grid-template-columns: 1fr; }
  .detail-grid { grid-template-columns: 1fr; }
}

.history-actions { padding: 4px 0 10px; }
.flag-note a { color: var(--red-text); font-weight: 600; text-decoration: underline; }
.delete-purchase { color: var(--text-faint); text-decoration: none; cursor: pointer; }
.delete-purchase:hover { color: var(--red-text); }

.row-actions { font-size: 11.5px; white-space: nowrap; }
.row-actions a { color: var(--text-muted); text-decoration: none; }
.row-actions a:hover { color: var(--orange-dark); text-decoration: underline; }

@media (max-width: 768px) {
  .topnav-inner { padding: 0 8px; gap: 8px; }
  .brand { font-size: 14px; }
  .nav-links { gap: 0; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .nav-link { padding: 5px 8px; font-size: 11px; white-space: nowrap; }
  .nav-sep { display: none; }
  .nav-user { display: none; }

  .page { padding: 16px 8px 40px; }
  .page-title { font-size: 22px; }
  .page-subtitle { font-size: 13px; margin-bottom: 16px; }

  .filter-bar { flex-direction: column; gap: 12px; padding: 16px; }
  .filter-group { width: 100%; }
  .filter-group.grow { min-width: 0; }
  .input, .select { width: 100%; font-size: 16px; padding: 10px 12px; }
  .btn { width: 100%; padding: 12px 16px; font-size: 15px; }
  .btn-sm { width: auto; }
  .checkbox-row { padding-bottom: 0; font-size: 14px; }

  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .price-table { min-width: 780px; }
  .price-table thead th { padding: 8px 10px; font-size: 10px; }
  .price-table tbody td { padding: 8px 10px; font-size: 12.5px; }

  .product-toggle { width: 26px; height: 26px; font-size: 16px; flex-shrink: 0; }
  .form-card, .login-card { max-width: 100%; padding: 20px; margin: 0 8px; }
  .modal-box { width: 90vw; }
  .stat-cards { grid-template-columns: repeat(2, 1fr); }
  .detail-grid { grid-template-columns: 1fr; }
}
