/*
 * EncodeLabel kit: the print panel (goes with assets/js/kit-print.js).
 *
 * The panel is the last step of every tool: how many, where to start, a
 * picture of the sheet, and the buttons. It lays itself out by the room it
 * is given, not by the size of the screen:
 *   wide (700px or more)   the choices on the left; the sheet, what the job
 *                          comes to and the buttons on the right, staying in view
 *   narrow                 the sheet on top, then the choices, then the buttons;
 *                          inside a pop-up the sheet is pinned to the top and
 *                          the buttons to the bottom
 * A tool page with the shell's own .tool layout puts the sheet and the
 * buttons in its own places instead (see the panel's "preview" and "actions").
 *
 * Buttons, switches, steppers, the desk and the paper are the shell's
 * (site.css). Colours come from the design tokens only, so the light and the
 * dark theme both work; the sheet itself is always black on white, because
 * that is what the printer makes.
 */

.kpr {
  container: kpr / inline-size;
  --kpr-bg: var(--paper);          /* what the pinned parts are painted with: the surface the panel sits on */
  --kpr-gutter: 0px;               /* the padding of a pop-up, so pinned parts can reach its edges */
  --kpr-gutter-top: 0px;
  --kpr-gutter-bottom: 0px;
  /* Wide layout: where the right-hand side sticks, and how tall it may be (the room under the site's header). */
  --kpr-top: calc(var(--header-h, 60px) + 12px);
  --kpr-room: calc(100dvh - var(--header-h, 60px) - 28px - var(--banner-space, 0px));
  min-width: 0;
}
.dialog .kpr, .card .kpr, .tool-panel .kpr { --kpr-bg: var(--card); }
/* In a pop-up: its padding, and the height of its scrolling part (the pop-up is at most 88dvh or 860px tall). */
.dialog-body .kpr { --kpr-gutter: 20px; --kpr-gutter-top: 18px; --kpr-gutter-bottom: 20px; --kpr-top: 0px; --kpr-room: calc(min(88dvh, 860px) - 108px); }
@media (max-width: 560px) {
  .dialog-body .kpr { --kpr-gutter: 16px; --kpr-gutter-top: 16px; --kpr-gutter-bottom: calc(18px + env(safe-area-inset-bottom)); }
}

