.scanner-panel,
.scanner-panel *,
.scanner-workspace,
.scanner-workspace * {
  box-sizing: border-box;
}

.scanner-panel { display: grid; gap: 22px; }
.scanner-dropzone { min-height: 268px; }
.scan-start-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 9px; }
html.scanner-editor-open,
html.scanner-editor-open body { overflow: hidden; overscroll-behavior: none; }

/* Full-screen editor shell. Each region owns a grid track, so controls cannot
   overlap or displace the preview at any viewport size. */
#workspace.scanner-workspace:not([hidden]) {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: flex;
  width: 100%;
  height: 100dvh;
  min-width: 0;
  min-height: 0;
  flex-direction: column;
  overflow: hidden;
  border: 0;
  border-radius: 0;
  background: var(--surface);
  box-shadow: none;
}

.scanner-editor-header {
  position: relative;
  z-index: 5;
  display: flex;
  width: 100%;
  min-width: 0;
  min-height: 56px;
  flex: 0 0 56px;
  align-items: center;
  gap: 8px;
  overflow: hidden;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  padding: 7px 12px;
}
.scanner-editor-brand {
  display: inline-flex;
  min-width: 0;
  flex: 0 1 auto;
  align-items: center;
  gap: 7px;
  overflow: hidden;
  color: var(--ink);
  font-size: .95rem;
  font-weight: 800;
  white-space: nowrap;
}
.scanner-editor-brand img {
  width: 30px;
  height: 30px;
  flex: 0 0 30px;
  border-radius: 8px;
  box-shadow: 0 5px 13px rgba(109,94,252,.28);
}
.scanner-editor-brand b { color: var(--brand); }
.scanner-editor-title { display: grid; min-width: 0; flex: 0 1 auto; gap: 2px; }
.scanner-editor-title strong,
.scanner-editor-title span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scanner-editor-title strong { color: var(--ink); font-size: .9rem; }
.scanner-editor-title span { color: var(--muted); font-size: .7rem; }
.scanner-header-spacer { min-width: 0; flex: 1 1 auto; }
.scanner-head-actions { display: flex; min-width: 0; flex: 0 0 auto; align-items: center; gap: 4px; }
.scanner-head-button {
  display: inline-grid;
  width: 40px;
  min-width: 40px;
  height: 40px;
  min-height: 40px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface);
  color: var(--ink-soft);
  padding: 0;
  touch-action: manipulation;
}
.scanner-head-button:hover:not(:disabled) { border-color: var(--brand); color: var(--brand); }
.scanner-head-button svg { width: 20px; height: 20px; pointer-events: none; }

.scanner-editor-shell {
  display: grid;
  width: 100%;
  min-width: 0;
  min-height: 0;
  flex: 1 1 auto;
  grid-template-columns: 68px clamp(238px, 20vw, 282px) minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
  overflow: hidden;
}

