/* ===== Design tokens (auto light/dark) ===== */
:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e3e6eb;
  --text: #1f2430;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-press: #1d4ed8;
  --accent: #2563eb;
  --ok: #16a34a;
  --err: #dc2626;
  --warn: #d97706;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 4px 16px rgba(16,24,40,.06);
  --radius: 12px;
  --radius-sm: 8px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b22;
    --surface-2: #1f232c;
    --border: #2b303b;
    --text: #e6e8ee;
    --muted: #9aa3b2;
    --primary: #3b82f6;
    --primary-press: #2563eb;
    --accent: #60a5fa;
    --ok: #34d399;
    --err: #f87171;
    --warn: #fbbf24;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.35);
  }
}
* { box-sizing: border-box; }
/* The `hidden` attribute / .hidden DOM toggling must always win over the
   component `display:` rules below (otherwise overlays like #busy stay
   permanently on screen and blank the app). */
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 14px/1.45 system-ui, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}
h3 { margin: 0; font-size: 13px; letter-spacing: .02em; text-transform: uppercase; color: var(--muted); }
code { background: var(--surface-2); border: 1px solid var(--border); padding: 0 4px; border-radius: 4px; font-size: 12px; }
.muted { color: var(--muted); font-size: 12px; margin: 8px 0 0; }
.grow { flex: 1; }

.app { display: flex; flex-direction: column; height: 100vh; }

/* ===== Top bar ===== */
.topbar {
  display: flex; align-items: center; gap: 20px;
  padding: 10px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand .logo {
  width: 32px; height: 32px; display: grid; place-items: center;
  background: var(--primary); color: #fff; border-radius: 9px; font-size: 17px;
}
.brand small { display: block; color: var(--muted); font-size: 11px; }
.topbar-right { margin-left: auto; }
.preset-bar { display: flex; gap: 6px; align-items: center; }

/* ===== Segmented control ===== */
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; }
.seg.full { display: flex; width: 100%; }
.seg.full .seg-btn { flex: 1; }
.seg-btn {
  border: 0; background: transparent; color: var(--muted);
  padding: 6px 16px; border-radius: 7px; cursor: pointer; font: inherit; font-weight: 600;
}
.seg-btn.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* ===== Buttons / inputs ===== */
button { font: inherit; cursor: pointer; }
.primary {
  background: var(--primary); color: #fff; border: 0;
  padding: 9px 18px; border-radius: var(--radius-sm); font-weight: 600;
}
.primary:hover { background: var(--primary-press); }
.primary:disabled { opacity: .45; cursor: not-allowed; }
.ghost {
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  padding: 7px 12px; border-radius: var(--radius-sm);
}
.ghost:hover { background: var(--surface-2); }
.ghost:disabled { opacity: .45; cursor: not-allowed; }
input[type=text], input[type=number], select {
  width: 100%; padding: 8px 9px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); border-radius: var(--radius-sm);
}
input[type=range] { width: 100%; accent-color: var(--primary); }
select { cursor: pointer; }
label { display: block; margin: 10px 0 0; font-size: 13px; }
label.check { display: flex; align-items: center; gap: 8px; margin-top: 12px; }
label.check input { width: auto; }
.row { display: flex; gap: 8px; }
.row label { flex: 1; }
.tag {
  display: inline-block; background: var(--surface-2); border: 1px solid var(--border);
  padding: 2px 8px; border-radius: 999px; font-size: 12px; color: var(--muted);
}
.readout { margin-top: 10px; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 12px; }

/* ===== Layout ===== */
.body { display: flex; flex: 1; min-height: 0; }
.rail {
  width: 320px; flex: none; overflow-y: auto;
  padding: 16px; display: flex; flex-direction: column; gap: 14px;
  border-right: 1px solid var(--border); background: var(--bg);
}
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-h { padding: 12px 14px 0; }
.card-b { padding: 10px 14px 14px; }

.stage { flex: 1; min-width: 0; display: flex; flex-direction: column; padding: 16px; gap: 12px; }
.view { display: flex; flex-direction: column; flex: 1; min-height: 0; gap: 12px; }
.stage-tools { display: flex; align-items: center; gap: 10px; }
.pager { display: flex; align-items: center; gap: 8px; }

