/* ==========================================================================
   Koharu — Settings Center drawer

   The visitor-facing preference panel. Loaded only when 主题设置 → 阅读体验 →
   「启用访客设置中心」 is on, which is why the styling hooks it toggles
   (`data-font-preset`, `.reader-justify`, `.motion-off`, `.wave-off`,
   `.scroll-progress-off`) live in the regular stylesheets instead — turning the
   drawer off must not turn a saved preference off with it.
   ========================================================================== */

/* Transparent click-catcher, not a dimmer: the panel is a preference popover, so
   the page behind it stays readable and visible. */
.settings-overlay {
  position: fixed;
  inset: 0;
  z-index: 45;
}

/*
 * Geometry is the source's, at every width: `SettingsPanelContent.tsx` puts the
 * panel at `fixed right-16 bottom-20 w-[320px] max-w-[calc(100vw-5rem)]`.
 *
 * The `max-width` is what keeps it clear of the floating buttons on a phone.
 * It used to be `calc(100vw - 2rem)`, which is 32px narrower than the screen —
 * but the panel's right edge sits 64px in, so on a 390px screen the box was
 * 358px wide and started at -32px, and a later `≤992px` rule stretched it to
 * `left: 1rem; right: 1rem` and covered the whole 374px column the buttons live
 * in. Measured on the source at 390×844: panel 16…326, settings button
 * 338…374 — two boxes that never meet. This is that.
 */
.settings-panel {
  position: fixed;
  right: 4rem;
  bottom: 5rem;
  z-index: 46;
  display: flex;
  width: 20rem;
  max-width: calc(100vw - 5rem);
  max-height: calc(100dvh - 9rem);
  flex-direction: column;
  overflow: hidden;
  padding: 1rem;
  border-radius: 1rem;
  background: var(--gradient-bg-start);
  color: hsl(var(--foreground));
  box-shadow: var(--box-bg-shadow) 0 1.25rem 2.5rem -1.25rem;
  font-size: 0.875rem;
}

.settings-panel[hidden] {
  display: none;
}

/* --------------------------------------------------------------- Headings */
.settings-head {
  display: flex;
  margin-bottom: 0.75rem;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.settings-title {
  display: flex;
  margin: 0;
  align-items: center;
  gap: 0.375rem;
  font-size: 0.875rem;
  font-weight: 600;
}

.settings-title svg {
  height: 1rem;
  width: 1rem;
  color: hsl(var(--primary));
}

.settings-close {
  display: flex;
  height: 1.75rem;
  width: 1.75rem;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 9999px;
  background: rgb(0 0 0 / 0.05);
  color: hsl(var(--muted-foreground));
  /* `transform` is in the list because the press shrinks the button —
     `SettingsPanelContent.tsx:225` carries `active:scale-[0.96]`. */
  transition: background var(--duration-ui) var(--ease-out), color var(--duration-ui) var(--ease-out),
    transform var(--duration-tween) var(--ease-out);
}

.settings-close:active {
  transform: scale(0.96);
}

.settings-close svg {
  height: 0.875rem;
  width: 0.875rem;
}

.settings-close:hover {
  background: rgb(0 0 0 / 0.1);
  color: hsl(var(--foreground));
}

.dark .settings-close {
  background: rgb(255 255 255 / 0.1);
}

.dark .settings-close:hover {
  background: rgb(255 255 255 / 0.2);
}

/* ------------------------------------------------------------------ Tabs */
.settings-tabs {
  position: relative;   /* the pill below is positioned against this */
  display: flex;
  margin-bottom: 0.75rem;
  gap: 0.25rem;
  padding: 0.25rem;
  border-radius: 0.5rem;
  background: hsl(var(--muted));
}

/*
 * The highlight. One node, moved and sized by the script to sit under the
 * selected tab, so switching tabs slides it across instead of cross-fading two
 * backgrounds. `transition: none` until `.is-ready`, which the script adds once
 * it has placed the pill for the first time — otherwise the very first
 * placement animates in from the container's top-left corner.
 *
 * The curve is the source's: `layoutId` motion with `microReboundPreset`
 * (`spring(stiffness 300, damping 24)`), the same slightly-overshooting spring
 * the panel itself enters on. `--ease-spring` is the port's stand-in.
 */
.settings-pill {
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 0.375rem;
  background: hsl(var(--background));
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.08);
  transition: none;
}

.settings-pill.is-ready {
  transition: transform var(--duration-ui) var(--ease-spring),
    width var(--duration-ui) var(--ease-spring),
    height var(--duration-ui) var(--ease-spring);
}

.settings-tab {
  position: relative;
  z-index: 1;           /* the label paints over the pill */
  flex: 1;
  padding: 0.25rem 0.75rem;
  border: 0;
  border-radius: 0.375rem;
  background: transparent;
  color: hsl(var(--muted-foreground));
  font-size: 0.75rem;
  font-weight: 500;
  /* Only the colour animates now: the background belongs to the pill, and the
     source's tab button carries `transition-colors` alone. */
  transition: color var(--duration-tween) var(--ease-out);
}

