:root {
  --bg: #f5f6f8;
  --toolbar-bg: #ffffff;
  --border: #d7dbe0;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --box-stroke: #334155;
  --box-fill: rgba(59, 130, 246, 0.06);
  --text: #1f2937;
  --muted: #6b7280;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
a { color: inherit; }

.noscript {
  padding: 10px 14px;
  background: #fef3c7;
  color: #78350f;
  border-bottom: 1px solid #f59e0b;
  font-size: 14px;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Toolbar ---------- */
#toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--toolbar-bg);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  z-index: 30;
}
#toolbar .title { font-size: 16px; font-weight: 600; margin: 0 8px 0 0; white-space: nowrap; }
#toolbar .sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }
#zoom-label {
  font-size: 13px; color: var(--muted); min-width: 46px; text-align: center;
  cursor: pointer; user-select: none;
}
#zoom-label:hover { color: var(--accent); }
button.tool {
  font: inherit; font-size: 13px; padding: 6px 12px;
  border: 1px solid var(--border); background: #fff; border-radius: 6px;
  cursor: pointer; color: var(--text); white-space: nowrap;
}
button.tool:hover { background: #f0f2f5; }
button.tool.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
button.tool.primary:hover { background: #1d4ed8; }
button.tool.danger:hover { background: #fee2e2; border-color: #fca5a5; }
button.tool:disabled { opacity: .45; cursor: default; }

/* 绘图工具组：当前工具高亮 */
.toolset { display: inline-flex; gap: 4px; }
button.tool.active { background: var(--accent); color: #fff; border-color: var(--accent); }
button.tool.active:hover { background: #1d4ed8; }
/* 绘图工具下画布用十字光标（平移态除外，让 grab/grabbing 生效）；
   命中形状时由 JS 内联改为 move（inline 优先级更高）。 */
body[data-tool="rect"] #viewport:not(.panning):not(.can-pan),
body[data-tool="ellipse"] #viewport:not(.panning):not(.can-pan),
body[data-tool="polygon"] #viewport:not(.panning):not(.can-pan) { cursor: crosshair; }

/* ---------- Main ---------- */
#main { flex: 1 1 auto; display: flex; min-height: 0; }

/* viewport = fixed window; world = transformed (pan/zoom) layer inside it.
   背景网格画在 viewport 上（铺满整个可视区、无限范围）：background-size 随 scale
   变化、background-position 随平移 (tx,ty) 偏移，由 JS 维护，线宽恒为 1 屏幕像素。 */
#viewport {
  flex: 1 1 auto;
  position: relative;
  min-width: 0;
  overflow: hidden;
  background-color: #eef0f3;
  background-image:
    linear-gradient(rgba(0,0,0,.05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0,0,0,.05) 1px, transparent 1px);
  background-size: 20px 20px;   /* 初始值，JS 会按 scale 覆盖 */
  cursor: default;
}
#viewport.panning { cursor: grabbing; }
#viewport.can-pan { cursor: grab; }

/* 原点(0,0)十字坐标轴：加粗常驻，帮助用户定位画布原点。
   位于网格之上、#world 内容（画板/方框）之下，靠 DOM 顺序压在下层。 */
#axis-x, #axis-y { position: absolute; background: rgba(220,38,38,.5); pointer-events: none; }
#axis-x { left: 0; width: 100%; height: 2px; }
#axis-y { top: 0; height: 100%; width: 2px; }

/* 新手引导：画布左下角「? 新手引导」，默认只见触发器；点击触发器开/关完整教程面板 */
#guide-help { position: absolute; left: 14px; bottom: 14px; z-index: 20; }
#guide-trigger { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; }
#guide-btn {
  width: 24px; height: 24px; border-radius: 50%;
  border: 1px solid var(--border); background: #fff; color: var(--muted);
  font: inherit; font-size: 14px; line-height: 1; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 1px 3px rgba(0,0,0,.12);
}
#guide-label { font-size: 13px; color: var(--muted); background: #fff;
  padding: 2px 8px; border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0,0,0,.12); }
#guide-trigger:hover #guide-btn,
#guide-trigger:hover #guide-label,
#guide-help.open #guide-btn,
#guide-help.open #guide-label { color: var(--accent); border-color: var(--accent); }
/* 教程面板：点击触发器开/关（#guide-help.open）；内容较多，给浅底色与滚动以保证可读 */
#guide-overlay {
  position: absolute; left: 0; bottom: calc(100% + 8px);
  display: none; white-space: nowrap;
  background: #fff; border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 8px 28px rgba(0,0,0,.16);
  padding: 12px 16px; color: var(--text); font-size: 12px; line-height: 1.95;
  max-height: 72vh; max-width: 86vw; overflow: auto;
}
#guide-help.open #guide-overlay { display: block; }
#guide-overlay b { color: var(--text); }
#guide-overlay b.gh {   /* 阶段小标题 */
  display: block; margin: 10px 0 3px; color: var(--accent); font-size: 12px;
}
#guide-overlay b.gh:first-child { margin-top: 0; }

