/* ===== Design tokens (light/dark aware) ===== */
:root {
  color-scheme: light;
  --page-bg:        #f6f5f2;
  --surface-card:   #ffffff;
  --surface-sunken: #f2efe8;
  --text-primary:   #26241f;
  --text-secondary: #6b6759;
  --text-muted:     #9a9686;
  --border:         #e7e3d8;
  --border-strong:  #d8d3c3;
  --gridline:       #eae6da;
  --accent:         #a07d33;       /* warm gold/brass -- tabs, links, focus */
  --accent-contrast:#ffffff;
  --series-1:       #a07d33;
  --good-bg:        #e5f3e6;
  --good-text:      #2f8a3e;
  --critical:       #c23b3b;
  --critical-bg:    #fbe9e9;
  --status-good:    #1a7f37;
  --status-critical:#cf222e;
  --btn-dark-bg:    #26241f;
  --btn-dark-text:  #ffffff;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page-bg:        #171613;
    --surface-card:   #211f1b;
    --surface-sunken: #1c1a16;
    --text-primary:   #f2f0e9;
    --text-secondary: #c9c4b3;
    --text-muted:     #8f8a78;
    --border:         #34312a;
    --border-strong:  #423e34;
    --gridline:       #2c2a24;
    --accent:         #cba75c;
    --accent-contrast:#1a1815;
    --series-1:       #cba75c;
    --good-bg:        #1e3320;
    --good-text:      #6bc27a;
    --critical:       #e07a7a;
    --critical-bg:    #3a2321;
    --status-good:    #2ea043;
    --status-critical:#e5484d;
    --btn-dark-bg:    #f2f0e9;
    --btn-dark-text:  #1a1815;
  }
}

/* Explicit toggle (top-right sun/moon button) always wins over the OS
   setting in both directions -- the site defaults to light regardless of
   the visitor's system theme, and only switches when they ask it to. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --page-bg:        #171613;
  --surface-card:   #211f1b;
  --surface-sunken: #1c1a16;
  --text-primary:   #f2f0e9;
  --text-secondary: #c9c4b3;
  --text-muted:     #8f8a78;
  --border:         #34312a;
  --border-strong:  #423e34;
  --gridline:       #2c2a24;
  --accent:         #cba75c;
  --accent-contrast:#1a1815;
  --series-1:       #cba75c;
  --good-bg:        #1e3320;
  --good-text:      #6bc27a;
  --critical:       #e07a7a;
  --critical-bg:    #3a2321;
  --status-good:    #2ea043;
  --status-critical:#e5484d;
  --btn-dark-bg:    #f2f0e9;
  --btn-dark-text:  #1a1815;
}

/* ===== Theme toggle (fixed top-right, visible on every screen) ===== */
.theme-toggle {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  background: var(--surface-card);
  color: var(--text-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
/* Only the login-screen copy floats over the page -- the one in the app
   header is a normal flex item next to Log out, so they never overlap. */
.theme-toggle-fixed {
  position: fixed;
  top: calc(1rem + env(safe-area-inset-top));
  right: calc(1.25rem + env(safe-area-inset-right));
  z-index: 50;
}
.theme-toggle:hover { border-color: var(--accent); }
.theme-toggle svg { width: 18px; height: 18px; display: block; }
.theme-toggle .icon-moon { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: block; }


* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page-bg);
  color: var(--text-primary);
}
h1, h2, h3 { margin: 0 0 0.25em; }
button { font-family: inherit; }
[hidden] { display: none !important; }

/* ===== Buttons ===== */
.btn {
  border: 1px solid var(--border-strong);
  background: var(--surface-card);
  color: var(--text-primary);
  border-radius: 8px;
  padding: 0.6em 1.1em;
  font-size: 0.9rem;
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--btn-dark-bg); border-color: var(--btn-dark-bg); color: var(--btn-dark-text); font-weight: 600; }
.btn-primary:hover { filter: brightness(1.15); }
.btn-ghost { background: transparent; }
.btn-text { background: transparent; border-color: transparent; padding: 0.3em 0.5em; color: var(--accent); }
.lang-toggle { font-weight: 700; }
.btn-small { padding: 0.35em 0.7em; font-size: 0.82rem; }
.btn-block { width: 100%; }

