:root {
  --bg: #fdf7ef;
  --bg-grain: #f6ecdf;
  --panel: #fffdfa;
  --ink: #2f2a26;
  --dim: #8d8378;
  --line: #ebe0d2;
  --accent: #ff7a45;
  --accent-soft: #ffe8dd;
  --ok: #35a37a;
  --radius: 16px;
  --shadow: 0 2px 4px rgba(90, 66, 40, .04), 0 8px 24px rgba(90, 66, 40, .07);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  background-image:
    radial-gradient(circle at 15% 10%, #fff2e2 0%, transparent 45%),
    radial-gradient(circle at 85% 85%, #eef6f2 0%, transparent 45%);
  color: var(--ink);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* ---------- 顶栏 ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 253, 250, .8);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar h1 { font-size: 19px; letter-spacing: .5px; }
.topbar-meta { display: flex; align-items: center; gap: 12px; }

.tag {
  font-size: 11px;
  color: var(--dim);
  background: var(--bg-grain);
  border: 1px solid var(--line);
  padding: 3px 9px;
  border-radius: 999px;
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
}

/* ---------- 布局 ---------- */

.layout {
  display: grid;
  grid-template-columns: 264px minmax(0, 1fr) 264px;
  gap: 18px;
  padding: 18px 22px 32px;
  align-items: start;
  max-width: 1600px;
  margin: 0 auto;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 18px;
  position: sticky;
  top: 78px;
}

.block { display: flex; flex-direction: column; gap: 10px; }

.block h2 {
  font-size: 12px;
  font-weight: 600;
  color: var(--dim);
  letter-spacing: 1.2px;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.val { color: var(--accent); font-family: ui-monospace, Consolas, monospace; font-size: 12px; }
.hint { font-size: 11px; color: var(--dim); line-height: 1.5; }
.dim { color: var(--dim); }
.center { text-align: center; }
.mono { font-family: ui-monospace, "SF Mono", Consolas, monospace; font-size: 12px; color: var(--dim); }
.hidden { display: none !important; }

/* ---------- 主题 ---------- */

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

.theme-grid button {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 11px;
  padding: 8px 2px 6px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  font-size: 11px;
  color: var(--ink);
  transition: transform .12s, border-color .12s, background .12s;
}
.theme-grid button span.emo { font-size: 18px; line-height: 1.1; }
.theme-grid button:hover { transform: translateY(-2px); border-color: var(--accent); }
.theme-grid button.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  font-weight: 600;
}

.subject-row { display: flex; gap: 6px; }

.subject-row input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 11px;
  font-size: 13px;
  font-family: inherit;
  background: #fff;
  color: var(--ink);
}
.subject-row input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.dice { padding: 0 11px; font-size: 15px; }

.chips { display: flex; flex-wrap: wrap; gap: 5px; }

.chips button {
  border: 1px dashed var(--line);
  background: #fff;
  color: var(--dim);
  border-radius: 999px;
  padding: 4px 9px;
  font-size: 11px;
  cursor: pointer;
  font-family: inherit;
}
.chips button:hover { border-style: solid; border-color: var(--accent); color: var(--accent); }

/* ---------- 按钮 ---------- */

.primary-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 11px;
  padding: 11px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 3px 10px rgba(255, 122, 69, .3);
  transition: transform .1s, filter .12s;
}
.primary-btn.small { padding: 9px; font-size: 13px; flex: 1; }
.primary-btn:hover:not(:disabled) { filter: brightness(1.05); }
.primary-btn:active:not(:disabled) { transform: translateY(1px); }
.primary-btn:disabled { opacity: .5; cursor: not-allowed; box-shadow: none; }

.ghost-btn {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 11px;
  font-size: 12px;
  cursor: pointer;
  color: var(--ink);
  font-family: inherit;
  transition: border-color .12s, background .12s;
}
.ghost-btn:hover:not(:disabled) { border-color: var(--accent); background: var(--accent-soft); }
.ghost-btn:disabled { opacity: .4; cursor: not-allowed; }
.ghost-btn.wide { width: 100%; }

.btn-row { display: flex; gap: 6px; }
.btn-row > * { flex: 1; }

