:root {
  color-scheme: light;
  --bg: #f6f5f2;
  --surface: #ffffff;
  --border: #e6e1d6;
  --border-strong: #d8d0c2;
  --text: #2b2620;
  --muted: #7a7266;
  --muted-soft: #a89f90;
  --accent: #b5652b;
  --accent-dark: #8f4d1f;
  --accent-soft: #f4e4d5;
  --danger: #a3372f;
  --danger-dark: #7d2a24;
  --danger-soft: #f3d9d5;
  --success-bg: #eef4e6;
  --success-text: #3d5c26;
  --error-bg: #f7e6e3;
  --error-text: #7d2a24;
  --info-bg: #eef1f4;
  --info-text: #3a4a5a;
  --radius-sm: 7px;
  --radius-md: 11px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(43, 38, 32, 0.05);
  --shadow-md: 0 10px 24px -8px rgba(43, 38, 32, 0.16);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

/* Defensive: several elements below (buttons, the scanner overlay) set
   their own `display`, which would otherwise fight the native [hidden]
   behavior. This keeps hidden always mean hidden. */
[hidden] { display: none !important; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent-dark); text-decoration: none; }
a:hover { text-decoration: underline; }

.icon {
  flex: 0 0 auto;
  vertical-align: -0.15em;
}

code {
  background: #eee7da;
  padding: 0.15em 0.45em;
  border-radius: 5px;
  font-size: 0.85em;
}

:focus-visible {
  outline: 2px solid var(--accent-dark);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ---------------------------------------------------------------- topbar */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1.5rem;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-weight: 700;
  font-size: 1.15rem;
  color: var(--text);
}
.brand:hover { text-decoration: none; color: var(--accent-dark); }
.brand-mark { font-size: 1.25rem; line-height: 1; }

.search-form {
  display: flex;
  gap: 0.5rem;
  flex: 1;
  max-width: 420px;
}

.search-wrap {
  position: relative;
  flex: 1;
  display: flex;
}

.search-wrap .icon {
  position: absolute;
  left: 0.65rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted-soft);
  pointer-events: none;
}

.search-form input[type="search"] {
  flex: 1;
  padding-left: 2.1rem;
}

.search-form button {
  flex: 0 0 auto;
}

.topbar-link {
  flex: 0 0 auto;
}

.topbar .btn-label {
  display: none;
}

@media (min-width: 640px) {
  .topbar .btn-label { display: inline; }
}

/* ------------------------------------------------------------------ main */

main {
  max-width: 860px;
  margin: 0 auto;
  padding: 1.75rem 1.5rem 3rem;
}

footer {
  text-align: center;
  color: var(--muted-soft);
  padding: 2rem 1rem 3rem;
  font-size: 0.85rem;
}

h1 {
  margin: 0 0 0.3rem;
  font-size: 1.6rem;
  letter-spacing: -0.01em;
}

.page-subtitle {
  color: var(--muted);
  margin: 0 0 1.5rem;
  font-size: 0.95rem;
}

h2 {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  margin: 2.25rem 0 0.9rem;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
h2::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* ------------------------------------------------------------ breadcrumbs */

.breadcrumbs {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-bottom: 1.25rem;
  color: var(--muted);
  font-size: 0.88rem;
}
.breadcrumbs a { color: var(--muted); }
.breadcrumbs a:hover { color: var(--accent-dark); }
.breadcrumbs .icon { color: var(--muted-soft); }
.breadcrumbs span { color: var(--text); font-weight: 600; }

/* ----------------------------------------------------------------- flash */

.flash {
  display: flex;
  align-items: flex-start;
  gap: 0.55rem;
  padding: 0.65rem 0.9rem;
  border-radius: var(--radius-sm);
  border-left: 3px solid transparent;
  margin-bottom: 1.25rem;
  font-size: 0.92rem;
}
.flash .icon { margin-top: 0.15rem; }
.flash-success { background: var(--success-bg); color: var(--success-text); border-left-color: var(--success-text); }
.flash-error { background: var(--error-bg); color: var(--error-text); border-left-color: var(--error-text); }
.flash-info { background: var(--info-bg); color: var(--info-text); border-left-color: var(--info-text); }

/* ----------------------------------------------------------------- empty */

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 0.5rem;
  color: var(--muted);
  padding: 2.5rem 1.5rem;
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-lg);
}
.empty-state .icon { color: var(--muted-soft); }
.empty-state p { margin: 0; }
.empty-state .empty-hint { font-size: 0.85rem; color: var(--muted-soft); }

