:root {
  --bg: #f6f4ef;
  --panel: #ffffff;
  --ink: #241a10;
  --muted: #7b6f5f;
  --line: #e3dccd;
  --accent: #7a5c2e;
  --accent-ink: #fff;
  --ok: #2e7d32;
  --sel: #2b6cb0;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

/* 3-column layout: sidebar (~10%) | image (rest) | actions + infos (~10%) */
.layout {
  display: grid;
  grid-template-columns: minmax(190px, 10%) 1fr minmax(400px, 20%);
  gap: 16px;
  align-items: start;
  padding: 0 16px;
  min-height: 100vh;
}

.sidebar { grid-column: 1; display: flex; flex-direction: column; gap: 14px; margin: 16px 0; }

.topbar h1 { font-size: 16px; margin: 0 0 6px; line-height: 1.3; }
.sub { color: var(--muted); margin: 0; font-size: 12px; line-height: 1.45; }

/* Dropzone */
.dropzone {
  border: 2px dashed var(--line);
  border-radius: 14px;
  background: var(--panel);
  padding: 18px 12px;
  text-align: center;
  transition: border-color .15s, background .15s;
  cursor: pointer;
  outline: none;
}
.dropzone.drag { border-color: var(--accent); background: #fbf7ee; }
.dropzone:focus-visible { border-color: var(--accent); }
.drop-icon { font-size: 28px; }
.dropzone p { margin: 6px 0; font-size: 13px; }
.hint { color: var(--muted); font-size: 12px; }
.link {
  background: none; border: none; color: var(--accent);
  font: inherit; text-decoration: underline; cursor: pointer; padding: 0;
}

/* Stage: image zone with checkerboard background */
.stage {
  grid-column: 2;
  align-self: stretch;
  position: relative;
  min-height: 100vh;
  background: conic-gradient(#d9d9d9 25%, #ffffff 0 50%, #d9d9d9 0 75%, #ffffff 0) 0 0 / 28px 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.stage .hint { background: rgba(255, 255, 255, .85); padding: 6px 12px; border-radius: 8px; }

/* Results: right column (actions + infos) */
.results { display: contents; }
.results[hidden] { display: none; }
.rightcol { grid-column: 3; display: flex; flex-direction: column; gap: 22px; margin: 16px 0; }

/* Without the right column, the checkerboard area extends to the edge. */
.layout:has(.results[hidden]) .stage { grid-column: 2 / -1; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; min-height: auto; }
  .sidebar, .stage, .rightcol { grid-column: auto; }
  .stage { min-height: 40vh; }
}

.panel h2 { font-size: 13px; margin: 0 0 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.panelhead { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.panelhead h2 { margin: 0; }

.fieldgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.fieldgrid .field { min-width: 0; }
.fieldgrid .field.wide { grid-column: 1 / -1; }
.field label { display: block; font-weight: 600; margin-bottom: 4px; font-size: 13px; }
.field input {
  width: 100%; min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fafafa;
  font: inherit;
  font-size: 13px;
  color: var(--ink);
}
.copyrow { display: flex; align-items: stretch; gap: 6px; }
.copyrow input { flex: 1 1 0%; width: auto; }
.copyrow .copy {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 0 4px; font-size: 14px; line-height: 1;
}

button {
  font: inherit;
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 8px 14px;
}
.copy { background: none; border: none; color: var(--muted); white-space: nowrap; }
.copy:hover { color: var(--ink); }
.copy.done { color: var(--ok); }
.primary { background: var(--sel); color: #fff; }
.primary:hover:not(:disabled) { filter: brightness(1.1); }
.secondary { background: #f0ebe0; border-color: var(--line); }
.secondary:hover:not(:disabled) { background: #e7dfcd; }
button:disabled { opacity: .45; cursor: not-allowed; }
.ghost {
  background: none; border: none; padding: 0;
  color: var(--accent); font-size: 13px;
}
.ghost:hover { text-decoration: underline; }

/* Viewer + crop: the canvases cover the whole checkerboard area */
.viewer { position: absolute; inset: 0; line-height: 0; }
.viewer[hidden] { display: none; }
#display, #overlay { position: absolute; inset: 0; width: 100%; height: 100%; }
#overlay { cursor: crosshair; touch-action: none; }

.imgtools .row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-top: 10px; }
.imgtools .row button { flex: 1 1 100%; }
/* Selection actions side by side: download takes the wider share. */
.imgtools .row.split button { flex: 2 1 auto; }
.imgtools .row.split button.secondary { flex: 1 1 auto; }
.sel-hint { margin: 0 0 4px; }
.fmt { font-size: 13px; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.fmt[hidden] { display: none; }
.fmt select { font: inherit; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--line); background: #fafafa; }
.dims { font-size: 12px; color: var(--muted); }

/* Help: floating "?" button on the checkerboard area + modal dialog */
.helpbtn {
  position: absolute; top: 12px; right: 12px; z-index: 5;
  width: 30px; height: 30px; border-radius: 50%;
  background: rgba(255, 255, 255, .92);
  border: 1px solid var(--line);
  padding: 0;
  font-weight: 600; font-size: 15px;
  color: var(--muted);
}
.helpbtn:hover { color: var(--accent); border-color: var(--accent); }
#helpDialog {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px 24px;
  max-width: 460px;
  color: var(--ink);
  background: var(--panel);
}
#helpDialog::backdrop { background: rgba(0, 0, 0, .35); }
#helpDialog h2 {
  font-size: 13px; margin: 0 0 12px;
  text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
}
#helpDialog p { margin: 0 0 10px; }
#helpDialog button { margin-top: 6px; }

/* Assembly adjustment dialog */
#adjustDialog {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px 24px;
  color: var(--ink);
  background: var(--panel);
}
#adjustDialog::backdrop { background: rgba(0, 0, 0, .35); }
#adjustDialog h2 {
  font-size: 13px; margin: 0 0 8px;
  text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
}
#adjustDialog .hint { margin: 0 0 12px; }
.adjust-stage {
  background: conic-gradient(#d9d9d9 25%, #ffffff 0 50%, #d9d9d9 0 75%, #ffffff 0) 0 0 / 20px 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  display: flex;
  justify-content: center;
}
#adjustCanvas { touch-action: none; display: block; max-width: 100%; }
.adjust-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 14px; }

/* Page navigator: floating bar at the bottom of the checkerboard area */
.pagenav {
  position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 4px;
  background: rgba(255, 255, 255, .92);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 3px 6px;
  font-size: 13px;
  z-index: 5;
}
.pagenav button { padding: 2px 10px; background: none; border: none; font-size: 16px; line-height: 1; }
.pagenav button:hover:not(:disabled) { color: var(--accent); }

/* Toast */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px;
  font-size: 14px; box-shadow: 0 6px 20px rgba(0,0,0,.25); z-index: 10;
}
