/* Speech / listen mode — bottom-left FAB; self-contained light/dark colors */

.read-aloud-root {
  --ra-text: #333333;
  --ra-text-muted: #6c757d;
  --ra-text-faint: #adb5bd;
  --ra-surface: #ffffff;
  --ra-surface-muted: #f8f9fa;
  --ra-border: #dee2e6;
  --ra-btn-bg: #ffffff;
  --ra-btn-text: #495057;
  --ra-hint-bg: #fff8e1;
  --ra-hint-text: #856404;
  --ra-hint-border: #ffc107;
  --ra-badge-bg: #ffffff;
  --ra-badge-text: var(--color-primary, #667eea);
  --ra-accent-gradient: var(--gradient-header, linear-gradient(135deg, #667eea 0%, #764ba2 100%));
}

html[data-color-mode='dark'] .read-aloud-root {
  --ra-text: #e8eaef;
  --ra-text-muted: #a8b0c4;
  --ra-text-faint: #7a849c;
  --ra-surface: #1e2438;
  --ra-surface-muted: #2f3854;
  --ra-border: #4a5578;
  --ra-btn-bg: #2f3854;
  --ra-btn-text: #e8eaef;
  --ra-hint-bg: #2f3854;
  --ra-hint-text: #e8eaef;
  --ra-hint-border: var(--color-primary, #8b9cf7);
  --ra-badge-bg: #2f3854;
  --ra-badge-text: var(--color-primary, #8b9cf7);
}

.read-aloud-root {
  position: fixed;
  left: max(1rem, env(safe-area-inset-left, 0px));
  bottom: max(1rem, env(safe-area-inset-bottom, 0px));
  z-index: 9500;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.75rem;
  max-width: min(100vw - 2rem, 22rem);
  pointer-events: none;
  isolation: isolate;
}

.read-aloud-root > * {
  pointer-events: auto;
}

.read-aloud-root button,
.read-aloud-root select,
.read-aloud-root input {
  pointer-events: auto;
  touch-action: manipulation;
}

.read-aloud-root.is-open .read-aloud-panel {
  display: block;
  visibility: visible;
}

.read-aloud-active {
  border-radius: 8px;
  background-color: var(--color-highlight-active, rgba(255, 243, 205, 0.95)) !important;
  box-shadow: 0 0 0 2px var(--color-primary-ring, rgba(102, 126, 234, 0.45));
  transition: background-color 0.3s ease, box-shadow 0.3s ease;
}

.read-aloud-panel {
  width: min(calc(100vw - 2rem), 22rem);
  max-height: min(70dvh, calc(100dvh - 7rem));
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  border-radius: 1rem;
  border: 1px solid var(--color-primary-soft, rgba(102, 126, 234, 0.25));
  background: var(--ra-surface, rgba(255, 255, 255, 0.97));
  box-shadow: var(--shadow-card-sm, 0 12px 40px rgba(102, 126, 234, 0.2));
  color: var(--ra-text, #333);
  backdrop-filter: blur(12px);
  animation: read-aloud-panel-in 0.28s ease-out;
}

.read-aloud-panel-header {
  padding: 0.875rem 1rem;
  background: var(--ra-accent-gradient, var(--gradient-header, linear-gradient(135deg, #667eea 0%, #764ba2 100%)));
  color: var(--color-on-primary, white);
}

.read-aloud-panel-header h2 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
}

.read-aloud-panel-header p {
  margin: 0.15rem 0 0;
  font-size: 0.7rem;
  opacity: 0.85;
}

.read-aloud-panel-body {
  padding: 1rem;
  color: var(--ra-text);
}

.read-aloud-progress-meta {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--ra-text-muted);
  margin-bottom: 0.25rem;
}

.read-aloud-play-btn {
  background: var(--ra-accent-gradient, var(--gradient-header, linear-gradient(135deg, #667eea 0%, #764ba2 100%)));
  color: var(--color-on-primary, white);
  border: none;
}

.read-aloud-fab-idle {
  background: var(--ra-accent-gradient, var(--gradient-header, linear-gradient(135deg, #667eea 0%, #764ba2 100%)));
  color: var(--color-on-primary, white);
  border: none;
  box-shadow: 0 6px 28px var(--color-primary-soft, rgba(102, 126, 234, 0.4));
}

.read-aloud-fab {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 3.5rem;
  min-height: 3.5rem;
  border-radius: 50%;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.read-aloud-fab:hover {
  transform: scale(1.04);
}

.read-aloud-fab-badge {
  position: absolute;
  top: -0.2rem;
  right: -0.2rem;
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
  background: var(--ra-badge-bg, white);
  color: var(--ra-badge-text, #667eea);
  font-size: 0.55rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
}

.read-aloud-fab-ping {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: rgba(102, 126, 234, 0.35);
  animation: read-aloud-ping 1.5s ease-out infinite;
  pointer-events: none;
}

@keyframes read-aloud-ping {
  0% {
    transform: scale(1);
    opacity: 0.6;
  }
  100% {
    transform: scale(1.5);
    opacity: 0;
  }
}

.read-aloud-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.read-aloud-icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 2.75rem;
  min-height: 2.75rem;
  border-radius: 50%;
  border: 1px solid var(--ra-border);
  background: var(--ra-btn-bg);
  color: var(--ra-btn-text);
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s, color 0.2s;
}

.read-aloud-icon-btn:hover:not(:disabled) {
  border-color: var(--color-primary, #667eea);
  color: var(--color-primary, #667eea);
}

.read-aloud-icon-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.read-aloud-icon-btn-primary {
  min-width: 3.5rem;
  min-height: 3.5rem;
  border: none;
}

.read-aloud-mode-btns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.read-aloud-mode-btn {
  min-height: 2.75rem;
  padding: 0.5rem 0.75rem;
  border-radius: 0.75rem;
  border: 1px solid var(--ra-border);
  background: var(--ra-surface-muted);
  font-size: 0.75rem;
  font-weight: 500;
  cursor: pointer;
  color: var(--ra-btn-text);
  transition: all 0.2s;
}

.read-aloud-mode-btn.active {
  background: var(--ra-accent-gradient, var(--gradient-header, linear-gradient(135deg, #667eea 0%, #764ba2 100%)));
  color: var(--color-on-primary, white);
  border-color: transparent;
}

.read-aloud-progress-track {
  height: 0.5rem;
  border-radius: 999px;
  background: var(--ra-surface-muted);
  overflow: hidden;
}

.read-aloud-progress-fill {
  height: 100%;
  background: var(--ra-accent-gradient, linear-gradient(90deg, #667eea 0%, #764ba2 100%));
  transition: width 0.3s ease;
}

.read-aloud-hint {
  padding: 0.5rem 0.75rem;
  border-radius: 0.75rem;
  border: 1px solid var(--ra-hint-border);
  background: var(--ra-hint-bg);
  font-size: 0.75rem;
  color: var(--ra-hint-text);
}

.read-aloud-label {
  display: block;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--ra-text-muted);
  margin-bottom: 0.35rem;
}

.read-aloud-speed-btns {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.read-aloud-speed-btn {
  min-width: 3rem;
  min-height: 2.25rem;
  padding: 0.25rem 0.5rem;
  border-radius: 0.5rem;
  border: none;
  background: var(--ra-surface-muted);
  font-size: 0.75rem;
  font-weight: 500;
  cursor: pointer;
  color: var(--ra-btn-text);
}

.read-aloud-speed-btn.active {
  background: var(--color-primary, #667eea);
  color: var(--color-on-primary, white);
}

.read-aloud-select {
  width: 100%;
  min-height: 2.75rem;
  padding: 0.5rem 0.75rem;
  border-radius: 0.75rem;
  border: 1px solid var(--ra-border);
  font-size: 0.75rem;
  background: var(--ra-btn-bg);
  color: var(--ra-text);
}

.read-aloud-range {
  width: 100%;
  accent-color: #667eea;
}

.read-aloud-close {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 2.5rem;
  min-height: 2.5rem;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  color: white;
  cursor: pointer;
}

.read-aloud-panel-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.read-aloud-waves {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 0.2rem;
  height: 1.5rem;
  margin-top: 0.5rem;
}

.read-aloud-wave {
  width: 0.25rem;
  height: 0.5rem;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.7);
}

.read-aloud-wave.active {
  animation: read-aloud-wave 0.9s ease-in-out infinite;
}

@keyframes read-aloud-wave {
  0%,
  100% {
    height: 0.5rem;
  }
  25% {
    height: 1.35rem;
  }
  50% {
    height: 0.6rem;
  }
  75% {
    height: 1.1rem;
  }
}

@keyframes read-aloud-panel-in {
  from {
    opacity: 0;
    transform: translateY(16px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.read-aloud-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.read-aloud-footer-note {
  text-align: center;
  font-size: 0.625rem;
  color: var(--ra-text-faint);
  margin-top: 0.75rem;
  line-height: 1.4;
}

.read-aloud-divider {
  border-top: 1px solid var(--ra-border);
  margin-top: 0.75rem;
  padding-top: 0.75rem;
}

/* Keep listen panel readable — override page-wide dark rules */
html[data-color-mode='dark'] .read-aloud-root .read-aloud-panel-header h2,
html[data-color-mode='dark'] .read-aloud-root .read-aloud-panel-header p {
  color: var(--color-on-primary, #fff) !important;
}

html[data-color-mode='dark'] .read-aloud-root .read-aloud-icon-btn:not(.read-aloud-icon-btn-primary) {
  background: var(--ra-btn-bg) !important;
  color: var(--ra-btn-text) !important;
  border-color: var(--ra-border) !important;
}

@media (max-width: 480px) {
  .read-aloud-root {
    left: max(0.75rem, env(safe-area-inset-left, 0px));
    bottom: max(0.75rem, env(safe-area-inset-bottom, 0px));
  }
}