.meta {
  color: var(--muted);
  font-size: 0.85rem;
}

/* ------------------------------------------------------------- card grid */

.card-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(240px, 100%), 1fr));
  gap: 0.75rem;
  align-items: start;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0.95rem 1rem;
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.15s ease, border-color 0.15s ease, transform 0.15s ease;
}
.card:hover {
  border-color: var(--border-strong);
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
}

.card-body {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  min-width: 0;
}

.card-title {
  font-weight: 600;
  font-size: 1.02rem;
  color: var(--text);
}
.card-title:hover { color: var(--accent-dark); text-decoration: none; }

.card .meta code {
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: bottom;
  background: transparent;
  padding: 0;
  color: var(--muted-soft);
}

/* card overflow menu — pure CSS <details>/<summary>, no JS required */

.card-menu {
  flex: 0 0 auto;
}
.card-menu > summary {
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border-radius: 999px;
  color: var(--muted);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.card-menu > summary::-webkit-details-marker { display: none; }
.card-menu > summary:hover { background: var(--bg); color: var(--text); }
.card-menu[open] > summary { background: var(--border); color: var(--text); }

.card-menu-body {
  margin-top: 0.75rem;
  padding-top: 0.7rem;
  border-top: 1px dashed var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.card-menu-body label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted-soft);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* "+ add" tile — matches the card grid, sits as the last item in the list */

.add-card {
  background: transparent;
  border: 1px dashed var(--border-strong);
  box-shadow: none;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 3.4rem;
}
.add-card:hover { border-color: var(--accent); transform: none; box-shadow: none; }
.add-card > details { width: 100%; }
.add-card summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  color: var(--accent-dark);
  font-weight: 600;
  font-size: 0.92rem;
  padding: 0.2rem 0;
}
.add-card summary::-webkit-details-marker { display: none; }
.add-card summary:hover { text-decoration: none; }
.add-card details[open] summary { color: var(--muted); margin-bottom: 0.6rem; }
.add-card form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.add-card form input[type="text"] { flex: none; width: 100%; }

/* ------------------------------------------------------- barcode scanning */

.barcode-row {
  display: flex;
  gap: 0.4rem;
}
.barcode-row input[type="text"] { flex: 1; min-width: 0; width: auto; }
.barcode-row button { flex: 0 0 auto; }

.scan-hint {
  margin: -0.15rem 0 0;
}
.scan-hint-new { color: var(--accent-dark); font-weight: 600; }
.scan-hint-warn { color: var(--danger); }

.scan-support-note {
  margin: -0.15rem 0 0;
  padding: 0.5rem 0.65rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.scanner-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(20, 16, 12, 0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.1rem;
  padding: 1.5rem;
}
.scanner-overlay video {
  width: 100%;
  max-width: 420px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: var(--radius-lg);
  background: #000;
  box-shadow: var(--shadow-md);
}
.scanner-overlay .scan-cancel {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  border-color: rgba(255, 255, 255, 0.35);
}
.scanner-overlay .scan-cancel:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.5);
}

/* -------------------------------------------------------------- qr block */

.qr-block {
  display: flex;
  gap: 1.1rem;
  align-items: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: 1.1rem;
  margin: 1.1rem 0 1.6rem;
}

.qr-image {
  flex: 0 0 auto;
  width: 120px;
  height: 120px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px;
}

.qr-image svg {
  width: 100%;
  height: 100%;
  display: block;
}

.qr-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.qr-info .meta { word-break: break-all; }

