:root {
  --bg: #f3f2ee;
  --bg-2: #eceae3;
  --surface: #ffffff;
  --surface-2: #f7f7f4;
  --ink: #12231e;
  --ink-2: #4b5a55;
  --ink-3: #8b958f;
  --line: #e4e3dd;
  --brand: #0d7462;
  --brand-ink: #0a5a4c;
  --brand-soft: #e2f0eb;
  --accent: #c8871f;
  --accent-soft: #f6ead2;
  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --ok: #17703a;
  --radius: 18px;
  --radius-sm: 12px;
  --shadow: 0 1px 2px rgba(18, 35, 30, 0.06), 0 12px 28px -16px rgba(18, 35, 30, 0.22);
  --tabbar-h: 64px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior-y: contain;
}

#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* App bar */
.appbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: linear-gradient(160deg, #0e4037, #0b3a31);
  color: #fff;
  box-shadow: 0 6px 18px -10px rgba(0, 0, 0, 0.5);
  padding-top: env(safe-area-inset-top);
}
.appbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 13px;
  background: rgba(255, 255, 255, 0.14);
  border: 1px solid rgba(255, 255, 255, 0.22);
  font-weight: 700;
  font-size: 20px;
}
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-text strong {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 0.2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand-text small { color: rgba(255, 255, 255, 0.72); font-size: 12px; }
.model-chip {
  flex: 0 0 auto;
  font-size: 12px;
  color: var(--brand-soft);
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.16);
  padding: 5px 10px;
  border-radius: 999px;
  white-space: nowrap;
}

/* Main */
.main {
  flex: 1;
  padding: 14px 14px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 20px);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.view { display: none; flex-direction: column; gap: 14px; }
.view.active { display: flex; }

/* Cards */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  border: 1px solid var(--line);
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}
.card-head h2 {
  font-size: 15px;
  font-weight: 700;
  margin: 0;
  letter-spacing: 0.3px;
}
.pill {
  font-size: 11.5px;
  color: var(--ink-2);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 9px;
  white-space: nowrap;
}

/* Photo box */
.photo-box {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  border-radius: var(--radius-sm);
  border: 2px dashed var(--line);
  background: var(--surface-2);
  overflow: hidden;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: border-color 0.15s;
}
.photo-box:active { border-color: var(--brand); }
.photo-box.has-photo { border-style: solid; border-color: var(--line); }
.photo-box img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.photo-empty {
  text-align: center;
  color: var(--ink-3);
  padding: 20px;
}
.photo-empty svg { width: 44px; height: 44px; fill: var(--ink-3); opacity: 0.8; margin-bottom: 6px; }
.photo-empty p { margin: 0; font-size: 15px; color: var(--ink-2); font-weight: 600; }
.photo-empty small { font-size: 12px; color: var(--ink-3); }

/* Buttons */
.btn {
  font: inherit;
  font-weight: 600;
  border: 0;
  border-radius: 14px;
  padding: 13px 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.15s ease, background 0.15s ease;
  user-select: none;
  flex: 1;
}
.btn:active { transform: scale(0.98); }
.btn svg { width: 20px; height: 20px; fill: currentColor; }
.btn-primary { background: var(--brand); color: #fff; box-shadow: 0 8px 20px -10px var(--brand); }
.btn-primary:active { background: var(--brand-ink); }
.btn-primary:disabled { background: var(--ink-3); box-shadow: none; cursor: not-allowed; opacity: 0.7; }
.btn-secondary {
  background: var(--brand-soft);
  color: var(--brand-ink);
  border: 1px solid rgba(13, 116, 98, 0.2);
}
.btn-block { width: 100%; }
.row-gap { display: flex; gap: 10px; margin-top: 12px; }
.row-gap .btn { flex: 1; }

.status-line {
  margin-top: 12px;
  font-size: 13.5px;
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  line-height: 1.45;
}
.status-line.ok { background: var(--brand-soft); color: var(--brand-ink); }
.status-line.warn { background: var(--accent-soft); color: #7a5310; }
.status-line.err { background: var(--danger-soft); color: var(--danger); }
.status-line.loading { background: var(--surface-2); color: var(--ink-2); }

/* Fields */
.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-2);
  margin-bottom: 6px;
}
.field input[type="text"],
.field input[type="number"],
.field input[type="password"],
.field input[type="search"] {
  width: 100%;
  font: inherit;
  padding: 13px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink);
  font-size: 16px;
}
.field input:focus, select:focus, button:focus-visible {
  outline: 3px solid rgba(13, 116, 98, 0.35);
  outline-offset: 1px;
}
.hint { font-size: 12.5px; color: var(--ink-3); margin: 6px 0 0; }
.hint.warn { color: #a05c00; font-weight: 600; }
.hint.ok { color: var(--ok); font-weight: 600; }

.select-wrap { position: relative; }
.select-wrap select {
  width: 100%;
  font: inherit;
  padding: 13px 40px 13px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink);
  font-size: 16px;
  appearance: none;
  -webkit-appearance: none;
}
.select-wrap svg {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  width: 22px;
  height: 22px;
  fill: var(--ink-3);
  pointer-events: none;
}

