/* tool.css — the shell shared by every page under /tools/<slug>/. Depends on tokens.css
 * and site.css, loaded in that order.
 *
 * A tool is its own small build, uploaded as one folder, so it cannot use the generator's
 * templates. This file is what makes a dozen independently written pages read as one site:
 * the same masthead, the same input fields, the same readout, the same edges.
 *
 * EVERY CLASS STARTS WITH tk- ("tool kit"). The /tools/ index already owns .tool and .tools
 * for its cards, and a tool page must not restyle those by accident — nor be restyled when
 * they change. See tools/README.md for the markup each block expects.
 */

.tk-page [hidden] { display: none !important; }

/* ============================================================ masthead
 *
 * Deliberately slimmer than the site header: logo, where you are, and the way back to the
 * index. No category bar — a tool is uploaded on its own schedule, and a copy of the
 * catalogue menu frozen into its HTML would be wrong the day a category is renamed. */

.tk-mast { background: var(--page-raised); border-bottom: var(--hair); }
.tk-mast__in { display: flex; align-items: center; gap: var(--s-5); min-height: var(--nav-h); }
.tk-mast .brand { flex: none; display: block; }
.tk-mast .logo { width: auto; height: 40px; }
.tk-mast__trail { margin: 0; min-width: 0; }
.tk-mast__all { margin-left: auto; font-size: var(--t-sm); font-weight: var(--w-medium);
                white-space: nowrap; }
@media (max-width: 560px) { .tk-mast__trail { display: none; } }

/* ============================================================ page head */

.tk { padding-top: var(--s-6); }
.tk-head { display: grid; gap: var(--s-2); margin-bottom: var(--s-6); }
.tk-head > * { margin: 0; }
.tk-head h1 { text-wrap: balance; }
.tk-tags { display: flex; flex-wrap: wrap; gap: var(--s-2); margin: var(--s-2) 0 0; padding: 0;
           list-style: none; }
.tk-tags li { font-size: var(--t-xs); color: var(--page-muted); background: var(--page-raised);
              border: var(--hair); border-radius: var(--r-sm); padding: 2px 8px; }

/* ============================================================ the working area
 *
 * Inputs on the left, results on the right, and the results column is the wider one —
 * the answer is what someone came for. One column under 900px, inputs first. */

.tk-app { display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 7fr);
          gap: var(--s-5); align-items: start; }
.tk-app--single { grid-template-columns: minmax(0, 1fr); }
@media (max-width: 900px) { .tk-app { grid-template-columns: minmax(0, 1fr); } }

.tk-panel { display: grid; gap: var(--s-4); align-content: start; min-width: 0;
            padding: var(--s-5); background: var(--page-raised); border: var(--hair);
            border-radius: var(--r-md); box-shadow: var(--lip); }
.tk-panel > * { margin: 0; }
.tk-out { display: grid; gap: var(--s-4); align-content: start; min-width: 0; }
.tk-out > * { margin: 0; }

.tk-panel__title,
.tk-group > legend { font-family: var(--font-display); font-size: var(--t-xs);
                     font-weight: var(--w-medium); letter-spacing: var(--ls-eyebrow);
                     text-transform: uppercase; color: var(--page-muted); margin: 0; padding: 0; }

/* A fieldset. The legend is floated because a legend will not take part in grid layout
 * otherwise, and the gap would not apply beneath it. */
.tk-group { display: grid; gap: var(--s-3); min-width: 0; margin: 0; padding: 0; border: 0; }
.tk-group > legend { float: left; width: 100%; margin-bottom: var(--s-3); }
.tk-group + .tk-group { border-top: var(--hair); padding-top: var(--s-4); }

.tk-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
          gap: var(--s-3); }

/* ============================================================ fields */

.tk-field { display: grid; gap: 6px; align-content: start; min-width: 0; }
.tk-label { font-size: var(--t-sm); font-weight: var(--w-medium); }
.tk-hint { font-size: var(--t-xs); color: var(--page-muted); margin: 0; }

/* An input with its unit fused to the right edge. The wrapper carries the border and the
 * focus ring, so the unit reads as part of the field rather than a label beside it. */
.tk-input { display: flex; align-items: stretch; min-width: 0; background: var(--media-bg);
            border: 1px solid var(--page-hair-cool); border-radius: var(--r-sm); }