/* #world 只做平移；自身透明，让 viewport 的网格透出来。
   尺寸放得极大以容纳各种平移/缩放下的画板与方框定位。 */
#world {
  position: absolute;
  top: 0; left: 0;
  width: 200000px;
  height: 200000px;
  transform-origin: 0 0;
  will-change: transform;
  background: transparent;
}
/* 框都直接放进 #canvas（与 #world 同原点，零偏移） */
#canvas {
  position: absolute;
  top: 0; left: 0;
  width: 0; height: 0;   /* 仅作框的定位容器，自身不占视觉空间 */
}

/* 参考图底板：与 #canvas 同坐标系。z-index 介于画板白底(-2)与方框(≥1)之间，
   于是层序为「画板白底 → 参考图 → 方框轮廓」，正合描摹所需。
   默认 pointer-events:none，纯展示、绝不抢绘图点击；仅「参考图」工具激活时
   （body[data-tool="refimg"]）把底板提到最上并对未锁定的图开启命中（见下）。 */
#backdrop {
  position: absolute;
  top: 0; left: 0;
  width: 0; height: 0;
  z-index: -1;
  pointer-events: none;
}
/* 每张参考图是一个 .refimg 包裹层（承载 left/top/width/height 与 rotate/flip transform），
   内含一个填满的 <img> 与（选中时）手柄。img 自身不接事件，交互落在包裹层上。 */
#backdrop .refimg {
  position: absolute;
  transform-origin: center center;
  user-select: none;
  pointer-events: none;
}
#backdrop .refimg > img {
  width: 100%; height: 100%; display: block;
  transform-origin: center center;   /* 翻转(scaleX/Y)只作用于 img，绕中心镜像；手柄在包裹层上不受影响 */
  -webkit-user-drag: none; user-select: none; pointer-events: none;
}
body[data-tool="refimg"] #backdrop { z-index: 100000; }      /* 盖过所有方框，保证图在框下也点得中 */
body[data-tool="refimg"] #backdrop .refimg:not(.locked) { pointer-events: auto; cursor: move; }
/* 选中的参考图：蓝色描边 + 手柄（手柄随 transform 一起旋转/翻转，定位在图的局部坐标系） */
#backdrop .refimg.selected { outline: 1.5px solid var(--accent); outline-offset: 0; }
#backdrop .refimg .rot-handle {
  position: absolute; left: 50%; top: -28px; margin-left: -6px;
  width: 12px; height: 12px; border-radius: 50%;
  background: #fff; border: 1.5px solid var(--accent); cursor: grab;
}
#backdrop .refimg .rot-stem {
  position: absolute; left: 50%; top: -16px; margin-left: -1px;
  width: 2px; height: 16px; background: var(--accent);
}

/* ---------- Artboard（画板边界） ---------- */
.artboard {
  position: absolute;
  background-color: #fff;
  /* 画板内网格：自原点(0,0)起平铺，background-size 由 JS 随 scale 设置 */
  background-image:
    linear-gradient(rgba(0,0,0,.045) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0,0,0,.045) 1px, transparent 1px);
  border: 1px solid #c2c8d0;
  box-shadow: 0 1px 4px rgba(0,0,0,.08);
  pointer-events: none;          /* 命中交给框/viewport，画板本身不拦事件 */
  z-index: -2;                   /* 沉到参考图底板(-1)之下，让描摹底图可见 */
}
.artboard.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), 0 2px 8px rgba(37,99,235,.15);
}
.artboard-title {
  position: absolute; left: 0;
  color: var(--muted); white-space: nowrap;
  padding-bottom: 4px; user-select: none;
  pointer-events: auto;          /* 标题可点击：拖动它移动整个画板 */
  cursor: move;
}
.artboard.active .artboard-title { color: var(--accent); font-weight: 600; }

