/* Settings share one compact layout in the desktop panel and mobile drawer. */
#settingsPanel {
  --settings-sticky-top: 0px;
  --settings-summary-surface: var(--bg);
}
#settingsPanel .settings-groups {
  display: grid; grid-template-columns: minmax(0, 1fr); gap: 0; margin-top: 0;
}
#settingsPanel .settings-group { min-width: 0; margin: 0; border-top: 1px solid var(--line); scroll-margin-top: 8px; }
#settingsPanel .settings-group:first-child { border-top: 0; }
#settingsPanel .settings-group > summary {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-width: 0; min-height: 68px; padding: 12px 0; border-radius: 4px;
  color: var(--text); cursor: pointer; list-style: none;
}
/* Native details bounds release each header at the end of its own section. */
#settingsPanel .settings-group[open] > summary {
  position: sticky; top: var(--settings-sticky-top); z-index: 2;
  border-radius: 0; background: var(--settings-summary-surface);
  box-shadow: 0 1px 0 var(--line);
}
#settingsPanel .settings-group > summary:focus-visible { outline-offset: -3px; }
/* The drawer reserves its own heading; targets reserve just their section header. */
#settingsPanel .settings-group-body,
#settingsPanel .settings-group-body :is(.setting-section, .option-field, button, input, select, textarea, a[href], [tabindex]) {
  scroll-margin-top: calc(var(--settings-summary-height, 68px) + 8px);
}
#settingsPanel .settings-group > summary::-webkit-details-marker { display: none; }
#settingsPanel .settings-group > summary::marker { content: ""; }
#settingsPanel .settings-summary-copy { display: grid; gap: 2px; min-width: 0; }
#settingsPanel .settings-group-title { font-size: 15px; font-weight: 600; line-height: 22px; }
#settingsPanel .settings-group-value {
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  color: var(--muted); font-size: 13px; line-height: 18px; font-variant-numeric: tabular-nums;
}
#settingsPanel .settings-chevron { width: 20px; height: 20px; color: var(--muted); }
#settingsPanel .settings-group[open] > summary .settings-chevron { transform: rotate(180deg); }
#settingsPanel .settings-group-body { min-width: 0; padding: 0 0 14px; }
#settingsPanel .settings-group:not([open]) > .settings-group-body { display: none; }

/* A stable message slot avoids moving the groups when persistence fails. */
#settingsPanel .settings-save-status {
  height: 36px; margin: 2px 0 4px; padding-top: 3px;
  overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin;
}
#settingsPanel .settings-save-status .setting-help { margin: 0; }
#settingsPanel .settings-save-status:has(#preferencesStatus:not([hidden])) .settings-save-hint { display: none; }
#settingsPanel .settings-save-status [tabindex]:focus-visible { outline-offset: -2px; }
#settingsPanel .setting-help, #settingsPanel .privacy-note,
#settingsPanel .profile-feedback-slot #profileStatus {
  font-size: 13px; line-height: 18px;
}
#settingsPanel .button, #settingsPanel .text-button {
  min-height: 44px; font-size: 14px; line-height: 20px;
}
#settingsPanel h3 { font-size: 14px; line-height: 20px; }

/* Appearance feedback does not share playback state or reader geometry. */
#settingsPanel .settings-preview {
  position: relative; height: 120px; margin-bottom: 12px; overflow: hidden;
  border-radius: 10px; background: var(--bg); color: var(--text);
  user-select: none; -webkit-user-select: none; pointer-events: none;
}
#settingsPanel .settings-preview-label {
  position: absolute; top: 9px; left: 12px;
  color: var(--muted); font-size: 11px; font-weight: 500; line-height: 16px;
}
#settingsPanel .settings-preview-word {
  position: absolute; top: 50%; left: 50%; width: max-content;
  font-size: 52px; font-weight: 500; line-height: 1; letter-spacing: 0;
  white-space: nowrap; transform: translate(-50%, -50%); transform-origin: 0 center;
}
#settingsPanel .settings-preview.anchored #settingsPreviewAnchor { color: var(--focus-color); }
#settingsPanel .settings-preview-guide {
  position: absolute; left: 50%; width: 1px; height: 8px;
  background: var(--focus-color); transform: translateX(-50%); visibility: hidden;
}
#settingsPanel .settings-preview-guide.top { top: 11px; }
#settingsPanel .settings-preview-guide.bottom { bottom: 11px; }
#settingsPanel .settings-preview.anchored:not(.guides-off) .settings-preview-guide { visibility: visible; }
#settingsPanel .display-options { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; }
#settingsPanel .option-field { min-width: 0; margin: 0; padding: 0; border: 0; }
#settingsPanel .option-label, #settingsPanel .option-field > legend {
  display: block; width: 100%; margin: 0 0 8px; padding: 0;
  color: var(--text); font-size: 13px; font-weight: 550; line-height: 18px;
}
#settingsPanel .setting-label { font-size: 13px; line-height: 18px; }
#settingsPanel .setting-label output { color: var(--muted); font-weight: 400; font-variant-numeric: tabular-nums; }
#settingsPanel .size-control { height: 44px; margin: 0; }
#settingsPanel .option-field > select { width: 100%; }
#settingsPanel select, #settingsPanel input:not([type="range"]):not([type="checkbox"]):not([type="radio"]) {
  min-height: 44px; font-size: 16px;
}

