/* SMBI Inventory — mobile first. Staff use phones on the floor. */

:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #dfe4ec;
  --text: #16202e;
  --muted: #5d6b7f;
  --brand: #14508c;
  --brand-dark: #0e3a67;
  --ok: #1a7f47;
  --warn: #a2640a;
  --danger: #b3261e;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 30, 54, .06), 0 4px 12px rgba(16, 30, 54, .05);
  --tap: 44px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f151d;
    --surface: #182230;
    --border: #2a3646;
    --text: #e8edf4;
    --muted: #9aa8bb;
    --brand: #5b9bd8;
    --brand-dark: #7fb4e6;
    --ok: #57c98a;
    --warn: #e0a94a;
    --danger: #f08279;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 12px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; }

/* `display` on .appbar would otherwise beat the hidden attribute */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

body { min-height: 100dvh; display: flex; flex-direction: column; }

/* ---------- App bar ---------- */

.appbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--brand);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 20;
}

@media (prefers-color-scheme: dark) {
  .appbar { background: #16232f; border-bottom: 1px solid var(--border); }
}

.appbar__logo { height: 30px; width: auto; flex: none; }
.appbar__title {
  font-size: 1rem;
  font-weight: 600;
  margin: 0;
  letter-spacing: .2px;
  white-space: nowrap;
}
.appbar__spacer { margin-left: auto; }
.appbar .btn-ghost { white-space: nowrap; }

.appbar__who {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  line-height: 1.2;
  font-size: .75rem;
  max-width: 45vw;
}
.appbar__who strong { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.appbar__who span { opacity: .85; }

/* Below this the name and role crowd the title off the bar. Both are shown in
   full on the Account screen, so dropping them here costs nothing.
   Must come after the rule above, which would otherwise win on source order. */
@media (max-width: 360px) {
  .appbar__who { display: none; }
}

.appbar .btn-ghost {
  background: rgba(255, 255, 255, .14);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, .28);
  min-height: 36px;
  padding: 0 12px;
  font-size: .8rem;
}
.appbar .btn-ghost:hover { background: rgba(255, 255, 255, .24); }

/* ---------- Nav ---------- */

.nav {
  display: flex;
  gap: 4px;
  padding: 0 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 15;
  overflow-x: auto;
  scrollbar-width: none;
}
.nav::-webkit-scrollbar { display: none; }

.nav__link {
  flex: none;
  padding: 12px 14px;
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
  color: var(--muted);
  text-decoration: none;
  font-size: .92rem;
  font-weight: 600;
  border-bottom: 3px solid transparent;
}
.nav__link.is-on { color: var(--brand); border-bottom-color: var(--brand); }
.nav__link:hover { color: var(--text); }

/* ---------- Layout ---------- */

main {
  flex: 1;
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: max(24px, env(safe-area-inset-bottom));
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

.card + .card { margin-top: 14px; }

h1 { font-size: 1.35rem; margin: 0 0 6px; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; margin: 0 0 6px; }
p { margin: 0 0 12px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: var(--tap);
  padding: 0 18px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: var(--brand);
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background .15s ease, opacity .15s ease;
}
.btn:hover { background: var(--brand-dark); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn:focus-visible { outline: 3px solid var(--brand); outline-offset: 2px; }

.btn-block { width: 100%; }

.btn-ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}
.btn-ghost:hover { background: rgba(125, 145, 175, .12); }

/* Microsoft sign in button, per Microsoft's own guidance: white, their mark */
.btn-ms {
  background: #fff;
  color: #1b1b1b;
  border: 1px solid #8c8c8c;
  font-weight: 600;
}
.btn-ms:hover { background: #f3f3f3; }
.btn-ms svg { flex: none; }

/* ---------- Sign in ---------- */

.signin { max-width: 420px; margin: 6vh auto 0; text-align: center; }
.signin__logo { width: 132px; height: auto; margin: 0 auto 18px; display: block; }
.signin .card { padding: 26px 22px; }
.signin__note { margin: 18px 0 0; font-size: .8rem; color: var(--muted); }

/* ---------- Gate / states ---------- */

.state { text-align: center; padding: 40px 20px; color: var(--muted); }
.state h2 { color: var(--text); }
.state.error h2 { color: var(--danger); }

.spinner {
  width: 30px; height: 30px; margin: 0 auto 14px;
  border: 3px solid var(--border);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }

.gate { max-width: 480px; margin: 4vh auto 0; text-align: center; }
.gate__icon {
  width: 56px; height: 56px; margin: 0 auto 14px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: rgba(162, 100, 10, .12);
  color: var(--warn);
  font-size: 26px;
}
.gate dl {
  margin: 18px 0 0;
  text-align: left;
  border-top: 1px solid var(--border);
  padding-top: 14px;
  font-size: .9rem;
}
.gate dl div { display: flex; justify-content: space-between; gap: 12px; padding: 5px 0; }
.gate dt { color: var(--muted); }
.gate dd { margin: 0; font-weight: 600; text-align: right; word-break: break-word; }

/* ---------- Badges ---------- */

.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .03em;
  text-transform: uppercase;
  border: 1px solid transparent;
}
.badge-admin  { background: rgba(20, 80, 140, .12);  color: var(--brand);  border-color: rgba(20, 80, 140, .3); }
.badge-staff  { background: rgba(26, 127, 71, .12);  color: var(--ok);     border-color: rgba(26, 127, 71, .3); }
.badge-viewer { background: rgba(93, 107, 127, .14); color: var(--muted);  border-color: rgba(93, 107, 127, .3); }
.badge-off    { background: rgba(179, 38, 30, .1);   color: var(--danger); border-color: rgba(179, 38, 30, .3); }

/* ---------- Capability list (role explainer) ---------- */

.caps { list-style: none; margin: 12px 0 0; padding: 0; font-size: .92rem; }
.caps li { display: flex; gap: 10px; align-items: flex-start; padding: 6px 0; }
.caps li::before { content: "✓"; color: var(--ok); font-weight: 700; flex: none; }
.caps li.no { color: var(--muted); }
.caps li.no::before { content: "✕"; color: var(--muted); }

.badge.tone-ok   { background: rgba(26, 127, 71, .12);  color: var(--ok);     border-color: rgba(26, 127, 71, .3); }
.badge.tone-warn { background: rgba(162, 100, 10, .13); color: var(--warn);   border-color: rgba(162, 100, 10, .32); }
.badge.tone-bad  { background: rgba(179, 38, 30, .1);   color: var(--danger); border-color: rgba(179, 38, 30, .3); }
.badge.tone-info { background: rgba(20, 80, 140, .12);  color: var(--brand);  border-color: rgba(20, 80, 140, .3); }
.badge.tone-mute { background: rgba(93, 107, 127, .14); color: var(--muted);  border-color: rgba(93, 107, 127, .3); }

/* ---------- Toolbar ---------- */

.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}
.toolbar h1 { margin: 0; flex: 1; }

.linkbtn {
  background: none;
  border: 0;
  color: var(--brand);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  padding: 10px 0;
  min-height: var(--tap);
}
.linkbtn:hover { text-decoration: underline; }

/* ---------- Filters ---------- */

.seg-group {
  display: flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px;
  margin-bottom: 12px;
}
.seg {
  flex: 1;
  min-height: 40px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.seg.is-on { background: var(--brand); color: #fff; }

.filters { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.filters__pair { display: flex; gap: 10px; }
.filters__pair > * { flex: 1; min-width: 0; }

.input {
  width: 100%;
  min-height: var(--tap);
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
.input:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; border-color: var(--brand); }

select.input {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
                    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 32px;
}

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 36px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font: inherit;
  font-size: .85rem;
  font-weight: 600;
  cursor: pointer;
}
.chip.is-on {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

/* ---------- List rows ---------- */

.list { margin-top: 10px; display: flex; flex-direction: column; gap: 8px; }

.row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  color: inherit;
  min-height: 64px;
}
.row:hover { border-color: var(--brand); }
.row:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }

.row__main { flex: 1; min-width: 0; }
.row__top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 3px; }
.row__tag {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .78rem;
  font-weight: 700;
  color: var(--muted);
  letter-spacing: .02em;
}
.row__name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row__meta { font-size: .82rem; color: var(--muted); }
.row__chev { color: var(--muted); font-size: 1.5rem; line-height: 1; flex: none; }

.row__qty { text-align: right; flex: none; line-height: 1.2; }
.row__qty strong { display: block; font-size: 1.2rem; }
.row__unit { font-size: .75rem; color: var(--muted); }

.row--late { border-left: 4px solid var(--danger); }
.row__main--link { text-decoration: none; color: inherit; display: block; }
.row__action { flex: none; min-height: 40px; padding: 0 14px; }

.row--clickable { cursor: pointer; }
.row--clickable:hover { border-color: var(--brand); }
.list--tight { gap: 0; margin-top: 12px; }
.list--tight .row { border-radius: 0; border-left: 0; border-right: 0; border-top: 0; box-shadow: none; background: none; padding: 12px 0; min-height: 0; }
.list--tight .row:last-child { border-bottom: 0; }

.pos { color: var(--ok); }
.neg { color: var(--danger); }

/* ---------- Movement preview ---------- */

.preview {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  background: var(--bg);
}
.preview--bad { border-color: var(--danger); }
.preview__row { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 3px 0; }
.preview__row--total {
  border-top: 1px solid var(--border);
  margin-top: 5px;
  padding-top: 8px;
}
.preview__row--total strong { font-size: 1.25rem; }
.preview p { margin: 8px 0 0; color: var(--danger); }

/* ---------- Lending ---------- */

.loanbox {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  margin-bottom: 12px;
}
.loanbox__top { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.loanbox__ref { font-size: 1.05rem; }

.history { list-style: none; margin: 8px 0 0; padding: 0; }
.history li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: .9rem;
}
.history li:last-child { border-bottom: 0; }

/* ---------- Detail ---------- */

.detail__head { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 8px; }
.detail__head > div:first-child { flex: 1; min-width: 0; }
.detail__head h1 { margin: 2px 0 0; }

.fields { margin: 0; }
.field {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
  font-size: .92rem;
}
.field:last-child { border-bottom: 0; }
.field dt { color: var(--muted); flex: none; }
.field dd { margin: 0; text-align: right; font-weight: 600; min-width: 0; word-break: break-word; }

.field dd .small { font-weight: 400; }

.stack-end { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; }

.qty-hero { display: flex; align-items: baseline; gap: 8px; margin: 10px 0 4px; }
.qty-hero strong { font-size: 2.2rem; line-height: 1; letter-spacing: -.02em; }

/* ---------- Forms ---------- */

.form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-top: 12px;
}

