:root {
  --bg: #f5f5f4;
  --surface: #ffffff;
  --text: #1c1917;
  --muted: #78716c;
  --border: #e7e5e4;
  --primary: #1f2937;
  --primary-text: #ffffff;
  --danger: #b91c1c;
  --cutout-bg: #efeeec;
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #18181b;
    --surface: #27272a;
    --text: #f4f4f5;
    --muted: #a1a1aa;
    --border: #3f3f46;
    --primary: #f4f4f5;
    --primary-text: #18181b;
    --danger: #f87171;
    --cutout-bg: #e4e4e7;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
}

a { color: inherit; }

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px;
}

h1 { font-size: 1.5rem; margin: 0 0 16px; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand { font-weight: 700; font-size: 1.15rem; text-decoration: none; }
.btn-add { white-space: nowrap; }
/* Small screens: the logo and the add button shrink to their icon so the top bar stays on one line. */
@media (max-width: 520px) {
  .brand { font-size: 1.6rem; }
  .btn-add { min-width: 42px; padding: 8px 0; font-size: 1.4rem; line-height: 1; }
  .brand .label, .btn-add .label { display: none; }
}
.topbar-actions { display: flex; align-items: center; gap: 4px; }
.topbar-actions form { margin: 0; }

.login { max-width: 420px; margin: 32px auto; }
.login code { font-size: 0.9rem; }
.login .hint { font-weight: 400; }
.switch { text-align: center; color: var(--muted); }

.count-badge {
  display: inline-block;
  min-width: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  font-size: 0.8rem;
  line-height: 20px;
  text-align: center;
}

h2 { font-size: 1.1rem; margin: 24px 0 8px; }
.accounts { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; max-width: 640px; }
.account {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.account form { margin: 0; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 42px;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.btn-primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-block { width: 100%; }

input, select, textarea {
  width: 100%;
  min-height: 42px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* prevents auto-zoom on iPhone */
}

/* ----- Filters ----- */

.filters {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-bottom: 12px;
}
@media (min-width: 720px) {
  .filters { grid-template-columns: 2fr repeat(4, 1fr) auto auto; }
}

.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 6px;
  margin-bottom: 8px;
  scrollbar-width: thin;
}
.chip {
  flex: none;
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  text-decoration: none;
  font-size: 0.9rem;
  white-space: nowrap;
}
.chip.active { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }

.count { color: var(--muted); font-size: 0.9rem; margin: 4px 0 12px; }

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

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
}
@media (min-width: 720px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
}

.card {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  text-decoration: none;
}
.card img, .card .noimg {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  display: block;
}
.card-body { padding: 8px 10px 10px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.card-body strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meta { color: var(--muted); font-size: 0.85rem; }

.card { position: relative; }
.card-cutout img {
  object-fit: contain;
  padding: 8px;
  background: var(--cutout-bg);
}
.badge {
  position: absolute;
  top: 6px;
  right: 6px;
  padding: 2px 6px;
  border-radius: 999px;
  background: var(--surface);
  font-size: 0.85rem;
}

.noimg {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3rem;
  background: var(--border);
}
.noimg.big { aspect-ratio: 1; font-size: 6rem; border-radius: var(--radius); }

.empty { text-align: center; padding: 48px 16px; color: var(--muted); }

/* ----- Item page ----- */

.detail { display: grid; gap: 20px; }
@media (min-width: 720px) {
  .detail { grid-template-columns: 1fr 1fr; align-items: start; }
}
.detail img { width: 100%; border-radius: var(--radius); display: block; }
.detail-photo { display: grid; gap: 8px; }
.cutout-frame {
  display: block;
  padding: 16px;
  border-radius: var(--radius);
  background: var(--cutout-bg);
}
.cutout-frame img { max-height: 70vh; object-fit: contain; }
.status {
  margin: 0;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.status-error { color: var(--danger); border-color: var(--danger); }
.detail dl { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0 0 16px; }
.detail dt { color: var(--muted); }
.detail dd { margin: 0; }
.notes { white-space: pre-wrap; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.actions form { margin: 0; }

/* ----- Form ----- */

.item-form { display: grid; gap: 14px; max-width: 560px; }
.item-form label { display: grid; gap: 4px; font-weight: 600; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.photo-zone {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.photo-zone img { width: 100%; max-height: 360px; object-fit: contain; border-radius: 8px; }
.photo-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.photo-buttons label { font-weight: 600; text-align: center; }
.hint { margin: 0; color: var(--muted); font-size: 0.85rem; }

.error {
  padding: 10px 14px;
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  color: var(--danger);
}

/* ----- Tabs ----- */

.tabs {
  display: flex;
  justify-content: center;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.tab {
  flex: 1;
  max-width: 220px;
  padding: 10px 16px;
  text-align: center;
  font-weight: 600;
  text-decoration: none;
  color: var(--muted);
  border-bottom: 3px solid transparent;
}
.tab.active { color: var(--text); border-bottom-color: var(--primary); }
@media (max-width: 520px) {
  .tab { padding: 10px 4px; font-size: 0.9rem; }
}

/* ----- Outfits ----- */

.filters-outfits { grid-template-columns: 1fr; }
@media (min-width: 720px) {
  .filters-outfits { grid-template-columns: 2fr 1fr auto auto; }
}
.card-outfit img, .card-outfit .noimg { aspect-ratio: 3 / 4; }
.card-outfit .notes-preview {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Outfit without a photo: its first pieces side by side. */
.collage {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  aspect-ratio: 3 / 4;
  overflow: hidden;
  background: var(--cutout-bg);
}
.card-outfit .collage img { aspect-ratio: auto; height: 100%; min-height: 0; object-fit: contain; padding: 6px; }

/* Outfit made of my pieces, laid out top to bottom like on a bed. */
.flatlay {
  display: grid;
  gap: 12px;
  padding: 16px;
  border-radius: var(--radius);
  background: var(--cutout-bg);
}
.flatlay-row { display: flex; justify-content: center; gap: 12px; }
.flatlay-row a { flex: 0 1 220px; min-width: 0; }
.detail .flatlay img { max-height: 32vh; object-fit: contain; border-radius: 0; }
.flatlay .noimg { aspect-ratio: 1; border-radius: var(--radius); }

.pieces {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}
.piece, .pick {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  text-decoration: none;
  font-size: 0.85rem;
}
.piece img, .pick img, .piece .noimg, .pick .noimg {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  font-size: 2rem;
}
.piece.card-cutout img, .pick.card-cutout img { object-fit: contain; padding: 6px; background: var(--cutout-bg); }
.piece > span:last-child, .pick > span:last-child {
  padding: 4px 6px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ----- Piece picker (outfit form) ----- */

.picker summary { font-weight: 600; cursor: pointer; padding: 6px 0; }
.picker .chips { margin-top: 8px; }
button.chip { font: inherit; font-size: 0.9rem; color: inherit; cursor: pointer; }
.pick-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 8px;
}
.item-form .pick { position: relative; display: flex; font-weight: 400; gap: 0; cursor: pointer; }
.pick input {
  position: absolute;
  top: 6px;
  left: 6px;
  width: 22px;
  min-height: 0;
  height: 22px;
  margin: 0;
  accent-color: var(--primary);
}
.pick:has(input:checked) { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary); }

/* ----- Tags ----- */

.tag-list { display: flex; flex-wrap: wrap; gap: 6px; }
.chip-small { padding: 2px 10px; font-size: 0.85rem; }
.tags-field { display: grid; gap: 8px; margin: 0; padding: 0; border: 0; }
.tags-field legend { font-weight: 600; padding: 0; margin-bottom: 4px; }
.item-form .tag-check { display: inline-flex; cursor: pointer; font-weight: 400; }
.tag-check input { position: absolute; opacity: 0; width: 1px; min-height: 0; height: 1px; }
.tag-check span {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-size: 0.9rem;
}
.tag-check input:checked + span { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.tag-check input:focus-visible + span { outline: 2px solid var(--primary); outline-offset: 2px; }

.worn-actions { align-items: center; margin-bottom: 16px; }
.detail-info h2 { margin-top: 20px; }