/* ---- The layout ------------------------------------------------------------ */
.kpr-desk { height: clamp(150px, 30dvh, 280px); padding: 14px; }
.kpr-grid { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.kpr-side { display: contents; }
.kpr-preview { order: 1; min-width: 0; }
.kpr-facts { order: 2; min-width: 0; }
.kpr-controls { order: 3; min-width: 0; }
.kpr-actions { order: 4; min-width: 0; }

@container kpr (min-width: 700px) {
  .kpr-grid { display: grid; grid-template-columns: minmax(300px, 380px) minmax(0, 1fr); gap: 0 28px; align-items: start; }
  .kpr-controls { grid-column: 1; grid-row: 1; }
  /* The right-hand side stays in view and is never taller than the room it has: the sheet gives way first,
     then the list of things to check scrolls, and the buttons are always there. */
  .kpr-side {
    display: flex; flex-direction: column; gap: 12px; grid-column: 2; grid-row: 1; min-height: 0;
    position: sticky; top: var(--kpr-top); max-height: var(--kpr-room);
  }
  .kpr-side > .kpr-preview { display: flex; flex: 1 1 auto; flex-direction: column; min-height: 196px; }
  .kpr-side .kpr-desk { flex: 1 1 400px; height: auto; min-height: 150px; max-height: 480px; }
  .kpr-side > .kpr-facts { flex: 0 1 auto; min-height: 1.6rem; overflow-y: auto; overscroll-behavior: contain; }
  .kpr-side > .kpr-actions { flex: none; }
  .kpr-fold { display: none; }
}

/* Narrow, inside a pop-up: the sheet stays at the top and the buttons at the bottom while the choices scroll.
   (A pinned part is placed from the inside of the pop-up's padding, so it is moved out by that padding to sit
   flush against the pop-up's own edges.) */
@container kpr (max-width: 699.98px) {
  .dialog-body .kpr-preview {
    position: sticky; top: calc(-1 * var(--kpr-gutter-top)); z-index: 3;
    margin: calc(-1 * var(--kpr-gutter-top)) calc(-1 * var(--kpr-gutter)) 0;
    padding: 10px var(--kpr-gutter) 8px;
    border-bottom: 1px solid var(--line); background: var(--kpr-bg);
  }
  .dialog-body .kpr-desk { height: clamp(130px, 25dvh, 230px); }
  .dialog-body .kpr-actions {
    position: sticky; bottom: calc(-1 * var(--kpr-gutter-bottom)); z-index: 3;
    margin: 0 calc(-1 * var(--kpr-gutter)) calc(-1 * var(--kpr-gutter-bottom));
    padding: 10px var(--kpr-gutter) calc(10px + env(safe-area-inset-bottom));
    border: 0; border-top: 1px solid var(--line); border-radius: 0; background: var(--kpr-bg);
    box-shadow: 0 -8px 24px color-mix(in srgb, var(--sheet-ink) 8%, transparent);
  }
  /* Two buttons share the bar; neither breaks its words in two (Print takes what "Download PDF" leaves). */
  .dialog-body .kpr-actions-buttons { flex-wrap: nowrap; }
  .dialog-body .kpr-actions-buttons .btn { flex: 1 1 auto; padding-left: 12px; padding-right: 12px; white-space: nowrap; }
  .kpr-actions-buttons .btn { flex: 1 1 auto; }
}

/* ---- The sheet ------------------------------------------------------------- */
.kpr-desk.is-empty .sheet-paper { opacity: .45; }
.kpr-desk.is-drawing .sheet-paper { opacity: .7; }
.kpr-preview-bar { display: flex; align-items: center; justify-content: center; gap: 8px; min-height: 36px; margin-top: 8px; }
.kpr-pager { display: flex; flex: 1 1 auto; align-items: center; justify-content: center; gap: 8px; min-width: 0; }
/* The shell pins .sheet-note to the desk; here it sits between the two arrows. */
.kpr-pager .kpr-page-note {
  position: static; transform: none; padding: 0; border-radius: 0; background: none; box-shadow: none;
  color: var(--muted); font-size: .9rem; font-variant-numeric: tabular-nums; text-align: center; min-width: 8.5em;
}
.kpr-fold { flex: none; }
.kpr-preview.is-folded .kpr-preview-bar { margin-top: 0; }

/* In a tool page's own preview column the bar and the line under it take no more room than they need. */
.tool-preview > .kpr-preview-bar { flex: none; }
.tool-preview > .kpr-result { flex: none; margin: 4px 0 0; text-align: center; }
@media (min-width: 761px) { .tool-preview > .kpr-preview-bar .kpr-fold { display: none; } }

/* ---- What the job comes to -------------------------------------------------- */
.kpr-result { margin: 0; font-size: 1.05rem; font-weight: 650; line-height: 1.4; }
.kpr-result .link-button { margin-left: .4em; font-weight: 500; }
.kpr-result.is-empty { color: var(--muted); font-weight: 500; }
.kpr-note { margin: 6px 0 0; color: var(--warn); font-size: .9rem; }
.kpr-skipped { margin: 8px 0 0; max-height: 180px; padding: 8px 12px; border: 1px solid var(--line); border-radius: var(--radius-small); overflow-y: auto; overscroll-behavior: contain; }
.kpr-skipped-list { margin: 0; padding: 0; list-style: none; font-size: .9rem; }
.kpr-skipped-list li { padding: 3px 0; overflow-wrap: anywhere; }

.kpr-warnings:empty { display: none; }
.kpr-warnings { margin: 10px 0 0; }
.kpr-warning-list { display: grid; gap: 6px; margin: 0; padding: 0; list-style: none; }
.kpr-warning {
  display: flex; align-items: flex-start; gap: 10px; padding: 8px 12px;
  border-left: 4px solid var(--warn); border-radius: var(--radius-small); background: var(--warn-soft);
}
.kpr-warning.is-error { border-left-color: var(--error); background: var(--error-soft); }
.kpr-warning.is-info { border-left-color: var(--link); background: var(--soft); }
.kpr-warning-icon { flex: none; color: var(--warn); line-height: 1.4; }
.kpr-warning.is-error .kpr-warning-icon { color: var(--error); }
.kpr-warning.is-info .kpr-warning-icon { color: var(--link); }
.kpr-warning-words { min-width: 0; }
.kpr-warning-text { margin: 0; font-size: .92rem; font-weight: 600; line-height: 1.4; overflow-wrap: anywhere; }
.kpr-warning.is-info .kpr-warning-text { font-weight: 500; }
.kpr-warning-detail { margin: 2px 0 0; color: var(--muted); font-size: .86rem; line-height: 1.4; overflow-wrap: anywhere; }
.kpr-warnings > .kpr-hint { margin-top: 6px; }

/* ---- The choices -------------------------------------------------------------- */
.kpr-section { margin: 0; padding: 16px 0; border-top: 1px solid var(--line); }
.kpr-controls > .kpr-section:first-child, .kpr-controls > .kpr-facts + .kpr-section { padding-top: 0; border-top: 0; }
.kpr-controls > .kpr-section:last-child { padding-bottom: 0; }
.kpr-controls > .kpr-facts { margin-bottom: 14px; }
.kpr-controls > .kpr-facts:not(:has(:not([hidden]):not(:empty))) { display: none; }
.kpr-title { margin: 0 0 10px; font: 700 1rem/1.25 var(--display); letter-spacing: -.01em; }
.kpr-hint { margin: 0 0 10px; }
.kpr-title + .kpr-hint { margin-top: -4px; }

/* One way of counting: a row to tick, and under the chosen one its number. */
.kpr-how { display: grid; gap: 8px; }
.kpr-choice { padding: 0 12px; border: 1px solid var(--line); border-radius: var(--radius-small); background: var(--card); }
.kpr-choice.is-on { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }
.kpr-choice .check { font-weight: 550; }
.kpr-choice-more { padding: 0 0 12px 30px; }
.kpr-choice-more .kpr-hint { margin: 8px 0 0; }
.kpr-count { display: flex; align-items: center; gap: 10px; }
.kpr-count-unit, .kpr-range-word, .kpr-picture-at { color: var(--muted); font-size: .92rem; }
.kpr-count .stepper > input { width: 4.6em; }

/* Where to start: the sheet, small, with every place a button. */
.kpr-start { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 14px 18px; }
.kpr-sheet {
  --ratio: .7727;
  position: relative; flex: none; width: min(100%, 190px); aspect-ratio: var(--ratio);
  border-radius: 2px; background: var(--sheet); color-scheme: light;
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--sheet-ink) 22%, transparent), 0 2px 8px color-mix(in srgb, var(--sheet-ink) 14%, transparent);
}
.kpr-cell {
  position: absolute; margin: 0; padding: 0; min-width: 0; min-height: 0;
  border: 1px solid color-mix(in srgb, var(--sheet-ink) 42%, var(--sheet)); border-radius: 1.5px;
  background: var(--sheet); cursor: pointer; -webkit-tap-highlight-color: transparent;
}
.kpr-cell.is-round { border-radius: 50%; }
.kpr-cell:hover { border-color: var(--brand); box-shadow: inset 0 0 0 1px var(--brand); }
/* Places already used: hatched out, like labels peeled off the sheet. */
.kpr-cell.is-used {
  border-color: color-mix(in srgb, var(--sheet-ink) 18%, var(--sheet));
  background: repeating-linear-gradient(135deg, color-mix(in srgb, var(--sheet-ink) 16%, var(--sheet)) 0 1.5px, color-mix(in srgb, var(--sheet-ink) 5%, var(--sheet)) 1.5px 5px);
}
.kpr-cell.is-first { border-color: var(--brand); background: var(--brand); }
.kpr-cell:focus-visible { z-index: 1; outline: 3px solid var(--focus); outline-offset: 1px; }
.kpr-start-side { flex: 1 1 150px; min-width: 0; }
.kpr-words { margin: 8px 0 0; color: var(--muted); font-size: .9rem; line-height: 1.45; }
@container kpr (max-width: 699.98px) {
  .kpr-sheet { width: min(100%, 230px); }
}
/* Fingers: where the screen is touched the picture is as wide as it can be, so the places of a sheet with
   seven across are not specks (the stepper beside it, with its full-size buttons, does the same job). */
