:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --text: #1f2420;
  --muted: #6b7169;
  --line: #e3e1db;
  --accent: #3a5a44;
  --accent-text: #ffffff;
  --badge-bg: #fbe8c8;
  --badge-text: #7a5312;
  --error: #b23b3b;
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181a;
    --surface: #202325;
    --text: #e9e9e6;
    --muted: #9aa09a;
    --line: #33373a;
    --accent: #6f9d7d;
    --accent-text: #10130f;
    --badge-bg: #4a3a1c;
    --badge-text: #f2d9ac;
    --error: #e2807f;
  }
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 720px;
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) 14px calc(28px + env(safe-area-inset-bottom));
}

/* ---- top bar ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 2px 16px;
}
.topbar h1 {
  font-size: 1.5rem;
  margin: 0;
  letter-spacing: -0.01em;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 9px 18px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  background: var(--surface);
  color: var(--text);
}
.btn-primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: transparent;
}
.btn-danger {
  color: var(--error);
  border-color: var(--error);
}
.btn:active {
  transform: translateY(1px);
}
a.btn {
  text-decoration: none;
}

.topbar-actions {
  display: flex;
  gap: 8px;
}

/* ---- occasion input ---- */
.occasion-form {
  display: flex;
  gap: 8px;
  margin: 0 2px 12px;
}
.occasion-form input {
  flex: 1;
  min-width: 0;
  padding: 11px 13px;
  font-size: 1rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
}
.occasion-form .btn-primary {
  white-space: nowrap;
}
.exclude-note {
  margin: 0 2px 12px;
  font-size: 0.85rem;
  color: var(--muted);
}
.linkbtn {
  border: none;
  background: none;
  padding: 0;
  font: inherit;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
}

/* ---- outfit cards ---- */
.outfits {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.outfit-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
}
.rationale {
  margin: 0 0 12px;
  font-size: 0.95rem;
  line-height: 1.45;
}
.slot-row {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: thin;
}
.slot {
  flex: 0 0 84px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.slot-thumb {
  width: 84px;
  height: 108px;
  border-radius: 10px;
  background: var(--line) center / cover no-repeat;
  border: 1px solid var(--line);
  display: block;
}
.slot-thumb[aria-busy="true"] {
  animation: pulse 1.4s ease-in-out infinite;
}
.slot-label {
  font-size: 0.72rem;
  color: var(--muted);
}
.slot-exclude {
  font-size: 0.7rem;
  padding: 2px 8px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
}

/* ---- inactive toggle ---- */
.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 2px 12px;
  font-size: 0.88rem;
  color: var(--muted);
}

/* ---- status line ---- */
.status {
  margin: 0 2px 14px;
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  font-size: 0.9rem;
  color: var(--muted);
}
.status-error {
  color: var(--error);
  border-color: var(--error);
}

/* ---- grid ---- */
.grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}
@media (min-width: 520px) {
  .grid {
    grid-template-columns: repeat(3, 1fr);
  }
}
.grid[aria-busy="true"]::after {
  content: "Loading…";
  color: var(--muted);
  grid-column: 1 / -1;
  padding: 40px 0;
  text-align: center;
}

.empty {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--muted);
  padding: 56px 16px;
  line-height: 1.7;
}

/* ---- tile ---- */
.tile {
  position: relative;
  display: block;
  margin: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  color: inherit;
  text-decoration: none;
}
.tile-inactive {
  opacity: 0.5;
}
.thumb {
  aspect-ratio: 3 / 4;
  background-color: var(--line);
  background-size: cover;
  background-position: center;
}
.thumb[aria-busy="true"] {
  animation: pulse 1.4s ease-in-out infinite;
}
.thumb-error {
  display: grid;
  place-items: center;
}
.thumb-error::after {
  content: "no image";
  font-size: 0.8rem;
  color: var(--muted);
}
.tile figcaption {
  padding: 8px 10px 10px;
  font-size: 0.85rem;
  text-transform: capitalize;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.badge {
  position: absolute;
  top: 8px;
  left: 8px;
  background: var(--badge-bg);
  color: var(--badge-text);
  font-size: 0.72rem;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
}
.badge-muted {
  top: auto;
  bottom: 40px;
  background: var(--line);
  color: var(--muted);
}

/* ---- item view ---- */
.item-photo {
  aspect-ratio: 1 / 1;
  max-height: 46vh;
  background: var(--line) center / contain no-repeat;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 16px;
}
.item-photo[aria-busy="true"] {
  animation: pulse 1.4s ease-in-out infinite;
}
.item-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.field > span {
  font-size: 0.8rem;
  color: var(--muted);
}
.field select,
.field textarea {
  width: 100%;
  padding: 10px 12px;
  font-size: 1rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
}
.field-check {
  flex-direction: row;
  align-items: center;
  gap: 10px;
  padding-top: 4px;
}
.field-check span {
  font-size: 0.9rem;
  color: var(--text);
}
.save-bar {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0 max(12px, env(safe-area-inset-bottom));
  margin-top: 8px;
  background: linear-gradient(to top, var(--bg) 60%, transparent);
}
.save-bar .btn-primary {
  flex: 1;
  justify-content: center;
  padding: 12px;
}
.save-status {
  font-size: 0.85rem;
  color: var(--muted);
}
.save-status.save-error {
  color: var(--error);
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.55;
  }
}

/* ---- key gate ---- */
#keygate {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: grid;
  place-items: center;
  padding: 24px;
  z-index: 100;
}
.keygate-card {
  width: 100%;
  max-width: 340px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.keygate-card h1 {
  margin: 0;
  font-size: 1.4rem;
}
.keygate-card p {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
}
.keygate-card input {
  width: 100%;
  padding: 11px 13px;
  font-size: 1rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
}
.keygate-card button {
  padding: 11px;
  font-size: 1rem;
  font-weight: 600;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
}
.keygate-card button:disabled {
  opacity: 0.6;
}
.keygate-error {
  color: var(--error) !important;
  font-size: 0.86rem !important;
}