.tk-input:focus-within { border-color: var(--brand); box-shadow: 0 0 0 1px var(--brand); }
.tk-input > input,
.tk-input > select,
.tk-input > textarea { flex: 1 1 auto; min-width: 0; margin: 0; padding: 8px 10px; border: 0;
                       background: transparent; color: var(--page-fg); font: inherit;
                       font-family: var(--font-mono); font-variant-numeric: tabular-nums;
                       outline: none; }
.tk-input > select { font-family: var(--font-sans); cursor: pointer; }
.tk-input > textarea { resize: vertical; min-height: 5.5em; }
.tk-input__u { flex: none; display: flex; align-items: center; padding: 0 10px;
               font-size: var(--t-sm); color: var(--page-muted); white-space: nowrap;
               background: var(--page-sunk); border-left: 1px solid var(--page-hair-cool);
               border-radius: 0 var(--r-sm) var(--r-sm) 0; }
.tk-input.is-bad,
.tk-input:has(> input:invalid) { border-color: var(--danger); }
.tk-err { font-size: var(--t-xs); color: var(--danger); margin: 0; }

/* A segmented choice built from radios. The inputs stay in the document for keyboard and
 * screen-reader use; only their painted box is removed. */
.tk-seg { position: relative; display: inline-flex; flex-wrap: wrap; max-width: 100%;
          width: fit-content; background: var(--media-bg);
          border: 1px solid var(--page-hair-cool); border-radius: var(--r-sm); overflow: hidden; }
.tk-seg input { position: absolute; width: 1px; height: 1px; margin: 0; opacity: 0;
                pointer-events: none; }
.tk-seg label { padding: 6px 12px; font-size: var(--t-sm); cursor: pointer; user-select: none;
                border-left: 1px solid var(--page-hair-cool); }
.tk-seg label:first-of-type { border-left: 0; }
.tk-seg input:checked + label { background: var(--brand); color: var(--brand-on); }
.tk-seg input:focus-visible + label { outline: 2px solid var(--brand); outline-offset: -4px; }

.tk-check { display: flex; align-items: center; gap: var(--s-2); font-size: var(--t-sm); }
.tk-check input { width: 16px; height: 16px; margin: 0; accent-color: var(--brand); }

/* ============================================================ readout
 *
 * The answer, set like the DRO on the machine: the dark slab, mono digits, units in a
 * quieter tone. One per tool is the point — a page with four slabs has no answer. */

.tk-readout { display: grid; gap: var(--s-4); padding: var(--s-5); color: var(--slab-fg);
              background: linear-gradient(160deg, var(--slab-bg-1), var(--slab-bg-2) 58%, var(--slab-bg-3));
              border: 1px solid var(--slab-edge); border-radius: var(--r-md); }
.tk-readout > * { margin: 0; }
.tk-readout__main { display: grid; gap: var(--s-1); min-width: 0; }
.tk-readout__k { font-family: var(--font-display); font-size: var(--t-xs);
                 font-weight: var(--w-medium); letter-spacing: var(--ls-eyebrow);
                 text-transform: uppercase; color: var(--slab-muted); }
.tk-readout__v { font-family: var(--font-mono); font-variant-numeric: tabular-nums;
                 font-size: clamp(2rem, 5vw, 3rem); line-height: 1.05; letter-spacing: -0.02em;
                 overflow-wrap: anywhere; }
.tk-readout__u { margin-left: 0.35em; font-size: var(--t-md); letter-spacing: 0;
                 color: var(--slab-muted); }
.tk-readout__grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
                    gap: var(--s-4); margin: 0; padding-top: var(--s-4);
                    border-top: 1px solid var(--slab-hair); }
.tk-readout__grid > div { display: grid; gap: 2px; min-width: 0; }
.tk-readout__grid dt { font-size: var(--t-xs); color: var(--slab-muted); }
.tk-readout__grid dd { margin: 0; font-family: var(--font-mono); font-variant-numeric: tabular-nums;
                       font-size: var(--t-lg); overflow-wrap: anywhere; }
.tk-readout a { color: var(--slab-hi); }
.tk-readout :focus-visible { outline-color: var(--slab-hi); }

/* ============================================================ status */