.scanner-option-rail {
  display: flex;
  min-width: 0;
  min-height: 0;
  grid-column: 1;
  grid-row: 1;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  overflow-x: hidden;
  overflow-y: auto;
  scrollbar-width: thin;
  border-right: 1px solid var(--line);
  background: var(--surface);
  padding: 7px 5px;
}
.scanner-rail-button {
  display: flex;
  width: 58px;
  min-width: 58px;
  height: 51px;
  min-height: 51px;
  flex: 0 0 51px;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  overflow: hidden;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  color: var(--ink-soft);
  padding: 0 3px;
  font-size: .64rem;
  font-weight: 700;
  line-height: 1;
  touch-action: manipulation;
}
.scanner-rail-button > span:last-child {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.scanner-rail-button.on {
  border-color: color-mix(in srgb, var(--brand) 38%, var(--line));
  background: color-mix(in srgb, var(--brand) 14%, var(--surface));
  color: var(--brand);
}
.scanner-rail-icon { width: 21px; height: 21px; flex: 0 0 21px; pointer-events: none; }

.scanner-inspector {
  position: relative;
  z-index: 3;
  width: 100%;
  min-width: 0;
  min-height: 0;
  grid-column: 2;
  grid-row: 1;
  overflow: hidden;
  border-right: 1px solid var(--line);
  background: var(--surface);
  box-shadow: 8px 0 22px rgba(28,30,60,.045);
}
.scanner-inspector-head {
  display: flex;
  width: 100%;
  height: 45px;
  min-width: 0;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border-bottom: 1px solid var(--line);
  padding: 0 14px;
}
.scanner-inspector-head strong { flex: 0 0 auto; color: var(--ink); font-size: .9rem; }
.scanner-inspector-head span {
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: .68rem;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.scanner-inspector-scroll {
  width: 100%;
  height: calc(100% - 45px);
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}
.scanner-control-group {
  display: grid;
  width: 100%;
  min-width: 0;
  align-content: start;
  gap: 11px;
  border-bottom: 1px solid var(--line);
  padding: 14px;
}
.scanner-control-group[hidden] { display: none !important; }
.scanner-control-group:last-child { border-bottom: 0; }
.control-heading { display: grid; min-width: 0; gap: 4px; }
.control-heading h2 {
  margin: 0;
  color: var(--ink-soft);
  font-size: .73rem;
  font-weight: 750;
  letter-spacing: .035em;
  text-transform: uppercase;
}
.control-heading p,
.scanner-help { margin: 0; color: var(--muted); font-size: .76rem; line-height: 1.45; }
.scanner-control-actions { display: grid; min-width: 0; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.scanner-control-actions .btn {
  width: 100%;
  min-width: 0;
  min-height: 40px;
  justify-content: center;
  padding: 0 8px;
  overflow: hidden;
  font-size: .75rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.scanner-control-actions .danger { grid-column: 1 / -1; color: #dc2626; }
.scanner-full-button { width: 100%; min-width: 0; min-height: 40px; justify-content: center; }

.scanner-view-tabs {
  display: grid;
  width: 100%;
  min-width: 0;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--dropzone);
  padding: 4px;
}
.scan-view {
  width: 100%;
  min-width: 0;
  min-height: 36px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font-size: .73rem;
  font-weight: 750;
  touch-action: manipulation;
}
.scan-view.on { background: var(--brand); color: #fff; }

.scanner-page-tray {
  display: grid;
  min-width: 0;
  max-height: min(44dvh, 410px);
  gap: 9px;
  overflow-x: hidden;
  overflow-y: auto;
  padding-right: 2px;
}
.scanner-page {
  position: relative;
  display: grid;
  width: 100%;
  min-width: 0;
  min-height: 58px;
  grid-template-columns: 44px minmax(0, 1fr) 30px;
  align-items: center;
  gap: 8px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--ink);
  padding: 5px;
  text-align: left;
  touch-action: manipulation;
  transition: border-color .12s, opacity .12s, transform .12s;
}
.scanner-page.on { border-color: var(--brand); box-shadow: 0 0 0 2px rgba(109,94,252,.15); }
.scanner-page.is-dragging { opacity: .48; }
.scanner-page.is-drop-target { border-color: var(--brand); transform: scale(.985); }
.scanner-page img { width: 44px; height: 50px; border-radius: 5px; background: #e5e7eb; object-fit: cover; }
.scanner-page-name { min-width: 0; overflow: hidden; font-size: .73rem; font-weight: 700; text-overflow: ellipsis; white-space: nowrap; }
.scanner-page-grip {
  display: grid;
  width: 30px;
  height: 40px;
  place-items: center;
  border-radius: 7px;
  color: var(--muted);
  cursor: grab;
  font-size: 1rem;
  font-style: normal;
  letter-spacing: -3px;
  touch-action: none;
}
.scanner-page-grip:hover { background: var(--dropzone); color: var(--brand); }
.scanner-page-grip:active { cursor: grabbing; }
.scanner-page em {
  position: absolute;
  top: 3px;
  left: 3px;
  display: grid;
  width: 17px;
  height: 17px;
  place-items: center;
  border-radius: 50%;
  background: rgba(15,23,42,.75);
  color: #fff;
  font-size: .6rem;
  font-style: normal;
  font-weight: 800;
}

.filter-choices,
.scanner-export-actions { display: grid; min-width: 0; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; }
.filter-choices button {
  width: 100%;
  min-width: 0;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface);
  color: var(--ink-soft);
  padding: 0 5px;
  font-size: .75rem;
  font-weight: 750;
  touch-action: manipulation;
}
.filter-choices button.on { border-color: var(--brand); background: rgba(109,94,252,.08); color: var(--brand); }
.scanner-control-group .field { display: grid; width: 100%; min-width: 0; gap: 6px; }
.scanner-control-group .field-label { display: flex; min-width: 0; justify-content: space-between; gap: 8px; }
.scanner-control-group input[type="text"] { width: 100%; min-width: 0; }
.scanner-control-group select {
  width: 100%;
  min-width: 0;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface);
  color: var(--ink);
  padding: 0 10px;
  font: inherit;
}
.scanner-control-group input[type="range"] { width: 100%; min-width: 0; }
.scanner-control-group output { flex: 0 0 auto; color: var(--brand); font-variant-numeric: tabular-nums; }
.scanner-inline-note {
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--dropzone);
  color: var(--muted);
  padding: 9px 10px;
  font-size: .72rem;
  line-height: 1.45;
}
.scanner-inline-note.ready { border-color: color-mix(in srgb, var(--brand) 35%, var(--line)); color: var(--ink-soft); }
.scanner-export-actions .btn {
  width: 100%;
  min-width: 0;
  min-height: 42px;
  justify-content: center;
  overflow: hidden;
  padding: 0 7px;
  font-size: .75rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.scanner-stage-section {
  display: flex;
  width: 100%;
  min-width: 0;
  min-height: 0;
  grid-column: 3;
  grid-row: 1;
  flex-direction: column;
  overflow: hidden;
  background: var(--dropzone);
}
.scanner-stage {
  display: grid;
  width: 100%;
  min-width: 0;
  min-height: 0;
  flex: 1 1 auto;
  place-items: center;
  overflow: hidden;
  background: repeating-conic-gradient(rgba(128,128,128,.13) 0 25%, transparent 0 50%) 50% / 18px 18px;
  padding: 22px;
}
#scanCanvas {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  background: #fff;
  box-shadow: 0 14px 38px rgba(20,24,50,.2);
  cursor: default;
  touch-action: none;
}
#scanCanvas.crop-ready { cursor: crosshair; }
#scanCanvas.dragging { cursor: grabbing; }
.scanner-help {
  width: 100%;
  min-width: 0;
  flex: 0 0 auto;
  overflow: hidden;
  border-top: 1px solid var(--line);
  background: var(--surface);
  padding: 8px 14px;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Tablet: retain the editor's side-by-side workflow, but tighten fixed tracks. */
@media (max-width: 900px) and (min-width: 641px) {
  .scanner-editor-shell { grid-template-columns: 64px clamp(220px, 31vw, 260px) minmax(0, 1fr); }
  .scanner-option-rail { padding-inline: 3px; }
  .scanner-rail-button { width: 56px; min-width: 56px; }
  .scanner-stage { padding: 14px; }
  .scanner-editor-brand { font-size: .88rem; }
}

/* Phone: match Photo Editor's stable canvas → inspector → toolbar stack. */
@media (max-width: 640px) {
  #workspace.scanner-workspace:not([hidden]) {
    --scanner-bottom-safe: max(8px, env(safe-area-inset-bottom));
    --scanner-rail-height: calc(55px + var(--scanner-bottom-safe));
    --scanner-panel-height: clamp(184px, 36dvh, 276px);
  }
  .scanner-editor-header {
    min-height: 52px;
    flex-basis: 52px;
    gap: 4px;
    padding: 6px 8px;
  }
  .scanner-editor-brand { gap: 0; font-size: 0; }
  .scanner-editor-brand img { width: 32px; height: 32px; flex-basis: 32px; }
  .scanner-editor-title { display: none; }
  .scanner-head-button { width: 36px; min-width: 36px; height: 36px; min-height: 36px; }
  .scanner-head-button svg { width: 19px; height: 19px; }
  .scanner-editor-shell {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) var(--scanner-panel-height) var(--scanner-rail-height);
  }
  .scanner-stage-section {
    grid-column: 1;
    grid-row: 1;
  }
  .scanner-stage { padding: 10px; }
  #scanCanvas {
    max-height: max(96px, calc(100dvh - 52px - var(--scanner-panel-height) - var(--scanner-rail-height) - 60px));
  }
  .scanner-help {
    display: grid;
    min-height: 38px;
    place-items: center;
    padding: 5px 10px;
    font-size: .69rem;
    white-space: normal;
  }
  .scanner-inspector {
    grid-column: 1;
    grid-row: 2;
    height: 100%;
    border-top: 1px solid var(--line);
    border-right: 0;
    box-shadow: 0 -8px 22px rgba(28,30,60,.06);
  }
  .scanner-inspector-head { height: 39px; padding: 0 12px; }
  .scanner-inspector-scroll { height: calc(100% - 39px); }
  .scanner-control-group { padding: 12px; }
  .scanner-option-rail {
    width: 100%;
    height: var(--scanner-rail-height);
    min-height: var(--scanner-rail-height);
    grid-column: 1;
    grid-row: 3;
    flex-direction: row;
    align-items: stretch;
    gap: 0;
    overflow-x: auto;
    overflow-y: hidden;
    border-top: 1px solid var(--line);
    border-right: 0;
    box-shadow: 0 -6px 18px rgba(17,19,39,.08);
    padding: 3px 4px var(--scanner-bottom-safe);
    scrollbar-width: none;
  }
  .scanner-option-rail::-webkit-scrollbar { display: none; }
  .scanner-rail-button {
    width: auto;
    min-width: 44px;
    height: 51px;
    min-height: 51px;
    flex: 1 1 44px;
  }
  .scanner-page-tray {
    display: flex;
    max-height: none;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 2px 2px 7px;
  }
  .scanner-page { flex: 0 0 132px; }
  .scanner-control-actions .btn,
  .filter-choices button,
  .scanner-export-actions .btn { min-height: 44px; }
}

@media (max-width: 420px) {
  .scanner-rail-button { min-width: 38px; flex-basis: 38px; }
  .scanner-rail-button > span:last-child { display: none; }
}

@media (max-width: 640px) and (max-height: 480px) {
  #workspace.scanner-workspace:not([hidden]) { --scanner-panel-height: clamp(104px, 30dvh, 140px); }
  .scanner-help { display: none; }
  #scanCanvas {
    max-height: max(48px, calc(100dvh - 52px - var(--scanner-panel-height) - var(--scanner-rail-height) - 22px));
  }
}

@media (max-width: 350px) {
  .scanner-editor-header { padding-inline: 5px; }
  .scanner-head-actions { gap: 2px; }
  .scanner-control-actions,
  .scanner-export-actions { grid-template-columns: minmax(0, 1fr); }
  .scanner-control-actions .danger { grid-column: auto; }
}

@media (max-height: 560px) and (min-width: 641px) {
  .scanner-rail-button { height: 45px; min-height: 45px; flex-basis: 45px; }
  .scanner-help { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .scanner-workspace *,
  .scanner-workspace *::before,
  .scanner-workspace *::after { scroll-behavior: auto !important; transition: none !important; }
}
