:root {
  --bg: #f4efe8;
  --surface: #fffdf9;
  --surface-2: #ece5db;
  --ink: #2a2522;
  --muted: #8a8079;
  --line: #ddd3c6;
  --accent: #8b5e3c;
  --accent-ink: #fffaf4;
  --rail: #b5a491;
  --shelf: #d8cbbb;
  --danger: #b4483c;
  --shadow: 0 1px 2px rgba(60, 40, 20, .06), 0 8px 24px rgba(60, 40, 20, .08);
  --item-shadow: drop-shadow(0 6px 10px rgba(50, 35, 20, .18)) drop-shadow(0 1px 1px rgba(50, 35, 20, .15));
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --tabs-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1917;
    --surface: #25221f;
    --surface-2: #2f2b27;
    --ink: #f1ebe3;
    --muted: #a39a90;
    --line: #3a3530;
    --accent: #c99468;
    --accent-ink: #1b1917;
    --rail: #6d6155;
    --shelf: #3e3731;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.3);
    --item-shadow: drop-shadow(0 6px 10px rgba(0,0,0,.45));
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font: 15px/1.45 var(--sans); overscroll-behavior-y: none; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; letter-spacing: .01em; margin: 0; }
.muted { color: var(--muted); }
.err { color: var(--danger); min-height: 1.2em; margin: 8px 0 0; font-size: 14px; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

/* Buttons */
.btn { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 11px 18px; font-weight: 600; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.small { padding: 7px 14px; font-size: 14px; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; }
.icon-btn { border: 0; background: transparent; padding: 8px; border-radius: 999px; display: inline-grid; place-items: center; }
.icon-btn:active { background: var(--surface-2); }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 8px 14px; font-size: 14px; white-space: nowrap; }
.chip.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card { width: 100%; max-width: 340px; text-align: center; display: grid; gap: 12px; }
.login-card h1 { font-size: 34px; }
.login-card p { margin: 0 0 8px; }
.login-card input { font: inherit; padding: 13px 16px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); }
.brand-mark { width: 64px; height: 64px; margin: 0 auto 4px; border-radius: 18px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font: 600 34px var(--serif); }

/* Layout */
#app { min-height: 100%; }
.top { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top, 0px) + 12px) 18px 10px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); }
.top h1 { font-size: 26px; }
.top-actions { display: flex; align-items: center; gap: 6px; }
main { padding-bottom: calc(var(--tabs-h) + var(--safe-b) + 16px); }
.view { padding: 0 0 8px; }

.tabs { position: fixed; left: 0; right: 0; bottom: 0; z-index: 6; display: flex; justify-content: space-around;
  height: calc(var(--tabs-h) + var(--safe-b)); padding-bottom: var(--safe-b); background: var(--surface); border-top: 1px solid var(--line); }
.tabs button { flex: 1; border: 0; background: none; display: grid; justify-items: center; align-content: center; gap: 2px; color: var(--muted); font-size: 12px; }
.tabs button.active { color: var(--accent); }

/* Empty states */
.empty { text-align: center; padding: 48px 28px; max-width: 420px; margin: 0 auto; display: grid; gap: 12px; justify-items: center; }
.empty h2 { font-size: 22px; }
.empty p { margin: 0; }
.empty .tip { font-size: 13px; margin-top: 6px; }
.empty-art { width: 120px; height: 90px; border-top: 3px solid var(--rail); border-radius: 2px; position: relative; margin-bottom: 8px; }
.empty-art::before, .empty-art::after { content: ""; position: absolute; top: 6px; width: 34px; height: 46px; border-radius: 4px 4px 8px 8px; background: var(--surface-2); }
.empty-art::before { left: 22px; } .empty-art::after { right: 22px; height: 56px; }

/* Closet: rails and shelves */
.cat { margin: 6px 0 22px; }
.cat-head { display: flex; align-items: baseline; justify-content: space-between; padding: 0 18px 6px; }
.cat-head h2 { font-size: 19px; }
.cat-head .count { color: var(--muted); font-size: 13px; }
.rail { position: relative; display: flex; gap: 6px; overflow-x: auto; scroll-snap-type: x proximity; padding: 0 18px 14px;
  scrollbar-width: none; }
