:root {
  --fg: #0f172a;
  --muted: #475569;
  --bg: #f8fafc;
  --border: #e2e8f0;
  --accent: #0ea5e9;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
               Roboto, Arial, sans-serif;
  font-size: 14px;
  color: var(--fg);
  background: var(--bg);
}

/* ---------- Page header ---------- */
header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
h1 { margin: 0; font-size: 20px; }
h2 { margin: 0 0 8px; font-size: 16px; }

/* ---------- Generic containers ---------- */
.container {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}
.card h3 { margin: 0 0 8px; font-size: 15px; }

/* ---------- Controls row ---------- */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  align-items: flex-end;
  margin-bottom: 8px;
}
.controls-spacer { flex: 1; }

label {
  font-size: 13px;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

input, select, button {
  font-family: inherit;
  font-size: 13px;
}

input, select {
  padding: 4px 6px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
}
input:focus, select:focus {
  outline: 2px solid #e0f2fe;
  border-color: var(--accent);
}

button {
  border-radius: 999px;
  border: 1px solid var(--border);
  padding: 6px 12px;
  background: #f8fafc;
  cursor: pointer;
}
button:disabled { opacity: .6; cursor: not-allowed; }

/* optional: highlight edited fields */
.input-dirty {
  background: #fefce8;
}

.unit-control {
  display: flex;
  align-items: center;
  gap: 6px;
}

.unit-step {
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 8px;
  border: 1px solid #d1d5db;
  background: #fff;
  color: #111827;
  font-weight: 700;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: border-color 120ms ease, background-color 120ms ease, box-shadow 120ms ease;
}

.unit-step:hover {
  border-color: #a7b8e5;
  background: #f8fafc;
  box-shadow: 0 0 0 2px #e7ecf8;
}

.unit-step:active {
  background: #eef2ff;
}

/* ---------- Table + sticky header ---------- */
.table-wrapper {
  max-height: 480px; /* scrolling body on desktop */
  overflow-y: auto;
  overflow-x: auto;  /* allow wide tables */
  width: 100%;
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
  min-width: 680px; /* prevents columns from squeezing too much */
}
.table th,
.table td {
  border-bottom: 1px solid #eef2f7;
  padding: 8px 6px;
  text-align: left;
  vertical-align: middle;
}
.table th { font-weight: 700; }

.table tr.modified-row td {
  background: #fef9c3;
}

.row-tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #fff7ed;
  color: #9a3412;
  border: 1px solid #fed7aa;
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 12px;
  font-weight: 600;
}

/* sticky header */
.table thead th {
  position: sticky;
  top: 0;
  background: #fff;
  z-index: 1;
  box-shadow: 0 1px 0 0 #eef2f7;
}

/* ---------- Badges ---------- */
.badge {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 999px;
  border: 1px solid var(--border);
  font-size: 12px;
}
.badge.low { background: #fef3c7; border-color: #f59e0b; }

/* ---------- Add form ---------- */
.add-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px 12px;
  margin-top: 4px;
}
.add-grid input { width: 100%; }
.add-actions { margin-top: 8px; text-align: right; }

/* ---------- Recurrence weekday picker ---------- */
.weekday-picker {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
  flex-wrap: wrap;
  margin: 6px 0 4px;
}

.weekday-chip {
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  border-radius: 50%;
  padding: 0;
  border: 1.5px solid #d1d5db;
  background: #fff;
  color: #1f2937;
  font-weight: 700;
  font-size: 12px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease, box-shadow 120ms ease;
}

.weekday-chip:not(:disabled):hover {
  border-color: #a7b8e5;
  box-shadow: 0 0 0 2px #e7ecf8;
}

.weekday-chip.selected {
  background: #7f94d4;
  border-color: #7f94d4;
  color: #fff;
  box-shadow: 0 0 0 2px rgba(127, 148, 212, 0.22);
}

.weekday-chip:disabled {
  background: #f8fafc;
  border-color: #e5e7eb;
  color: #9ca3af;
  opacity: 0.8;
}

.weekday-chip.selected:disabled {
  background: #b9c6ec;
  border-color: #b9c6ec;
  color: #f8fafc;
}

.weekday-hint {
  color: #6b7280;
  font-size: 12px;
  text-align: left;
}

.recurrence-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.weekday-stack {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 2px;
}

