/* ──────────────────────────────────────────────────────────────────────────
   Command toolbars — one geometry for both of them.

   There are two: the GLOBAL strip under the navbar (`.lf-full-edit-toolbar`,
   Views/Shared/_FullEditToolbar.cshtml) and each view's own FUNCTION BAR
   (`.section-toolbar-row`, built by core/sectionPage.ts). They were drawn by two
   unrelated sets of rules, so their buttons agreed on nothing: some carried a
   label and some did not, `.section-tb-btn` was a hard 2rem square while
   `.lf-fe-btn` took whatever width Bootstrap's padding gave it, and the split
   carets were a third size again.

   This file owns the part that has to be the same in both:

     • ONE HEIGHT for every button, in every mode, so a row of them reads as a
       row and not as a skyline (--lf-tb-btn-height).
     • ONE SQUARE for a button that shows only an icon — same width as height,
       so icon-only mode is a grid of identical keys.
     • THREE SHAPES, selected by `data-lf-toolbar-buttons` on <html>: icon and
       text, icon only, text only. The attribute is written server-side by
       _LiveLayout.cshtml from the user's cookie (Theming/ToolbarButtonStyle.cs)
       and rewritten live by core/toolbarStyle.ts.
     • THE OVERFLOW MENU both toolbars fall back to when the row runs out of
       width (core/toolbarOverflow.ts).

   Colours are NOT here, with one exception: the overflow panel needs a ground of
   its own or it would be a menu drawn over whatever is behind it, so it takes the
   theme's panel pair as a fallback. Which pair each toolbar's panel actually uses
   is css/core/theme.css's business, like every other colour.
   ────────────────────────────────────────────────────────────────────────── */

:root {
  /* The one measurement everything else is derived from. Both toolbars used
     2rem-tall buttons already (section.css's .section-tb-btn, and what
     btn-sm + the icon line-height happened to produce in the global strip), so
     this keeps today's density and only makes it deliberate. */
  --lf-tb-btn-height: 2rem;
  /* Horizontal breathing room for a button that carries a label. A square
     button ignores it — its padding is zero and its width is its height. */
  --lf-tb-btn-padding: 0.55rem;
  --lf-tb-btn-gap: 0.35rem;
  --lf-tb-btn-font: 0.8rem;
  /* A split button's caret is deliberately narrower than a command: it is an
     affordance on the button beside it, not a button in its own right. */
  --lf-tb-caret-width: 1.1rem;
  /* The height of a toolbar ROW (issue #339): one button, the row's own padding and its 1px
     edge. The application bar (a row) and the command strip answer to the same number, so the
     two are interchangeable in a dock zone — swap them and nothing under them moves — and so is
     the logo cell beside them, which takes it as its minimum. */
  --lf-bar-height: calc(var(--lf-tb-btn-height) + 0.5rem + 1px);
}

/* ── The button contract ───────────────────────────────────────────────────
   Three class names because there are three families of markup and renaming
   them all would have touched every call site for no gain: `.lf-fe-btn` is the
   global strip's, `.section-tb-btn` the function bar's, and `.lf-tb-btn` is
   what anything new should use. */
.lf-fe-btn,
.section-tb-btn,
.lf-tb-btn {
  box-sizing: border-box;
  height: var(--lf-tb-btn-height);
  min-height: var(--lf-tb-btn-height);
  /* Width is free (the label decides) but never narrower than the square. */
  width: auto;
  min-width: var(--lf-tb-btn-height);
  padding: 0 var(--lf-tb-btn-padding);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--lf-tb-btn-gap);
  font-size: var(--lf-tb-btn-font);
  line-height: 1;
  white-space: nowrap;
  /* Toolbars measure themselves to decide what overflows; a button that
     shrinks would make that measurement a moving target. */
  flex: 0 0 auto;
}

/* The icon: a fixed box, so a wide glyph and a narrow one leave the label in
   the same place down a column of buttons.

   `i` AND `svg` everywhere an icon is selected in this file: the layout loads Font
   Awesome's JS build, which replaces every <i class="fa-…"> with an <svg> (keeping
   the classes) once it runs. A rule that names only `i` therefore applies for the
   first few hundred milliseconds of the page's life and never again — which is
   exactly the kind of bug that looks like "it works on my machine, on reload". */
.lf-fe-btn > i,
.lf-fe-btn > svg,
.section-tb-btn > i,
.section-tb-btn > svg,
.lf-tb-btn > i,
.lf-tb-btn > svg {
  flex: 0 0 auto;
  width: 1em;
  text-align: center;
}