.tk-flag { display: inline-flex; align-items: center; gap: 6px; width: fit-content;
           padding: 2px 8px; font-size: var(--t-xs); font-weight: var(--w-medium);
           border: 1px solid currentColor; border-radius: var(--r-sm); }
.tk-flag::before { content: ""; width: 6px; height: 6px; border-radius: 50%;
                   background: currentColor; }
.tk-flag--ok   { color: var(--ok); }
.tk-flag--warn { color: var(--warn); }
.tk-flag--bad  { color: var(--danger); }
/* The page's semantic inks fail on the slab; these are the slab's own. */
.tk-readout .tk-flag--ok   { color: var(--led); }
.tk-readout .tk-flag--warn { color: var(--axis-a); }
.tk-readout .tk-flag--bad  { color: var(--slab-accent); }

.tk-msg { display: grid; gap: 2px; padding: var(--s-3) var(--s-4); font-size: var(--t-sm);
          background: var(--page-raised); border: var(--hair); border-radius: var(--r-md); }
.tk-msg > * { margin: 0; }
.tk-msg > b { font-weight: var(--w-medium); }
.tk-msg--ok   > b { color: var(--ok); }
.tk-msg--warn { border-color: var(--warn); }
.tk-msg--warn > b { color: var(--warn); }
.tk-msg--bad  { border-color: var(--danger); }
.tk-msg--bad  > b { color: var(--danger); }

/* ============================================================ tables */

.tk-scroll { overflow-x: auto; background: var(--page-raised); border: var(--hair);
             border-radius: var(--r-md); }
.tk-table { width: 100%; border-collapse: collapse; font-size: var(--t-sm); }
.tk-table caption { text-align: left; padding: var(--s-3) var(--s-4); font-weight: var(--w-medium); }
.tk-table th,
.tk-table td { padding: 8px var(--s-3); text-align: left; vertical-align: top; }
.tk-table thead th { background: var(--page-sunk); color: var(--page-muted); white-space: nowrap;
                     font-size: var(--t-xs); font-weight: var(--w-medium);
                     letter-spacing: 0.04em; text-transform: uppercase; }
.tk-table tbody tr + tr > * { border-top: var(--hair); }
.tk-table tbody th { font-weight: var(--w-medium); }
.tk-table .tk-n { text-align: right; white-space: nowrap; font-family: var(--font-mono);
                  font-variant-numeric: tabular-nums; }
.tk-table .tk-c { white-space: nowrap; font-family: var(--font-mono); }
.tk-table tr.is-on > * { background: var(--brand-wash); }

.tk-page code,
.tk-page kbd { font-family: var(--font-mono); font-size: 0.92em; padding: 1px 5px;
               background: var(--page-sunk); border-radius: var(--r-sm); }

/* ============================================================ byte frames
 *
 * A serial frame drawn as its bytes, each underlined in the colour of its field. The four
 * colours are the controller's axis inks — the one palette on the site already known to
 * stay distinct from each other and from the accent. */

.tk-bytes { display: flex; flex-wrap: wrap; gap: 6px; margin: 0; padding: 0; list-style: none;
            font-family: var(--font-mono); }
.tk-byte { display: grid; justify-items: center; gap: 2px; min-width: 2.6em; padding: 6px 6px 4px;
           font-size: var(--t-base); background: var(--media-bg);
           border: 1px solid var(--page-hair-cool); border-bottom: 3px solid var(--page-muted);
           border-radius: var(--r-sm); }
.tk-byte small { font-family: var(--font-sans); font-size: 10px; letter-spacing: 0.04em;
                 color: var(--page-muted); }
.tk-byte--addr { border-bottom-color: var(--axis-x); }
.tk-byte--fn   { border-bottom-color: var(--axis-y); }
.tk-byte--data { border-bottom-color: var(--axis-z); }
.tk-byte--crc  { border-bottom-color: var(--axis-a); }

.tk-key { display: flex; flex-wrap: wrap; gap: var(--s-2) var(--s-4); margin: 0; padding: 0;
          list-style: none; font-size: var(--t-xs); color: var(--page-muted); }
.tk-key li::before { content: ""; display: inline-block; width: 12px; height: 3px;
                     margin-right: 6px; vertical-align: middle; background: var(--page-muted); }
.tk-key .tk-key--addr::before { background: var(--axis-x); }
.tk-key .tk-key--fn::before   { background: var(--axis-y); }
.tk-key .tk-key--data::before { background: var(--axis-z); }
.tk-key .tk-key--crc::before  { background: var(--axis-a); }

