/* Area styles: canvas. Only what the shared components (components.css) cannot
   express. Use the tokens from tokens.css — never a literal colour. */

/* ---------------------------------------------------------- the mode --- */

/* No page scroll and no rubber-banding while the editor is open. A class,
   not an inline style: sheets reset body's inline overflow when they close. */
html.cv-open,
html.cv-open body {
  overflow: hidden;
  overscroll-behavior: none;
}

.cv {
  position: fixed; inset: 0; z-index: 42;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: var(--bg);
  color: var(--ink);
}

/* ----------------------------------------------------------- top bar --- */

.cv-bar {
  display: flex; align-items: center; gap: var(--sp-1);
  padding: calc(env(safe-area-inset-top, 0px) + 2px) max(var(--sp-2), env(safe-area-inset-right, 0px)) 2px max(var(--sp-1), env(safe-area-inset-left, 0px));
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  border-bottom: 1px solid var(--line);
}

.cv-bar__done { flex: none; gap: 0; padding-left: 4px; }

.cv-bar__title {
  flex: 1; min-width: 0; min-height: var(--touch);
  display: flex; align-items: center; justify-content: center; gap: 4px;
  border: 0; border-radius: var(--r-md); background: none; padding: 0 var(--sp-2);
}

.cv-bar__title:disabled { cursor: default; opacity: 1; }
.cv-bar__title:not(:disabled):active { background: var(--surface-sunken); }

.cv-bar__text { display: flex; flex-direction: column; align-items: center; min-width: 0; line-height: 1.15; }

.cv-bar__kicker { color: var(--ink-2); font-size: var(--fs-xs); font-weight: 500; }

