/* ============================================================
   WORKBOOK.CSS — the in-app spreadsheet grid.
   Porcelain + Midnight, using the existing tokens only. Note the
   standing convention: legacy token NAMES were re-pointed in
   base.css (--brass resolves to midnight), so they are used here
   as-is rather than "cleaned up".
   Figures use --font-mono with tnum, per the type rule that mono
   is for numbers only.
   ============================================================ */

/* ── page toolbar (header, right side) ──────────────────── */
.wb-toolbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
}
/* "Ask about this sheet": a question box that opens the Copilot dock. */
.wb-ask {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  flex: 1 1 260px;
  max-width: 380px;
  min-width: 200px;
}
.wb-ask .form-input { flex: 1 1 auto; min-width: 0; width: auto; }
.wb-ask .btn { flex: 0 0 auto; }

/* The Workbook is a bare top-level tab: nothing sits between the page header
   and the grid, so the shell takes the height the Close sub-tab bar used to
   (220px - about 55px). Starting value; confirm by eye (docs/BACKLOG.md). */
.wb-shell {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 165px);
  min-height: 420px;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

/* ── sheet tabs (UI-SPEC 3.13.7) ───────────────────────── */
.wb-sheetbar {
  display: flex;
  align-items: stretch;
  gap: var(--space-1);
  padding: 0 var(--space-3);
  border-top: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  flex: 0 0 auto;
}
.wb-tabs { display: flex; align-items: stretch; gap: 2px; flex: 1 1 auto; min-width: 0; overflow-x: auto; scrollbar-width: none; }
.wb-tabs::-webkit-scrollbar { display: none; }
.wb-sheet {
  position: relative;
  padding: 6px 14px;
  border-top: 2px solid transparent;
  font-size: var(--text-sm);
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
  transition: background var(--transition), color var(--transition);
}
.wb-sheet:hover { background: var(--bg-highlight); color: var(--text-primary); }
.wb-sheet.active {
  background: var(--porcelain);
  color: var(--text-primary);
  font-weight: 600;
  border-top-color: var(--cobalt);
}
.wb-sheet.wb-tc::after { content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 3px; background: var(--tc, transparent); }
.wb-tc-cobalt { --tc: var(--cobalt); }
.wb-tc-success { --tc: var(--success); }
.wb-tc-warning { --tc: var(--warning); }
.wb-tc-danger { --tc: var(--danger); }
.wb-tc-navy { --tc: var(--navy-dark); }
.wb-tc-navyMid { --tc: var(--navy-mid); }
.wb-tc-muted { --tc: var(--silver-dark); }
.wb-tc-faint { --tc: var(--silver-mid); }
.wb-tab-input { width: 9em; font: inherit; padding: 0 2px; border: 1px solid var(--cobalt); border-radius: var(--radius-sm); background: var(--bg-card); color: var(--text-primary); }
.wb-tabnav, .wb-addsheet {
  align-self: center;
  border: 1px solid var(--border-color);
  background: var(--bg-card);
  color: var(--text-secondary);
  width: 24px; height: 24px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  line-height: 1;
  font-size: var(--text-xs);
}
.wb-tabnav:hover, .wb-addsheet:hover { color: var(--cobalt); border-color: var(--cobalt); }
.wb-tabmenu { transform: translateY(-100%); z-index: 20; }
.wb-ctx-label { padding: 4px 12px 2px; font-size: var(--text-xs); color: var(--text-secondary); }
.wb-swatches { display: grid; grid-template-columns: repeat(4, 22px); gap: 6px; padding: 4px 12px 8px; }
.wb-swatch { width: 22px; height: 22px; border-radius: var(--radius-sm); border: 1px solid var(--border-color); background: var(--tc); cursor: pointer; padding: 0; }
.wb-swatch:hover, .wb-swatch:focus-visible { outline: 2px solid var(--cobalt); outline-offset: 1px; }

/* ── formula bar ────────────────────────────────────────── */
.wb-fxbar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border-color);
  flex: 0 0 auto;
}
.wb-addr {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: var(--text-sm);
  color: var(--text-secondary);
  min-width: 56px;
  padding: 2px 8px;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  text-align: center;
}
.wb-fx {
  flex: 1;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  outline: none;
}
.wb-fx:focus { border-color: var(--cobalt); background: var(--bg-input); }

