:root {
  --read-aloud-size: 48px;
  --read-aloud-right: max(12px, env(safe-area-inset-right));
  --read-aloud-bottom: max(12px, env(safe-area-inset-bottom));
  --read-aloud-clearance: calc(var(--read-aloud-bottom) + var(--read-aloud-size) + 12px);
  --read-aloud-scroll-clearance: calc(var(--read-aloud-clearance) + var(--read-aloud-panel-height, 0px));
}

.read-aloud-floating { scroll-padding-block-end: var(--read-aloud-scroll-clearance); }
.read-aloud-floating body { padding-block-end: var(--read-aloud-scroll-clearance); }

.read-aloud-launcher,
.read-aloud-panel,
.read-aloud-keyboard:focus,
.read-aloud-notice[data-notice] {
  position: fixed;
  z-index: 10;
  border: 1px solid var(--island-line);
  background: #fffdfa;
  box-shadow: 0 4px 16px #38254a30;
  color: var(--island-purple-dark);
}

.read-aloud-launcher {
  right: var(--read-aloud-right);
  bottom: var(--read-aloud-bottom);
  display: grid;
  width: var(--read-aloud-size);
  height: var(--read-aloud-size);
  padding: 0;
  place-items: center;
  border-radius: 50%;
  cursor: pointer;
}

.read-aloud-launcher .island-icon { width: 24px; height: 24px; }

.read-aloud-launcher--active,
.read-aloud-launcher[aria-expanded="true"] {
  border-color: var(--island-purple-dark);
  background: var(--island-purple-dark);
  color: #fff;
}

.read-aloud-panel {
  right: var(--read-aloud-right);
  bottom: calc(var(--read-aloud-clearance) - 4px);
  display: grid;
  width: min(17rem, calc(100% - var(--read-aloud-right) - max(12px, env(safe-area-inset-left))));
  max-height: calc(100dvh - var(--read-aloud-clearance) - max(60px, env(safe-area-inset-top)));
  gap: 7px;
  padding: 9px;
  overflow: auto;
  overscroll-behavior: contain;
  border-radius: 17px;
}

.read-aloud-panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.3rem;
  padding-inline-start: 8px;
}

.read-aloud-panel-heading h2 {
  margin: 0;
  color: var(--island-purple-dark);
  font-size: 1.05rem;
}

.read-aloud-close,
.read-aloud-switch,
.read-aloud-stop,
.read-aloud-keyboard {
  min-height: 44px;
  min-width: 44px;
  padding: 0.35rem 0.65rem;
  border: 1px solid var(--island-line);
  border-radius: 10px;
  background: #fff;
  color: var(--island-purple-dark);
  font-size: 0.9rem;
  font-weight: 800;
  cursor: pointer;
}

.read-aloud-close {
  display: grid;
  place-items: center;
  padding: 0;
  border-color: transparent;
  background: var(--island-lilac);
}

.read-aloud-switch {
  position: relative;
  padding-inline-end: 3.7rem;
  text-align: start;
}

.read-aloud-switch::before,
.read-aloud-switch::after {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  border-radius: 100px;
  content: "";
}

.read-aloud-switch::before {
  right: 9px;
  width: 38px;
  height: 22px;
  border: 1px solid #886ba0;
  background: var(--island-lilac);
}

.read-aloud-switch::after {
  right: 29px;
  width: 14px;
  height: 14px;
  background: #745789;
}

.read-aloud-switch[aria-checked="true"] {
  border-color: #9d7fba;
  background: #f4ecfc;
}

.read-aloud-switch[aria-checked="true"]::before { background: var(--island-purple-dark); }
.read-aloud-switch[aria-checked="true"]::after { right: 13px; background: #fff; }

.read-aloud-panel :focus-visible { outline-offset: 1px; }

.read-aloud-panel-state {
  display: -webkit-box;
  margin: 1px 7px 3px;
  overflow: hidden;
  color: var(--island-muted);
  font-size: 0.8rem;
  overflow-wrap: anywhere;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}

.read-aloud-notice[data-notice],
.read-aloud-keyboard:focus {
  bottom: var(--read-aloud-bottom);
  left: max(12px, env(safe-area-inset-left));
  width: auto;
  height: auto;
  clip-path: none;
}

.read-aloud-keyboard:focus { padding: 0.35rem 0.65rem; }

.settings-page .read-aloud-keyboard:focus {
  position: relative;
  inset: auto;
  margin: 0.5rem;
}

.read-aloud-notice[data-notice] {
  max-width: min(15rem, calc(100% - var(--read-aloud-clearance) - 12px));
  margin: 0;
  padding: 0.55rem 0.7rem;
  border-radius: 10px;
  font-size: 0.86rem;
  pointer-events: none;
  white-space: normal;
}

.read-aloud-keyboard:focus ~ .read-aloud-notice[data-notice] { bottom: var(--read-aloud-clearance); }

.read-aloud-launcher[hidden],
.read-aloud-panel[hidden],
.read-aloud-stop[hidden],
.read-aloud-keyboard[hidden] { display: none; }

:where(h1, h2, h3, h4, h5, h6, p, th, td, dt, dd, legend, [role="img"], [data-read-aloud-text]):not([data-read-aloud-ignore] *) {
  cursor: pointer;
}

.read-aloud-mode :where(a[href], button, summary, label, select, input, textarea) { cursor: pointer; }

[data-read-aloud-playing], [data-read-aloud-selected] {
  outline: 3px solid #8c4fbc;
  outline-offset: 2px;
  scroll-margin-block: 12px;
}

[data-read-aloud-selected]:not([data-read-aloud-playing]) { outline-style: dashed; }