/* ---------- Box ---------- */
.box {
  position: absolute;
  border: 2px solid var(--box-stroke);
  background: var(--box-fill);
  border-radius: 4px;
  overflow: hidden;
  cursor: inherit;   /* 光标由 JS 按精确命中驱动（仅落在形状上才显示 move） */
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: center;
}
.box .box-label {
  position: absolute; top: 0; left: 0;
  font-size: 11px; line-height: 1; padding: 2px 6px;
  background: var(--box-stroke); color: #fff;
  border-bottom-right-radius: 4px;
  max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  pointer-events: none;
}
.box .box-desc {
  font-size: 13px; color: var(--text); text-align: center;
  padding: 18px 8px 8px; width: 100%;
  word-break: break-word; white-space: pre-wrap; pointer-events: none;
}
/* 右下角淡淡的坐标/尺寸/层级标注（font-size 由 JS 随 scale 设置） */
.box .box-meta {
  position: absolute; right: 0; bottom: 0;
  font-size: 10px; line-height: 1.2; padding: 1px 4px;
  color: var(--muted); background: rgba(255,255,255,.65);
  border-top-left-radius: 4px;
  white-space: nowrap; pointer-events: none; opacity: .7;
}
/* 就地编辑中的标签/描述：可点击、可选文本、显示光标，并高亮当前编辑区 */
.box .box-edit {
  pointer-events: auto;
  user-select: text; -webkit-user-select: text;
  cursor: text;
  outline: 2px solid var(--accent); outline-offset: -2px;
}
.box .box-desc.box-edit { background: rgba(255,255,255,.96); }

/* selection styling MUST NOT change z-index (z-order is data-driven) */
.box.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
/* 非活动画板里的框：只读弱化展示，点击会先切换画板 */
.box.dim { opacity: .5; cursor: pointer; }

/* resize handles */
.handle {
  position: absolute; width: 10px; height: 10px;
  background: #fff; border: 1.5px solid var(--accent); border-radius: 2px;
}
.handle.nw { top: -6px; left: -6px; cursor: nwse-resize; }
.handle.n  { top: -6px; left: 50%; margin-left: -5px; cursor: ns-resize; }
.handle.ne { top: -6px; right: -6px; cursor: nesw-resize; }
.handle.e  { top: 50%; right: -6px; margin-top: -5px; cursor: ew-resize; }
.handle.se { bottom: -6px; right: -6px; cursor: nwse-resize; }
.handle.s  { bottom: -6px; left: 50%; margin-left: -5px; cursor: ns-resize; }
.handle.sw { bottom: -6px; left: -6px; cursor: nesw-resize; }
.handle.w  { top: 50%; left: -6px; margin-top: -5px; cursor: ew-resize; }

/* 椭圆：盒身是透明的零圆角交互矩形（hover/命中由 JS 精确驱动，与矩形、多边形一致）；
   真实椭圆由非交互子层 .box-ellipse 用 border-radius:50% 绘制。溢出可见以显示选中高亮。 */
.box.shape-ellipse { border: none; background: none; overflow: visible; }
.box-ellipse {
  inset: 0;
  border: 2px solid var(--box-stroke);
  background: var(--box-fill);
  border-radius: 50%;
}
.box.shape-ellipse.selected { box-shadow: none; }
.box.shape-ellipse.selected .box-ellipse { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }

