:root {
  --paper: #ffffff;
  --paper-deep: #e7decf;
  --ink: #14110e;
  --muted: #8d867c;
  --line: #1b1713;
  --panel: #fffdf7;
  --blue: #c4dfe7;
  --green: #cbdcc1;
  --clay: #c77b64;
  --shadow: 0 28px 80px rgba(36, 28, 17, 0.16);
  --sidebar-w: 220px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }

body {
  background: linear-gradient(180deg, #ffffff 0%, #ffffff 100%);
  color: var(--ink);
  font-family: "Trebuchet MS", "Microsoft YaHei", sans-serif;
}

button, input { font: inherit; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

/* ========== App Layout ========== */
.app-layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
}

/* ========== Sidebar ========== */
.sidebar {
  display: flex;
  flex-direction: column;
  height: 100vh;
  padding: 16px 14px;
  border-right: 1.5px solid #e0d8cb;
  background: #ffffff;
  overflow-y: auto;
}

.sidebar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand-icon {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: 1.5px solid var(--clay);
}

.brand-name {
  font-size: 16px;
  font-weight: 900;
  color: var(--clay);
  letter-spacing: -0.3px;
}

.sidebar-toggle {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  color: var(--muted);
}

.sidebar-toggle svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

.new-topic-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  height: 40px;
  padding: 0 14px;
  border: 1.5px solid #d5cec3;
  border-radius: 10px;
  background: #f6f6f6;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  color: var(--ink);
  transition: background 120ms;
}

.new-topic-btn:hover { background: #f5efe2; }

.new-topic-btn svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
}

.sidebar-search {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 36px;
  margin: 10px 0 14px;
  padding: 0 10px;
  border: 1.5px solid #ddd6ca;
  border-radius: 8px;
  background: #fffdf8;
}

.sidebar-search svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  fill: none;
  stroke: var(--muted);
  stroke-width: 2;
  stroke-linecap: round;
}

.sidebar-search input {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: 0;
  background: transparent;
  font-size: 13px;
  color: var(--ink);
}

.sidebar-search input::placeholder { color: #b5aea5; }

.sidebar-section-label {
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  color: var(--muted);
  letter-spacing: 0.5px;
  margin-top: 10px;
  margin-bottom: 8px;
}

.history-empty {
  padding: 12px 8px;
  font-size: 13px;
  color: #b5aea5;
  text-align: center;
}

.history-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
  text-align: left;
  width: 100%;
  transition: background 100ms;
}

.history-item:hover { background: #eee8db; }

.history-item-thumb {
  width: 36px;
  height: 36px;
  border-radius: 6px;
  background: var(--paper-deep);
  object-fit: cover;
  flex-shrink: 0;
}

.history-item-info {
  min-width: 0;
  flex: 1;
}

.history-item-title {
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-item-meta {
  font-size: 11px;
  color: var(--muted);
  margin-top: 1px;
}

.sidebar-bottom {
  margin-top: auto;
  padding-top: 14px;
  border-top: 1px solid #e0d8cb;
}

.curator-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 4px;
  border: 0;
  background: transparent;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  cursor: pointer;
}

.curator-btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

/* ========== Main Content ========== */
.main-content {
  position: relative;
  overflow-y: auto;
  height: 100vh;
}

/* ========== Home View ========== */
.home-view {
  display: flex;
  justify-content: center;
  padding: 60px 40px 40px;
}

.home-inner {
  width: 100%;
  max-width: 780px;
}

.home-hero {
  margin: 0 0 36px;
  font-size: clamp(24px, 3.2vw, 34px);
  font-weight: 900;
  line-height: 1.35;
  text-align: center;
  color: var(--ink);
}

.home-search {
  display: flex;
  align-items: center;
  height: 54px;
  padding: 0 6px 0 22px;
  border: 2px solid #d5cec3;
  border-radius: 999px;
  background: #f6f6f6;
  transition: border-color 150ms;
}

.home-search:focus-within { border-color: var(--clay); }

.home-search input {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: 0;
  background: transparent;
  font-size: 15px;
  color: var(--ink);
}

.home-search input::placeholder { color: #b5aea5; }

.home-search-btn {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 1.5px solid #d5cec3;
  border-radius: 50%;
  background: #f6f6f6;
  cursor: pointer;
  color: var(--ink);
  transition: background 120ms, border-color 120ms;
}

.home-search-btn:hover {
  background: var(--ink);
  border-color: var(--ink);
  color: #fffdf8;
}

.home-search-btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.suggestion-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  margin-top: 18px;
}

.chip {
  height: 36px;
  padding: 0 18px;
  border: 1.5px solid #d5cec3;
  border-radius: 999px;
  background: #f6f6f6;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  cursor: pointer;
  transition: background 120ms, border-color 120ms;
}

.chip:hover {
  background: #f0e9dc;
  border-color: var(--clay);
}

.gallery-divider {
  display: flex;
  align-items: center;
  gap: 16px;
  margin: 42px 0 24px;
}

.gallery-divider::before,
.gallery-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: #d5cec3;
}

