/**
 * Fernwork — Canvas ("Fernboard"): frame bar, board bar, split editors, previews, export dialog, board shelf.
 * Loaded after office-canvas.css (which keeps the shared workspace/split/Excalidraw rules) and only on
 * tools/canvas.html. Kit tokens only.
 */

/* Own buttons start from a clean slate (no UA borders/backgrounds). */
.fb-boardname, .fb-newcard, .fb-card__open { appearance: none; border: 0; background: transparent; font: inherit; color: inherit; cursor: pointer; }

/* ───────────── Workspace ───────────── */
.fb-workspace {
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--bg);
  box-shadow: none;
}

/* ───────────── Frame bar (one row) ───────────── */
.fb-framebar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
  min-height: 48px;
  padding: 6px var(--space-2) 6px 6px;
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg);
  flex: 0 0 auto;
}
.fb-framebar__boards { display: flex; align-items: center; min-width: 0; flex: 0 1 auto; }
.fb-framebar__actions {
  display: flex; align-items: center; gap: 2px; flex: none;
  padding-left: var(--space-2); border-left: 1px solid var(--border-subtle);
}
.fb-action { color: var(--text-2); }
.fb-action[aria-expanded="true"] { background: var(--surface-active); color: var(--text-1); }
.fb-status { font-size: var(--text-xs); color: var(--text-3); white-space: nowrap; font-variant-numeric: tabular-nums; }
.fb-more { display: none; }
.fb-export-btn { flex: none; }

/* Board bar: All boards · name menu · history · save state */
.fb-boardbar { display: flex; align-items: center; gap: 2px; min-width: 0; }
.fb-boardname {
  display: inline-flex; align-items: center; gap: 6px; min-width: 0; max-width: 280px;
  height: var(--control-h-sm); padding: 0 8px; border-radius: var(--radius-sm);
  font-size: var(--text-sm); font-weight: 600; color: var(--text-1);
  transition: background var(--dur-1) var(--ease-out);
}
.fb-boardname[aria-expanded="true"] { background: var(--surface-hover); }
@media (hover: hover) { .fb-boardname:hover { background: var(--surface-hover); } }
.fb-boardname .fw-icon { color: var(--text-3); flex: none; }
.fb-boardname__text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.fb-save {
  display: inline-flex; align-items: center; gap: 6px; padding: 0 8px 0 4px;
  font-size: var(--text-xs); color: var(--text-3); white-space: nowrap;
}
.fb-save::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: var(--accent-fill); flex: none; }
.fb-save[data-state="dirty"]::before { background: var(--text-disabled); }
.fb-save[data-state="saving"]::before { background: var(--text-disabled); animation: fb-pulse 900ms var(--ease-in-out) infinite alternate; }
.fb-save[data-state="error"] { color: var(--danger); }
.fb-save[data-state="error"]::before { background: var(--danger); }
@keyframes fb-pulse { to { opacity: .35; } }
@media (prefers-reduced-motion: reduce) { .fb-save[data-state="saving"]::before { animation: none; } }

/* Version history popover */
.fb-versions { display: flex; flex-direction: column; max-height: min(420px, 70dvh); }
.fb-versions__head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
  padding: 10px 12px 8px; font-size: var(--text-sm); font-weight: 600; color: var(--text-1);
  border-bottom: 1px solid var(--border-subtle);
}
.fb-versions__head .fw-faint { font-weight: 400; font-size: var(--text-xs); }
.fb-versions__list { list-style: none; margin: 0; padding: 4px; overflow: auto; }
.fb-versions__row { display: flex; align-items: center; gap: 8px; padding: 6px 6px 6px 8px; border-radius: var(--radius-sm); }
@media (hover: hover) { .fb-versions__row:hover { background: var(--overlay-hover); } }
.fb-versions__time { flex: 1; min-width: 0; display: flex; flex-direction: column; font-size: var(--text-sm); color: var(--text-1); font-variant-numeric: tabular-nums; }
.fb-versions__time .fw-faint { font-size: var(--text-xs); }
.fb-versions__empty { margin: 0; padding: 14px 12px; font-size: var(--text-sm); color: var(--text-3); }

