/* =========================================
   字幕アトリエ — Languages tab

   js/ui-languages.js: the per-line matrix, the display settings beside it,
   the offline pack and the translation engine.

   Split out of styles.css. Everything stays inside @layer components:
   styles.css declares the layer order and must load first, and a rule
   left outside a layer would outrank every layered rule in the app.
   ========================================= */

@layer components {
  .style-align-control {
    justify-self: start;
  }

  /* Lives inside .help-popover now, which sets the colour, size and leading —
     dimming it there would only make a bubble the user opened on purpose harder
     to read. */
  .offline-pack-info { margin: 0; }

  /* Beside the engine picker (see ui-languages.js). Takes what the engine row
     leaves beside the advanced button. */
  .engine-inline {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-width: 0;

    &:not(:has(> :not([hidden]))) { display: none; }

    /* The status line gives way, a button beside it does not: squeezed, the
       button's label broke onto a second line inside the pill. */
    > .manual-status { flex: 1 1 auto; min-width: 0; }
    > .btn { flex-shrink: 0; white-space: nowrap; }

    /* PLaMo's size picker: "2B" / "0.8B" need none of .select-compact's
       150px. */
    > .plamo-model-select { flex-shrink: 0; width: auto; min-width: 0; }

    /* The license's "Built with PLaMo" notice, linked to the license. Quiet
       like a footnote, but kept on one line: it is a required notice. */
    > .plamo-credit {
      flex-shrink: 0;
      white-space: nowrap;
      font-size: 0.8em;
      color: var(--color-text-dim);
      &:hover { color: var(--color-accent); }
    }
  }

  /* A steady engine state as a chip (js/ui-languages.js, engineStatusLine):
     a dot for the tone, a word or two, and the info glyph that says there is
     more. The same quiet material as the other row-level chips; the dot is the
     only colour, so a caveat reads as one without shouting. */
  .engine-status-chip {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 10px;
    border-radius: var(--radius-full);
    background: var(--fill-1);
    color: var(--color-text-dim);
    font-size: var(--text-sm);
    white-space: nowrap;
    anchor-name: --engine-status-chip;
    transition: background 0.15s, color 0.15s, transform 0.15s var(--ease-out);

    &:hover { background: var(--fill-2); color: var(--color-text); }
    &:active { transform: scale(0.97); }

    .engine-status-dot {
      width: 7px;
      height: 7px;
      border-radius: 50%;
      background: var(--color-success);
    }
    &[data-tone="warn"]  .engine-status-dot { background: var(--color-warning); }
    &[data-tone="error"] .engine-status-dot { background: var(--color-danger); }
    &[data-tone="info"]  .engine-status-dot { background: var(--color-accent); }
    /* A failure is the one state worth more than a dot. */
    &[data-tone="error"] { color: var(--color-danger-text); }

    svg { opacity: 0.7; }
  }

  /* Its sentence, above the chip and growing rightward from its left edge, so
     it scales out of that corner. Tooltip timing: quick in, quicker out. Only
     one engine's chip is ever shown, so the three share one anchor name. */
  .engine-status-tip {
    position-anchor: --engine-status-chip;
    position-area: top span-right;
    position-try-fallbacks: flip-inline;
    margin: var(--space-2) 0;
    max-width: min(360px, 80vw);

    opacity: 0;
    transform: scale(0.97);
    transform-origin: bottom left;
    transition:
      opacity   100ms var(--ease-out),
      transform 100ms var(--ease-out),
      overlay   100ms allow-discrete,
      display   100ms allow-discrete;

    &:popover-open {
      opacity: 1;
      transform: none;
      transition-duration: 150ms;

      @starting-style {
        opacity: 0;
        transform: scale(0.97);
      }
    }
  }

  .offline-popover { position-anchor: --offline-help-anchor; }

  .offline-help-toggle { anchor-name: --offline-help-anchor; }
  .offline-pack-btn    { anchor-name: --offline-pack-anchor; }

  /* The one .help-popover the app opens by itself rather than on a click, so
     it is the one that needs to announce its own arrival: it fades in instead
     of blinking, and fades back out when it auto-clears. allow-discrete is what
     lets `display` and `overlay` participate, without which the exit frame is
     skipped and it would vanish instantly on the way out. */
  .offline-status-popover {
    position-anchor: --offline-pack-anchor;
    opacity: 0;
    transform: translateY(4px);
    transition:
      opacity 0.18s var(--ease-out),
      transform 0.18s var(--ease-out),
      overlay 0.18s allow-discrete,
      display 0.18s allow-discrete;

    &:popover-open {
      opacity: 1;
      transform: translateY(0);

      @starting-style {
        opacity: 0;
        transform: translateY(4px);
      }
    }

    /* A failure has to read as one — it stays until dismissed, so without this
       it is the same bubble as "ready", just with different words in it. */
    &.is-error {
      color: var(--color-danger-text);
      border-color: color-mix(in oklab, var(--color-danger) 45%, var(--color-border-strong));
    }
  }

  /* --- language controls: source side | translation side --- */
  /* One column, as wide as the matrix and set against the card's left edge
     like the panel's other tabs, so the rows under it (presets, offline pack)
     start at the matrix's left edge and end at its right one. Those rows are
     contained so they never widen it; on a narrow window it shrinks to the
     card and the matrix scrolls inside itself.
     No gap here: each row under the matrix owns its separator's spacing.
     Having both meant a gap, a margin and a padding all stacking into 36px of
     dead space in a panel that is short on height. */
  .lang-grid {
    display: grid;
    grid-template-columns: minmax(0, max-content);
    justify-content: start;
    align-content: start;
    height: 100%;
    min-height: 0;
  }

  /* One row per subtitle line: label, language, then its four appearance
     controls. Rows are display:contents so every cell joins this grid and the
     columns line up down the table. The two slider tracks are fixed lengths;
     everything else is sized by its control. */
  /* The matrix's row. The source-line settings that used to share it moved
     into a menu on the presets row (.sub-source-menu). When the window
     narrows, the matrix scrolls inside itself. */
  .lang-top {
    display: flex;
    gap: var(--space-4);
    min-width: 0;
  }

  /* The source-line menu (js/ui-languages.js): the themes button's twin
     beside it, and the themes menu's dress, position and timing. Two groups
     inside — the source line's settings, then the whole-display pair under a
     hairline. Bounded so the symbol boxes and the corner buttons share one
     comfortable width instead of stretching to their intrinsic size. */
  .sub-source-btn {
    flex-shrink: 0;
    anchor-name: --sub-source-btn;
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    height: var(--preset-chip-h);
    white-space: nowrap;

    &:has(+ .sub-source-menu:popover-open) {
      background: var(--color-accent-soft);
      color: var(--color-accent-hover);
    }
  }

  .sub-source-menu {
    position-anchor: --sub-source-btn;
    position-area: top span-right;
    position-try-fallbacks: flip-inline;
    inset: auto;
    margin: 0 0 var(--space-2);
    padding: var(--space-3);
    /* English's alignment/background row is the widest content (~352px);
       below this it wraps onto two lines. */
    width: 380px;
    display: none;
    flex-direction: column;
    gap: var(--space-2);
    background: var(--color-surface-popover);
    color: var(--color-text);
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-popover);

    opacity: 0;
    transform: scale(0.97);
    transform-origin: bottom left;
    transition:
      opacity   100ms var(--ease-out),
      transform 100ms var(--ease-out),
      overlay   100ms allow-discrete,
      display   100ms allow-discrete;

    &:popover-open {
      display: flex;
      opacity: 1;
      transform: none;
      transition-duration: 150ms;

      @starting-style {
        opacity: 0;
        transform: scale(0.97);
      }
    }
  }

  /* Source group: heading across the top, then label | control rows. The
     control track takes whatever the well's width leaves, so the picker and
     the symbol boxes end on the same edge as the display row below; 140px
     floor keeps the picker readable. */
  .lang-extras-source {
    display: grid;
    grid-template-columns: max-content minmax(140px, 1fr);
    column-gap: var(--space-3);
    row-gap: var(--space-1);
    align-items: center;
    > * { transition: opacity 150ms ease; }

    .seg-switch { width: 100%; }
    .seg-switch label { flex: 1; }
    .seg-switch label > span { width: 100%; text-align: center; }
    /* flex-basis, not `width: auto` — auto restores the browser's default input
       width (~20 characters), and two of those blow the block out, squeeze the
       matrix to its minimum and clip its last column behind its own overflow. */
    .symbol-inputs .text-input { flex: 1 1 3em; min-width: 0; }
  }

  /* The source line is hidden, so these settings currently do nothing. Dimmed
     rather than disabled: they stay editable for setting up in advance. The
     heading holds the switch that brings them back, so it stays lit. */
  /* The switch is the eye on the source row of the Appearance matrix now, so
     the menu looks for it from the root. */
  :root:has(#lang-show-source:not(:checked)) .lang-extras-source > :not(.lang-extras-heading) {
    opacity: 0.45;
  }

  /* Where the show-source switch went, under the group's title. */
  .lang-extras-hint {
    font-size: var(--text-xs);
    font-weight: 400;
    color: var(--color-text-faint);
  }

  /* The group's title with its show-source switch at the far end, like a
     settings group switched on from its own header. The whole row is the
     switch's label, so the title is a click target too. */
  .lang-extras-heading {
    grid-column: 1 / -1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding-bottom: var(--space-1);
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--color-text);
  }

  /* Whole-display pair on one row, each control at its natural width and the
     two pairs pushed to the row's ends. The hairline marks the change of
     scope from the source group above. */
  .lang-extras-display {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2) var(--space-3);
    padding-top: var(--space-2);
    border-top: 1px solid color-mix(in oklab, var(--color-border) 60%, transparent);
  }

  .lang-extras-pair {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
  }

  /* The line gap takes the display group's whole width, its slider the
     length of the matrix's. */
  .lang-extras-gap {
    flex-basis: 100%;
    justify-content: space-between;

    .slider-group { width: calc(100px + var(--space-2) + 28px); }
  }

  .lang-extras-label {
    color: var(--color-text-dim);
    font-size: var(--text-md);
    white-space: nowrap;
  }

  .lang-matrix {
    display: grid;
    /* Both slider tracks share one fixed length so they line up and don't
       stretch and shrink as the window is resized. The <output> beside each
       slider shows the exact value, so the track only needs to be comfortable
       to drag, not precise. A narrow window scrolls the matrix (overflow-x
       below) rather than shortening the tracks. The column holds the whole
       .slider-group, so its width is the 100px track plus the group's gap and
       28px readout. */
    grid-template-columns:
      max-content max-content max-content max-content
      calc(100px + var(--space-2) + 28px) calc(100px + var(--space-2) + 28px)
      calc(100px + var(--space-2) + 28px);
    column-gap: var(--space-4);
    row-gap: var(--space-2);
    align-items: center;
    justify-content: start;
    /* Rows stay packed at the top now that .lang-top stretches its children to
       a common height for the divider. */
    align-content: start;
    /* A 7-column table cannot collapse without losing the column comparison it
       exists for, so in a narrow window it scrolls inside itself rather than
       widening the card. */
    overflow-x: auto;
    /* Sideways only. overflow-x: auto alone turns the other axis to auto as
       well, and at 150% scaling the engine row's two-line status overhangs its
       grid row by a fraction of a pixel — enough for a vertical scrollbar,
       which then took its width out of the slider columns. */
    overflow-y: hidden;
    /* ...and hidden clips at the padding edge, so the last row's focus ring
       (3px, --focus-ring) needs room inside it or its bottom is cut off. The
       engine row's extra step above gives the height back. */
    padding-bottom: 4px;
  }

  .lang-matrix-head,
  .lang-matrix-row { display: contents; }

  /* A slider column's header with its link button beside the label. */
  .link-head {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
  }

  /* An icon toggle: a closed link while the translation lines follow the
     source line's value, a broken one while each has its own. The icon's
     shape is the state, so it is not lit: on is the default and stays on,
     and four accent spots always lit along the header drew the eye from the
     start button and the selections, which the accent is kept for. */
  .link-toggle {
    display: inline-grid;
    place-items: center;
    width: 20px;
    height: 20px;
    border-radius: var(--radius-sm);
    color: var(--color-text-dim);
    cursor: pointer;

    &:hover { background: var(--color-surface-2); color: var(--color-text); }
    &:has(input:focus-visible) { box-shadow: var(--focus-ring); }

    .link-off { display: none; }
    &:has(input:not(:checked)) {
      .link-on  { display: none; }
      .link-off { display: inline; }
    }
  }

  /* A translation line's slider while it follows the source line's: the
     track fades, plainly not its own to drag, but the number stays at full
     strength — it is the value the line is drawn with, not a dead control. */
  .lang-matrix .slider-group:has(input:disabled) input { opacity: 0.4; }

  .lang-matrix-head > span {
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--color-text-dim);
  }

  /* The width and rim columns hold one slider per style, and the headers
     name the plain outline or the cloud (js/subtitle-cloud.js); the root
     carries which (js/output.js). The outline cell holds two swatches in
     every style, outline (or cloud) and rim, without their hex readouts, so
     the column keeps about its usual width and the table is the same seven
     columns whichever style is on. */
  :root[data-sub-outline="stroke"] .outline-cloud-only,
  :root:not([data-sub-outline="stroke"]) .outline-stroke-only { display: none; }

  .outline-colors {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
  }

  .outline-colors .color-value { display: none; }

  /* --- colour presets: one row under the matrix and the display settings --- */

  /* A hairline above, like any second row of a panel: the presets act on
     every line at once, so they sit apart from the per-line rows. */
  .sub-preset-row {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    /* Tighter than the panel's other separators: this row is what the card's
       height was sized around, and the full step tipped the tab into a
       scrollbar. */
    margin-top: var(--space-2);
    padding-top: var(--space-2);
    border-top: 1px solid color-mix(in oklab, var(--color-border) 60%, transparent);
  }

  /* The themes pop-up: the look in use — its three lines' colours, the name
     of the set it matches (or was last, then "edited") — and the chevrons of
     a pop-up button. It shows a state, so it is not lit for a theme in use;
     only while its menu is open. */
  .sub-preset-themes {
    flex-shrink: 0;
    anchor-name: --sub-preset-themes;
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    height: var(--preset-chip-h);
    padding-inline: 10px 8px;
    white-space: nowrap;

    .sub-preset-modified {
      margin-left: calc(-1 * var(--space-1));
      font-size: var(--text-xs);
      color: var(--color-text-faint);
    }
    svg { opacity: 0.6; }

    &:has(+ .sub-preset-menu:popover-open) {
      background: var(--color-accent-soft);
      color: var(--color-accent-hover);
    }
  }

  /* Save to a slot: an outlined pill, quieter than the pop-up beside it.
     Lit (accent outline and label) while the look in use is saved nowhere. */
  .sub-preset-save-btn {
    flex-shrink: 0;
    anchor-name: --sub-preset-save;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    height: var(--preset-chip-h);
    padding-inline: 10px;
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-full);
    font-size: var(--text-sm);
    color: var(--color-text-dim);
    white-space: nowrap;
    transition: background 0.15s, color 0.15s, border-color 0.15s;

    &:hover { background: var(--fill-1); color: var(--color-text); }
    &.is-unsaved {
      color: var(--color-accent-hover);
      border-color: color-mix(in oklab, var(--color-accent) 60%, transparent);
    }
    &:has(+ .sub-preset-save-menu:popover-open) { background: var(--color-accent-soft); }
  }

  /* The three slots to save to, above the button like the themes menu. */
  .sub-preset-save-menu {
    position-anchor: --sub-preset-save;
    position-area: top span-right;
    position-try-fallbacks: flip-inline;
    inset: auto;
    margin: 0 0 var(--space-2);
    padding: var(--space-2);
    min-width: 240px;
    display: none;
    gap: 2px;
    background: var(--color-surface-popover);
    color: var(--color-text);
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-popover);

    &:popover-open { display: grid; }

    .sub-preset-head {
      padding: 2px 8px 4px;
      font-size: var(--text-xs);
      font-weight: 600;
      color: var(--color-text-dim);
    }
    .sub-preset-save-list { display: grid; gap: 2px; }
  }

  .sub-preset-save-item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: 6px 8px;
    border-radius: var(--radius-md);
    text-align: left;
    font-size: var(--text-md);

    &:hover, &:focus-visible { background: var(--fill-1); outline: none; }
    /* The slot saved to last, where a repeated save most likely goes. */
    &.is-last { background: var(--color-accent-soft); }
    .sub-preset-save-state {
      margin-left: auto;
      font-size: var(--text-xs);
      color: var(--color-text-faint);
    }
    /* An empty slot's dots: outlines only. */
    .sub-preset-dots i.is-blank {
      background: transparent;
      box-shadow: inset 0 0 0 1px var(--color-border-strong);
    }
  }

  /* After applying or saving: a line of text at the row's far end, with undo
     after an apply. Placed last so its appearing never moves the buttons. */
  .sub-preset-notice {
    order: 99;
    margin: 0 0 0 auto;
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--text-sm);
    color: var(--color-text-dim);
    white-space: nowrap;
  }

  /* Every built-in set in a grid, then the user's three slots, above the
     button — the panel has no room below. Opens out of the button's corner,
     with the toolbar popovers' timing. */
  .sub-preset-menu {
    position-anchor: --sub-preset-themes;
    position-area: top span-right;
    position-try-fallbacks: flip-inline;
    inset: auto;
    margin: 0 0 var(--space-2);
    padding: var(--space-3);
    display: none;
    gap: var(--space-2);
    background: var(--color-surface-popover);
    color: var(--color-text);
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-popover);

    opacity: 0;
    transform: scale(0.97);
    transform-origin: bottom left;
    transition:
      opacity   100ms var(--ease-out),
      transform 100ms var(--ease-out),
      overlay   100ms allow-discrete,
      display   100ms allow-discrete;

    &:popover-open {
      display: grid;
      opacity: 1;
      transform: none;
      transition-duration: 150ms;

      @starting-style {
        opacity: 0;
        transform: scale(0.97);
      }
    }

    /* The outline switch tops the menu, the rim width beside it. */
    .sub-outline-bar {
      display: flex;
      align-items: center;
      gap: var(--space-4);
      margin-bottom: var(--space-1);
    }

    .sub-preset-head {
      font-size: var(--text-xs);
      font-weight: 600;
      color: var(--color-text-dim);
    }

    /* The second heading starts a new group: room above it. */
    .sub-preset-head:not(:first-child) { margin-top: var(--space-2); }

    /* Three equal columns, so the slots below line up under the sets. */
    .sub-preset-grid,
    .sub-preset-mine {
      display: grid;
      grid-template-columns: repeat(3, minmax(max-content, 1fr));
      gap: var(--space-2);
    }

    .sub-preset-grid [data-preset] { justify-content: flex-start; }

    /* As many rows as the longest style's list (--preset-rows, set by
       js/subtitle-presets.js), whichever style is showing: the menu keeps one
       height as the outline switch changes the list, so the switch stays
       under the pointer, and マイテーマ stays at the bottom. */
    .sub-preset-grid {
      grid-template-rows: repeat(var(--preset-rows, 3), var(--preset-chip-h));
      align-content: start;
    }

  }

  /* A preset button, on the row and in the menu alike. It draws its name in
     the preset's own colours (--p-color and --p-stroke, set inline from
     js/subtitle-presets.js), so the buttons are their own preview. The outline
     is thinner than on stream — at this size the real widths would fill the
     letters in. */
  .sub-preset-row {
    --preset-chip-h: 28px;

    [data-preset] {
      flex-shrink: 0;
      display: inline-flex;
      align-items: center;
      gap: var(--space-2);
      height: var(--preset-chip-h);
      padding: 0 12px 0 10px;
      border-radius: var(--radius-full);
      background: var(--fill-1);
      white-space: nowrap;
      transition: background 0.15s, transform 0.15s var(--ease-out);

      &:hover:not(:disabled)  { background: var(--fill-2); }
      &:active:not(:disabled) { transform: scale(0.97); }

      &[aria-pressed="true"] {
        background: var(--color-accent-soft);
        box-shadow: inset 0 0 0 1px var(--color-accent);
      }

      /* An empty slot: nothing to apply until something is saved into it. */
      &.is-empty {
        background: transparent;
        outline: 1px dashed var(--color-border-strong);
        outline-offset: -1px;
        cursor: default;

        .sub-preset-name {
          color: var(--color-text-faint);
          -webkit-text-stroke: 0;
          font-family: inherit;
          font-size: var(--text-sm);
        }
      }
    }

    .sub-preset-name {
      font-family: var(--font-subtitle);
      font-size: var(--text-md);
      color: var(--p-color);
      -webkit-text-stroke: 3px var(--p-stroke);
      paint-order: stroke fill;
    }

    /* One dot per line: its text colour, ringed in its outline colour, then a
       hairline so a ring that matches the chip (white on the light theme)
       still has an edge. */
    .sub-preset-dots {
      display: flex;
      gap: 5px;

      i {
        width: 8px;
        height: 8px;
        border-radius: 50%;
        background: var(--d-color);
        box-shadow:
          0 0 0 2px var(--d-stroke),
          0 0 0 3px var(--color-border-strong);
      }
    }

    /* A cloud set: the name keeps the plain preview (cloud colour as its
       outline) — a rim drawn around it at this size fused the letters into
       an unreadable blob — and the rim shows as the dots' outer ring. A
       plain set with a rim (a saved slot) shows its rim the same way. */
    .is-cloud, .has-rim {
      .sub-preset-dots {
        gap: 7px;

        i {
          box-shadow:
            0 0 0 2px var(--d-stroke),
            0 0 0 3.5px var(--d-rim),
            0 0 0 4.5px var(--color-border-strong);
        }
      }
    }
  }

  /* A row's control cell. It holds only the select now that the show-source
     toggle moved to the header row; kept as a flex wrapper for any future
     inline extra. */
  .lang-control {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-width: 0;
  }

  .lang-field-label {
    color: var(--color-text-dim);
    font-size: var(--text-md);
    white-space: nowrap;
  }

  /* Offline pack row. Nothing here needs a width cap — the long removal
     instructions live in a popover. */
  .lang-sub { min-width: 0; }

  /* --- Languages tab: two grouped cards (languages | engine) ---
     The way grouped settings are laid out: a small heading above each card,
     rows of "name on the left, control on the right" divided by hairlines,
     the controls lining up down the right edge. Equal columns and equal card
     heights, so the two groups weigh the same. Both cards share one surface,
     a step lighter than the panel; colour is kept for state — the status dot,
     the red delete, the dimmed "none" — not for telling the groups apart. */
  .lang-layout {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    /* Three shared rows — headings, cards, footnotes — that each group
       takes as a subgrid, so the two cards are always one height: the
       engine card keeps the languages card's height when it has fewer rows
       (Google), its rows at the top, and the footnote under it never
       shortens it. */
    grid-template-rows: auto auto auto;
    column-gap: var(--space-4);
    align-content: start;
    max-width: 960px;
    position: relative;   /* anchors .lang-tools on the headings' line */
  }

  .lang-group {
    --group-surface: color-mix(in oklab, var(--color-surface-1) 94%, white);
    :root[data-theme="light"] & { --group-surface: var(--fill-3); }

    grid-row: span 3;
    display: grid;
    grid-template-rows: subgrid;
    min-width: 0;
  }

  /* The heading sits outside the card, with its group's glyph in the same
     dim colour. */
  .group-title {
    display: flex;
    align-items: center;
    gap: 6px;
    min-height: 26px;   /* room for the servers pill on the same line */
    margin: 0 0 6px 4px;
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--color-text-dim);

    svg { flex-shrink: 0; opacity: 0.85; }
  }

  .group-card {
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--group-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    overflow: hidden;
  }

  /* One row: name on the left, control on the right. A min-height rather
     than a height, so written-out progress can wrap without being cut. */
  .group-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    min-height: 40px;
    padding: 4px var(--space-3);
    min-width: 0;

    & + .group-row {
      border-top: 1px solid color-mix(in oklab, var(--color-border) 70%, transparent);
    }

    > .lang-field-label { flex-shrink: 0; }
  }

  /* The language pickers take the card's room rather than .select-compact's
     150px, which cut the longest names ("中国語（香港・書き言葉）"). One width
     for all three, so their edges line up down the card. */
  .lang-line-row select { width: min(19em, 62%); }

  /* "Not translated" reads as off: the picker's value dims (colour only —
     it is still the control that turns the line back on). */
  .lang-line-row select:has(option[value="none"]:checked) { color: var(--color-text-faint); }

  /* The offline pack row (experimental, usually absent): its heading and
     button already sit inline in .col-head. */
  .group-row.lang-sub > .col-head { flex: 1; }

  /* Engine group. The picker takes the width the full engine names need,
     which .select-compact's 150px cut off ("AI 翻訳（PLaMo…"). */
  .lang-engine .engine-select { flex: 0 1 auto; width: auto; min-width: 0; max-width: 70%; }

  /* Row 2: the state on the left, what the engine needs on the right. Gone
     when there is nothing in it (Google). A written-out status takes the
     room and wraps; a chip keeps its size. */
  .group-row.engine-inline {
    /* Not the old inline row's flex: 1 — in the card's column it made this
       row swallow the card's spare height, dropping the chip to the middle. */
    flex: none;
    justify-content: flex-start;
    flex-wrap: wrap;
    &:not(:has(> :not([hidden]):not(.help-popover))) { display: none; }

    > .manual-status:not(.visually-hidden) { flex: 1 1 12em; min-width: 0; margin: 0; }
    > .plamo-model-select,
    > .btn { margin-left: auto; }
    /* Only the first of the right-hand controls takes the push. */
    > .plamo-model-select ~ .btn { margin-left: 0; }
  }

  /* Row 3: the engine's settings, removing the model at the far end. Only a
     loaded PLaMo model has either, so the row is usually gone. */
  .group-row.engine-actions {
    justify-content: flex-start;
    flex-wrap: wrap;
    &:not(:has(> :not([hidden]):not(.help-popover))) { display: none; }
    > .btn { flex-shrink: 0; margin-left: 0; }
    > .text-btn { margin-left: auto; }
  }

  /* While the recognition server translates, there is no engine state. */
  .lang-engine[data-stt-translates="true"] .group-row.engine-inline { display: none; }

  /* Small print under the card. */
  .group-foot {
    margin: 4px 0 0 4px;
    font-size: var(--text-xs);
    color: var(--color-text-faint);
    &:not(:has(> :not([hidden]))) { display: none; }

    > .plamo-credit { color: inherit; &:hover { color: var(--color-accent); } }
  }

  /* A button that reads as a link: no fill, just its label. */
  .text-btn,
  .link-btn {
    padding: 2px 4px;
    border-radius: var(--radius-sm);
    font-size: var(--text-sm);
    color: var(--color-accent-hover);
    &:hover { text-decoration: underline; }
    &.danger { color: var(--color-danger-text); }
  }

  /* --- Appearance tab ---
     The theme row first, then the lines' matrix in a grouped card of the
     Languages tab's material. */
  /* As wide as the Languages tab's two cards together, so the card fills the
     panel instead of stopping at its content. */
  .appearance-layout {
    display: grid;
    gap: var(--space-3);
    align-content: start;
    max-width: 960px;
    min-width: 0;
  }

  /* The card's spare width is split evenly into two gaps, one after the line
     names and one between the two header groups, so the names, the colours
     and the sizes sit at an even rhythm along each row whatever the interface
     language makes the names' width. The sliders keep their length and the
     size group stays on the card's right edge, in line with the theme row
     above. Columns 2 and 6 are the gaps; the colour group is told to start at
     3 and the size group at 7. */
  .appearance-group .lang-matrix {
    grid-template-columns:
      max-content
      minmax(0, 1fr)
      max-content max-content max-content
      minmax(0, 1fr)
      calc(100px + var(--space-2) + 28px) calc(100px + var(--space-2) + 28px)
      calc(100px + var(--space-2) + 28px);
  }
  .appearance-group .lang-matrix {
    .lang-matrix-head > .matrix-group:not(.size-col) { grid-column: 3 / span 3; }
    .lang-matrix-head > [data-i18n="style.col.text"],
    .line-name + .color-cell { grid-column-start: 3; }
    .size-col { grid-column-start: 7; }
    .matrix-group.size-col { grid-column: 7 / span 3; }
    .line-unused-note { grid-column: 3 / -1; }
  }

  /* The source row's eye (subShowSource), and the same width kept empty on
     the translation rows so the three names line up. */
  .line-eye,
  .line-eye-spacer {
    flex-shrink: 0;
    width: 22px;
    height: 22px;
    align-self: center;
  }
  .line-eye {
    display: inline-grid;
    place-items: center;
    border-radius: var(--radius-sm);
    color: var(--color-accent-hover);
    cursor: pointer;

    &:hover { background: var(--fill-1); }
    &:has(input:focus-visible) { box-shadow: var(--focus-ring); }
    .eye-off { display: none; }
    &:has(input:not(:checked)) {
      color: var(--color-text-faint);
      .eye-on  { display: none; }
      .eye-off { display: block; }
    }
  }

  /* The source line hidden: its row dims but stays editable; the crossed-out
     eye says why. */
  .lang-matrix-row[data-line="source"]:has(#lang-show-source:not(:checked)) {
    > :not(.line-name) { opacity: 0.4; }
    .lang-field-label { color: var(--color-text-faint); }
  }

  /* On the card's heading line now: no separator above. */
  .appearance-head > .sub-preset-row { margin-top: 0; padding-top: 0; border-top: 0; }

  .appearance-group {
    --group-surface: color-mix(in oklab, var(--color-surface-1) 94%, white);
    :root[data-theme="light"] & { --group-surface: var(--fill-3); }

    display: grid;
    min-width: 0;

    > .group-card { padding: var(--space-2) var(--space-3) var(--space-1); }
    .lang-matrix { row-gap: 4px; }
  }

  /* The card's heading line: the title on the left, the theme row on the
     right — one line instead of two, so three lines fit the panel. */
  .appearance-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
    margin-bottom: 6px;

    > .group-title { margin: 0 0 0 4px; }
    > .sub-preset-row { flex-wrap: nowrap; }
  }

  /* The header's first tier: "colour" over the three swatch columns, "size
     and width" over the three sliders, each with a rule under it. */
  .lang-matrix-head > .matrix-group {
    grid-column: span 3;
    padding-bottom: 2px;
    border-bottom: 1px solid color-mix(in oklab, var(--color-border) 80%, transparent);
    text-align: center;
  }

  /* The swatches carry their colour; the hex is in the palette. */
  .lang-matrix .color-value { display: none; }

  /* A line's name only. Its language used to follow it, which ran this
     column wide enough in English (and close in Japanese) to push the slider
     columns out of the card; the language is on the Languages tab. */
  .line-name {
    display: flex;
    align-items: baseline;
    gap: var(--space-2);
    padding-right: var(--space-2);
    white-space: nowrap;

    > .lang-field-label { color: var(--color-text); }
  }

  /* A translation slider following the source line's (wireLinks): "= source"
     and the value, not a dimmed track. */
  .slider-follow { display: none; }
  .lang-matrix .slider-group:has(input:disabled) {
    > input { display: none; }
    > .slider-follow {
      display: inline;
      order: -1;   /* before the readout, though after it in the DOM */
      flex: 1;
      font-size: var(--text-sm);
      color: var(--color-text-faint);
      white-space: nowrap;
    }
  }

  /* A translation line set to "none" draws nothing: its controls give way to
     a note across the appearance columns, linking to the Languages tab. */
  .line-unused-note {
    display: none;
    grid-column: 2 / -1;
    margin: 0;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--text-sm);
    color: var(--color-text-faint, var(--color-text-dim));
  }

  .lang-matrix-row.is-unused {
    > :not(.line-name, .line-unused-note) { display: none; }
    > .line-unused-note { display: flex; }
    .line-name > * { color: var(--color-text-faint, var(--color-text-dim)); }
  }

  /* Both translation lines unused: the first row speaks for both ("翻訳 1・2"),
     the second goes. */
  .label-both { display: none; }
  .lang-matrix-row.is-unused-both {
    .label-one  { display: none; }
    .label-both { display: inline; }
  }
  /* :is() lifts this over .is-unused's note rule, which would show it. */
  .lang-matrix-row.is-merged > :is(*, .line-unused-note) { display: none; }

  /* --- custom URL format help (Languages tab) --- */
  .format-help {
    margin-top: var(--space-2);
    padding: var(--space-3);
    background: color-mix(in oklab, var(--color-bg) 60%, var(--color-surface-1));
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    font-size: var(--text-sm);
    line-height: 1.5;
    color: var(--color-text-dim);

    p {
      margin-top: var(--space-2);
    }

    ul.format-help-list {
      list-style: disc;
      padding-left: var(--space-4);
      margin-top: var(--space-1);

      li { margin-top: 2px; }
    }

    code {
      font-family: ui-monospace, "SF Mono", Consolas, "Liberation Mono", monospace;
      font-size: 0.85em;
      background: var(--color-bg);
      color: var(--color-text);
      padding: 1px 4px;
      border-radius: var(--radius-sm);
    }

    pre {
      margin-top: var(--space-1);
      background: var(--color-bg);
      border: 1px solid var(--color-border);
      border-radius: var(--radius-sm);
      padding: var(--space-2) var(--space-3);
      max-height: 220px;
      overflow-y: auto;
      overflow-x: hidden;

      code {
        background: none;
        padding: 0;
        color: var(--color-text);
        white-space: pre-wrap;
        overflow-wrap: anywhere;
        font-size: var(--text-sm);
        line-height: 1.45;
      }
    }
  }

  .format-help-examples {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: var(--space-2);
    margin-top: var(--space-3);

    /* Label takes a full line so the buttons sit below it in a single row. */
    > span {
      flex-basis: 100%;
      color: var(--color-text-dim);
      font-size: var(--text-sm);
    }

    .btn {
      padding: 3px 10px;
      font-size: var(--text-sm);
    }
  }

  /* Two small boxes for the characters wrapped around the recognised line.
     Not scoped to a tab — the control moved to the Languages tab. */
  .symbol-inputs {
    display: flex;
    gap: var(--space-2);

    .text-input {
      width: 3em;
      text-align: center;
      padding-inline: var(--space-2);
    }
  }

  /* Corner pictures (js/ui-deco.js): two buttons sharing the source group's
     control column, each showing its corner's picture at its angle, and lit
     while that corner is on or its menu is open. */
  .deco-buttons {
    display: flex;
    gap: var(--space-2);
  }

  .deco-btn {
    flex: 1 1 0;
    min-width: 0;
    padding-inline: var(--space-2);
    white-space: nowrap;

    .deco-btn-thumb {
      display: inline-grid;
      place-items: center;
      width: 16px;
      height: 16px;

      img {
        max-width: 16px;
        max-height: 16px;
        transform: rotate(var(--deco-angle, 0deg));
      }
      &:has(img[hidden]) { display: none; }
    }
    /* A picture chosen but switched off. */
    &:not(.is-on) .deco-btn-thumb img { opacity: 0.45; }

    &.is-on,
    &:has(+ .deco-menu:popover-open) {
      background: var(--color-accent-soft);
      color: var(--color-accent-hover);
    }
  }

  /* A corner's menu, above its button (the panel has no room below), in the
     themes menu's dress and timing. */
  .deco-menu {
    position-area: top center;
    position-try-fallbacks: flip-inline;
    inset: auto;
    margin: 0 0 var(--space-2);
    padding: var(--space-3);
    display: none;
    gap: var(--space-2);
    background: var(--color-surface-popover);
    color: var(--color-text);
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-popover);

    opacity: 0;
    transform: scale(0.97);
    transform-origin: bottom center;
    transition:
      opacity   100ms var(--ease-out),
      transform 100ms var(--ease-out),
      overlay   100ms allow-discrete,
      display   100ms allow-discrete;

    &:popover-open {
      display: grid;
      opacity: 1;
      transform: none;
      transition-duration: 150ms;

      @starting-style {
        opacity: 0;
        transform: scale(0.97);
      }
    }

    .toggle input:disabled + .toggle-track { opacity: 0.45; cursor: not-allowed; }
  }

  .deco-menu-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
  }

  .deco-menu-title {
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--color-text-dim);
  }

  .deco-menu-body {
    display: flex;
    align-items: center;
    gap: var(--space-3);
  }

  /* The picture at its angle, on a dashed well that reads as "drop / choose
     here". Sized so a square picture turned 45° still fits. */
  .deco-preview {
    flex-shrink: 0;
    display: grid;
    place-items: center;
    width: 88px;
    height: 88px;
    padding: var(--space-1);
    border: 1px dashed var(--color-border-strong);
    border-radius: var(--radius-md);
    background: var(--fill-1);
    color: var(--color-text-dim);
    font-size: var(--text-xs);
    cursor: pointer;
    overflow: hidden;
    transition: background 0.15s;

    &:hover { background: var(--fill-2); }

    img {
      max-width: 60px;
      max-height: 60px;
      transform: rotate(var(--deco-angle, 0deg));
    }
  }

  .deco-menu-controls {
    display: grid;
    grid-template-columns: max-content auto;
    column-gap: var(--space-2);
    row-gap: var(--space-1);
    align-items: center;
    font-size: var(--text-sm);
    color: var(--color-text-dim);

    /* Wider readout than the matrix's: angles go to "-180". */
    .slider-group {
      grid-template-columns: 1fr 36px;
      width: calc(110px + var(--space-2) + 36px);
    }
  }

  .deco-actions {
    grid-column: 1 / -1;
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    margin-top: var(--space-1);

    .btn { padding: 3px var(--space-3); }
  }

  .deco-note {
    margin: 0;
    max-width: 290px;
    font-size: var(--text-xs);
    color: var(--color-text-dim);

    &.is-error { color: var(--color-danger); }
  }

  /* Picker dimmed while the translation server overrides it: it names the
     server, and the note beside it says why. Still usable — picking an engine
     is how the server is turned off from here. */
  .lang-engine .engine-select.is-overridden { opacity: 0.55; }

  /* The recognition server translating: the stand-in picker, and nothing of
     the engine's beside it — its status, downloads and options are moot. */
  .lang-engine .engine-select:disabled { opacity: 0.55; cursor: not-allowed; }
  .lang-engine[data-stt-translates="true"] .engine-inline { display: none; }
  .advanced-section[inert] { opacity: 0.55; }

  /* --- advanced: servers of the user's own --- */

  /* On the group headings' line, at the tab's right edge (.lang-tools): a
     setting of the whole tab, since a recognition server replaces the
     recogniser as well as the engine. Fixed there, so it never moves with
     the engine's rows. */
  .lang-tools {
    position: absolute;
    top: 0;
    right: 0;
    /* The headings' line is made tall enough for the outlined pill
       (.group-title min-height), so its border is not clipped by the tab
       panel's top edge. */
    height: 26px;
    display: flex;
    align-items: center;
  }

  /* An outlined pill — a hairline, no fill: plainly a button, yet quieter
     than the cards' filled controls, since it is rarely needed. The label
     says what it is; the faint note after it says who it is for. */
  .advanced-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 2px 10px;
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-full);
    font-size: var(--text-sm);
    color: var(--color-text-dim);
    white-space: nowrap;
    anchor-name: --advanced-anchor;
    transition: background 0.15s, color 0.15s, border-color 0.15s;

    .advanced-note {
      font-size: var(--text-xs);
      color: var(--color-text-faint);
    }

    &:hover { background: var(--fill-1); color: var(--color-text); }
    &:has(+ .advanced-popover:popover-open) { background: var(--fill-1); color: var(--color-text); }

    /* Lit while a server is in use, so a setting tucked away in the popover
       cannot be forgotten about: the outline and label take the accent, and
       the recognition server's connection rides along as the dot. */
    &[data-active="true"] {
      color: var(--color-accent-hover);
      border-color: color-mix(in oklab, var(--color-accent) 60%, transparent);
    }
  }

  /* Opens upward from the button, over the preview: the panel has no room
     below it, and the popover is taller than the panel. */
  .advanced-popover {
    position-anchor: --advanced-anchor;
    position-area: top span-left;
    position-try-fallbacks: flip-inline;
    margin: var(--space-2) 0;
    width: min(400px, 90vw);
    max-width: none;
  }

  /* PLaMo engine options: for now only the glossary (the replacement tab's source →
     target rows), in a popover that opens upward over the preview like the
     advanced one beside it. Only the glossary list scrolls once it outgrows
     the popover. */
  .ai-options-btn { anchor-name: --ai-options-anchor; }

  .ai-options-popover {
    position-anchor: --ai-options-anchor;
    position-area: top span-right;
    position-try-fallbacks: flip-inline;
    margin: var(--space-2) 0;
    width: min(520px, 90vw);
    max-width: none;
    max-height: min(480px, 70vh);

    &:popover-open {
      display: flex;
      flex-direction: column;
    }

    .ai-glossary {
      flex: 1 1 auto;
      min-height: 0;
    }

    /* The replacement tab's lists fill a panel of fixed height, so they take
       flex-basis 0 (tab-filter.css). Here the height comes from the content,
       and basis 0 collapsed the list to nothing: rows were added but never
       seen. As specific as that rule, so the load order cannot bring it back. */
    .filter-list-wrap { flex: 1 1 auto; }
    .filter-list-wrap .filter-list { flex: 0 1 auto; }

    /* Source → target, then the language the term is for. The picker fills
       its column: .select-compact's fixed 150px ran past it and under the
       delete button. */
    .filter-row-lang { grid-template-columns: minmax(0, 1fr) 16px minmax(0, 1fr) 9.5em 28px; }
    .filter-lang {
      width: 100%;
      padding-block: 3px;
      font-size: var(--text-sm);
    }
  }

  .advanced-section {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);

    & + & {
      margin-top: var(--space-3);
      padding-top: var(--space-3);
      border-top: 1px solid var(--color-border);
    }

    .format-help-examples { margin-top: 0; }
  }

  .advanced-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
  }

  .advanced-title {
    margin: 0;
    font-size: var(--text-md);
    font-weight: 600;
  }

  .advanced-desc {
    margin: 0;
    color: var(--color-text-dim);
  }

  .advanced-option {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    font-size: var(--text-sm);
    cursor: pointer;
  }

  .advanced-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
  }

  .advanced-link {
    flex-shrink: 0;
    color: var(--color-accent-hover);
    font-size: var(--text-sm);
  }

  /* Recognition server connection: in the popover, and as a bare dot on the
     button. */
  .stt-status {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    min-width: 0;
    margin: 0;
    color: var(--color-text-dim);
    font-size: var(--text-sm);
    white-space: nowrap;
  }

  .stt-status-text {
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .stt-status-dot {
    flex: 0 0 auto;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--color-text-faint);

    &[data-phase="connecting"], .stt-status[data-phase="connecting"] & {
      background: var(--color-warning);
      animation: dot-pulse 1.2s ease-in-out infinite;
    }
    &[data-phase="connected"], &[data-phase="ready"],
    .stt-status[data-phase="connected"] &, .stt-status[data-phase="ready"] & {
      background: var(--color-success);
    }
    &[data-phase="retrying"], &[data-phase="failed"],
    .stt-status[data-phase="retrying"] &, .stt-status[data-phase="failed"] & {
      background: var(--color-danger);
    }
  }

  .stt-status[data-phase="retrying"],
  .stt-status[data-phase="failed"] { color: var(--color-danger-text); }
}
