: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;
}

/* ---------- 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: 700px; /* 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; }

/* 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; }

/* ---------- 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;
}

/* 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;
  }

  .content {
    padding: 12px;
  }

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

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

/* 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;
  }

  /* 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;
  }

  /* 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;
  }
}
