:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --border: #ececec;
  --text: #111;
  --text-secondary: #6b6b6b;
  --text-tertiary: #9b9b9b;
  --accent: #111;
  --accent-soft: #f2f2f2;
  --danger: #e54848;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  --radius: 14px;
  --radius-sm: 10px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  user-select: none;
}

body {
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  overflow: hidden;
}

/* ============ Header ============ */
.app-header {
  position: fixed;
  top: var(--safe-top);
  left: 0;
  right: 0;
  background: var(--bg);
  z-index: 50;
  padding: 8px 16px 0;
}

.user-switch {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
}

.user-chip {
  flex: 1;
  padding: 6px 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
}

.user-chip.active {
  background: var(--accent);
  color: var(--surface);
  border-color: var(--accent);
}

.nav-tabs {
  display: flex;
  gap: 24px;
  padding: 8px 0 6px;
  border-bottom: 1px solid var(--border);
}

.tab {
  background: none;
  border: none;
  padding: 4px 0;
  font-size: 17px;
  color: var(--text-tertiary);
  position: relative;
  cursor: pointer;
  font-weight: 500;
}

.tab.active {
  color: var(--text);
}

.tab.active::after {
  content: "";
  position: absolute;
  bottom: -7px;
  left: 50%;
  transform: translateX(-50%);
  width: 20px;
  height: 2px;
  background: var(--text);
  border-radius: 2px;
}

/* ============ Pages ============ */
.page {
  position: fixed;
  top: calc(var(--safe-top) + 88px);
  left: 0;
  right: 0;
  bottom: 0;
  display: none;
  overflow: hidden;
}

.page.active {
  display: flex;
  flex-direction: column;
}

/* ============ Category Bar ============ */
.category-bar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 16px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  flex-shrink: 0;
}

.category-bar::-webkit-scrollbar { display: none; }

.cat-chip {
  flex-shrink: 0;
  padding: 6px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
}

.cat-chip.active {
  background: var(--accent);
  color: var(--surface);
  border-color: var(--accent);
}

/* ============ Clothes Grid ============ */
.clothes-grid {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 16px 80px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(95px, 1fr));
  gap: 10px;
  align-content: start;
}

.clothes-item {
  aspect-ratio: 1 / 1;
  background: var(--surface);
  border-radius: var(--radius-sm);
  overflow: hidden;
  cursor: pointer;
  position: relative;
  box-shadow: var(--shadow);
  transition: transform 0.15s;
}

.clothes-item:active {
  transform: scale(0.95);
}

.clothes-item img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.clothes-item .cat-tag {
  position: absolute;
  bottom: 4px;
  left: 4px;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 6px;
  backdrop-filter: blur(8px);
}

.clothes-empty {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--text-tertiary);
  padding: 60px 20px;
  font-size: 14px;
}

/* ============ FAB ============ */
.fab {
  position: fixed;
  bottom: calc(var(--safe-bottom) + 24px);
  right: 20px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--surface);
  border: none;
  font-size: 28px;
  font-weight: 300;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
  z-index: 40;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ============ Outfit Stage ============ */
.outfit-stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.outfit-card-stack {
  position: relative;
  width: 100%;
  height: 100%;
  touch-action: pan-y;
}

.outfit-card {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(1);
  width: min(85vw, 360px);
  aspect-ratio: 9 / 16;
  background: white;
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.3s;
  will-change: transform, opacity;
}