@media (max-width: 520px) {
  .qr-block { flex-direction: column; align-items: stretch; }
  .qr-image { width: 96px; height: 96px; }
}

/* ----------------------------------------------------------- label block */

.label-block {
  display: flex;
  gap: 1.1rem;
  align-items: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: 1.1rem;
  margin: 1.1rem 0 1.6rem;
}

.label-preview {
  flex: 0 0 auto;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px;
}

.label-preview img {
  display: block;
  width: 220px;
  height: auto;
}

.label-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

@media (max-width: 520px) {
  .label-block { flex-direction: column; align-items: stretch; }
  .label-preview { align-self: center; }
  .label-preview img { width: 200px; }
}

/* ----------------------------------------------------------------- forms */

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.4rem;
}

.inline-form {
  display: flex;
  gap: 0.4rem;
  align-items: center;
}

.stack-form {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.5rem;
  flex-wrap: wrap;
}

input[type="text"],
input[type="search"],
input[type="number"] {
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
input[type="text"]:focus,
input[type="search"]:focus,
input[type="number"]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* ----------------------------------------------------------- quantities */

.qty-input {
  width: 4.2rem;
  flex: 0 0 auto;
}

.qty-badge {
  display: inline-flex;
  align-items: center;
  padding: 0.06rem 0.5rem;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-dark);
  font-size: 0.78rem;
  font-weight: 700;
}

.name-qty-row {
  display: flex;
  gap: 0.4rem;
}
.name-qty-row input[type="text"].name-input { flex: 1; min-width: 0; width: auto; }
.name-qty-row input[type="number"].qty-input { flex: 0 0 auto; width: 4.2rem; }

.section-note {
  color: var(--muted-soft);
  font-size: 0.85rem;
  margin: -0.3rem 0 0.8rem;
}

/* ---------------------------------------------------------------buttons */

button, .btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 0.9rem;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #fff;
  font-size: 0.9rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
button:hover, .btn:hover { background: var(--accent-dark); text-decoration: none; }

button.secondary, .btn.secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border-strong);
}
button.secondary:hover, .btn.secondary:hover { background: var(--bg); border-color: var(--border-strong); }

button.danger, .btn.danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger-soft);
}
button.danger:hover, .btn.danger:hover { background: var(--danger); color: #fff; border-color: var(--danger); }

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border-color: var(--border);
}
.btn-ghost:hover { background: var(--surface); color: var(--text); border-color: var(--border-strong); }

/* (The former 520px phone block lived here; it has been folded into the
   single 639px block at the end of this file so the layout breakpoint and
   the topbar's label breakpoint can't drift apart again.) */

/* --------------------------------------------------------- accounts/login */

/* Menus opened from inside the topbar (the "My stuff" context switcher and
   the account menu) float as an actual dropdown instead of expanding the
   surrounding element in place — unlike the same .card-menu pattern used on
   item cards, where growing the card downward reads fine, growing the whole
   sticky header would push everything else in the bar around. */
.topbar .card-menu {
  position: relative;
}
.topbar .card-menu-body {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 0.5rem;
  padding: 0.6rem;
  border: 1px solid var(--border);
  border-top: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  box-shadow: var(--shadow-md);
  z-index: 20;
  max-width: 16rem;
}
.topbar .card-menu-body button,
.topbar .card-menu-body a.btn {
  white-space: nowrap;
}

.context-switcher {
  flex: 0 0 auto;
}
.context-switcher > summary {
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 0.75rem;
  border-radius: var(--radius-sm);
  color: var(--muted);
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.15s ease, color 0.15s ease;
}
.context-switcher > summary::-webkit-details-marker { display: none; }
.context-switcher > summary:hover { background: var(--bg); color: var(--text); }
.context-switcher[open] > summary { background: var(--border); color: var(--text); }
.context-switcher .card-menu-body { min-width: 12rem; left: 0; right: auto; }
.context-switcher .card-menu-body form.inline-form { justify-content: stretch; }
.context-switcher .card-menu-body button { width: 100%; justify-content: flex-start; }