.settings-tab[aria-selected="true"] {
  color: hsl(var(--foreground));
}

/* ------------------------------------------------------------------ Body */
.settings-body {
  min-height: 0;
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.settings-section[hidden] {
  display: none;
}

/*
 * Content cross-fade, at the source's values: `AnimatePresence mode="wait"`
 * over `{ opacity: 0, y: 6 }` -> `{ opacity: 1, y: 0 }`, the outgoing section
 * leaving at `{ opacity: 0, y: -4 }`, all on
 * `{ duration: 0.15, ease: 'easeOut' }` (`SettingsPanelContent.tsx:262-269`).
 *
 * `mode="wait"` is the load-bearing half: the old section finishes leaving
 * before the new one starts arriving, so the two are never on screen together.
 * The script drives that hand-off; these two classes are the states it toggles.
 */
.settings-section {
  transition: opacity var(--duration-fast) var(--ease-out),
    transform var(--duration-fast) var(--ease-out);
}

/* Leaving — the source's exit, `y: -4`. */
.settings-section.is-leaving {
  opacity: 0;
  transform: translateY(-0.25rem);
}

/* Arriving — the source's `initial`, `y: 6`. Dropped again on the next frame. */
.settings-section.is-entering {
  opacity: 0;
  transform: translateY(0.375rem);
}

/*
 * Reduced motion keeps the fade and drops the movement, which is what the source
 * does — `initial`/`exit` collapse to bare opacity while the 150ms stays. The
 * pill goes further and stops dead, because the source gives it `duration: 0`.
 */
@media (prefers-reduced-motion: reduce) {
  .settings-section.is-leaving,
  .settings-section.is-entering {
    transform: none;
  }

  .settings-pill.is-ready {
    transition: none;
  }
}

html.motion-off .settings-section.is-leaving,
html.motion-off .settings-section.is-entering {
  transform: none;
}

html.motion-off .settings-pill.is-ready {
  transition: none;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding-block: 0.625rem;
  border-top: 1px solid hsl(var(--border));
}

.settings-row:first-child {
  border-top: 0;
  padding-top: 0.25rem;
}

.settings-row--stacked {
  flex-direction: column;
  align-items: stretch;
  gap: 0.5rem;
}

.settings-row[hidden] {
  display: none;
}

.settings-label {
  font-size: 0.875rem;
}

.settings-hint {
  margin: 0;
  color: hsl(var(--muted-foreground));
  font-size: 0.75rem;
  line-height: 1.5;
}

.settings-hint--foot {
  padding-top: 0.75rem;
}

/* ------------------------------------------------------------- Segmented */
.settings-segments {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.settings-segment {
  padding: 0.25rem 0.625rem;
  border: 0;
  border-radius: 0.375rem;
  background: hsl(var(--muted));
  color: hsl(var(--muted-foreground));
  font-size: 0.75rem;
  transition: background var(--duration-tween) var(--ease-out), color var(--duration-tween) var(--ease-out);
}

.settings-segment:hover {
  background: hsl(var(--accent));
  color: hsl(var(--foreground));
}

.settings-segment[aria-pressed="true"] {
  background: hsl(var(--primary));
  color: hsl(var(--primary-foreground));
}

/* --------------------------------------------------------------- Inputs */
.settings-text,
.settings-number {
  padding: 0.25rem 0.5rem;
  border: 1px solid hsl(var(--input));
  border-radius: 0.375rem;
  background: hsl(var(--background));
  color: inherit;
  font-size: 0.875rem;
  outline: none;
  transition: border-color var(--duration-tween) var(--ease-out), box-shadow var(--duration-tween) var(--ease-out);
}

.settings-text {
  width: 100%;
}

.settings-number {
  width: 4.5rem;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.settings-text:focus-visible,
.settings-number:focus-visible {
  border-color: hsl(var(--ring));
  box-shadow: 0 0 0 2px hsl(var(--ring) / 0.3);
}

/* The steppers write real values, so the spinners would just be clutter. */
.settings-number::-webkit-outer-spin-button,
.settings-number::-webkit-inner-spin-button {
  margin: 0;
  -webkit-appearance: none;
}

.settings-number {
  -moz-appearance: textfield;
  appearance: textfield;
}

.settings-stepper {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.settings-step {
  display: flex;
  height: 1.75rem;
  width: 1.75rem;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 0.375rem;
  background: transparent;
  color: hsl(var(--muted-foreground));
  font-size: 1rem;
  line-height: 1;
  transition: background var(--duration-tween) var(--ease-out), color var(--duration-tween) var(--ease-out),
    transform var(--duration-tween) var(--ease-out);
}

.settings-step:hover {
  background: hsl(var(--accent));
  color: hsl(var(--foreground));
}

.settings-step:active {
  transform: scale(0.96);
}

.settings-unit {
  width: 1.25rem;
  color: hsl(var(--muted-foreground));
  font-size: 0.75rem;
}

/* --------------------------------------------------------------- Switch */
.settings-switch {
  display: inline-flex;
  flex-shrink: 0;
  cursor: pointer;
}

.settings-switch input {
  position: absolute;
  height: 1px;
  width: 1px;
  overflow: hidden;
  clip-path: inset(50%);
}

.settings-switch-track {
  position: relative;
  display: block;
  height: 1.25rem;
  width: 2.25rem;
  border-radius: 9999px;
  background: hsl(var(--muted));
  transition: background var(--duration-ui) var(--ease-out);
}

.settings-switch-track::after {
  content: "";
  position: absolute;
  top: 0.1875rem;
  left: 0.1875rem;
  height: 0.875rem;
  width: 0.875rem;
  border-radius: 9999px;
  background: hsl(var(--background));
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.25);
  transition: transform var(--duration-ui) var(--ease-out);
}

.settings-switch input:checked + .settings-switch-track {
  background: hsl(var(--primary));
}

.settings-switch input:checked + .settings-switch-track::after {
  transform: translateX(1rem);
}

.settings-switch input:focus-visible + .settings-switch-track {
  box-shadow: 0 0 0 2px hsl(var(--ring) / 0.4);
}

/* 波浪动效 has no effect while 减弱动画 is on — the source disables it too. */
.settings-switch input:disabled + .settings-switch-track,
.settings-row--disabled {
  opacity: 0.5;
}

.settings-switch input:disabled + .settings-switch-track {
  cursor: not-allowed;
}

/* ---------------------------------------------------------------- Reset */
.settings-reset {
  margin-top: 0.75rem;
  width: 100%;
  padding-block: 0.375rem;
  border: 1px solid hsl(var(--input));
  border-radius: 0.375rem;
  background: transparent;
  color: hsl(var(--muted-foreground));
  font-size: 0.75rem;
  transition: background var(--duration-tween) var(--ease-out), color var(--duration-tween) var(--ease-out),
    transform var(--duration-tween) var(--ease-out);
}

.settings-reset:hover {
  background: hsl(var(--accent));
  color: hsl(var(--foreground));
}

.settings-reset:active {
  transform: scale(0.96);
}

/* --------------------------------------------------------------- Motion */
/*
 * Enter *and* exit, both. The source animates this panel both ways with one
 * preset: `initial` and `exit` are `{ opacity: 0, y: 20, scale: 0.95 }`,
 * `animate` is the resting state, and the transition is `microReboundPreset`
 * — `SettingsPanelContent.tsx:214-217`, and the preset itself is
 * `spring(stiffness: 300, damping: 24)` in `@constants/anim/spring`.
 *
 * A spring is a scripted curve, so this is the closest CSS shape rather than an
 * exact copy: stiffness 300 / damping 24 is a damping ratio near 0.69, which
 * overshoots by roughly 5%, and `--ease-spring` overshoots by roughly 10%. Same
 * character, a shade livelier. Opacity takes `--ease-out` instead — an
 * overshooting opacity is not a thing, since the curve clips at 1 and the excess
 * reads as a flicker.
 *
 * Driven by `.is-open` rather than by `[hidden]`, because `base.css` gives
 * `[hidden]` a `display: none` and nothing transitions out of that. The script
 * flips the class, waits out the longer of the two durations, then sets `hidden`
 * — the same two-step `.mobile-drawer` already uses.
 *
 * `pointer-events` is toggled with the class rather than left to the timeout:
 * for the length of the close the panel is still in the layer, and at `opacity:
 * 0` it would otherwise go on swallowing clicks aimed at the page behind it.
 */
.settings-panel {
  opacity: 0;
  transform: translateY(1.25rem) scale(0.95);
  pointer-events: none;
  transition: opacity var(--duration-normal) var(--ease-out),
    transform var(--duration-ui) var(--ease-spring);
}

.settings-panel.is-open {
  opacity: 1;
  transform: none;
  pointer-events: auto;
}

/*
 * Reduced motion keeps the fade and drops the movement, which is what the source
 * does: `shouldReduceMotion` swaps the spring for `{ duration: 0.15 }` over the
 * same opacity-only start and end states. Deleting the animation outright — as
 * this used to — made the panel blink into place, which is a bigger jolt than
 * the fade it was meant to spare the reader.
 */
@media (prefers-reduced-motion: reduce) {
  .settings-panel {
    transform: none;
    transition: opacity var(--duration-fast) var(--ease-out);
  }
}

html.motion-off .settings-panel {
  transform: none;
  transition: opacity var(--duration-fast) var(--ease-out);
}

/* ----------------------------------------------------------- Small screens */
/*
 * No width override here on purpose. A full-bleed panel is the tempting thing
 * to write at this size, and it is what the port used to do — but the buttons
 * it belongs to are fixed to the same corner, and at 390px the two boxes ended
 * up on top of each other (`topAtToggle` resolved to the panel, not the
 * button). The only thing a phone needs is a shorter reach upward.
 */
@media (max-width: 992px) {
  .settings-panel {
    max-height: calc(100dvh - 7rem);
  }
}