/* Native radios retain keyboard behavior; their labels provide generous targets. */
#settingsPanel .setting-choices {
  display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 6px;
}
#settingsPanel .choice-option { position: relative; display: block; min-width: 0; cursor: pointer; }
#settingsPanel .choice-option > input {
  position: absolute; inset: 0; z-index: 1; width: 100%; height: 100%; margin: 0;
  opacity: 0; cursor: inherit;
}
#settingsPanel .choice-face, #settingsPanel .segmented button, #settingsPanel .font-options button {
  display: flex; align-items: center; justify-content: center;
  min-width: 0; min-height: 44px; padding: 8px 6px;
  border: 1px solid var(--line); border-radius: 24px;
  background: transparent; color: var(--text); font-size: 13px; font-weight: 500; line-height: 20px;
  text-align: center; overflow-wrap: anywhere;
}
#settingsPanel .choice-face { height: 100%; }
#settingsPanel .choice-option > input:checked + .choice-face,
#settingsPanel .segmented button.selected, #settingsPanel .font-options button.selected {
  border-color: var(--focus-color); background: var(--soft); color: var(--text);
  box-shadow: inset 0 0 0 1px var(--focus-color);
}
#settingsPanel .choice-option > input:focus-visible { outline: none; }
#settingsPanel .choice-option > input:focus-visible + .choice-face {
  outline: 2px solid var(--focus-color); outline-offset: 3px;
}
#settingsPanel .choice-option:has(> input:disabled) { cursor: default; }
#settingsPanel .choice-option > input:disabled + .choice-face { opacity: .55; }
#settingsPanel .segmented, #settingsPanel .font-options { gap: 6px; }
#settingsPanel .colour-choices .choice-face { gap: 6px; padding: 8px 4px; }
#settingsPanel .colour-dot {
  position: relative; display: block; width: 14px; height: 14px; flex: 0 0 14px; border-radius: 50%;
}
#settingsPanel .colour-choices [data-value="amber"] .colour-dot { background: var(--settings-amber); }
#settingsPanel .colour-choices [data-value="teal"] .colour-dot { background: var(--settings-teal); }
#settingsPanel .colour-choices [data-value="blue"] .colour-dot { background: var(--settings-blue); }
#settingsPanel .colour-choices [data-value="rose"] .colour-dot { background: var(--settings-rose); }
#settingsPanel .colour-choices input:checked + .choice-face .colour-dot::after {
  content: ""; position: absolute; top: 4px; left: 3px; width: 8px; height: 5px;
  border: solid var(--bg); border-width: 0 0 2px 2px; transform: rotate(-45deg);
}

/* Boolean preferences use one flat row style, not a second family of pills. */
#settingsPanel .display-switches {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 9rem), 1fr));
  align-content: start; gap: 0 16px;
}
#settingsPanel .toggle-row, #settingsPanel .setting-switch {
  min-height: 48px; margin: 0; padding: 10px 0; border: 0; border-radius: 4px;
  background: transparent; color: var(--text); font-size: 14px; line-height: 20px;
}
#settingsPanel .setting-switch { grid-template-columns: minmax(0, 1fr) 34px; gap: 12px; }
#settingsPanel .setting-switch .switch-state { display: none; }
#settingsPanel .toggle-row strong, #settingsPanel .setting-switch > span:first-child { font-size: 14px; font-weight: 500; }
#settingsPanel .toggle-row > span:first-child { min-width: 0; overflow-wrap: anywhere; }
#settingsPanel .toggle-row:has(input:focus-visible) { outline: 2px solid var(--focus-color); outline-offset: 3px; }
#settingsPanel .toggle-row input:focus-visible + .switch { outline: none; }
#settingsPanel .toggle-row:has(input:disabled), #settingsPanel .setting-switch:disabled { opacity: .55; }
#settingsPanel .switch { background: var(--muted); }
#settingsPanel .toggle-row input:checked + .switch,
#settingsPanel .setting-switch[aria-checked="true"] .switch { background: var(--green); }
#settingsPanel .setting-context-slot { block-size: 18px; margin-top: 4px; font-size: 12px; line-height: 18px; }
#settingsPanel .alignment-setting .setting-context-slot { block-size: 36px; }
#settingsPanel .setting-context-slot .setting-help { margin: 0; font-size: inherit; line-height: inherit; }