/* ───────────── Draw ───────────── */
.fb-loading {
  display: flex; align-items: center; justify-content: center; gap: 10px; height: 100%; min-height: 240px;
  color: var(--text-3); font-size: var(--text-sm);
}

/* ───────────── Split editors (Mind Map, Mermaid) ───────────── */
.fb-paneswitch { display: none; }
.fb-split { flex: 1; min-height: 0; }
.fb-split .oo-split-resizer { width: 6px; background: var(--border-subtle); }
.fb-split .oo-split-resizer:hover,
.fb-split .oo-split-resizer:focus-visible { background: color-mix(in srgb, var(--accent-fill) 45%, transparent); outline: none; }
.fb-pane { background: var(--bg); }
.fb-editor {
  padding: 16px 18px;
  background: var(--bg);
  color: var(--text-1);
  font-size: var(--text-sm);
  tab-size: 2;
}
.fb-editor::placeholder { color: var(--text-disabled); }
/* iOS Safari zooms the page into any field under 16 px on every tap: keep source fields at 16 px on touch screens. */
@media (pointer: coarse) { .fb-editor { font-size: var(--text-md); } }
.fb-editor:focus { box-shadow: inset 2px 0 0 var(--accent-fill); }
.fb-error.is-visible { color: var(--danger); background: var(--danger-bg); border-top-color: var(--danger-border); font-family: var(--font-mono); font-size: var(--text-xs); max-height: 40%; overflow: auto; }

.fb-pane--preview { position: relative; }
.fb-preview {
  flex: 1;
  min-height: 0;
  padding: 0;
  overflow: auto;
  background:
    radial-gradient(circle at 12px 12px, color-mix(in srgb, var(--text-3) 22%, transparent) 1px, transparent 1.5px) 0 0 / 24px 24px,
    var(--stage);
}
.oo-mindmap-shell .fb-preview { overflow: hidden; }
.oo-mindmap-shell .fb-preview__stage { width: 100%; height: 100%; }
.oo-mindmap-shell .fb-preview__stage > svg,
.oo-mindmap-shell #mindmap-svg { display: block; width: 100%; height: 100%; min-height: 0; max-width: none; margin: 0; }
/* Readable mind map labels in dark mode (Markmap's own default text colour is #333). */
[data-theme="dark"] .oo-mindmap-shell .markmap { --markmap-text-color: var(--text-1); --markmap-circle-open-bg: var(--surface-1); }
.oo-mermaid-shell .fb-preview__stage {
  box-sizing: border-box; width: max-content; min-width: 100%; padding: 28px 24px 76px;
}
.oo-mermaid-shell .fb-preview__stage > svg {
  box-sizing: content-box; padding: 20px; border-radius: var(--radius-md);
  background: var(--surface-1); border: 1px solid var(--border-subtle); box-shadow: var(--shadow-1);
}
/* G1: while frozen (divider drag, window resize) the last frame keeps its size, centred and cropped. */
.fb-preview:has(> .fb-preview__stage.fw-stable-frozen) { overflow: hidden; display: grid; place-content: center; }
.fb-preview .oo-empty-state { min-height: 100%; }

/* Off-screen layout for exports while the preview pane is hidden (phones). */
.fb-offscreen { position: fixed; left: -10000px; top: 0; width: 1200px; height: 800px; overflow: hidden; visibility: hidden; pointer-events: none; }
.fb-offscreen > svg { width: 1200px; height: 800px; }

/* Render state: visible while changes are pending or rendering failed; the ready text stays for screen readers. */
.fb-render-state {
  position: absolute; left: 12px; top: 12px; z-index: 2;
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: var(--radius-pill);
  font-size: var(--text-xs); color: var(--text-2); background: var(--surface-1);
  border: 1px solid var(--border-subtle); box-shadow: var(--shadow-1); pointer-events: none;
}
.fb-render-state[data-state="error"] { color: var(--danger); background: var(--danger-bg); border-color: var(--danger-border); }
.fb-render-state:is([data-state="ready"], [data-state="idle"], [data-state="busy"]) {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0; box-shadow: none;
}
.fb-zoom { z-index: 2; }
.fb-zoom__sep { width: 1px; height: 20px; background: var(--border-subtle); margin: 0 2px; }
.fb-zoom .fw-zoom__pct { min-width: 44px; }

