:root {
  --bg: #0d1014;
  --surface: #14181e;
  --surface-2: #1a1f27;
  --surface-3: #222933;
  --line: #2a323d;
  --line-2: #364050;
  --text: #e6eaf0;
  --muted: #8e9aab;
  --accent: #5b9dff;
  --accent-ink: #0b1220;
  --ok: #3fb950;
  --warn: #e3b341;
  --bad: #f2555a;
  --radius: 10px;
  --top-h: 52px;
  --bar-h: 64px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
.i { flex: none; display: block; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* ---------- header ---------- */

.appbar {
  height: var(--top-h);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 0 14px;
  padding-top: env(safe-area-inset-top);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; letter-spacing: .01em; white-space: nowrap; }
.brand h1 { font-size: inherit; margin: 0; }
.doc { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; }
.docname {
  min-width: 0; max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  background: none; border: 0; border-radius: 6px; padding: 4px 8px; color: var(--muted); cursor: pointer;
}
.docname:hover { background: var(--surface-3); color: var(--text); }
.btn.save { height: 32px; padding: 0 11px; color: var(--accent); border-color: #2d4a75; background: #162238; }
.btn.save:hover { background: #1c2c48; }
.printer { display: flex; align-items: center; gap: 8px; min-width: 0; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px; font-size: 12px; white-space: nowrap;
  background: var(--surface-3); color: var(--muted); border: 1px solid var(--line);
}
.chip.ok { color: var(--ok); border-color: #23462e; background: #13241a; }
.chip.bad { color: var(--bad); border-color: #5a2a2d; background: #2a1617; }
.chip.warn { color: var(--warn); border-color: #54451c; background: #241e10; }

/* ---------- buttons and inputs ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: 36px; padding: 0 13px; border-radius: 8px;
  background: var(--surface-3); border: 1px solid var(--line-2);
  cursor: pointer; white-space: nowrap; user-select: none;
  transition: background .12s, border-color .12s;
}
.btn:hover { background: #2a323e; border-color: #465265; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: default; transform: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 650; }
.btn.primary:hover { background: #77afff; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); }
.btn.big { height: 44px; padding: 0 20px; font-size: 15px; border-radius: 10px; }
.btn.connected { color: var(--ok); border-color: #2c5a39; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; padding: 0; border-radius: 8px;
  background: transparent; border: 1px solid transparent; cursor: pointer; color: var(--text);
}
.icon-btn:hover { background: var(--surface-3); }
.icon-btn:disabled { opacity: .35; cursor: default; background: transparent; }
.icon-btn.sm { width: 30px; height: 30px; }
.icon-btn.on, .toggle.on { background: #1f3352; color: var(--accent); border-color: #2d4a75; }

input[type=text], input[type=number], input[type=search], select, textarea {
  background: var(--bg); border: 1px solid var(--line-2); border-radius: 8px;
  padding: 7px 9px; min-width: 0; width: 100%;
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(91,157,255,.18); }
input[type=number] { width: 84px; }
textarea { resize: vertical; min-height: 64px; line-height: 1.35; font-size: 16px; }
select { appearance: none; padding-right: 28px; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 15px) 52%, calc(100% - 10px) 52%; background-size: 5px 5px; background-repeat: no-repeat; }
input[type=range] { width: 100%; accent-color: var(--accent); }
input[type=checkbox] { accent-color: var(--accent); width: 18px; height: 18px; }

/* ---------- layout ---------- */

.layout { display: grid; }

@media (min-width: 900px) {
  body { overflow: hidden; display: flex; flex-direction: column; height: 100dvh; }
  .appbar, #banners { flex: none; }
  .layout {
    flex: 1; min-height: 0; margin-bottom: var(--bar-h);
    grid-template-columns: minmax(0, 1fr) 380px;
  }
  .workspace { display: flex; flex-direction: column; min-height: 0; padding: 14px; gap: 12px; }
  .stage { flex: 1; min-height: 180px; }
  .side { border-left: 1px solid var(--line); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
  .panel { overflow-y: auto; flex: 1; }
}

@media (max-width: 899px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding-bottom: calc(var(--bar-h) + env(safe-area-inset-bottom)); }
  .workspace { padding: 10px 10px 8px; display: flex; flex-direction: column; gap: 8px; background: var(--bg); min-width: 0; }
  .stage { height: clamp(170px, 32vh, 300px); }
  .side { background: var(--surface); border-top: 1px solid var(--line); min-height: 60vh; }
  .brand h1 { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .docname { display: none; }
  .tool span { display: none; }
  .tool { width: 38px; padding: 0; }
  .addbar { gap: 3px; }
  .history .icon-btn { width: 34px; }
  #chipRoll { display: none; }
}
/* Keep the label in view while editing, but only when there's room left for the panel. */
@media (max-width: 899px) and (min-height: 740px) {
  .workspace { position: sticky; top: 0; z-index: 5; box-shadow: 0 8px 16px -12px rgba(0,0,0,.8); }
}

/* ---------- workspace ---------- */

.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-width: 0; }
.addbar { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; min-width: 0; }
@media (max-width: 380px) {
  .addbar { -webkit-mask-image: linear-gradient(to right, #000 82%, transparent); mask-image: linear-gradient(to right, #000 82%, transparent); padding-right: 24px; }
}
.addbar::-webkit-scrollbar { display: none; }
.tool {
  display: inline-flex; align-items: center; gap: 6px; height: 38px; padding: 0 11px; flex: none;
  justify-content: center;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 9px; cursor: pointer;
}
.tool:hover { background: var(--surface-3); border-color: var(--line-2); }
.history { display: flex; gap: 2px; flex: none; }

.stage {
  position: relative; border-radius: var(--radius); overflow: hidden;
  background: radial-gradient(circle at 50% 40%, #1b212a, #10141a 70%);
  border: 1px solid var(--line);
}
.stage canvas { position: absolute; inset: 0; touch-action: none; outline: none; }
.stage-tools {
  position: absolute; left: 8px; right: 8px; bottom: 8px;
  display: flex; justify-content: space-between; pointer-events: none;
}
.stage-tools .group {
  pointer-events: auto; display: flex; align-items: center; gap: 2px; padding: 2px;
  background: rgba(20,24,30,.88); border: 1px solid var(--line); border-radius: 9px; backdrop-filter: blur(6px);
}
.zoom-pct { background: none; border: 0; min-width: 46px; font-size: 12px; color: var(--muted); cursor: pointer; }

.preview { display: flex; flex-direction: column; gap: 4px; }
.preview-head { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }
.preview-head .warn { color: var(--warn); }
.preview-box { display: flex; justify-content: center; }
#dots { max-width: 100%; border-radius: 4px; image-rendering: pixelated; }

/* ---------- side panel ---------- */

.tabs { display: flex; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--surface); z-index: 2; }
.tabs button {
  flex: 1; height: 44px; background: none; border: 0; border-bottom: 2px solid transparent;
  color: var(--muted); font-weight: 600; cursor: pointer;
}
.tabs button.on { color: var(--text); border-bottom-color: var(--accent); }
.panel { padding: 6px 16px 24px; display: none; }
.panel.on { display: block; }
.panel h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 0 8px; }
.panel h2:first-child { margin-top: 12px; }

.field { display: grid; grid-template-columns: 96px minmax(0, 1fr); align-items: center; gap: 10px; margin: 8px 0; }
.field > label { color: var(--muted); }
.field.top { align-items: start; }
.field.top > label { padding-top: 7px; }
.inline { display: flex; align-items: center; gap: 8px; min-width: 0; }
.inline.grow > input[type=range] { flex: 1; }
.unit { color: var(--muted); font-size: 13px; white-space: nowrap; }
.val { min-width: 26px; text-align: right; font-variant-numeric: tabular-nums; }
.row { display: flex; gap: 8px; align-items: center; margin: 10px 0; }
.row.wrap { flex-wrap: wrap; }
.hint { font-size: 12.5px; color: var(--muted); margin: 6px 0; }
.full { width: 100%; }

.seg { display: inline-flex; background: var(--bg); border: 1px solid var(--line-2); border-radius: 8px; padding: 2px; gap: 2px; }
.seg button {
  display: inline-flex; align-items: center; justify-content: center; gap: 5px;
  min-width: 32px; height: 30px; padding: 0 8px; border: 0; border-radius: 6px; background: none; cursor: pointer; color: var(--muted);
}
.seg button:hover { color: var(--text); }
.seg button.on { background: var(--surface-3); color: var(--text); box-shadow: inset 0 0 0 1px var(--line-2); }
.seg-row { display: flex; gap: 8px; flex-wrap: wrap; }

.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.switch input { appearance: none; width: 38px; height: 22px; border-radius: 999px; background: var(--surface-3); border: 1px solid var(--line-2); position: relative; cursor: pointer; margin: 0; transition: background .15s; }
.switch input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: var(--muted); transition: transform .15s, background .15s; }
.switch input:checked { background: #1f3a66; border-color: var(--accent); }
.switch input:checked::after { transform: translateX(16px); background: var(--accent); }

.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.chips button { font-size: 12px; padding: 3px 9px; border-radius: 999px; border: 1px solid var(--line-2); background: var(--surface-2); cursor: pointer; color: var(--muted); }
.chips button:hover { color: var(--text); }

.obj-head { display: flex; align-items: center; justify-content: space-between; margin: 12px 0 4px; }
.obj-head .title { font-weight: 650; display: flex; align-items: center; gap: 8px; }
.obj-head .actions { display: flex; gap: 2px; }

.geo { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.geo label { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
.geo label input { width: 100%; }
.geo .rot { display: flex; gap: 4px; align-items: center; }

.tool-row { display: flex; flex-wrap: wrap; gap: 4px; }
.tool-row .icon-btn { background: var(--surface-2); border-color: var(--line); }
.tool-row .icon-btn:hover { background: var(--surface-3); }

.layers { display: flex; flex-direction: column; gap: 4px; }
.layer {
  display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px;
  background: var(--surface-2); border: 1px solid var(--line); cursor: pointer;
}
.layer:hover { border-color: var(--line-2); }
.layer.on { border-color: var(--accent); background: #17253a; }
.layer .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer .pick {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; padding: 0;
  background: none; border: 0; color: inherit; text-align: left; cursor: pointer;
}
.layer .icon-btn { width: 28px; height: 28px; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 18px 8px; }

.swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.swatch { width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--line-2); cursor: pointer; padding: 0; }
.swatch.on { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(91,157,255,.25); }

.nudge { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
.pad { display: grid; grid-template-columns: repeat(3, 40px); grid-template-rows: repeat(3, 40px); gap: 2px; }
.pad .icon-btn { width: 40px; height: 40px; background: var(--surface-2); border-color: var(--line); }
.pad [data-nudge=up] { grid-column: 2; grid-row: 1; }
.pad [data-nudge=left] { grid-column: 1; grid-row: 2; }
.pad [data-nudge=right] { grid-column: 3; grid-row: 2; }
.pad [data-nudge=down] { grid-column: 2; grid-row: 3; }
.cal-fields { flex: 1; min-width: 200px; }
.cal-fields .field { margin: 4px 0; grid-template-columns: 92px 1fr; }

.save-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin: 6px 0 4px; }
.save-row .hint { margin: 0; }
.lib-seg { display: flex; width: 100%; margin: 20px 0 6px; }
.lib-seg button { flex: 1; height: 34px; font-weight: 600; }
.lib-seg .count { font-weight: 500; color: var(--muted); font-size: 12px; }
.lib-seg .count:empty { display: none; }

.designs { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.designs .empty { grid-column: 1 / -1; display: flex; flex-direction: column; align-items: center; gap: 10px; }
.design {
  position: relative; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2);
  padding: 8px; cursor: pointer; display: flex; flex-direction: column; gap: 6px; text-align: left; color: inherit; font: inherit;
}
.design.template { align-items: stretch; }
.design.template:hover { border-color: var(--accent); }
.design:hover { border-color: var(--line-2); }
.design.on { border-color: var(--accent); }
.design .thumb { display: flex; align-items: center; justify-content: center; height: 54px; }
.design .thumb img { max-width: 100%; max-height: 54px; border-radius: 3px; }
.design .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.design .meta { font-size: 11.5px; color: var(--muted); }
.design .del { position: absolute; bottom: 4px; right: 4px; width: 26px; height: 26px; opacity: .55; }
.design .del:hover { opacity: 1; color: var(--bad); }

.info { display: grid; grid-template-columns: 96px 1fr; gap: 4px 10px; font-size: 13px; }
.info dt { color: var(--muted); }
.info dd { margin: 0; }

.batch { background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-top: 12px; }
.batch h3 { margin: 0 0 6px; font-size: 13px; }
.batch summary { cursor: pointer; color: var(--muted); margin: 8px 0 6px; user-select: none; }
.batch summary:hover { color: var(--text); }
.batch details[open] summary { color: var(--text); }
#seriesHint, #batchSum { margin-bottom: 0; }
#tableInfo .hint { margin: 6px 0 0; }
.inline select, .inline input[type=text] { flex: 1; min-width: 0; }

/* ---------- print bar ---------- */

.printbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  height: calc(var(--bar-h) + env(safe-area-inset-bottom));
  padding: 0 14px env(safe-area-inset-bottom);
  display: flex; align-items: center; gap: 12px;
  background: rgba(20,24,30,.96); border-top: 1px solid var(--line); backdrop-filter: blur(8px);
}
.status { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
#msg { overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-height: 1.25; color: var(--muted); }
#msg.bad { color: var(--bad); }
#msg.ok { color: var(--ok); }
progress { width: 100%; max-width: 260px; height: 6px; accent-color: var(--accent); }
.copies { display: flex; align-items: center; gap: 2px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.copies span { min-width: 30px; text-align: center; font-variant-numeric: tabular-nums; }
.copies span::after { content: "×"; color: var(--muted); margin-left: 1px; }

/* ---------- banners, toast, dialog ---------- */

#banners:empty { display: none; }
.banner {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 9px 14px; font-size: 13.5px; background: #1b2433; border-bottom: 1px solid #2b3b55;
}
.banner.warn { background: #262012; border-color: #4a3d1a; }
.banner .text { flex: 1; min-width: 200px; }
.banner .actions { display: flex; gap: 6px; }
.banner .btn { height: 30px; padding: 0 10px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--bar-h) + 14px + env(safe-area-inset-bottom)); z-index: 30;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  display: flex; align-items: center; gap: 12px;
  background: #222a35; border: 1px solid var(--line-2); border-radius: 10px; padding: 9px 14px;
  box-shadow: 0 10px 30px rgba(0,0,0,.45); transition: opacity .18s, transform .18s; max-width: calc(100vw - 24px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast button { background: none; border: 0; color: var(--accent); font-weight: 650; cursor: pointer; padding: 0; }

dialog.sheet {
  width: min(560px, calc(100vw - 20px)); max-height: min(640px, calc(100dvh - 40px));
  padding: 0; border: 1px solid var(--line-2); border-radius: 14px; background: var(--surface); color: var(--text);
  display: none; flex-direction: column;
}
dialog.sheet[open] { display: flex; }
dialog::backdrop { background: rgba(0,0,0,.6); }
.sheet-head { display: flex; gap: 8px; padding: 12px; border-bottom: 1px solid var(--line); }
.icon-grid { overflow-y: auto; padding: 8px 12px 14px; }
.icon-grid h3 { margin: 12px 0 6px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.icon-grid .cells { display: grid; grid-template-columns: repeat(auto-fill, minmax(46px, 1fr)); gap: 4px; }
.icon-grid button { height: 46px; display: flex; align-items: center; justify-content: center; background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; cursor: pointer; }
.icon-grid button:hover { border-color: var(--accent); }
.sheet-head h2 { flex: 1; margin: 0; font-size: 15px; align-self: center; }
dialog.keys { width: min(460px, calc(100vw - 20px)); }
.keys-list { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0; padding: 14px 16px 18px; overflow-y: auto; font-size: 13.5px; }
.keys-list dt { font-family: "JetBrains Mono", ui-monospace, monospace; font-size: 12.5px; color: var(--accent); white-space: nowrap; }
.keys-list dd { margin: 0; color: var(--muted); }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.stage canvas:focus-visible { outline: none; }
.stage:has(canvas:focus-visible) { border-color: var(--accent); }