@media (pointer: coarse) {
  .kpr-sheet { width: min(100%, 340px); }
}

/* Options */
.kpr-switch { display: flex; }
.kpr-range { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px; margin: 2px 0 6px; }
.kpr-range .stepper > input { width: 3.6em; }

/* Line it up: folded away until it is needed, with the nudge in words beside its name. */
.kpr-line > summary { display: flex; align-items: center; gap: 10px; min-height: 44px; margin: -10px 0; list-style: none; cursor: pointer; }
.kpr-line > summary::-webkit-details-marker { display: none; }
.kpr-line > summary .kpr-title { margin: 0; }
.kpr-line > summary::after {
  content: ""; flex: none; width: 8px; height: 8px; margin: 0 4px 3px auto;
  border: solid var(--muted); border-width: 0 2px 2px 0; transform: rotate(45deg);
}
.kpr-line[open] > summary::after { margin-bottom: -3px; transform: rotate(225deg); }
.kpr-line[open] > summary { margin-bottom: 0; }
.kpr-nudge-pill { color: var(--muted); font-weight: 500; }
.kpr-nudge-pill.is-set { color: var(--ink); font-weight: 650; }
.kpr-line-body { padding-top: 4px; }
.kpr-nudges { display: grid; gap: 8px; }
.kpr-nudge { display: grid; grid-template-columns: 44px minmax(0, 1fr) 44px; align-items: center; max-width: 300px; }
.kpr-nudge .btn { border-radius: var(--radius-small) 0 0 var(--radius-small); }
.kpr-nudge .btn:last-child { border-radius: 0 var(--radius-small) var(--radius-small) 0; }
.kpr-nudge .btn:focus-visible { position: relative; z-index: 1; }
.kpr-nudge-value {
  display: grid; place-items: center; align-self: stretch; margin: 0 -1px; padding: 0 8px;
  border: 1px solid var(--field-line); background: var(--field);
  font-size: .95rem; font-variant-numeric: tabular-nums; text-align: center;
}
.kpr-nudge-summary { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; margin: 8px 0 0; }
.kpr-nudge-summary .kpr-words { margin: 0; }
.kpr-test { margin-top: 14px; }
.kpr-test .btn { margin-bottom: 8px; }
.kpr-test .kpr-hint { margin: 0 0 6px; }
.kpr-tips-link { margin: 10px 0 0; font-size: .92rem; }

