:root {
  color-scheme: light dark;
  --ink: oklch(0.22 0 0);
  --ink-2: oklch(0.47 0 0);
  --rule: oklch(0.77 0 0);
  --paper: oklch(0.97 0 0);
  --paper-2: oklch(0.94 0 0);
  --paper-pure: oklch(1 0 0);
  --focus: oklch(0.57 0.19 255);
  --danger: oklch(0.56 0.2 27);
  --sans: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  --settings-width: 19rem;
  --settings-padding: 1rem;
  --nav-height: 3.25rem;
  --canvas-stage: #2a2a2a;
  --retained-pattern: repeating-linear-gradient(135deg, rgb(236 132 24 / .32) 0 3px, rgb(236 132 24 / .07) 3px 7px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --ink: oklch(0.95 0 0);
    --ink-2: oklch(0.63 0 0);
    --rule: oklch(0.37 0 0);
    --paper: oklch(0.18 0 0);
    --paper-2: oklch(0.22 0 0);
    --paper-pure: oklch(0.13 0 0);
    --focus: oklch(0.72 0.14 250);
    --danger: oklch(0.7 0.17 27);
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body:not(.advanced-mode) .advanced-only { display: none !important; }
html { background: var(--paper); color: var(--ink); font-family: var(--sans); }
body { margin: 0; min-width: 0; }
::selection { background: var(--ink); color: var(--paper); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 3px; }
.workbench {
  display: grid; grid-template-columns: var(--settings-width) minmax(0, 1fr);
  width: 100%; height: 100dvh; margin: 0; padding: 0;
}
body.sidebar-collapsed .workbench { grid-template-columns: minmax(0, 1fr); }

/* Settings-panel dimensions, typography, spacing and surfaces match svg-tools'
   shared tool-panel.css, used by Optimize. No framework or shared JS runtime. */
.tool-panel {
  min-width: 0; min-height: 0; height: 100%; overflow: auto;
  padding: 0 var(--settings-padding) var(--settings-padding);
  border-right: 1px solid var(--rule); background: var(--paper-2); color: var(--ink);
  scrollbar-color: transparent transparent;
}
.tool-panel:is(:hover, :focus-within) { scrollbar-color: var(--rule) transparent; }
.tool-panel-header {
  position: sticky; z-index: 3; top: 0;
  display: flex; align-items: center; justify-content: space-between; gap: .5rem;
  height: var(--nav-height); min-height: var(--nav-height);
  margin: 0 calc(-1 * var(--settings-padding)) 1rem;
  padding: 0 var(--settings-padding); background: var(--paper-2);
}
.tool-panel-brand { margin: 0; white-space: nowrap; font: 760 .78rem/1 var(--sans); letter-spacing: -.025em; }
.tool-panel-brand span { color: var(--ink-2); font-weight: 450; }
.panel-section { margin: 1rem 0 0; padding: .8rem 0 0; border: 0; border-top: 1px solid var(--rule); min-width: 0; }
.source-section:first-of-type { margin: 0; padding: 0; border: 0; }
#results { margin-top: 0; padding-top: 0; border-top: 0; }
.mode-buttons { display: flex; }
.mode-buttons button { flex: 1; }
.mode-buttons button + button { border-left: 0; }
.mode-buttons button[aria-pressed="true"] { background: var(--ink); color: var(--paper); border-color: var(--ink); }
#panel-action-slot { display: flex; align-items: center; gap: .5rem; flex: 1; min-width: 0; }
.tool-panel-header:has(#panel-action-slot:not([hidden])) .tool-panel-brand { display: none; }
h2, legend, .tool-panel summary { color: var(--ink); font: 600 .8rem/1.4 var(--sans); text-transform: none; letter-spacing: 0; }
h2, legend { padding: 0; margin: 0 0 .6rem; }
legend { float: left; width: 100%; }
fieldset > :not(legend) { clear: both; }
summary { cursor: pointer; }
.tool-panel summary { padding-block: .35rem; }
button, input, select { font: inherit; border-radius: 0; }
button, select, input[type="number"] {
  min-height: 32px; max-width: 100%; padding: .4rem .6rem;
  border: 1px solid var(--rule); font: 500 .78rem/1.3 var(--sans);
  background: var(--paper-pure); color: var(--ink);
}
button { cursor: pointer; }
button:hover:not(:disabled) { background: var(--paper); border-color: var(--ink); }
button:disabled, select:disabled { opacity: .5; cursor: default; }
fieldset:disabled { color: var(--ink-2); }
fieldset:disabled input { opacity: .5; }
.primary { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.primary:hover:not(:disabled) { background: var(--ink-2); color: var(--paper-pure); }
.wide { width: 100%; }
.wide + .wide { margin-top: .5rem; }
#save { min-height: 36px; }
.tool-panel-toggle {
  display: inline-grid; place-items: center; flex: none;
  width: 36px; height: 36px; min-height: 36px; padding: 7px;
  border: 1px solid transparent; background: var(--paper-2); cursor: ew-resize;
}
.tool-panel-toggle:hover:not(:disabled) { background: var(--paper-2); border-color: var(--rule); }
.tool-panel-toggle svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; }
.tool-panel-toggle .panel-rail { stroke: none; }
.tool-panel-toggle[aria-expanded="true"] .panel-rail { fill: currentColor; opacity: .18; }
#panel-toggle-slot { display: flex; flex: none; }
#panel-toggle-slot .tool-panel-toggle { border-color: var(--rule); }
.rotate-controls { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; margin-top: .5rem; }
.rotate-controls button { min-width: 0; }
.file-name { overflow-wrap: anywhere; font-size: .75rem; line-height: 1.5; margin: .7rem 0 0; color: var(--ink-2); }
dl { margin: .7rem 0 0; font: .7rem/1.7 var(--mono); }
dl div { display: flex; justify-content: space-between; gap: .5rem; }
dt { color: var(--ink-2); } dd { margin: 0; }
.advanced-coordinates { margin-top: .75rem; }
.advanced-coordinates summary { color: var(--ink-2); }
.advanced-coordinates[open] .coordinates { margin-top: .8rem; }
.coordinates { display: grid; grid-template-columns: 1fr 1fr; gap: .65rem; }
.coordinates label { display: grid; gap: .3rem; color: var(--ink-2); font: .75rem/1.4 var(--sans); }
input[type="number"] { min-width: 0; width: 100%; }
input[aria-invalid="true"] { border-color: var(--danger); }
.check { display: flex; gap: .5rem; align-items: start; font-size: .78rem; line-height: 1.45; }
.check input { margin: .14rem 0 0; accent-color: var(--ink); }
.note { color: var(--ink-2); font: .72rem/1.5 var(--sans); margin: .7rem 0 .9rem; }

.workspace { min-width: 0; min-height: 0; display: grid; grid-template-rows: auto minmax(0, 1fr) auto; }
.mobile-brand, #panel-scrim { display: none; }
.workspace-bar {
  display: flex; align-items: center; flex-wrap: wrap; gap: .5rem 1rem;
  min-height: var(--nav-height); padding: .5rem 1rem; border-bottom: 1px solid var(--rule);
  font: .7rem/1.3 var(--mono); color: var(--ink-2);
}
.history-tools { display: flex; }
.history-tools button { min-height: 32px; padding: .3rem .65rem; font-size: .72rem; margin: 0; white-space: nowrap; }
.history-tools button + button { border-left: 0; }
.bar-select { display: flex; align-items: center; gap: .4rem; white-space: nowrap; }
.bar-select select { width: 5.1rem; min-width: 0; }
.bar-select.aspect-control select { width: 5.6rem; }
.workspace-actions { position: relative; display: flex; flex-wrap: wrap; gap: .35rem; }
.workspace-actions button { white-space: nowrap; }
/* Cancel replaces the unavailable save action without changing toolbar width. */
.workspace-actions:has(#cancel:not([hidden])) #save { visibility: hidden; }
#cancel { position: absolute; inset: 0; width: 100%; }
.size-readout { width: 25ch; margin-left: auto; color: var(--ink); white-space: nowrap; text-align: right; }
.size-readout[data-stale] { opacity: .55; }
.stage-wrap {
  min-width: 0; min-height: 0; overflow: auto; overscroll-behavior: contain;
  background: var(--paper-pure);
}
.stage-wrap[data-zoom="fit"] { overflow: hidden; }
/* Auto margins centre small images without putting oversized images outside
   the scrollable origin. Padding keeps all crop handles reachable. */
.stage-space {
  min-width: 100%; min-height: 100%; width: max-content;
  display: flex; padding: 1.5rem;
}
/* Touch zoom and pan are handled in preview.js; the page must not pinch-zoom. */
.stage-wrap.has-image { background: var(--canvas-stage); touch-action: none; }
.stage-wrap.dragging { outline: 2px solid var(--focus); outline-offset: -3px; }
.empty-state { margin: auto; text-align: center; color: var(--ink-2); font: .88rem/1.5 var(--sans); }
.empty-state p { margin: 0 0 .8rem; }
.stage { position: relative; flex: none; margin: auto; touch-action: none; cursor: crosshair; user-select: none; }
.stage img { display: block; width: 100%; height: 100%; pointer-events: none; }
.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.setting { display: grid; gap: .3rem; margin: .7rem 0; }
.setting select, .setting input { width: 100%; min-width: 0; }
.option-row { display: grid; gap: .25rem; text-align: left; width: 100%; padding: .6rem; margin: .4rem 0; }
.option-row[aria-pressed="true"] { border-color: var(--ink); box-shadow: inset 3px 0 currentColor; }
.option-row small { font-weight: normal; line-height: 1.5; }
.option-row .option-warning { min-block-size: 3em; color: var(--ink-2); }
.comparison-controls { display: flex; gap: .25rem; margin: .6rem 0; }
.comparison-controls button { flex: 1; padding: .3rem; font-size: .75rem; }
#result-report { font-size: .7rem; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 20rem; overflow: auto; }
.workspace-note {
  display: flex; align-items: start; gap: .75rem; padding: .6rem 1rem;
  border-top: 1px solid var(--rule); color: var(--ink-2); font: .72rem/1.5 var(--sans);
}
.workspace-feedback { min-width: 0; flex: 1; }
.workspace-note p { margin: 0; overflow-wrap: anywhere; }
#status:empty, #pointer-readout:empty { display: none; }
/* A measured result keeps its short status row even after a download. */
.workbench:has(#results:not([hidden])) #status { display: block; min-height: 1.5em; }
#status { color: var(--ink-2); }
#error { color: var(--danger); }
.pointer-readout { font: .7rem/1.5 var(--mono); color: var(--ink); }
.inline-confirm, .saved-note {
  display: flex; flex-wrap: wrap; align-items: center; gap: .4rem .6rem;
  margin: .1rem 0 .3rem; color: var(--ink);
}
.inline-confirm button, .saved-note button { min-height: 32px; padding: .3rem .6rem; font-size: .72rem; }
.saved-note span { min-width: 0; overflow-wrap: anywhere; }
.interaction-details { flex: 0 0 auto; max-width: 32rem; }
.interaction-details summary { text-align: right; }
.interaction-details[open] { flex: 1 1 20rem; }
.interaction-details[open] p { margin-top: .5rem; }

/* Crop overlays and handles are independent of the settings-panel layout.
   The spread is a functional crop mask, not a decorative UI shadow. */
.selection { position: absolute; border: 1px solid white; outline: 1px solid #111; box-shadow: 0 0 0 9999px rgb(0 0 0 / .5); pointer-events: none; }
.stage-overlay { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
.retained-edge { position: absolute; background: var(--retained-pattern); pointer-events: none; }
.workspace-note .retained-note { margin: .25rem 0 0; color: var(--ink-2); font: .7rem/1.5 var(--sans); }
.retained-key { display: inline-block; width: .9rem; height: .9rem; margin-right: .35rem; background: var(--retained-pattern); border: 1px solid rgb(156 72 0 / .45); vertical-align: -1px; }
.adjustment { position: absolute; cursor: move; }
.resize-handle {
  position: absolute; width: 28px; height: 28px; min-height: 0; max-width: none; margin: 0;
  padding: 0; border: 0; background: transparent; transform: translate(-50%, -50%);
  touch-action: none; z-index: 2;
}
.resize-handle::after { content: ""; position: absolute; inset: 10px; background: white; border: 1px solid #111; }
.resize-handle:hover:not(:disabled) { background: transparent; }
.resize-handle:focus-visible { outline: 2px solid white; outline-offset: -6px; }
.resize-handle[data-handle="nw"] { left: 0; top: 0; cursor: nwse-resize; }
.resize-handle[data-handle="n"] { left: 50%; top: 0; cursor: ns-resize; }
.resize-handle[data-handle="ne"] { left: 100%; top: 0; cursor: nesw-resize; }
.resize-handle[data-handle="e"] { left: 100%; top: 50%; cursor: ew-resize; }
.resize-handle[data-handle="se"] { left: 100%; top: 100%; cursor: nwse-resize; }
.resize-handle[data-handle="s"] { left: 50%; top: 100%; cursor: ns-resize; }
.resize-handle[data-handle="sw"] { left: 0; top: 100%; cursor: nesw-resize; }
.resize-handle[data-handle="w"] { left: 0; top: 50%; cursor: ew-resize; }
.stage[data-drag="draw"] * { cursor: crosshair; }
.stage[data-drag="move"] * { cursor: move; }
.snap-preview { position: absolute; inset: 0; pointer-events: none; }
.snap-x { position: absolute; left: var(--snap-x); top: 0; bottom: 0; width: 1px; background: white; box-shadow: 1px 0 #111; }
.snap-y { position: absolute; top: var(--snap-y); left: 0; right: 0; height: 1px; background: white; box-shadow: 0 1px #111; }
.snap-point { position: absolute; left: var(--snap-x); top: var(--snap-y); width: 7px; height: 7px; transform: translate(-50%, -50%); border: 1px solid #111; background: white; }
.snap-preview[data-axes="x"] .snap-y, .snap-preview[data-axes="y"] .snap-x,
.snap-preview:not([data-axes="xy"]) .snap-point { display: none; }
/* Phones in either orientation: the panel starts collapsed and opens as an
   overlay drawer. Portrait phones dock it as a bottom bar instead (below). */
@media (max-width: 820px), (max-height: 540px) and (pointer: coarse) {
  .workbench, body.sidebar-collapsed .workbench {
    height: 100dvh; min-height: 0; grid-template-columns: minmax(0, 1fr);
  }
  #panel-scrim:not([hidden]) {
    display: block; position: fixed; inset: 0; z-index: 9;
    background: rgb(0 0 0 / .5);
  }
  .tool-panel {
    position: fixed; inset: 0 auto 0 0; z-index: 10;
    width: min(22rem, calc(100vw - 3rem)); height: 100dvh; max-height: none;
    border-right: 1px solid var(--rule); border-bottom: 0;
    box-shadow: none; overscroll-behavior: contain;
  }
  .workspace, body.sidebar-collapsed .workspace { height: 100dvh; min-height: 0; }
  #view-label, .tool-label { display: none; }
  .stage-space { padding: 1rem; }
  .workspace-note { padding: .55rem .75rem; }
  .interaction-details[open] { flex: 1; }
  #options { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .5rem; }
  #options .option-row { min-width: 0; margin: 0; overflow-wrap: anywhere; }
  #options .option-row:only-child { grid-column: 1 / -1; }
}
/* Portrait phones: a fixed-height header row (toggle, brand or output size,
   Save) above the tools (undo/redo, ratio, zoom), so the preview does not
   shift when the size estimate appears. */
@media (max-width: 820px) {
  .workspace-bar {
    display: grid; grid-template-columns: auto minmax(0, 1fr) auto auto;
    grid-template-rows: minmax(36px, auto) auto;
    align-items: center; gap: .55rem .5rem;
    min-height: 0; padding: .6rem .75rem;
  }
  #panel-toggle-slot { grid-column: 1; grid-row: 1; }
  .mobile-brand {
    display: block; grid-column: 2 / 4; grid-row: 1;
    overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
    color: var(--ink); font: 760 .78rem/1.2 var(--sans); letter-spacing: -.025em;
  }
  .mobile-brand span { color: var(--ink-2); font-weight: 450; }
  /* Once an image is loaded, its output size is more useful than the name. */
  .workspace-bar:has(.size-readout:not([hidden])) .mobile-brand { display: none; }
  .size-readout {
    grid-column: 2 / 4; grid-row: 1; margin: 0; min-width: 0; width: auto; text-align: left;
    display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden;
    font: .68rem/1.3 var(--mono); color: var(--ink); white-space: normal;
  }
  .workspace-actions { grid-column: 4; grid-row: 1; flex-wrap: nowrap; justify-self: end; }
  .workspace-actions button { min-height: 36px; }
  .history-tools { grid-column: 1 / 3; grid-row: 2; }
  .history-tools button { flex: 1; min-width: 0; min-height: 36px; }
  .aspect-control { grid-column: 3; grid-row: 2; }
  .zoom-control { grid-column: 4; grid-row: 2; }
  .bar-select select { min-height: 36px; }
  .bar-select.aspect-control select { width: 5.4rem; }
  .zoom-control select { width: 5.1rem; }
}
/* Portrait phones: the panel docks below the image as a bottom bar, so the
   crop stays visible and usable while the controls are open. */
@media (max-width: 820px) and (min-height: 541px), (max-width: 820px) and (pointer: fine) {
  .workbench, body.sidebar-collapsed .workbench { grid-template-rows: minmax(0, 1fr) auto; }
  .workspace, body.sidebar-collapsed .workspace { grid-row: 1; height: auto; }
  .tool-panel {
    position: static; grid-row: 2; width: auto; height: auto; max-height: 45dvh;
    border-right: 0; border-top: 1px solid var(--rule);
  }
  #panel-scrim:not([hidden]) { display: none; }
  .tool-panel-toggle svg { transform: rotate(-90deg); }
}
/* Landscape phones: one compact toolbar row leaves the height to the image. */
@media (max-height: 540px) and (pointer: coarse) {
  .workspace-bar {
    display: flex; flex-wrap: nowrap; gap: .45rem;
    min-height: 0; padding: .35rem .75rem;
  }
  .mobile-brand { display: none; }
  #panel-toggle-slot, .history-tools, .bar-select, .workspace-actions { flex: none; }
  .history-tools button { flex: none; }
  .size-readout {
    flex: 0 1 auto; min-width: 0; margin-left: auto;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  .workspace-actions { flex-wrap: nowrap; }
  .stage-space { padding: .75rem; }
  .workspace-note { padding: .35rem .75rem; }
}
@media (max-width: 380px) {
  .workspace-bar { column-gap: .4rem; }
  .bar-select select { font-size: .75rem; }
  .bar-select.aspect-control select { width: 4.9rem; }
  .zoom-control select { width: 4.4rem; }
  .mobile-brand { font-size: .72rem; }
  .workspace-actions button { padding-inline: .45rem; }
}
@media (hover: none) {
  .tool-panel { scrollbar-color: var(--rule) transparent; }
}
/* Mobile docks the same Save/Cancel nodes at the top of the open menu. */
#panel-action-slot .size-readout {
  flex: 1; width: auto; min-width: 0; margin: 0; text-align: left;
  font: .7rem/1.3 var(--mono); white-space: normal;
}
#panel-action-slot .workspace-actions { flex: none; }
/* Disabling crop interaction clears hover text; keep its footprint during work. */
@media (hover: hover) {
  .workbench:has(.stage-wrap.has-image) #pointer-readout { display: block; min-height: 1.5em; }
}
@media (hover: hover) and (max-width: 820px) {
  .workbench:has(.stage-wrap.has-image) #pointer-readout { min-height: 3em; }
}
@media (pointer: coarse) {
  .resize-handle { width: 36px; height: 36px; }
  .resize-handle::after { inset: 13px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; }
}

.nearby-outline { position: absolute; border: 2px dashed #d68426; pointer-events: none; }