/* ===== Dropzone ===== */
.dropzone {
  border: 2px dashed var(--border); border-radius: var(--radius);
  background: var(--surface); transition: border-color .15s, background .15s;
  position: relative;
}
.dropzone:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.dropzone.small { padding: 10px; }
.dropzone.stagezone { flex: 1; min-height: 0; display: grid; place-items: center; overflow: auto; padding: 16px; }
.dropzone.drag { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 8%, var(--surface)); }
.dz-empty { display: flex; flex-direction: column; align-items: center; gap: 8px; color: var(--muted); cursor: pointer; padding: 18px; }
.dz-empty.big { padding: 60px 20px; font-size: 15px; }
.dz-ico { font-size: 30px; }
.dz-empty.big .dz-ico { font-size: 46px; }
.dz-filled { display: flex; align-items: center; gap: 10px; }
#sigThumb {
  max-width: 100%; max-height: 90px; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 6px; padding: 4px;
}
.chip-x {
  border: 1px solid var(--border); background: var(--surface); color: var(--muted);
  width: 24px; height: 24px; border-radius: 50%; line-height: 1;
}
.chip-x:hover { color: var(--err); border-color: var(--err); }

/* ===== Canvas + overlay ===== */
.canvas-wrap {
  position: relative; display: inline-block; line-height: 0;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 6px; box-shadow: var(--shadow); overflow: hidden;
}
.canvas-wrap canvas { display: block; }
.kw-layer { position: absolute; inset: 0; pointer-events: none; }
.guide { position: absolute; background: var(--accent); pointer-events: none; z-index: 5; }
.guide.v { top: 0; bottom: 0; width: 1px; }
.guide.h { left: 0; right: 0; height: 1px; }
.sig-overlay {
  position: absolute; border: 1.5px solid var(--primary);
  cursor: move; touch-action: none; z-index: 6;
  box-shadow: 0 0 0 1px rgba(255,255,255,.4);
}
.sig-overlay:focus-visible { outline: 2px solid var(--primary); outline-offset: 3px; }
.sig-overlay img { width: 100%; height: 100%; display: block; opacity: .9; }
.handle {
  position: absolute; width: 12px; height: 12px;
  background: var(--surface); border: 2px solid var(--primary); border-radius: 50%;
}
.handle.tl { left: -7px;  top: -7px;    cursor: nwse-resize; }
.handle.tr { right: -7px; top: -7px;    cursor: nesw-resize; }
.handle.bl { left: -7px;  bottom: -7px; cursor: nesw-resize; }
.handle.br { right: -7px; bottom: -7px; cursor: nwse-resize; }
.tpl-note { width: 100%; text-align: center; }

