/* =========================================
   字幕アトリエ — toolbar

   The toolbar along the bottom of the control card: start/stop, the
   microphone button and its panel (device, level test), the interface-
   language menu, the subtitle-window button and the background-click lock.
   js/speech.js, js/ui-mic.js, js/main.js, js/ui-obs.js.

   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 {
  /* --- start/stop (toolbar; js/speech.js) ---
     One button (index.html, #btn-speech). Both faces sit in the same grid
     cell so the button keeps the longer label's width; only the one for the
     current state is visible. Here in components rather than with the rest of
     the toolbar in layout, because it has to outrank .btn and .btn.primary. */
  .speech-btn {
    display: inline-grid;
    align-self: stretch;
    flex-shrink: 0;
    padding-inline: 16px;
    white-space: nowrap;

    /* The faces crossfade through a slight blur rather than swapping in one
       frame: the flip lands only once recognition has actually started, a
       beat after the press, and an instant swap that late reads as a glitch
       rather than as the answer. Blur makes the two labels read as one thing
       changing instead of two overlapping. visibility rides along so the
       hidden face still leaves the accessible name. */
    .speech-face {
      grid-area: 1 / 1;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      gap: var(--space-1);
      transition:
        opacity    150ms var(--ease-out),
        filter     150ms var(--ease-out),
        visibility 150ms;
    }
    .speech-face-stop { visibility: hidden; opacity: 0; filter: blur(2px); }

    /* Live: the stop face, on a pulsing red badge — the toolbar's one
       at-a-glance sign that recognition is running. */
    &[data-recording="true"] {
      background: color-mix(in oklab, var(--color-danger) 20%, transparent);
      color: color-mix(in oklab, var(--color-danger) 70%, white);

      /* Lightened toward white it is pale pink on a pink badge in the light
         theme; the theme's own danger text is dark enough to read there. */
      :root[data-theme="light"] & { color: var(--color-danger-text); }

      &:hover:not(:disabled) {
        background: color-mix(in oklab, var(--color-danger) 30%, transparent);
      }

      .speech-face-start { visibility: hidden;  opacity: 0; filter: blur(2px); }
      .speech-face-stop  { visibility: visible; opacity: 1; filter: none; }
      svg { animation: rec-pulse 1.4s ease-in-out infinite; }
    }
  }

  /* --- background-click lock (toolbar) ---
     Two glyphs in one button: an open shackle while a background click still
     collapses/restores the panel, a closed one once it doesn't. The .is-active
     tint alone would be too quiet for a state you check mid-stream, so the icon
     carries it. Both are in the same inline-grid cell, hence display, not
     opacity. --- */
  .lock-btn {
    .lock-icon-closed { display: none; }

    &.is-active {
      .lock-icon-open   { display: none; }
      .lock-icon-closed { display: block; }
    }
  }

  /* --- microphone button + panel (toolbar; ui-mic.js) --- */
  .toolbar-start {
    justify-self: start;
    display: flex;
    align-items: center;
    gap: var(--space-2);
  }

  /* "2" before start/stop, in the second window only (js/channel.js). */
  .ch-badge {
    display: none;
    place-items: center;
    min-width: 22px;
    height: 22px;
    padding-inline: 6px;
    border-radius: var(--radius-full);
    background: var(--color-accent-fill);
    color: #fff;
    font-size: var(--text-sm);
    font-weight: 700;
    line-height: 1;

    :root[data-ch="2"] & { display: inline-grid; }
  }

  .mic-btn {
    position: relative;
    anchor-name: --mic-btn;
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    white-space: nowrap;

    /* Something needs attention: the picked device is unplugged (warn), or
       the last level test found a problem (bad). */
    &[data-alert="warn"]::after,
    &[data-alert="bad"]::after {
      content: "";
      position: absolute;
      top: 0;
      right: 2px;
      width: 7px;
      height: 7px;
      border-radius: 50%;
      background: var(--color-warning);
      box-shadow: 0 0 0 2px var(--color-surface-1);
    }
    &[data-alert="bad"]::after { background: var(--color-danger); }

    &:has(+ .mic-panel:popover-open) {
      background: var(--color-accent-soft);
      color: var(--color-accent-hover);
    }
  }

  /* Opens upward: the toolbar sits at the bottom of the window. Grows
     leftward from the button's right edge, since the button is on the right
     half of the toolbar now. */
  .mic-panel {
    position-anchor: --mic-btn;
    position-area: top span-left;
    inset: auto;
    /* Clears the card's top edge, which sits a toolbar-padding above the button. */
    margin: 0 0 calc(var(--space-2) + var(--space-3));
    width: min(440px, calc(100vw - 2 * var(--space-4)));
    /* The area above the button is the containing block here, so 100% is all
       the room there is; a short window scrolls the panel instead of letting
       it spill down over the toolbar. */
    max-height: 100%;
    overflow-y: auto;
    padding: var(--space-4);
    display: none;
    gap: var(--space-4);
    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; }

    h3 {
      font-size: var(--text-md);
      font-weight: 600;
    }
    p {
      color: var(--color-text-dim);
      font-size: var(--text-sm);
      line-height: 1.45;
    }
  }

  .mic-panel-row {
    display: grid;
    grid-template-columns: auto 1fr;
    align-items: center;
    gap: var(--space-3);

    .mic-select { min-width: 0; text-overflow: ellipsis; }
  }

  .mic-segment,
  .mic-second {
    grid-template-columns: 1fr auto;
    padding-top: var(--space-3);
    border-top: 1px solid color-mix(in oklab, var(--color-border) 60%, transparent);
  }


  /* --- subtitle window button (toolbar; js/ui-obs.js) ---
     Matches the mic button's box. Lit while the window is open, in the same
     accent the mic button wears while its panel is up. */
  .subwin-btn {
    align-self: stretch;
    padding-inline: 14px;
    white-space: nowrap;

    &[aria-pressed="true"] {
      background: var(--color-accent-soft);
      color: var(--color-accent-hover);
    }
  }

  /* --- interface language (global actions; js/main.js) ---
     An .icon-btn that also names the current language, small and after the
     globe like a keyboard-input indicator, so the glyph stays the same size
     as its neighbours'. Its menu opens upward like the mic panel. */
  .lang-btn {
    anchor-name: --lang-btn;
    width: auto;
    padding-inline: 7px;
    grid-auto-flow: column;
    gap: 4px;

    .lang-btn-current {
      font-size: var(--text-xs);
      font-weight: 600;
      letter-spacing: 0.02em;
      white-space: nowrap;
    }

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

  .lang-menu {
    position-anchor: --lang-btn;
    position-area: top span-left;
    inset: auto;
    margin: 0 0 calc(var(--space-2) + var(--space-3));
    padding: var(--space-1);
    display: none;
    min-width: 150px;
    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; }

    button {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: var(--space-3);
      padding: 6px 10px;
      border-radius: var(--radius-md);
      font-size: var(--text-md);
      text-align: left;

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

      /* checkmark on the active language */
      &[aria-checked="true"]::after {
        content: "✓";
        color: var(--color-accent-hover);
      }
    }
  }

  /* Both toolbar popovers grow up and to the left from their button, so they
     scale out of its corner (bottom right) rather than from their own centre,
     the way the colour popover does. Opened a few times a session, which is
     the frequency that earns a short entrance; the exit is quicker, since by
     then the user has moved on. allow-discrete lets display and overlay wait
     for the fade, or the exit would be skipped and they would just vanish. */
  .mic-panel,
  .lang-menu {
    opacity: 0;
    transform: scale(0.97);
    transform-origin: bottom right;
    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);
      }
    }
  }

  /* --- source switch (first row of the mic panel; js/ui-mic.js) ---
     Microphone | tab audio. The tab half waits on the browser's share dialog
     while it is open. */
  .mic-source-switch button {
    font-size: var(--text-md);
    &:disabled { opacity: 0.45; cursor: progress; }
  }

  /* --- tab-share badge (toolbar; js/ui-mic.js) ---
     Stands in for the source button while the panel is collapsed and the
     button with it. A label, not a button: no fill of its own to read
     as pressable. [hidden] still wins (utilities) when nothing is shared. */
  .tab-share-badge {
    display: none;
    .panel-collapsed & { display: inline-flex; }
    align-items: center;
    gap: var(--space-1);
    padding: 3px 10px;
    border: 1px solid color-mix(in oklab, var(--color-accent) 45%, transparent);
    border-radius: var(--radius-full);
    color: var(--color-accent-hover);
    font-size: var(--text-sm);
    white-space: nowrap;
  }

  /* While a tab is shared (js/ui-mic.js): the microphone is not what
     recognition hears, so its row and the level test are greyed out. Their
     controls are disabled from JS; sentence splitting stays as it is. */
  .mic-panel[data-tab-shared="true"] :is(.mic-device, .mic-test) {
    opacity: 0.45;
  }

  /* --- level test (inside the mic panel) ---
     One block with three states: idle shows the button and a hint, running
     the prompt and a live bar, done the two measured bars and the advice. */
  .mic-test {
    display: grid;
    gap: var(--space-2);

    .mic-test-head {
      display: flex;
      align-items: center;
      flex-wrap: wrap;
      gap: var(--space-3);
    }
    .mic-test-step {
      color: var(--color-text);
      font-weight: 600;
    }

    &[data-state="idle"]    :is(.mic-test-step, .mic-test-live, .mic-test-result) { display: none; }
    &[data-state="running"] :is(.mic-test-hint, .mic-test-result)                 { display: none; }
    &[data-state="done"]    :is(.mic-test-step, .mic-test-live)                   { display: none; }

    .mic-test-result {
      display: grid;
      gap: var(--space-2);
      justify-items: start;
    }
    .mic-test-bars {
      display: grid;
      gap: var(--space-2);
      justify-self: stretch;
    }
    .mic-test-row {
      display: grid;
      grid-template-columns: 5em 1fr 4.5em;
      align-items: center;
      gap: var(--space-2);
      font-size: var(--text-sm);
    }
    .level-value {
      text-align: right;
      font-variant-numeric: tabular-nums;
      color: var(--color-text-dim);
    }
    /* Background above the line: that bar is the problem. */
    .mic-test-bg.is-over .level-fill {
      background: var(--color-danger);
    }
    .mic-test-verdict {
      &[data-tone="ok"]   { color: var(--color-success); }
      &[data-tone="warn"] { color: var(--color-warning); }
      &[data-tone="bad"]  { color: var(--color-danger-text); }
    }
  }

  .level-bar {
    position: relative;
    height: 8px;
    border-radius: 4px;
    background: var(--color-surface-2);

    .level-fill {
      width: 0;
      height: 100%;
      border-radius: inherit;
      background: var(--color-accent);
      transition: width 60ms linear;
    }
    /* The line the background has to stay under. */
    .level-mark {
      position: absolute;
      top: -3px;
      bottom: -3px;
      width: 2px;
      margin-left: -1px;
      background: var(--color-warning);
    }
  }
}