/* Secondary controls stay in normal flow, including feedback and reserved actions. */
#settingsPanel .settings-group-body > .setting-section { margin-top: 14px; padding-top: 14px; }
#settingsPanel .settings-group-body > .setting-section:first-child { margin-top: 0; padding-top: 0; border-top: 0; }
#settingsPanel .wpm-presets-heading, #settingsPanel .wpm-preset-fields,
#settingsPanel .wpm-preset-status, #settingsPanel .ramp-fields,
#settingsPanel .ramp-footer, #settingsPanel .audio-mode-controls,
#settingsPanel .audio-options, #settingsPanel .profile-load-row,
#settingsPanel .profile-edit-row, #settingsPanel .profile-editor,
#settingsPanel .profile-actions { max-width: none; }
#settingsPanel .wpm-presets-heading h3 { margin: 0; }
#settingsPanel .wpm-presets-section { container: reader-wpm-shortcuts / inline-size; }
#settingsPanel .wpm-preset-fields { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px 8px; }
#settingsPanel .wpm-preset-fields label, #settingsPanel .ramp-fields label,
#settingsPanel .profile-edit-row > label { font-size: 13px; line-height: 18px; }
#settingsPanel .wpm-preset-fields label { gap: 4px; }
#settingsPanel .wpm-preset-fields input {
  padding-inline: 4px; text-align: center; appearance: textfield; -moz-appearance: textfield;
}
#settingsPanel .wpm-preset-fields input::-webkit-inner-spin-button,
#settingsPanel .wpm-preset-fields input::-webkit-outer-spin-button { margin: 0; -webkit-appearance: none; }
#settingsPanel .wpm-preset-status { block-size: 36px; }
#settingsPanel .ramp-fields { gap: 8px; margin-top: 6px; }
#settingsPanel .ramp-fields label { grid-template-rows: minmax(18px, auto) 44px; gap: 4px; }
#settingsPanel .ramp-footer { gap: 8px; font-size: 12px; }
#settingsPanel .pace-feedback { height: 18px; }
#settingsPanel #rampUnit.sr-only { width: 1px; height: 1px; }
#settingsPanel .pace-feedback .setting-help { margin: 0; }
#settingsPanel #rampInputStatus {
  block-size: 36px; overflow-y: auto; overflow-wrap: anywhere;
  overscroll-behavior: contain; scrollbar-width: thin;
}
#settingsPanel #rampInputStatus:focus-visible { outline-offset: -2px; }
#settingsPanel .settings-action-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  width: 100%; min-height: 68px; margin-top: 14px; padding: 12px 0;
  border-top: 1px solid var(--line); background: transparent; color: var(--text); text-align: left;
}
#settingsPanel .settings-action-row > span { display: grid; gap: 2px; min-width: 0; }
#settingsPanel .settings-action-row strong { font-size: 14px; font-weight: 550; line-height: 20px; }
#settingsPanel .settings-action-row small { color: var(--muted); font-size: 13px; line-height: 18px; }
#settingsPanel .settings-action-row svg { width: 18px; height: 18px; color: var(--muted); }
#settingsPanel .audio-file-row {
  grid-template-columns: minmax(0, 1fr) auto; gap: 4px 12px; height: 68px;
}
#settingsPanel #audioFileName { grid-column: 1 / -1; font-size: 13px; line-height: 20px; }
#settingsPanel #audioUploadButton { justify-self: start; }
#settingsPanel .audio-remove-slot { grid-column: 2; }
#settingsPanel .audio-mode-controls { grid-template-columns: minmax(0, 1fr); gap: 0; margin-top: 8px; }
#settingsPanel .audio-feedback-slot { height: 60px; padding-top: 4px; }
#settingsPanel .audio-display-help { block-size: 36px; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; }
#settingsPanel .profile-feedback-slot { height: 54px; margin-top: 8px; padding-top: 0; }
#settingsPanel .profile-feedback-slot #profileStatus { overflow-wrap: anywhere; }
#settingsPanel .profile-editor { margin-top: 4px; }
#settingsPanel .profile-actions { gap: 6px; }
#settingsPanel .profile-actions button { padding-inline: 4px; }
#settingsPanel #installHelp { height: 54px; }
#settingsPanel .install-status-slot { height: 54px; }
#settingsPanel .settings-actions { gap: 4px 12px; margin-top: 10px; padding-top: 8px; }
#settingsPanel .transcript-actions { margin-top: 4px; padding-top: 0; border-top: 0; }

