/* dosi.dev read-aloud — header buttons, bottom player bar, settings dialog.
   Built on the sacred theme's tokens (themes/sacred/assets/css/01-global.css): text is --font-size (16px) or
   --font-size-small (12.8px), spacing is the --line-rhythm grid (20px), and controls on these two app surfaces are
   drawn at --hit: 28px with a pointer, 44px with a finger. One bar, one control height. */

.tts-controls {
  display: inline-flex;
  align-items: center;
  gap: max(1ch, var(--hit-gap));
  visibility: hidden; /* shown by reader.js once speech is available: no jump, no dead buttons */
}
.tts-controls.tts-ready { visibility: visible; }
.tts-controls .tts-play[aria-pressed="true"] .hotkey { background: var(--theme-focused-foreground); }
.tts-controls[data-state="loading"] .tts-play .hotkey { opacity: 0.5; }

/* The read-aloud buttons take room in the bar, which must stay one row (measured 2026-11-19: at 685–820px the
   post-page header wrapped, ◐ THEME falling to a second line). Below 1100px ▶ and ◐ keep their glyph and lose the
   label; below 800px the page links fold into MENU, as the theme does below 684px. */
@media (max-width: 1100px) {
  .header__bar:has(.tts-controls) .theme-button .content { display: none; }
  .tts-controls .tts-play .content { display: none; }
  /* glyph-only buttons are square: ◐ is wider than ▶ and ⚙ with the theme's 1ch padding */
  .header__bar:has(.tts-controls) .theme-button .hotkey,
  .tts-controls .hotkey { padding: 0; width: var(--control); }
}
@media (max-width: 800px) {
  .header__bar:has(.tts-controls) .menu--desktop { display: none; }
  .header__bar:has(.tts-controls) .menu--mobile { display: inline-block; }
}

/* ------------------------------------------------------------ shared: buttons on the bar and in the dialog */

.tts-ui {
  --tts-pad: calc(var(--line-rhythm) * 0.5); /* 10px: bar padding, gap inside a group */
  --tts-gap: 4px; /* between neighbouring buttons: their hit area is the button itself */
  font-family: var(--font-family-mono);
  font-size: var(--font-size);
  line-height: var(--theme-line-height-base);
  color: var(--theme-text);
}
.tts-ui button {
  font: inherit;
  color: var(--theme-text);
  background: var(--theme-button-background);
  border: 0;
  border-radius: 0;
  box-shadow: inset 0 0 0 2px var(--theme-button-foreground);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--hit);
  min-height: var(--hit);
  padding: 0 1ch;
  margin: 0;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 120ms ease, box-shadow 120ms ease;
}
.tts-ui button:hover,
.tts-ui button:focus-visible {
  box-shadow: inset 0 0 0 2px var(--theme-focused-foreground);
  outline: 0;
}
.tts-ui button[aria-pressed="true"] {
  background: var(--theme-focused-foreground);
  box-shadow: inset 0 0 0 2px var(--theme-focused-foreground);
}

/* ------------------------------------------------------------ bottom bar */