/* ── grid frame ─────────────────────────────────────────── */
.wb-grid {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: var(--wb-hw, 52px) 1fr;
  grid-template-rows: var(--wb-hh, 28px) 1fr;
  min-height: 0;
  outline: none;
  position: relative;
}
.wb-corner {
  border-right: 1px solid var(--border-color);
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-surface);
}
.wb-colhead-wrap, .wb-rowhead-wrap { overflow: hidden; position: relative; background: var(--bg-surface); }
.wb-colhead-wrap { border-bottom: 1px solid var(--border-color); }
.wb-rowhead-wrap { border-right: 1px solid var(--border-color); }
.wb-colhead, .wb-rowhead { position: relative; height: 100%; will-change: transform; }

.wb-ch, .wb-rh {
  position: absolute;
  font-size: var(--text-xs);
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  cursor: pointer;
}
.wb-ch { top: 0; height: 27px; border-right: 1px solid var(--border-subtle); }
.wb-rh { left: 0; width: 51px; border-bottom: 1px solid var(--border-subtle); }
.wb-ch.active, .wb-rh.active {
  background: var(--brass-wash);
  color: var(--brass);
  font-weight: 600;
}
.wb-resize {
  position: absolute;
  right: -3px; top: 0; bottom: 0;
  width: 6px;
  cursor: col-resize;
}

/* ── scrolling body ─────────────────────────────────────── */
.wb-viewport { overflow: auto; position: relative; min-height: 0; background: var(--bg-card); }
.wb-sizer { position: relative; }
.wb-cells { position: absolute; inset: 0; }
.wb-grid.wb-nohead .wb-corner, .wb-grid.wb-nohead .wb-colhead-wrap, .wb-grid.wb-nohead .wb-rowhead-wrap { display: none; }

.wb-cell {
  position: absolute;
  display: flex;
  align-items: center;
  padding: 0 6px;
  font-size: var(--text-sm);
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  box-sizing: border-box;
}
/* Figures are mono + tabular so columns of numbers line up on the decimal. */
.wb-cell.wb-num {
  justify-content: flex-end;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}
.wb-cell.wb-bool { justify-content: center; text-transform: uppercase; font-size: var(--text-xs); }
.wb-cell.wb-err {
  justify-content: center;
  color: var(--danger);
  font-family: var(--font-mono);
  font-size: var(--text-xs);
}

.wb-selbox {
  position: absolute;
  border: 2px solid var(--cobalt);
  border-radius: 2px;
  pointer-events: none;
  box-sizing: border-box;
  transition: left 60ms var(--ease-ios), top 60ms var(--ease-ios);
}
.wb-editor {
  position: absolute;
  display: none;
  border: 2px solid var(--cobalt);
  background: var(--bg-card);
  color: var(--text-primary);
  font-size: var(--text-sm);
  font-family: var(--font-mono);
  padding: 0 4px;
  box-sizing: border-box;
  outline: none;
  z-index: 3;
}