/* ===== Bulk ===== */
.bulk-grid { display: flex; gap: 12px; flex: 1; min-height: 0; }
.bulk-grid .dropzone { flex: 1; }
.bulk-side { width: 300px; flex: none; display: flex; flex-direction: column; gap: 10px; overflow: hidden; }
.progress { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px; }
.progress .bar { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress .bar span { display: block; height: 100%; width: 0; background: var(--primary); transition: width .2s; }
.progress small { color: var(--muted); display: block; margin-top: 6px; }
.file-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 6px; }
.file-list li {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 8px 10px; display: flex; align-items: center; gap: 8px;
}
.file-list .fl-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.file-list input[type=text] { width: 84px; padding: 4px 6px; font-size: 12px; }
.badge { font-size: 11px; padding: 2px 7px; border-radius: 999px; font-weight: 600; }
.badge.pending { background: var(--surface-2); color: var(--muted); }
.badge.ok { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.badge.error { background: color-mix(in srgb, var(--err) 18%, transparent); color: var(--err); }
.badge.skip { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.fl-skip { display: flex; align-items: center; gap: 4px; font-size: 11px; color: var(--muted); }

/* ===== Split ===== */
.sep { border: 0; border-top: 1px solid var(--border); margin: 14px 0 4px; }
#splitList { width: 100%; max-width: 560px; align-self: stretch; flex: none; max-height: 100%; }
#splitList .fl-name { font-size: 13px; }

/* ===== Toasts / busy ===== */
.toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--muted);
  box-shadow: var(--shadow); border-radius: var(--radius-sm); padding: 10px 14px;
  min-width: 240px; max-width: 360px; animation: slidein .2s ease;
}
.toast.ok { border-left-color: var(--ok); }
.toast.error { border-left-color: var(--err); }
.toast.info { border-left-color: var(--primary); }
.toast strong { display: block; font-size: 13px; }
.toast small { color: var(--muted); }
@keyframes slidein { from { transform: translateY(8px); opacity: 0; } }
.busy {
  position: fixed; inset: 0; background: rgba(15,17,21,.45);
  display: grid; place-items: center; z-index: 200; gap: 14px; grid-auto-flow: row;
  color: #fff;
}
.spinner {
  width: 42px; height: 42px; border: 4px solid rgba(255,255,255,.25);
  border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== SaaS: account widget, badges, modal, ads, footer, auth/account pages, consent ===== */
.acct { display: flex; align-items: center; gap: 10px; }
.acct .sep-v { width: 1px; height: 22px; background: var(--border); }
.plan-badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 9px; border-radius: 999px; font-size: 11px; font-weight: 600;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--muted);
}
.plan-badge.pro { background: rgba(37,99,235,.12); color: var(--primary); border-color: transparent; }
.plan-badge.free { color: var(--muted); }
.quota-pill { font-size: 12px; color: var(--muted); }
.quota-pill b { color: var(--text); }
.quota-pill.low b { color: var(--warn); }
.quota-pill.out b { color: var(--err); }
.acct a.link, .linkbtn {
  background: none; border: 0; color: var(--primary); cursor: pointer;
  font: inherit; padding: 4px 6px; border-radius: 6px; text-decoration: none;
}
.acct a.link:hover, .linkbtn:hover { background: var(--surface-2); }
.acct .email { font-size: 12px; color: var(--muted); max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Modal */
.modal { position: fixed; inset: 0; z-index: 300; display: grid; place-items: center; background: rgba(15,17,21,.5); padding: 16px; }
.modal-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); width: 100%; max-width: 460px; padding: 22px;
}
.modal-card h2 { margin: 0 0 6px; font-size: 18px; }
.modal-card p { color: var(--muted); margin: 0 0 16px; }
.modal-x { float: right; }
.plan-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 14px 0; }
.plan-opt {
  border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 14px; cursor: pointer;
  text-align: left; background: var(--surface-2);
}
.plan-opt:hover, .plan-opt.sel { border-color: var(--primary); }
.plan-opt .price { font-size: 20px; font-weight: 700; color: var(--text); }
.plan-opt .per { font-size: 12px; color: var(--muted); }
.plan-opt .save { color: var(--ok); font-size: 11px; font-weight: 600; }

/* Ads */
.ad-slot {
  position: relative;
  display: flex; align-items: center; justify-content: center;
  border: 1px dashed var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--muted); font-size: 11px;
  min-height: 90px; margin: 10px 0; overflow: hidden;
}
.ad-slot[data-hidden] { display: none; }
.ad-rail { min-height: 250px; }
.ad-tag { position: absolute; top: 4px; left: 6px; font-size: 9px; letter-spacing: .08em; opacity: .6; }

/* Footer */
.site-footer {
  border-top: 1px solid var(--border); background: var(--surface);
  padding: 12px 18px; display: flex; gap: 16px; align-items: center; flex-wrap: wrap;
  font-size: 12px; color: var(--muted);
}
.site-footer a { color: var(--muted); text-decoration: none; }
.site-footer a:hover { color: var(--text); }