.gallery-divider span {
  font-size: 16px;
  font-weight: 800;
  white-space: nowrap;
  color: var(--ink);
}

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

.gallery-card {
  border: 1.5px solid #ddd6ca;
  border-radius: 14px;
  overflow: hidden;
  background: #f6f6f6;
  cursor: pointer;
  transition: transform 150ms, box-shadow 150ms;
}

.gallery-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 12px 32px rgba(36, 28, 17, 0.12);
}

.gallery-card-img {
  width: 100%;
  aspect-ratio: 1.45;
  background: var(--paper-deep);
}

.gallery-card-title {
  padding: 12px 14px;
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ========== Explorer View ========== */
.explorer-view {
  display: none;
  align-items: center;
  justify-content: center;
  height: 100vh;
  padding: 24px;
}

.explorer-view.is-active {
  display: flex;
}

.home-view.is-hidden {
  display: none;
}

.explorer-shell {
  position: relative;
  width: 100%;
  max-width: 1100px;
  aspect-ratio: 1.62;
  max-height: calc(100vh - 56px);
  border: 2.5px solid var(--line);
  border-radius: 24px;
  background: var(--paper-deep);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.explorer-topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 58px;
  padding: 0 18px;
  border-bottom: 2px solid var(--line);
  background: rgba(255, 253, 247, 0.94);
  flex-shrink: 0;
}

.window-dots {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
}

.window-dots span {
  width: 14px;
  height: 14px;
  border: 2px solid #d7d0c7;
  border-radius: 50%;
  background: #fffaf1;
}

.explorer-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
}

.clear-button {
  height: 34px;
  padding: 0 16px;
  border: 1.5px solid #d5cec3;
  border-radius: 999px;
  background: #f6f6f6;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  color: var(--ink);
  transition: background 120ms;
}

.clear-button:hover { background: #f0e9dc; }

.upload-btn {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1.5px solid #d5cec3;
  border-radius: 50%;
  background: #f6f6f6;
  cursor: pointer;
  color: var(--ink);
  transition: background 120ms;
}

.upload-btn:hover { background: #f0e9dc; }

.upload-btn svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.crumbs {
  display: none;
  min-width: 0;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  white-space: nowrap;
  font-size: 13px;
}

.crumbs.is-visible { display: flex; }

.crumb {
  max-width: 180px;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 13px;
}

.crumb:last-child { color: var(--ink); font-weight: 800; cursor: default; }
.crumb:disabled { pointer-events: none; }

.crumb + .crumb::before {
  content: "/";
  margin-right: 8px;
  color: #b5aea5;
  font-weight: 500;
}

/* ========== Stage ========== */
.stage {
  position: relative;
  flex: 1;
  display: grid;
  place-items: center;
  background: var(--paper-deep);
  overflow: hidden;
  cursor: crosshair;
}

.artwork {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #f7f2e8;
  user-select: none;
  -webkit-user-drag: none;
  transition: opacity 0.3s ease;
}

.stage.is-loading .artwork { opacity: 0.6; }

.error-toast {
  position: absolute;
  left: 50%;
  bottom: 26px;
  z-index: 9;
  max-width: calc(100% - 48px);
  padding: 10px 16px;
  border: 2px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 253, 248, 0.96);
  color: var(--ink);
  font-size: 14px;
  font-weight: 800;
  box-shadow: 0 16px 38px rgba(20, 17, 14, 0.16);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 8px);
  transition: opacity 160ms ease, transform 160ms ease;
}

