* { box-sizing: border-box; }

:root {
  --ink: #2d2a26;
  --paper: #f4efe7;
  --card: #ffffff;
  --line: #ddd5c8;
  --accent: #b5443c;
}

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--paper);
}

header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: 0.5px;
  white-space: nowrap;
}

/* The wordmark shrinks to its initials on narrow windows (media query below). */
.brand-short { display: none; }

/* The quilt combo: the name box (type to rename the open quilt) with the
   quilt menu's ▾ fused onto its right edge (click to switch quilts). */
.quilt-combo {
  position: relative;
  display: flex;
  flex: 0 1 170px;
  min-width: 110px;
}

#designName {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  padding: 4px 8px;
  border: 1px solid var(--line);
  border-radius: 6px 0 0 6px;
  background: var(--paper);
}

#quiltMenuBtn {
  flex-shrink: 0;
  font-size: 10px;
  padding: 4px 7px;
  border-left: none;
  border-radius: 0 6px 6px 0;
}

/* display:flex would defeat the hidden attribute — restate it. */
.quilt-menu[hidden] { display: none; }

.quilt-menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0; /* the combo sits toward the window edge; grow into the page */
  min-width: 100%;
  max-width: 320px;
  z-index: 25; /* over the stage and its banners */
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 4px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .15);
}

.quilt-item {
  border: none;
  background: none;
  font-size: 13px;
  text-align: left;
  border-radius: 6px;
  padding: 5px 8px 5px 24px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.quilt-item:hover { background: #faf6ee; }

/* The open quilt gets a check mark in the gutter the padding reserves. */
.quilt-item.active { position: relative; font-weight: 600; }
.quilt-item.active::before { content: '✓'; position: absolute; left: 8px; color: var(--accent); }

/* Library actions (new/copy/delete) sit below the quilt list in the menu. */
.quilt-menu-sep {
  height: 1px;
  margin: 4px 2px;
  background: var(--line);
}

.quilt-action {
  border: none;
  background: none;
  font-size: 13px;
  color: #6b6357;
  text-align: left;
  border-radius: 6px;
  padding: 5px 8px 5px 24px;
  white-space: nowrap;
}

.quilt-action:hover { background: #faf6ee; color: var(--ink); }

.quilt-action.danger:hover { color: var(--accent); }

.header-actions { margin-left: auto; display: flex; gap: 8px; }

#fullscreenBtn, #sidebarBtn { display: flex; align-items: center; padding: 6px 8px; }
#fullscreenBtn svg, #sidebarBtn svg { width: 17px; height: 17px; display: block; }

/* The icon's right panel is filled while the sidebar is showing. */
#sidebarBtn[aria-expanded="false"] #sidebarIconFill { display: none; }

button {
  font: inherit;
  font-size: 14px;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 13px;
  cursor: pointer;
}

button:hover:not(:disabled) { border-color: #b8ac99; background: #faf6ee; }
button:disabled { opacity: 0.4; cursor: default; }

main {
  flex: 1;
  display: flex;
  min-height: 0; /* allow children to shrink inside the flex column */
}

/* --- canvas stage --- */

.stage {
  position: relative;
  flex: 1;
  min-width: 0;
  background: #e9e2d6;
}

#canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: crosshair;
}

/* Tool buttons live in the header bar, laid out horizontally. The bar
   never wraps: when the window is too narrow it scrolls sideways instead. */
.toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: thin;
}

.toolbar button {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  font-size: 13px;
  flex-shrink: 0;
  white-space: nowrap;
}

.toolbar svg { width: 17px; height: 17px; }

.toolbar button[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.tool-sep {
  width: 1px;
  height: 24px;
  background: var(--line);
  margin: 0 4px;
  flex-shrink: 0;
}

.toolbar .zoom-btn {
  min-width: 34px;
  justify-content: center;
  padding: 6px 8px;
  font-size: 14px;
}

/* Per-tool usage help, in its own (collapsed by default) sidebar box. */
.help-body p {
  margin: 0 0 9px;
  font-size: 12.5px;
  line-height: 1.45;
}

.help-body p:last-child { margin-bottom: 0; }

#flash {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  max-width: min(560px, calc(100% - 40px));
  margin: 0;
  padding: 7px 14px;
  font-size: 13px;
  color: #fff;
  background: var(--accent);
  border-radius: 999px;
  box-shadow: 0 2px 8px rgba(45, 42, 38, 0.25);
  opacity: 0;
  transition: opacity 0.25s;
  pointer-events: none;
}

#flash.show { opacity: 1; }

.readout {
  position: absolute;
  left: 14px;
  bottom: 12px;
  min-width: 190px;
  padding: 8px 12px;
  font-size: 12px;
  line-height: 1.5;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(45, 42, 38, 0.1);
  pointer-events: none;
  white-space: pre;
}

.readout div:last-child { color: #8a8175; }

/* --- sidebar --- */

/* display:flex below would defeat the hidden attribute — restate it. */
.sidebar[hidden] { display: none; }

.sidebar {
  width: 290px;
  flex-shrink: 0;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  background: var(--paper);
  border-left: 1px solid var(--line);
}

.sidebar section {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 13px 15px;
  flex-shrink: 0;
}

.sidebar h2 {
  margin: 0 0 10px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #8a8175;
}

/* Every sidebar box collapses: the heading is a <summary> with its own
   twist marker; the collapsed state persists (see app.js). */
.sidebar summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  user-select: none;
  list-style: none;
}

.sidebar summary::-webkit-details-marker { display: none; }

.sidebar summary h2 { margin: 0; }

.sidebar summary::after {
  content: '▾';
  font-size: 11px;
  color: #8a8175;
}

.sidebar details:not([open]) summary::after { content: '▸'; }

.sidebar details[open] summary { margin-bottom: 10px; }

.sidebar label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 14px;
  margin-top: 8px;
}

.sidebar label:first-of-type { margin-top: 0; }

.sidebar select {
  font: inherit;
  font-size: 14px;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
}

.size-inputs {
  display: flex;
  align-items: center;
  gap: 6px;
}

.size-inputs input[type="number"] {
  width: 58px;
  font: inherit;
  font-size: 14px;
  padding: 5px 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
}

.sidebar label.check {
  justify-content: flex-start;
  gap: 8px;
}

.fineprint {
  margin: 8px 0 0;
  font-size: 12px;
  color: #8a8175;
}

.focus-info {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card);
  border: 1px solid #1a73c7;
  border-radius: 10px;
  padding: 6px 12px;
  font-size: 13px;
  color: #134e7d;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .12);
  pointer-events: none; /* don't swallow canvas clicks under the banner */
}