/* Auth / Account standalone pages */
.page-wrap { max-width: 880px; margin: 0 auto; padding: 28px 18px 60px; }
.auth-card, .panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 24px; margin: 0 auto;
}
.auth-card { max-width: 400px; }
.auth-card h1 { font-size: 20px; margin: 0 0 4px; }
.auth-card .sub { color: var(--muted); margin: 0 0 18px; font-size: 13px; }
.field { display: block; margin: 0 0 14px; }
.field span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; }
.field input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); background: var(--surface-2); color: var(--text); font: inherit;
}
.field input:focus { outline: 2px solid var(--primary); outline-offset: 0; }
.btn-block { width: 100%; justify-content: center; }
.form-msg { font-size: 13px; margin: 0 0 14px; padding: 10px 12px; border-radius: var(--radius-sm); display: none; }
.form-msg.show { display: block; }
.form-msg.err { background: rgba(220,38,38,.1); color: var(--err); }
.form-msg.ok { background: rgba(22,163,74,.1); color: var(--ok); }
.auth-alt { text-align: center; margin: 16px 0 0; font-size: 13px; color: var(--muted); }
.auth-alt a { color: var(--primary); text-decoration: none; }
.topnav {
  display: flex; align-items: center; gap: 14px; padding: 12px 18px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.topnav .brand strong { font-size: 15px; }
.usage-bar { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; margin: 8px 0; }
.usage-bar > span { display: block; height: 100%; background: var(--primary); }
.kv { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.kv:last-child { border-bottom: 0; }
.kv b { font-weight: 600; }
.panel h2 { margin: 0 0 4px; font-size: 17px; }
.panel + .panel { margin-top: 18px; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 16px; }

/* Consent banner */
.consent {
  position: fixed; left: 16px; right: 16px; bottom: 16px; z-index: 320;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px 16px; display: flex; gap: 14px;
  align-items: center; flex-wrap: wrap; font-size: 13px;
}
.consent p { margin: 0; flex: 1; min-width: 220px; color: var(--muted); }
.consent a { color: var(--primary); text-decoration: none; }
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 9px 14px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font: inherit; cursor: pointer; text-decoration: none; }
.btn.primary { background: var(--primary); border-color: transparent; color: #fff; }
.btn.primary:hover { background: var(--primary-press); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* OAuth buttons + "or" divider on the auth page */
.oauth-btn { justify-content: center; margin-bottom: 10px; background: var(--surface); }
.oauth-btn:hover { border-color: var(--primary); }
.oauth-ic {
  display: inline-grid; place-items: center; width: 18px; height: 18px;
  border-radius: 4px; background: var(--surface-2); font-weight: 700; font-size: 12px;
}
.or { display: flex; align-items: center; gap: 10px; margin: 6px 0 16px; color: var(--muted); font-size: 12px; }
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--border); }
#alt br { line-height: 2.2; }

/* ===================== Editor & Tools ===================== */
/* Tool tabs hide the signing rail + original views, show our views. */
.tool-view { display: none; }
body[data-tv] .rail { display: none; }
body[data-tv] .ad-slot[data-ad-slot="leaderboard"] { display: none; }
body[data-tv="editor"] #view-editor { display: flex; }
body[data-tv="tools"]  #view-tools  { display: flex; }

/* Tools grid */
.tools-grid {
  display: grid; gap: 14px; overflow: auto; align-content: start;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}
.tool-card .card-b { display: flex; flex-direction: column; gap: 8px; }
.tool-card .tool-files { list-style: none; padding: 8px 0 0; margin: 0; max-height: 96px; overflow: auto; }
.tool-card .tool-files li { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool-card label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.tool-card .tool-run { margin-top: 4px; }
.tool-imgf input { margin-top: 4px; }

/* Editor layout */
.ed-grid { flex: 1; min-height: 0; display: flex; gap: 12px; }
.ed-pages {
  width: 150px; flex: none; overflow-y: auto; display: flex;
  flex-direction: column; gap: 8px; padding-right: 4px;
}
.ed-thumb {
  border: 2px solid var(--border); border-radius: 8px; background: var(--surface);
  cursor: pointer; overflow: hidden;
}
.ed-thumb.sel { border-color: var(--primary); }
.ed-thumb canvas { display: block; width: 100%; height: auto; }
.ed-thumb-bar {
  display: flex; align-items: center; gap: 2px; padding: 3px 4px;
  background: var(--surface-2); border-top: 1px solid var(--border);
}
.ed-thumb-bar span { flex: 1; font-size: 11px; color: var(--muted); }
.ed-thumb-bar button {
  border: none; background: transparent; cursor: pointer; font-size: 11px;
  color: var(--muted); padding: 2px 3px; border-radius: 4px;
}
.ed-thumb-bar button:hover { background: var(--surface); color: var(--text); }

.ed-stage { flex: 1; min-width: 0; overflow: auto; display: grid; place-items: start center; }
.ed-canvas-wrap { position: relative; box-shadow: var(--shadow); }
.ed-canvas-wrap canvas { display: block; }
.ed-layer { position: absolute; inset: 0; }
.ed-ink { position: absolute; inset: 0; pointer-events: none; }

.ed-box { position: absolute; box-sizing: border-box; min-width: 8px; min-height: 8px; }
.ed-box.ed-text { outline: 1px dashed var(--primary); padding: 1px 3px; line-height: 1.15; overflow: hidden; }
.ed-box.ed-rect, .ed-box.ed-highlight { border-radius: 2px; }
.ed-box.ed-image img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.ed-box .ed-del {
  position: absolute; top: -10px; right: -10px; width: 18px; height: 18px;
  border-radius: 50%; border: none; background: var(--err); color: #fff;
  font-size: 10px; cursor: pointer; display: none;
}
.ed-box:hover .ed-del { display: block; }
.ed-box .ed-handle {
  position: absolute; right: -5px; bottom: -5px; width: 12px; height: 12px;
  background: var(--primary); border: 2px solid var(--surface); border-radius: 3px;
  cursor: se-resize;
}

.ed-toolbar { display: flex; align-items: center; gap: 6px; width: 100%; }
.ed-toolbar .ed-t { width: 34px; padding: 6px 0; text-align: center; font-size: 14px; }
.ed-toolbar .ed-t.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.ed-toolbar input[type=color] { width: 34px; height: 32px; padding: 2px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); }
.ed-toolbar .sep-v { width: 1px; height: 24px; background: var(--border); }
.ed-toolbar .grow { flex: 1; }

/* ===================== Ask AI (Chat with your PDF) ===================== */
body.ai-active .rail { display: none; }
.ai-wrap { flex: 1; min-height: 0; display: flex; }

.ai-gate { flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px; }
.ai-gate-card {
  max-width: 540px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px;
}
.ai-gate-card h2 { margin: 0 0 8px; font-size: 20px; }
.ai-gate-card .muted { font-size: 13px; line-height: 1.6; }
.ai-check {
  display: flex; gap: 10px; align-items: flex-start; margin: 18px 0;
  font-size: 13px; color: var(--text); cursor: pointer;
}
.ai-check input { margin-top: 2px; }

.ai-chat { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 10px; }
.ai-bar { display: flex; align-items: center; gap: 10px; }
.ai-doc { display: flex; align-items: center; gap: 8px; min-width: 0; }
.ai-doc-name {
  font-size: 12px; color: var(--muted); max-width: 280px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.ai-transcript {
  flex: 1; min-height: 0; overflow: auto; border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--surface); padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
}
.ai-empty { margin: auto; text-align: center; color: var(--muted); }
.ai-empty .dz-ico { font-size: 38px; }

.ai-msg { display: flex; }
.ai-msg.user { justify-content: flex-end; }
.ai-msg-b {
  max-width: 75%; padding: 10px 13px; border-radius: 14px;
  font-size: 14px; line-height: 1.55; word-wrap: break-word;
}
.ai-msg.user .ai-msg-b { background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.ai-msg.assistant .ai-msg-b {
  background: var(--surface-2); border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.ai-msg-b p { margin: 0; }
.ai-msg-b p + p { margin-top: 8px; }
.ai-msg-b code { font-size: 12px; }
.ai-msg.pending .ai-msg-b { color: var(--muted); font-style: italic; }
.ai-msg.error .ai-msg-b {
  background: color-mix(in srgb, var(--err) 12%, transparent);
  border: 1px solid var(--err); color: var(--err);
}

.ai-composer { display: flex; gap: 8px; align-items: flex-end; }
.ai-composer textarea {
  flex: 1; resize: none; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); background: var(--surface); color: var(--text);
  font: inherit; line-height: 1.4; max-height: 160px;
}
.ai-composer .btn { white-space: nowrap; }

/* ===================== Responsive (tablets & phones) ===================== */
/* The app shell is a fixed-height, two-column desktop layout. These queries
   progressively tighten it, then (≤900px) switch to a single scrolling column.
   NOTE: the PDF/editor <canvas> elements are rendered at a fixed intrinsic
   pixel size and the signature-drag math maps screen px 1:1 to canvas px, so
   we never CSS-scale them — they keep intrinsic size and scroll inside their
   own viewport. Only thumbnails (.ed-thumb canvas) are allowed to fluid-scale. */

/* --- Narrow desktops / landscape tablets: reclaim space from the side rails --- */
@media (max-width: 1100px) {
  .topbar { gap: 14px; }
  .rail { width: 264px; }
  .bulk-side { width: 240px; }
  .ed-pages { width: 132px; }
}

/* --- Portrait tablets & phones: stack into one scrolling column --- */
@media (max-width: 900px) {
  /* Let the document scroll instead of trapping two panes inside 100vh. */
  html, body { height: auto; }
  body { overflow-x: hidden; }                 /* belt-and-suspenders against sideways drift */
  .app { height: auto; min-height: 100vh; }

  /* Avoid the iOS "zoom on focus" jump (fires when input text < 16px). */
  input[type=text], input[type=email], input[type=password],
  input[type=number], select, textarea { font-size: 16px; }

  /* Top bar: brand + account on row 1, tab strip scrolls on its own row. */
  .topbar { flex-wrap: wrap; gap: 10px; padding: 10px 12px; }
  .brand small { display: none; }
  .topbar-right { gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
  .topbar-right .sep-v { display: none; }
  .preset-bar { display: none; }               /* power-user convenience; hidden on small screens */
  .topbar .seg {
    order: 3; flex: 1 0 100%; display: flex; max-width: 100%;
    overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none;
  }
  .topbar .seg::-webkit-scrollbar { display: none; }
  .topbar .seg-btn { white-space: nowrap; }
  .acct { gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
  .acct .email { display: none; }              /* email lives on /account; declutter the bar */
  .acct a.link, .linkbtn { padding: 6px 8px; }

  /* Body: rail stacks above the stage, both full width, page scrolls. */
  .body { flex-direction: column; }
  .rail {
    width: auto; flex: none; max-height: none; overflow: visible;
    border-right: 0; border-bottom: 1px solid var(--border);
  }
  .stage { flex: none; padding: 12px; }
  .stage-tools { flex-wrap: wrap; }
  .view { flex: none; }

  /* Empty dropzones need a real tap target; loaded canvases scroll inside. */
  .dropzone.stagezone { flex: none; min-height: 56vh; max-height: 78vh; }
  .dz-empty.big { padding: 40px 16px; }

  /* Bulk: file list drops below the preview. */
  .bulk-grid { flex-direction: column; }
  .bulk-side { width: auto; overflow: visible; }
  .bulk-side .file-list { max-height: 46vh; }

  /* Split list spans the column. */
  #splitList { max-width: none; }

  /* Editor: page thumbnails become a horizontal scrolling strip. */
  .ed-grid { flex: none; flex-direction: column; }
  .ed-pages {
    width: auto; flex-direction: row; max-height: none;
    overflow-x: auto; overflow-y: hidden; padding: 0 0 6px; gap: 6px;
  }
  .ed-thumb { flex: 0 0 92px; }
  .ed-stage { flex: none; min-height: 54vh; max-height: 76vh; }
  .ed-toolbar { flex-wrap: wrap; row-gap: 8px; }
  .ed-toolbar .grow { display: none; }         /* with wrapping, the spacer just adds an empty gap */

  /* Tools: one card per row (the 280px min can overflow a 320px viewport). */
  .tools-grid { grid-template-columns: 1fr; }

  /* Ask-AI chat: keep it usefully tall inside the now-scrolling page. */
  .ai-wrap { min-height: 72vh; }
  .ai-msg-b { max-width: 88%; }
  .ai-doc-name { max-width: 46vw; }

  /* Upgrade modal: stack the plan options. */
  .plan-grid { grid-template-columns: 1fr; }

  /* Toasts span the viewport width rather than a fixed pill. */
  .toasts { left: 12px; right: 12px; bottom: 12px; }
  .toast { min-width: 0; max-width: none; }
}

/* --- Small phones: trim padding so content isn't squeezed --- */
@media (max-width: 560px) {
  .page-wrap { padding: 18px 14px 48px; }
  .auth-card, .panel { padding: 18px; }
  .modal { padding: 12px; }
  .modal-card { padding: 18px; }
  .ai-gate { padding: 16px; }
  .ai-gate-card { padding: 20px; }
  .consent { left: 10px; right: 10px; bottom: 10px; padding: 12px; }
  .consent .btn { flex: 1; justify-content: center; }
  .site-footer { padding: 12px 14px; gap: 10px 14px; }
}