/* ============================================================ charts
 *
 * SVG built by the tool's script. Colours come from these classes, never from attributes
 * holding hex values, so a chart follows the tokens like everything else. */

.tk-chart { padding: var(--s-3); background: var(--media-bg); border: var(--hair);
            border-radius: var(--r-md); }
.tk-chart svg { width: 100%; height: auto; overflow: visible; }
.tk-chart text { fill: var(--page-muted); font-family: var(--font-sans); font-size: 11px; }
.tk-chart__grid  { fill: none; stroke: var(--page-hair); stroke-width: 1; }
.tk-chart__axis  { fill: none; stroke: var(--page-muted); stroke-width: 1; }
.tk-chart__line  { fill: none; stroke: var(--brand); stroke-width: 2; stroke-linejoin: round; }
.tk-chart__line--alt  { stroke: var(--brand-2); }
.tk-chart__line--dash { stroke-dasharray: 5 4; }
.tk-chart__area  { fill: var(--brand-wash); stroke: none; }
.tk-chart__mark  { fill: var(--brand); stroke: var(--media-bg); stroke-width: 2; }
.tk-chart__limit { fill: none; stroke: var(--danger); stroke-width: 1; stroke-dasharray: 3 3; }

/* ============================================================ actions and filters */

.tk-actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-2); }
.tk-actions .btn.is-done { color: var(--ok); }

/* The search-and-narrow bar on a reference tool. Sticky, so the filter stays in reach
 * while scrolling a long table. */
.tk-filter { position: sticky; top: 0; z-index: 2; display: flex; flex-wrap: wrap;
             align-items: center; gap: var(--s-3); padding-block: var(--s-3);
             background: var(--page-bg); border-bottom: var(--hair); }
.tk-filter .tk-input { flex: 1 1 260px; }
.tk-count { margin: 0 0 0 auto; font-size: var(--t-sm); color: var(--page-muted); }

/* ============================================================ the explanation
 *
 * How it works, the formula, a worked example. EVERY CHILD SHARES THE CONTAINER'S EDGES:
 * no narrower prose column beside a wider table. That mismatch is the fault the G-code
 * reference had as a CMS page, and one width everywhere is the site-wide call (see the
 * .wrap-narrow note in site.css). */

.tk-doc { display: grid; gap: var(--s-4); margin-top: var(--s-8); padding-top: var(--s-7);
          border-top: var(--hair); }
.tk-doc > * { margin: 0; max-width: none; }
.tk-doc h2 { margin-top: var(--s-5); }
.tk-doc > h2:first-child { margin-top: 0; }
.tk-doc h3 { margin-top: var(--s-3); }
.tk-doc ul,
.tk-doc ol { display: grid; gap: var(--s-1); padding-left: 1.25em; }
.tk-formula { margin: 0; padding: var(--s-3) var(--s-4); overflow-x: auto; white-space: pre;
              font-family: var(--font-mono); font-size: var(--t-sm);
              background: var(--page-sunk); border-radius: var(--r-md); }

/* ============================================================ footer */

.tk-foot { margin-top: var(--s-9); font-size: var(--t-sm); color: var(--page-muted);
           background: var(--page-sunk); border-top: var(--hair); }
.tk-foot__in { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-2) var(--s-5);
               padding-block: var(--s-5); }
.tk-foot__in > :first-child { margin-right: auto; }
.tk-foot a { color: inherit; }
.tk-foot a:hover { color: var(--page-fg); }

/* ============================================================ motion and print */

@media (prefers-reduced-motion: reduce) {
  .tk-page *, .tk-page *::before, .tk-page *::after {
    transition: none !important;
    animation: none !important;
  }
}

/* A calculation is often printed and taped to the machine. Drop the chrome, keep the
 * inputs and the answer side by side, and give the slab ink that survives a mono laser. */
@media print {
  .tk-mast, .tk-foot, .tk-filter, .tk-actions, .skip-link { display: none !important; }
  .tk-app { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .tk-readout { background: none; color: #000; border-color: #000; }
  .tk-readout__k, .tk-readout__u, .tk-readout__grid dt { color: #444; }
  .tk-doc { margin-top: var(--s-6); }
}