.focus-info button {
  font: inherit;
  font-size: 12px;
  padding: 2px 8px;
  pointer-events: auto;
}

/* Stamping inside a focused block shows both banners — stack them. */
#stampInfo { top: 52px; }

.group-tree {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 13px;
}

.group-row {
  display: flex;
  align-items: center;
  gap: 4px;
  border-radius: 6px;
  padding: 2px 4px;
}

.group-row:hover { background: rgba(26, 115, 199, .08); }

.group-twist {
  border: none;
  background: none;
  width: 14px;
  padding: 0;
  font-size: 10px;
  color: #8a8175;
  cursor: pointer;
  flex-shrink: 0;
}

.group-name {
  border: none;
  background: none;
  font: inherit;
  cursor: pointer;
  text-align: left;
  flex: 1;
  min-width: 0;
  padding: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.group-count {
  color: #8a8175;
  font-size: 11px;
  flex-shrink: 0;
}

.group-swatch {
  width: 16px;
  height: 16px;
  border: 1px solid rgba(0, 0, 0, .3);
  border-radius: 4px;
  padding: 0;
  cursor: pointer;
  flex-shrink: 0;
}

.group-act {
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: 5px;
  font-size: 11px;
  line-height: 1;
  padding: 2px 4px;
  cursor: pointer;
  flex-shrink: 0;
}

kbd {
  font-family: inherit;
  font-size: 11px;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
  background: var(--paper);
}

.palette {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 3px 8px;
  margin-bottom: 10px;
}

/* A swatch is a color chip plus its always-visible name (the hex code
   until the fabric is named — click the selected swatch's name to edit). */
.swatch {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  padding: 2px 4px;
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
}

.swatch:hover { background: #faf6ee; border-color: #b8ac99; }

.chip {
  display: inline-block;
  width: 17px;
  height: 17px;
  flex-shrink: 0;
  border: 1px solid rgba(0, 0, 0, 0.25);
  border-radius: 4px;
}

.swatch .fname {
  flex: 1;
  min-width: 0;
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.swatch.selected .fname { cursor: text; }

.swatch.selected {
  outline: 2px solid var(--accent);
  outline-offset: 0;
}

/* The per-swatch delete button only shows on hover or selection. */
.swatch .del {
  visibility: hidden;
  flex-shrink: 0;
  border: none;
  background: none;
  padding: 0 2px;
  font-size: 10px;
  line-height: 1;
  color: #8a8175;
}

.swatch:hover .del, .swatch.selected .del { visibility: visible; }

.swatch .del:hover:not(:disabled) { color: var(--accent); background: none; border: none; }

/* Inline fabric-rename editor, swapped in for the name label. */
.swatch .fname-edit {
  flex: 1;
  min-width: 0;
  width: 100%;
  font: inherit;
  font-size: 11px;
  padding: 0 2px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--card);
}

.custom-color input[type="color"] {
  width: 44px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: none;
  cursor: pointer;
}

.piece-count {
  margin: 10px 0 0;
  font-size: 14px;
  font-weight: 600;
}

.mirror-half {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 8px;
  font-size: 14px;
}

.mh-buttons {
  display: flex;
  gap: 4px;
}

.mh-buttons button {
  padding: 3px 8px;
  font-size: 13px;
  line-height: 1.2;
}

/* One row: [add/delete] [amount] [unit]; the amount box soaks up the slack. */
.resize-action input[type="number"] { flex: 1; min-width: 48px; }

/* The four side toggles, one even row across the panel. */
.resize-sides {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  margin-top: 8px;
}

.resize-sides button {
  padding: 3px 0;
  font-size: 12px;
  line-height: 1.2;
}

.resize-sides button[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* The current -> new size readout; doubles as the error line. */
.resize-summary {
  margin: 8px 0 0;
  font-size: 13px;
  font-weight: 600;
}

.resize-summary.invalid { color: #b23f38; font-weight: 400; }

.pending-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 8px;
  font-size: 13px;
  font-weight: 600;
  color: #b06008;
}

.small-btn {
  padding: 3px 10px;
  font-size: 12px;
}

.trace-opacity input[type="range"],
.slider input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
}

.file-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}

.file-buttons .wide { grid-column: 1 / -1; }

.file-buttons button[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.file-load {
  position: relative;
  overflow: hidden;
}

.file-load input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

/* --- interactive sewing guide --- */

.guide {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  background: var(--paper);
}

/* display:flex above would beat the UA's [hidden] rule — restate it. */
.guide[hidden] { display: none; }

.guide-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px 18px;
  padding: 8px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.guide-bar h2 {
  margin: 0;
  font-size: 16px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.guide-bar label.check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.guide-bar-right {
  margin-left: auto;
  display: flex;
  gap: 6px;
}

.guide-body {
  flex: 1;
  display: flex;
  min-height: 0;
}

.guide-stage {
  position: relative;
  flex: 1;
  min-width: 0;
  background: #e9e2d6;
}

#guideCanvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: grab;
}

#guideCanvas:active { cursor: grabbing; }

.guide-hint {
  position: absolute;
  left: 14px;
  bottom: 12px;
  margin: 0;
  max-width: calc(100% - 28px);
  padding: 6px 12px;
  font-size: 12px;
  color: #8a8175;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: 8px;
  pointer-events: none;
}

.guide-steps {
  width: 430px;
  flex-shrink: 0;
  overflow-y: auto;
  padding: 14px;
  background: var(--card);
  border-left: 1px solid var(--line);
}

.guide-steps h3 {
  margin: 20px 0 8px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #8a8175;
}

.guide-steps h3:first-child { margin-top: 0; }

.guide-fabric {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px 0 4px;
  font-size: 13px;
  font-weight: 600;
}

.guide-step {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin: 6px 0;
  padding: 8px 10px;
  font-size: 13px;
  line-height: 1.45;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}

.guide-step:hover { border-color: #b8ac99; }

.guide-step.active {
  background: #eef5fc;
  border-color: #1a73c7;
  outline: 1.5px solid #1a73c7;
}

.guide-step > div { min-width: 0; }

.guide-step svg { max-width: 100%; height: auto; }

.guide-step svg.step { display: block; margin-top: 6px; }

.guide-no {
  flex-shrink: 0;
  min-width: 22px;
  font-weight: 700;
  color: #134e7d;
}

/* Technique warnings on a step (off-45° / HRT-style joins). */
.guide-warn {
  margin-top: 4px;
  font-size: 12px;
  color: #b5443c;
}

/* Narrow windows: tool buttons drop their text (icons + tooltips remain)
   before the toolbar has to scroll. */
@media (max-width: 1100px) {
  .toolbar .tool-label { display: none; }
}

@media (max-width: 800px) {
  .brand-full { display: none; }
  .brand-short { display: inline; }
}

@media (max-width: 640px) {
  .sidebar { width: 220px; }
  .guide-steps { width: 250px; }
}
