:root {
  color-scheme: light;
  --ink: #24312e;
  --muted: #72807b;
  --paper: #f4efe4;
  --panel: #fffdf8;
  --line: #d8d6cd;
  --green: #315f54;
  --green-dark: #213f39;
  --green-soft: #dfe9e3;
  --coral: #c96554;
  --coral-soft: #fae2dc;
  --shadow: 0 16px 38px rgb(43 55 49 / 14%);
  font-family: "Segoe UI", Roboto, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { width: 100%; min-height: 100%; margin: 0; overscroll-behavior: none; }
body {
  min-width: 320px;
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 0, rgb(201 101 84 / 9%), transparent 32rem),
    var(--paper);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
button, input, select { font: inherit; }
button, select, label { touch-action: manipulation; }
button {
  min-height: 44px;
  padding: 9px 14px;
  color: var(--ink);
  font-weight: 700;
  background: #eceee9;
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
}
button:hover { filter: brightness(.97); }
button:active { transform: translateY(1px); }
button:focus-visible, input:focus-visible, select:focus-visible {
  outline: 3px solid rgb(49 95 84 / 35%);
  outline-offset: 2px;
}
button.primary { color: #fff; background: var(--green); border-color: var(--green); }
button.danger { color: #9a4038; background: var(--coral-soft); border-color: #e8b9b1; }
.icon-button { min-width: 46px; padding-inline: 10px; font-size: 1.3rem; }
.compact { min-height: 40px; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: 1.35rem; }
h2 { margin-bottom: 0; font-size: 1.25rem; }
.eyebrow {
  display: block;
  margin-bottom: 3px;
  color: var(--green);
  font-size: .7rem;
  font-weight: 850;
  letter-spacing: .13em;
  text-transform: uppercase;
}
.app { min-height: 100vh; display: grid; grid-template-rows: auto 1fr auto; }
.topbar {
  z-index: 30;
  position: sticky;
  top: 0;
  display: grid;
  grid-template-columns: minmax(200px, 1fr) auto minmax(200px, 1fr);
  align-items: center;
  gap: 14px;
  min-height: 72px;
  padding: 10px clamp(14px, 2.5vw, 30px);
  color: #fff;
  background: rgb(33 63 57 / 96%);
  border-bottom: 1px solid rgb(255 255 255 / 12%);
  backdrop-filter: blur(14px);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { width: 50px; height: 46px; object-fit: contain; object-position: center; background: #fff8e9; border-radius: 11px; }
.brand div { display: grid; }
.brand strong { font-size: 1.1rem; letter-spacing: .025em; }
.brand span { color: rgb(255 255 255 / 68%); font-size: .73rem; }
.view-switch { display: flex; padding: 4px; background: rgb(0 0 0 / 15%); border-radius: 14px; }
.view-switch button { min-height: 38px; color: rgb(255 255 255 / 72%); background: transparent; border: 0; }
.view-switch button.active { color: var(--green-dark); background: #fff7e9; box-shadow: 0 2px 8px rgb(0 0 0 / 14%); }
.header-actions { display: flex; justify-content: flex-end; gap: 7px; }
.header-actions button { color: #fff; background: rgb(255 255 255 / 10%); border-color: rgb(255 255 255 / 18%); }
.header-actions button.primary { color: var(--green-dark); background: #f3c96a; border-color: #f3c96a; }

main { min-width: 0; min-height: 0; }
.basket-view { display: grid; grid-template-columns: 250px minmax(0, 1fr); gap: 16px; min-height: calc(100vh - 112px); padding: 16px clamp(12px, 2vw, 28px); }
.sidebar, .collection {
  min-width: 0;
  background: rgb(255 253 248 / 94%);
  border: 1px solid rgb(49 95 84 / 12%);
  border-radius: 20px;
  box-shadow: var(--shadow);
}
.sidebar {
  position: sticky;
  top: 88px;
  display: flex;
  flex-direction: column;
  height: calc(100vh - 128px);
  padding: 16px;
}
.sidebar-heading, .collection-toolbar, .canvas-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.group-list { display: grid; gap: 6px; margin-top: 7px; overflow-y: auto; }
.group-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 48px;
  text-align: left;
  background: transparent;
  border-color: transparent;
}
.group-row b { min-width: 28px; padding: 3px 7px; text-align: center; color: var(--muted); background: #ecece6; border-radius: 999px; font-size: .72rem; }
.group-row.active { color: var(--green-dark); background: var(--green-soft); border-color: #bfd2c8; }
.sidebar-bottom { display: grid; gap: 7px; margin-top: auto; padding-top: 12px; border-top: 1px solid var(--line); }
.sidebar-bottom > button { width: 100%; text-align: left; background: transparent; }
.backup-box { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; margin-top: 7px; }
.collection { position: relative; min-height: 680px; padding: clamp(16px, 2vw, 24px); overflow: hidden; }
.collection-toolbar { align-items: flex-start; }
.filter-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 7px; }
input, select {
  min-height: 44px;
  padding: 9px 11px;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 11px;
}
#searchInput { width: min(240px, 30vw); }
.drop-zone {
  display: grid;
  place-items: center;
  margin: 18px 0;
  min-height: 86px;
  padding: 15px;
  color: var(--green);
  text-align: center;
  background:
    repeating-linear-gradient(45deg, rgb(49 95 84 / 3%) 0 8px, transparent 8px 16px),
    var(--green-soft);
  border: 2px dashed #9db8ad;
  border-radius: 16px;
  transition: .15s ease;
}
.drop-zone.dragover, body.dragging .drop-zone { transform: scale(1.008); background-color: #d2e8dc; border-color: var(--green); }
.drop-zone small { color: var(--muted); }
.media-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  align-items: start;
  gap: 14px;
}
.media-card {
  position: relative;
  min-width: 0;
  padding: 7px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 15px;
  box-shadow: 0 6px 15px rgb(43 55 49 / 7%);
  cursor: pointer;
  user-select: none;
}
.media-card.selected { outline: 3px solid #73a494; }
.media-preview {
  position: relative;
  display: grid;
  width: 100%;
  aspect-ratio: 4 / 3;
  place-items: center;
  overflow: hidden;
  background: #e8e6df;
  border-radius: 11px;
}
.media-preview img, .media-preview video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}
.media-preview video { pointer-events: auto; }
.media-preview .type-badge {
  position: absolute;
  top: 7px;
  left: 7px;
  padding: 4px 7px;
  color: #fff;
  background: rgb(22 30 28 / 72%);
  border-radius: 999px;
  font-size: .67rem;
  font-weight: 800;
}
.link-preview { padding: 18px; color: var(--green); text-align: center; word-break: break-word; }
.link-preview b { display: block; font-size: 2rem; }
.media-card h3 { margin: 9px 4px 2px; overflow: hidden; font-size: .82rem; text-overflow: ellipsis; white-space: nowrap; }
.media-card-meta { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 0 4px 3px; color: var(--muted); font-size: .7rem; }
.card-delete {
  position: absolute;
  z-index: 2;
  top: 5px;
  right: 5px;
  display: grid;
  width: 38px;
  min-height: 38px;
  padding: 0;
  place-items: center;
  color: #fff;
  background: rgb(25 34 31 / 78%);
  border: 0;
  border-radius: 50%;
  opacity: 0;
}
.media-card:hover .card-delete, .media-card:focus-within .card-delete { opacity: 1; }
.empty-state { display: grid; min-height: 330px; place-content: center; justify-items: center; text-align: center; }
.empty-state img { width: 150px; height: 142px; object-fit: contain; }
.empty-state h3 { margin-bottom: 4px; }
.empty-state p { max-width: 420px; color: var(--muted); line-height: 1.5; }

.canvas-view { height: calc(100vh - 112px); min-height: 560px; }
.canvas-toolbar {
  min-height: 66px;
  padding: 9px clamp(12px, 2vw, 28px);
  background: #fffaf1;
  border-bottom: 1px solid var(--line);
}
.tool-group { display: flex; align-items: center; gap: 7px; }
.tool-group button { min-width: 44px; }
.scene-picker { display: flex; align-items: center; gap: 8px; font-size: .78rem; font-weight: 750; }
.scene-picker select { max-width: 240px; }
.canvas-wrap { position: relative; height: calc(100% - 66px); overflow: hidden; background: #d9d9d2; }
#moodboardCanvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: default; }
.canvas-hint {
  position: absolute;
  left: 16px;
  bottom: 16px;
  max-width: min(650px, calc(100% - 32px));
  padding: 8px 11px;
  color: #50605a;
  background: rgb(255 253 248 / 86%);
  border: 1px solid rgb(49 95 84 / 12%);
  border-radius: 10px;
  pointer-events: none;
  font-size: .76rem;
  backdrop-filter: blur(8px);
}
.floating-delete { position: absolute; right: 18px; bottom: 16px; color: #922f2c; background: var(--coral-soft); }

footer { padding: 10px 24px 14px; color: var(--muted); text-align: center; font-size: .76rem; }
.mascot {
  z-index: 45;
  position: fixed;
  right: 18px;
  bottom: max(18px, env(safe-area-inset-bottom));
  width: 112px;
  height: 126px;
  min-height: 0;
  padding: 0;
  background: transparent;
  border: 0;
  touch-action: manipulation;
  filter: drop-shadow(0 10px 8px rgb(0 0 0 / 20%));
}
.mascot img { position: relative; width: 100%; height: 100%; object-fit: contain; transform-origin: 50% 88%; transition: transform .16s ease; }
.mascot.happy img { animation: pug-hop .55s ease; }
.mascot.receive img { animation: pug-receive .7s ease; }
.mascot-shadow { position: absolute; right: 16px; bottom: 2px; left: 16px; height: 16px; background: rgb(28 42 37 / 25%); border-radius: 50%; filter: blur(3px); }
.heart {
  z-index: 80;
  position: fixed;
  color: #d85f66;
  font-size: 24px;
  pointer-events: none;
  animation: float-heart 1.05s ease-out forwards;
}
@keyframes pug-hop { 50% { transform: translateY(-12px) rotate(-3deg); } }
@keyframes pug-receive { 30% { transform: scale(1.08) rotate(4deg); } 65% { transform: scale(.98) rotate(-4deg); } }
@keyframes float-heart { from { opacity: 1; transform: translate(0, 0) scale(.6); } to { opacity: 0; transform: translate(var(--heart-x), -90px) scale(1.25) rotate(18deg); } }

.wardrobe-dialog { width: min(760px, calc(100vw - 24px)); max-height: min(780px, calc(100vh - 24px)); padding: 22px; overflow: auto; }
.wardrobe-heading { display: flex; align-items: center; justify-content: space-between; gap: 18px; }
.wardrobe-heading h2 { margin: 0; }
.wardrobe-note { margin: 7px 0 16px; color: var(--muted); font-size: .8rem; }
.skin-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(145px, 1fr)); gap: 11px; }
.skin-card { position: relative; display: grid; min-width: 0; padding: 8px; gap: 7px; text-align: center; background: #fff; border: 2px solid transparent; border-radius: 15px; box-shadow: 0 4px 12px rgb(43 55 49 / 8%); }
.skin-card.selected { border-color: var(--green); background: var(--green-soft); }
.skin-card img { width: 100%; height: 150px; object-fit: contain; background: linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%), linear-gradient(45deg, #eee 25%, #fafafa 25%, #fafafa 75%, #eee 75%); background-position: 0 0, 8px 8px; background-size: 16px 16px; border-radius: 10px; }
.skin-card span { overflow: hidden; font-size: .78rem; font-weight: 750; text-overflow: ellipsis; white-space: nowrap; }
.skin-card.selected::after { content: "✓"; position: absolute; top: 12px; right: 12px; display: grid; width: 27px; height: 27px; place-items: center; color: #fff; background: var(--green); border-radius: 50%; }
.upload-skin { width: 100%; margin-top: 16px; }

dialog {
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 30px 80px rgb(0 0 0 / 35%);
}
dialog::backdrop { background: rgb(12 29 25 / 76%); backdrop-filter: blur(5px); }
.viewer { width: min(1100px, calc(100vw - 24px)); height: min(900px, calc(100vh - 24px)); padding: 0; overflow: hidden; background: #18221f; color: #fff; }
.viewer::backdrop { background: rgb(5 10 8 / 92%); }
.viewer-bar { display: flex; align-items: center; justify-content: space-between; min-height: 64px; gap: 12px; padding: 10px 14px; border-bottom: 1px solid rgb(255 255 255 / 12%); }
.viewer-bar div { display: grid; min-width: 0; }
.viewer-bar strong, .viewer-bar span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-bar span { color: rgb(255 255 255 / 58%); font-size: .72rem; }
.viewer-bar button { color: #fff; background: rgb(255 255 255 / 10%); border-color: rgb(255 255 255 / 15%); }
.viewer-body { display: grid; height: calc(100% - 64px); place-items: center; }
.viewer-body img, .viewer-body video { display: block; max-width: 100%; max-height: 100%; width: auto; height: auto; object-fit: contain; }
.viewer-body video { width: 100%; height: 100%; background: #000; }
.viewer-body iframe { width: 100%; height: 100%; border: 0; }
.confirm-dialog { width: min(420px, calc(100vw - 24px)); padding: 22px; }
.confirm-dialog h2 { margin-bottom: 10px; }
.confirm-dialog p { color: var(--muted); line-height: 1.45; }
.name-dialog form { display: grid; gap: 12px; }
.name-dialog label { display: grid; gap: 7px; color: var(--muted); font-size: .8rem; font-weight: 750; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.toast {
  z-index: 100;
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  max-width: min(520px, calc(100vw - 32px));
  padding: 12px 16px;
  color: #fff;
  text-align: center;
  background: var(--green-dark);
  border-radius: 13px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 10px);
  transition: .18s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: #8c3833; }
.busy-overlay { z-index: 90; position: fixed; inset: 0; display: grid; place-content: center; justify-items: center; gap: 12px; color: #fff; background: rgb(22 42 37 / 78%); backdrop-filter: blur(4px); }
.spinner { width: 44px; height: 44px; border: 4px solid rgb(255 255 255 / 28%); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }
.hidden { display: none !important; }
.visually-hidden { position: fixed; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 850px) {
  .topbar { grid-template-columns: 1fr auto; }
  .view-switch { grid-row: 2; grid-column: 1 / -1; justify-self: stretch; }
  .view-switch button { flex: 1; }
  .header-actions { grid-column: 2; grid-row: 1; }
  .header-actions .compact { padding-inline: 10px; }
  .basket-view { grid-template-columns: 180px minmax(0, 1fr); padding: 10px; }
  .sidebar { top: 138px; height: calc(100vh - 170px); padding: 11px; }
  .collection { padding: 14px; }
  .collection-toolbar { align-items: stretch; flex-direction: column; }
  .filter-actions { justify-content: stretch; }
  #searchInput { width: 100%; flex: 1; }
  .media-grid { grid-template-columns: repeat(auto-fill, minmax(145px, 1fr)); }
  .mascot { right: 6px; width: 88px; height: 100px; }
  .canvas-toolbar { flex-wrap: wrap; height: auto; min-height: 66px; }
  .canvas-view { height: calc(100vh - 160px); }
  .canvas-wrap { height: calc(100% - 116px); }
}

@media (max-width: 560px) {
  .brand span { display: none; }
  .header-actions #pasteButton { display: none; }
  .basket-view { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; }
  .group-list { display: flex; overflow-x: auto; }
  .group-row { width: auto; min-width: 130px; }
  .sidebar-bottom { grid-template-columns: 1fr 1fr; }
  .sidebar-bottom .backup-box { grid-column: 1 / -1; }
  .collection { min-height: 450px; }
  .skin-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .skin-card img { height: 120px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; animation-duration: .01ms !important; }
}