/* 多边形：盒身透明，真实轮廓由内嵌 svg 描出；溢出可见以便完整显示描边与顶点手柄 */
.box.shape-polygon { border: none; background: none; overflow: visible; }
.box-shape { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
.box.shape-polygon .box-shape polygon { fill: var(--box-fill); stroke: var(--box-stroke); stroke-width: 2; }
.box.shape-polygon .box-shape polyline { fill: none; stroke: var(--box-stroke); stroke-width: 2; }
.box.shape-polygon.selected { box-shadow: none; }   /* 轮廓变蓝即为选中，不再叠加矩形高亮 */
.box.shape-polygon.selected .box-shape > * { stroke: var(--accent); }

/* 多边形顶点手柄（圆形，区别于矩形缩放手柄） */
.vhandle {
  position: absolute; width: 10px; height: 10px; margin: -5px 0 0 -5px;
  background: var(--accent); border: 1.5px solid #fff; border-radius: 50%;
  cursor: pointer; z-index: 2;
}

/* 矩形/椭圆两次单击绘制的预览框（活在 world 坐标内，随画布平移/缩放） */
.shape-pending {
  position: absolute;
  box-sizing: border-box;
  border: 2px dashed var(--accent);
  background: var(--accent-soft);
  opacity: .5; pointer-events: none; z-index: 40;
}
.shape-pending.ellipse { border-radius: 50%; }

/* 多边形绘制预览：虚线折线 + 顶点点位 + 起点高亮（吸附闭合提示） */
.poly-pending { z-index: 40; }
.poly-pending-line { fill: none; stroke: var(--accent); stroke-width: 1.5; stroke-dasharray: 5 4; }
.poly-pending-dot { fill: var(--accent); }
.poly-pending-start { fill: #fff; stroke: var(--accent); stroke-width: 2; }

/* rubber band for new box (lives in viewport coords, not world) */
#rubber {
  position: absolute;
  border: 2px dashed var(--accent);
  background: var(--accent-soft);
  opacity: .5; pointer-events: none; display: none; z-index: 50;
}

/* ---------- Sidebar ---------- */
#sidebar {
  flex: 0 0 300px;
  background: var(--toolbar-bg);
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column;
  min-height: 0;
}
#sidebar section { padding: 12px 14px; border-bottom: 1px solid var(--border); }
#sidebar h3 { margin: 0 0 8px; font-size: 14px; display: flex; align-items: center; gap: 6px; }
#sidebar h3 .count {
  font-size: 11px; font-weight: 500; color: var(--muted);
  background: #eef1f5; border-radius: 10px; padding: 1px 8px;
}