/* ───────────── Export dialog ───────────── */
.fb-export-dialog .fw-dialog__body { padding-top: var(--space-2); }
.fb-export { display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(240px, 1fr); gap: var(--space-5); align-items: start; }
.fb-export__preview {
  position: relative; display: grid; place-items: center; height: 340px; padding: 16px; overflow: hidden;
  border: 1px solid var(--border-subtle); border-radius: var(--radius-md);
  background: repeating-conic-gradient(var(--checker-a) 0 25%, var(--checker-b) 0 50%) 0 0 / 16px 16px;
}
.fb-export__view { width: 100%; height: 100%; display: grid; place-items: center; min-height: 0; }
.fb-export__img { display: block; max-width: 100%; max-height: 306px; object-fit: contain; box-shadow: var(--shadow-1); }
.fb-export__page {
  display: grid; place-items: center; height: 100%; max-width: 100%; padding: 4%; box-sizing: border-box;
  background: #fff; box-shadow: var(--shadow-2);
}
.fb-export__page img { display: block; max-width: 100%; max-height: 100%; object-fit: contain; }
.fb-export__text {
  align-self: stretch; justify-self: stretch; margin: 0; padding: 12px; overflow: auto; border-radius: var(--radius-sm);
  background: var(--surface-1); color: var(--text-2); font: 12px/1.5 var(--font-mono); white-space: pre-wrap; word-break: break-word;
}
.fb-export__error { align-self: center; max-width: 100%; }
.fb-export__form { display: flex; flex-direction: column; gap: var(--space-4); min-width: 0; }
.fb-export__formats { align-self: flex-start; }
.fb-export__name { display: flex; align-items: center; gap: 6px; min-width: 0; }
.fb-export__name .fw-input { flex: 1; min-width: 0; }
.fb-export__ext { color: var(--text-3); font-family: var(--font-mono); font-size: var(--text-sm); flex: none; }
.fb-export__size { margin: 0; min-height: 18px; font-size: var(--text-xs); color: var(--text-3); }
.fb-export-dialog .fw-dialog__foot .fw-btn .fw-spinner { width: 14px; height: 14px; }