.status { font-size: 11px; color: var(--dim); line-height: 1.5; min-height: 15px; }
.status.error { color: #d1493f; }
.status.ok { color: var(--ok); }

/* ---------- 画布 ---------- */

.stage-wrap { display: flex; flex-direction: column; align-items: center; gap: 10px; }

.stage {
  position: relative;
  width: min(100%, 74vh);
  aspect-ratio: 1;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  touch-action: none; /* 触屏上画画时别把页面滚走 */
}

/* transform 会让 wrap 成为一个新的层叠上下文，line 层的 multiply 从此只跟 wrap 内部
   的内容混合。所以白底要挂在 wrap 上：白纸 → 颜色 → 线稿，混合组自成一体，
   不再依赖 .stage 的背景色。 */
.canvas-wrap {
  position: absolute;
  inset: 0;
  background: #fff;
  transform-origin: 0 0;
  will-change: transform;
}

.layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

/* 只有 stroke 层收指针事件；line 层用 multiply 压在最上面 */
.layer-stroke { z-index: 2; cursor: crosshair; }
.layer-line { z-index: 3; pointer-events: none; mix-blend-mode: multiply; }

/* 光标由 stage 上的状态类统一决定，免得和 .layer-stroke 的 crosshair 打架 */
.stage.can-pan .layer-stroke { cursor: grab; }
.stage.panning .layer-stroke { cursor: grabbing; }

.placeholder,
.loading {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: rgba(255, 253, 250, .94);
  color: var(--dim);
  font-size: 13px;
  text-align: center;
}
.placeholder-art { font-size: 46px; opacity: .55; margin-bottom: 4px; }
.placeholder p.dim { font-size: 12px; }

.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.art-info { font-size: 12px; color: var(--dim); min-height: 16px; }

/* ---------- 工具 ---------- */

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

.tool {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 11px;
  padding: 9px 2px 7px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  font-size: 15px;
  font-family: inherit;
  color: var(--ink);
  transition: transform .12s, border-color .12s, background .12s;
}
.tool span { font-size: 10px; color: var(--dim); }
.tool:hover { transform: translateY(-2px); border-color: var(--accent); }
.tool.active { background: var(--accent-soft); border-color: var(--accent); }
.tool.active span { color: var(--accent); font-weight: 600; }

input[type=range] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

.size-preview {
  height: 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-grain);
  border-radius: 10px;
}
#sizeDot { border-radius: 50%; background: var(--ink); display: block; }

/* ---------- 调色板 ---------- */

.current-color { display: flex; align-items: center; gap: 8px; }

.color-dot {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px var(--line), 0 2px 5px rgba(0, 0, 0, .1);
  flex-shrink: 0;
}

#colorPicker {
  width: 30px;
  height: 26px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  cursor: pointer;
  padding: 2px;
}

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

.palette button {
  aspect-ratio: 1;
  border-radius: 6px;
  border: 1px solid rgba(0, 0, 0, .1);
  cursor: pointer;
  padding: 0;
  transition: transform .1s;
}
.palette button:hover { transform: scale(1.18); z-index: 1; }
.palette button.active {
  outline: 2px solid var(--ink);
  outline-offset: 1px;
  transform: scale(1.1);
}

.actions { gap: 6px; }

/* ---------- 作品 ---------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(47, 42, 38, .45);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.modal-card {
  background: var(--panel);
  border-radius: 18px;
  padding: 20px;
  width: min(880px, 100%);
  max-height: 84vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}
.modal-head h2 { font-size: 16px; }

.works-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }

.work {
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  background: #fff;
  position: relative;
  transition: transform .12s, border-color .12s;
}
.work:hover { transform: translateY(-2px); border-color: var(--accent); }
.work img { width: 100%; display: block; aspect-ratio: 1; object-fit: cover; cursor: zoom-in; }
.work-meta { padding: 7px 9px; font-size: 11px; }
.work-meta .dim { font-size: 10px; }

/* 悬停才浮出来的操作条，平时不挡画面 */
.work-actions {
  position: absolute;
  left: 6px;
  right: 6px;
  top: 6px;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity .12s;
}
.work:hover .work-actions,
.work:focus-within .work-actions { opacity: 1; }

.work-actions button {
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, .95);
  border-radius: 8px;
  height: 26px;
  cursor: pointer;
  font-size: 11px;
  font-family: inherit;
  padding: 0 7px;
  backdrop-filter: blur(4px);
}
.work-actions button:hover:not(:disabled) { border-color: var(--accent); background: var(--accent-soft); }
.work-actions button:disabled { opacity: .45; cursor: not-allowed; }
.work-actions .grow { flex: 1; }
.work-actions .del:hover { background: #ffe0dd; border-color: #f08c84; }

/* ---------- 原图大图 ---------- */

.lightbox-inner {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: min(92vw, 900px);
  max-height: 90vh;
}

.lightbox-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: #fff;
  font-size: 13px;
}
.lightbox-actions { display: flex; gap: 6px; }

#lightboxImg {
  min-height: 0;
  max-width: 100%;
  max-height: calc(90vh - 46px);
  object-fit: contain;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .4);
}

/* ---------- toast ---------- */

.toast {
  position: fixed;
  bottom: 26px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 10px 20px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 100;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
  animation: rise .22s ease;
}
@keyframes rise {
  from { opacity: 0; transform: translate(-50%, 10px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* ---------- 窄屏 ---------- */

@media (max-width: 1100px) {
  .layout { grid-template-columns: 1fr; }
  .panel { position: static; }
  .stage { width: 100%; }
  .theme-grid { grid-template-columns: repeat(5, 1fr); }
  .tool-grid { grid-template-columns: repeat(6, 1fr); }
}
