/* =========================================
   字幕アトリエ — colour picker

   The swatch/hex triggers and the popover they open (js/color-picker.js).
   Used by the Languages tab today, but a component rather than part of it.

   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 {
  /* swatch + live hex readout, macOS-style — confirms "is this pure white?"
     without opening the palette (kept in sync by ui-bind.js). position:relative
     keeps the visually-hidden colour input from escaping to the viewport, where
     focusing it would scroll the page (same trap as the segmented-switch
     radios). */
  .color-cell {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);

    .color-value {
      font-family: ui-monospace, "SF Mono", Consolas, "Liberation Mono", monospace;
      font-size: var(--text-xs);
      color: var(--color-text-faint);
      letter-spacing: 0.02em;
    }
  }

  /* Colour trigger with no visible swatch — see the note in ui-languages.js for
     why the background colour must not paint itself; its hex label carries the
     value instead. Wrapper is position:relative for the same reason as
     .color-cell above. */
  .color-pick {
    position: relative;
    display: inline-flex;
    align-items: center;

    .color-value { color: var(--color-text); }
  }

  /* The trigger the palette opens from: the old <input type="color"> swatch.
     Narrowed from the native 64px to 40px: the two colour columns are in the
     matrix that shares .lang-top with .lang-extras, and the 48px they give
     back is what lets the English extras block fit beside it. The hex
     readout beside each swatch carries the exact value, so the swatch only
     needs to show the colour. Painted from --selected-color, which
     js/color-picker.js writes on every change. */
  .color-swatch {
    --selected-color: #FFFFFF;
    width: 40px;
    height: 28px;
    padding: 3px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    background: var(--color-surface-2);
    transition: border-color 0.15s;

    &:hover { border-color: var(--color-border-strong); }

    /* Inner fill rather than a background on the button itself: it keeps the
       3px inset the native swatch had, so a white value still reads as a swatch
       against the surface instead of merging with the panel. */
    &::before {
      content: "";
      display: block;
      width: 100%;
      height: 100%;
      border-radius: var(--radius-sm);
      background: var(--selected-color);
    }
  }

  /* ============ in-page colour palette (js/color-picker.js) ============ */

  /* Fixed and positioned from JS — it opens out of a card pinned to the bottom
     of the window and has to flip above its trigger when there is no room
     below. z-index clears the panel and the manual-translate drawer; it never
     opens from inside a <dialog>, which would out-rank it via the top layer. */
  .color-popover {
    position: fixed;
    z-index: 900;
    display: none;
    width: 268px;
    padding: var(--space-3);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    background: var(--color-surface-popover);
    box-shadow: var(--shadow-overlay);
    /* transform-origin is set by positionPopover() to the trigger's centre */
    transition: opacity 0.15s var(--ease-out), transform 0.15s var(--ease-out);

    &.is-open {
      display: block;

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

  .color-popover-head {
    margin-bottom: var(--space-2);
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--color-text);
  }

  /* Saturation across, value down, over the current hue — the standard square.
     Two gradients stacked on the hue fill: white to transparent left to right,
     black to transparent bottom to top. */
  .color-field {
    --field-hue: hsl(0, 100%, 50%);
    position: relative;
    width: 100%;
    height: 132px;
    border-radius: var(--radius-md);
    overflow: hidden;
    cursor: crosshair;
    touch-action: none;   /* the drag is ours, not the browser's scroll */
    background-color: var(--field-hue);
    background-image:
      linear-gradient(to top, #000, transparent),
      linear-gradient(to right, #FFF, transparent);
    box-shadow: inset 0 0 0 1px var(--color-border);
  }

  .color-field-cursor {
    position: absolute;
    width: 12px;
    height: 12px;
    border: 2px solid #FFF;
    border-radius: var(--radius-full);
    /* Dark halo under the white ring so the cursor stays visible on white. */
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.55), var(--shadow-1);
    transform: translate(-50%, -50%);
    pointer-events: none;
  }

  .color-popover-label {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    margin-top: var(--space-2);
    font-size: var(--text-xs);
    color: var(--color-text-dim);
  }

  /* Qualified by the popover on purpose, and it must stay that way: the panel's
     generic slider is `input[type="range"]`, and an attribute selector carries
     class-level specificity — (0,1,1) beats a lone `.color-hue` at (0,1,0), so
     unqualified this rule loses every declaration to the macOS-style slider
     further down and the hue track renders as a flat 4px line. */
  .color-popover .color-hue {
    appearance: none;
    -webkit-appearance: none;
    width: 100%;
    height: 12px;
    margin: 0;
    padding: 0;
    border-radius: var(--radius-full);
    cursor: pointer;
    background: linear-gradient(to right,
      #F00 0%, #FF0 17%, #0F0 33%, #0FF 50%, #00F 67%, #F0F 83%, #F00 100%);

    &::-webkit-slider-runnable-track { height: 12px; background: transparent; }
    &::-webkit-slider-thumb {
      appearance: none;
      -webkit-appearance: none;
      width: 14px;
      height: 18px;
      margin-top: -3px;
      border: 2px solid #FFF;
      border-radius: var(--radius-sm);
      background: transparent;
      box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5), var(--shadow-1);
    }
    &::-moz-range-track { height: 12px; background: transparent; }
    &::-moz-range-thumb {
      width: 14px;
      height: 18px;
      border: 2px solid #FFF;
      border-radius: var(--radius-sm);
      background: transparent;
    }
  }

  .color-popover-row {
    display: flex;
    align-items: flex-end;
    gap: var(--space-2);
  }

  .color-preview {
    width: 34px;
    height: 30px;
    flex: 0 0 34px;
    border-radius: var(--radius-sm);
    box-shadow: inset 0 0 0 1px var(--color-border-strong);
  }

  .color-hex-label { flex: 1; }

  .color-hex {
    width: 100%;
    height: 30px;
    padding: 4px var(--space-2);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-surface-2);
    color: var(--color-text);
    font-family: ui-monospace, "SF Mono", Consolas, "Liberation Mono", monospace;
    font-size: var(--text-sm);
    text-transform: uppercase;

    &:focus { outline: none; border-color: var(--color-accent); box-shadow: var(--focus-ring); }
    /* Only a hint while typing — nothing is written through until the value
       parses, so an invalid box has changed nothing. */
    &.is-invalid { border-color: var(--color-danger); }
  }

  .color-presets-label {
    margin-top: var(--space-3);
    font-size: var(--text-xs);
    color: var(--color-text-dim);
  }

  .color-presets {
    display: grid;
    grid-template-columns: repeat(10, 1fr);
    gap: var(--space-1);
    margin-top: var(--space-1);
  }

  .color-preset {
    --preset-color: #FFF;
    aspect-ratio: 1;
    padding: 0;
    border-radius: var(--radius-sm);
    background: var(--preset-color);
    box-shadow: inset 0 0 0 1px var(--color-border-strong);
    transition: transform 0.12s var(--ease-out);

    &:hover  { transform: scale(1.12); }
    &:active { transform: scale(0.94); }
    /* A ring rather than a border: a border would shrink the colour it marks. */
    &.is-selected {
      box-shadow: 0 0 0 2px var(--color-surface-1), 0 0 0 3.5px var(--color-accent);
    }
  }
}
