:root {
  --bg: #f4f5f2;
  --card: #ffffff;
  --ink: #1c1e21;
  --muted: #6b7280;
  --primary: #1f6f43;
  --primary-dark: #175231;
  --danger: #c62828;
  --warn: #b45309;
  --ok: #1f6f43;
  --border: #e3e5e0;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  --tabbar-h: 60px;
}

* { box-sizing: border-box; }

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

body { display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--primary);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 5;
}
.topbar h1 { margin: 0; font-size: 18px; font-weight: 700; }
.stats { display: flex; gap: 8px; font-size: 12px; }
.stats .stat { background: rgba(255, 255, 255, 0.18); padding: 3px 8px; border-radius: 999px; }

main { flex: 1; overflow-y: auto; padding-bottom: var(--tabbar-h); }
.view { display: none; padding: 14px; max-width: 720px; margin: 0 auto; }
.view.active { display: block; }

h2 { font-size: 16px; margin: 4px 0 12px; }

.toolbar { display: flex; gap: 8px; margin-bottom: 12px; align-items: center; }
.toolbar select {
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  font-size: 14px;
}
#filter-location { flex: 1; }
#filter-status { flex-shrink: 0; }
.chip {
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
}

/* Items */
.item-list { display: flex; flex-direction: column; gap: 10px; }
.item {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
  display: flex;
  gap: 12px;
  border-left: 4px solid var(--border);
}
.item.expired { border-left-color: var(--danger); }
.item.soon { border-left-color: var(--warn); }
.item.fresh { border-left-color: var(--ok); }
.item.no-expiry { border-left-color: #9ca3af; }
.item.archived { border-left-color: #9ca3af; opacity: 0.85; }
.item img.thumb {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 8px;
  background: #eee;
  flex-shrink: 0;
}
.item .body { flex: 1; min-width: 0; }
.item .photo-row { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.item .photo-row img.mini {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 6px;
  background: #eee;
  border: 1px solid var(--border);
  cursor: zoom-in;
}
.item .name { font-weight: 600; font-size: 15px; }
.item .meta { font-size: 13px; color: var(--muted); margin-top: 2px; }
.item .badges { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: #eef1ee;
  color: var(--ink);
}
.badge.danger { background: #fdeaea; color: var(--danger); }
.badge.warn { background: #fdf3e0; color: var(--warn); }
.badge.ok { background: #e7f4ec; color: var(--ok); }
.badge.muted { background: #eef0f2; color: var(--muted); }
.item .actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }

/* Buttons */
.btn {
  border: none;
  border-radius: 8px;
  padding: 9px 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  background: #eef0ec;
  color: var(--ink);
}
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:active { background: var(--primary-dark); }
.btn.ghost { background: transparent; color: var(--primary); border: 1px solid var(--border); }
.btn.danger { background: #fdeaea; color: var(--danger); }
.btn.ok { background: #e7f4ec; color: var(--ok); }
.btn.small { padding: 5px 10px; font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; }
.row.two > .field { flex: 1; }
.row.actions { margin-top: 16px; justify-content: space-between; }

/* Forms */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; color: #374151; }
.field input[type="text"],
.field input[type="number"],
.field input[type="date"],
.field select {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  font-size: 15px;
}
.field .row input { flex: 1; }
.seg { display: flex; margin-bottom: 8px; }
.seg-btn {
  flex: 1;
  padding: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.seg-btn:first-child { border-radius: 8px 0 0 8px; }
.seg-btn:last-child { border-radius: 0 8px 8px 0; }
.seg-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.photo-actions { display: flex; gap: 8px; margin-top: 8px; }
.photo-actions .photo-btn { flex: 1; margin-top: 0; }
.photo-wrap { margin-top: 8px; display: flex; flex-direction: column; gap: 8px; }
.photo-wrap.hidden { display: none; }
.photo-btn {
  display: inline-block;
  text-align: center;
  padding: 10px;
  border: 1.5px dashed var(--primary);
  border-radius: 8px;
  color: var(--primary);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
}
.photo-btn.ready { background: #e7f4ec; border-style: solid; }
.photo-preview { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.preview-item { position: relative; }
.preview-item img.mini {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 6px;
  background: #eee;
  border: 1px solid var(--border);
  display: block;
}
.preview-item .btn {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 20px;
  height: 20px;
  padding: 0;
  line-height: 18px;
  border-radius: 50%;
  font-size: 14px;
}
.hint { font-size: 12px; color: var(--muted); margin-top: 5px; }
.location-manage { margin-top: 8px; border: 1px solid var(--border); border-radius: 8px; padding: 8px; background: var(--card); }
.location-manage.hidden { display: none; }
.manage-loc { display: flex; align-items: center; justify-content: space-between; padding: 4px 2px; }
.manage-loc + .manage-loc { border-top: 1px solid var(--border); }
.manage-loc span { font-size: 14px; }
.status { font-size: 13px; margin-top: 8px; min-height: 18px; }
.status.error { color: var(--danger); }
.status.ok { color: var(--ok); }

/* Chat */
.chat-log { display: flex; flex-direction: column; gap: 10px; padding-bottom: 10px; }
.msg {
  max-width: 82%;
  padding: 10px 12px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.user { align-self: flex-end; background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.msg.agent { align-self: flex-start; background: var(--card); box-shadow: var(--shadow); border-bottom-left-radius: 4px; }
.msg.typing { color: var(--muted); font-style: italic; }
.msg .chat-img { max-width: 180px; max-height: 180px; border-radius: 8px; display: block; margin-top: 6px; }
.msg .chat-img + .chat-img { margin-top: 6px; }
.chat-img { cursor: zoom-in; }
.chat-input-wrap {
  display: flex;
  gap: 8px;
  padding: 10px 0;
  position: sticky;
  bottom: calc(var(--tabbar-h) + 4px);
  background: var(--bg);
}
.chat-input-wrap textarea {
  flex: 1;
  resize: none;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;  font-family: inherit;
  max-height: 120px;
}
#btn-mic {
  flex-shrink: 0;
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 8px;
  min-width: 40px;
  font-size: 16px;
  cursor: pointer;
}
#btn-mic.listening {
  background: var(--danger);
  color: #fff;
  border-color: var(--danger);
  animation: micpulse 1.2s ease-in-out infinite;
}
@keyframes micpulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, .45); }
  50% { box-shadow: 0 0 0 8px rgba(220, 38, 38, 0); }
}

/* Tab bar */
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--tabbar-h);
  background: var(--card);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 10;
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  flex: 1;
  border: none;
  background: none;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}
.tab.active { color: var(--primary); }
.tab-icon { font-size: 18px; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + 16px);
  transform: translateX(-50%);
  background: #222;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 20;
  max-width: 90%;
  text-align: center;
}
.toast.show { opacity: 1; }
.empty-state { text-align: center; color: var(--muted); padding: 40px 0; font-size: 14px; }

/* Scan overlay */
.scan-overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
}
.scan-overlay.hidden { display: none; }
.scan-box {
  width: 100%;
  height: 100%;
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 20px;
}
#scan-video {
  width: 100%;
  max-height: 70vh;
  object-fit: contain;
  border-radius: 12px;
  background: #000;
}
.scan-guide {
  color: #fff;
  font-size: 15px;
  text-align: center;
  padding: 6px 14px;
  background: rgba(0, 0, 0, 0.55);
  border-radius: 999px;
}
.scan-guide.error { color: #ffb4b4; }
.scan-actions {
  display: flex;
  gap: 10px;
  width: 100%;
  max-width: 320px;
}
.scan-actions .btn { flex: 1; background: #fff; color: #111; }
.scan-actions .btn:active { background: #e5e5e5; }

@media (min-width: 720px) {
  .view { padding: 20px; }
}
