/* ---------- tokens ---------------------------------------------------- */
:root{
  --bg:#EEF2F0;
  --surface:#FFFFFF;
  --surface-2:#E4E9E6;
  --surface-3:#D6DEDA;
  --ink:#16232A;
  --ink-soft:#51636A;
  --ink-faint:#8B9A9A;
  --border:rgba(22,35,42,0.14);
  --border-strong:rgba(22,35,42,0.24);
  --brand:#1F4E68;
  --brand-ink:#ffffff;
  --accent:#C6402A;
  --accent-ink:#ffffff;
  --ok:#2E7D5B;
  --shadow: 0 1px 2px rgba(16,26,32,.07), 0 10px 28px -14px rgba(16,26,32,.22);
  --radius: 10px;
  --mono: ui-monospace, 'SFMono-Regular', Menlo, Consolas, 'Roboto Mono', monospace;
  --sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}
@media (prefers-color-scheme: dark){
  :root{
    --bg:#101A22; --surface-2:#1B2932; --surface-3:#243642;
    --ink:#E7EDEF; --ink-soft:#93A5AC; --ink-faint:#5B6F76;
    --border:rgba(231,237,239,0.14); --border-strong:rgba(231,237,239,0.26);
    --brand:#5C9BBC; --brand-ink:#0C171D; --accent:#E1613F; --accent-ink:#1A0D08; --ok:#4FAE85;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 14px 34px -16px rgba(0,0,0,.65);
  }
}
:root[data-theme="dark"]{
  --bg:#101A22; --surface-2:#1B2932; --surface-3:#243642;
  --ink:#E7EDEF; --ink-soft:#93A5AC; --ink-faint:#5B6F76;
  --border:rgba(231,237,239,0.14); --border-strong:rgba(231,237,239,0.26);
  --brand:#5C9BBC; --brand-ink:#0C171D; --accent:#E1613F; --accent-ink:#1A0D08; --ok:#4FAE85;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 14px 34px -16px rgba(0,0,0,.65);
}
:root[data-theme="light"]{
  --bg:#EEF2F0; --surface-2:#E4E9E6; --surface-3:#D6DEDA;
  --ink:#16232A; --ink-soft:#51636A; --ink-faint:#8B9A9A;
  --border:rgba(22,35,42,0.14); --border-strong:rgba(22,35,42,0.24);
  --brand:#1F4E68; --brand-ink:#ffffff; --accent:#C6402A; --accent-ink:#ffffff; --ok:#2E7D5B;
  --shadow: 0 1px 2px rgba(16,26,32,.07), 0 10px 28px -14px rgba(16,26,32,.22);
}

*{box-sizing:border-box; -webkit-tap-highlight-color:transparent;}
html,body{height:100%; overscroll-behavior:none;}
body{
  margin:0; background:var(--bg); color:var(--ink); font-family:var(--sans);
  -webkit-font-smoothing:antialiased; overscroll-behavior:none;
}
button{font:inherit; color:inherit; touch-action:manipulation; -webkit-touch-callout:none; -webkit-user-select:none; user-select:none;}
input, textarea{font:inherit;}
.keyhead, .eyebrow, .wordmark, .rail-hint, .tool span, .empty-hint, .toast, .dz-title, .dz-sub, .pageind, .showoncanvas{
  -webkit-touch-callout:none; -webkit-user-select:none; user-select:none;
}
::selection{background:var(--accent); color:var(--accent-ink);}
:focus-visible{outline:2px solid var(--brand); outline-offset:2px;}

.wordmark{
  font-family:var(--mono); font-weight:700; letter-spacing:.14em; text-transform:uppercase;
  font-size:15px; color:var(--ink);
}
.wordmark b{color:var(--accent);}

/* ---------- app shell --------------------------------------------------- */
.app{
  height:100dvh; display:flex; flex-direction:column;
  padding-left:env(safe-area-inset-left); padding-right:env(safe-area-inset-right);
}

