/* MakerPrep — one coherent workshop theme.
   The canvas is a cutting mat; everything around it is quiet, warm and flat. */

@font-face {
  font-family: "MP Display";
  src: url("/fonts/fredoka-700.woff") format("woff");
  font-weight: 700;
  font-display: swap;
}
@font-face {
  font-family: "MP Display";
  src: url("/fonts/fredoka-400.woff") format("woff");
  font-weight: 400;
  font-display: swap;
}

:root {
  --bg: #F2EFE9;
  --panel: #FAF8F4;
  --surface: #FFFFFF;
  --ink: #26231E;
  --ink-2: #5F5950;
  --ink-3: #928A7D;
  --line: #E4DFD6;
  --line-2: #D2CBBF;
  --mat: #2F6E5B;
  --mat-deep: #275D4D;
  --mat-grid: rgba(255, 255, 255, 0.11);
  --accent: #1D6A58;
  --accent-hover: #175848;
  --accent-ink: #FFFFFF;
  --accent-soft: #DCEDE6;
  --wood: #C8914A;
  --wood-soft: #F6E8D2;
  --danger: #C2412D;
  --danger-soft: #F8E1DC;
  --warn-soft: #FFF1C9;
  --focus: #2E8B74;

  --layer-cut: #E53935;
  --layer-score: #1E66F5;
  --layer-engrave: #26231E;
  --layer-print: #7A3FB5;
  --layer-white: #7C8794;
  --layer-varnish: #D4A017;
  --layer-guide: #9AA0A6;

  --radius-s: 6px;
  --radius: 10px;
  --radius-l: 14px;
  --shadow-1: 0 1px 2px rgba(38, 35, 30, 0.08);
  --shadow-2: 0 8px 24px rgba(38, 35, 30, 0.14);
  --shadow-sheet: 0 10px 30px rgba(0, 0, 0, 0.35);

  --font-display: "MP Display", "Segoe UI", system-ui, sans-serif;
  --font-ui: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --fs-0: 12px;
  --fs-1: 13.5px;
  --fs-2: 15px;
  --fs-3: 18px;
  --fs-4: 24px;
  --fs-5: 34px;

  --rail-w: 76px;
  --drawer-w: 292px;
  --props-w: 324px;
  --topbar-h: 58px;
  --target: 36px;
  --ruler-w: 22px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 var(--fs-2)/1.45 var(--font-ui);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3, h4 { font-family: var(--font-display); font-weight: 700; margin: 0; line-height: 1.15; letter-spacing: 0.005em; }
h1 { font-size: var(--fs-5); }
h2 { font-size: var(--fs-4); }
h3 { font-size: var(--fs-3); }
h4 { font-size: var(--fs-2); }
p { margin: 0; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }
.app { height: 100%; }
.muted { color: var(--ink-2); }
.small { font-size: var(--fs-1); }
.tiny { font-size: var(--fs-0); }
.nowrap { white-space: nowrap; }
.spacer { flex: 1; }

/* ---------- boot ---------- */
.boot {
  height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  color: var(--ink-2);
}
.boot-mark {
  width: 44px; height: 44px; border-radius: 12px; background: var(--mat);
  position: relative; overflow: hidden;
}
.boot-mark::after {
  content: ""; position: absolute; inset: 0;
  background-image: linear-gradient(var(--mat-grid) 1px, transparent 1px), linear-gradient(90deg, var(--mat-grid) 1px, transparent 1px);
  background-size: 11px 11px;
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--target); padding: 0 14px; border-radius: var(--radius-s);
  border: 1px solid var(--line-2); background: var(--surface); color: var(--ink);
  font-weight: 500; white-space: nowrap; user-select: none;
}
.btn:hover { background: var(--panel); border-color: var(--ink-3); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn svg { width: 18px; height: 18px; flex: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-big { min-height: 44px; padding: 0 22px; font-family: var(--font-display); font-size: var(--fs-3); font-weight: 700; border-radius: var(--radius); }
.btn-quiet { border-color: transparent; background: transparent; }
.btn-quiet:hover { background: rgba(38, 35, 30, 0.06); border-color: transparent; }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn-icon { width: var(--target); padding: 0; }
.btn-sm { min-height: 30px; padding: 0 10px; font-size: var(--fs-1); }
.btn-sm svg { width: 16px; height: 16px; }
.btn.is-on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.btn-group { display: inline-flex; }
.btn-group .btn { border-radius: 0; margin-left: -1px; }
.btn-group .btn:first-child { border-radius: var(--radius-s) 0 0 var(--radius-s); margin-left: 0; }
.btn-group .btn:last-child { border-radius: 0 var(--radius-s) var(--radius-s) 0; }

/* ---------- form controls ---------- */
.field { display: flex; flex-direction: column; gap: 5px; }
.field > label, .label { font-size: var(--fs-1); color: var(--ink-2); font-weight: 500; }
.input, .select, .textarea {
  min-height: var(--target); padding: 6px 10px; border-radius: var(--radius-s);
  border: 1px solid var(--line-2); background: var(--surface); width: 100%;
}
.input:focus, .select:focus, .textarea:focus { border-color: var(--accent); outline: 2px solid var(--accent-soft); outline-offset: 0; }
.textarea { min-height: 110px; resize: vertical; line-height: 1.5; }
.input-num { width: 84px; text-align: right; }
.input-sm { min-height: 30px; padding: 3px 8px; font-size: var(--fs-1); }
.select { appearance: none; padding-right: 30px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%235F5950' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; }
.row { display: flex; align-items: center; gap: 10px; }
.row-wrap { flex-wrap: wrap; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.check { display: inline-flex; align-items: center; gap: 8px; min-height: var(--target); cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; }
.range { width: 100%; accent-color: var(--accent); }
.hint { font-size: var(--fs-1); color: var(--ink-2); line-height: 1.4; }
.sep { height: 1px; background: var(--line); margin: 6px 0; }

/* toggle switch */
.switch { position: relative; display: inline-flex; align-items: center; gap: 10px; min-height: var(--target); cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch .knob { width: 40px; height: 22px; border-radius: 11px; background: var(--line-2); position: relative; transition: background .15s; flex: none; }
.switch .knob::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: var(--shadow-1); }
.switch input:checked + .knob { background: var(--accent); }
.switch input:checked + .knob::after { transform: translateX(18px); }
.switch input:focus-visible + .knob { outline: 2px solid var(--focus); outline-offset: 2px; }

/* chips */
.chip {
  display: inline-flex; align-items: center; gap: 6px; height: 26px; padding: 0 10px; border-radius: 13px;
  font-size: var(--fs-1); font-weight: 500; background: var(--panel); border: 1px solid var(--line); color: var(--ink-2);
}
.chip-live { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.chip-soon { background: var(--warn-soft); color: #7A5A00; border-color: transparent; }
.chip-wood { background: var(--wood-soft); color: #7A5319; border-color: transparent; }
.chip-dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }

/* ---------- toasts & dialogs ---------- */
.toasts { position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 1000; pointer-events: none; }
.toast { pointer-events: auto; background: var(--ink); color: #fff; padding: 10px 16px; border-radius: var(--radius); box-shadow: var(--shadow-2); font-size: var(--fs-1); display: flex; align-items: center; gap: 10px; animation: toast-in .18s ease-out; max-width: 520px; }
.toast-err { background: var(--danger); }
.toast-ok { background: var(--accent); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
@media (prefers-reduced-motion: reduce) { .toast { animation: none; } }

.dialog-backdrop { position: fixed; inset: 0; background: rgba(38, 35, 30, 0.45); display: flex; align-items: center; justify-content: center; z-index: 900; padding: 20px; }
.dialog { background: var(--surface); border-radius: var(--radius-l); box-shadow: var(--shadow-2); width: min(560px, 100%); max-height: calc(100vh - 40px); display: flex; flex-direction: column; overflow: hidden; }
.dialog-wide { width: min(980px, 100%); }
.dialog-head { display: flex; align-items: center; gap: 12px; padding: 18px 22px 10px; }
.dialog-head h2 { font-size: var(--fs-4); flex: 1; }
.dialog-body { padding: 8px 22px 18px; overflow: auto; display: flex; flex-direction: column; gap: 14px; }
.dialog-foot { display: flex; gap: 10px; justify-content: flex-end; padding: 12px 22px 18px; border-top: 1px solid var(--line); flex-wrap: wrap; }

/* expander */
.expander { border: 1px solid var(--line); border-radius: var(--radius-s); background: var(--panel); }
.expander > summary { list-style: none; cursor: pointer; padding: 8px 12px; font-size: var(--fs-1); color: var(--accent); font-weight: 500; display: flex; align-items: center; gap: 8px; min-height: var(--target); }
.expander > summary::-webkit-details-marker { display: none; }
.expander > summary::before { content: ""; width: 8px; height: 8px; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor; transform: rotate(-45deg); transition: transform .12s; }
.expander[open] > summary::before { transform: rotate(45deg); }
.expander-body { padding: 0 12px 12px; font-size: var(--fs-1); color: var(--ink-2); line-height: 1.5; }
.expander-body ul { margin: 6px 0 0; padding-left: 18px; }

/* ---------- login ---------- */
.login { height: 100%; display: grid; place-items: center; padding: 20px; }
.login-card { width: min(420px, 100%); background: var(--surface); border-radius: var(--radius-l); padding: 32px; box-shadow: var(--shadow-2); display: flex; flex-direction: column; gap: 18px; }
.login-card .brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { width: 40px; height: 40px; border-radius: 10px; background: var(--mat); position: relative; overflow: hidden; flex: none; }
.brand-mark::after { content: ""; position: absolute; inset: 0; background-image: linear-gradient(var(--mat-grid) 1px, transparent 1px), linear-gradient(90deg, var(--mat-grid) 1px, transparent 1px); background-size: 10px 10px; }
.brand-mark svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.login .err { color: var(--danger); font-size: var(--fs-1); min-height: 1.2em; }

/* ---------- home ---------- */
.home { height: 100%; overflow: auto; }
.home-inner { max-width: 1180px; margin: 0 auto; padding: 28px 32px 60px; display: flex; flex-direction: column; gap: 34px; }
.home-head { display: flex; align-items: flex-end; gap: 18px; }
.home-head .brand { display: flex; align-items: center; gap: 14px; }
.home-head h1 { font-size: var(--fs-5); }
.home-head .sub { color: var(--ink-2); margin-top: 2px; }
.section { display: flex; flex-direction: column; gap: 14px; }
.section-head { display: flex; align-items: baseline; gap: 12px; }
.section-head h2 { font-size: var(--fs-4); }
.section-head .muted { font-size: var(--fs-1); }
.machine-row { display: flex; flex-direction: column; gap: 10px; }
.machine-title { display: flex; align-items: center; gap: 10px; font-family: var(--font-display); font-size: var(--fs-3); }
.machine-title .bed { font-family: var(--font-ui); font-size: var(--fs-1); color: var(--ink-2); font-weight: 400; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.obj-card {
  display: flex; flex-direction: column; align-items: flex-start; gap: 10px; padding: 14px 14px 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); text-align: left; min-height: 132px;
}
.obj-card:hover { border-color: var(--accent); box-shadow: var(--shadow-1); }
.obj-card .ico { width: 44px; height: 44px; color: var(--accent); }
.obj-card .ico svg { width: 100%; height: 100%; }
.obj-card .name { font-weight: 600; line-height: 1.25; }
.obj-card .dims { display: block; font-size: var(--fs-1); color: var(--ink-2); font-variant-numeric: tabular-nums; }
.obj-card.is-custom .ico { color: var(--wood); }
.proj-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 14px; }
.proj-card { display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; text-align: left; padding: 0; position: relative; }
.proj-card:hover { border-color: var(--accent); box-shadow: var(--shadow-1); }
.proj-card .thumb { height: 130px; background: var(--mat); display: grid; place-items: center; background-image: linear-gradient(var(--mat-grid) 1px, transparent 1px), linear-gradient(90deg, var(--mat-grid) 1px, transparent 1px); background-size: 12px 12px; }
.proj-card .thumb img { max-width: 88%; max-height: 88%; box-shadow: var(--shadow-sheet); }
.proj-card .meta { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 3px; }
.proj-card .meta .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.proj-card .meta .when { font-size: var(--fs-0); color: var(--ink-3); }
.proj-card .del { position: absolute; top: 8px; right: 8px; opacity: 0; background: rgba(255,255,255,.9); }
.proj-card:hover .del, .proj-card:focus-within .del { opacity: 1; }
.tpl-row { display: flex; gap: 12px; overflow-x: auto; padding-bottom: 6px; scroll-snap-type: x proximity; }
.tpl-row > .tpl-card { flex: 0 0 230px; scroll-snap-align: start; }
/* template gallery (home) */
.tpl-filters { display: flex; flex-wrap: wrap; gap: 6px; }
.tpl-filter { min-height: 32px; padding: 0 12px; border-radius: 16px; border: 1px solid var(--line-2); background: var(--surface); color: var(--ink-2); font-size: var(--fs-1); font-weight: 500; }
.tpl-filter:hover { border-color: var(--accent); color: var(--ink); }
.tpl-filter.is-on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.tpl-gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 14px; }
.tpl-gallery.is-blank { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
.tpl-card { display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; text-align: left; padding: 0; min-height: 44px; transition: transform .12s ease-out, box-shadow .12s ease-out, border-color .12s; }
.tpl-card:hover, .tpl-card:focus-visible { border-color: var(--accent); box-shadow: var(--shadow-2); transform: translateY(-2px); }
.tpl-card:active { transform: translateY(0) scale(.99); box-shadow: var(--shadow-1); }
.tpl-card .thumb { position: relative; width: 100%; height: 150px; flex: none; background: var(--mat); display: flex; align-items: center; justify-content: center; color: rgba(255,255,255,.75); overflow: hidden;
  background-image: linear-gradient(var(--mat-grid) 1px, transparent 1px), linear-gradient(90deg, var(--mat-grid) 1px, transparent 1px); background-size: 12px 12px; }
.tpl-card .thumb img { max-width: 86%; max-height: 86%; box-shadow: var(--shadow-sheet); background: #fff; }
.tpl-card .thumb svg { width: 36px; height: 36px; }
.tpl-card .thumb .machine-chip { position: absolute; top: 8px; left: 8px; display: inline-flex; align-items: center; gap: 5px; height: 24px; padding: 0 8px; border-radius: 12px; background: rgba(255,255,255,.92); color: var(--accent); font-size: var(--fs-0); font-weight: 600; white-space: nowrap; }
.tpl-card .thumb .machine-chip svg { width: 14px; height: 14px; }
.tpl-card .meta { width: 100%; padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 4px; }
.tpl-card .meta .name { font-weight: 600; line-height: 1.25; }
.tpl-card .meta .desc { font-size: var(--fs-0); color: var(--ink-2); line-height: 1.35; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.tpl-card .meta .foot { display: flex; align-items: center; gap: 6px; margin-top: 4px; font-size: var(--fs-0); color: var(--ink-3); }
.tpl-card.is-soon { opacity: 0.75; }
.tpl-gallery.is-blank .tpl-card .thumb { height: 92px; background: var(--panel); color: var(--ink-3); background-image: none; }
.tpl-gallery.is-blank .tpl-card .thumb .machine-chip { background: var(--accent-soft); }
/* compact (editor drawer) */
.tpl-section .tpl-gallery.is-compact { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.tpl-gallery.is-compact .tpl-card .thumb { height: 92px; }
.tpl-gallery.is-compact .tpl-card .meta { padding: 8px 9px 9px; }
.tpl-gallery.is-compact .tpl-card .meta .name { font-size: var(--fs-1); }
.tpl-gallery.is-compact .tpl-card .desc { -webkit-line-clamp: 1; }
.tpl-gallery.is-compact .tpl-card .thumb .machine-chip { height: 20px; padding: 0 6px; }
.drawer .tpl-section { gap: 10px; display: flex; flex-direction: column; }
.drawer .tpl-section .section-head h4 { font-family: var(--font-display); font-size: var(--fs-2); }
@media (prefers-reduced-motion: reduce) { .tpl-card { transition: none; } .tpl-card:hover { transform: none; } }
.empty { padding: 22px; border: 1px dashed var(--line-2); border-radius: var(--radius); color: var(--ink-2); font-size: var(--fs-1); }

/* ---------- editor ---------- */
.editor {
  height: 100%; display: grid;
  grid-template-columns: var(--rail-w) auto minmax(0, 1fr) var(--props-w);
  grid-template-rows: var(--topbar-h) auto auto minmax(0, 1fr) auto;
  grid-template-areas:
    "top top top top"
    "rail drawer arrange props"
    "rail drawer tips props"
    "rail drawer canvas props"
    "rail drawer pages props";
}
.topbar {
  grid-area: top; display: flex; align-items: center; gap: 10px; padding: 0 14px 0 10px;
  background: var(--surface); border-bottom: 1px solid var(--line); min-width: 0; overflow: hidden;
}
.topbar > * { flex: none; }
.topbar .pname { font-family: var(--font-display); font-size: var(--fs-3); font-weight: 700; border: 1px solid transparent; border-radius: var(--radius-s); padding: 4px 8px; min-width: 120px; max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; background: transparent; }
.topbar .pname:hover { border-color: var(--line-2); }
.topbar .pname:focus { border-color: var(--accent); background: var(--surface); outline: none; }
.topbar .machine { background: var(--accent-soft); color: var(--accent); border-color: transparent; height: 30px; white-space: nowrap; }
.topbar .select { width: auto; min-width: 150px; max-width: 220px; min-height: 36px; }
.topbar .zoom { display: inline-flex; align-items: center; gap: 2px; }
.topbar .zoom .pct { min-width: 52px; text-align: center; font-size: var(--fs-1); color: var(--ink-2); font-variant-numeric: tabular-nums; }
.topbar .saved { font-size: var(--fs-0); color: var(--ink-3); min-width: 70px; text-align: right; }

.rail { grid-area: rail; background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; align-items: stretch; padding: 8px 6px; gap: 4px; overflow-y: auto; }
.rail-btn { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 2px 6px; border-radius: var(--radius-s); border: 1px solid transparent; background: transparent; color: var(--ink-2); font-size: var(--fs-0); font-weight: 500; }
.rail-btn svg { width: 22px; height: 22px; }
.rail-btn:hover { background: var(--panel); color: var(--ink); }
.rail-btn.is-on { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

.drawer { grid-area: drawer; width: 0; overflow: hidden; background: var(--panel); border-right: 1px solid var(--line); transition: width .14s ease-out; }
.drawer.is-open { width: var(--drawer-w); }
.drawer-inner { width: var(--drawer-w); height: 100%; display: flex; flex-direction: column; }
.drawer-head { display: flex; align-items: center; gap: 8px; padding: 14px 14px 8px; }
.drawer-head h3 { flex: 1; }
.drawer-body { padding: 6px 14px 20px; display: flex; flex-direction: column; gap: 14px; overflow-y: auto; flex: 1; }
@media (prefers-reduced-motion: reduce) { .drawer { transition: none; } }

.tips { grid-area: tips; min-height: 0; }
.tip { display: flex; align-items: center; gap: 10px; padding: 7px 14px; background: var(--wood-soft); color: #5A3E12; font-size: var(--fs-1); border-bottom: 1px solid #EAD9BC; }
.tip svg { width: 18px; height: 18px; flex: none; color: var(--wood); }
.tip .txt { flex: 1; }
.tip .btn { color: inherit; }

.canvas-area { grid-area: canvas; position: relative; background: var(--mat); overflow: hidden; display: grid; grid-template-columns: var(--ruler-w) minmax(0, 1fr); grid-template-rows: var(--ruler-w) minmax(0, 1fr); min-height: 0; min-width: 0; }
.ruler { background: #F4F1EA; }
.ruler-corner { grid-column: 1; grid-row: 1; border-right: 1px solid var(--line-2); border-bottom: 1px solid var(--line-2); }
.ruler-top { grid-column: 2; grid-row: 1; border-bottom: 1px solid var(--line-2); }
.ruler-left { grid-column: 1; grid-row: 2; border-right: 1px solid var(--line-2); }
.canvas-wrap { grid-column: 2; grid-row: 2; position: relative; overflow: hidden; min-height: 0; min-width: 0;
  background-image: linear-gradient(var(--mat-grid) 1px, transparent 1px), linear-gradient(90deg, var(--mat-grid) 1px, transparent 1px);
  background-size: 40px 40px; }
.canvas-wrap canvas { display: block; }
.canvas-wrap.is-panning { cursor: grab; }
.sel-toolbar { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); display: flex; gap: 4px; padding: 4px; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-2); z-index: 5; }
.sel-toolbar .vsep { width: 1px; background: var(--line); margin: 4px 2px; }
.canvas-hint { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); background: rgba(255,255,255,.9); color: var(--ink-2); font-size: var(--fs-0); padding: 4px 10px; border-radius: 12px; pointer-events: none; }

.pages { grid-area: pages; background: var(--surface); border-top: 1px solid var(--line); display: flex; align-items: center; gap: 8px; padding: 6px 12px; min-height: 0; }
.pages.is-hidden { display: none; }
.page-thumb { display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 4px 6px; border-radius: var(--radius-s); border: 1px solid transparent; background: transparent; font-size: var(--fs-0); color: var(--ink-2); }
.page-thumb .box { width: 56px; height: 42px; background: var(--panel); border: 1px solid var(--line-2); display: grid; place-items: center; overflow: hidden; }
.page-thumb .box img { max-width: 100%; max-height: 100%; }
.page-thumb.is-on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.pages .count { font-size: var(--fs-1); color: var(--ink-2); margin-left: auto; }

.props { grid-area: props; background: var(--surface); border-left: 1px solid var(--line); overflow-y: auto; display: flex; flex-direction: column; }
.props-section { padding: 14px 16px; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 10px; }
.props-section h4 { color: var(--ink-2); font-size: var(--fs-1); font-family: var(--font-ui); font-weight: 600; }
.props-empty { padding: 26px 18px; color: var(--ink-2); font-size: var(--fs-1); display: flex; flex-direction: column; gap: 10px; }
.props-empty .kv { display: grid; grid-template-columns: 90px 1fr; gap: 4px 10px; font-size: var(--fs-1); }
.props-empty .kv b { font-weight: 500; color: var(--ink); }
.dims { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.dims .field { flex-direction: row; align-items: center; gap: 6px; }
.dims .field > label { width: 18px; font-weight: 600; color: var(--ink-3); }
.dims .input { text-align: right; min-height: 32px; padding: 4px 8px; }
.dims-foot { display: flex; align-items: center; gap: 6px; }
.layer-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.layer-chip { display: inline-flex; align-items: center; gap: 7px; min-height: var(--target); padding: 0 12px; border-radius: 18px; border: 1px solid var(--line-2); background: var(--surface); font-weight: 500; font-size: var(--fs-1); }
.layer-chip .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--c); border: 1px solid rgba(0,0,0,.12); }
.layer-chip:hover { border-color: var(--ink-3); }
.layer-chip.is-on { border-color: var(--c); background: color-mix(in srgb, var(--c) 10%, white); color: var(--ink); box-shadow: inset 0 0 0 1px var(--c); }
.layer-desc { font-size: var(--fs-1); color: var(--ink-2); line-height: 1.4; }
.font-pick { display: flex; flex-direction: column; gap: 4px; max-height: 240px; overflow-y: auto; border: 1px solid var(--line); border-radius: var(--radius-s); padding: 4px; background: var(--panel); }
.font-opt { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: var(--target); padding: 4px 10px; border-radius: 5px; border: 1px solid transparent; background: var(--surface); text-align: left; font-size: 19px; line-height: 1.1; }
.font-opt small { font-family: var(--font-ui); font-size: var(--fs-0); color: var(--ink-3); }
.font-opt:hover { border-color: var(--line-2); }
.font-opt.is-on { border-color: var(--accent); background: var(--accent-soft); }
.font-current { display: flex; align-items: center; justify-content: space-between; min-height: var(--target); padding: 4px 10px; border: 1px solid var(--line-2); border-radius: var(--radius-s); background: var(--surface); font-size: 18px; }
.color-row { display: flex; align-items: center; gap: 8px; }
.color-row input[type=color] { width: 36px; height: 32px; padding: 0; border: 1px solid var(--line-2); border-radius: 6px; background: none; }
.swatches { display: flex; gap: 6px; flex-wrap: wrap; }
.swatch { width: 24px; height: 24px; border-radius: 50%; border: 1px solid rgba(0,0,0,.15); padding: 0; }
.swatch.is-on { outline: 2px solid var(--accent); outline-offset: 1px; }

/* tool drawer bits */
.shape-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.shape-btn { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 4px 8px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-s); font-size: var(--fs-0); color: var(--ink-2); }
.shape-btn svg { width: 28px; height: 28px; color: var(--ink); }
.shape-btn:hover { border-color: var(--accent); color: var(--ink); }
.img-list { display: flex; flex-direction: column; gap: 8px; }
.drop { border: 2px dashed var(--line-2); border-radius: var(--radius); padding: 20px 14px; text-align: center; color: var(--ink-2); font-size: var(--fs-1); background: var(--surface); display: flex; flex-direction: column; gap: 8px; align-items: center; }
.drop.is-over { border-color: var(--accent); background: var(--accent-soft); }
.tpl-list { display: flex; flex-direction: column; gap: 8px; }
.tpl-item { display: flex; align-items: center; gap: 10px; padding: 8px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-s); text-align: left; min-height: 44px; }
.tpl-item .thumb { width: 64px; height: 48px; background: var(--mat); display: grid; place-items: center; flex: none; border-radius: 4px; overflow: hidden; color: rgba(255,255,255,.7); }
.tpl-item .thumb img { max-width: 92%; max-height: 92%; background: #fff; box-shadow: 0 2px 6px rgba(0,0,0,.35); }
.tpl-item .thumb svg { width: 20px; height: 20px; }
.tpl-item:active { transform: scale(.99); }

/* decor library (inside the Shapes drawer) */
.decor-head { display: flex; align-items: baseline; gap: 8px; }
.decor-head h4 { font-size: var(--fs-2); font-family: var(--font-display); }
.decor-chips { display: flex; flex-wrap: wrap; gap: 5px; }
.decor-chip { min-height: 28px; padding: 0 10px; border-radius: 14px; border: 1px solid var(--line-2); background: var(--surface); color: var(--ink-2); font-size: var(--fs-0); font-weight: 500; }
.decor-chip:hover { border-color: var(--accent); color: var(--ink); }
.decor-chip.is-on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.decor-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.decor-btn { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 6px 4px 5px; min-height: 44px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-s); color: var(--ink); font-size: 11px; line-height: 1.2; text-align: center; }
.decor-btn svg { width: 56px; height: 56px; display: block; }
.decor-btn .n { width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--ink-2); }
.decor-btn:hover, .decor-btn:focus-visible { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.decor-btn:active { transform: scale(.97); }
.decor-status { min-height: 16px; }
.decor-actions .btn { gap: 4px; }

/* Align & arrange strip (above the canvas while something is selected) */
.arrange { grid-area: arrange; display: flex; flex-wrap: wrap; align-items: center; gap: 2px; padding: 4px 10px; background: var(--surface); border-bottom: 1px solid var(--line); min-height: 44px; }
.arrange.is-hidden { display: none; }
.arrange .lbl { font-size: var(--fs-0); color: var(--ink-3); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; margin-right: 6px; white-space: nowrap; }
.arrange .vsep { width: 1px; height: 22px; background: var(--line); margin: 0 6px; flex: none; }
.arrange .btn { flex: none; min-width: var(--target); }
.arrange .btn.is-on { background: var(--accent-soft); color: var(--accent); }
.arrange .btn .t { font-size: var(--fs-0); }
.tpl-item .t { flex: 1; display: flex; flex-direction: column; gap: 2px; }
.tpl-item .t .n { font-weight: 600; font-size: var(--fs-1); }
.tpl-item:hover { border-color: var(--accent); }
.tpl-item:disabled { opacity: .6; cursor: default; }
.text-obj-list { display: flex; flex-direction: column; gap: 6px; }
.text-obj { text-align: left; padding: 8px 10px; border: 1px solid var(--line); background: var(--surface); border-radius: var(--radius-s); font-size: var(--fs-1); }
.text-obj.is-on { border-color: var(--accent); background: var(--accent-soft); }
.stat { padding: 10px 12px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-s); font-size: var(--fs-1); }
.stat b { font-family: var(--font-display); font-size: var(--fs-3); font-weight: 700; display: block; }
.trace-preview { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-s); height: 220px; display: grid; place-items: center; overflow: hidden; }
.trace-preview svg { max-width: 100%; max-height: 100%; }
.trace-preview img { max-width: 100%; max-height: 100%; }

/* export dialog */
.layer-previews { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.layer-preview { display: flex; flex-direction: column; gap: 6px; }
.layer-preview .box { aspect-ratio: var(--ar, 1 / 1); min-height: 64px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius-s); display: grid; place-items: center; overflow: hidden; }
.layer-preview .box img { max-width: 92%; max-height: 92%; filter: drop-shadow(0 0 0.5px rgba(0,0,0,.4)); }
.layer-preview .cap { display: flex; align-items: center; gap: 6px; font-size: var(--fs-1); }
.layer-preview.is-empty { opacity: .5; }
.opt-block { display: flex; flex-direction: column; gap: 8px; padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); }
.opt-block .row { justify-content: flex-start; gap: 18px; }
.opt-block .title { font-weight: 600; }
.mockup-img { max-width: 100%; max-height: 70vh; border-radius: var(--radius-s); background: var(--panel); }

/* ---------- settings ---------- */
.settings { height: 100%; display: grid; grid-template-columns: 220px 1fr; grid-template-rows: var(--topbar-h) 1fr; grid-template-areas: "top top" "nav main"; }
.settings .topbar { grid-area: top; }
.settings-nav { grid-area: nav; background: var(--surface); border-right: 1px solid var(--line); padding: 12px 8px; display: flex; flex-direction: column; gap: 2px; }
.settings-nav .nav-btn { text-align: left; padding: 10px 12px; border-radius: var(--radius-s); border: 0; background: transparent; color: var(--ink-2); font-weight: 500; min-height: var(--target); }
.settings-nav .nav-btn:hover { background: var(--panel); color: var(--ink); }
.settings-nav .nav-btn.is-on { background: var(--accent-soft); color: var(--accent); }
.settings-main { grid-area: main; overflow-y: auto; padding: 26px 32px 60px; }
.settings-main .inner { max-width: 980px; display: flex; flex-direction: column; gap: 20px; }
.settings-main h2 { font-size: var(--fs-4); }
.table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.table th, .table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); font-size: var(--fs-1); vertical-align: middle; }
.table th { color: var(--ink-2); font-weight: 600; background: var(--panel); }
.table tr:last-child td { border-bottom: 0; }
.table .actions { display: flex; gap: 6px; justify-content: flex-end; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; display: flex; flex-direction: column; gap: 12px; }
.card h3 { font-size: var(--fs-3); }
.preset-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.machine-card { display: flex; align-items: center; gap: 14px; }
.machine-card .ico { width: 40px; height: 40px; color: var(--accent); }
.machine-card .ico svg { width: 100%; height: 100%; }
.machine-card .t { flex: 1; }
.blank-stage { position: relative; display: inline-block; max-width: 100%; user-select: none; }
.blank-stage img { max-width: 100%; display: block; border-radius: var(--radius-s); }
.blank-stage .rect { position: absolute; border: 2px solid var(--layer-cut); background: rgba(229,57,53,.12); pointer-events: none; }
.blank-stage.is-drawing { cursor: crosshair; }
.pack-list { display: flex; flex-direction: column; gap: 8px; }
.pack-item { display: flex; align-items: center; gap: 12px; padding: 10px 12px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-s); }
.pack-item .t { flex: 1; }
.pack-item .n { font-weight: 600; }
.code { font-family: ui-monospace, Consolas, monospace; font-size: var(--fs-0); background: var(--panel); padding: 2px 6px; border-radius: 4px; }
.notice { padding: 12px 14px; background: var(--warn-soft); border-radius: var(--radius-s); font-size: var(--fs-1); color: #5A3E12; }

/* responsiveness: keep usable on a narrow window */
@media (max-width: 1280px) {
  :root { --props-w: 280px; --drawer-w: 260px; }
  .topbar { gap: 6px; }
  .topbar .pname { max-width: 180px; }
  .topbar .machine span, .topbar .saved { display: none; }
  .topbar .btn-big { padding: 0 14px; }
  .topbar .btn-mockup .lbl { display: none; }
}
@media (max-width: 1100px) {
  .topbar .machine { display: none; }
  .topbar .zoom .pct { display: none; }
}
@media (max-width: 900px) {
  .editor { grid-template-columns: var(--rail-w) auto 1fr; grid-template-areas: "top top top" "rail drawer arrange" "rail drawer tips" "rail drawer canvas" "rail drawer pages"; }
  .props { position: fixed; right: 0; top: var(--topbar-h); bottom: 0; width: var(--props-w); box-shadow: var(--shadow-2); z-index: 20; }
  .settings { grid-template-columns: 1fr; grid-template-areas: "top" "nav" "main"; grid-template-rows: var(--topbar-h) auto 1fr; }
  .settings-nav { flex-direction: row; flex-wrap: wrap; border-right: 0; border-bottom: 1px solid var(--line); }
  .home-inner { padding: 20px 16px 40px; }
}

/* ---------- Letter Board tool (felt board phrases) ---------- */
.lb-lines { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.lb-line { padding: 8px 10px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-s); display: flex; flex-direction: column; gap: 6px; }
.lb-line-text { font-size: var(--fs-1); font-weight: 600; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lb-line-ctl { gap: 6px; flex-wrap: wrap; align-items: center; }
.lb-line-ctl .swatches { gap: 4px; }
.lb-line-ctl .swatch { width: 20px; height: 20px; }
.lb-line-ctl .input-num { width: 56px; }
.lb-fonts { max-height: 300px; }
.lb-fonts .font-opt { font-size: 24px; }
.lb-icons { margin-top: 4px; }
.lb-warnings { display: flex; flex-direction: column; gap: 6px; }
.lb-warnings:empty { display: none; }
.lb-stat { margin-top: 4px; }
.lb-calib .grid-3 { margin-bottom: 6px; }
.lb-calib .input-num { width: 100%; }
.lb-felts .swatch { width: 28px; height: 28px; border-color: rgba(0,0,0,.3); }
.drawer-body > .expander > .expander-body { display: flex; flex-direction: column; gap: 10px; color: var(--ink); }

/* ---------- category tiles & browse (1.5.0) ---------- */
.search-field { position: relative; display: flex; align-items: center; min-width: 220px; }
.search-field .search-ico { position: absolute; left: 11px; display: flex; color: var(--ink-3); pointer-events: none; }
.search-field .input { padding-left: 36px; width: 100%; }
.search-field .input::-webkit-search-cancel-button { cursor: pointer; }

.cat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.cat-tile {
  display: flex; align-items: stretch; gap: 0; text-align: left; text-decoration: none; color: inherit;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden;
  transition: transform .12s ease-out, box-shadow .12s ease-out, border-color .12s;
}
.cat-tile:hover, .cat-tile:focus-visible { border-color: var(--accent); box-shadow: var(--shadow-2); transform: translateY(-2px); }
.cat-tile:active { transform: translateY(0) scale(.995); }
.cat-collage {
  position: relative; flex: none; width: 112px; background: var(--mat); overflow: hidden;
  background-image: linear-gradient(var(--mat-grid) 1px, transparent 1px), linear-gradient(90deg, var(--mat-grid) 1px, transparent 1px);
  background-size: 12px 12px;
}
/* three thumbnails fanned like a small stack of finished pieces */
.cat-collage img { position: absolute; top: 50%; left: 50%; width: 58px; background: #fff; border-radius: 2px; box-shadow: var(--shadow-sheet); }
.cat-collage img.i0 { transform: translate(-72%, -52%) rotate(-6deg); z-index: 1; }
.cat-collage img.i1 { transform: translate(-50%, -50%) rotate(0deg);  z-index: 2; }
.cat-collage img.i2 { transform: translate(-28%, -48%) rotate(6deg);  z-index: 3; }
.cat-collage.is-empty { display: grid; place-items: center; color: rgba(255,255,255,.55); }
.cat-meta { flex: 1; min-width: 0; padding: 12px 14px; display: flex; flex-direction: column; gap: 3px; justify-content: center; }
.cat-top { display: flex; align-items: center; gap: 8px; }
.cat-ico { display: flex; color: var(--accent); }
.cat-name { font-family: var(--font-display); font-size: var(--fs-3); line-height: 1.1; }
.cat-blurb { font-size: var(--fs-0); color: var(--ink-2); line-height: 1.35; }
.cat-count { font-size: var(--fs-0); color: var(--ink-3); font-variant-numeric: tabular-nums; margin-top: 2px; }

.more-btn { align-self: flex-start; }
.browse-head { display: flex; align-items: flex-end; gap: 16px; flex-wrap: wrap; }
.browse-head .browse-title h1 { font-size: var(--fs-5); }
.browse-head .browse-title p { margin-top: 2px; }
.browse-results { display: flex; flex-direction: column; gap: 14px; }
.browse-group { margin-top: 8px; }
.browse-group h3 { font-size: var(--fs-3); font-family: var(--font-display); }

/* ============================================================================
   TOUCH & MOBILE — 1.4.0
   Added additively: every rule below lives in a media query, so the desktop
   layout above 1100px is byte-for-byte unchanged. Three tiers:
     (pointer: coarse)                      -> hit targets & font sizes (any width)
     max-width 1099px, min-width 760px      -> tablet  (see PHONE/TABLET SHELL below)
     max-width 759px                        -> phone   (see PHONE/TABLET SHELL below)
   Do not "tidy" these into the blocks above; keeping them separate is what
   guarantees a desktop regression is impossible. See SPEC.md section 10.
   ========================================================================== */

/* ---- 1. hit targets: one token drives ~10 selectors ---- */
@media (pointer: coarse) {
  :root { --target: 44px; }

  /* controls that hard-code a smaller height */
  .btn-sm { min-height: var(--target); }
  .btn-sm.btn-icon { width: var(--target); }
  .tpl-filter { min-height: var(--target); padding: 0 14px; }
  .decor-chip { min-height: var(--target); padding: 0 14px; }
  button.chip { min-height: var(--target); height: auto; border-radius: var(--radius-s); }
  .text-obj { min-height: var(--target); display: flex; align-items: center; }
  .page-thumb { min-height: var(--target); }
  .dims .input { min-height: var(--target); }
  .range { min-height: var(--target); }
  .color-row input[type=color] { width: var(--target); height: var(--target); }

  /* color swatches: 20px dots were the smallest targets in the app. A full 44px dot is
     what Apple's own colour pickers use, and seven of them still fit a full-width sheet. */
  .swatch { width: var(--target); height: var(--target); }
  .swatches { gap: 8px; }
  .lb-line-ctl .swatch { width: var(--target); height: var(--target); }
  .lb-line-ctl .swatches { gap: 8px; }
  .lb-felts .swatch { width: var(--target); height: var(--target); }

  /* .btn-group pulls each button 1px left, which lands a 44px button at 43px */
  .btn-sm, .btn { min-width: var(--target); }

  /* 16px is the threshold below which iOS Safari zooms the page on focus */
  .input, .select, .textarea, .topbar .pname { font-size: 16px; }
  .input-sm { font-size: 16px; min-height: var(--target); padding: 6px 10px; }
  .dims .input { font-size: 16px; }
  .input-num { width: 92px; min-width: 72px; }
  .lb-line-ctl .input-num { width: 76px; }
  .lb-calib .input-num { width: 100%; min-width: 0; }   /* stretches in its own grid cell */

  /* hover-reveal is unreachable without a pointer */
  .proj-card .del { opacity: 1; }
}

/* A pointer that cannot hover must never gate functionality behind :hover. */
@media (hover: none) {
  .proj-card .del { opacity: 1; }
}

/* ---- 2. viewport units & safe areas (notches, home indicators) ---- */
@media (max-width: 1099px) {
  /* dvh tracks the collapsing mobile URL bar; the 100vh rule above is the fallback */
  .dialog { max-height: calc(100dvh - 40px); }
  .mockup-img { max-height: 70dvh; }
  .toasts { bottom: calc(22px + env(safe-area-inset-bottom)); }

  /* .topbar > * is flex:none with overflow:hidden, so it clips rather than wraps.
     Letting it wrap keeps every action reachable without hiding or scrolling any of them. */
  .topbar { flex-wrap: wrap; height: auto; min-height: var(--topbar-h); overflow: visible; }
  .editor { grid-template-rows: auto auto auto minmax(0, 1fr) auto; }

  /* dense 3-up rows are unusable at drawer width on touch (the 11 calibrate fields) */
  .drawer .grid-3 { grid-template-columns: 1fr 1fr; }
}

/* ---- 3. phone: stop the silent clipping ---- */
@media (max-width: 759px) {
  /* .row does not wrap by default, which is why dense dialog rows overflow */
  .row { flex-wrap: wrap; }
  .grid-3, .grid-2, .drawer .grid-3 { grid-template-columns: 1fr; }
  .props-empty .kv { grid-template-columns: 1fr; gap: 2px 0; }

  /* a nested scroller inside a scrolling sheet traps the gesture */
  .font-pick, .lb-fonts { max-height: none; }

  /* authored for a 292px drawer; on a full-width sheet it can breathe */
  .tpl-section .tpl-gallery.is-compact { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }

  .home-head { flex-wrap: wrap; align-items: flex-start; gap: 8px; }
  .home-head h1 { font-size: var(--fs-4); }
  .home-head .spacer { display: none; }          /* else the wrapped actions leave a dead band */
  .home-inner { gap: 22px; }
  .login-card { padding: 22px; }

  /* admin tables have no horizontal-scroll wrapper and would be clipped */
  .settings-main .inner > .table { display: block; overflow-x: auto; white-space: nowrap; }

  .dialog-head { padding: 14px 16px 8px; }
  .dialog-body { padding: 8px 16px 14px; }
  .dialog-foot { padding: 12px 16px calc(14px + env(safe-area-inset-bottom)); }
}

/* ---- 4. props panel shell (all widths) ----
   .props is now an outer shell wrapping .props-scroll, which is what renderProps() fills.
   On desktop the head is hidden and the shell behaves exactly as the old single element did. */
.props-head { display: none; }
.props-scroll { display: flex; flex-direction: column; }
.topbar .btn-props { display: none; }

/* ---- 5. TABLET: 760-1099px — rail stays, drawer and props become dismissible ---- */
@media (min-width: 760px) and (max-width: 1099px) {
  /* Both side panels leave the grid: rail 76 + drawer 260 + props 280 would leave a
     letterboard project (which opens its drawer on mount) only 56% of an iPad for canvas. */
  .editor {
    grid-template-columns: var(--rail-w) minmax(0, 1fr);
    grid-template-areas:
      "top top"
      "rail arrange"
      "rail tips"
      "rail canvas"
      "rail pages";
  }
  .drawer {
    position: fixed; left: var(--rail-w); top: var(--topbar-h); bottom: 0; width: var(--drawer-w);
    /* it starts --rail-w in from the edge, so -101% would leave it peeking over the rail */
    transform: translateX(calc(-100% - var(--rail-w) - 4px)); transition: transform .18s ease-out;
    box-shadow: var(--shadow-2); z-index: 29; pointer-events: none;
    display: flex; flex-direction: column; overflow: hidden;
  }
  .drawer.is-open { transform: translateX(0); width: var(--drawer-w); pointer-events: auto; }
  .drawer-inner { width: 100%; }
  .topbar .btn-props { display: inline-flex; }
  .props {
    position: fixed; right: 0; top: var(--topbar-h); bottom: 0; width: var(--props-w);
    transform: translateX(101%); transition: transform .18s ease-out;
    box-shadow: var(--shadow-2); z-index: 30; pointer-events: none;
  }
  .props.is-open { transform: translateX(0); pointer-events: auto; }
  .props-head {
    display: flex; align-items: center; gap: 8px; padding: 12px 10px 8px 16px;
    border-bottom: 1px solid var(--line); flex: none;
  }
  .props-head h3 { flex: 1; font-size: var(--fs-3); }
  .props-scroll { overflow-y: auto; flex: 1; }
  .props { overflow: hidden; }
}

/* ---- 6. PHONE: <=759px — single column, bottom tab bar, bottom sheets ---- */
@media (max-width: 759px) {
  /* The canvas is the only thing that needs the width. Everything else overlays it. */
  .editor {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto auto minmax(0, 1fr) auto auto;
    grid-template-areas:
      "top"
      "arrange"
      "tips"
      "canvas"
      "pages"
      "rail";
  }

  /* Two rows, not three: every row of chrome is a row stolen from the canvas.
     Zoom in/out are the only controls dropped anywhere in this release — pinch is a
     strict superset of them and Fit stays. See SPEC.md section 10, rule 5. */
  .topbar { flex-wrap: wrap; height: auto; min-height: var(--topbar-h); padding: 5px 8px; gap: 5px; overflow: visible; }
  .topbar .pname { flex: 1 1 100px; min-width: 80px; max-width: none; }
  .topbar .select { flex: 1 1 110px; min-width: 96px; max-width: none; }
  .topbar .spacer { display: none; }
  .topbar .btn-props { display: inline-flex; }
  .topbar .btn-zoom-out, .topbar .btn-zoom-in { display: none; }
  .topbar .btn-big { font-size: var(--fs-2); padding: 0 16px; }

  /* the tip bar runs to three lines at this width and is pure overhead above the canvas */
  .tip { padding: 6px 10px; font-size: var(--fs-0); gap: 8px; }

  /* rail -> bottom tab bar, thumb-reachable, scrolls horizontally past the safe area */
  .rail {
    flex-direction: row; align-items: stretch; overflow-x: auto; overflow-y: hidden;
    border-right: 0; border-top: 1px solid var(--line);
    padding: 4px 6px calc(4px + env(safe-area-inset-bottom));
    gap: 2px; -webkit-overflow-scrolling: touch;
  }
  .rail-btn { flex: 0 0 auto; min-width: 66px; padding: 6px 6px 4px; }

  /* no rulers: unreadable at phone zoom, and 44px of canvas back (JS reads this token) */
  :root { --ruler-w: 0px; }
  .ruler, .ruler-corner, .ruler-top, .ruler-left { display: none; }

  /* drawer and props -> bottom sheets over the canvas, driven by the SAME .is-open class
     the desktop drawer already uses, so toggleTool()/closeDrawer() need no mobile branch. */
  .drawer, .props {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto; width: auto;
    height: min(58dvh, 480px); max-height: 58dvh;
    background: var(--panel); border: 0; border-top: 1px solid var(--line-2);
    border-radius: var(--radius-l) var(--radius-l) 0 0; box-shadow: var(--shadow-2);
    transform: translateY(101%); transition: transform .18s ease-out;
    z-index: 40; overflow: hidden; pointer-events: none;
    display: flex; flex-direction: column;
  }
  .drawer.is-open, .props.is-open { transform: translateY(0); width: auto; pointer-events: auto; }
  .drawer-inner { width: 100%; }
  .props { background: var(--surface); z-index: 41; }
  .props-head {
    display: flex; align-items: center; gap: 8px; padding: 12px 10px 10px 16px;
    border-bottom: 1px solid var(--line); flex: none;
  }
  .props-head h3 { flex: 1; font-size: var(--fs-3); }
  .props-scroll { overflow-y: auto; flex: 1; padding-bottom: env(safe-area-inset-bottom); }
  /* a grab-handle bar reads as "this sheet can be dismissed" */
  .drawer-head, .props-head { position: relative; }
  .drawer-head::before, .props-head::before {
    content: ""; position: absolute; top: 5px; left: 50%; transform: translateX(-50%);
    width: 36px; height: 4px; border-radius: 2px; background: var(--line-2);
  }
  .drawer-head { padding-top: 16px; }
  .props-head { padding-top: 16px; }
  .drawer-body { padding-bottom: calc(20px + env(safe-area-inset-bottom)); }

  /* the two floating/wrapping toolbars: scroll instead of overflowing or eating the canvas */
  .sel-toolbar {
    left: 8px; right: 8px; transform: none; max-width: none;
    overflow-x: auto; -webkit-overflow-scrolling: touch; justify-content: flex-start;
  }
  .arrange { flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .arrange .lbl { position: sticky; left: 0; background: var(--surface); z-index: 1; }
  .pages { overflow-x: auto; }
}

@media (prefers-reduced-motion: reduce) {
  .props { transition: none; }
}

/* ---- 7. canvas gestures ----
   Fabric sets touch-action:none on its own canvases; the wrap needs it too or the browser
   claims the gesture before the pinch/pan handlers in editor.js ever see it. */
@media (pointer: coarse) {
  .canvas-wrap { touch-action: none; }
  .canvas-wrap canvas { touch-action: none; }
}

/* ---- 8. dialogs as bottom sheets on a phone ----
   Every modal in the app goes through openDialog() in ui.js, so this one block covers all
   seven call sites — export, mockup, trace, apply-template, custom size and both settings
   editors. The frame was already fluid (min(560px,100%) with a scrolling body); what it
   lacked was a full-height sheet shape and action buttons that stay put while you scroll
   the densest form in the app. */
@media (max-width: 759px) {
  .dialog-backdrop { padding: 0; align-items: flex-end; }
  .dialog, .dialog-wide {
    width: 100%; max-width: 100%;
    height: 92dvh; max-height: 92dvh;
    border-radius: var(--radius-l) var(--radius-l) 0 0;
  }
  .dialog-head {
    position: sticky; top: 0; z-index: 2; flex: none;
    background: var(--surface); border-bottom: 1px solid var(--line);
    padding: 16px 16px 12px;
  }
  .dialog-head h2 { font-size: var(--fs-3); }
  .dialog-head::before {
    content: ""; position: absolute; top: 6px; left: 50%; transform: translateX(-50%);
    width: 36px; height: 4px; border-radius: 2px; background: var(--line-2);
  }
  .dialog-body { flex: 1; min-height: 0; }
  /* the primary action must never be below the fold of a long export form */
  .dialog-foot {
    position: sticky; bottom: 0; z-index: 2; flex: none;
    background: var(--surface); box-shadow: 0 -4px 12px rgba(38, 35, 30, 0.06);
  }
  .dialog-foot .btn { flex: 1 1 auto; }

  /* dense option rows inside the export dialog */
  .opt-block .row { gap: 10px; }
  .opt-block .row > .hint { flex: 1 1 100%; }
  .layer-previews { grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); }
}


/* ---- 9. categories, search and browse on touch and small screens — 1.5.0 ----
   Same contract as everything above: every rule is inside a media query, so the desktop
   layout is untouched. The category tiles are the main way into the library on a phone. */
@media (pointer: coarse) {
  .cat-tile { min-height: var(--target); }
  .search-field .input { min-height: var(--target); }
  .more-btn { width: 100%; }
}
@media (max-width: 1099px) {
  .cat-grid { grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
}
@media (max-width: 759px) {
  /* brand and sign-out share the first row; the search box takes the second */
  .home-head { flex-wrap: wrap; row-gap: 10px; align-items: center; }
  .home-head .brand { flex: 1 1 auto; min-width: 0; }
  .home-head .sub { display: none; }
  .home-head .search-field { order: 9; flex: 1 1 100%; min-width: 0; }
  .home-head .spacer { display: none; }

  /* a two-line heading beside a two-line caption is unreadable at this width */
  .section-head { flex-wrap: wrap; row-gap: 2px; }
  .section-head > .muted { flex: 1 1 100%; }
  .browse-head .browse-title { flex: 1 1 100%; }
  .browse-head > .muted { flex: 0 0 auto; order: -1; }

  .cat-grid { grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .cat-collage { width: 86px; }
  .cat-collage img { width: 44px; }
  .tpl-row > .tpl-card { flex: 0 0 72%; }
  .cat-blurb { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

  .browse-head { row-gap: 8px; }
  .browse-head .browse-title { flex: 1 1 100%; }
  .browse-head .browse-title h1 { font-size: var(--fs-4); }
  .browse .search-field { position: sticky; top: 0; z-index: 3; padding: 8px 0; background: var(--bg); }
}