/* One label as a picture */
.kpr-picture { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px; margin: 0 0 10px; }
.kpr-picture:last-child { margin-bottom: 0; }
.kpr-picture .btn { min-width: 92px; }

/* ---- The buttons --------------------------------------------------------------- */
.kpr-actions { padding: 12px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); box-shadow: var(--shadow); }
.kpr-actions-info { min-width: 0; }
.kpr-actions-info:not(:has(:not([hidden]):not(:empty))) { display: none; }
.kpr-meter:empty { display: none; }
.kpr-meter .usage-meter { margin: 0; }
.kpr-big { margin: 6px 0 0; color: var(--muted); font-size: .88rem; line-height: 1.45; }
.kpr-status { margin: 6px 0 0; align-items: flex-start; }
.kpr-status::before { align-self: flex-start; margin-top: .5em; }
.kpr-status:empty { display: none; }
.kpr-status > span { min-width: 0; overflow-wrap: anywhere; }
.kpr-actions-buttons { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
.kpr-actions-info:not([hidden]) + .kpr-actions-buttons { margin-top: 10px; }

/* Inside a tool page's own action bar the panel's box disappears and its parts become the bar's. */
.kpr-actions.kpr-actions-flat { display: contents; }
.kpr-actions-flat .kpr-actions-info { flex: 1 1 auto; display: block; }
.kpr-actions-flat .kpr-actions-buttons { display: contents; }
.kpr-actions-flat .kpr-actions-info + .kpr-actions-buttons { margin-top: 0; }
/* On a phone that bar is narrow. What it has to say (the labels left this month, what was just made, "the
   first 36 of 63") is said in full, on as many lines as it takes, never cut off with dots; and neither
   button breaks its words in two (Print takes the room "Download PDF" leaves). */
@media (max-width: 760px) {
  .tool-actions .kpr-actions-flat .kpr-actions-info { overflow: visible; text-overflow: clip; white-space: normal; }
  .tool-actions .kpr-actions-flat .btn { flex: 1 1 auto; padding-left: 12px; padding-right: 12px; white-space: nowrap; }
}

/* Words that are pressed (Reset, "see why", "Print settings to check"): room for a finger around them,
   without moving the lines they sit in. */
@media (pointer: coarse) {
  .kpr .link-button, .kpr-result .link-button, .kpr-status .link-button { margin-block: -11px; padding-block: 11px; }
}

/* ---- The pop-ups ---------------------------------------------------------------- */
.kpr-progress { padding: 4px 0 2px; }
.kpr-bar { height: 10px; border: 1px solid var(--field-line); border-radius: 3px; background: var(--field); overflow: hidden; }
.kpr-bar-fill { width: 0; height: 100%; background: var(--brand); }
.kpr-progress-text { margin: 10px 0 0; color: var(--muted); font-size: .95rem; font-variant-numeric: tabular-nums; }
.kpr-progress-actions { display: flex; justify-content: flex-end; margin-top: 14px; }

.kpr-tips-paper { margin: 0 0 10px; }
.kpr-tips-list { display: grid; gap: 8px; margin: 0; padding: 0 0 0 20px; line-height: 1.5; }

.kpr-ready > p { margin: 0 0 14px; }
.kpr-ready-actions { display: grid; gap: 8px; }
.kpr-ready-actions .btn { width: 100%; }

@media (prefers-reduced-motion: no-preference) {
  .kpr-bar-fill { transition: width .2s linear; }
  .kpr-desk .sheet-paper { transition: opacity .15s; }
}

/* Windows high-contrast mode drops backgrounds: say "chosen" and "used" with the system's own colours. */
@media (forced-colors: active) {
  .kpr-cell { forced-color-adjust: none; border-color: CanvasText; background: Canvas; }
  .kpr-cell.is-used { background: GrayText; }
  .kpr-cell.is-first { background: Highlight; border-color: Highlight; }
  .kpr-bar-fill { forced-color-adjust: none; background: Highlight; }
}
