/*
 * Settings surface.
 *
 * One section is open at a time and it owns the whole working column. The page
 * used to stack every section as an accordion, which meant the state of the page
 * was a set of independent open/closed flags that no save could restore: after a
 * form posted, the server re-rendered with only the first section open, a script
 * re-opened another one and scrolled it to the top of the scroller, and the page
 * heading went off screen. A single active section has one piece of state, and it
 * survives a swap because it is one identifier.
 *
 * Desktop and mobile are the same markup and two compositions, not one squeezed
 * into the other. Desktop: index rail beside the open section. Mobile: the index
 * is the screen, and choosing a section replaces it. That is also why a section is
 * named once on a phone — the old page showed the same names twice, as a scrolling
 * chip row and again as accordion headers directly under it.
 *
 * The breakpoint is the shell's own `1199px`: below it there is no sidebar and no
 * top bar, so the page must not be holding a desktop two-column layout.
 */

.settings {
  display: grid;
  grid-template-columns: minmax(190px, 240px) minmax(0, 1fr);
  align-items: start;
  gap: var(--fs-page-gap);
  min-width: 0;
}

/* Index ------------------------------------------------------------------- */

.settings-index {
  position: sticky;
  z-index: 1;
  top: calc(54px + var(--fs-page-gap));
  display: grid;
  align-content: start;
  gap: 2px;
  padding: var(--fs-space-2);
}

.settings-index__label {
  padding: var(--fs-space-2) var(--fs-space-2) var(--fs-space-1);
  color: var(--fs-color-text-secondary);
  font-size: var(--fs-text-xs);
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.settings-index__item {
  display: grid;
  grid-template-columns: 20px minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--fs-space-2);
  min-height: var(--fs-control-height);
  padding: var(--fs-space-2);
  color: var(--fs-color-text);
  border-radius: var(--fs-radius-md);
  font-size: var(--fs-text-md);
  font-weight: 500;
  text-decoration: none;
}

.settings-index__icon {
  width: 20px;
  height: 20px;
  flex: 0 0 20px;
  opacity: 0.7;
}

.settings-index__text {
  display: grid;
  min-width: 0;
}

.settings-index__text strong {
  overflow: hidden;
  font-weight: inherit;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* The rail names the section; the open section repeats the description itself. */
.settings-index__text small,
.settings-index__chevron {
  display: none;
}

.settings-index__item:hover {
  background: var(--fs-color-surface-hover);
}

.settings-index__item:focus-visible {
  outline: 0;
  box-shadow: var(--fs-focus-ring);
}

.settings-index__item:hover .settings-index__icon,
.settings-index__item[aria-current] .settings-index__icon {
  opacity: 1;
}

/*
 * Current place, not a highlighted row: the same selected family navigation uses
 * for the page you are on, so settings does not invent a sixth kind of "active".
 */
.settings-index__item[aria-current] {
  color: var(--fs-color-selected-text);
  background: var(--fs-color-selected);
  font-weight: 600;
}

/* Panel ------------------------------------------------------------------- */

.settings-panels {
  display: grid;
  gap: var(--fs-page-gap);
  min-width: 0;
}

/*
 * No `overflow: clip` here. The old section clipped its own content, so a select,
 * date popover or picker opened by a feature's settings form was cut off by the
 * card that contained it.
 */
.settings-panel {
  min-width: 0;
  padding: 0;
}

.settings-panel__head {
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr);
  align-items: center;
  gap: var(--fs-space-3);
  padding: var(--fs-space-3) var(--fs-card-padding);
  border-bottom: 1px solid var(--fs-color-border-subtle);
}

.settings-panel__back {
  display: none;
}

.settings-panel__icon {
  width: 22px;
  height: 22px;
  flex: 0 0 22px;
  opacity: 0.7;
}