.error-toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.loading-layer {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  background: rgba(231, 222, 207, 0.72);
  overflow: hidden;
}

.stage.is-click-loading .loading-layer {
  background: rgba(245, 239, 226, 0.34);
}

.stage.is-generate-loading .ripple { border-color: transparent; }
.stage.is-loading .loading-layer { display: flex; }

.ripple {
  position: absolute;
  left: var(--ripple-x, 50%);
  top: var(--ripple-y, 50%);
  width: 28px;
  height: 28px;
  border: 3px solid var(--ink);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  opacity: 0.9;
}

.ripple::before,
.ripple::after {
  content: "";
  position: absolute;
  inset: -3px;
  border: 3px solid var(--ink);
  border-radius: inherit;
  animation: pulse-ring 2.25s cubic-bezier(.16, .84, .32, 1) infinite;
}

.stage.is-generate-loading .ripple::before,
.stage.is-generate-loading .ripple::after {
  border-color: rgba(20, 17, 14, 0.28);
}

.ripple::after { animation-delay: 0.72s; }
.stage.is-loading .ripple { animation: pulse-core 1.35s ease-in-out infinite; }

@keyframes pulse-ring {
  0% { transform: scale(1); opacity: 0.72; }
  100% { transform: scale(18); opacity: 0; }
}

@keyframes pulse-core {
  0%, 100% { box-shadow: 0 0 0 0 rgba(20, 17, 14, 0.34); }
  50% { box-shadow: 0 0 0 18px rgba(20, 17, 14, 0); }
}

.loading-copy {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 2;
  transform: translate(-50%, -50%);
  display: grid;
  justify-items: center;
  white-space: nowrap;
}

.loading-copy strong {
  color: #4b4037;
  font-size: 18px;
  font-weight: 500;
}

/* ========== Inspector ========== */
.inspector {
  position: absolute;
  top: 14px;
  right: 14px;
  z-index: 8;
  width: min(340px, calc(100% - 28px));
  max-height: calc(100% - 28px);
  overflow: auto;
  padding: 16px;
  border: 1.5px solid #ddd6ca;
  border-radius: 14px;
  background: rgba(255, 253, 248, 0.96);
  box-shadow: 0 24px 68px rgba(20, 17, 14, 0.18);
  transform: translateX(calc(100% + 24px));
  transition: transform 220ms ease;
}

.inspector.is-open { transform: translateX(0); }

.inspector-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.inspector-head strong { font-size: 16px; }

.inspector-head button {
  width: 28px;
  height: 28px;
  border: 0;
  background: transparent;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
}

.inspector dl { display: grid; gap: 8px; margin: 12px 0 0; }
.inspector dt { color: var(--muted); font-size: 12px; font-weight: 800; }
.inspector dd { margin: 0 0 8px; font-size: 13px; line-height: 1.56; overflow-wrap: anywhere; }

.is-busy { cursor: wait; }

/* ========== Mobile ========== */
@media (max-width: 768px) {
  .app-layout {
    grid-template-columns: 1fr;
  }

  .sidebar {
    display: none;
  }

  .sidebar.is-open {
    display: flex;
    position: fixed;
    inset: 0;
    z-index: 100;
    width: 260px;
    box-shadow: 4px 0 24px rgba(0,0,0,0.15);
  }

  .home-view {
    padding: 40px 20px 20px;
  }

  .gallery-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }

  .home-hero {
    font-size: 22px;
  }

  .explorer-view {
    padding: 12px;
  }

  .explorer-shell {
    max-height: calc(100vh - 32px);
    border-radius: 18px;
  }

  .explorer-topbar {
    height: 50px;
    gap: 8px;
    padding: 0 12px;
  }
}
