:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --text: #1d1d1f;
  --text-2: #6e6e73;
  --text-3: #8e8e93;
  --hairline: rgba(0, 0, 0, 0.1);
  --fill: rgba(118, 118, 128, 0.12);
  --accent: #0071e3;
  --danger: #c4231b;
  --paper: #ffffff;
  --radius: 8px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #000000;
  --surface: #1c1c1e;
  --text: #f5f5f7;
  --text-2: #a1a1a6;
  --text-3: #8e8e93;
  --hairline: rgba(255, 255, 255, 0.12);
  --fill: rgba(118, 118, 128, 0.24);
  --accent: #2997ff;
  --danger: #ff6961;
  --paper: #f2f2f2;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #000000;
    --surface: #1c1c1e;
    --text: #f5f5f7;
    --text-2: #a1a1a6;
    --text-3: #8e8e93;
    --hairline: rgba(255, 255, 255, 0.12);
    --fill: rgba(118, 118, 128, 0.24);
    --accent: #2997ff;
    --danger: #ff6961;
    --paper: #f2f2f2;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { display: grid; grid-template-columns: 320px 1fr; overflow: hidden; }

/* ---------- side panel ---------- */
.panel {
  display: flex; flex-direction: column; min-height: 0;
  background: var(--surface);
  border-right: 1px solid var(--hairline);
  padding-left: env(safe-area-inset-left);
}
.panel-head { padding: 24px 16px 8px 24px; display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.theme-btn {
  width: 44px; height: 44px; margin-top: -8px; padding: 0; flex: none;
  display: grid; place-items: center; background: transparent; color: var(--text-2);
}
.theme-btn:hover { background: var(--fill); color: var(--text); }
.theme-btn .i-sun { display: none; }
.theme-btn[aria-pressed="true"] .i-sun { display: block; }
.theme-btn[aria-pressed="true"] .i-moon { display: none; }
h1 { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; margin: 0; }
.subtitle { font-size: 13px; color: var(--text-2); margin: 4px 0 0; }

form { flex: 1; overflow-y: auto; padding: 0 24px 16px; }
section { padding: 16px 0; border-bottom: 1px solid var(--hairline); }
section:last-child { border-bottom: 0; }
h2 { font-size: 11px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-3); margin: 0 0 12px; }

.row.two { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }

