/* Procreate 风格：全屏画板 + 半透明深色毛玻璃浮层 */
:root {
  --glass: rgba(28, 28, 30, 0.78);
  --glass-2: rgba(44, 44, 48, 0.85);
  --line: rgba(255, 255, 255, 0.08);
  --fg: #f2f2f4;
  --fg-dim: rgba(242, 242, 244, 0.55);
  --accent: #2f7bff;
  --lock: #ff9f0a;
  --ok: #30d158;
  --bad: #ff453a;
  --radius: 14px;
  --sa-l: env(safe-area-inset-left, 0px);
  --sa-r: env(safe-area-inset-right, 0px);
  --sa-t: env(safe-area-inset-top, 0px);
  --sa-b: env(safe-area-inset-bottom, 0px);
  --top-h: 44px;
  --panel-w: min(330px, 36vw);
  --ui-scale: 1;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none;
  background: #f4f3ef; color: var(--fg);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", sans-serif;
  -webkit-user-select: none; user-select: none; touch-action: none;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
svg { display: block; }
[hidden] { display: none !important; }
.muted { color: var(--fg-dim); font-size: 12px; }
.hidden-input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
/* 可被 label / 程序点击激活；不要用 pointer-events:none 或 display:none，否则 Android PWA 相机调不起来 */
.visually-hidden-input {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); clip-path: inset(50%);
  white-space: nowrap; border: 0;
}

.glass {
  background: var(--glass);
  -webkit-backdrop-filter: blur(22px) saturate(1.4);
  backdrop-filter: blur(22px) saturate(1.4);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.28);
}

