:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --border: #e1e4e8;
  --text: #1f2328;
  --text-muted: #6b7280;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #dc2626;
  --danger-bg: #fee2e2;
  --warn-bg: #fff1f2;
  --warn-border: #fca5a5;
  --ok-badge: #eef2ff;
  --header-bg: #fafbfc;
  --minus-border: #1f2328;
  --radius: 8px;
}

* { box-sizing: border-box; }

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

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.topbar h1 {
  margin: 0;
  font-size: 1.2rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-logo {
  height: 28px;
  width: auto;
  display: block;
  flex-shrink: 0;
}

/* Light variant shows by default; the dark-theme block below swaps which
   one is visible. Two <img>s (rather than one swapped via JS) means the
   correct logo is already in the DOM before app.js runs, so there's no
   flash of the light logo on a dark background on first paint. */
.brand-logo-dark {
  display: none;
}

.theme-toggle-btn {
  width: 44px;
  height: 44px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1.15rem;
  cursor: pointer;
}

.theme-toggle-btn:hover { background: var(--bg); }

.location-picker {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.location-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  min-height: 44px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
}

.location-btn .check {
  width: 16px;
  height: 16px;
  border-radius: 4px;
  border: 1px solid var(--text-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  line-height: 1;
  flex-shrink: 0;
}

.location-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.location-btn.active .check {
  background: white;
  border-color: white;
  color: var(--accent);
}

.tabs {
  display: flex;
  gap: 6px;
}

.tab-btn {
  background: none;
  border: 1px solid transparent;
  padding: 8px 14px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.tab-btn:hover { background: var(--bg); }

.tab-btn.active {
  background: var(--accent);
  color: white;
}

.badge {
  background: var(--danger);
  color: white;
  border-radius: 999px;
  font-size: 0.7rem;
  padding: 1px 6px;
  margin-left: 4px;
}

.tab-btn.active .badge { background: rgba(255,255,255,0.3); }

main {
  max-width: 900px;
  margin: 0 auto;
  padding: 16px 20px 60px;
}

.page { display: none; }
.page.active { display: block; }

.page-desc {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin: 4px 0 14px;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}

#searchBox, #disciplineFilter {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
  background: var(--surface);
  color: var(--text);
}

#searchBox { flex: 1; min-width: 180px; }

.checkbox-inline {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.summary-bar {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.summary-bar strong { color: var(--text); }

.protocol-group {
  margin-bottom: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.protocol-header {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 14px;
  background: var(--header-bg);
  border-bottom: 1px solid var(--border);
}

.protocol-header .protocol-title-row {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 8px;
}

.protocol-header .protocol-name {
  font-weight: 600;
  font-size: 0.95rem;
}

.protocol-header .discipline-tag {
  font-size: 0.7rem;
  color: var(--text-muted);
  padding: 1px 6px;
  background: var(--ok-badge);
  border-radius: 4px;
  white-space: nowrap;
}

.protocol-header .protocol-subtotal {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.item-row {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

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

.item-row.low-stock {
  background: var(--warn-bg);
}

.item-info {
  flex: 1;
  min-width: 0;
}

.item-form-name {
  font-size: 0.9rem;
}

.item-company {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.star-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.3rem;
  color: #d1d5db;
  padding: 8px;
  line-height: 1;
  min-width: 44px;
  min-height: 44px;
}

.star-btn.active { color: #f59e0b; }

.stepper {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.step-btn {
  width: 44px;
  height: 44px;
  flex-shrink: 0;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
}

.step-minus {
  color: var(--danger);
  border: 1px solid var(--minus-border);
}

.step-plus {
  color: var(--accent);
  border-color: #bfdbfe;
}

.step-btn:hover { background: var(--bg); }
.step-btn:active { transform: scale(0.94); }
.step-btn:disabled { opacity: 0.35; cursor: not-allowed; transform: none; }

.stepper input[type="number"] {
  width: 56px;
  height: 44px;
  text-align: center;
  padding: 5px 2px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1.1rem;
  font-weight: 600;
  -moz-appearance: textfield;
}

.stepper input[type="number"]::-webkit-outer-spin-button,
.stepper input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

@media (max-width: 480px) {
  .item-row { gap: 10px; }
  .item-info { flex-basis: 100%; }
  .stepper { margin-left: 0; }
}

.low-stock-tag {
  font-size: 0.7rem;
  color: var(--danger);
  font-weight: 600;
  margin-left: 4px;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 40px 0;
  font-size: 0.9rem;
}

.manage-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 18px;
}

.manage-block h2 {
  margin: 0 0 4px 0;
  font-size: 1rem;
}

.manage-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.manage-actions button, .file-btn {
  padding: 8px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  font-size: 0.85rem;
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  color: inherit;
}

.manage-actions button:hover, .file-btn:hover { background: var(--bg); }

/* [hidden] has lower specificity than the rule above (which needs to win
   normally, for the always-visible buttons/links it targets), so it loses
   the display tug-of-war without this — a hidden button was rendering
   anyway. */
.manage-actions [hidden] { display: none; }

.manage-actions .danger {
  color: var(--danger);
  border-color: var(--danger-bg);
}

.manage-actions .danger:hover { background: var(--danger-bg); }

.manage-actions button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.manage-actions button:disabled:hover { background: var(--surface); }

/* Dark palette applies when the OS prefers dark and the user hasn't
   explicitly chosen light (data-theme="light"), OR when the user has
   explicitly chosen dark (data-theme="dark") regardless of OS preference.
   Keep both blocks' variable values in sync if either one changes. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #171a21;
    --border: #2a2e37;
    --text: #e6e8eb;
    --text-muted: #9aa2ad;
    --ok-badge: #1e2740;
    --warn-bg: #2a1a1c;
    --danger-bg: #3a1c1c;
    --header-bg: #1c2028;
    --minus-border: #ffffff;
  }
  :root:not([data-theme="light"]) .brand-logo-light { display: none; }
  :root:not([data-theme="light"]) .brand-logo-dark { display: block; }
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #171a21;
  --border: #2a2e37;
  --text: #e6e8eb;
  --text-muted: #9aa2ad;
  --ok-badge: #1e2740;
  --warn-bg: #2a1a1c;
  --danger-bg: #3a1c1c;
  --header-bg: #1c2028;
  --minus-border: #ffffff;
}

:root[data-theme="dark"] .brand-logo-light { display: none; }
:root[data-theme="dark"] .brand-logo-dark { display: block; }