.seg { display: flex; background: var(--fill); border-radius: var(--radius); padding: 2px; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label {
  flex: 1; text-align: center; font-size: 13px; font-weight: 500;
  min-height: 32px; display: grid; place-items: center; border-radius: 6px; cursor: pointer;
  color: var(--text-2); transition: background .15s, color .15s;
}
.seg input:checked + label { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.seg input:focus-visible + label { outline: 2px solid var(--accent); outline-offset: 1px; }

.fields { display: grid; gap: 8px; }
.field {
  display: flex; align-items: center; gap: 8px;
  min-height: 44px; padding: 0 12px;
  background: var(--fill); border-radius: var(--radius);
  font-size: 15px; margin-bottom: 8px;
}
.fields .field { margin-bottom: 0; }
.field span { flex: 1; color: var(--text); }
.field input, .field select {
  width: 96px; border: 0; background: transparent; color: var(--text);
  font: inherit; font-variant-numeric: tabular-nums; text-align: right; outline: none;
  -moz-appearance: textfield;
}
.field input::-webkit-inner-spin-button, .field input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.field:focus-within { box-shadow: inset 0 0 0 2px var(--accent); }
.field .unit { font-style: normal; color: var(--text-3); font-size: 13px; width: 20px; }
.field.select select { width: auto; text-align-last: right; cursor: pointer; appearance: none; padding-right: 2px; }
.field.select::after { content: "⌄"; color: var(--text-3); font-size: 13px; margin-top: -6px; }

.hint { font-size: 12px; color: var(--text-3); margin: 8px 0 0; line-height: 1.4; }
.errors { font-size: 13px; color: var(--danger); margin: 8px 0 0; line-height: 1.4; }
.errors:empty { display: none; }
.field.invalid { box-shadow: inset 0 0 0 1.5px var(--danger); }

.toggle {
  display: flex; align-items: center; justify-content: space-between;
  min-height: 44px; font-size: 15px; cursor: pointer;
}
.toggle input { position: absolute; opacity: 0; }
.toggle i {
  width: 44px; height: 26px; border-radius: 13px; background: var(--fill);
  position: relative; transition: background .2s;
}
.toggle i::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 22px; height: 22px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s;
}
.toggle input:checked + i { background: #34c759; }
.toggle input:checked + i::after { transform: translateX(18px); }
.toggle input:focus-visible + i { outline: 2px solid var(--accent); outline-offset: 2px; }

.summary { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0; font-size: 13px; }
.summary dt { color: var(--text-2); }
.summary dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

.panel-foot {
  padding: 16px 24px calc(16px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--hairline);
  display: flex; align-items: center; gap: 12px;
}
.foot-label { font-size: 13px; color: var(--text-2); }
.exports { display: flex; gap: 8px; flex: 1; justify-content: flex-end; }
button {
  font: inherit; font-size: 13px; font-weight: 600; color: var(--text);
  background: var(--fill); border: 0; border-radius: var(--radius);
  min-height: 36px; min-width: 52px; padding: 0 12px; cursor: pointer;
}
button:hover { background: rgba(118,118,128,.2); }
button:active { opacity: .7; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.exports button[data-export="pdf"] { background: var(--accent); color: #fff; }

/* ---------- canvas ---------- */
.canvas { position: relative; overflow: hidden; container: canvas / inline-size; background: var(--bg); cursor: grab; touch-action: none; }
.canvas.dragging { cursor: grabbing; }
.stage { position: absolute; inset: 0; }
.stage svg { width: 100%; height: 100%; display: block; user-select: none; -webkit-user-select: none; }
.stage svg #cut, .stage svg #crease, .stage svg #bleed, .stage svg #dimensions { stroke-width: 1.25px; }
.stage svg #dimensions { stroke-width: 0.75px; }
.stage svg path, .stage svg line { vector-effect: non-scaling-stroke; }
.stage .sheet { fill: var(--paper); }

.legend {
  position: absolute; left: 16px; bottom: calc(16px + env(safe-area-inset-bottom));
  display: flex; gap: 16px; padding: 8px 12px; font-size: 12px; color: var(--text-2);
  background: var(--surface); border: 1px solid var(--hairline); border-radius: var(--radius);
}
.legend span { display: flex; align-items: center; gap: 6px; }
.sw { width: 18px; height: 0; border-top: 1.5px solid; }
.sw.cut { border-color: var(--text); }
.sw.crease { border-color: #e5322d; border-top-style: dashed; }
.sw.bleed { border-color: #1f9d55; }
.sw.dim { border-color: #1a6fd1; }

.zoom {
  position: absolute; right: calc(16px + env(safe-area-inset-right)); bottom: calc(16px + env(safe-area-inset-bottom));
  display: flex; background: var(--surface); border: 1px solid var(--hairline); border-radius: var(--radius); overflow: hidden;
}
.zoom button { background: transparent; border-radius: 0; min-width: 44px; min-height: 36px; font-size: 16px; font-weight: 500; }
.zoom .rot { min-width: 40px; display: grid; place-items: center; color: var(--text-2); }
.zoom .rot:hover { color: var(--text); background: var(--fill); }
.zoom #rot-cw { border-right: 1px solid var(--hairline); }
.zoom .pct { font-size: 12px; min-width: 60px; font-variant-numeric: tabular-nums; border-left: 1px solid var(--hairline); border-right: 1px solid var(--hairline); }

@media (max-width: 760px) {
  body { grid-template-columns: 1fr; grid-template-rows: 55vh 45vh; overflow: auto; }
  .canvas { grid-row: 1; }
  .panel { grid-row: 2; border-right: 0; border-top: 1px solid var(--hairline); }
  .legend { display: none; }
}

/* ---------- view modes ---------- */
.modes {
  position: absolute; top: calc(16px + env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
  z-index: 3; width: 200px; background: var(--surface); border: 1px solid var(--hairline);
}
.modes label:hover { color: var(--text); }
.canvas input[type="radio"] { position: absolute; opacity: 0; pointer-events: none; }
.view3d { position: absolute; inset: 0; display: none; }
.view3d canvas { display: block; width: 100%; height: 100%; outline: none; }
.canvas[data-mode="3d"] .view3d { display: block; }
.canvas[data-mode="3d"] .stage, .canvas[data-mode="3d"] .only-2d { display: none; }
.canvas[data-mode="2d"] .only-3d { display: none; }
.canvas[data-mode="3d"] { cursor: default; }

.fold {
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: calc(16px + env(safe-area-inset-bottom));
  display: flex; align-items: center; gap: 12px; padding: 6px 6px 6px 8px;
  background: var(--surface); border: 1px solid var(--hairline); border-radius: 12px;
  width: min(560px, calc(100% - 32px));
}
.fold .icon { min-width: 36px; padding: 0; display: grid; place-items: center; }
.fold .i-pause { display: none; }
.fold.playing .i-play { display: none; }
.fold.playing .i-pause { display: inline; }
.presets { display: flex; gap: 4px; }
.presets button { background: transparent; font-weight: 500; min-width: 0; padding: 0 10px; }
.presets button:hover { background: var(--fill); }

input[type="range"] {
  flex: 1; min-width: 80px; height: 36px; background: transparent; -webkit-appearance: none; appearance: none; margin: 0;
}
input[type="range"]::-webkit-slider-runnable-track { height: 4px; border-radius: 2px; background: var(--fill); }
input[type="range"]::-moz-range-track { height: 4px; border-radius: 2px; background: var(--fill); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 20px; height: 20px; margin-top: -8px; border-radius: 50%;
  background: #fff; box-shadow: 0 0 0 0.5px rgba(0,0,0,.12), 0 2px 4px rgba(0,0,0,.2);
}
input[type="range"]::-moz-range-thumb { width: 20px; height: 20px; border: 0; border-radius: 50%; background: #fff; box-shadow: 0 0 0 0.5px rgba(0,0,0,.12), 0 2px 4px rgba(0,0,0,.2); }
input[type="range"]:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

.cams {
  position: absolute; top: calc(16px + env(safe-area-inset-top)); right: calc(16px + env(safe-area-inset-right)); z-index: 2;
  display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--hairline); border-radius: var(--radius); overflow: hidden;
}
.cams button { background: transparent; border-radius: 0; font-weight: 500; text-align: left; min-height: 36px; padding: 0 14px; }
.cams button + button { border-top: 1px solid var(--hairline); }
.cams button:hover { background: var(--fill); }
.help {
  position: absolute; top: calc(16px + env(safe-area-inset-top)); left: 16px; margin: 0; max-width: 180px;
  font-size: 12px; line-height: 1.5; color: var(--text-3); pointer-events: none;
}

@media (max-width: 760px) {
  .help { display: none; }
  .cams { flex-direction: row; top: auto; bottom: 72px; right: 16px; }
  .cams button + button { border-top: 0; border-left: 1px solid var(--hairline); }
  .cams button { padding: 0 10px; font-size: 12px; }
  .modes { width: 180px; }
}
.exports { gap: 6px; }
.exports button { min-width: 44px; padding: 0 10px; }
:root[data-theme="dark"] .canvas[data-mode="3d"] { background: #161618; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .canvas[data-mode="3d"] { background: #161618; }
}

/* ---------- design mode ---------- */
body:not([data-mode="design"]) .design-only, body[data-mode="design"] .hide-design { display: none; }
.design-only[hidden] { display: none; }
.modes { width: 270px; }

.templates { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tpl {
  display: grid; gap: 6px; padding: 0; border: 0; background: none; color: var(--text);
  font: inherit; font-size: 12px; text-align: left; cursor: pointer; min-height: 44px;
}
.tpl canvas {
  width: 100%; height: auto; display: block; border-radius: 6px;
  box-shadow: 0 0 0 1px var(--hairline);
}
.tpl:hover canvas, .tpl:focus-visible canvas { box-shadow: 0 0 0 1.5px var(--accent); }
.tpl:focus-visible { outline: none; }
.add { display: flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; }
.seg.btns button {
  flex: 1; min-width: 0; min-height: 32px; padding: 0 6px; border-radius: 6px;
  background: transparent; color: var(--text-2); font-weight: 500; display: grid; place-items: center;
}
.seg.btns button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.seg.btns button:disabled { opacity: .35; cursor: default; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }
.grid2 .field { margin-bottom: 0; padding: 0 10px; gap: 4px; font-size: 13px; }
.grid2 .field span { color: var(--text-2); }
.grid2 .field input { width: 100%; min-width: 0; flex: 1; }
.grid2 .field .unit { width: auto; font-size: 12px; }
.field.color input[type="color"] {
  width: 28px; height: 28px; padding: 0; border: 0; border-radius: 50%; overflow: hidden; cursor: pointer; flex: none;
  box-shadow: inset 0 0 0 1px var(--hairline);
}
.field.color input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
.field.color input[type="color"]::-webkit-color-swatch { border: 0; border-radius: 50%; }
.field.color input[type="color"]::-moz-color-swatch { border: 0; border-radius: 50%; }
.props textarea {
  width: 100%; margin-bottom: 8px; padding: 10px 12px; resize: vertical; min-height: 44px;
  font: inherit; font-size: 15px; color: var(--text); background: var(--fill); border: 0; border-radius: var(--radius); outline: none;
}
.props textarea:focus { box-shadow: inset 0 0 0 2px var(--accent); }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.actions button { font-weight: 500; }
.actions .danger { color: var(--danger); }

.layers { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.layers li {
  display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 0 8px;
  border-radius: var(--radius); cursor: pointer; font-size: 13px;
}
.layers li:hover { background: var(--fill); }
.layers li.selected { background: var(--fill); box-shadow: inset 0 0 0 1.5px var(--accent); }
.layers .thumb {
  width: 32px; height: 32px; flex: none; border-radius: 6px; display: grid; place-items: center;
  background: var(--paper) center / contain no-repeat; box-shadow: inset 0 0 0 1px var(--hairline);
  font-size: 16px; font-weight: 700; color: #1d1d1f;
}
.layers .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layers li { gap: 8px; padding-right: 0; touch-action: pan-y; user-select: none; -webkit-user-select: none; outline: none; }
.layers li:focus-visible { box-shadow: inset 0 0 0 2px var(--accent); }
.layers .ico {
  flex: none; width: 36px; min-width: 0; min-height: 44px; padding: 0; display: grid; place-items: center;
  background: transparent; color: var(--text-2); opacity: 0; border-radius: var(--radius);
}
.layers .ico:hover { color: var(--text); }
.layers li:hover .ico, .layers li.selected .ico, .layers li:focus-within .ico, .layers .ico[aria-pressed="true"] { opacity: 1; }
.layers .eye .off { opacity: 0; }
.layers .eye[aria-pressed="true"] .off { opacity: 1; }
.layers .lock[aria-pressed="false"] .shackle { transform: translateX(3.5px) rotate(-14deg); transform-origin: 13px 9px; }
.layers li.is-hidden .thumb, .layers li.is-hidden .name { opacity: 0.4; }
.layers.sorting { cursor: grabbing; }
.layers li.dragging { background: var(--surface); box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12), inset 0 0 0 1px var(--hairline); position: relative; z-index: 1; }
.layers .rename {
  flex: 1; min-width: 0; height: 30px; padding: 0 8px; font: inherit; font-size: 13px; color: var(--text);
  background: var(--surface); border: 0; border-radius: 6px; box-shadow: inset 0 0 0 2px var(--accent); outline: none;
}
.with-count span { color: var(--text-3, var(--text-2)); font-weight: 400; margin-left: 4px; }
.layers-hint { margin-top: 8px; }
#layers-empty:not([hidden]) + .layers-hint { display: none; }
@media (pointer: coarse) { .layers .ico { opacity: 1; } }

.sides { position: absolute; top: calc(16px + env(safe-area-inset-top)); left: 16px; z-index: 3; display: none; gap: 8px; }
.sides .seg { width: 180px; background: var(--surface); border: 1px solid var(--hairline); }
.history { display: flex; background: var(--surface); border: 1px solid var(--hairline); border-radius: var(--radius); overflow: hidden; }
.history button { background: transparent; border-radius: 0; min-width: 40px; padding: 0; display: grid; place-items: center; color: var(--text-2); }
.history button:hover { color: var(--text); background: var(--fill); }
.history button + button { border-left: 1px solid var(--hairline); }

.canvas[data-mode="design"] { cursor: default; }
.canvas[data-mode="design"] .sides { display: flex; }
.canvas[data-mode="design"] .stage { right: 50%; }
.canvas[data-mode="design"] .view3d { display: block; left: 50%; border-left: 1px solid var(--hairline); }
.canvas[data-mode="design"] .legend, .canvas[data-mode="design"] .help { display: none; }
.canvas[data-mode="design"] .zoom { right: calc(50% + 16px); }
.canvas[data-mode="design"] .fold { left: 75%; width: min(440px, calc(50% - 32px)); }
@container canvas (min-width: 761px) and (max-width: 1100px) {
  .sides { top: calc(64px + env(safe-area-inset-top)); }
}
.canvas.drop .stage { box-shadow: inset 0 0 0 2px var(--accent); }

.stage svg #art [data-layer] { cursor: move; }
.stage svg #art text { white-space: pre; }
.stage svg #board { pointer-events: none; }
.stage svg #sel .box, .stage svg #sel .stem { stroke: var(--accent); stroke-width: 1.5px; fill: none; vector-effect: non-scaling-stroke; }
.stage svg #sel .box.locked { stroke-dasharray: 4 3; stroke: var(--text-2); }
.stage svg #sel .guide { stroke: #e5322d; stroke-width: 1px; vector-effect: non-scaling-stroke; }
.stage svg #sel .h { fill: #fff; stroke: var(--accent); stroke-width: 1.5px; vector-effect: non-scaling-stroke; }
.stage svg #sel .hit { fill: transparent; }

@media (max-width: 760px) {
  .canvas[data-mode="design"] .stage { right: 0; bottom: 50%; }
  .canvas[data-mode="design"] .view3d { left: 0; top: 50%; border-left: 0; border-top: 1px solid var(--hairline); }
  .canvas[data-mode="design"] .fold, .canvas[data-mode="design"] .cams { display: none; }
  .canvas[data-mode="design"] .zoom { right: 8px; bottom: calc(45% + 8px); }
  .canvas[data-mode="design"] .zoom .zbtn { display: none; }
  .canvas[data-mode="design"] .zoom #rot-cw { border-right: 0; }
  body[data-mode="design"] { grid-template-rows: 68vh 32vh; }
  .canvas[data-mode="design"] .stage { top: 60px; bottom: 45%; }
  .canvas[data-mode="design"] .view3d { top: 55%; }
  .canvas[data-mode="design"] .sides { top: auto; bottom: calc(45% + 8px); left: 8px; }
  .sides .seg { width: 150px; }
}
