:root {
  --bg: #faf7f2;
  --surface: #ffffff;
  --text: #2a2420;
  --muted: #7a6f66;
  --line: #e8e0d6;
  --accent: #a47a2c;
  --accent-ink: #ffffff;
  --accent-soft: #f4ead6;
  --in: #1f7a4d;
  --in-soft: #e3f3ea;
  --out: #b3412f;
  --out-soft: #fbe8e4;
  --warn: #9a6a00;
  --warn-soft: #fff3d1;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / .05), 0 4px 16px rgb(0 0 0 / .04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171412;
    --surface: #221e1b;
    --text: #f1ebe4;
    --muted: #a89c91;
    --line: #3a332e;
    --accent: #d4a94f;
    --accent-ink: #1b1611;
    --accent-soft: #3a2f1c;
    --in: #5fc795;
    --in-soft: #1d3428;
    --out: #f08a78;
    --out-soft: #3d221d;
    --warn: #f0c25a;
    --warn-soft: #3a3019;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
button, input, select, textarea { font: inherit; color: inherit; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 18px; margin: 0; letter-spacing: .2px; }
.me { display: flex; gap: 10px; align-items: center; font-size: 13px; color: var(--muted); margin-right: auto; margin-left: 12px; }
.me b { color: var(--text); }
.me .role { font-size: 11px; padding: 2px 7px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.me a { color: var(--accent); }
.bell { position: relative; font-size: 17px; padding: 6px 12px; }
.bell-n {
  position: absolute; top: -6px; right: -6px; min-width: 20px; height: 20px; padding: 0 5px;
  border-radius: 999px; background: var(--out); color: #fff; font-size: 12px; font-weight: 700;
  display: grid; place-items: center; line-height: 1;
}
.alerts { display: grid; gap: 6px; max-height: 50dvh; overflow: auto; }
.alert-row { display: grid; grid-template-columns: 1fr auto auto; gap: 10px; align-items: center; padding: 8px 10px; border: 1px solid var(--line); border-left: 4px solid var(--warn); border-radius: 10px; }
.alert-row.zero { border-left-color: var(--out); }
.alert-row .nm { font-size: 14px; }
.alert-row .nm small { display: block; color: var(--muted); }
.alert-row .q { font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; }
.alert-row .q small { display: block; font-weight: 400; font-size: 11px; color: var(--muted); }
.alert-row button { width: 40px; height: 40px; padding: 0; font-size: 20px; font-weight: 600; color: var(--in); background: var(--in-soft); border-color: transparent; }
.notify-box { border-top: 1px solid var(--line); padding-top: 12px; display: grid; gap: 8px; }
.notify-box h3 { margin: 0; font-size: 15px; }
.row.wrap { flex-wrap: wrap; }
.chat-opt { display: flex; gap: 8px; align-items: center; justify-content: space-between; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; }
.chat-opt code { font-size: 13px; background: var(--accent-soft); padding: 2px 6px; border-radius: 6px; user-select: all; }
.ok-msg { color: var(--in); background: var(--in-soft); padding: 8px 10px; border-radius: 8px; margin: 0; font-size: 14px; }
/* ---------- emergency ---------- */
.sos-fab {
  position: fixed; right: 16px; bottom: 16px; z-index: 6;
  display: inline-flex; gap: 8px; align-items: center;
  background: #c62828; color: #fff; border: 0; border-radius: 999px;
  padding: 12px 20px; min-height: 56px; font-size: 16px;
  box-shadow: 0 4px 14px rgb(198 40 40 / .45);
}
.sos-fab span:first-child { font-size: 22px; }
.sos-fab:hover { background: #b71c1c; }
.sos-n {
  position: absolute; top: -6px; right: -4px; min-width: 22px; height: 22px; padding: 0 6px;
  border-radius: 999px; background: #fff; color: #c62828; font-size: 12px; font-weight: 800;
  display: grid; place-items: center; border: 2px solid #c62828;
}
main { padding-bottom: 96px; }
.danger-dlg h2 { color: #c62828; }
.cam-row { display: grid; grid-template-columns: 2fr 1fr; gap: 8px; }
.camera-btn, .gallery-btn {
  display: grid; place-items: center; text-align: center; cursor: pointer;
  border-radius: 12px; padding: 14px 10px; font-size: 16px; font-weight: 600; color: var(--text);
}
.camera-btn { background: #c62828; color: #fff; min-height: 64px; font-size: 18px; }
.gallery-btn { border: 1px solid var(--line); background: var(--bg); font-size: 14px; }
.report-photos { display: flex; gap: 8px; flex-wrap: wrap; }
.report-photos:empty { display: none; }
.rp { position: relative; width: 84px; height: 84px; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); }
.rp img { width: 100%; height: 100%; object-fit: cover; }
.rp button { position: absolute; top: 4px; right: 4px; width: 26px; height: 26px; min-height: 0; padding: 0; border-radius: 50%; background: rgb(0 0 0 / .65); color: #fff; border: 0; font-size: 14px; }
.types { min-width: 0; border: 0; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.types legend { font-size: 13px; color: var(--muted); padding: 0; margin-bottom: 6px; }
.types label { display: block; position: relative; }
.types input { position: absolute; opacity: 0; pointer-events: none; }
.types span { display: block; padding: 8px 12px; border-radius: 999px; border: 1px solid var(--line); color: var(--text); font-size: 14px; cursor: pointer; }
.types input:checked + span { background: #c62828; border-color: #c62828; color: #fff; font-weight: 600; }
.types input:focus-visible + span { outline: 2px solid var(--accent); }
.sos-send { background: #c62828; color: #fff; border-color: #c62828; font-weight: 700; }
.sos-send:hover { background: #b71c1c; border-color: #b71c1c; }

.alarm-dlg { border: 3px solid #c62828; }
.alarm-dlg::backdrop { background: rgb(120 0 0 / .55); }
.alarm-head {
  margin: -18px -18px 4px; padding: 14px 18px; background: #c62828; color: #fff;
  font-size: 22px; font-weight: 800; letter-spacing: .5px; animation: flash 1s steps(2) 6;
}
@keyframes flash { 50% { background: #7f0000; } }
@media (prefers-reduced-motion: reduce) { .alarm-head { animation: none; } }
.alarm-type { font-size: 20px; font-weight: 700; margin: 0; }
.alarm-meta { color: var(--muted); font-size: 14px; margin: 2px 0 0; }
.alarm-note { font-size: 16px; margin: 8px 0 0; white-space: pre-wrap; }
.alarm-photos { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 6px; margin-top: 10px; }
.alarm-photos button { padding: 0; border: 0; border-radius: 10px; overflow: hidden; min-height: 0; cursor: zoom-in; background: none; }
.alarm-photos img { width: 100%; max-height: 42dvh; object-fit: cover; display: block; }
.alarm-photos.one { grid-template-columns: 1fr; }
.alarm-more { margin: 8px 0 0; color: #c62828; font-weight: 600; font-size: 14px; }

.reports { display: grid; gap: 8px; max-height: 60dvh; overflow: auto; }
.report { border: 1px solid var(--line); border-left: 4px solid #c62828; border-radius: 10px; padding: 10px 12px; display: grid; gap: 6px; }
.report.resolved { border-left-color: var(--in); opacity: .85; }
.report .top { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.report .ty { font-weight: 700; }
.report .st { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: #fdecea; color: #c62828; }
.report.resolved .st { background: var(--in-soft); color: var(--in); }
.report .meta { color: var(--muted); font-size: 13px; }
.report .thumbs { display: flex; gap: 6px; flex-wrap: wrap; }
.report .thumbs button { width: 72px; height: 72px; padding: 0; min-height: 0; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); cursor: zoom-in; }
.report .thumbs img { width: 100%; height: 100%; object-fit: cover; }
.report .acts { display: flex; gap: 8px; flex-wrap: wrap; }
.report .acts button { min-height: 34px; padding: 4px 12px; font-size: 13px; }

/* ---------- search by photo ---------- */
.photo-search-btn { white-space: nowrap; }
.camera-btn.neutral { background: var(--accent); color: var(--accent-ink); }
.ps-query { display: flex; gap: 10px; align-items: center; }
.ps-query img { width: 72px; height: 72px; object-fit: cover; border-radius: 10px; border: 2px solid var(--accent); }
.ps-results { display: grid; gap: 6px; max-height: 50dvh; overflow: auto; }
.ps-row { display: grid; grid-template-columns: 64px 1fr auto; gap: 10px; align-items: center; padding: 8px; border: 1px solid var(--line); border-radius: 10px; }
.ps-row.weak { opacity: .6; }
.ps-row img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; }
.ps-row .nm { font-size: 14px; min-width: 0; }
.ps-row .nm small { display: block; color: var(--muted); }
.ps-row .score { display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 999px; margin-top: 3px; }
.score.hi { background: var(--in-soft); color: var(--in); }
.score.mid { background: var(--warn-soft); color: var(--warn); }
.score.lo { background: var(--line); color: var(--muted); }
.ps-row .btns { display: flex; gap: 4px; align-items: center; }
.ps-row .btns button { min-height: 36px; padding: 4px 10px; font-size: 13px; }
.ps-row .btns .minus { color: var(--out); background: var(--out-soft); border-color: transparent; font-size: 18px; width: 36px; padding: 0; }
.ps-row .btns .plus { color: var(--in); background: var(--in-soft); border-color: transparent; font-size: 18px; width: 36px; padding: 0; }
.item.flash { animation: itemflash 1.6s ease-out; }
@keyframes itemflash { 0%, 40% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: var(--shadow); } }

.banner { position: fixed; top: 0; left: 0; right: 0; z-index: 20; background: var(--out); color: #fff; padding: 12px 16px; text-align: center; }
.banner button { margin-left: 10px; min-height: 32px; padding: 4px 12px; color: var(--text); }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 50%; border: 1px solid rgb(0 0 0 / .2); vertical-align: -1px; margin-right: 3px; }
.mv .by { color: var(--accent); font-weight: 600; }
.top-actions { display: flex; flex-wrap: wrap; gap: 8px; }

main { max-width: 1100px; margin: 0 auto; padding: 16px; }

button {
  border: 1px solid var(--line); background: var(--surface);
  border-radius: 10px; padding: 8px 14px; cursor: pointer;
  min-height: 40px;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.ghost { background: transparent; }
button.danger { color: var(--out); }
button:disabled { opacity: .5; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); padding: 9px 11px; min-height: 40px;
}
textarea { resize: vertical; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin-bottom: 14px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.stat .k { color: var(--muted); font-size: 13px; }
.stat .v { font-size: 22px; font-weight: 650; font-variant-numeric: tabular-nums; }
.stat.warn .v { color: var(--warn); }

.filters { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.filters input[type=search] { flex: 1 1 260px; }
.check { display: inline-flex; gap: 6px; align-items: center; white-space: nowrap; color: var(--muted); }
.check input { width: auto; min-height: 0; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 12px 0; scrollbar-width: thin; }
.chip {
  flex: none; border-radius: 999px; padding: 6px 14px; min-height: 34px;
  background: var(--surface); border: 1px solid var(--line); color: var(--muted);
}
.chip[aria-pressed=true] { background: var(--accent-soft); border-color: var(--accent); color: var(--text); font-weight: 600; }
.chip .n { opacity: .7; margin-left: 4px; font-variant-numeric: tabular-nums; }

.list { display: grid; gap: 10px; }
.item {
  display: grid; grid-template-columns: auto 1fr auto; gap: 10px 16px; align-items: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; box-shadow: var(--shadow);
}
.item.low { border-left: 4px solid var(--warn); }
.item.zero { border-left: 4px solid var(--out); }
.thumb {
  width: 64px; height: 64px; border-radius: 10px; overflow: hidden; padding: 0; min-height: 0;
  background: var(--accent-soft); border: 1px solid var(--line);
  display: grid; place-items: center; font-size: 26px; color: var(--accent);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
button.thumb { cursor: zoom-in; }
.item .head { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.item .code { font: 600 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--accent-soft); padding: 4px 7px; border-radius: 6px; letter-spacing: .5px; }
.item .name { font-weight: 600; font-size: 16px; }
.item .meta { color: var(--muted); font-size: 13px; margin-top: 3px; }
.item .links { display: flex; gap: 12px; margin-top: 6px; }
.linkbtn { background: none; border: 0; padding: 0; min-height: 0; color: var(--accent); cursor: pointer; font-size: 13px; }
.badge { font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 999px; background: var(--warn-soft); color: var(--warn); }
.badge.out { background: var(--out-soft); color: var(--out); }

.qty { display: flex; align-items: center; gap: 6px; }
.qty button { width: 44px; height: 44px; padding: 0; font-size: 22px; font-weight: 600; border-radius: 12px; }
.qty .minus { color: var(--out); background: var(--out-soft); border-color: transparent; }
.qty .plus { color: var(--in); background: var(--in-soft); border-color: transparent; }
.qty .n { min-width: 52px; text-align: center; font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.qty .n small { display: block; font-size: 11px; font-weight: 400; color: var(--muted); }

.empty { text-align: center; color: var(--muted); padding: 40px 10px; }

dialog {
  border: 1px solid var(--line); border-radius: 16px; padding: 0;
  background: var(--surface); color: var(--text);
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog form, .dlg-body { padding: 18px; display: grid; gap: 12px; }
dialog h2 { margin: 0; font-size: 18px; }
label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
label > input, label > select, label > textarea, label > .row, label > .stepper { color: var(--text); font-size: 15px; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.grid .full { grid-column: 1 / -1; }
.row { display: flex; gap: 8px; }
.row > input, .row > select { flex: 1; }
.actions { display: flex; gap: 8px; align-items: center; }
.spacer { flex: 1; }
.muted { color: var(--muted); margin: 0; font-size: 13px; }
.error { color: var(--out); background: var(--out-soft); padding: 8px 10px; border-radius: 8px; margin: 0; font-size: 14px; }
.code-line { margin: 0; font: 600 13px ui-monospace, Menlo, monospace; color: var(--accent); }

.photo-field { display: flex; gap: 14px; align-items: center; }
.photo-preview {
  flex: none; width: 96px; height: 96px; border-radius: 12px; overflow: hidden;
  background: var(--accent-soft); border: 1px dashed var(--accent);
  display: grid; place-items: center; color: var(--muted); font-size: 12px;
}
.photo-preview img { width: 100%; height: 100%; object-fit: cover; }
.photo-btns { display: grid; gap: 6px; justify-items: start; }
.filebtn {
  display: inline-flex; gap: 6px; align-items: center; cursor: pointer;
  border: 1px solid var(--accent); color: var(--text); font-size: 15px;
  border-radius: 10px; padding: 8px 14px; min-height: 40px; background: var(--accent-soft);
}
.photo-btns button { min-height: 34px; padding: 4px 10px; font-size: 14px; }
.photo-dlg { width: min(720px, calc(100vw - 24px)); }
.photo-dlg img { width: 100%; max-height: 75dvh; object-fit: contain; border-radius: 10px; background: var(--bg); }

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seg label { display: block; position: relative; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; text-align: center; padding: 10px; border-radius: 10px; border: 1px solid var(--line); cursor: pointer; color: var(--text); font-size: 15px; font-weight: 600; }
.seg input[value=in]:checked + span { background: var(--in-soft); border-color: var(--in); color: var(--in); }
.seg input[value=out]:checked + span { background: var(--out-soft); border-color: var(--out); color: var(--out); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); }

.stepper { display: grid; grid-template-columns: 48px 1fr 48px; gap: 8px; }
.stepper input { text-align: center; font-size: 18px; font-weight: 600; }
.stepper button { font-size: 20px; padding: 0; }

.history { display: grid; gap: 6px; max-height: 60dvh; overflow: auto; }
.mv { display: grid; grid-template-columns: 56px 1fr auto; gap: 10px; align-items: center; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; }
.mv .chg { font-weight: 700; font-variant-numeric: tabular-nums; text-align: center; border-radius: 8px; padding: 4px 0; }
.mv .chg.in { color: var(--in); background: var(--in-soft); }
.mv .chg.out { color: var(--out); background: var(--out-soft); }
.mv .what { font-size: 14px; }
.mv .what small { display: block; color: var(--muted); }
.mv .when { color: var(--muted); font-size: 12px; text-align: right; white-space: nowrap; }

.cat-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; max-height: 50dvh; overflow: auto; }
.cat-list li { display: flex; gap: 8px; align-items: center; padding: 6px 8px; border: 1px solid var(--line); border-radius: 10px; }
.cat-list .pfx { font: 600 12px ui-monospace, Menlo, monospace; background: var(--accent-soft); padding: 3px 6px; border-radius: 6px; }
.cat-list .nm { flex: 1; }
.cat-list .ct { color: var(--muted); font-size: 12px; white-space: nowrap; }
.cat-list button { min-height: 32px; padding: 4px 10px; font-size: 13px; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  opacity: 0; pointer-events: none; transition: .2s; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%); }

@media (max-width: 560px) {
  .topbar h1 { font-size: 16px; }
  .me { margin-left: 0; width: 100%; order: 3; }
  .top-actions { order: 4; }
  .top-actions { width: 100%; }
  .top-actions .bell { flex: 0 0 auto; }
  .sos-fab { right: 12px; bottom: 12px; padding: 10px 16px; min-height: 52px; }
  .cam-row { grid-template-columns: 1fr 1fr; }
  .filters input[type=search] { flex-basis: 100%; }
  .ps-row { grid-template-columns: 56px 1fr; }
  .ps-row img { width: 56px; height: 56px; }
  .ps-row .btns { grid-column: 1 / -1; justify-content: flex-end; }
  .top-actions button { flex: 1; padding: 8px 8px; font-size: 14px; }
  .grid { grid-template-columns: 1fr; }
  .stats { grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
  .stat { padding: 8px 10px; }
  .stat .k { font-size: 11px; }
  .stat .v { font-size: 18px; }
  .item { gap: 8px 10px; padding: 10px 12px; grid-template-columns: auto 1fr; }
  .item .qty { grid-column: 1 / -1; justify-content: flex-end; margin-top: -4px; }
  .thumb { width: 56px; height: 56px; align-self: start; }
  .qty { gap: 4px; }
  .qty button { width: 38px; height: 38px; font-size: 20px; }
  .qty .n { min-width: 40px; font-size: 19px; }
}