.formfield { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.formfield__label { font-size: .85rem; font-weight: 600; color: var(--muted); }
.formfield__label .req { color: var(--danger); }
.formfield__help { font-size: .78rem; color: var(--muted); }
.formfield__err { font-size: .8rem; color: var(--danger); font-weight: 600; }

.input.has-error { border-color: var(--danger); }
.input.has-error:focus-visible { outline-color: var(--danger); }

textarea.input { resize: vertical; min-height: 62px; line-height: 1.45; }

.formrow { display: flex; gap: 12px; }
.formrow > * { flex: 1; min-width: 0; }

.form__section {
  margin: 6px 0 0;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  font-size: .95rem;
}

.form-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 4px; }

.formerror {
  background: rgba(179, 38, 30, .1);
  border: 1px solid rgba(179, 38, 30, .3);
  color: var(--danger);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: .88rem;
  font-weight: 600;
}

.tagbox {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 12px;
  border: 1px dashed var(--border);
  border-radius: 10px;
  background: var(--bg);
}
.tagbox .row__tag { font-size: .95rem; color: var(--text); }

.danger-zone {
  margin-top: 8px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.danger-zone .form__section { margin: 0; padding: 0; border: 0; }
.danger-zone__rule { border: 0; border-top: 1px solid var(--border); margin: 4px 0; width: 100%; }

.notice {
  background: rgba(162, 100, 10, .1);
  border: 1px solid rgba(162, 100, 10, .3);
  border-radius: 10px;
  padding: 12px;
}
.notice strong { display: block; color: var(--warn); margin-bottom: 4px; font-size: .9rem; }
.notice p { margin: 0; color: var(--muted); }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #8f1e17; }
@media (prefers-color-scheme: dark) {
  .btn-danger { color: #2a0d0b; }
}

.detail__actions { margin-top: 14px; }

/* ---------- Dialog ---------- */

.dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 22px;
  max-width: min(92vw, 420px);
  box-shadow: 0 16px 48px rgba(0, 0, 0, .3);
}
.dialog::backdrop { background: rgba(10, 16, 24, .55); }
.dialog h2 { margin-bottom: 8px; }
.dialog__actions { display: flex; flex-direction: column; gap: 10px; margin-top: 18px; }
.dialog__actions .btn { width: 100%; }

@media (min-width: 560px) {
  .dialog__actions { flex-direction: row-reverse; }
  .dialog__actions .btn { width: auto; flex: 1; }
  .form-actions { flex-direction: row-reverse; }
  .form-actions .btn { flex: 1; }
}

/* ---------- Reports ---------- */

.report-actions { display: flex; gap: 10px; margin: 4px 0 10px; }
.report-actions .btn { flex: 1; min-height: 40px; }

.tablewrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 10px; }
.report-table { border-collapse: collapse; width: 100%; font-size: .82rem; }
.report-table th, .report-table td {
  text-align: left;
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.report-table th { background: var(--bg); position: sticky; top: 0; font-weight: 700; }
.report-table tr:last-child td { border-bottom: 0; }

/* ---------- Label picker ---------- */

.row--pick { cursor: pointer; gap: 12px; }
.label-check { width: 20px; height: 20px; flex: none; accent-color: var(--brand); }

.labelbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--brand);
  border-radius: var(--radius);
  position: sticky;
  top: 56px;
  z-index: 10;
  font-size: .9rem;
  font-weight: 600;
}
.labelbar .btn { margin-left: auto; min-height: 38px; }

/* ---------- Label sheet (screen preview hidden; print only) ---------- */

#labels-print { display: none; }

@media print {
  /* Only the labels leave the printer. */
  body > *:not(#labels-print) { display: none !important; }
  #labels-print { display: block; }
  body { background: #fff; }

  .labelsheet {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 4mm;
  }
  .label {
    border: 1px dashed #999;             /* cutting guide */
    border-radius: 2mm;
    padding: 3mm;
    text-align: center;
    break-inside: avoid;
  }
  .label__qr svg { width: 28mm; height: 28mm; }
  .label__tag {
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: 8pt;
    font-weight: 700;
    letter-spacing: .02em;
    word-break: break-all;
  }
  .label__name {
    font-size: 7pt;
    color: #444;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  background: #16202e;
  color: #fff;
  padding: 12px 18px;
  border-radius: 10px;
  font-size: .9rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  z-index: 60;
  max-width: min(92vw, 460px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--danger); }

/* ---------- Wider screens ---------- */

@media (min-width: 700px) {
  .appbar { padding-left: 24px; padding-right: 24px; }
  .appbar__title { font-size: 1.1rem; }
  main { padding: 24px; }
}
