:root {
  --bg: #16121a;
  --panel: #211b26;
  --panel-2: #2b2431;
  --line: #3a3141;
  --text: #f3eaf0;
  --dim: #a99bb0;
  --jam: #d22a5a;
  --jam-2: #ff5c8a;
  --stage: #2a2430;
  --radius: 8px;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden;
  font: 14px/1.3 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent; }
button, input, select { font: inherit; color: inherit; }
button { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); min-height: 34px;
  min-width: 34px; padding: 0 8px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
button:hover { border-color: var(--dim); }
button.on, button[aria-pressed="true"] { background: var(--jam); border-color: var(--jam-2); color: #fff; }
button:disabled { opacity: .4; cursor: default; }
button svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
input[type="number"], input[type="text"], select { background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 5px 6px; min-width: 0; }
input[type="range"] { accent-color: var(--jam); min-width: 0; flex: 1; }
input[type="color"] { width: 44px; height: 34px; border: 1px solid var(--line); border-radius: 6px; background: none; padding: 2px; }
h2 { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--dim); margin: 14px 0 6px; font-weight: 600; }
hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }

#app { position: fixed; inset: 0; display: grid; grid-template-columns: 56px minmax(0, 1fr) 290px;
  grid-template-rows: auto auto minmax(0, 1fr); grid-template-areas: "top top top" "tools opts panel" "tools stage panel"; }
#topbar { grid-area: top; display: flex; align-items: center; gap: 10px; padding: 6px 10px; padding-top: max(6px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line); min-width: 0; }
#topbar .group { display: flex; gap: 4px; align-items: center; }
#topbar .grow { flex: 1; min-width: 0; justify-content: center; color: var(--dim); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.brand { display: flex; align-items: center; gap: 6px; font-weight: 700; }
.brand img { width: 24px; height: 24px; }
.panels-toggle { display: none !important; }

#toolbar { grid-area: tools; display: flex; flex-direction: column; gap: 4px; padding: 8px 6px; background: var(--panel);
  border-right: 1px solid var(--line); overflow-y: auto; }
#toolbar button { width: 42px; height: 42px; padding: 0; }
#toolbar button svg { width: 22px; height: 22px; }
#toolbar .sep { height: 1px; background: var(--line); margin: 4px 2px; flex: none; }

#options { grid-area: opts; display: flex; align-items: center; gap: 10px; padding: 6px 10px; background: var(--panel);
  border-bottom: 1px solid var(--line); min-height: 46px; overflow-x: auto; white-space: nowrap; }
#options label { display: flex; align-items: center; gap: 6px; color: var(--dim); }
#options input[type="range"] { width: 120px; flex: none; }
#options input[type="number"] { width: 64px; }
#options .hint { color: var(--dim); font-size: 12px; }
#options > * { flex: none; }

#stage { grid-area: stage; position: relative; overflow: hidden; background: var(--stage); touch-action: none; }
#artboard, #sketch { position: absolute; left: 0; top: 0; transform-origin: 0 0; pointer-events: none; }
#artboard { background: #fff; box-shadow: 0 2px 18px rgba(0,0,0,.45); }
#artboard.transparent { background: repeating-conic-gradient(#ddd 0 25%, #fff 0 50%) 0 0 / 24px 24px; }
#sketch { max-width: none; user-select: none; display: none; }
#paper, #overlay { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#paper { pointer-events: none; }
#overlay { touch-action: none; }