/* ───────────── Board shelf ───────────── */
.fb-shelf { display: flex; flex-direction: column; gap: var(--space-5); }
.fb-shelf__new { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--space-3); }
.fb-newcard {
  display: flex; align-items: center; gap: var(--space-3); min-height: 64px; min-width: 0; padding: var(--space-3);
  border: 1px solid var(--border-subtle); border-radius: var(--radius-md); background: var(--surface-1); text-align: left;
  transition: border-color var(--dur-1) var(--ease-out), background var(--dur-1) var(--ease-out);
}
.fb-newcard[aria-expanded="true"] { border-color: var(--accent-border); background: var(--surface-2); }
@media (hover: hover) { .fb-newcard:hover { border-color: var(--accent-border); background: var(--surface-2); } }
.fb-newcard__icon {
  display: grid; place-items: center; width: 36px; height: 36px; flex: none; border-radius: 10px;
  background: var(--accent-subtle-bg); color: var(--accent-text);
}
.fb-newcard--more .fb-newcard__icon { background: var(--surface-sunken); color: var(--text-2); }
.fb-newcard__text { display: flex; flex-direction: column; min-width: 0; }
.fb-newcard__title { font-size: var(--text-sm); font-weight: 600; color: var(--text-1); }
.fb-newcard__sub { font-size: var(--text-xs); color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fb-shelf__bar { display: flex; align-items: center; flex-wrap: wrap; gap: var(--space-3); }
.fb-shelf__h { margin: 0; font-size: var(--text-md); font-weight: 600; color: var(--text-1); }
.fb-shelf__grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(196px, 1fr)); gap: var(--space-4); }
.fb-shelf__loading { display: contents; }
.fb-card-skel { aspect-ratio: 16 / 13; border-radius: var(--radius-md); }
.fb-shelf__empty { grid-column: 1 / -1; margin: 0; padding: var(--space-6) 0; text-align: center; font-size: var(--text-sm); color: var(--text-3); }
.fb-card {
  display: flex; flex-direction: column; min-width: 0; overflow: hidden;
  border: 1px solid var(--border-subtle); border-radius: var(--radius-md); background: var(--surface-1);
  transition: border-color var(--dur-1) var(--ease-out), box-shadow var(--dur-1) var(--ease-out);
}
@media (hover: hover) { .fb-card:hover { border-color: var(--border-strong); box-shadow: var(--shadow-2); } }
.fb-card.is-current { border-color: var(--accent-border); }
.fb-card__open { position: relative; display: block; width: 100%; aspect-ratio: 16 / 10; min-height: 0; padding: 0; overflow: hidden; background: var(--surface-sunken); border-bottom: 1px solid var(--border-subtle); }
.fb-card__open:focus-visible { outline-offset: -2px; }
.fb-card__thumb { position: absolute; inset: 0; display: grid; place-items: center; overflow: hidden; }
.fb-card__thumb img { position: absolute; inset: 0; display: block; width: 100%; height: 100%; padding: 10px; box-sizing: border-box; object-fit: contain; }
.fb-card__placeholder { color: var(--text-disabled); }
.fb-card__meta { display: flex; align-items: center; gap: 4px; min-width: 0; padding: 8px 6px 8px 12px; }
.fb-card__text { display: flex; flex-direction: column; gap: 2px; flex: 1; min-width: 0; }
.fb-card__name { font-size: var(--text-sm); font-weight: 600; color: var(--text-1); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fb-card__sub { display: flex; align-items: center; gap: 6px; min-width: 0; font-size: var(--text-xs); color: var(--text-3); white-space: nowrap; }
.fb-card__sub > span { overflow: hidden; text-overflow: ellipsis; }
.fb-card__badge { flex: none; }
.fb-prompt { margin: 0; }
.fb-confirm { margin: 0; color: var(--text-2); }

/* ───────────── Narrower screens ───────────── */
.fb-action__label { white-space: nowrap; }
@media (max-width: 1279px) {
  .fb-action .fb-action__label {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
  }
  .fb-action { padding: 0 8px; }
  .fb-boardname { max-width: 200px; }
}
@media (max-width: 1023px) {
  .fb-status { display: none; }
  .fb-export { grid-template-columns: 1fr; }
  .fb-export__preview { height: 240px; }
  .fb-export__img { max-height: 206px; }
  .fb-shelf__new { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 767px) {
  .tab-content.active:has(.fb-workspace) .oo-workspace,
  /* Full-bleed like the viewer frames: the board gets the whole phone width (no gutter, no card corners). */
  .fb-workspace { min-height: 0; margin-inline: calc(-1 * var(--gutter, 16px)); border-inline: 0; border-bottom: 0; border-radius: 0; }
  .fb-framebar { gap: 2px; padding-inline: 4px; }
  .fb-framebar__actions { display: none; }
  .fb-more { display: inline-grid; }
  .fb-boardname { max-width: none; flex: 0 1 auto; }
  .fb-save { padding: 0 4px; }
  .fb-save { font-size: 0; gap: 0; }
  .fb-export-btn { padding: 0 12px; min-width: 44px; }
  .fb-export-btn__label {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
  }

  .fb-paneswitch { display: flex; margin: 6px; flex: none; }
  .fb-paneswitch .fw-seg__opt { flex: 1; }
  .fb-split,
  .fb-split[style] { grid-template-columns: minmax(0, 1fr) !important; grid-template-rows: minmax(0, 1fr) !important; }
  .fb-split .oo-split-resizer { display: none; }
  .fb-split[data-view="source"] .fb-pane--preview,
  .fb-split[data-view="preview"] .fb-pane--source { display: none; }

  .fb-shelf__grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-3); }
  .fb-export__formats { align-self: stretch; }
  .fb-export__formats .fw-seg__opt { flex: 1; }
  .fb-shelf__bar .fw-spacer { display: none; }
  .fb-shelf__filter { order: 3; width: 100%; }
  .fb-shelf__filter .fw-seg__opt { flex: 1; }
  .fb-newcard { padding: 10px; gap: 10px; }
  .fb-newcard__icon { width: 32px; height: 32px; }
}
@media (max-width: 359px) {
  .fb-shelf__new { grid-template-columns: minmax(0, 1fr); }
  .fb-shelf__grid { grid-template-columns: minmax(0, 1fr); }
}