.rail::-webkit-scrollbar { display: none; }
.rail.hanging { padding-top: 18px; }
.rail.hanging::before { content: ""; position: absolute; left: 0; right: 0; top: 8px; height: 4px; border-radius: 2px;
  background: linear-gradient(var(--rail), color-mix(in srgb, var(--rail) 70%, #000)); min-width: 100%; }
.rail.shelf { padding-bottom: 20px; }
.rail.shelf::after { content: ""; position: absolute; left: 0; right: 0; bottom: 8px; height: 8px; border-radius: 2px;
  background: var(--shelf); box-shadow: 0 3px 6px rgba(0,0,0,.08); }

.piece { flex: 0 0 auto; scroll-snap-align: start; position: relative; border: 0; background: none; padding: 0; width: 128px; }
.piece .frame { height: 168px; display: grid; place-items: end center; padding: 10px 4px 0; }
.rail.hanging .piece .frame { place-items: start center; padding-top: 14px; }
.rail.shelf .piece .frame { height: 150px; }
.cat[data-slot="shoes"] .piece { width: 112px; }
.cat[data-slot="shoes"] .piece .frame { height: 96px; }
.piece img { max-width: 100%; max-height: 100%; object-fit: contain; filter: var(--item-shadow); transition: transform .15s; }
.piece:active img { transform: scale(.96); }
.rail.hanging .piece::before { /* hanger hook */
  content: ""; position: absolute; top: -12px; left: 50%; width: 12px; height: 16px; margin-left: -6px;
  border: 2px solid var(--rail); border-bottom: 0; border-radius: 8px 8px 0 0; }
.piece .label { display: block; font-size: 12px; color: var(--muted); text-align: center; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 4px; }
.piece .fav { position: absolute; top: 4px; right: 6px; color: var(--accent); font-size: 14px; }
.piece.pending .frame { place-items: center; }
.pending-card { width: 92px; height: 110px; border-radius: 10px; background: linear-gradient(100deg, var(--surface-2) 30%, var(--surface) 50%, var(--surface-2) 70%);
  background-size: 300% 100%; animation: shimmer 1.4s infinite linear; display: grid; place-items: center; font-size: 12px; color: var(--muted); text-align: center; padding: 6px; }
.pending-card.failed { animation: none; background: var(--surface-2); color: var(--danger); }
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: 0 0; } }

/* Mix & match */
#view-mix { display: flex; flex-direction: column; height: calc(100dvh - var(--tabs-h) - var(--safe-b) - 58px - env(safe-area-inset-top, 0px)); padding: 0; }
.mixer { flex: 1; display: flex; flex-direction: column; min-height: 0; padding: 4px 0; }
.slot { position: relative; min-height: 0; display: flex; }
.slot[data-slot="layer"] { flex: 26; }
.slot[data-slot="top"] { flex: 32; }
.slot[data-slot="bottom"] { flex: 40; }
.slot[data-slot="shoes"] { flex: 15; }
.carousel { flex: 1; display: flex; overflow-x: auto; scroll-snap-type: x mandatory; scrollbar-width: none; overscroll-behavior-x: contain; }
.carousel::-webkit-scrollbar { display: none; }
.slide { flex: 0 0 100%; scroll-snap-align: center; display: grid; place-items: center; padding: 4px 64px; min-height: 0; }
.slide img { width: 100%; height: 100%; object-fit: contain; filter: var(--item-shadow); }
.slide { overflow: hidden; }
.slide.none { color: var(--muted); font-size: 13px; }
.slot .lock, .slot .meta { position: absolute; z-index: 2; }
.slot .lock { left: 10px; top: 50%; transform: translateY(-50%); width: 40px; height: 40px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); display: grid; place-items: center; color: var(--muted); box-shadow: var(--shadow); }
.slot .lock svg { width: 18px; height: 18px; }
.slot .lock.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.slot .meta { right: 14px; bottom: 6px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.slot .meta b { font-weight: 600; color: var(--ink); }
.mix-bar { display: flex; gap: 8px; align-items: center; justify-content: center; padding: 8px 14px 10px; }
.mix-bar .btn { padding: 10px 16px; }

/* Outfits */
.outfit-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; padding: 4px 16px; }
.outfit-card { border: 0; padding: 10px; background: var(--surface); border-radius: 18px; box-shadow: var(--shadow); text-align: left; display: grid; gap: 6px; }
.flatlay { aspect-ratio: 4/5; display: grid; grid-template-rows: 36fr 44fr 16fr; gap: 2px; background: var(--surface-2); border-radius: 12px; padding: 8px; }
.flatlay .row { display: flex; gap: 4px; justify-content: center; min-height: 0; }
.flatlay img { max-width: 100%; max-height: 100%; object-fit: contain; filter: var(--item-shadow); min-width: 0; }
.flatlay .row.two img { max-width: 50%; }
.outfit-card .name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.outfit-card .sub { font-size: 12px; color: var(--muted); }