/* ===== Login ===== */
.login-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}
.login-card {
  background: var(--surface-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 2.25rem 2rem;
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.9rem;
  text-align: center;
}
.login-logo { width: 100%; max-width: 220px; height: auto; margin-bottom: 0.25rem; }
.login-card .subtitle { margin: 0 0 0.25rem; color: var(--text-secondary); font-size: 0.88rem; }
.login-card label { width: 100%; }
label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); text-align: left; }
.checkbox-row { flex-direction: row; align-items: center; gap: 0.5rem; text-transform: none; letter-spacing: normal; font-size: 0.85rem; color: var(--text-primary); }
.checkbox-row input[type="checkbox"] { width: 16px; height: 16px; flex: 0 0 auto; }
input, select, textarea {
  font: inherit;
  padding: 0.55em 0.7em;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--surface-card);
  color: var(--text-primary);
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.error-text { color: var(--critical); font-size: 0.85rem; margin: 0; }

/* ===== App shell ===== */
.app-header {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
  justify-content: space-between;
  padding: calc(1.1rem + env(safe-area-inset-top)) calc(1.5rem + env(safe-area-inset-right)) 1.1rem calc(1.5rem + env(safe-area-inset-left));
  background: var(--page-bg);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 0.9rem; }
.brand-logo { height: 46px; width: auto; display: block; }
.brand-divider { width: 1px; height: 30px; background: var(--border-strong); }
.brand-title { font-size: 0.8rem; letter-spacing: 0.08em; text-transform: uppercase; color: var(--text-muted); }
.app-header-right { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
.role-badge {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  padding: 0.25em 0.7em;
}

.dept-tabs {
  display: flex;
  gap: 1.75rem;
  padding: 0.9rem 1.5rem 0;
  overflow-x: auto;
  border-bottom: 1px solid var(--border);
}
.dept-tab {
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 0 0 0.8rem;
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  cursor: pointer;
  white-space: nowrap;
  border-bottom: 2px solid transparent;
}
.dept-tab.active { color: var(--text-primary); border-bottom-color: var(--accent); }
.tab-count { font-size: 0.72rem; color: var(--text-muted); font-weight: 500; }
.dept-tab.active .tab-count { color: var(--accent); }
.dept-tab.reorder-tab { color: var(--critical); }
.dept-tab.reorder-tab.active { border-bottom-color: var(--critical); }
.dept-tab.reorder-tab .tab-count { color: var(--critical); }
.dept-tab.hazardous-tab { color: var(--critical); }
.dept-tab.hazardous-tab.active { border-bottom-color: var(--critical); }
.dept-tab.hazardous-tab .tab-count { color: var(--critical); }
.add-dept-tab {
  color: var(--accent);
  font-size: 1.05rem;
  font-weight: 700;
  padding-left: 0.2rem;
  padding-right: 0.2rem;
}
.add-dept-tab:hover { color: var(--text-primary); }

.toolbar {
  display: flex;
  gap: 0.75rem;
  padding: 1.1rem 1.5rem;
  flex-wrap: wrap;
}
#search-box { flex: 1; min-width: 180px; }

.empty-state { text-align: center; color: var(--text-muted); padding: 2rem; }

.app-footer {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.72rem;
  letter-spacing: 0.02em;
  padding: 0 1.5rem calc(1.5rem + env(safe-area-inset-bottom));
}
.login-footer-version {
  margin: -0.3rem 0 0;
  font-size: 0.7rem;
  color: var(--text-muted);
}

/* ===== Item grid & cards ===== */
.items-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1.25rem;
  padding: 0 1.5rem 2.5rem;
}
.item-card {
  background: var(--surface-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
}
.card-top { position: relative; }
.item-thumb {
  width: 100%;
  aspect-ratio: 16 / 10;
  background: var(--surface-sunken);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.admin-editable-thumb { cursor: pointer; }
.item-img { width: 100%; height: 100%; object-fit: contain; display: block; }
.item-img-fallback { font-size: 0.78rem; color: var(--text-muted); }
.delete-btn {
  position: absolute;
  top: 0.6rem;
  right: 0.6rem;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  background: var(--surface-card);
  color: var(--text-secondary);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.delete-btn:hover { border-color: var(--critical); color: var(--critical); }

/* Hazard types -- a row of large tappable icon buttons under Current Qty
   (no separate checkbox: the checkbox + icon combo felt repetitive, and the
   icon itself is now the full touch target, sized for mobile/iPad use). */
.hazard-block { gap: 0.5rem; }
.hazard-group-label {
  display: block;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}
.hazard-options {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.6rem;
}
/* Fixed-width tiles (not content-width chips) so every row -- full or partial --
   wraps into a clean, evenly-spaced, centered grid at any card width or browser
   zoom level, instead of packing left and leaving an odd item stranded. */
.hazard-option {
  appearance: none;
  -webkit-appearance: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 0 0 78px;
  width: 78px;
  min-height: 78px;
  gap: 0.3rem;
  font: inherit;
  font-size: 0.66rem;
  line-height: 1.15;
  text-align: center;
  color: var(--text-secondary);
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.5rem 0.35rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}
.hazard-option:hover { border-color: var(--border-strong); }
.hazard-option:active { transform: scale(0.96); }
.hazard-option:disabled { cursor: default; }
.hazard-option:disabled:hover { border-color: var(--border); }
.hazard-icon {
  width: 28px;
  height: 28px;
  flex: 0 0 auto;
  color: #c0271d;
  opacity: 0.4;
  filter: grayscale(70%);
  transition: opacity 0.15s, filter 0.15s;
}
.hazard-diamond { fill: #ffffff; stroke: currentColor; stroke-width: 1.6; }
.hazard-glyph { fill: currentColor; }
.hazard-option[aria-pressed="true"] {
  background: rgba(192, 39, 29, 0.1);
  border-color: rgba(192, 39, 29, 0.4);
}
.hazard-option[aria-pressed="true"] .hazard-icon { opacity: 1; filter: none; }
.hazard-option[aria-pressed="true"] .hazard-option-label { color: var(--text-primary); font-weight: 600; }

.card-body { padding: 1.1rem 1.2rem 1.3rem; display: flex; flex-direction: column; gap: 0.65rem; }

.item-name-input {
  border: none;
  background: transparent;
  padding: 0;
  font-size: 1.05rem;
  font-weight: 700;
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.01em;
  color: var(--text-primary);
}
.item-name-input:disabled { color: var(--text-primary); opacity: 1; cursor: default; }
.item-name-input:not(:disabled):hover, .item-name-input:not(:disabled):focus {
  outline: none;
  border-bottom: 1px dashed var(--border-strong);
}

.status-pill {
  align-self: stretch;
  margin: 0;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  text-align: center;
  border-radius: 8px;
  padding: 0.5em 0.75em;
  background: var(--status-good);
  color: #ffffff;
}
.status-pill.reorder { background: var(--status-critical); color: #ffffff; }

.field-row, .field-block {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  border-top: 1px solid var(--gridline);
  padding-top: 0.6rem;
}
.field-block { flex-direction: column; align-items: stretch; }
.field-label {
  flex: 0 0 auto;
  min-width: 84px;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.field-input {
  flex: 1;
  min-width: 0;
  font-size: 0.88rem;
  padding: 0.4em 0.55em;
}
.field-input:disabled { background: transparent; border-color: transparent; color: var(--text-secondary); padding-left: 0; }
.target-input { text-align: center; font-weight: 700; }
.vendor-input, .notes-input { text-align: center; }
.dept-select:not(:disabled) { cursor: pointer; }
.field-error {
  color: var(--critical);
  font-size: 0.78rem;
  margin: -0.35rem 0 0;
}
.icon-btn {
  flex: 0 0 auto;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: 1px solid var(--border-strong);
  background: var(--surface-card);
  color: var(--accent);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
}
.icon-btn:hover { border-color: var(--accent); }
.icon-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.icon-btn:disabled:hover { border-color: var(--border-strong); }

.stepper { flex: 1; display: flex; align-items: stretch; border: 1px solid var(--border-strong); border-radius: 8px; overflow: hidden; }
.stepper-btn {
  flex: 0 0 auto;
  width: 32px;
  border: none;
  background: var(--surface-sunken);
  color: var(--text-primary);
  font-size: 1rem;
  cursor: pointer;
}
.stepper-btn:hover { background: var(--border); }
.stepper .qty-input {
  flex: 1;
  text-align: center;
  border: none;
  border-left: 1px solid var(--border-strong);
  border-right: 1px solid var(--border-strong);
  border-radius: 0;
  font-weight: 700;
}
/* Hide native number-input spinners everywhere -- we use our own +/- steppers,
   and the browser's built-in ones are redundant/fiddly on top of them. */
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
input[type="number"] {
  -moz-appearance: textfield;
  appearance: textfield;
}

.history-toggle {
  align-self: flex-start;
  border: none;
  background: transparent;
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  padding: 0.2rem 0;
}
.chevron { display: inline-block; transition: transform 0.15s ease; font-size: 0.7em; }
.history-toggle[aria-expanded="true"] .chevron { transform: rotate(90deg); }

.comments-input { width: 100%; font-size: 0.85rem; }

.item-detail {
  border-top: 1px solid var(--border);
  padding: 0.9rem 1.2rem 1.2rem;
  background: var(--surface-sunken);
}
.range-controls { display: flex; gap: 0.4rem; flex-wrap: wrap; margin-bottom: 0.6rem; }
.range-btn {
  border: 1px solid var(--border-strong);
  background: var(--surface-card);
  color: var(--text-secondary);
  border-radius: 999px;
  padding: 0.3em 0.8em;
  font-size: 0.78rem;
  cursor: pointer;
}
.range-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

.chart-wrap { position: relative; }
.qty-chart { width: 100%; height: auto; display: block; }
.chart-empty { color: var(--text-muted); font-size: 0.85rem; text-align: center; padding: 1.5rem 0; }

.chart-tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--text-primary);
  color: var(--page-bg);
  font-size: 0.75rem;
  padding: 0.3em 0.55em;
  border-radius: 6px;
  transform: translate(-50%, -120%);
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.1s ease;
}
.chart-tooltip.visible { opacity: 1; }

.history-table-wrap { margin-top: 0.75rem; overflow-x: auto; }
.history-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.history-table th, .history-table td {
  text-align: left;
  padding: 0.4em 0.6em;
  border-bottom: 1px solid var(--gridline);
  font-variant-numeric: tabular-nums;
}
.history-table th { color: var(--text-muted); font-weight: 600; }

/* ===== Modal (Add item) ===== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20,18,14,0.45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 3vh 1rem;
  overflow-y: auto;
  z-index: 20;
}
.modal-card {
  background: var(--surface-card);
  border-radius: 14px;
  border: 1px solid var(--border);
  padding: 1.5rem;
  width: 100%;
  max-width: 440px;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }
.label-row { display: flex; gap: 0.5rem; align-items: center; }
.label-row input { flex: 1; min-width: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 0.4rem; }

.modal-subtext { font-size: 0.82rem; color: var(--text-muted); margin: -0.4rem 0 0; }
.depts-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.5rem; max-height: 45vh; overflow-y: auto; }
.dept-row-item { display: flex; gap: 0.5rem; align-items: center; }
.dept-label-input { flex: 1; min-width: 0; }
.dept-delete-btn { color: var(--text-muted); font-size: 1.1rem; }
.dept-delete-btn:hover { border-color: var(--critical); color: var(--critical); }
.depts-add-form { display: flex; gap: 0.5rem; padding-top: 0.4rem; border-top: 1px solid var(--border); }
.depts-add-form input { flex: 1; min-width: 0; }

/* ===== Responsive ===== */

/* Larger tap targets on touch devices (phone/tablet, including iPad with a
   trackpad still using fine pointer -- this specifically targets touch input,
   not just narrow screens) rather than a fixed screen-width cutoff. */
@media (pointer: coarse) {
  /* iOS Safari auto-zooms the page when focusing a text input smaller than
     16px -- bump every form control up to that floor so tapping into a
     field never triggers an unwanted zoom. */
  input, select, textarea { font-size: 16px; }
  .field-label { font-size: 0.72rem; }

  /* Every text field, select, and the +/- stepper get a solid 44px-tall tap
     target (Apple's HIG minimum) instead of the tighter desktop sizing --
     .stepper-btn has no explicit height of its own, it stretches to match
     the qty-input box next to it since .stepper uses align-items: stretch. */
  .field-input, select { min-height: 44px; }
  .stepper-btn { width: 46px; font-size: 1.25rem; }

  .icon-btn { width: 44px; height: 44px; font-size: 1.05rem; }
  .delete-btn { width: 44px; height: 44px; font-size: 1.2rem; }
  .theme-toggle { width: 44px; height: 44px; }
  .theme-toggle svg { width: 20px; height: 20px; }

  .dept-tab { padding: 0.6rem 0.1rem 1rem; min-height: 44px; }
  .range-btn { padding: 0.65em 1.1em; }
  .history-toggle { padding: 0.65rem 0; min-height: 44px; }

  .btn { padding: 0.75em 1.2em; }
  .btn-small { padding: 0.55em 0.9em; }

  /* Snappier taps: no gray flash on tap, no double-tap-to-zoom delay. */
  button, .dept-tab, .hazard-option, .stepper-btn, .icon-btn, .range-btn,
  .history-toggle, .theme-toggle, a {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
  }
}

@media (max-width: 520px) {
  .form-row { grid-template-columns: 1fr; }
  .items-grid { padding: 0 1rem calc(2rem + env(safe-area-inset-bottom)); }
  .toolbar, .dept-tabs { padding-left: 1rem; padding-right: 1rem; }

  /* Search + Add Item felt cramped side-by-side on a phone-width toolbar --
     stack them full-width instead of squeezing both into one row. */
  .toolbar { flex-direction: column; align-items: stretch; }
  #search-box { min-width: 0; width: 100%; }
  #add-item-btn { width: 100%; }

  /* The wordmark next to the logo is redundant with the browser/app-icon
     title on a narrow phone header -- drop it so the header has more room
     for the role badge and the dark-mode/language/logout controls. */
  .brand-divider, .brand-title { display: none; }

  .modal-card { padding: 1.1rem; }
}