.outfit-card canvas {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.outfit-card .outfit-name {
  position: absolute;
  bottom: 12px;
  left: 12px;
  right: 12px;
  background: rgba(0, 0, 0, 0.55);
  color: white;
  font-size: 13px;
  padding: 6px 10px;
  border-radius: 8px;
  backdrop-filter: blur(8px);
  text-align: center;
}

.empty-stage {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: var(--text-tertiary);
  padding: 40px;
  width: min(80vw, 320px);
}

.empty-stage .primary-btn {
  margin-top: 16px;
}

.swipe-hint {
  position: absolute;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 11px;
  color: var(--text-tertiary);
  background: rgba(255, 255, 255, 0.8);
  padding: 4px 10px;
  border-radius: 999px;
  backdrop-filter: blur(8px);
  pointer-events: none;
}

.swipe-hint.hidden {
  opacity: 0;
  transition: opacity 0.4s;
}

/* ============ Sheets (Bottom Modals) ============ */
.sheet {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 100;
  display: none;
  align-items: flex-end;
  opacity: 0;
  transition: opacity 0.25s;
}

.sheet.active {
  display: flex;
  opacity: 1;
}

.sheet-content {
  width: 100%;
  max-height: 92vh;
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  padding: 12px 20px calc(var(--safe-bottom) + 20px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
}

.sheet.active .sheet-content {
  transform: translateY(0);
}

.sheet-handle {
  width: 36px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 0 auto 12px;
}

.sheet h2 {
  margin: 0 0 16px;
  font-size: 18px;
  text-align: center;
}

/* ============ Buttons ============ */
.primary-btn {
  width: 100%;
  padding: 14px;
  background: var(--accent);
  color: var(--surface);
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 500;
  margin-top: 10px;
  cursor: pointer;
}

.primary-btn:disabled {
  opacity: 0.4;
}

.ghost-btn {
  width: 100%;
  padding: 14px;
  background: transparent;
  color: var(--text-secondary);
  border: none;
  font-size: 15px;
  margin-top: 6px;
  cursor: pointer;
}

.danger-btn {
  width: 100%;
  padding: 14px;
  background: var(--danger);
  color: var(--surface);
  border: none;
  border-radius: 12px;
  font-size: 15px;
  margin-top: 10px;
  cursor: pointer;
}

.text-input {
  width: 100%;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 15px;
  margin-top: 10px;
  outline: none;
  -webkit-appearance: none;
}

/* ============ Crop Area ============ */
.crop-area {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 10px;
}

.crop-area.hidden { display: none; }

#crop-canvas {
  width: 100%;
  height: auto;
  display: block;
  background: #f0f0f0;
  border-radius: var(--radius-sm);
  image-rendering: -webkit-optimize-contrast;
}

.crop-controls {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.crop-controls label {
  display: flex;
  flex-direction: column;
  font-size: 12px;
  color: var(--text-secondary);
  gap: 4px;
}

.crop-controls input[type="range"] {
  width: 100%;
}

.crop-controls button {
  align-self: flex-start;
  width: auto;
  padding: 6px 14px;
  margin: 0;
  background: var(--accent-soft);
  color: var(--text);
  border-radius: 8px;
}

/* ============ Category Picker ============ */
.category-picker.hidden { display: none; }

.category-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 10px;
}

.cat-option {
  padding: 14px 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.cat-option .icon {
  font-size: 22px;
}

.cat-option.selected {
  background: var(--accent);
  color: var(--surface);
  border-color: var(--accent);
}

.hint {
  font-size: 12px;
  color: var(--text-tertiary);
  margin: 0 0 6px;
}

/* ============ Outfit Sheet ============ */
.sheet-content.large {
  max-height: 94vh;
}

.outfit-canvas-wrap {
  background: white;
  border-radius: var(--radius);
  padding: 8px;
  display: flex;
  justify-content: center;
  margin-bottom: 12px;
}

#outfit-canvas {
  width: 100%;
  max-width: 320px;
  height: auto;
  background: #fafafa;
  border-radius: 10px;
}

.layer-controls {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 10px;
}

.layer-category-tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 8px;
  scrollbar-width: none;
}

.layer-category-tabs::-webkit-scrollbar { display: none; }

.layer-cat-tab {
  flex-shrink: 0;
  padding: 4px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12px;
  cursor: pointer;
}

.layer-cat-tab.active {
  background: var(--accent);
  color: var(--surface);
  border-color: var(--accent);
}

.layer-items {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  max-height: 180px;
  overflow-y: auto;
}

.layer-item {
  aspect-ratio: 1;
  background: var(--bg);
  border: 2px solid transparent;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  position: relative;
}

.layer-item.selected {
  border-color: var(--accent);
}

.layer-item img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.layer-empty {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--text-tertiary);
  padding: 24px 10px;
  font-size: 12px;
}

/* ============ Detail Sheet ============ */
#detail-img {
  width: 100%;
  max-height: 50vh;
  object-fit: contain;
  background: white;
  border-radius: var(--radius);
  margin-bottom: 10px;
}

#detail-category {
  text-align: center;
  color: var(--text-secondary);
  margin: 6px 0 16px;
  font-size: 14px;
}

.hidden { display: none !important; }