/* ---------- 画板 ---------- */
.board {
  position: fixed; inset: 0; width: 100vw; height: 100vh; height: 100dvh;
  background: radial-gradient(ellipse at 50% 40%, #fbfaf7 0%, #f1efe9 100%);
  touch-action: none; cursor: crosshair;
}
.board[data-tool="select"] { cursor: default; }
.board[data-tool="rotate"] { cursor: grab; }
.board[data-tool="highlight"] { cursor: pointer; }
.board[data-tool="eraser"] { cursor: cell; }
.board[data-tool="doodle"] { cursor: crosshair; }
.board .pt-unlocked { fill: #fff; stroke: var(--lock); stroke-width: 2.4; }
.board .pt-unlocked-ring { fill: none; stroke: var(--lock); stroke-width: 2; opacity: 0.9; }
.board .pt-construct { fill: #fff; stroke: #1a5f4a; stroke-width: 2; }
.board .pt-construct-label { fill: #1a5f4a; }
.board .rot-handle { fill: #fff; stroke: var(--accent); stroke-width: 2.2; }
.board .rot-stem { stroke: var(--accent); stroke-width: 1.6; stroke-dasharray: 3 3; fill: none; }
.board .rot-arrow { fill: none; stroke: var(--accent); stroke-width: 1.6; stroke-linecap: round; }
.board .tri-label {
  font-size: 13px; font-weight: 700; text-anchor: middle; dominant-baseline: central;
  paint-order: stroke; stroke: #f7f6f2; stroke-width: 4px;
}
.board .seg { stroke: #1c1c1e; stroke-width: 2.4; stroke-linecap: round; }
.board .original-curve { fill: none; stroke: #53677e; stroke-width: 2; }
.board .seg.original-dash { stroke: #9a500c; stroke-dasharray: 7 5; }
.board .original-label { fill: #174e9b; font-weight: 700; text-anchor: middle; dominant-baseline: central; paint-order: stroke; stroke: #f4f3ef; stroke-width: 3px; }
.board .pt { fill: #1c1c1e; stroke: #fff; stroke-width: 2; }
.board .pt-label {
  fill: #1c1c1e; font-size: 17px; font-weight: 600; font-style: italic; text-anchor: middle;
  font-family: "Times New Roman", "STIXGeneral", serif; paint-order: stroke; stroke: #f7f6f2; stroke-width: 4px;
}
.board .angle-label { font-size: 13px; font-weight: 600; text-anchor: middle; paint-order: stroke; stroke: #f7f6f2; stroke-width: 4px; }
.board .snap-ring { fill: rgba(47, 123, 255, 0.12); stroke: var(--accent); stroke-width: 2; }
.board .handle { fill: #fff; stroke: var(--accent); stroke-width: 2.5; }
.board .sel-glow { stroke: rgba(47, 123, 255, 0.28); stroke-linecap: round; }
.board .sel-glow-c { fill: rgba(47, 123, 255, 0.12); stroke: rgba(47, 123, 255, 0.5); stroke-width: 1.5; }
.board .pick-dot { fill: #ff2d55; stroke: #fff; stroke-width: 2; }
.board .pick-ray { stroke: #ff2d55; stroke-width: 1.5; stroke-dasharray: 4 4; }
.board .pick-num { fill: #ff2d55; font-size: 12px; font-weight: 700; }
.board .shake { animation: shake 0.42s; transform-box: fill-box; transform-origin: center; }
@keyframes shake {
  0%, 100% { transform: translateX(0); fill: #1c1c1e; }
  20% { transform: translateX(-5px); fill: var(--bad); }
  40% { transform: translateX(5px); fill: var(--bad); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(3px); }
}

/* ---------- 顶栏 ---------- */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; height: calc(var(--top-h) + var(--sa-t));
  padding: var(--sa-t) calc(10px + var(--sa-r)) 0 calc(10px + var(--sa-l));
  display: flex; align-items: center; justify-content: space-between; z-index: 20;
}
.glass-bar {
  background: linear-gradient(rgba(18, 18, 20, 0.88), rgba(18, 18, 20, 0.72));
  -webkit-backdrop-filter: blur(20px); backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--line);
}
.tb-left, .tb-right { display: flex; align-items: center; gap: 4px; min-width: 0; }
.tb-text { padding: 6px 10px; border-radius: 8px; font-weight: 500; letter-spacing: 0.5px; }
.tb-icon {
  height: 34px; min-width: 34px; padding: 0 7px; border-radius: 9px;
  display: inline-flex; align-items: center; justify-content: center; gap: 5px; color: var(--fg);
  cursor: pointer;
}
.tb-icon svg, .tool svg, .side-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linejoin: round; stroke-linecap: round; }
.tb-text:active, .tb-icon:active, .tool:active, .side-btn:active { background: rgba(255, 255, 255, 0.12); }
.tb-icon.active { color: var(--accent); }
.tb-ai span { font-size: 13px; }
.tb-ai.busy svg { animation: spin 1.1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.tb-title {
  margin-left: 8px; color: var(--fg-dim); font-size: 12.5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 30vw;
}
.color-dot {
  width: 28px; height: 28px; border-radius: 50%; margin-left: 6px;
  border: 2px solid rgba(255, 255, 255, 0.85); box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.4);
}

/* ---------- 左侧 ---------- */
.left-dock {
  position: fixed; z-index: 15;
  left: calc(8px + var(--sa-l)); top: calc(var(--top-h) + var(--sa-t) + 10px); bottom: calc(10px + var(--sa-b));
  display: flex; gap: 6px; align-items: flex-start; pointer-events: none;
}
.left-dock > * { pointer-events: auto; }
.toolbar { display: flex; flex-direction: column; padding: 5px; gap: 2px; }
.tool {
  width: 40px; height: 40px; border-radius: 10px; display: flex; align-items: center; justify-content: center;
  color: var(--fg-dim); transition: color 0.15s, background 0.15s;
}
.tool.active { color: var(--accent); background: rgba(47, 123, 255, 0.14); }

.sidebar {
  width: 40px; padding: 10px 0; display: flex; flex-direction: column; align-items: center; gap: 10px;
  border-radius: 12px;
}
.vslider { width: 40px; height: clamp(70px, calc((100dvh - 290px) / 2), 170px); display: flex; justify-content: center; touch-action: none; cursor: ns-resize; }
.vs-track { position: relative; width: 6px; height: 100%; border-radius: 3px; background: rgba(255, 255, 255, 0.16); }
.vs-fill { position: absolute; left: 0; right: 0; bottom: 0; border-radius: 3px; background: rgba(255, 255, 255, 0.7); }
.vs-thumb {
  position: absolute; left: 50%; width: 22px; height: 14px; margin-left: -11px; margin-bottom: -7px;
  border-radius: 7px; background: #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.45);
}
.vslider.dragging .vs-thumb { transform: scale(1.15); }
.side-square {
  width: 22px; height: 22px; border-radius: 5px; border: 1.5px solid rgba(255, 255, 255, 0.6);
  background: rgba(255, 255, 255, 0.06);
}
.side-square.on { background: var(--accent); border-color: var(--accent); }
.side-btn { width: 36px; height: 32px; border-radius: 8px; display: flex; align-items: center; justify-content: center; }
.side-btn:disabled { opacity: 0.3; }

/* 工具选项浮窗 */
.flyout {
  position: fixed; z-index: 16; left: calc(108px + var(--sa-l)); top: calc(var(--top-h) + var(--sa-t) + 10px);
  padding: 10px; min-width: 190px; max-width: 280px;
}
.flyout h4 { margin: 2px 4px 8px; font-size: 12px; font-weight: 500; color: var(--fg-dim); }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip-btn {
  padding: 6px 10px; border-radius: 8px; background: rgba(255, 255, 255, 0.08); font-size: 13px;
}
.chip-btn.on { background: var(--accent); }

/* ---------- 右侧面板 ---------- */
.panel-right {
  position: fixed; z-index: 15;
  right: calc(8px + var(--sa-r)); top: calc(var(--top-h) + var(--sa-t) + 10px); bottom: calc(10px + var(--sa-b));
  width: var(--panel-w); display: flex; flex-direction: column; gap: 8px; pointer-events: none;
  transition: transform 0.25s ease, opacity 0.25s ease;
}
.panel-right > * { pointer-events: auto; }
.panel-right.collapsed { transform: translateX(calc(100% + 20px)); opacity: 0; }
.pr-results { flex: 1 1 56%; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.pr-layers { flex: 1 1 44%; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.panel-right.only-results .pr-results { flex-basis: 100%; }
.panel-right.only-layers .pr-layers { flex-basis: 100%; }

.seg-tabs { display: flex; margin: 8px 8px 4px; padding: 2px; border-radius: 9px; background: rgba(255, 255, 255, 0.07); }
.seg { flex: 1; padding: 5px 0; border-radius: 7px; font-size: 13px; color: var(--fg-dim); }
.seg.active { background: rgba(255, 255, 255, 0.16); color: var(--fg); }
.tab-body { flex: 1; overflow-y: auto; padding: 6px 12px 12px; -webkit-overflow-scrolling: touch; touch-action: pan-y; user-select: text; -webkit-user-select: text; }
.problem-text { margin: 4px 0 8px; font-size: 14px; line-height: 1.65; }
.photo-thumb { width: 100%; max-height: 110px; object-fit: contain; border-radius: 8px; background: #000; cursor: zoom-in; }
.status { font-size: 12px; color: var(--fg-dim); margin: 6px 0 0; }

.c-group h3 { font-size: 12px; font-weight: 500; color: var(--fg-dim); margin: 10px 0 6px; letter-spacing: 0.3px; }
.c-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 5px; }
.c-item { display: flex; gap: 8px; align-items: flex-start; padding: 7px 9px; border-radius: 9px; background: rgba(255, 255, 255, 0.05); font-size: 13px; }
.c-item .ico { flex: none; width: 16px; text-align: center; }
.c-item .txt { flex: 1; }
.c-item .chk { flex: none; font-size: 11px; padding: 1px 6px; border-radius: 6px; white-space: nowrap; }
.chk.ok { color: var(--ok); background: rgba(48, 209, 88, 0.12); }
.chk.bad { color: var(--bad); background: rgba(255, 69, 58, 0.12); }
.c-item.lock .ico { color: var(--lock); }
.c-item.ok .ico { color: var(--ok); }
.c-item.forbid .ico { color: var(--bad); }

.goal-line { font-size: 13px; margin: 6px 0 8px; color: var(--fg); }
.hint-step { border-radius: 10px; background: rgba(255, 255, 255, 0.05); margin-bottom: 6px; overflow: hidden; }
.hint-step summary { list-style: none; padding: 8px 10px; display: flex; gap: 8px; align-items: center; cursor: pointer; font-size: 13px; }
.hint-step summary::-webkit-details-marker { display: none; }
.hint-step .num { flex: none; width: 20px; height: 20px; border-radius: 50%; background: rgba(255, 255, 255, 0.12); font-size: 11px; display: grid; place-items: center; }
.hint-step[open] .num { background: var(--accent); }
.hint-step summary::after { content: "›"; margin-left: auto; color: var(--fg-dim); transition: transform 0.2s; }
.hint-step[open] summary::after { transform: rotate(90deg); }
.hint-step p { margin: 0; padding: 0 12px 10px 38px; font-size: 13px; line-height: 1.6; color: rgba(242, 242, 244, 0.85); }
.hint-step.answer summary { color: #ffd60a; }
.next-hint { width: 100%; padding: 8px; border-radius: 9px; background: rgba(47, 123, 255, 0.2); color: #9cc2ff; font-size: 13px; }

.pr-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 6px; }
.pr-head h2 { margin: 0; font-size: 15px; font-weight: 600; }
.icon-plus { width: 28px; height: 28px; border-radius: 8px; font-size: 20px; line-height: 1; }
.icon-plus:active { background: rgba(255, 255, 255, 0.12); }
.add-menu { display: flex; gap: 6px; padding: 0 10px 8px; flex-wrap: wrap; }
.add-menu button { padding: 5px 9px; border-radius: 8px; background: rgba(255, 255, 255, 0.08); font-size: 12px; }

.layer-list { list-style: none; margin: 0; padding: 0 8px 8px; overflow-y: auto; flex: 1; touch-action: pan-y; }
.layer {
  display: flex; align-items: center; gap: 8px; padding: 6px; margin-bottom: 5px; border-radius: 10px;
  background: rgba(255, 255, 255, 0.05); border: 1px solid transparent; cursor: pointer;
}
.layer.active { background: rgba(47, 123, 255, 0.85); }
.layer.hidden-layer .l-thumb, .layer.hidden-layer .l-name { opacity: 0.45; }
.l-thumb { flex: none; width: 54px; height: 38px; border-radius: 5px; background: #f7f6f2; overflow: hidden; }
.l-thumb svg { width: 100%; height: 100%; }
.l-meta { flex: 1; min-width: 0; }
.l-name { font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.l-sub { font-size: 11px; color: var(--fg-dim); }
.layer.active .l-sub { color: rgba(255, 255, 255, 0.75); }
.l-act { flex: none; width: 26px; height: 26px; border-radius: 6px; display: grid; place-items: center; font-size: 13px; color: var(--fg-dim); }
.layer.active .l-act { color: #fff; }
.l-act:active { background: rgba(255, 255, 255, 0.15); }
.l-vis { width: 20px; height: 20px; border-radius: 5px; border: 1.5px solid rgba(255, 255, 255, 0.55); display: grid; place-items: center; font-size: 12px; }
.l-vis.on { background: #fff; color: #111; border-color: #fff; }
.l-items { list-style: none; margin: -2px 0 6px 8px; padding: 0; }
.l-items li { display: flex; align-items: center; gap: 6px; padding: 4px 8px; font-size: 12px; color: var(--fg-dim); border-radius: 7px; }
.l-items li.sel { color: var(--fg); background: rgba(47, 123, 255, 0.25); }
.l-items .sw { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.l-items .grow { flex: 1; cursor: pointer; }

/* ---------- HUD / Toast ---------- */
.hud, .toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 30; pointer-events: none;
  padding: 7px 14px; border-radius: 18px; font-size: 13px; white-space: nowrap; max-width: 60vw; overflow: hidden; text-overflow: ellipsis;
  background: rgba(28, 28, 30, 0.82); -webkit-backdrop-filter: blur(18px); backdrop-filter: blur(18px);
  transition: opacity 0.25s; opacity: 0;
}
.hud { top: calc(var(--top-h) + var(--sa-t) + 10px); }
.toast { top: calc(var(--top-h) + var(--sa-t) + 50px); background: rgba(60, 30, 0, 0.88); color: #ffd28a; }
.hud.show, .toast.show { opacity: 1; }

/* ---------- 弹窗 ---------- */
.popover { position: fixed; z-index: 25; padding: 12px; }
#color-pop { right: calc(10px + var(--sa-r)); top: calc(var(--top-h) + var(--sa-t) + 6px); width: 214px; }
.swatches { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; }
.swatches button { width: 30px; height: 30px; border-radius: 50%; border: 2px solid transparent; }
.swatches button.on { border-color: #fff; box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.5) inset; }
.dash-toggle { display: flex; align-items: center; gap: 6px; margin-top: 12px; font-size: 13px; }
.library { left: calc(10px + var(--sa-l)); top: calc(var(--top-h) + var(--sa-t) + 6px); width: 280px; max-height: 70vh; overflow: auto; }
.library h3 { margin: 0 0 8px; font-size: 14px; }
.library ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.library li button { width: 100%; text-align: left; padding: 8px 10px; border-radius: 9px; background: rgba(255, 255, 255, 0.07); font-size: 13px; }
.library li button.on { outline: 1.5px solid var(--accent); }

.ref-win { position: fixed; z-index: 18; left: min(120px, 18vw); bottom: max(12px, var(--sa-b)); width: min(320px, 82vw); height: min(calc(44vh + 36px), 326px); display: flex; flex-direction: column; overflow: hidden; padding: 0; }
.ref-bar { display: flex; justify-content: space-between; align-items: center; flex: 0 0 auto; padding: 3px 6px 3px 10px; font-size: 12px; color: var(--fg); cursor: move; touch-action: none; }
.ref-controls { display: flex; gap: 2px; }
.ref-controls button { min-width: 28px; min-height: 30px; padding: 0 4px; border-radius: 5px; }
.ref-controls button:hover, .ref-controls button:focus-visible { background: rgba(255,255,255,.18); }
.ref-controls button:disabled { opacity: .45; cursor: default; }
.ref-viewport { position: relative; flex: 1; min-height: 0; overflow: hidden; background: #101010; touch-action: none; cursor: grab; }
.ref-viewport:active { cursor: grabbing; }
.ref-viewport img { display: block; width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.ref-resize { position: absolute; right: 0; bottom: 0; width: 22px; height: 22px; cursor: nwse-resize; touch-action: none; }
.ref-resize::before { content: ""; position: absolute; right: 5px; bottom: 5px; width: 10px; height: 10px; border-right: 2px solid rgba(255,255,255,.7); border-bottom: 2px solid rgba(255,255,255,.7); }

/* ---------- 竖屏 ---------- */
.rotate-hint { display: none; }
@media (orientation: portrait) {
  body:not(.allow-portrait) .rotate-hint {
    display: grid; place-items: center; position: fixed; inset: 0; z-index: 100;
    background: rgba(12, 12, 14, 0.92); -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
  }
  .rh-card { text-align: center; padding: 24px; }
  .rh-card p { margin: 6px 0; }
  .rh-icon { width: 84px; height: 84px; margin: 0 auto 10px; fill: none; stroke: #fff; stroke-width: 2.2; stroke-linecap: round; animation: tilt 2s ease-in-out infinite; }
  @keyframes tilt { 0%, 30% { transform: rotate(0); } 60%, 100% { transform: rotate(-90deg); } }
  .rh-card button { margin: 16px 3px 0; padding: 9px 16px; border-radius: 10px; background: rgba(255, 255, 255, 0.12); }
  .rh-card #btn-landscape { background: #245fc7; color: #fff; }
  /* 竖屏降级：右侧面板变底部抽屉，左侧工具缩短 */
  body.allow-portrait .panel-right {
    top: auto; left: calc(60px + var(--sa-l)); right: calc(8px + var(--sa-r)); width: auto; height: 42dvh;
  }
  body.allow-portrait .panel-right.collapsed { transform: translateY(calc(100% + 20px)); }
  body.allow-portrait .tb-title, body.allow-portrait .tb-ai span { display: none; }
}

/* 低矮横屏（手机横置） */
@media (max-height: 430px) {
  :root { --top-h: 40px; }
  .tool { width: 36px; height: 36px; }
  .toolbar { padding: 4px; }
  .sidebar { gap: 7px; padding: 8px 0; }
  .side-btn { height: 28px; }
  .tab-body { padding: 4px 10px 10px; }
  .l-thumb { width: 46px; height: 32px; }
}

/* ---------- 来源标签：AI / 模拟 ---------- */
.src-tag {
  display: inline-block; margin-left: 8px; padding: 1px 6px; border-radius: 6px;
  font-size: 11px; line-height: 16px; font-weight: 600; vertical-align: middle; white-space: nowrap;
}
.src-tag.ai { background: rgba(52, 199, 89, 0.18); color: #34c759; }
.src-tag.mock { background: rgba(255, 149, 0, 0.18); color: #ff9500; }
.library li button .src-tag { margin: 0 6px 0 0; }
.library #btn-mock { margin-top: 8px; }
body.ai-busy .tb-ai { color: var(--accent); }

/* ---------- 应用内相机（PWA 拍照） ---------- */
.camera-overlay {
  position: fixed; inset: 0; z-index: 220;
  background: #000;
  display: flex; flex-direction: column;
}
.camera-overlay[hidden] { display: none !important; }
.camera-overlay video {
  flex: 1; width: 100%; height: 100%; object-fit: cover; background: #000;
  touch-action: none;
}
.camera-bar {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 12px calc(20px + var(--sa-r)) calc(18px + var(--sa-b)) calc(20px + var(--sa-l));
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.68));
  pointer-events: none;
}
.camera-bar > * { pointer-events: auto; }
.camera-cancel {
  position: absolute; left: calc(16px + var(--sa-l)); bottom: calc(22px + var(--sa-b));
  color: #fff; font-size: 16px; padding: 10px 14px; min-height: 44px;
}
.camera-shutter {
  width: 68px; height: 68px; border-radius: 50%;
  background: #fff; border: 4px solid rgba(255, 255, 255, 0.35);
  box-shadow: 0 0 0 3px #fff; margin-bottom: 4px;
}
.camera-shutter:active { transform: scale(0.94); }