.settings-panel__title {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.settings-panel__title strong {
  color: var(--fs-color-text);
  font-size: var(--fs-text-lg);
  font-weight: 600;
}

.settings-panel__title small,
.settings-panel__body p {
  color: var(--fs-color-text-secondary);
  font-size: var(--fs-text-sm);
  line-height: 1.4;
}

.settings-panel__body {
  padding: var(--fs-card-padding);
}

/*
 * Enhanced state. The attribute is written by settings.js, so a page without
 * JavaScript keeps every section rendered and reachable by its own anchor.
 */
.settings[data-settings-mode] .settings-panel {
  display: none;
}

.settings[data-settings-mode] .settings-panel[data-settings-active] {
  display: block;
}

/* Shared section primitives ----------------------------------------------- */

/*
 * These belong to the settings surface, not to any one feature: a section that
 * needs a titled group of controls or a labelled switch uses them instead of
 * inventing its own row, so two features' sections keep the same rhythm.
 */
.settings-composite {
  display: grid;
  gap: var(--fs-space-5);
  min-width: 0;
}

.settings-subsection {
  display: grid;
  gap: var(--fs-space-3);
  min-width: 0;
}

.settings-subsection + .settings-subsection {
  padding-top: var(--fs-space-5);
  border-top: 1px solid var(--fs-color-border-subtle);
}

.settings-subsection > h2 {
  margin: 0;
  color: var(--fs-color-text);
  font-size: var(--fs-text-md);
  font-weight: 600;
}

.settings-switch-option {
  display: grid;
  grid-template-columns: 20px minmax(0, 1fr);
  align-items: start;
  gap: var(--fs-space-2);
  width: 100%;
  padding: var(--fs-space-3);
  background: var(--fs-color-surface-subtle);
  border: 1px solid var(--fs-color-border-subtle);
  border-radius: var(--fs-radius-md);
  cursor: pointer;
}

.settings-switch-option:has(input:focus-visible) {
  outline: 0;
  box-shadow: var(--fs-focus-ring);
}

.settings-switch-option input {
  width: 18px;
  height: 18px;
  margin: 1px 0 0;
  accent-color: var(--fs-color-primary);
}

.settings-switch-option span {
  display: grid;
  gap: var(--fs-space-1);
  min-width: 0;
}

.settings-switch-option small {
  color: var(--fs-color-text-secondary);
  font-size: var(--fs-text-sm);
  line-height: 1.4;
}

@media (hover: hover) {
  .settings-switch-option:hover {
    background: var(--fs-color-surface-hover);
  }
}

/* Mobile ------------------------------------------------------------------ */

@media (max-width: 1199px) {
  /*
   * Nothing on this page is pinned on a phone, per the shell rules: a pinned rail
   * or heading takes a band of a short screen for the whole visit.
   */
  .settings {
    grid-template-columns: minmax(0, 1fr);
  }

  /*
   * One settings surface with hairline dividers, not a stack of raised cards —
   * the index is a list of destinations, and every row is the whole target.
   */
  .settings-index {
    position: static;
    gap: 0;
    padding: 0;
    overflow: hidden;
  }

  .settings-index__label {
    padding: var(--fs-space-3) var(--fs-card-padding) var(--fs-space-2);
  }

  .settings-index__item {
    grid-template-columns: 30px minmax(0, 1fr) 10px;
    gap: var(--fs-space-3);
    min-height: 60px;
    padding: var(--fs-space-2) var(--fs-card-padding);
    border-radius: 0;
  }

  /* The hairline starts where the names start, not under the icons. */
  .settings-index__item + .settings-index__item {
    box-shadow: inset 0 1px 0 var(--fs-color-border-subtle);
  }

  .settings-index__icon {
    width: 30px;
    height: 30px;
    flex: 0 0 30px;
    opacity: 1;
  }

  .settings-index__text {
    gap: 2px;
  }

  .settings-index__text small {
    display: block;
    overflow: hidden;
    color: var(--fs-color-text-secondary);
    font-size: var(--fs-text-sm);
    line-height: 1.35;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .settings-index__chevron {
    display: block;
    width: 8px;
    height: 8px;
    border-top: 2px solid var(--fs-color-text-muted);
    border-right: 2px solid var(--fs-color-text-muted);
    transform: rotate(45deg);
  }

  /* A row is a destination, not the current place: nothing here is "selected". */
  .settings-index__item[aria-current] {
    color: var(--fs-color-text);
    background: transparent;
    font-weight: 500;
  }

  /* The open section replaces the index instead of standing under it. */
  .settings[data-settings-mode="mobile"][data-settings-open] .settings-index {
    display: none;
  }

  .settings-panel__head {
    grid-template-columns: var(--fs-control-height) 30px minmax(0, 1fr);
    gap: var(--fs-space-2);
    padding: var(--fs-space-2) var(--fs-card-padding) var(--fs-space-2) var(--fs-space-1);
  }

  .settings[data-settings-mode="mobile"] .settings-panel__back {
    display: grid;
    place-items: center;
    width: var(--fs-control-height);
    height: var(--fs-control-height);
    padding: 0;
    color: var(--fs-color-primary);
    background: transparent;
    border: 0;
    border-radius: var(--fs-radius-md);
    cursor: pointer;
  }

  .settings-panel__back:focus-visible {
    outline: 0;
    box-shadow: var(--fs-focus-ring);
  }

  .settings-panel__back-arrow {
    width: 9px;
    height: 9px;
    border-bottom: 2px solid currentcolor;
    border-left: 2px solid currentcolor;
    transform: rotate(45deg);
  }

  .settings-panel__icon {
    width: 30px;
    height: 30px;
    flex: 0 0 30px;
    opacity: 1;
  }

  .settings-panel__title strong {
    font-size: var(--fs-text-md);
  }

  /* The rail already carried the description; inside the section it is repetition. */
  .settings-panel__title small {
    display: none;
  }

  .settings-panel__body {
    padding: var(--fs-card-padding) var(--fs-card-padding) var(--fs-space-5);
  }

  .settings-composite {
    gap: var(--fs-space-4);
  }

  .settings-subsection + .settings-subsection {
    padding-top: var(--fs-space-4);
  }

  .settings-switch-option {
    grid-template-columns: 22px minmax(0, 1fr);
    min-height: var(--fs-control-height);
    background: var(--fs-color-surface);
  }
}