.lf-fe-label,
.lf-tb-label {
  font-size: inherit;
  line-height: 1;
}

/* A button with no label of its own is a square: the pagination arrows, the
   caret of a split button, anything whose meaning is the glyph. `:has()` reads
   the DOM, not what is visible — hiding a label does NOT make a button match
   this, which is why icon-only mode below squares its buttons explicitly. */
.lf-fe-btn:not(:has(> .lf-fe-label, > .lf-tb-label)),
.section-tb-btn:not(:has(> .lf-tb-label)),
.lf-tb-btn:not(:has(> .lf-tb-label)) {
  width: var(--lf-tb-btn-height);
  padding: 0;
}

/* Split buttons: the caret keeps the shared height and its own narrow width,
   and the two halves must not be pushed apart by the button gap. */
.lf-full-edit-toolbar .lf-split-btn-caret,
.section-page-card .lf-split-btn-caret,
.lf-split-btn > .lf-tb-btn.lf-split-btn-caret {
  width: var(--lf-tb-caret-width);
  min-width: var(--lf-tb-caret-width);
  height: var(--lf-tb-btn-height);
  padding: 0;
}

.lf-split-btn > .btn + .btn {
  margin-left: 0;
}

/* The second icon that rides on Bulk insert names the family without a second
   word of label; it must not be mistaken for the button's own icon. */
.lf-fe-badge-icon {
  width: auto;
  font-size: 0.6em;
  margin-left: -0.15rem;
  align-self: flex-start;
}

/* ── Shape 2: icon only ───────────────────────────────────────────────────
   Every label stops being drawn and every button becomes the same square: one
   width, one height, nothing to line up by eye. The labels stay in the DOM —
   the tooltip and the overflow menu both read them. */
html[data-lf-toolbar-buttons="icon"] .lf-fe-label,
html[data-lf-toolbar-buttons="icon"] .lf-tb-label {
  display: none;
}

/* Squared here rather than by the `:not(:has())` rule above: that one asks
   whether a label EXISTS, and the labels are still in the DOM (the tooltip and
   the overflow menu both need them) — they are only not drawn. The caret keeps
   its own narrower width; it is the one button that is not a command. */
html[data-lf-toolbar-buttons="icon"] .lf-fe-btn:not(.lf-split-btn-caret),
html[data-lf-toolbar-buttons="icon"] .section-tb-btn:not(.lf-split-btn-caret),
html[data-lf-toolbar-buttons="icon"] .lf-tb-btn:not(.lf-split-btn-caret) {
  width: var(--lf-tb-btn-height);
  padding: 0;
}

/* ── Shape 3: text only ───────────────────────────────────────────────────
   The icon goes away only where a label is there to replace it. A button that
   never had one — the pagination arrows, a caret — keeps its icon and stays a
   square: an empty button is not a text button. */
html[data-lf-toolbar-buttons="text"] .lf-fe-btn:has(> .lf-fe-label, > .lf-tb-label) > i,
html[data-lf-toolbar-buttons="text"] .lf-fe-btn:has(> .lf-fe-label, > .lf-tb-label) > svg,
html[data-lf-toolbar-buttons="text"] .section-tb-btn:has(> .lf-tb-label) > i,
html[data-lf-toolbar-buttons="text"] .section-tb-btn:has(> .lf-tb-label) > svg,
html[data-lf-toolbar-buttons="text"] .lf-tb-btn:has(> .lf-tb-label) > i,
html[data-lf-toolbar-buttons="text"] .lf-tb-btn:has(> .lf-tb-label) > svg {
  display: none;
}

/* ── The global strip ─────────────────────────────────────────────────────
   One row, never two: what does not fit goes to the overflow menu rather than
   wrapping, because a strip that silently grows a second row is a strip whose
   height nobody predicted — the header, or the dock zone it was moved to,
   takes that row out of the page. (It used to be position:fixed, and the same
   sentence was about --lf-header-height telling the truth; issue #188 F1b put
   it in the flow, which makes the height honest and the rule no less useful.)

   No `overflow: hidden` here or on the function bar below, tempting as it is:
   every dropdown in either toolbar (the view menu, a split button's menu, the
   overflow panel itself) is absolutely positioned inside it, and clipping the
   box clips them. core/toolbarOverflow.ts measures the trailing edge of the
   content against the trailing edge of the row instead, which needs no
   clipping — and which is why the row is briefly too wide, for one frame
   before the first pass runs, rather than silently cut off forever. */
.lf-full-edit-toolbar {
  flex-wrap: nowrap;
  gap: 0.5rem;
}