.cv-bar__name {
  max-width: 100%; font-weight: 700; font-size: var(--fs-md);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.cv-bar__chevron { width: 16px; height: 16px; color: var(--ink-3); }

.cv-bar__status { color: var(--warning); }
.cv-bar__status--danger { color: var(--danger); }

.cv-bar__history { display: flex; flex: none; }
.cv-dock__history { display: none; }

/* Phones keep undo/redo top right: eight tools in the dock would cut every
   label. From tablet width the dock has room for them. */
@media (min-width: 30rem) {
  .cv-bar__history { display: none; }
  .cv-dock__history { display: flex; border-left: 1px solid var(--line); border-right: 1px solid var(--line); margin: 6px 2px; }
}

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

.cv-stage {
  position: relative; overflow: hidden;
  background: var(--plan-ground);
  touch-action: none;
}

.cv-canvas {
  position: absolute; inset: 0;
  display: block; width: 100%; height: 100%;
  touch-action: none; user-select: none; -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.cv-hint {
  position: absolute; z-index: 2; top: var(--sp-2); left: 50%; transform: translateX(-50%);
  width: max-content; max-width: calc(100% - 2 * var(--sp-4));
  min-height: var(--touch); padding: var(--sp-2) var(--sp-3);
  border: 0; border-radius: var(--r-md);
  background: color-mix(in srgb, var(--primary-bg) 90%, transparent); color: var(--primary-ink);
  font-size: var(--fs-sm); font-weight: 500; line-height: 1.35; text-align: center;
  box-shadow: var(--shadow-float);
  animation: fade-in 180ms var(--ease);
}

.cv-chip {
  position: absolute; z-index: 2; top: var(--sp-2); right: var(--sp-2);
  display: inline-flex; align-items: center; gap: 6px;
  min-height: var(--touch); padding: 0 var(--sp-3);
  border: 0; border-radius: var(--r-pill);
  background: var(--accent); color: #fff; font-weight: 600; font-size: var(--fs-sm);
  box-shadow: var(--shadow-float);
}

.cv-hint:not([hidden]) ~ .cv-chip { top: calc(var(--sp-2) + 64px); }

/* ------------------------------------------------------------- panel --- */

.cv-panel-host {
  position: absolute; z-index: 3; left: 0; right: 0; bottom: 0;
  padding: 0 max(var(--sp-2), env(safe-area-inset-right, 0px)) var(--sp-2) max(var(--sp-2), env(safe-area-inset-left, 0px));
  pointer-events: none;
}

.cv-panel {
  pointer-events: auto;
  max-width: 34rem; margin: 0 auto;
  max-height: min(60dvh, 26rem); overflow-y: auto; overscroll-behavior: contain;
  display: flex; flex-direction: column; gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-2) var(--sp-3) var(--sp-3);
  background: var(--surface-raised); border: 1px solid var(--line); border-radius: var(--r-lg);
  box-shadow: var(--shadow-float);
  animation: cv-panel-in 180ms var(--ease);
}

.cv-panel--compact { padding-bottom: var(--sp-2); }

@keyframes cv-panel-in { from { transform: translateY(12px); opacity: 0; } to { transform: none; opacity: 1; } }

.cv-panel__head { display: flex; align-items: center; gap: var(--sp-2); }

.cv-panel__titles { flex: 1; min-width: 0; }

.cv-panel__title {
  margin: 0; font-weight: 700; font-size: var(--fs-md);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.cv-panel__sub { margin: 1px 0 0; color: var(--ink-2); font-size: var(--fs-xs); line-height: 1.3; }

.cv-panel__label { margin: 0 0 4px; color: var(--ink-2); font-size: var(--fs-xs); font-weight: 600; }

.cv-panel__fields {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(5.5rem, 1fr)); gap: var(--sp-2);
  padding-right: var(--sp-1);
}

.cv-panel__row {
  display: flex; flex-wrap: wrap; align-items: flex-end; gap: var(--sp-2);
  padding-right: var(--sp-1);
}

.cv-panel__row > .cv-field { flex: 1 1 7rem; min-width: 0; }
.cv-panel__action { flex: none; }
.cv-panel__grow { flex: 1 1 auto; }
.cv-panel__icon { flex: none; }

.cv-danger { color: var(--danger); }

/* A compact labelled number input (the sheet field is too tall for a panel). */
.cv-field { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.cv-field__label { color: var(--ink-2); font-size: var(--fs-xs); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.cv-field__wrap {
  display: flex; align-items: center; gap: 4px;
  min-height: var(--touch); padding: 0 var(--sp-2);
  background: var(--surface-sunken); border: 1.5px solid transparent; border-radius: var(--r-sm);
}

.cv-field__wrap:focus-within { border-color: var(--accent); background: var(--surface); }

.cv-field__control {
  flex: 1; min-width: 0; width: 100%; min-height: 44px;
  border: 0; outline: 0; background: none;
  font-size: var(--fs-base); font-weight: 600; font-variant-numeric: tabular-nums;
}

.cv-field__unit { color: var(--ink-2); font-size: var(--fs-sm); }

/* Door swing: the four real orientations, 2 × 2 on a phone, one row wider. */
.cv-swing { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--sp-2); padding-right: var(--sp-1); }

.cv-swing__option {
  display: grid; place-items: center;
  min-height: 52px; min-width: var(--touch);
  border: 1.5px solid var(--line); border-radius: var(--r-md);
  background: var(--surface); color: var(--ink-2);
}

.cv-swing__option[aria-checked="true"] { border-color: var(--accent); background: var(--accent-wash); color: var(--accent-ink); }

/* ---------------------------------------------------------- furniture --- */

.cv-swatch-dot {
  width: 22px; height: 22px; flex: none; border-radius: 7px;
  box-shadow: inset 0 0 0 1px rgb(0 0 0 / 0.12);
}

.cv-swatches {
  display: flex; gap: var(--sp-1);
  overflow-x: auto; scrollbar-width: none; overscroll-behavior-x: contain;
  margin-right: calc(-1 * var(--sp-2)); padding-right: var(--sp-2);
}

.cv-swatches::-webkit-scrollbar { display: none; }

.cv-swatch {
  position: relative; flex: none;
  width: var(--touch); height: var(--touch);
  border: 0; border-radius: 50%; background: none;
  display: grid; place-items: center;
}

.cv-swatch::before {
  content: ''; width: 28px; height: 28px; border-radius: 50%;
  background: var(--swatch, transparent);
  box-shadow: inset 0 0 0 1px rgb(0 0 0 / 0.12);
}

.cv-swatch[aria-checked="true"]::before { box-shadow: 0 0 0 2px var(--surface-raised), 0 0 0 4px var(--accent); }

.cv-swatch--custom::before {
  background: conic-gradient(#dc2626, #d97706, #65a30d, #0d9488, #2563eb, #7c3aed, #db2777, #dc2626);
}

.cv-swatch__input {
  position: absolute; inset: 0; width: 100%; height: 100%;
  opacity: 0; border: 0; padding: 0; cursor: pointer;
}

.cv-patterns { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--sp-2); }

.cv-pattern {
  display: flex; align-items: center; gap: var(--sp-2);
  min-height: 48px; padding: 0 var(--sp-3);
  border: 1.5px solid var(--line); border-radius: var(--r-md); background: var(--surface);
  font-weight: 500; text-align: left;
}

.cv-pattern[aria-checked="true"] { border-color: var(--accent); background: var(--accent-wash); color: var(--accent-ink); }

.cv-pattern__preview {
  width: 26px; height: 26px; flex: none; border-radius: 6px;
  background-color: var(--surface-sunken); background-size: 8px 8px;
}

/* -------------------------------------------------------------- dock --- */

.cv-foot {
  padding: var(--sp-1) max(var(--sp-2), env(safe-area-inset-right, 0px)) calc(env(safe-area-inset-bottom, 0px) + var(--sp-2)) max(var(--sp-2), env(safe-area-inset-left, 0px));
  background: var(--plan-ground);
}

.cv-dock {
  display: flex; align-items: stretch;
  max-width: 34rem; margin: 0 auto; padding: 4px;
  background: var(--surface-raised); border: 1px solid var(--line); border-radius: var(--r-xl);
  box-shadow: var(--shadow-float);
}

.cv-tool {
  flex: 1 1 0; min-width: var(--touch); min-height: 52px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  padding: 0 2px; border: 0; border-radius: var(--r-md);
  background: none; color: var(--ink-2);
}

.cv-tool .icon { width: 22px; height: 22px; }
.cv-tool:active { background: var(--surface-sunken); }
.cv-tool:disabled { opacity: 0.35; }

.cv-tool__label {
  max-width: 100%; font-size: 0.6875rem; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.cv-tool--active,
.cv-tool[aria-pressed="true"] { background: var(--accent-wash); color: var(--accent-ink); }

.cv-tool--icon { flex: 0 0 var(--touch); }

.cv-bar .cv-tool { min-height: var(--touch); }

/* ------------------------------------------------------------ sheets --- */

.cv-sheet-block { display: flex; flex-direction: column; gap: var(--sp-1); }

.cv-sheet-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(8rem, 100%), 1fr)); gap: var(--sp-3); }

.cv-sheet-meta { margin: var(--sp-1) var(--sp-1) 0; color: var(--ink-2); font-size: var(--fs-sm); font-variant-numeric: tabular-nums; }

.cv-range { gap: var(--sp-3); flex-wrap: wrap; }
.cv-range .row__main { flex: 1 1 8rem; }

.cv-range__input {
  flex: 1 1 10rem; min-width: 0; height: var(--touch); margin: 0;
  accent-color: var(--accent);
}

.cv-export {
  display: grid; place-items: center;
  padding: var(--sp-2); border-radius: var(--r-md);
  background: var(--surface-sunken);
}

.cv-export__img {
  display: block; max-width: 100%; max-height: 40dvh;
  background: #fff; border-radius: 4px; box-shadow: var(--shadow-card);
}

/* Toasts in canvas mode: below the top bar, never over the dock or a panel,
   and never catching a tap meant for the plan. */
html.cv-open .toast {
  top: calc(env(safe-area-inset-top, 0px) + 60px); bottom: auto;
  pointer-events: none;
}