/* Sheet */
.sheet { position: fixed; inset: 0; z-index: 20; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(20, 14, 8, .4); animation: fade .2s; }
.sheet-panel { position: absolute; left: 0; right: 0; bottom: 0; max-height: 92dvh; overflow-y: auto; background: var(--bg);
  border-radius: 22px 22px 0 0; padding: 8px 18px calc(18px + var(--safe-b)); animation: up .25s cubic-bezier(.2,.8,.2,1); max-width: 640px; margin: 0 auto; }
.sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 12px; }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet h2 { font-size: 22px; margin-bottom: 4px; }
.sheet .field { display: grid; gap: 6px; margin: 14px 0; }
.sheet .field > span { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.sheet input[type=text], .sheet textarea { font: inherit; padding: 11px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); width: 100%; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.row-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.row-actions .btn { flex: 1 1 auto; }
.preview { background: var(--surface-2); border-radius: 18px; height: 42dvh; display: grid; place-items: center; padding: 18px;
  background-image: linear-gradient(45deg, rgba(0,0,0,.025) 25%, transparent 25%, transparent 75%, rgba(0,0,0,.025) 75%), linear-gradient(45deg, rgba(0,0,0,.025) 25%, transparent 25%, transparent 75%, rgba(0,0,0,.025) 75%);
  background-size: 22px 22px; background-position: 0 0, 11px 11px; }
.preview img { width: 100%; height: 100%; object-fit: contain; filter: var(--item-shadow); }
.preview { overflow: hidden; }
.picks { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 8px; margin-top: 10px; }
.picks img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 10px; }
.drop { border: 2px dashed var(--line); border-radius: 16px; padding: 22px; text-align: center; display: grid; gap: 6px; justify-items: center; background: var(--surface); cursor: pointer; }
.drop input { display: none; }
.progress { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; margin-top: 12px; }
.progress > div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.small-note { font-size: 13px; color: var(--muted); margin: 6px 0 0; }

.toast { position: fixed; left: 50%; bottom: calc(var(--tabs-h) + var(--safe-b) + 14px); transform: translateX(-50%); z-index: 30;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px; box-shadow: var(--shadow); animation: fade .2s; max-width: 90vw; }

@media (min-width: 760px) {
  .piece { width: 150px; } .piece .frame { height: 200px; }
  .slide { padding: 4px 25%; }
  main { max-width: 1100px; margin: 0 auto; }
}

/* fix: keep images inside their rows (no cropping of wide items) */
.slide, .preview { grid-template: minmax(0, 1fr) / minmax(0, 1fr); }
.slide img, .preview img { min-width: 0; min-height: 0; }

/* size */
.piece .size-badge { position: absolute; top: 4px; left: 6px; z-index: 1; font-size: 11px; font-weight: 600; padding: 2px 7px;
  border-radius: 999px; background: var(--surface); color: var(--ink); border: 1px solid var(--line); }
.rail.hanging .piece .size-badge { top: 10px; }
.size-other { font: inherit; font-size: 14px; width: 90px; padding: 7px 12px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); }