#panel { grid-area: panel; background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; padding: 4px 12px 24px; }
.row { display: flex; align-items: center; gap: 8px; margin: 6px 0; color: var(--dim); }
.row input[type="number"] { width: 64px; }
.row #hex { flex: 1; font-family: ui-monospace, monospace; }
.xform { display: grid; grid-template-columns: 1fr 1fr 34px; gap: 6px; align-items: center; }
.xform label { display: flex; align-items: center; gap: 6px; color: var(--dim); min-width: 0; }
.xform input { width: 100%; }
.xform input:disabled { opacity: .45; }
#tf-lock { grid-row: span 2; min-height: 0; height: 100%; }
#tf-lock[aria-pressed="false"] { background: var(--panel-2); border-color: var(--line); color: var(--dim); }
.snaps { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; color: var(--dim); }
.snaps label { display: flex; align-items: center; gap: 6px; }
.snaps input[type="number"] { width: 64px; }
.snaps .hint { font-size: 12px; }
.chips { display: flex; gap: 6px; }
.chip { flex: 1; justify-content: flex-start; }
.chip.on { outline: 2px solid var(--jam-2); outline-offset: 1px; background: var(--panel-2); color: var(--text); }
.sw { width: 20px; height: 20px; border-radius: 5px; border: 1px solid #0008; flex: none;
  background-image: linear-gradient(45deg, transparent 45%, #e33 45% 55%, transparent 55%); }
.swatches { display: grid; grid-template-columns: repeat(auto-fill, minmax(30px, 1fr)); gap: 5px; margin-top: 8px; }
.swatches button { min-width: 0; min-height: 0; aspect-ratio: 1; padding: 0; border-radius: 6px; border: 1px solid #0008; }
.swatches .add { background: var(--panel-2); border: 1px dashed var(--dim); }
.btns { display: flex; flex-wrap: wrap; gap: 4px; }
.btns button { flex: 1 0 auto; }
#layers { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 3px; }
#layers li { display: flex; align-items: center; gap: 4px; padding: 3px; border-radius: var(--radius); background: var(--bg); border: 1px solid transparent; }
#layers li.active { border-color: var(--jam-2); }
#layers li .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 6px; cursor: pointer; }
#layers li button { min-width: 30px; min-height: 30px; padding: 0; }
#layers li button svg { width: 17px; height: 17px; }
#layers li .off { opacity: .35; }

.popup { position: fixed; z-index: 20; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 6px;
  display: flex; flex-direction: column; gap: 3px; box-shadow: 0 10px 30px #000a; min-width: 230px; }
.popup[hidden] { display: none; }
.popup button { justify-content: flex-start; border-color: transparent; background: none; }
.popup button:hover { background: var(--panel-2); }
dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 12px; max-width: min(520px, 92vw); }
dialog::backdrop { background: #0008; }
dialog .row { color: var(--text); }
dialog table { border-collapse: collapse; width: 100%; font-size: 13px; }
dialog td { padding: 3px 6px; border-bottom: 1px solid var(--line); }
dialog td:first-child { color: var(--jam-2); font-family: ui-monospace, monospace; white-space: nowrap; }
dialog menu { display: flex; justify-content: flex-end; gap: 6px; padding: 0; margin: 14px 0 0; }
dialog.floating { position: fixed; inset: auto 16px auto auto; top: 104px; margin: 0; width: min(380px, calc(100vw - 32px));
  max-height: calc(100vh - 124px); overflow-y: auto; z-index: 15; box-shadow: 0 12px 40px #000c; padding: 12px; }
dialog.floating textarea { width: 100%; background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px; font: 18px/1.3 system-ui, sans-serif; resize: vertical; }
.tx-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.tx-font { color: var(--jam-2); font-size: 13px; }
.tx-search { width: 100%; margin-bottom: 6px; }
.tx-groups button { flex: 0 0 auto; min-height: 28px; font-size: 12px; padding: 0 8px; }
.tx-list { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; max-height: 270px; overflow-y: auto; margin-top: 6px; padding-right: 2px; }
.tx-list button { flex-direction: column; align-items: flex-start; gap: 2px; padding: 6px 8px; min-height: 58px; overflow: hidden; }
.tx-list button.on { background: var(--panel-2); outline: 2px solid var(--jam-2); color: var(--text); }
.tx-prev { font-size: 21px; line-height: 1.15; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.tx-list small { color: var(--dim); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.tx-foot { margin-top: 10px; }
.tx-note { color: var(--dim); font-size: 12px; margin: 8px 0 0; }
[hidden] { display: none !important; }
#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #000d; color: #fff; padding: 8px 14px;
  border-radius: 20px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 30; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; }

/* Phones and narrow windows: tools along the bottom, panels as a sheet. */
@media (max-width: 820px) {
  #app { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto minmax(0, 1fr) auto;
    grid-template-areas: "top" "opts" "stage" "tools"; }
  .brand span, #docname { display: none !important; }
  .panels-toggle { display: flex !important; }
  #topbar [data-act="zoom-in"], #topbar [data-act="zoom-out"] { display: none; }
  #topbar { justify-content: space-between; }
  #topbar { gap: 6px; padding-left: 8px; padding-right: 8px; }
  #toolbar { flex-direction: row; border-right: 0; border-top: 1px solid var(--line); overflow-x: auto; overflow-y: hidden;
    padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  #toolbar button { flex: none; }
  #toolbar .sep { width: 1px; height: auto; margin: 2px 4px; }
  #panel { position: fixed; left: 0; right: 0; bottom: 0; max-height: 62vh; z-index: 10; border-left: 0; border-top: 1px solid var(--line);
    border-radius: 14px 14px 0 0; box-shadow: 0 -10px 30px #000a; transform: translateY(105%); transition: transform .2s; }
  body.sheet-style #panel, body.sheet-layers #panel { transform: none; }
  body.sheet-style #p-layers, body.sheet-layers #p-style { display: none; }
  #options input[type="range"] { width: 90px; }
  dialog.floating { top: auto; bottom: 8px; inset-inline: 8px; width: auto; max-height: 58vh; }
  .tx-list { max-height: 180px; }
  dialog.floating textarea { height: 3.4em; }
}
@media (max-width: 400px) {
  #topbar button.txt { padding: 0 6px; }
}
