:root {
  color-scheme: dark;
  --bg: #0f1419;
  --surface: #1a1f26;
  --surface-2: #232931;
  --fg: #e4e7eb;
  --muted: #8b95a3;
  --border: #2c333c;
  --row-hover: #232931;
  --accent: #5a8def;
  --accent-fg: #ffffff;
  --danger: #e0625e;
  --stock-bg: #1c2d2a;
  --stock-hover: #243a36;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font: 14px -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
}
.topbar {
  position: sticky;
  top: 0;
  z-index: 6;
}
header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1rem 1.5rem;
  background: linear-gradient(180deg, #1e2632 0%, #161b22 100%);
  color: var(--fg);
  border-bottom: 1px solid var(--border);
}
header h1 { margin: 0; font-size: 1.25rem; }
.version {
  font-size: 0.7rem;
  font-weight: 400;
  color: var(--muted);
  margin-left: 0.4rem;
  letter-spacing: 0.02em;
  vertical-align: 0.15em;
}
.stats { font-size: 0.85rem; opacity: 0.85; }
.user-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  /* Match the admin button (padding/font/border) so the two sit at the
     same height. Keeps the rounded "pill" shape for visual distinction. */
  padding: 0.45rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--fg);
  font: inherit;
  white-space: nowrap;
}
.user-pill .signout {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.85em;
  border-left: 1px solid var(--border);
  padding-left: 0.5rem;
}
.user-pill .signout:hover { text-decoration: underline; }
/* Right-side group: admin-btn (if rendered) then the user pill. Only the
   first element of that group gets margin-left: auto so the two stay
   adjacent on the right instead of splitting the free space. */
.admin-btn { margin-left: auto; }
header > .stats + .user-pill { margin-left: auto; }

.splash-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}
.splash {
  text-align: center;
  padding: 2.5rem 3rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 16px 40px rgba(0,0,0,0.45);
  max-width: 380px;
  width: 92%;
}
.splash h1 { margin: 0 0 0.5rem; font-size: 1.4rem; }
.splash-sub { color: var(--muted); margin: 0 0 1.5rem; }
.ms-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 1.1rem;
  background: var(--surface-2);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-weight: 500;
  text-decoration: none;
}
.ms-btn:hover { background: #2e3640; }

/* Wide modal for the audit log — overrides dialog max-width. */
dialog.wide-dlg {
  max-width: 920px;
  width: 92vw;
}
dialog.wide-dlg form { padding: 1rem 1.25rem 1.1rem; }
.hint-block { margin: 0 0 0.75rem; padding: 0; color: var(--muted); font-size: 0.85rem; }

.audit-table-wrap {
  max-height: 70vh;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
}
.audit-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  font-size: 0.88rem;
}
.audit-table thead th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  text-align: left;
  font-weight: 600;
  padding: 0.45rem 0.6rem;
  border-bottom: 1px solid var(--border);
  z-index: 1;
}
.audit-table tbody td {
  padding: 0.35rem 0.6rem;
  border-bottom: 1px solid #1e242c;
  vertical-align: top;
}
.audit-table tbody tr:hover { background: var(--row-hover); }
.audit-table .ts {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  white-space: nowrap;
}
.audit-table .muted { color: var(--muted); font-style: italic; }
.audit-count {
  color: var(--muted);
  font-weight: 400;
  font-size: 0.85em;
  margin-left: 0.4rem;
}

