/* ============================================================
   Plantry — mobile-first, dark-mode-aware stylesheet
   ============================================================ */

:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --border: #e5e7eb;
  --text: #111827;
  --text-muted: #6b7280;
  --primary: #b45309;
  --primary-light: #fef3c7;
  --primary-dark: #78350f;
  --danger: #dc2626;
  --danger-light: #fee2e2;
  --warning: #f59e0b;
  --success: #10b981;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.07), 0 2px 4px rgba(0,0,0,0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111827;
    --surface: #1f2937;
    --surface-2: #111827;
    --border: #374151;
    --text: #f9fafb;
    --text-muted: #9ca3af;
    --primary: #f59e0b;
    --primary-light: #451a03;
    --primary-dark: #fbbf24;
    --danger: #f87171;
    --danger-light: #450a0a;
    --shadow: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.3);
  }
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 16px; -webkit-text-size-adjust: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  line-height: 1.5;
}

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

.screen { flex: 1; display: flex; flex-direction: column; padding: 16px; padding-top: calc(16px + env(safe-area-inset-top)); gap: 16px; }
.screen.hidden { display: none !important; }
.hidden { display: none !important; }

/* ---- Header ---- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.app-header h1 { font-size: 18px; font-weight: 700; color: var(--primary); }
.header-actions { display: flex; gap: 8px; min-width: 32px; }
.icon-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  padding: 4px;
  border-radius: 8px;
  color: var(--text-muted);
  transition: background 0.15s;
}
.icon-btn:hover, .icon-btn:active { background: var(--border); }

/* ---- Search ---- */
.search-bar { position: relative; }
.search-bar input {
  width: 100%;
  padding: 10px 40px 10px 40px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s;
}
.search-bar input:focus { border-color: var(--primary); }
.search-bar input::-webkit-search-cancel-button { display: none; }
.search-bar .search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}
.search-bar .search-clear {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.search-bar .search-clear:hover { background: var(--border); color: var(--text); }

/* ---- Location tabs ---- */
.category-tabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: none;
}
.category-tabs::-webkit-scrollbar { display: none; }
.category-tabs.disabled { opacity: 0.5; pointer-events: none; }
@media (hover: hover) and (pointer: fine) {
  .category-tabs {
    scrollbar-width: thin;
    scrollbar-color: var(--text-muted) transparent;
  }
  .category-tabs::-webkit-scrollbar { display: block; height: 6px; }
  .category-tabs::-webkit-scrollbar-track { background: transparent; }
  .category-tabs::-webkit-scrollbar-thumb { background: var(--text-muted); border-radius: 99px; }
  .category-tabs::-webkit-scrollbar-thumb:hover { background: var(--primary); }
}
.tab-btn {
  flex-shrink: 0;
  padding: 6px 14px;
  border-radius: 99px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.tab-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ---- Item list ---- */
.item-list { display: flex; flex-direction: column; gap: 6px; }

.item-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: transform 0.1s;
  -webkit-tap-highlight-color: transparent;
}
.item-row:active { transform: scale(0.98); }
.item-info { flex: 1; min-width: 0; }
.item-name { font-size: 15px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-meta { display: flex; gap: 6px; align-items: center; margin-top: 2px; flex-wrap: wrap; }
.item-category {
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 99px;
  background: var(--border);
  color: var(--text-muted);
}
.item-quantity { font-size: 12px; color: var(--text-muted); }
.item-expiry { font-size: 11px; color: var(--warning); }
.item-chevron { color: var(--text-muted); font-size: 18px; flex-shrink: 0; }

/* ---- Floating add button ---- */
.fab {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  border: none;
  font-size: 24px;
  cursor: pointer;
  box-shadow: var(--shadow-md);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s;
  z-index: 20;
}
.fab:active { transform: scale(0.93); }

/* ---- Detail screen ---- */
.detail-body { padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.detail-row {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.detail-label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 4px; }
.detail-value { font-size: 16px; font-weight: 500; }

/* ---- PIN screen ---- */
.pin-screen { align-items: center; justify-content: center; min-height: 100dvh; }
.pin-title { font-size: 22px; font-weight: 700; text-align: center; }
.pin-subtitle { font-size: 15px; color: var(--text-muted); text-align: center; }
.pin-dots { display: flex; gap: 16px; justify-content: center; margin: 24px 0; }
.pin-dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: transparent;
  transition: all 0.15s;
}
.pin-dot.filled { background: var(--primary); border-color: var(--primary); }
.pin-error { color: var(--danger); font-size: 14px; text-align: center; font-weight: 500; }
.pin-lockout { color: var(--warning); font-size: 14px; text-align: center; }
.pin-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  max-width: 280px;
  width: 100%;
}
.pin-key {
  aspect-ratio: 1;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 22px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.1s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
.pin-key:active { background: var(--primary-light); transform: scale(0.93); }
.pin-key.delete { font-size: 16px; color: var(--text-muted); }
.pin-key:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---- Forms ---- */
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group label { font-size: 14px; font-weight: 500; }
.form-group input,
.form-group select {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text);
  font-size: 15px;
  outline: none;
}
.form-group input:focus,
.form-group select:focus { border-color: var(--primary); }

.person-picker { display: flex; gap: 10px; }
.person-btn {
  flex: 1;
  padding: 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.person-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.duplicate-hint {
  background: var(--primary-light);
  color: var(--primary-dark);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 13px;
}

.btn {
  padding: 14px 20px;
  border-radius: var(--radius);
  border: none;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  width: 100%;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-dark); }
.btn-ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: var(--danger); color: #fff; }

/* ---- Barcode scan ---- */
.scan-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: #000;
  margin: -16px;
  padding: 16px;
}
#scan-video {
  width: 100%;
  max-width: 480px;
  aspect-ratio: 3/4;
  background: #000;
  border-radius: var(--radius);
  object-fit: cover;
}
#scan-qr-fallback { width: 100%; max-width: 480px; }
.scan-hint { color: #fff; font-size: 14px; text-align: center; }
#btn-scan-retry { width: 100%; max-width: 280px; }

/* ---- Modals ---- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  backdrop-filter: blur(4px);
}
.modal-backdrop.hidden { display: none; }
.modal {
  background: var(--surface);
  border-radius: 24px 24px 0 0;
  padding: 24px 20px 40px;
  max-height: 90dvh;
  overflow-y: auto;
  width: 100%;
  max-width: 480px;
}
.modal-handle { width: 40px; height: 4px; background: var(--border); border-radius: 99px; margin: 0 auto 20px; }
.modal h2 { font-size: 20px; font-weight: 700; margin-bottom: 8px; }
.modal-close {
  float: right;
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
}

/* ---- Loading ---- */
.loading-screen { align-items: center; justify-content: center; min-height: 100dvh; }
.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Empty state ---- */
.empty-state { text-align: center; padding: 40px 20px; color: var(--text-muted); }
.empty-state .icon { font-size: 48px; margin-bottom: 12px; }
.empty-state p { font-size: 15px; }

/* ---- Toast ---- */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 20px;
  border-radius: 99px;
  font-size: 14px;
  font-weight: 500;
  z-index: 100;
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none;
  white-space: nowrap;
  max-width: calc(100vw - 40px);
  text-overflow: ellipsis;
  overflow: hidden;
}
.toast.show { opacity: 1; }

/* ---- Offline banner ---- */
.offline-banner {
  background: var(--warning);
  color: #000;
  text-align: center;
  padding: 8px;
  font-size: 13px;
  font-weight: 500;
}
.offline-banner.hidden { display: none; }