/* ── status strip ───────────────────────────────────────── */
.wb-status {
  flex: 0 0 auto;
  padding: var(--space-2) var(--space-3);
  border-top: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* The grid draws its own hairlines, so the porcelain ground needs a slightly
   stronger line in dark mode or the cells dissolve into the card. */
[data-theme="dark"] .wb-gl { background: var(--border-color); }

/* ── ribbon (UI-SPEC 3.13.3) ────────────────────────────── */
.wb-ribbon { flex: 0 0 auto; border-bottom: 1px solid var(--border-color); background: var(--bg-surface); }
.wb-rtabs { display: flex; gap: var(--space-1); padding: 0 var(--space-3); }
.wb-rtab {
  background: transparent; border: 0; border-bottom: 2px solid transparent;
  padding: var(--space-2) var(--space-3); font-size: var(--text-sm);
  color: var(--text-secondary); cursor: pointer;
}
.wb-rtab:hover { color: var(--text-primary); }
.wb-rtab.active { color: var(--cobalt); border-bottom-color: var(--cobalt); }
.wb-rbody { display: flex; gap: var(--space-4); padding: var(--space-2) var(--space-3); border-top: 1px solid var(--border-subtle); }
.wb-rgroup { display: flex; flex-direction: column; gap: var(--space-1); }
.wb-rbtns { display: flex; gap: var(--space-1); }
.wb-rbtn {
  display: inline-flex; align-items: center; gap: var(--space-1);
  background: transparent; border: 1px solid var(--border-subtle); border-radius: var(--radius-sm);
  padding: 4px 10px; font-size: var(--text-sm); color: var(--text-primary); cursor: pointer;
}
.wb-rbtn:hover { border-color: var(--cobalt); }
.wb-rbtn.active { background: var(--cobalt-wash); border-color: var(--cobalt); color: var(--cobalt); }
.wb-rglabel { font-size: var(--text-xs); color: var(--text-muted); }

/* ── name box and formula bar (3.13.4) ──────────────────── */
.wb-namebox { position: relative; display: flex; align-items: center; }
input.wb-addr { width: 96px; background: var(--bg-input); color: var(--text-primary); }
input.wb-addr:focus { border-color: var(--cobalt); outline: none; }
.wb-addr-caret { background: transparent; border: 0; color: var(--text-muted); cursor: pointer; padding: 2px 4px; }
.wb-addr-list {
  position: absolute; top: 100%; left: 0; z-index: 10; min-width: 160px; max-height: 220px; overflow: auto;
  background: var(--bg-surface); border: 1px solid var(--border-color); border-radius: var(--radius-sm);
}
.wb-addr-list[hidden], .wb-addr-caret[hidden] { display: none; }
.wb-addr-item { display: block; width: 100%; text-align: left; background: transparent; border: 0; padding: 4px 10px; font-size: var(--text-sm); color: var(--text-primary); cursor: pointer; }
.wb-addr-item:hover { background: var(--bg-highlight); }
textarea.wb-fx { resize: none; line-height: 1.4; overflow: auto; }

/* ── status bar (3.13.6) ────────────────────────────────── */
.wb-status { display: flex; align-items: center; gap: var(--space-4); min-height: 28px; }
.wb-st-state { color: var(--text-secondary); }
.wb-st-calc { color: var(--cobalt); }
.wb-st-stats { display: flex; gap: var(--space-3); margin-left: auto; font-variant-numeric: tabular-nums; }
.wb-st-zoom { display: inline-flex; align-items: center; gap: var(--space-1); }
.wb-zbtn, .wb-zpct { background: transparent; border: 0; color: var(--text-secondary); cursor: pointer; font-size: var(--text-xs); padding: 2px 6px; }
.wb-zbtn:hover, .wb-zpct:hover { color: var(--cobalt); }
.wb-zrange { width: 96px; accent-color: var(--cobalt); }

/* ── Pass 3 T5: panes, selection, references, menus ─────── */
.wb-colhead-fz, .wb-rowhead-fz { position: absolute; top: 0; left: 0; background: var(--bg-surface); z-index: 2; display: none; }
.wb-colhead-fz { height: 100%; border-right: 2px solid var(--border-strong, var(--border-color)); }
.wb-rowhead-fz { width: 100%; border-bottom: 2px solid var(--border-strong, var(--border-color)); }
.wb-pane { position: absolute; overflow: hidden; background: var(--bg-card); z-index: 1; display: none; }
.wb-pane-top { border-bottom: 2px solid var(--border-strong, var(--border-color)); }
.wb-pane-left { border-right: 2px solid var(--border-strong, var(--border-color)); }
.wb-pane-corner { border-right: 2px solid var(--border-strong, var(--border-color)); border-bottom: 2px solid var(--border-strong, var(--border-color)); z-index: 2; }
.wb-pane-in { position: absolute; left: 0; top: 0; width: 0; height: 0; will-change: transform; }
.wb-gl { position: absolute; background: var(--border-subtle); pointer-events: none; }
.wb-gl-h { height: 1px; }
.wb-gl-v { width: 1px; }
.wb-grid.wb-nogrid .wb-gl { display: none; }
.wb-cell.wb-al-l { justify-content: flex-start; }
.wb-cell.wb-al-c { justify-content: center; }
.wb-cell.wb-al-r { justify-content: flex-end; }
.wb-cell.wb-spill { color: var(--text-primary); }
.wb-fc-danger { color: var(--danger); }
.wb-fc-success { color: var(--success); }
.wb-fc-cobalt { color: var(--cobalt); }
.wb-fc-warning { color: var(--warning); }
.wb-fc-text { color: var(--text-primary); }
.wb-fc-white { color: var(--bg-card); }
.wb-selrange { position: absolute; background: var(--cobalt-wash); border: 1px solid var(--cobalt); pointer-events: none; box-sizing: border-box; }
.wb-selcur { position: absolute; border: 2px solid var(--cobalt); pointer-events: none; box-sizing: border-box; }
.wb-refbox { position: absolute; border: 2px solid var(--cobalt); pointer-events: none; box-sizing: border-box; }
.wb-marquee { position: absolute; border: 2px dashed var(--navy-mid); pointer-events: none; box-sizing: border-box; }
.wb-focusband { position: absolute; background: var(--brass-wash); pointer-events: none; }
.wb-editor { z-index: 4; }
.wb-sig, .wb-ac, .wb-ctx {
  position: absolute; z-index: 6; background: var(--bg-card); color: var(--text-primary);
  border: 1px solid var(--border-color); border-radius: var(--radius-sm); box-shadow: var(--shadow-sm);
  font-size: var(--text-xs);
}
.wb-sig { padding: 4px 8px; font-family: var(--font-mono); color: var(--text-secondary); max-width: 420px; }
.wb-ac { min-width: 220px; max-height: 220px; overflow-y: auto; }
.wb-ac-i { padding: 4px 8px; cursor: pointer; display: flex; gap: var(--space-2); }
.wb-ac-i b { font-family: var(--font-mono); font-weight: 600; }
.wb-ac-i span { color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wb-ac-i.on, .wb-ac-i:hover { background: var(--cobalt-wash); }
.wb-ctx { position: absolute; min-width: 190px; padding: 4px 0; }
.wb-ctx-i { display: block; width: 100%; text-align: left; padding: 6px 12px; border: 0; background: transparent; color: var(--text-primary); font-size: var(--text-sm); cursor: pointer; }
.wb-ctx-i:hover:not(:disabled) { background: var(--cobalt-wash); }
.wb-ctx-i:disabled { color: var(--text-muted); cursor: default; }
.wb-ctx-sep { height: 1px; margin: 4px 0; background: var(--border-subtle); }

/* Home tab: cell looks and dialogs (T6) */
.wb-cell.wb-merged { z-index: 1; background-color: var(--bg-card); }
.wb-cell[class*="wb-role-"] { display: flex; align-items: center; }
.wb-dialog { position: absolute; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; background: color-mix(in srgb, var(--navy-dark) 25%, transparent); }
.wb-dialog[hidden] { display: none; }
.wb-dlg { background: var(--bg-card); border: 1px solid var(--silver-light); border-radius: var(--radius-md, 8px); padding: 16px; width: min(420px, calc(100% - 32px)); max-height: calc(100% - 32px); overflow: auto; color: var(--text-primary); }
.wb-dlg-t { margin: 0 0 12px; font-size: var(--text-md, 16px); }
.wb-dlg-b { display: grid; gap: 8px; }
.wb-dlg-f { display: grid; grid-template-columns: 130px 1fr; align-items: center; gap: 8px; font-size: var(--text-sm); }
.wb-dlg-f input, .wb-dlg-f select { padding: 6px 8px; border: 1px solid var(--silver-light); border-radius: 6px; background: var(--white); color: var(--text-primary); font: inherit; }
.wb-dlg-c { display: flex; gap: 8px; align-items: center; font-size: var(--text-sm); }
.wb-dlg-msg, .wb-dlg-res { min-height: 18px; margin-top: 8px; font-size: var(--text-sm); color: var(--danger); }
.wb-dlg-res { color: var(--text-secondary); }
.wb-dlg-a { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; flex-wrap: wrap; }

/* Formulas and Data tabs (T8): Insert function, Name manager, Error checking, Goal seek */
.wb-fn-list { max-height: 180px; overflow: auto; border: 1px solid var(--silver-light); border-radius: 6px; display: flex; flex-direction: column; }
.wb-fn-row { text-align: left; padding: 4px 10px; border: 0; background: transparent; color: var(--text-primary); font: inherit; font-size: var(--text-sm); cursor: pointer; }
.wb-fn-row:hover { background: var(--cobalt-wash); }
.wb-fn-row.is-on { background: var(--cobalt-wash); color: var(--cobalt); font-weight: 600; }
.wb-fn-help { font-size: var(--text-sm); color: var(--text-secondary); min-height: 54px; display: grid; gap: 4px; }
.wb-fn-sig { font-family: var(--font-mono, monospace); color: var(--text-primary); }
.wb-fn-ex { font-family: var(--font-mono, monospace); color: var(--text-muted); }
.wb-names-t { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
.wb-names-t th, .wb-names-t td { text-align: left; padding: 4px 8px; border-bottom: 1px solid var(--border-subtle); }
.wb-names-t tbody tr { cursor: pointer; }
.wb-names-t tbody tr:hover, .wb-names-t tbody tr.is-on { background: var(--cobalt-wash); }
.wb-err-list { display: grid; gap: 4px; max-height: 280px; overflow: auto; }
.wb-err-row { display: grid; grid-template-columns: 70px 90px 1fr auto; gap: 8px; align-items: center; font-size: var(--text-sm); }
.wb-err-code { color: var(--danger); font-weight: 600; }
.wb-err-f { color: var(--text-secondary); font-family: var(--font-mono, monospace); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wb-card { display: grid; gap: 4px; padding: 10px 12px; border: 1px solid var(--silver-light); border-left: 3px solid var(--success); border-radius: 6px; background: var(--bg-card); font-size: var(--text-sm); }