/* Layers panel —— 与参考图、属性区按 flex 公平瓜分剩余高度，各自滚动（不再用百分比硬上限） */
#layers-section { flex: 1 1 0; min-height: 88px; display: flex; flex-direction: column; }
/* 列表是分区里唯一可伸缩/滚动的部分（min-height:0 才能在 flex 列里真正收缩并滚动，而非把兄弟挤出） */
.layer-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1 1 auto; min-height: 0; }
.layer-item {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px; border-radius: 6px; cursor: pointer; font-size: 13px;
}
.layer-item:hover { background: #f0f2f5; }
.layer-item.selected { background: var(--accent-soft); }
.layer-item .swatch {
  width: 12px; height: 12px; border: 2px solid var(--box-stroke);
  border-radius: 3px; flex: 0 0 auto;
}
.layer-item .swatch.swatch-ellipse { border-radius: 50%; }
.layer-item .swatch.swatch-polygon {
  border: none; background: var(--box-stroke); border-radius: 0;
  clip-path: polygon(50% 0, 100% 100%, 0 100%);   /* 三角形示意多边形 */
}
.layer-item .name { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer-item .name.untitled { color: var(--muted); font-style: italic; }
.layer-empty { color: var(--muted); font-size: 13px; padding: 4px 8px; }

/* 画板提示词（四等分之一，文本框填满该栏） */
#prompt-section { flex: 1 1 0; min-height: 96px; position: relative; display: flex; flex-direction: column; }
#board-prompt { flex: 1 1 auto; min-height: 64px; }
#board-prompt:disabled { background: #f3f4f6; color: var(--muted); }
.mention-pop {
  position: absolute; z-index: 60; display: none;
  background: #fff; border: 1px solid var(--border); border-radius: 6px;
  box-shadow: 0 6px 20px rgba(0,0,0,.14);
  max-height: 180px; overflow-y: auto;
}
.mention-pop .mi { padding: 6px 10px; font-size: 13px; cursor: pointer; white-space: nowrap; }
.mention-pop .mi.active, .mention-pop .mi:hover { background: var(--accent-soft); }

/* 第 4 栏「属性」：随选中切换（盒子属性 / 参考图属性 / 占位）；整体可滚，内容不溢出 */
#detail-section { flex: 1 1 0; min-height: 120px; overflow-y: auto; }
#sidebar label { display: block; font-size: 12px; color: var(--muted); margin: 12px 0 4px; }
#sidebar input[type=text], #sidebar textarea {
  width: 100%; font: inherit; font-size: 13px;
  padding: 7px 9px; border: 1px solid var(--border); border-radius: 6px; resize: vertical;
}
#sidebar input[type=range] { width: 100%; margin: 2px 0; }
#sidebar textarea { min-height: 96px; }

/* 参考图层（四等分之一）：图片列表分区，列表伸缩+滚动，提示保持自然高度 */
#refimg-list-section { flex: 1 1 0; min-height: 88px; display: flex; flex-direction: column; }
#refimg-list-section .hint-sm { flex: 0 0 auto; }
#refimg-list { flex: 1 1 auto; min-height: 40px; }
.hint-sm { margin: 0 0 8px; font-size: 11px; color: var(--muted); line-height: 1.5; }
.layer-item .swatch.swatch-refimg { border-radius: 2px; background: var(--accent-soft); border-color: var(--accent); }
.layer-item.hidden .name { color: var(--muted); text-decoration: line-through; }
.layer-item.locked-row .name { color: var(--muted); }
/* 行内锁图标：未锁=灰且淡的「开锁」幽灵，已锁=蓝色实心徽标的「闭锁」，对比强、一眼可辨 */
.layer-item .lockmark {
  flex: 0 0 auto; cursor: pointer;
  font-size: 12px; line-height: 1;
  padding: 2px 4px; border-radius: 4px;
  opacity: .3; filter: grayscale(1);
  transition: opacity .12s, background .12s;
}
.layer-item:hover .lockmark { opacity: .65; }
.layer-item .lockmark:hover { opacity: 1; background: #e5e9f0; }
.layer-item .lockmark.locked {
  opacity: 1; filter: none;   /* 已锁=满色实心闭锁；未锁=灰且淡的开锁（无底色，仅靠图标与浓淡区分） */
}
.empty { color: var(--muted); font-size: 13px; line-height: 1.7; }
.meta { font-size: 12px; color: var(--muted); margin-top: 14px; line-height: 1.6; }
.row { display: flex; gap: 6px; margin-top: 14px; flex-wrap: wrap; }
.row button { flex: 1 1 auto; padding: 6px 8px; }
#sidebar label.checkbox {
  display: flex; align-items: center; gap: 6px; margin: 0;
  color: var(--text); font-size: 12px; cursor: pointer; flex: 1 1 auto;
}
#sidebar label.checkbox input { width: auto; }

kbd {
  font-family: inherit; font-size: 11px;
  background: #eef1f5; border: 1px solid var(--border);
  border-radius: 3px; padding: 1px 5px;
}

/* ---------- 画板选择器 + 新建对话框 ---------- */
#board-select {
  font: inherit; font-size: 13px; padding: 5px 8px;
  border: 1px solid var(--border); border-radius: 6px;
  background: #fff; color: var(--text); cursor: pointer; max-width: 220px;
}
#board-dialog {
  border: 1px solid var(--border); border-radius: 10px; padding: 18px 20px;
  width: 340px; max-width: 90vw; color: var(--text);
  box-shadow: 0 10px 40px rgba(0,0,0,.18);
}
#board-dialog::backdrop { background: rgba(0,0,0,.25); }
#board-dialog h3 { font-size: 15px; }
#board-dialog label { display: block; font-size: 12px; color: var(--muted); margin: 12px 0 4px; }
#board-dialog input[type=text], #board-dialog input[type=number], #board-dialog select {
  width: 100%; font: inherit; font-size: 13px;
  padding: 7px 9px; border: 1px solid var(--border); border-radius: 6px;
}
#board-dialog .bd-size { display: flex; gap: 12px; }
#board-dialog .bd-size span { flex: 1 1 0; }
#board-dialog .bd-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

@media (max-width: 920px) {
  body { overflow: auto; }
  #toolbar { align-items: flex-start; }
  #main {
    flex-direction: column;
    min-height: calc(100vh - 96px);
  }
  #viewport {
    flex: 1 1 58vh;
    min-height: 420px;
  }
  #sidebar {
    flex: 0 0 auto;
    border-left: 0;
    border-top: 1px solid var(--border);
    max-height: none;
  }
  /* 窄屏：侧栏转底部堆叠，各栏改为自然高度（列表设上限可滚），不再四等分 */
  #prompt-section { flex: 0 0 auto; }
  #layers-section { flex: 0 0 auto; max-height: 220px; }
  #refimg-list-section { flex: 0 0 auto; max-height: 220px; }
  #detail-section { flex: 0 0 auto; }
}

@media (max-width: 560px) {
  #toolbar { gap: 6px; padding: 8px; }
  #toolbar .title { flex-basis: 100%; }
  #toolbar .sep { display: none; }
  #board-select { flex: 1 1 160px; max-width: none; }
  button.tool { padding: 6px 9px; }
  #viewport { min-height: 360px; }
  #sidebar section { padding: 12px; }
}