.lf-fe-group {
  flex: 0 0 auto;
  min-width: 0;
}

/* An empty group (every button in it moved to the overflow menu, or none of
   them relevant to what has focus) must not still claim the gap around it.
   `:not(:has(> *))` rather than `:empty`, which the whitespace between the
   buttons in the Razor source already disqualifies. A button that is still in
   the group but `d-none` is the same kind of empty: the PDF viewer, which is
   not a section, hides every section command and would otherwise leave a run
   of zero-width groups and their gaps in front of its own. */
.lf-fe-group:not(:has(> *)),
.lf-fe-group:not(:has(.lf-fe-btn:not(.d-none), .lf-tb-btn:not(.d-none))) {
  display: none;
}

/* Read-only sits outside every .lf-fe-group so overflow leaves it alone
   (fixedSelector in fullEditToolbar.ts). margin-left: auto is in fullEdit.css. */
.lf-fe-readonly {
  flex: 0 0 auto;
}

/* ── The function bar ─────────────────────────────────────────────────────
   A three-zone grid: title / commands / page info.

   Two things have to be true for the commands zone to be measurable, and neither
   was before:

   `justify-self: stretch` (section.css centres it instead) — a grid item that is
   `justify-self: center` is sized to its own content and centred in the track,
   so it is never narrower than what it holds and `does this overflow?` is a
   question that always answers no, however narrow the window gets. Stretched, the
   box IS the track, the buttons overflow it, and core/toolbarOverflow.ts can see
   them do it. `justify-content: center` then keeps the look the other property
   was there for.

   `min-width: 0`, without which the track's own minimum is the content's
   min-content width and the track never shrinks in the first place. */
/* Every selector in this block is prefixed with `.section-page-card`, and that is not
   decoration: a section page's own view re-links section.css in its `Head` section
   (Views/Core/Section/_Default.cshtml and four more, plus whatever a host app does), so a
   SECOND copy of section.css lands after this file in the cascade. Anything here that
   overrides one of its rules at equal specificity therefore loses on those pages and only
   those — the worst kind of bug to find, because it works everywhere you first look. The
   extra class is what makes these win on specificity instead of on position. */
.section-page-card .section-toolbar-center,
.section-page-card .section-full-edit-caption > .section-action-btn-group {
  min-width: 0;
  justify-self: stretch;
  justify-content: center;
}

/* The tracks, above the phone breakpoint (below it section.css gives this row two
   columns and a second line for the commands, and these selectors outrank it).

   Column 2 is the one that yields: its minimum is zero, so it gives up width
   before the other two — which is the point, since it is the only one with
   somewhere to put what does not fit. Column 3 is `auto`, not `minmax(0, 1fr)`:
   the page info and the enlarge button have nowhere to go, and a 1fr track whose
   floor is zero let them be crushed to nothing and drawn over the commands.
   Column 1 keeps a floor so the title truncates to an ellipsis rather than
   disappearing — but only when there IS a title (a section inside a surface, or
   the FK picker, hides its own and must not pay for the space). 8rem, not the
   4.5 it started at: measured on Currency, the smaller floor left "Curr…", which
   is an ellipsis that tells you nothing. One more command in the menu is the
   cheaper half of that trade. */
@media (min-width: 768px) {
  .section-page-card .section-toolbar-row {
    grid-template-columns: minmax(0, 1fr) minmax(0, auto) auto;
  }

  .section-page-card .section-toolbar-row:has(> .section-toolbar-title) {
    grid-template-columns: minmax(8rem, 1fr) minmax(0, auto) auto;
  }
}

.section-page-card .section-toolbar-center > .btn-group,
.section-page-card .section-toolbar-center > .section-action-btn-group {
  flex: 0 0 auto;
}

/* Bootstrap's .btn-group collapses the radii of the buttons it holds, which is
   right for a real group (the pagination arrows) and wrong for
   .section-action-btn-group, which is not a group of alternatives but the whole
   command set — and which the overflow pass empties from the right. */
.section-page-card .section-action-btn-group {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  flex-wrap: nowrap;
}

/* Deep enough to outrank bootstrap.css's own
   `.btn-group > .btn:not(:last-child):not(.dropdown-toggle)`, which squares off
   the inner corners. */
.section-page-card .section-action-btn-group > .btn,
.section-page-card .section-action-btn-group > .lf-split-btn-wrap > .btn-group > .btn:first-child {
  border-radius: var(--bs-btn-border-radius, 0.25rem);
}

.section-page-card .section-action-btn-group .lf-split-btn > .lf-split-btn-main {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}

