:root {
  color-scheme: light dark;
  --bg: #f4f7f9;
  --surface: #ffffff;
  --text: #1a2b33;
  --text-muted: #5b6b73;
  --brand: #1b4b66;
  --brand-light: #e6eef2;
  --border: #dbe3e7;
  --danger: #c0392b;
  --urgency-expired: #c0392b;
  --urgency-soon: #e0562b;
  --urgency-warning: #d99a1f;
  --urgency-ok: #2f8f5b;
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10181d;
    --surface: #182228;
    --text: #eef3f5;
    --text-muted: #9aabb2;
    --brand: #6fb3d2;
    --brand-light: #1e2f38;
    --border: #2a3941;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: env(safe-area-inset-bottom);
}

a { color: inherit; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1rem;
  background: var(--brand);
  color: white;
  padding-top: calc(0.9rem + env(safe-area-inset-top));
}

.app-header__title {
  font-weight: 700;
  font-size: 1.1rem;
  text-decoration: none;
  color: white;
}

.app-header__nav {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  font-size: 1.2rem;
}

.app-header__nav a { text-decoration: none; }

.sync-indicator {
  font-size: 0.75rem;
  opacity: 0.85;
  min-width: 3.5rem;
}

.sync-indicator--error { color: #ffd1cc; }

#app {
  max-width: 640px;
  margin: 0 auto;
  padding: 1rem;
}

.view { padding-bottom: 5rem; }

.toolbar {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  margin-bottom: 1rem;
}

.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
}

.filter-field {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  flex: 1;
  min-width: 110px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.filter-field select {
  width: 100%;
  min-width: 0;
  padding: 0.4rem 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.use-soon-link {
  display: inline-block;
  color: var(--urgency-soon);
  font-weight: 600;
  font-size: 0.85rem;
  text-decoration: none;
}

.item-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.item-card {
  position: relative;
  display: flex;
  align-items: stretch;
  background: var(--surface);
  border-radius: var(--radius);
  border-left: 5px solid var(--urgency-ok);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
  overflow: hidden;
}

.item-card.urgency-expired { border-left-color: var(--urgency-expired); }
.item-card.urgency-soon { border-left-color: var(--urgency-soon); }
.item-card.urgency-warning { border-left-color: var(--urgency-warning); }
.item-card.urgency-ok { border-left-color: var(--urgency-ok); }

.item-card__body {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.8rem 0.9rem;
  background: none;
  border: none;
  text-align: left;
  cursor: pointer;
  color: inherit;
  font: inherit;
  min-width: 0;
}

.item-card__thumb {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  /* Whole photo, scaled to fit; the background fills the margins. */
  object-fit: contain;
  background: var(--brand-light);
}

.item-card__main {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  min-width: 0;
}

.item-card__name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item-card__meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  display: flex;
  gap: 0.3rem;
  flex-wrap: wrap;
}

.item-card__dot { opacity: 0.6; }

.item-card__expiry { flex: none; }

.expiry-badge {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.25rem 0.55rem;
  border-radius: 999px;
  white-space: nowrap;
}

.expiry-badge--expired { background: color-mix(in srgb, var(--urgency-expired) 18%, transparent); color: var(--urgency-expired); }
.expiry-badge--soon { background: color-mix(in srgb, var(--urgency-soon) 18%, transparent); color: var(--urgency-soon); }
.expiry-badge--warning { background: color-mix(in srgb, var(--urgency-warning) 20%, transparent); color: var(--urgency-warning); }
.expiry-badge--ok { background: color-mix(in srgb, var(--urgency-ok) 15%, transparent); color: var(--urgency-ok); }

.item-card__delete {
  flex: none;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 1rem;
  padding: 0 0.9rem;
  cursor: pointer;
}

.item-card__delete:hover { color: var(--danger); }

/* --- List view: shelf-life bar behind the row --- */

.item-card__bar {
  position: absolute;
  inset: 0 auto 0 0;
  background: color-mix(in srgb, var(--urgency-ok) 14%, transparent);
  pointer-events: none;
}
.item-card.urgency-soon .item-card__bar { background: color-mix(in srgb, var(--urgency-soon) 16%, transparent); }
.item-card.urgency-warning .item-card__bar { background: color-mix(in srgb, var(--urgency-warning) 18%, transparent); }

/* Content sits above the bar. */
.item-card__body,
.item-card__delete { position: relative; }

.item-card__days {
  font-size: 0.85rem;
  font-weight: 700;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.item-card__days--expired { color: var(--urgency-expired); }
.item-card__days--soon { color: var(--urgency-soon); }
.item-card__days--warning { color: var(--urgency-warning); }
.item-card__days--ok { color: var(--urgency-ok); }

/* --- Grid view --- */

.item-list--grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.5rem;
}

.item-card--grid {
  border-left: none;
  border-top: 5px solid var(--urgency-ok);
  min-width: 0;
}
.item-card--grid.urgency-expired { border-top-color: var(--urgency-expired); }
.item-card--grid.urgency-soon { border-top-color: var(--urgency-soon); }
.item-card--grid.urgency-warning { border-top-color: var(--urgency-warning); }

.item-card--grid .item-card__body {
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  gap: 0.3rem;
  padding: 0 0 0.5rem;
}

.item-card--grid .item-card__thumb {
  width: 100%;
  height: auto;
  aspect-ratio: 1;
  border-radius: 0;
}

.item-card__thumb--empty {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--brand-light);
  color: var(--text-muted);
  font-size: 2rem;
  font-weight: 700;
}