/* The desktop panel is wider than the drawer; groups themselves never form columns. */
@media (min-width: 360px) {
  #settingsDrawer .display-options { gap: 8px; }
  #settingsDrawer .display-options > .option-field:has(.font-options),
  #settingsDrawer .display-options > .size-field,
  #settingsDrawer .display-options > .alignment-setting {
    display: grid; grid-template-columns: 92px minmax(0, 1fr); align-items: center; column-gap: 12px;
  }
  #settingsDrawer .display-options > .option-field > .option-label { margin: 0; }
  #settingsDrawer .size-field .setting-label { display: grid; gap: 0; }
  #settingsDrawer .alignment-setting .setting-context-slot { grid-column: 2; }
  /* A floated legend keeps native fieldset semantics without absolute sizing. */
  #settingsDrawer .display-options > .choice-field:has(#fontWeight) > legend {
    float: left; display: flex; align-items: center;
    width: 92px; min-height: 44px; margin: 0;
  }
  #settingsDrawer .display-options > .choice-field:has(#fontWeight) > .setting-choices { margin-left: 104px; }
  #settingsDrawer .display-options > .choice-field:has(#fontWeight) > select {
    width: calc(100% - 104px); margin-left: 104px;
  }
  /* Without enhancement, the native select keeps its simple stacked layout. */
  #settingsDrawer .display-options > .choice-field:has(#fontWeight):not(:has(.setting-choices)) > legend {
    float: none; display: block; width: 100%; min-height: 0; margin-bottom: 8px;
  }
  #settingsDrawer .display-options > .choice-field:has(#fontWeight):not(:has(.setting-choices)) > select {
    width: 100%; margin-left: 0;
  }
}
@media (min-width: 720px) {
  .app > #settingsPanel .display-options { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px 24px; }
}
/* Five 52px fields and four 8px gaps fit at 292px (18.25rem by default).
   Older browsers and narrower or enlarged-text layouts keep three plus two. */
@container reader-wpm-shortcuts (min-width: 18.25rem) {
  #settingsPanel .wpm-preset-fields { grid-template-columns: repeat(5, minmax(0, 1fr)); }
}
@media (max-width: 359px) {
  #settingsPanel .colour-choices { grid-auto-flow: row; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #settingsPanel .colour-choices .choice-face { flex-direction: row; min-height: 44px; gap: 8px; }
}
@media (hover: hover) {
  #settingsPanel .settings-group > summary:hover .settings-group-title { color: var(--focus-color); }
  #settingsPanel .choice-option:has(> input:not(:disabled)):hover .choice-face,
  #settingsPanel .segmented button:not(:disabled):hover,
  #settingsPanel .font-options button:not(:disabled):hover { border-color: var(--muted); }
  #settingsPanel .settings-action-row:hover strong { text-decoration: underline; text-underline-offset: 3px; }
}
@media (prefers-reduced-motion: reduce) {
  #settingsPanel .switch::after { transition: none; }
}
@media (forced-colors: active) {
  #settingsPanel .choice-face, #settingsPanel .segmented button, #settingsPanel .font-options button {
    border-color: ButtonText; background: Canvas; color: CanvasText;
  }
  #settingsPanel .choice-option > input:checked + .choice-face,
  #settingsPanel .segmented button.selected, #settingsPanel .font-options button.selected {
    outline: 2px solid Highlight; outline-offset: -3px; box-shadow: none;
  }
  #settingsPanel .choice-option > input:focus-visible + .choice-face,
  #settingsPanel .toggle-row:has(input:focus-visible) { outline: 2px solid Highlight; outline-offset: 3px; }
  #settingsPanel .colour-dot { forced-color-adjust: none; }
  #settingsPanel .switch { border: 1px solid ButtonText; background: Canvas; }
  #settingsPanel .switch::after { background: ButtonText; }
  #settingsPanel .toggle-row input:checked + .switch,
  #settingsPanel .setting-switch[aria-checked="true"] .switch { background: Highlight; }
  #settingsPanel .toggle-row input:checked + .switch::after,
  #settingsPanel .setting-switch[aria-checked="true"] .switch::after { background: HighlightText; }
  #settingsPanel .settings-preview.anchored #settingsPreviewAnchor { color: Highlight; }
  #settingsPanel .settings-preview-guide { background: Highlight; }
}