.section-page-card .section-action-btn-group .lf-split-btn > .lf-split-btn-caret {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}

/* ── The overflow menu ────────────────────────────────────────────────────
   Same element in both toolbars: a trigger pinned at the trailing edge of the
   row and a panel under it holding the buttons that did not fit. The buttons
   are MOVED, not rebuilt — they keep their ids, their listeners and whatever
   enabled/pressed state the toolbar had already given them. */
.lf-tb-overflow {
  position: relative;
  flex: 0 0 auto;
  display: none;
}

.lf-tb-overflow.is-active {
  display: inline-flex;
}

.lf-tb-overflow-panel {
  position: absolute;
  top: calc(100% + 0.25rem);
  right: 0;
  z-index: 1080;
  display: none;
  flex-direction: column;
  align-items: stretch;
  gap: 0.15rem;
  min-width: 12rem;
  max-height: min(70vh, 28rem);
  overflow-y: auto;
  padding: 0.25rem;
  border: 1px solid var(--lf-theme-panel-border, #dee2e6);
  border-radius: 0.375rem;
  background: var(--lf-theme-panel-bg, #fff);
  box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.18);
}

.lf-tb-overflow.is-open .lf-tb-overflow-panel {
  display: flex;
}

/* Inside the panel a button is a menu row: full width, icon and label both
   shown whatever shape the toolbar itself is in — a column of bare squares
   would be a worse menu than the toolbar it came from. */
html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .lf-fe-btn,
html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .section-tb-btn,
html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .lf-tb-btn {
  width: 100%;
  min-width: 0;
  justify-content: flex-start;
  padding: 0 var(--lf-tb-btn-padding);
}

html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .lf-fe-label,
html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .lf-tb-label {
  display: inline;
}

html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .lf-fe-btn > i,
html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .lf-fe-btn > svg,
html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .section-tb-btn > i,
html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .section-tb-btn > svg,
html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .lf-tb-btn > i,
html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .lf-tb-btn > svg {
  display: inline-block;
}

/* A dropdown that came into the panel on the back of its button (a split button's
   menu, the view menu) stops floating and becomes the block under it. Floating
   would mean being clipped by the panel's own scrolling, which for Bulk update
   and Bulk delete would mean unreachable at exactly the width where the panel is
   the only way to reach them. */
.lf-tb-overflow-panel .lf-split-btn-wrap,
.lf-tb-overflow-panel .section-view-menu-wrap {
  display: flex;
  flex-direction: column;
  width: 100%;
}

.lf-tb-overflow-panel .section-view-menu {
  position: static;
  width: 100%;
  min-width: 0;
  margin-top: 0.15rem;
  box-shadow: none;
}

/* A group that moved as a whole (the pagination arrows) stays horizontal: they
   are one control spelled with four buttons, not four commands. */
.lf-tb-overflow-panel .lf-fe-group,
.lf-tb-overflow-panel .btn-group {
  display: inline-flex;
  width: 100%;
  justify-content: flex-start;
}

html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .lf-fe-group .lf-fe-btn,
html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .btn-group .btn {
  width: var(--lf-tb-btn-height);
  min-width: var(--lf-tb-btn-height);
  justify-content: center;
  padding: 0;
}

.lf-tb-overflow-panel .lf-split-btn {
  width: 100%;
}

/* A split button stays a split button in the menu: the command takes the row, the
   caret keeps its own width beside it. Spelled at the same weight as the rule that
   gives every other button in here the full width, which would otherwise take the
   caret with it. The `.btn-group .btn` rule above centres pagination squares — the
   main must left-align like every other overflow row (#234). */
html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .lf-split-btn > .lf-split-btn-main {
  flex: 1 1 auto;
  width: auto;
  min-width: 0;
  justify-content: flex-start;
  padding: 0 var(--lf-tb-btn-padding);
}

html[data-lf-toolbar-buttons] .lf-tb-overflow-panel .lf-split-btn > .lf-split-btn-caret {
  flex: 0 0 auto;
  width: var(--lf-tb-caret-width);
  min-width: var(--lf-tb-caret-width);
  padding: 0;
  justify-content: center;
}

/* ── Small screens ────────────────────────────────────────────────────────
   The overflow pass already keeps either toolbar to one row at any width, so
   the only thing left to do here is give it more to work with: a shorter
   button and less padding buy several more commands before the menu starts
   filling up. */
@media (max-width: 767.98px) {
  :root {
    --lf-tb-btn-height: 1.85rem;
    --lf-tb-btn-padding: 0.4rem;
    --lf-tb-btn-font: 0.75rem;
  }
}