/* Low-stock popup + history download */
.lowstock-popup {
  width: 90%;
  max-width: 600px;
  margin: auto;
  background: white;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.25);
}

@media (max-width: 768px) {
  .lowstock-popup {
    width: 95%;
    padding: 16px;
    max-height: 80vh;
    overflow-y: auto;
  }
}

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

.lowstock-popup th, .lowstock-popup td {
  padding: 10px;
  border-bottom: 1px solid #ddd;
}

.history-download {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.history-download label {
  flex-direction: row;
  align-items: center;
  gap: 6px;
}

/* ---------- Sidebar shell (expand/collapse with pill toggle) ---------- */
.shell {
  --sbw: 260px;
  display: flex;
  min-height: 100vh;
}
.shell.closed { --sbw: 70px; }

.sidebar {
  position: relative;
  width: var(--sbw);
  background: #f7f8fb;
  border-right: 1px solid #e5e7eb;
  transition: width 200ms ease;
  padding: 12px 8px;
  overflow: visible; /* allow toggle + auth popover */
}

.content {
  flex: 1 1 auto;
  min-width: 0;
  padding: 16px;
}

.topbar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.notif-wrapper {
  position: relative;
}

.notif-btn {
  position: relative;
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 12px;
  border: 1px solid #e5e7eb;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(15,23,42,0.08);
  line-height: 1;
  box-sizing: border-box;
}
.notif-btn:hover { background: #f8fafc; }
.notif-btn svg {
  width: 20px;
  height: 20px;
  display: block;
}

.notif-badge {
  position: absolute;
  top: 0;
  right: 0;
  transform: translate(40%, -40%);
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: #3b82f6;
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  display: grid;
  place-items: center;
  border: 2px solid #fff;
  box-shadow: 0 2px 6px rgba(59,130,246,0.35);
}
.notif-badge.overdue {
  background: #ef4444;
  box-shadow: 0 2px 6px rgba(239,68,68,0.35);
}

.notif-panel {
  position: absolute;
  top: 44px;
  right: 0;
  width: 360px;
  max-width: 90vw;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  box-shadow: 0 16px 40px rgba(15,23,42,0.18);
  padding: 10px;
  z-index: 30;
}

.notif-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-weight: 600;
  font-size: 14px;
  padding-bottom: 6px;
  border-bottom: 1px solid #eef2f7;
}

.notif-alert,
.notif-alert-inline {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 6px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: #fef2f2;
  color: #b91c1c;
  border: 1px solid #fecdd3;
}
.notif-alert-inline { padding: 1px 6px; }

.notif-empty {
  text-align: center;
  color: #6b7280;
  font-size: 13px;
  padding: 12px 4px 6px;
}

.notif-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 8px;
  max-height: 320px;
  overflow-y: auto;
}

.notif-item {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  padding: 8px 10px;
  background: #f9fafb;
}
.notif-item.overdue {
  border-color: #fecaca;
  background: #fff1f2;
}

.notif-meta {
  flex: 1;
  min-width: 0;
}
.notif-title {
  font-weight: 600;
  font-size: 14px;
  color: #111827;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.notif-sub {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: #6b7280;
  margin-top: 2px;
}
.notif-date {
  white-space: nowrap;
}
.notif-action {
  padding: 6px 10px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: #facc15;
}
.status-dot.overdue {
  background: #ef4444;
}

/* pill toggle */
.sb-toggle {
  position: absolute;
  top: 12px;
  right: -12px;        /* sits on right edge */
  width: 28px;
  height: 28px;
  border-radius: 9999px;
  border: 1px solid #e5e7eb;
  background: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
  z-index: 5;
}