.chip {
  display: inline-block;
  padding: 0.1rem 0.55rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.chip-add    { background: #2c4257; color: #b8d4ff; }
.chip-assign { background: #2c4233; color: #b8e0c1; }
.chip-revert { background: #4a3a2a; color: #f0c693; }

/* Toast — top-center slide-in, auto-dismisses. Used for confirmations
   after async actions (clipboard copy, revert-to-stock, etc.). */
.toast {
  position: fixed;
  top: 1.5rem;
  left: 50%;
  z-index: 1000;
  max-width: 560px;
  padding: 1rem 1.6rem;
  background: var(--surface-2);
  color: var(--fg);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 8px;
  box-shadow: 0 12px 32px rgba(0,0,0,0.55);
  font-size: 1.05rem;
  font-weight: 500;
  opacity: 0;
  transform: translate(-50%, -8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
}
.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}
.toast.warn  { border-left-color: var(--danger); }
.toolbar {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  padding: 0.75rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.toolbar input[type="search"] {
  flex: 1;
  min-width: 220px;
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--fg);
}
.toolbar input[type="search"]::placeholder { color: var(--muted); }
.toolbar select {
  padding: 0.4rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--fg);
}
.chk { display: inline-flex; align-items: center; gap: 0.35rem; user-select: none; }
.switch {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  user-select: none;
}
.switch input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}
.switch .slider {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
  background: #4a525c;
  border-radius: 999px;
  transition: background 0.15s;
  flex-shrink: 0;
}
.switch .slider::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  background: white;
  border-radius: 50%;
  transition: transform 0.15s;
  box-shadow: 0 1px 2px rgba(0,0,0,0.25);
}
.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(16px); }
.switch input:focus-visible + .slider { outline: 2px solid var(--accent); outline-offset: 2px; }
button {
  padding: 0.45rem 0.85rem;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--fg);
  border-radius: 4px;
  cursor: pointer;
  font: inherit;
}
button:hover { background: #2e3640; }
button.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
button.primary:hover { background: #4373d6; }
button.danger { background: var(--danger); color: white; border-color: var(--danger); }
button.danger:hover { background: #c14a46; }

table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
}
thead th {
  position: sticky;
  top: var(--topbar-h, 114px);
  background: var(--surface-2);
  text-align: left;
  font-weight: 600;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
  z-index: 4;
}
thead th[data-sort]::after { content: " ⇅"; color: var(--muted); font-size: 0.8em; }
thead th.sorted.asc::after { content: " ▲"; color: var(--accent); }
thead th.sorted.desc::after { content: " ▼"; color: var(--accent); }
tbody tr { border-bottom: 1px solid #1e242c; cursor: pointer; }
tbody tr:hover { background: var(--row-hover); }
tbody tr.stock { background: var(--stock-bg); }
tbody tr.stock:hover { background: var(--stock-hover); }
tbody td { padding: 0.4rem 0.75rem; vertical-align: top; }
td.url a { color: var(--accent); }
td.url { max-width: 26ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td.muted { color: var(--muted); font-style: italic; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

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

dialog {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0;
  max-width: 460px;
  width: 92%;
  background: var(--surface);
  color: var(--fg);
  box-shadow: 0 16px 40px rgba(0,0,0,0.55);
}
dialog::backdrop { background: rgba(0,0,0,0.55); }
dialog form { padding: 1.25rem 1.25rem 1rem; }
dialog h2 { margin: 0 0 1rem; font-size: 1.1rem; }
dialog label[hidden] { display: none !important; }
dialog label {
  display: grid;
  grid-template-columns: 110px 1fr;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.55rem;
  font-size: 0.92rem;
}
dialog input, dialog textarea {
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font: inherit;
  width: 100%;
  background: var(--surface-2);
  color: var(--fg);
}
dialog input:read-only { color: var(--muted); }
dialog input::placeholder, dialog textarea::placeholder { color: var(--muted); }
.admin-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}
.admin-actions button { width: 100%; }
.dlg-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
  align-items: center;
}
.dlg-actions .spacer { flex: 1; }
.error { color: var(--danger); margin: 0.5rem 0 0; font-size: 0.9rem; }
dialog p { margin: 0 0 0.6rem; line-height: 1.4; }
dialog p.warn { color: var(--danger); }
.hint  { color: var(--muted); margin: 0.25rem 0 0.75rem 110px; font-size: 0.85rem; }
dialog label.paste-row {
  display: block;
  margin-bottom: 0.75rem;
}
dialog label.paste-row textarea {
  width: 100%;
  display: block;
  margin-top: 0.3rem;
  font: 12px ui-monospace, SFMono-Regular, Menlo, monospace;
  resize: vertical;
}
.lookup-row { display: flex; gap: 0.4rem; align-items: center; }
.lookup-row input { flex: 1; }
.lookup-row button { white-space: nowrap; padding: 0.4rem 0.6rem; }
.lookup-row button.loading {
  position: relative;
  padding-left: 1.9rem;
  cursor: wait;
}
.lookup-row button.loading::before {
  content: "";
  position: absolute;
  left: 0.6rem;
  top: 50%;
  width: 0.85rem;
  height: 0.85rem;
  margin-top: -0.45rem;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
dialog label.required-stock { color: var(--danger); font-weight: 600; }
dialog label.required-stock input,
dialog label.required-stock .lookup-row input { border-color: var(--danger); }
dialog label.chk-inline {
  display: flex;
  justify-content: flex-start;
  gap: 0.5rem;
  margin-left: calc(110px + 0.5rem);
}
dialog label.chk-inline input { width: auto; margin: 0; }
