/* ============================================================
   InPlace — edit text in documents
   ============================================================ */

* { box-sizing: border-box; }
:root {
  --bg: #eef1f5;
  --panel: #ffffff;
  --panel-2: #f7f8fa;
  --line: #e4e7ec;
  --line-strong: #d0d5dd;
  --ink: #101828;
  --ink-2: #344054;
  --muted: #667085;
  --muted-2: #5b6476;
  --accent: #2563eb;
  --accent-600: #1d4ed8;
  --accent-soft: #eff4ff;
  --danger: #d92d20;
  --danger-soft: #fef3f2;
  --ok: #067647;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, .06);
  --shadow-md: 0 4px 14px rgba(16, 24, 40, .09);
  --shadow-lg: 0 14px 38px rgba(16, 24, 40, .16);
  --radius: 10px;
  --ui: "Segoe UI", system-ui, -apple-system, Arial, sans-serif;
}

html, body {
  height: 100%;
  height: 100dvh;
  overflow-x: clip;
}
body {
  margin: 0;
  font-family: var(--ui);
  font-size: 14px;
  color: var(--ink);
  background: var(--bg);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.btn:focus-visible,
.ghost:focus-visible,
.tb-toggle:focus-visible,
.size-control button:focus-visible,
.dropdown .menu button:focus-visible,
.tb-ink input[type="color"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.hidden { display: none !important; }

::-webkit-scrollbar { width: 11px; height: 11px; }
::-webkit-scrollbar-thumb { background: #c8cfda; border-radius: 8px; border: 3px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-thumb:hover { background: #adb7c5; border: 3px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-track { background: transparent; }

/* ============================ Top bar ============================ */
.topbar {
  height: 56px;
  flex: 0 0 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 14px 0 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: relative;
  z-index: 30;
}
.brand { display: flex; align-items: center; gap: 9px; min-width: 0; }
.logo {
  width: 30px; height: 30px;
  border-radius: 8px;
  background: linear-gradient(180deg, #3b82f6, var(--accent));
  color: #fff;
  display: grid; place-items: center;
  box-shadow: 0 2px 6px rgba(37, 99, 235, .28);
  flex: none;
}
.brand-name { font-size: 16px; font-weight: 700; letter-spacing: -.2px; }
.top-actions { display: flex; align-items: center; gap: 6px; }
.divider { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 34px;
  padding: 0 13px;
  border-radius: 8px;
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--ink-2);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  user-select: none;
  transition: background .13s, border-color .13s, color .13s, transform .05s, box-shadow .13s;
}
.btn:hover { background: var(--panel-2); border-color: #b9c2d0; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .42; cursor: not-allowed; transform: none; }
.btn.primary {
  background: var(--accent);
  border-color: var(--accent-600);
  color: #fff;
  box-shadow: 0 1px 3px rgba(37, 99, 235, .28);
}
.btn.primary:hover { background: var(--accent-600); }
.btn.icon { width: 34px; padding: 0; }
.btn.small { height: 30px; padding: 0 12px; font-size: 12.5px; }
.btn.done,
.btn.done:disabled {
  opacity: 1;
  color: var(--ok);
  border-color: #b7e4c7;
  background: #f0fdf4;
  cursor: default;
}

/* dropdown */
.dropdown { position: relative; }
.dropdown .menu {
  position: absolute;
  top: calc(100% + 7px);
  right: 0;
  min-width: 212px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 5px;
  display: none;
  z-index: 60;
}
.dropdown.open .menu { display: block; }
.dropdown .menu button {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  padding: 8px 10px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  text-align: left;
}
.dropdown .menu button:hover { background: var(--accent-soft); }
.dropdown .menu b { font-size: 13px; font-weight: 700; }
.dropdown .menu span { font-size: 11.5px; color: var(--muted); }

/* ============================ Stage ============================ */
.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.viewport {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: auto;
  display: flex;
  background-color: var(--bg);
  scrollbar-gutter: stable;
}
.canvas-wrap {
  position: relative;
  margin: auto;
  flex: none;
  background: #fff;
  box-shadow: 0 1px 3px rgba(16, 24, 40, .10), 0 16px 40px rgba(16, 24, 40, .13);
  border-radius: 2px;
  touch-action: none;
}
.canvas-wrap canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
#mainCanvas { position: relative; }
#overlayCanvas { pointer-events: none; }
.canvas-wrap.panning { cursor: grabbing !important; }
.canvas-wrap.space-pan { cursor: grab; }

/* Inline text editor: invisible input, canvas renders the glyphs */
.inline-text-editor {
  position: absolute;
  z-index: 6;
  margin: 0;
  padding: 0;
  border: 0;
  outline: 0;
  background: transparent;
  color: transparent;
  caret-color: var(--accent);
  resize: none;
  overflow: hidden;
  white-space: pre;
  word-break: normal;
  letter-spacing: 0;
  box-shadow: none;
  border-radius: 0;
}
.inline-text-editor::selection { background: rgba(37, 99, 235, .22); }

/* ============================ Empty state ============================ */
.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow: auto;
  z-index: 2;
}
.drop-card {
  width: min(520px, 100%);
  background: rgba(255, 255, 255, .94);
  border: 1.5px dashed #c6cedb;
  border-radius: 18px;
  padding: 34px 30px 28px;
  text-align: center;
  box-shadow: var(--shadow-md);
  transition: border-color .15s, background .15s, transform .15s;
}
.drop-card.dragover { border-color: var(--accent); background: #fff; transform: scale(1.01); }
.drop-icon {
  width: 58px; height: 58px;
  margin: 0 auto 14px;
  border-radius: 16px;
  background: var(--accent-soft);
  color: var(--accent);
  display: grid; place-items: center;
}
.drop-card h1 { margin: 0 0 6px; font-size: 21px; font-weight: 700; letter-spacing: -.3px; }
.drop-card .lead { margin: 0; color: var(--muted); font-size: 13.5px; line-height: 1.6; }
.drop-actions { display: flex; gap: 8px; justify-content: center; margin: 20px 0 14px; flex-wrap: wrap; }
.formats { margin: 0; font-size: 11px; color: var(--muted-2); }

.drop-overlay {
  position: absolute;
  inset: 12px;
  border: 2px dashed var(--accent);
  border-radius: 16px;
  background: rgba(239, 244, 255, .88);
  display: grid;
  place-items: center;
  z-index: 40;
  font-size: 15px;
  font-weight: 700;
  color: var(--accent);
  pointer-events: none;
}

/* ============================ Scan pill ============================ */
.scan-pill {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 250px;
  max-width: min(420px, calc(100% - 24px));
  padding: 9px 13px;
  background: rgba(255, 255, 255, .97);
  border: 1px solid var(--line);
  border-radius: 11px;
  box-shadow: var(--shadow-lg);
  z-index: 36;
  pointer-events: none;
  animation: pill-in .16s ease;
}
@keyframes pill-in { from { opacity: 0; transform: translate(-50%, -6px); } }
.scan-pill.done { border-color: #b7e4c7; }
.spinner {
  width: 16px; height: 16px;
  border-radius: 50%;
  border: 2px solid #dbe3f0;
  border-top-color: var(--accent);
  animation: spin .7s linear infinite;
  flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }
.scan-check { color: var(--ok); display: grid; place-items: center; flex: none; }
.scan-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 5px; }
.scan-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.scan-track { height: 4px; border-radius: 3px; background: #e8edf5; overflow: hidden; display: block; }
.scan-track i { display: block; height: 100%; width: 0%; background: var(--accent); border-radius: 3px; transition: width .2s ease; }
.scan-pct { font-size: 11.5px; font-weight: 700; color: var(--muted); font-variant-numeric: tabular-nums; flex: none; }
.scan-pill.done .scan-track { display: none; }

/* ============================ Text toolbar ============================ */
.text-toolbar {
  position: absolute;
  bottom: 52px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 9px;
  background: rgba(255, 255, 255, .98);
  border: 1px solid var(--line);
  border-radius: 11px;
  box-shadow: var(--shadow-lg);
  z-index: 34;
  max-width: calc(100% - 20px);
  animation: tb-in .15s ease;
}
@keyframes tb-in { from { opacity: 0; transform: translate(-50%, 5px); } }
.tb-field { display: flex; align-items: center; gap: 7px; }
.tb-field > span { font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .3px; }
.tb-sep { width: 1px; height: 22px; background: var(--line); }
.text-toolbar select {
  height: 30px;
  min-width: 108px;
  padding: 0 24px 0 8px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='11' height='11' viewBox='0 0 24 24' fill='none' stroke='%23667085' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") no-repeat right 8px center;
  font-size: 12.5px;
  font-weight: 600;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  outline: none;
}
.text-toolbar select::-ms-expand,
.lang-control select::-ms-expand { display: none; }
.text-toolbar select::picker-icon,
.lang-control select::picker-icon { display: none; }
.text-toolbar select:focus-visible { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37, 99, 235, .18); }
.size-control { display: flex; align-items: center; border: 1px solid var(--line-strong); border-radius: 7px; overflow: hidden; height: 30px; background: #fff; }
.size-control:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37, 99, 235, .16); }
.size-control button {
  width: 26px; height: 100%;
  border: 0;
  background: transparent;
  color: var(--ink-2);
  font-size: 15px;
  font-weight: 700;
  line-height: 1;
  display: grid;
  place-items: center;
  padding: 0;
}
.size-control button:hover { background: var(--panel-2); color: var(--accent); }
.size-control input {
  width: 42px;
  height: 100%;
  border: 0;
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
  text-align: center;
  font-size: 12.5px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  outline: none;
  padding: 0;
  background: #fff;
  -moz-appearance: textfield;
}
.size-control input::-webkit-outer-spin-button,
.size-control input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

.tb-ink input[type="color"] {
  width: 27px;
  height: 27px;
  padding: 2px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: #fff;
  cursor: pointer;
}
.tb-ink input[type="color"]::-webkit-color-swatch-wrapper { padding: 1px; }
.tb-ink input[type="color"]::-webkit-color-swatch { border: 0; border-radius: 4px; }
.tb-style { display: flex; align-items: center; gap: 4px; }
.tb-toggle {
  width: 27px;
  height: 27px;
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: #fff;
  color: var(--ink-2);
  font-size: 12px;
  font-weight: 800;
  line-height: 1;
  display: grid;
  place-items: center;
}
.tb-toggle:hover { background: var(--panel-2); }
.tb-toggle.active {
  background: var(--accent-soft);
  border-color: #a9c2ff;
  color: var(--accent-600);
}
.tb-toggle.italic { font-style: italic; }

/* ============================ Bottom bar ============================ */
.stagebar {
  flex: 0 0 34px;
  height: 34px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 12px;
  background: var(--panel);
  border-top: 1px solid var(--line);
  font-size: 11.5px;
  color: var(--muted);
  z-index: 20;
}
.status { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lang-control { display: flex; align-items: center; gap: 5px; flex: none; white-space: nowrap; }
.lang-control > span {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .3px;
  font-weight: 700;
  color: var(--muted-2);
}
.lang-control select {
  height: 24px;
  max-width: 132px;
  padding: 0 18px 0 6px;
  border: 1px solid transparent;
  border-radius: 6px;
  background-color: transparent;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 24 24' fill='none' stroke='%23667085' stroke-width='2.6' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 4px center;
  color: var(--ink-2);
  font-size: 11.5px;
  font-weight: 600;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  outline: none;
}
.lang-control select:hover { background-color: var(--panel-2); }
.lang-control select:focus-visible { border-color: var(--accent); background-color: #fff; box-shadow: 0 0 0 2px rgba(37, 99, 235, .18); }
.lang-control select:disabled { opacity: .5; cursor: default; }
.page-controls { display: flex; align-items: center; gap: 2px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.page-controls b { color: var(--ink-2); font-weight: 700; }
.ghost {
  height: 24px;
  min-width: 24px;
  padding: 0 6px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--ink-2);
  font-weight: 600;
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.ghost:hover { background: var(--panel-2); }
.ghost:disabled { opacity: .35; cursor: default; }
#pdfPrev, #pdfNext { font-size: 16px; line-height: 1; padding: 0 4px; }
.zoom-controls { display: flex; align-items: center; gap: 1px; white-space: nowrap; }
.zoom-value { min-width: 48px; font-variant-numeric: tabular-nums; color: var(--muted); font-weight: 600; }
.bar-sep { width: 1px; height: 15px; background: var(--line); margin: 0 4px; }
#zoomFit { font-size: 11.5px; padding: 0 8px; }

/* ============================ Toast ============================ */
.toast {
  position: fixed;
  bottom: 48px;
  left: 50%;
  transform: translate(-50%, 10px);
  background: #101828;
  color: #f9fafb;
  padding: 9px 16px;
  border-radius: 9px;
  font-size: 12.5px;
  font-weight: 600;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s, transform .18s;
  z-index: 200;
  max-width: min(520px, 90vw);
  text-align: center;
  line-height: 1.5;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: #912018; }
.toast.success { background: #085d3a; }

/* ============================ Responsive ============================ */
@media (max-width: 760px) {
  .brand-name { display: none; }
  .topbar { padding: 0 10px; gap: 8px; }
  .btn { padding: 0 10px; }
  #enhanceLabel { display: none; }
  #enhanceBtn { width: 34px; padding: 0; }
  .text-toolbar {
    gap: 6px;
    padding: 6px 8px;
    bottom: 44px;
    max-width: calc(100% - 16px);
    overflow-x: auto;
    scrollbar-width: none;
  }
  .text-toolbar::-webkit-scrollbar { display: none; }
  .text-toolbar > * { flex: none; }
  .tb-sep { display: none; }
  .tb-field > span { display: none; }
  .text-toolbar select { min-width: 74px; max-width: 96px; padding: 0 20px 0 7px; }
  .size-control button { width: 24px; }
  .size-control input { width: 34px; }
  #doneBtn { padding: 0 10px; }
  .stagebar { padding: 0 8px; gap: 8px; }
  .status { display: none; }
  .lang-control > span { display: none; }
  .lang-control select { max-width: 108px; }
  .page-controls { flex: 1; justify-content: center; }
}

@media (prefers-reduced-motion: reduce) {
  .scan-pill,
  .text-toolbar { animation: none; }
  .spinner { animation-duration: 1.8s; }
  .btn,
  .drop-card,
  .toast,
  .scan-track i { transition: none; }
}