/* menu */
.menu {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  text-decoration: none;
  color: #111827;
  white-space: nowrap;
}
.item:hover { background: #eef2ff; }

.icon {
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  flex: 0 0 28px;      /* fixed slot so labels align */
}
.icon svg {
  width: 20px;
  height: 20px;
  display: block;
  overflow: visible;
}

.label {
  transition: opacity 150ms ease, transform 150ms ease;
}

/* collapse hides labels, keeps icons */
.shell.closed .label {
  opacity: 0;
  transform: translateX(-8px);
  width: 0;
  overflow: hidden;
  pointer-events: none;
}

/* active state */
.menu .item { color: #111827; }
.menu .item.active,
.menu .item:hover { color: #4f46e5; }

/* ---------- Auth menu item + popover ---------- */
.sb-auth-wrapper {
  position: relative;
  margin-top: 40px; /* clears the toggle pill */
}

.sb-auth-item {
  width: 100%;
}

.sb-auth-label {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  min-width: 0;
}

.auth-name {
  font-weight: 600;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.auth-role-badge {
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 9999px;
  background: #eef2ff;
  color: #4f46e5;
  text-transform: capitalize;
}

/* small card-style popover to the right of the sidebar */
.sb-auth-popover {
  position: absolute;
  top: 0;
  left: calc(100% + 8px);
  background: #ffffff;
  border-radius: 12px;
  border: 1px solid #e5e7eb;
  box-shadow: 0 12px 30px rgba(15,23,42,0.18);
  padding: 10px 12px;
  min-width: 220px;
  z-index: 20;
}

.shell.closed .sb-auth-popover {
  left: 72px; /* collapsed sidebar width + small offset */
}

/* ---------- Dashboard layout ---------- */
.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
@media (max-width: 1100px) {
  .grid-2 { grid-template-columns: 1fr; }
}

/* KPI tiles */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0,1fr));
  gap: 12px;
  margin: 12px 0;
}
.kpi {
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 12px;
}
.kpi-label { color: #6b7280; font-size: 12px; }
.kpi-metric { font-size: 22px; font-weight: 600; margin-top: 6px; }

/* ---------- Card header row + small button (for Download CSV) ---------- */
.card-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.btn {
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 6px 10px;
  font: inherit;
  cursor: pointer;
}
.btn:hover { background: #f8fafc; }

/* =========================================================
   RESPONSIVE BEHAVIOR (tablets, phones, iPods)
   ========================================================= */

/* Medium and down (tablets, small laptops) */
@media (max-width: 1024px) {

  .container {
    padding: 8px;
  }

  .card {
    padding: 10px;
  }

  .content {
    padding: 12px;
  }

  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .add-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .table-wrapper {
    max-height: none; /* allow full height on iPad */
  }

  .table {
    min-width: 620px;
  }
}

/* Phones / iPods */
@media (max-width: 768px) {

  html, body {
    font-size: 13px;
  }

  /* Sidebar moves on top, content below */
  .shell {
    flex-direction: column;
    min-height: 100vh;
  }

  .sidebar {
    width: 100% !important;
    padding: 8px 8px 12px;
    border-right: none;
    border-bottom: 1px solid #e5e7eb;
  }

  .shell.closed {
    --sbw: 100%;
  }

  .sb-toggle {
    top: 8px;
    right: 8px;
  }

  .content {
    padding: 10px;
  }

  .container {
    padding: 8px 4px;
    gap: 8px;
  }

  /* Controls stack vertically */
  .controls {
    flex-direction: column;
    align-items: stretch;
  }

  .controls-spacer {
    display: none;
  }

  /* KPIs in a single column for readability */
  .kpi-grid {
    grid-template-columns: 1fr;
  }

  /* Add form uses fewer columns */
  .add-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  header {
    padding: 8px 12px;
  }

  /* On phones, let table grow; rely on horizontal scroll only */
  .table-wrapper {
    max-height: none;
  }

  .table {
    min-width: 540px;
  }

  .table th,
  .table td {
    padding: 6px 4px;
  }

  /* Better login popover on phones */
  .sb-auth-wrapper {
    margin-top: 4px;
  }

  .sb-auth-popover {
    position: fixed;
    top: 60px;      /* just below the header bar */
    left: 8px;
    right: 8px;
    min-width: 0;
    z-index: 50;    /* above sidebar/content */
  }

  .shell.closed .sb-auth-popover {
    left: 8px;      /* same margin when collapsed */
  }
}

/* Very small phones */
@media (max-width: 480px) {

  /* Add form collapses fully */
  .add-grid {
    grid-template-columns: 1fr;
  }

  .table {
    min-width: 480px;
  }

  /* Hide less critical columns (6 and 7) on very narrow screens,
     e.g., Cost/Unit and Last Updated if you follow that order. */
  .table th:nth-child(6),
  .table td:nth-child(6),
  .table th:nth-child(7),
  .table td:nth-child(7) {
    display: none;
  }
}