.item-card--grid .item-card__name,
.item-card--grid .item-card__meta { padding: 0 0.5rem; }

/* Three tiles per row leaves ~90px per tile on a 320px phone. */
.item-card--grid .item-card__name { font-size: 0.85rem; }
.item-card--grid .item-card__meta { font-size: 0.7rem; gap: 0.2rem; }

.item-card--grid .expiry-badge {
  align-self: flex-start;
  margin: 0 0.5rem;
  font-size: 0.65rem;
  padding: 0.2rem 0.4rem;
  max-width: calc(100% - 1rem);
  white-space: normal;
}

.item-card--grid .item-card__delete {
  position: absolute;
  top: 0.25rem;
  right: 0.25rem;
  width: 1.75rem;
  height: 1.75rem;
  padding: 0;
  border-radius: 50%;
  background: color-mix(in srgb, var(--surface) 85%, transparent);
}


.empty-state {
  list-style: none;
  text-align: center;
  color: var(--text-muted);
  padding: 3rem 1rem;
}

.fab {
  position: fixed;
  right: 1.2rem;
  bottom: calc(1.2rem + env(safe-area-inset-bottom));
  width: 3.5rem;
  height: 3.5rem;
  border-radius: 50%;
  border: none;
  background: var(--brand);
  color: white;
  font-size: 1.8rem;
  line-height: 1;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  cursor: pointer;
}

.view-subtitle {
  color: var(--text-muted);
  margin-top: 0;
}

.back-link {
  display: inline-block;
  margin: 0.75rem 0;
  text-decoration: none;
}

.item-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  background: var(--surface);
  padding: 1rem;
  border-radius: var(--radius);
}

.item-form label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  flex: 1;
  /* Flex items default to min-width:auto, so native date/select inputs
     refuse to shrink below their intrinsic width and overflow the
     viewport on narrow phones. This lets them shrink properly. */
  min-width: 0;
}

.item-form input,
.item-form select,
.item-form textarea {
  font: inherit;
  width: 100%;
  padding: 0.55rem 0.6rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.name-row {
  display: flex;
  gap: 0.5rem;
}

.name-row input { min-width: 0; }
.name-row .btn { flex: none; }

.photo-field {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.photo-preview {
  height: 240px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.photo-preview img {
  width: 100%;
  height: 100%;
  object-fit: contain; /* whole photo, like the thumbnails */
}

.photo-placeholder {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.scan-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: black;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  padding-top: calc(1rem + env(safe-area-inset-top));
  padding-bottom: calc(1rem + env(safe-area-inset-bottom));
}

.scan-overlay video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.scan-overlay__frame {
  position: relative;
  width: min(80vw, 320px);
  aspect-ratio: 3 / 2;
  border: 3px solid white;
  border-radius: 12px;
  box-shadow: 0 0 0 999px rgba(0, 0, 0, 0.45);
}

.scan-overlay__hint {
  position: relative;
  color: white;
  margin-top: 1rem;
  font-size: 0.9rem;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
}

.scan-overlay__cancel {
  position: relative;
  margin-top: 1.5rem;
}

.form-row {
  display: flex;
  gap: 0.75rem;
}

/* Below ~380px, native date inputs get too cramped to use comfortably
   even once they're allowed to shrink — stack them instead. */
@media (max-width: 380px) {
  .form-row {
    flex-direction: column;
    gap: 1rem;
  }
}

.form-actions {
  display: flex;
  gap: 0.6rem;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.btn {
  font: inherit;
  padding: 0.55rem 1.1rem;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
}

.btn--primary { background: var(--brand); color: white; }
.btn--secondary { background: var(--brand-light); color: var(--brand); border-color: var(--border); }
.btn--ghost { background: transparent; color: var(--text-muted); border-color: var(--border); }
.btn--danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn--icon { padding: 0.3rem 0.55rem; background: transparent; border: 1px solid var(--border); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.settings-section {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
}

.settings-section h2 {
  margin-top: 0;
  font-size: 1rem;
}

.settings-section > label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.settings-section > label input[type="text"] {
  width: 100%;
  min-width: 0;
  padding: 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.origin-row code {
  flex: 1;
  min-width: 0;
  overflow-wrap: break-word;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.4rem 0.6rem;
  font-size: 0.8rem;
}

.settings-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-top: 0.6rem;
  flex-wrap: wrap;
}

.settings-actions input[type="text"],
.settings-actions input[type="email"] {
  flex: 1;
  min-width: 0;
  padding: 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.hint {
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* Blank until a scan: no gap under the name field. */
#barcode-status:empty { display: none; }

/* Shared by Settings' editable lists (storage zones, categories). */
.editable-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.editable-list li {
  display: flex;
  gap: 0.4rem;
}

.editable-list-input {
  flex: 1;
  min-width: 0;
  padding: 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.file-input-label {
  display: inline-flex;
  align-items: center;
}

.app-version {
  text-align: center;
  margin-top: 1.5rem;
}

.toolbar-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

/* After .btn, so its padding wins. */
.view-toggle {
  flex: none;
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.4rem 0.6rem;
}

.member-list {
  margin: 0 0 0.75rem;
  padding-left: 1.2rem;
  font-size: 0.9rem;
  color: var(--text-muted);
}