/* The centered card that /setup, /login, and the public /view/{token}
   "not found" state render inside — same idea as .card but sized and
   positioned for a standalone page rather than a grid item. */
.auth-card {
  max-width: 360px;
  margin: 2.5rem auto 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.75rem 1.75rem 1.9rem;
  box-shadow: var(--shadow-md);
}
.auth-card h1 {
  text-align: center;
  margin-bottom: 0.4rem;
}
.auth-card .page-subtitle {
  text-align: center;
}
.auth-card .flash {
  margin-bottom: 1rem;
}

/* A vertically-stacked form — each label/input pair on its own line — used
   by the login/setup/account/people forms instead of the inline row layout
   .stack-form uses for short add-a-thing forms. */
.stack-form-v {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  margin-top: 0.5rem;
}
.stack-form-v label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}
.stack-form-v label .meta {
  font-weight: 400;
  font-size: 0.78rem;
}
.stack-form-v input[type="text"],
.stack-form-v input[type="password"],
.stack-form-v select {
  font-weight: 400;
}
.stack-form-v button {
  margin-top: 0.2rem;
  justify-content: center;
}

/* The "add items from a photo" tile on box/place pages — an .add-card with
   a bit of extra room for the upload form and the OCR-review list that
   replaces it once a photo's been scanned. */
.photo-scan-card {
  min-height: auto;
  display: block;
  text-align: left;
}
.photo-scan-card form {
  align-items: flex-start;
}
.photo-scan-card input[type="file"] {
  font-size: 0.85rem;
  color: var(--muted);
}
.scan-support-note {
  margin: 0;
}

/* The OCR "review before adding" list — each row needs room for a name
   input, a quantity input, and two buttons, which is far too wide for the
   auto-fill card grid .card-list uses elsewhere, so this is a plain
   single-column stack instead. */
.scan-review-list {
  list-style: none;
  margin: 0 0 0.75rem;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}
.scan-review-list .card-head {
  flex-wrap: wrap;
}
.scan-review-list .inline-form {
  flex-wrap: nowrap;
}
.scan-review-list .name-input {
  flex: 1;
  min-width: 8rem;
  width: auto;
}
@media (max-width: 520px) {
  .scan-review-list .card-head { flex-direction: column; align-items: stretch; }
  .scan-review-list .inline-form { flex-wrap: wrap; }
}

/* ------------------------------------------------- JSON / photo-LLM import */

/* The import tile carries a lot more than a file picker — a numbered
   how-to, a copyable prompt, and the schema — so it drops the centred
   .add-card layout for a plain left-aligned block. */
.import-card {
  min-height: auto;
  display: block;
  text-align: left;
}
.import-steps {
  margin: 0 0 0.75rem;
  padding-left: 1.25rem;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.5;
}
.import-steps li + li { margin-top: 0.2rem; }

.import-prompt {
  margin-bottom: 0.75rem;
}
.import-prompt-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 0.4rem;
}
.import-prompt-title {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-weight: 600;
  font-size: 0.9rem;
}
.import-prompt-text {
  width: 100%;
  box-sizing: border-box;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.78rem;
  line-height: 1.45;
  resize: vertical;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.7rem;
}
.copy-status {
  margin: 0.3rem 0 0;
  min-height: 1.1em;
}
.copy-status.is-ok { color: var(--accent-dark); }

.import-schema {
  margin-bottom: 0.85rem;
}
/* The nested schema disclosure sits inside an .add-card, whose summary rule
   centres its text and hides the marker — neither of which suits a
   left-aligned sub-section, so both are put back here. */