.stepper {
  display: flex;
  align-items: center;
  gap: 8px;
}
.stepper .step {
  flex: 0 0 auto;
  width: 48px;
  height: 48px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-size: 24px;
  line-height: 1;
  color: var(--ink);
  cursor: pointer;
}
.stepper .step:active { background: var(--brand-soft); }
.stepper input {
  flex: 1;
  text-align: center;
  font-size: 22px;
  font-weight: 700;
}

/* Entry list (records) */
.entry-list { display: flex; flex-direction: column; gap: 8px; }
.entry {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  border: 1px solid var(--line);
}
.entry .entry-ico {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  border-radius: 11px;
  display: grid;
  place-items: center;
  background: var(--brand-soft);
  color: var(--brand-ink);
  font-weight: 700;
  font-size: 15px;
}
.entry .entry-main { flex: 1; min-width: 0; }
.entry .entry-name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.entry .entry-sub { font-size: 12.5px; color: var(--ink-3); }
.entry .entry-qty {
  font-weight: 700;
  font-size: 17px;
  color: var(--brand-ink);
  white-space: nowrap;
}
.entry .entry-act {
  flex: 0 0 auto;
  border: 0;
  background: transparent;
  color: var(--danger);
  font-size: 13px;
  font-weight: 600;
  padding: 6px 8px;
  cursor: pointer;
}
.empty-state { text-align: center; color: var(--ink-3); padding: 22px 10px; font-size: 14px; }

/* Inventory list */
.search-wrap {
  position: relative;
  margin-bottom: 12px;
}
.search-wrap svg {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  width: 20px;
  height: 20px;
  fill: var(--ink-3);
}
.search-wrap input {
  width: 100%;
  font: inherit;
  padding: 12px 14px 12px 42px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-size: 15px;
}

.inventory-list { display: flex; flex-direction: column; gap: 8px; }
.inv-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--surface-2);
}
.inv-row .inv-name { flex: 1; min-width: 0; font-size: 14.5px; font-weight: 600; }
.inv-row .inv-gauge { flex: 0 0 auto; text-align: right; }
.inv-row .inv-avail { font-size: 18px; font-weight: 700; line-height: 1; }
.inv-row .inv-avail.low { color: #b06300; }
.inv-row .inv-avail.zero { color: var(--danger); }
.inv-row .inv-label { font-size: 11px; color: var(--ink-3); }

/* Admin */
.admin-card details { border: 1px solid var(--line); border-radius: var(--radius-sm); margin-bottom: 10px; overflow: hidden; }
.admin-card summary {
  cursor: pointer;
  padding: 12px 14px;
  font-weight: 600;
  font-size: 14px;
  background: var(--surface-2);
  list-style: none;
}
.admin-card summary::-webkit-details-marker { display: none; }
.admin-card summary::after { content: "›"; float: right; transform: rotate(90deg); color: var(--ink-3); }
.admin-card details[open] summary::after { transform: rotate(-90deg); }
.admin-body { padding: 14px; }
.admin-body .btn { width: 100%; }

/* Tabbar */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--line);
  display: flex;
  max-width: 560px;
  margin: 0 auto;
}
.tabbar .tab {
  flex: 1;
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 11.5px;
  color: var(--ink-3);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  cursor: pointer;
  padding: 8px 0 6px;
}
.tabbar .tab svg { width: 24px; height: 24px; fill: currentColor; }
.tabbar .tab.active { color: var(--brand); font-weight: 700; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 18px);
  transform: translateX(-50%) translateY(24px);
  max-width: 88vw;
  z-index: 50;
  background: rgba(18, 35, 30, 0.94);
  color: #fff;
  padding: 12px 18px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  text-align: center;
  box-shadow: 0 10px 30px -8px rgba(0, 0, 0, 0.5);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: rgba(150, 36, 30, 0.96); }
.toast.ok { background: rgba(17, 90, 48, 0.96); }

/* Modal */
.modal { position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; padding: 20px; }
.modal[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(12, 22, 19, 0.45); backdrop-filter: blur(2px); }
.modal-panel {
  position: relative;
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: 0 24px 60px -20px rgba(0, 0, 0, 0.5);
}
.modal-panel h3 { margin: 0 0 14px; font-size: 17px; }
.modal-actions { display: flex; gap: 10px; margin-top: 18px; }

@media (max-width: 380px) {
  #app { font-size: 15px; }
  .card { padding: 14px; }
  .btn { padding: 12px 14px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