.tts-bar {
  position: fixed;
  left: 50%;
  bottom: max(8px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(36rem, calc(100vw - 16px));
  box-sizing: border-box;
  z-index: 40;
  background: var(--theme-background);
  box-shadow: inset 0 0 0 2px var(--theme-text), 1ch 1ch 0 0 var(--theme-window-shadow);
  padding: var(--tts-pad);
  animation: tts-bar-in 200ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.tts-bar--out { animation: tts-bar-out 160ms ease-in forwards; }
@keyframes tts-bar-in { from { opacity: 0; transform: translate(-50%, 12px); } }
@keyframes tts-bar-out { to { opacity: 0; transform: translate(-50%, 12px); } }

/* text at the top left, count (and ⌖ when the page was scrolled away) at the top right: both sit --tts-pad from
   their two edges */
.tts-bar__top { display: flex; align-items: flex-start; gap: 1ch; margin-bottom: var(--tts-pad); }
.tts-bar__text {
  flex: 1;
  min-width: 0;
  max-height: calc(3 * var(--line-rhythm));
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  word-break: keep-all;
  overflow-wrap: anywhere;
}
.tts-bar__text mark {
  background: rgba(255, 170, 0, 0.55);
  color: inherit;
  text-decoration: underline 2px #e65c00;
  text-underline-offset: 3px;
}
.tts-bar__side { display: flex; align-items: center; gap: var(--tts-gap); flex: none; }
.tts-bar__count {
  color: var(--theme-text-subdued);
  font-size: var(--font-size-small);
  line-height: var(--line-rhythm);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.tts-bar__follow { display: none; }
.tts-bar--away .tts-bar__follow { display: inline-flex; background: var(--theme-focused-foreground); }

/* one row: every button the same height, sharing the width */
.tts-bar__controls { display: flex; align-items: stretch; gap: var(--tts-gap); }
.tts-bar__controls > button,
.tts-bar__speed > button { flex: 1 1 0; padding: 0; }
.tts-bar__speed { display: contents; }
.tts-bar__speedval {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 5ch;
  padding: 0 0.5ch;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
/* a narrow phone keeps six buttons of at least 44px: the speed steps stay in ⚙ */
@media (max-width: 479px) {
  .tts-bar__speed > button { display: none; }
}

/* the page keeps its end readable above the bar (reader.js measures the bar into --tts-bar-space) */
body.tts-bar-open .post-float-nav { bottom: var(--tts-bar-space, 160px); }
body.tts-bar-open .footer { padding-bottom: var(--tts-bar-space, 160px); }

/* ------------------------------------------------------------ settings dialog */

.tts-dialog {
  border: 0;
  padding: 0;
  width: min(32rem, calc(100vw - 16px));
  max-width: none;
  max-height: calc(100dvh - 16px);
  box-sizing: border-box;
  overflow: auto;
  background: var(--theme-background);
  box-shadow: inset 0 0 0 2px var(--theme-text), 1ch 1ch 0 0 var(--theme-window-shadow);
  opacity: 1;
  transform: none;
  transition: opacity 200ms ease, transform 200ms cubic-bezier(0.2, 0.8, 0.2, 1), overlay 200ms allow-discrete, display 200ms allow-discrete;
}
.tts-dialog:not([open]) { opacity: 0; transform: translateY(12px); }
@starting-style { .tts-dialog[open] { opacity: 0; transform: translateY(12px); } }
.tts-dialog::backdrop { background: rgba(0, 0, 0, 0.45); transition: background-color 200ms ease, overlay 200ms allow-discrete, display 200ms allow-discrete; }
@starting-style { .tts-dialog[open]::backdrop { background: rgba(0, 0, 0, 0); } }

/* --line-rhythm on every side; groups a rhythm apart, a label half a rhythm above its control */
.tts-dialog__inner { padding: var(--line-rhythm); display: grid; gap: var(--line-rhythm); }
.tts-dialog__head { display: flex; align-items: center; justify-content: space-between; gap: 1ch; }
.tts-dialog__head h2 { margin: 0; font-size: var(--font-size); line-height: var(--line-rhythm); font-weight: 700; outline: 0; } /* focused on open, so the close button does not look pressed */
.tts-dialog__close { padding: 0; }
.tts-field { display: grid; gap: var(--tts-pad); }
.tts-field[hidden] { display: none; }
.tts-field > span { font-weight: 700; line-height: var(--line-rhythm); }
.tts-dialog select {
  font: inherit;
  font-size: max(16px, var(--font-size)); /* iOS zooms into a field under 16px */
  width: 100%;
  min-height: var(--hit);
  color: var(--theme-text);
  /* drawn like the theme's other fields on every engine (iOS draws its own rounded select otherwise): the
     chevron is two gradient triangles in the text colour */
  appearance: none;
  -webkit-appearance: none;
  background-color: var(--theme-background-input);
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 1ch - 5px) 50%, calc(100% - 1ch) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  border: 0;
  border-radius: 0;
  box-shadow: inset 0 0 0 2px var(--theme-button-foreground);
  padding: 0 calc(2ch + 10px) 0 1ch;
  text-overflow: ellipsis;
}
.tts-dialog select:disabled { opacity: 1; color: var(--theme-text); }
.tts-dialog select:focus-visible { outline: 0; box-shadow: inset 0 0 0 2px var(--theme-focused-foreground); }
.tts-preview { justify-self: start; gap: 1ch; }
.tts-speed-row { display: flex; align-items: center; gap: var(--tts-pad); }
.tts-speed-row input[type="range"] { flex: 1; min-width: 0; margin: 0; min-height: var(--hit); accent-color: var(--theme-focused-foreground); }
/* 18 speeds as a 6 × 3 grid filling the width, every chip the same size */
.tts-speed-chips { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: var(--tts-gap); }
.tts-speed-chips button { min-width: 0; padding: 0; font-variant-numeric: tabular-nums; }
.tts-note { margin: 0; color: var(--theme-text-subdued); font-size: var(--font-size-small); line-height: 1.5; }
.tts-note:empty { display: none; }
.tts-dialog__inner > .tts-note { margin-top: calc(var(--line-rhythm) * -0.5); } /* a note belongs to the row above */

/* checkboxes drawn like the theme's buttons, the same in both states: a 20px box, filled inside when on. The edge
   is the subdued text colour, not the button grey: a control's boundary needs 3:1 (WCAG 1.4.11); gray-78 on white
   is 1.7:1, gray-42 is 5.3:1 (gray-66 on black 8.6:1). */
.tts-check { display: flex; align-items: center; gap: 1ch; min-height: var(--hit); cursor: pointer; }
.tts-check input {
  appearance: none;
  -webkit-appearance: none;
  border: 0;
  border-radius: 0; /* iOS rounds a checkbox even without its appearance */
  flex: none;
  width: var(--line-rhythm);
  height: var(--line-rhythm);
  margin: 0;
  background: var(--theme-background);
  box-shadow: inset 0 0 0 2px var(--theme-text-subdued);
  cursor: pointer;
}
.tts-check input:checked {
  background: var(--theme-text);
  box-shadow: inset 0 0 0 2px var(--theme-text-subdued), inset 0 0 0 5px var(--theme-background);
}
.tts-check input:focus-visible { outline: 2px solid var(--theme-focused-foreground); outline-offset: 2px; }
.tts-help summary { display: flex; align-items: center; gap: 1ch; min-height: var(--hit); cursor: pointer; list-style: none; }
.tts-help summary::-webkit-details-marker { display: none; }
.tts-help summary::before { content: '▸'; flex: none; width: var(--line-rhythm); text-align: center; }
.tts-help[open] summary::before { content: '▾'; }
.tts-help p { margin: var(--tts-pad) 0 0; font-size: var(--font-size-small); line-height: 1.5; color: var(--theme-text-subdued); }

@media (prefers-reduced-motion: reduce) {
  .tts-bar, .tts-bar--out { animation: none; }
  .tts-dialog, .tts-dialog::backdrop, .tts-ui button { transition: none; }
}