.import-card .import-schema > summary {
  list-style: revert;
  display: list-item;
  justify-content: flex-start;
  text-align: left;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--accent-dark);
  padding: 0.2rem 0;
  margin: 0;
}
.import-card .import-schema > summary::-webkit-details-marker { display: list-item; }
.import-schema-label {
  margin: 0.6rem 0 0.25rem;
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.import-code {
  margin: 0;
  max-height: 14rem;
  overflow: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.7rem;
  font-size: 0.75rem;
  line-height: 1.45;
}
.import-code-tall { max-height: 16rem; }
.import-code code { background: none; padding: 0; font-size: inherit; }

/* Beats `.add-card form { flex-direction: column }` — the picker and the
   Import button belong on one line here. */
.import-card .import-form {
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-start;
  gap: 0.5rem;
}
.import-card .import-form input[type="file"] {
  font-size: 0.85rem;
  color: var(--muted);
  flex: 1 1 12rem;
  min-width: 0;
  width: auto;
}
.import-card .import-form button { flex: 0 0 auto; }
@media (max-width: 520px) {
  .import-prompt-head { align-items: flex-start; }
  .import-card .import-form { flex-direction: column; align-items: stretch; }
}

/* =================================================================
   MOBILE / RESPONSIVE FIXES
   =================================================================
   Appended last on purpose: everything here is a correction to a rule
   defined earlier in this file, so it relies on source order rather
   than on inflating selector specificity.
   ================================================================= */

/* ------------------------------------------------------- <select> base

   Selects appear in the item "Move" form and in the people/share forms
   but never had any base styling, so they rendered as raw platform
   controls and — because Chrome sizes a <select> to its widest <option>
   — a long box name would push the whole card wider than the screen.
   These match the text-input rules and, crucially, let the control
   shrink inside its flex parent. */

select {
  max-width: 100%;
  min-width: 0;
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.95rem;
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* Text inputs had no max-width either, so a wide `size`/placeholder or a
   long value could overflow a narrow card. */
input[type="text"],
input[type="search"],
input[type="number"],
input[type="password"],
input[type="file"],
textarea {
  max-width: 100%;
}

/* ------------------------------------------------------- search shrinking

   An <input> has an intrinsic min-content width (roughly 20 characters),
   and as a flex item its default `min-width: auto` honours it — so on a
   320px screen the search field held its ground and shoved the Search
   button off the edge instead. */

.search-form,
.search-wrap { min-width: 0; }
.search-form input[type="search"] { min-width: 0; }

/* ------------------------------------------------ open card-menu layout

   `details.card-menu` is a flex item of `.card-head`, and its open state
   contains a rename form (a text input, a number input, a Move select and
   three buttons). Because the item couldn't wrap, opening the menu let
   that form's max-content width claim the whole row and squeezed the item
   title down to roughly one character per line. Letting the open menu take
   a full-width row of its own fixes it at every breakpoint. */

/* Wrapping is enabled only while a menu is actually open. Left on
   unconditionally it pushed the closed "\2026" button onto a second line
   in every card (a long title's max-content base size fills the first
   line), which made every card taller for no reason. */
.card-head:has(> .card-menu[open]) { flex-wrap: wrap; }
.card-head > .card-body { flex: 1 1 auto; min-width: 0; }
.card-head > .card-menu { flex: 0 0 auto; margin-left: auto; }
.card-head > .card-menu[open] { flex: 1 1 100%; min-width: 0; }
.card-head > .card-menu[open] > summary { margin-left: auto; }

/* `min-width: 0` has to be repeated the whole way down. A flex item's
   default `min-width: auto` refuses to shrink below its min-content width,
   and the Move <select>'s min-content is the width of its longest option
   ("(loose in Garden shed behind the greenhouse)") — so without these the
   longest place name in the account decides how wide every card is. */
.card-menu-body { min-width: 0; }
.card-menu-body .inline-form { min-width: 0; }
/* Cards in the auto-fill grid can be as narrow as 240px, which is not
   enough for a select and a button side by side — let the row wrap rather
   than overflow the card. Scoped to the row layout on purpose: the phone
   block below switches .inline-form to `flex-direction: column`, where
   `flex-wrap: wrap` would wrap into extra *columns* and spill sideways. */
@media (min-width: 640px) {
  .card-menu-body .inline-form { flex-wrap: wrap; }
}
.card-menu-body .inline-form > * { min-width: 0; }
/* No `width: 100%` here — on the tablet-and-up row layout that resolves as
   a 100% flex-basis and pushes the adjacent Save/Move button off the card.
   The mobile column layout stretches these to full width on its own. */
.card-menu-body .inline-form input[type="text"],
.card-menu-body .inline-form select { flex: 1 1 8rem; min-width: 0; }
.card-menu-body .inline-form button { flex: 0 0 auto; }

/* The "\00d7 N" quantity pill is a direct child of .card-body, which is a
   column flex container — so the default `align-items: stretch` blew the
   pill out to the full card width instead of hugging its label. */
.qty-badge { align-self: flex-start; }

/* ------------------------------------------------- long-string overflow

   Item names, box slugs and barcodes are user-supplied and frequently
   arrive as one unbroken string (a scanned EAN, a URL, "PARAFUSO-M6X40-INOX-A2").
   Without this, the card grid just overflows horizontally on a phone. */

.card-body,
.card-title,
.card-head,
.breadcrumbs,
.flash,
.empty-state,
.meta {
  overflow-wrap: break-word;
}
.card-title { min-width: 0; }
/* Slugs, URLs and barcodes have no break opportunities at all, so they get
   the stronger `anywhere`. */
code { overflow-wrap: anywhere; }

/* `.qr-info .meta { word-break: break-all }` applied to the whole panel,
   including the sentence above the URL — which is why that paragraph broke
   as "a read-only pag / e". Only the URL itself needs break-all. */
.qr-info .meta { word-break: normal; }
.qr-info .meta code { word-break: break-all; }

/* The scanner overlay is `position: fixed; inset: 0`, which on mobile
   resolves against the *large* viewport — so the Cancel button ends up
   underneath the browser's bottom toolbar. 100dvh tracks the visible
   area instead, and the safe-area padding keeps it clear of the notch
   and the home indicator. */
.scanner-overlay {
  height: 100dvh;
  overflow-y: auto;
  padding-top: max(1.5rem, env(safe-area-inset-top));
  padding-bottom: max(1.5rem, env(safe-area-inset-bottom));
}

/* Honour the OS "reduce motion" setting — smooth scrolling and the card
   hover lift are both motion the user may have opted out of. */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
  .card:hover { transform: none; }
}

/* ================================================================
   Phone layout.

   The single breakpoint used to be 520px, which left 521–639px in a
   half-broken middle state: the topbar hid its button labels (that
   switch is at 640px) while still laying everything out in one row.
   Both now flip at the same place.
   ================================================================ */

@media (max-width: 639px) {

  /* --- topbar ------------------------------------------------------
     Previously `flex-direction: column`, which stacked brand, account
     switcher, Barcodes, search and the account menu as five separate
     full-width rows — a sticky header eating roughly a third of a
     phone screen. Instead: keep the icon-sized controls on one row and
     let only the search field wrap onto a second row. */
  .topbar {
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5rem;
    padding: 0.6rem 0.9rem;
    padding-top: max(0.6rem, env(safe-area-inset-top));
  }
  .topbar .brand { font-size: 1.05rem; margin-right: auto; }
  .topbar .search-form {
    order: 10;
    flex: 1 1 100%;
    max-width: none;
  }

  /* Dropdowns are anchored to the right edge of their trigger; near the
     screen edge that used to run off-canvas. */
  .topbar .card-menu-body {
    max-width: min(16rem, calc(100vw - 1.8rem));
  }
  .context-switcher .card-menu-body {
    left: auto;
    right: 0;
    min-width: 11rem;
  }
  .context-switcher > summary { padding: 0.45rem 0.5rem; }

  /* With the labels hidden, the Barcodes link sat in a bordered box next to
     two borderless icon triggers. Same treatment for all three. */
  .topbar .topbar-link.btn-ghost {
    border-color: transparent;
    background: transparent;
    padding: 0.45rem 0.5rem;
  }
  .topbar .topbar-link.btn-ghost:hover { background: var(--bg); }

  /* --- page gutters ------------------------------------------------
     1.5rem each side costs ~19% of a 320px screen. */
  main { padding: 1.25rem 1rem 2.5rem; }
  footer { padding: 1.5rem 1rem max(2rem, env(safe-area-inset-bottom)); }
  h1 { font-size: 1.35rem; }
  h2 { margin-top: 1.75rem; }

  /* --- tap targets -------------------------------------------------
     Buttons were ~34px tall and the card "⋯" menu 32px square, both
     under the 44px minimum every mobile accessibility guideline uses. */
  button, .btn {
    min-height: 44px;
    padding: 0.6rem 0.9rem;
  }
  .card-menu > summary,
  .add-card summary,
  .context-switcher > summary {
    min-height: 44px;
  }
  .card-menu > summary {
    width: 44px;
    height: 44px;
  }

  /* --- form controls -----------------------------------------------
     iOS Safari force-zooms the page whenever a focused field's text is
     under 16px, and every input here was 0.95rem (~15px). This is the
     fix for "the page jumps and stays zoomed after I tap the search
     box". 16px exactly, not rem, so a changed root size can't undo it. */
  input[type="text"],
  input[type="search"],
  input[type="number"],
  input[type="password"],
  select,
  textarea {
    font-size: 16px;
  }
  .import-prompt-text { font-size: 16px; }

  /* Stacked layouts: these already existed at 520px, restated here so
     they move with the new breakpoint. */
  .row-actions,
  .inline-form { flex-direction: column; align-items: stretch; }
  .card-list { grid-template-columns: minmax(0, 1fr); }

  /* The card head used to stack on mobile, which dropped the overflow-menu
     button onto its own line below the metadata and left a dead gap in
     every card. Now that long titles and slugs wrap or ellipsize properly
     it fits as a row again: title left, 44px menu button right. */
  .card-head { flex-direction: row; align-items: flex-start; gap: 0.5rem; }

  /* Full-width buttons inside a stacked form read as buttons, not as
     labels floating left. */
  .row-actions > button,
  .row-actions > .btn,
  .inline-form > button,
  .card-menu-body > .btn,
  .card-menu-body form > button { justify-content: center; }

  /* Inside the (now column-stacked) card menu, the 4.2rem quantity field
     looked like a stray fragment next to the full-width name field. */
  .card-menu-body .inline-form .qty-input { width: 100%; }

  /* Barcode + quantity rows stay side-by-side (they're deliberately
     narrow) but must be allowed to shrink rather than overflow. */
  .barcode-row,
  .name-qty-row { flex-wrap: wrap; }
  .barcode-row input[type="text"],
  .name-qty-row input[type="text"].name-input { flex: 1 1 8rem; }
  .barcode-row button { flex: 0 0 auto; }

  /* The photo-scan review rows put a name field, a quantity field and
     two buttons on one line — impossible under ~420px. */
  .scan-review-list .inline-form { flex-direction: row; flex-wrap: wrap; }
  .scan-review-list .name-input { flex: 1 1 100%; }

  .photo-scan-card input[type="file"],
  .import-card .import-form input[type="file"] { max-width: 100%; }

  .import-code { font-size: 0.72rem; }
  .qr-block, .label-block { padding: 0.9rem; gap: 0.9rem; }
}

/* Very narrow phones (320–360px): the QR/label side-by-side layouts and
   the two-up input rows give up and stack completely. */
@media (max-width: 380px) {
  main { padding-left: 0.8rem; padding-right: 0.8rem; }
  .name-qty-row,
  .barcode-row { flex-direction: column; align-items: stretch; }
  .name-qty-row input[type="number"].qty-input,
  .qty-input { width: 100%; }
  .topbar .brand span:not(.brand-mark) { font-size: 0.95rem; }
}

/* Landscape phones: the sticky topbar plus its wrapped search row leaves
   very little room, so the header stops sticking. */
@media (max-width: 900px) and (max-height: 480px) {
  .topbar { position: static; }
}