.titlebar{
  display:flex; align-items:center; gap:14px;
  padding:calc(10px + env(safe-area-inset-top)) 16px 10px;
  background:var(--surface-2); border-bottom:1px solid var(--border);
  flex-wrap:wrap; row-gap:8px;
}
.titlebar .brandblock{ display:flex; align-items:baseline; gap:8px; margin-right:auto; }
.titlebar .brandblock .sub{ font-size:11px; color:var(--ink-soft); }
.filebar{ display:flex; gap:6px; flex-wrap:wrap; }

.btn{
  display:inline-flex; align-items:center; gap:6px; border:1px solid var(--border-strong);
  background:var(--surface); color:var(--ink); padding:7px 11px; border-radius:8px;
  font-size:12.5px; font-weight:600; cursor:pointer; white-space:nowrap;
  box-shadow:var(--shadow); transition:transform .08s ease, border-color .12s ease;
}
.btn:hover{ border-color:var(--brand); }
.btn:active{ transform:translateY(1px); }
.btn.primary{ background:var(--brand); border-color:var(--brand); color:var(--brand-ink); }
.btn.accent{ background:var(--accent); border-color:var(--accent); color:var(--accent-ink); }
.btn.ghost{ box-shadow:none; background:transparent; border-color:transparent; }
.btn.ghost:hover{ background:var(--surface-3); }
.btn.small{ padding:5px 8px; font-size:11.5px; border-radius:6px; }
.btn[disabled]{ opacity:.45; cursor:not-allowed; pointer-events:none; }

.body{ flex:1; display:flex; min-height:0; position:relative; }

/* ---------- tool rail ---------------------------------------------------- */
.toolrail{
  width:96px; flex:none; background:var(--surface-2); border-right:1px solid var(--border);
  display:flex; flex-direction:column; align-items:stretch; padding:14px 10px; gap:8px; overflow-y:auto;
}
.tool{
  display:flex; flex-direction:column; align-items:center; gap:5px; padding:10px 4px;
  border-radius:9px; border:1px solid transparent; background:transparent; cursor:pointer;
  color:var(--ink-soft);
}
.tool svg{ width:20px; height:20px; }
.tool span{ font-size:10px; font-weight:700; letter-spacing:.02em; text-transform:uppercase; }
.tool:hover{ background:var(--surface-3); color:var(--ink); }
.tool.active{ background:var(--brand); border-color:var(--brand); color:var(--brand-ink); }
.rail-hint{
  margin-top:auto; font-size:10.5px; line-height:1.5; color:var(--ink-faint); padding:8px 4px 0;
  border-top:1px dashed var(--border);
}

/* ---------- canvas stage --------------------------------------------------- */
.stagewrap{ flex:1; min-width:0; min-height:0; position:relative; display:flex; flex-direction:column; }
.stagescroll{
  flex:1; overflow:auto; touch-action:none; background:
    repeating-linear-gradient(0deg, var(--surface-3) 0 1px, transparent 1px 28px),
    repeating-linear-gradient(90deg, var(--surface-3) 0 1px, transparent 1px 28px),
    var(--bg);
  padding:36px;
  display:flex; align-items:flex-start; justify-content:flex-start;
}
.stage{
  position:relative; background:var(--surface); box-shadow:var(--shadow); flex:none;
  transform-origin: top left; display:none; touch-action:none;
}
.stage.has-doc{ display:block; }
.stage.tool-select .shape{ cursor:pointer; }
.stage .shape.text-shape{ cursor:text; }
.stage.tool-freehand,.stage.tool-rect{ cursor:crosshair; }
.stage.tool-text{ cursor:text; }
/* Belt-and-suspenders: touch-action:none on every layer, not just the SVG on top — Safari
   has been inconsistent about which element in a stack actually owns gesture handling. */
.stage svg{ position:absolute; inset:0; display:block; touch-action:none; }
.stage img.baseimg{ position:absolute; inset:0; width:100%; height:100%; object-fit:contain; display:block; user-select:none; -webkit-user-drag:none; background:#fff; touch-action:none; }
.stage canvas{ position:absolute; inset:0; width:100%; height:100%; display:block; pointer-events:none; touch-action:none; }
.stage.panning{ cursor:grabbing; }

.dropzone{
  position:absolute; inset:16px; border:2px dashed var(--border-strong); border-radius:14px;
  display:flex; align-items:center; justify-content:center; text-align:center; padding:24px;
  pointer-events:none; transition:border-color .15s ease, background .15s ease;
}
.dropzone.active{ border-color:var(--brand); background:color-mix(in srgb, var(--brand) 8%, transparent); }
.dz-title{ font-size:15px; font-weight:700; margin-bottom:6px; }
.dz-sub{ font-size:12px; color:var(--ink-soft); max-width:320px; }

.pagebar{
  position:absolute; left:50%; top:14px; transform:translateX(-50%);
  display:flex; align-items:center; gap:8px; background:var(--surface); border:1px solid var(--border-strong);
  border-radius:10px; box-shadow:var(--shadow); padding:5px 8px; z-index:30;
}
.pagebar button{ width:32px; height:32px; border-radius:7px; font-size:16px; font-weight:700; }
.pageind{ font-family:var(--mono); font-size:11.5px; color:var(--ink-soft); font-variant-numeric:tabular-nums; min-width:82px; text-align:center; }

.zoomrow{
  position:absolute; right:calc(14px + env(safe-area-inset-right)); bottom:calc(14px + env(safe-area-inset-bottom));
  display:flex; align-items:center; gap:2px;
  background:var(--surface); border:1px solid var(--border-strong); border-radius:10px; box-shadow:var(--shadow);
  padding:3px;
}
.zoomrow button{ width:38px; height:38px; border:none; background:transparent; color:var(--ink); border-radius:7px; cursor:pointer; font-size:17px; touch-action:manipulation; }
.zoomrow button.fitbtn{ width:auto; padding:0 10px; font-size:11px; font-weight:800; letter-spacing:.04em; text-transform:uppercase; font-family:var(--mono); }
.zoomrow button:hover{ background:var(--surface-3); }
.zoomrow .pct{ font-family:var(--mono); font-size:11px; width:46px; text-align:center; color:var(--ink-soft); font-variant-numeric:tabular-nums; }

/* ---------- floating contextual toolbars ---------------------------------- */
.floatbar{
  position:absolute; display:flex; align-items:center; gap:6px; background:var(--surface);
  border:1px solid var(--border-strong); border-radius:9px; box-shadow:var(--shadow); padding:6px;
  z-index:40;
}
.swatchbtn{ width:30px; height:30px; border-radius:7px; border:2px solid transparent; cursor:pointer; touch-action:manipulation; }
.swatchbtn.ring{ border-color:var(--ink); }
.floatbar .sep{ width:1px; align-self:stretch; background:var(--border); margin:0 2px; }
.floatbar select{ border:1px solid var(--border-strong); border-radius:6px; background:var(--surface); color:var(--ink); font-size:12.5px; padding:6px 4px; min-height:30px; }
.floatbar .iconbtn{ width:30px; height:30px; display:flex; align-items:center; justify-content:center; border-radius:7px; border:1px solid var(--border-strong); background:var(--surface); cursor:pointer; font-size:13px; font-weight:700; touch-action:manipulation; }
.floatbar .iconbtn.on{ background:var(--brand); color:var(--brand-ink); border-color:var(--brand); }
.floatbar .iconbtn.danger{ color:var(--accent); }

textarea.editorbox{
  position:absolute; z-index:50; resize:none; border:1.5px solid var(--brand); border-radius:5px;
  padding:5px 7px; background:rgba(255,255,255,.92); font-family:var(--mono); line-height:1.25;
  box-shadow:var(--shadow); min-width:60px; min-height:26px; overflow:hidden;
}

/* ---------- on-canvas legend (draggable / resizable) ------------------------ */
.legend-drag{ cursor:grab; touch-action:none; }
.legend-drag:active{ cursor:grabbing; }
.legend-resize{ cursor:nwse-resize; touch-action:none; }

/* ---------- legend / key panel --------------------------------------------- */
.keypanel{
  width:236px; flex:none; background:var(--surface-2); border-left:1px solid var(--border);
  display:flex; flex-direction:column; overflow:hidden;
}
.keyhead{ padding:14px 14px 8px; }
.keyhead .title{ font-size:12.5px; font-weight:700; }
.keyhead .desc{ font-size:11px; color:var(--ink-soft); margin-top:3px; line-height:1.5; }
.keylist{ flex:1; overflow-y:auto; padding:6px 10px 10px; display:flex; flex-direction:column; gap:6px; }
.keyrow{
  display:flex; align-items:center; gap:8px; padding:7px 8px; border-radius:8px;
  border:1px solid var(--border); background:var(--surface); cursor:pointer;
}
.keyrow.active{ border-color:var(--brand); box-shadow:0 0 0 1px var(--brand) inset; }
.keyrow input[type="color"]{
  width:32px; height:32px; padding:0; border:1px solid var(--border-strong); border-radius:7px;
  background:none; flex:none; cursor:pointer;
}
.keyrow input[type="text"]{
  flex:1; min-width:0; border:none; background:transparent; color:var(--ink); font-size:13px; font-weight:600;
  padding:6px 0;
}
.keyrow input[type="text"]:focus{ outline:none; }
.keyrow .del{
  flex:none; width:30px; height:30px; border:none; background:none; color:var(--ink-faint); cursor:pointer;
  font-size:16px; line-height:1; display:flex; align-items:center; justify-content:center; touch-action:manipulation;
}
.keyrow .del:hover{ color:var(--accent); }
.addkey{ margin:2px 10px 4px; }
.addkey button{ width:100%; justify-content:center; }
.showoncanvas{
  display:flex; align-items:center; gap:8px; margin:6px 14px 14px; font-size:11.5px; font-weight:600;
  color:var(--ink-soft); padding-top:10px; border-top:1px solid var(--border);
}
.showoncanvas input{ width:16px; height:16px; }

.toast{
  position:fixed; left:50%; bottom:22px; transform:translate(-50%,12px); background:var(--ink);
  color:var(--bg); padding:9px 16px; border-radius:8px; font-size:12.5px; font-weight:600;
  opacity:0; pointer-events:none; transition:opacity .18s ease, transform .18s ease; z-index:100;
  box-shadow:var(--shadow);
}
.toast.show{ opacity:1; transform:translate(-50%,0); }

.loading{
  position:fixed; inset:0; background:rgba(16,26,32,.42); z-index:200; display:none;
  align-items:center; justify-content:center; flex-direction:column; gap:12px; color:#fff; font-size:13px; font-weight:600;
}
.loading:not([hidden]){ display:flex; }
.spin{ width:30px; height:30px; border-radius:50%; border:3px solid rgba(255,255,255,.3); border-top-color:#fff; animation:spin .8s linear infinite; }
@keyframes spin{ to{ transform:rotate(360deg); } }

@media (prefers-reduced-motion: reduce){ *{ transition:none !important; } .spin{ animation-duration:1.6s; } }

/* ---------- responsive: phone / iPad portrait ------------------------------ */
.legendToggle{ display:none; }
.sheetbackdrop{ display:none; }

@media (max-width: 880px){
  .body{ flex-direction:column; position:relative; }
  .toolrail{
    width:auto; flex-direction:row; overflow-x:auto; -webkit-overflow-scrolling:touch;
    padding:8px calc(10px + env(safe-area-inset-right)) 8px calc(10px + env(safe-area-inset-left));
    border-right:none; border-bottom:1px solid var(--border); gap:6px;
  }
  .tool{ flex:none; flex-direction:row; min-height:44px; padding:8px 14px; }
  .rail-hint{ display:none; }
  .titlebar .brandblock{ width:100%; }
  .legendToggle{ display:inline-flex; }

  .keypanel{
    position:fixed; left:0; right:0; bottom:0; width:auto; max-height:60vh; z-index:60;
    border-left:none; border-top:1px solid var(--border); border-radius:16px 16px 0 0;
    box-shadow:0 -8px 28px -10px rgba(16,26,32,.35);
    padding-bottom:env(safe-area-inset-bottom);
    transform:translateY(105%); transition:transform .22s ease;
  }
  .keypanel.open{ transform:translateY(0); }
  .sheetbackdrop{
    display:block; position:fixed; inset:0; background:rgba(16,26,32,.32); z-index:55;
    opacity:0; pointer-events:none; transition:opacity .2s ease;
  }
  .sheetbackdrop.show{ opacity:1; pointer-events:auto; }
}
