/* PeerLoop's styles: tokens, components, screens, then accessibility queries. */

/* --- tokens ------------------------------------------------------------------
   Dark first. Each colour is a light-dark() pair of oklch() values, so the
   theme follows the system unless Settings forces one (html[data-theme]).
   web/lib/tokens.test.js holds every text pair to 4.5:1 in both themes. */
:root {
  color-scheme: dark light;
  --bg: light-dark(oklch(97% 0.012 260), oklch(15% 0.025 268));
  --surface-solid: light-dark(oklch(99.5% 0.004 260), oklch(21% 0.03 268));
  --raised: light-dark(oklch(94% 0.015 260), oklch(27% 0.035 268));
  --text: light-dark(oklch(22% 0.03 268), oklch(96% 0.01 260));
  --text-muted: light-dark(oklch(44% 0.03 268), oklch(76% 0.025 262));
  --line: light-dark(oklch(88% 0.02 262), oklch(36% 0.035 268));
  --accent: light-dark(oklch(50% 0.16 255), oklch(80% 0.13 195));
  --accent-2: light-dark(oklch(52% 0.2 300), oklch(72% 0.17 300));
  --on-accent: light-dark(oklch(99% 0 0), oklch(17% 0.03 268));
  --good: light-dark(oklch(48% 0.12 155), oklch(82% 0.15 155));
  --warn: light-dark(oklch(50% 0.11 70), oklch(85% 0.14 85));
  --danger: light-dark(oklch(54% 0.2 25), oklch(64% 0.21 25));
  --danger-text: light-dark(oklch(50% 0.19 25), oklch(78% 0.14 25));
  --on-danger: light-dark(oklch(99% 0 0), oklch(15% 0.02 25));
  /* Glass: translucent tints over the aurora. Text on glass must also pass on
     --surface-solid, which is what glass becomes when transparency is reduced. */
  --glass: light-dark(oklch(99% 0.005 260 / 0.66), oklch(22% 0.03 268 / 0.58));
  --glass-strong: light-dark(oklch(99% 0.005 260 / 0.86), oklch(18% 0.03 268 / 0.82));
  --glass-edge: light-dark(oklch(100% 0 0 / 0.7), oklch(100% 0 0 / 0.12));
  --glass-highlight: light-dark(oklch(100% 0 0 / 0.9), oklch(100% 0 0 / 0.16));
  --shadow: light-dark(oklch(40% 0.05 268 / 0.16), oklch(0% 0 0 / 0.45));
  --radius: 20px;
  --radius-sm: 12px;
  --tap: 44px; /* WCAG 2.5.8 and the platforms' own minimum */
  --ease-out: cubic-bezier(.2, .8, .2, 1);
  --ease-spring: linear(0, .25 7%, .56 15%, .82 24%, .97 33%, 1.03 42%, 1.02 55%, 1);
  --fast: 150ms;
  --normal: 240ms;
}
:root[data-theme="dark"] { color-scheme: dark; }
:root[data-theme="light"] { color-scheme: light; }

/* --- glass ---------------------------------------------------------------------- */
.glass {
  background: var(--glass);
  -webkit-backdrop-filter: blur(24px) saturate(1.6);
  backdrop-filter: blur(24px) saturate(1.6);
  border: 1px solid var(--glass-edge);
  border-radius: var(--radius);
  box-shadow: inset 0 1px 0 var(--glass-highlight), 0 18px 48px var(--shadow);
}
:root[data-transparency="reduce"] .glass { background: var(--surface-solid); -webkit-backdrop-filter: none; backdrop-filter: none; }
@media (prefers-reduced-transparency: reduce) {
  :root:not([data-transparency="full"]) .glass { background: var(--surface-solid); -webkit-backdrop-filter: none; backdrop-filter: none; }
}
@media (prefers-contrast: more) {
  .glass { background: var(--surface-solid); -webkit-backdrop-filter: none; backdrop-filter: none; border: 2px solid var(--text); box-shadow: none; }
}
@media (forced-colors: active) {
  .glass { background: Canvas; color: CanvasText; border: 1px solid CanvasText; -webkit-backdrop-filter: none; backdrop-filter: none; }
}


/* --- base -------------------------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
/* The page colour is the root's: body is a query container, which stops its
   background spreading to the canvas, and it would then cover the aurora. */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; background: var(--bg); }
body {
  margin: 0;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  color: var(--text);
  font: 400 1rem/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  /* Edge to edge: every side keeps clear of notches and the home indicator. */
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  overflow-x: clip;
  /* Layout follows the space the app has (a split-screen window, an iframe),
     not the device: main's layout rules query this container. */
  container: app / inline-size;
}
h1, h2, h3 { margin: 0; line-height: 1.15; text-wrap: balance; }
h1 { font-size: clamp(1.75rem, 1.2rem + 3vw, 2.9rem); letter-spacing: -0.02em; font-weight: 700; }
h2 { font-size: 1.15rem; font-weight: 650; }
h3 { font-size: .8rem; font-weight: 650; text-transform: uppercase; letter-spacing: .08em; color: var(--text-muted); }
p { margin: 0; }
code { font-family: ui-monospace, "Cascadia Code", Menlo, monospace; font-size: .92em; }
a { color: var(--accent); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
[data-screen-heading]:focus { outline: none; }
.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.icon { width: 1.25em; height: 1.25em; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* --- the aurora behind everything ----------------------------------------------------------- */
.aurora {
  position: fixed; inset: -25vmax; z-index: -1; pointer-events: none;
  background:
    radial-gradient(38% 42% at 22% 30%, color-mix(in oklch, var(--accent) 60%, transparent), transparent 70%),
    radial-gradient(40% 46% at 78% 22%, color-mix(in oklch, var(--accent-2) 58%, transparent), transparent 72%),
    radial-gradient(46% 40% at 55% 88%, color-mix(in oklch, var(--accent) 22%, color-mix(in oklch, var(--accent-2) 22%, transparent)), transparent 70%);
  filter: blur(48px) saturate(1.15);
  animation: drift 48s ease-in-out infinite alternate;
  transition: opacity var(--normal) var(--ease-out);
}
@keyframes drift {
  50% { transform: rotate(8deg) scale(1.08) translate(2%, -1%); }
  100% { transform: rotate(-6deg) scale(1.12) translate(-2%, 2%); }
}
/* Calm during a call: the video is the picture. */
:root[data-screen="call"] .aurora { animation-play-state: paused; opacity: .35; }

/* --- controls ---------------------------------------------------------------------- */
button, input, select, textarea { font: inherit; color: inherit; }
button {
  display: inline-flex; align-items: center; justify-content: center; gap: .5em;
  min-height: var(--tap); padding: .55rem 1.1rem;
  border: 1px solid transparent; border-radius: 999px;
  cursor: pointer; font-weight: 600; line-height: 1.1;
  transition: background var(--fast), border-color var(--fast), transform var(--fast) var(--ease-spring), opacity var(--fast);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
button:active:not(:disabled) { transform: scale(.96); }
button:disabled { opacity: .45; cursor: default; }
.primary { background: var(--accent); color: var(--on-accent); }
.primary:hover:not(:disabled) { background: color-mix(in oklch, var(--accent) 88%, var(--text)); }
.secondary { background: color-mix(in oklch, var(--text) 8%, transparent); color: var(--text); border-color: var(--line); }
.secondary:hover:not(:disabled) { background: color-mix(in oklch, var(--text) 14%, transparent); }
.big { min-height: 3.25rem; padding-inline: 1.5rem; font-size: 1.05rem; }
.icon-button { width: var(--tap); padding: 0; background: transparent; color: var(--text); }
.icon-button:hover { background: color-mix(in oklch, var(--text) 10%, transparent); }
.icon-only { width: var(--tap); padding: 0; }
.chip {
  min-height: var(--tap); padding: .35rem .95rem; font-size: .9rem; font-weight: 550;
  background: color-mix(in oklch, var(--good) 14%, transparent); color: var(--text);
  border-color: color-mix(in oklch, var(--good) 35%, transparent);
}
.chip .icon { color: var(--good); }
label { display: block; font-size: .9rem; font-weight: 550; margin-bottom: .35rem; }
input:not([type="checkbox"], [type="radio"]) {
  width: 100%; min-width: 0; min-height: var(--tap); padding: .6rem .9rem;
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  background: color-mix(in oklch, var(--surface-solid) 70%, transparent);
  text-overflow: ellipsis;
}
input::placeholder { color: var(--text-muted); opacity: 1; }
input:focus-visible { outline-offset: 0; }
input[aria-invalid="true"] { border-color: var(--danger-text); }
input[readonly] { color: var(--text-muted); }
.field-row { display: flex; gap: .5rem; align-items: stretch; }
.field-row input { flex: 1; }
.field-row button { flex: none; }
.actions { display: flex; flex-wrap: wrap; gap: .6rem; align-items: center; }
.hint { color: var(--text-muted); font-size: .88rem; }
.error { color: var(--danger-text); font-weight: 550; }
.status { font-weight: 600; }
.eyebrow { display: inline-flex; gap: .45rem; align-items: center; font-size: .8rem; font-weight: 650; letter-spacing: .08em; text-transform: uppercase; color: var(--accent); }
.pill { font-size: .78rem; font-weight: 650; padding: .2rem .65rem; border-radius: 999px; background: color-mix(in oklch, var(--good) 18%, transparent); color: var(--text); }
:root[data-net="offline"] .pill { background: var(--danger); color: var(--on-danger); }
:root[data-net="unknown"] .pill { background: color-mix(in oklch, var(--text) 10%, transparent); }

/* Options: a checkbox with its explanation, the whole row a target. */
.option { display: flex; gap: .8rem; align-items: flex-start; padding: .8rem; margin: 0 0 .5rem; border-radius: var(--radius-sm); cursor: pointer; font-weight: 400; }
.option:hover { background: color-mix(in oklch, var(--text) 6%, transparent); }
.option input { width: 1.35rem; height: 1.35rem; margin: .1rem 0 0; flex: none; accent-color: var(--accent); }
.option strong { display: block; font-weight: 600; }
.option small { display: block; color: var(--text-muted); font-size: .86rem; margin-top: .15rem; }
.segmented { display: flex; gap: .25rem; padding: .25rem; margin: 0 0 .75rem; border: 1px solid var(--line); border-radius: 999px; }
.segmented legend { font-size: .9rem; font-weight: 550; padding: 0 .4rem; }
.segmented label { flex: 1; margin: 0; position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.segmented span { display: grid; place-items: center; min-height: var(--tap); border-radius: 999px; font-weight: 600; }
.segmented input:checked + span { background: var(--accent); color: var(--on-accent); }
.segmented input:focus-visible + span { outline: 3px solid var(--accent); outline-offset: 2px; }
details > summary { cursor: pointer; min-height: var(--tap); display: flex; align-items: center; font-weight: 600; list-style: none; }
details > summary::-webkit-details-marker { display: none; }
details > summary::before { content: "›"; display: inline-block; width: 1.2em; transition: transform var(--fast); }
details[open] > summary::before { transform: rotate(90deg); }

/* --- top bar ------------------------------------------------------------------------ */
.topbar {
  position: sticky; top: max(.5rem, env(safe-area-inset-top)); z-index: 10;
  display: flex; align-items: center; gap: .4rem;
  margin: .5rem clamp(.5rem, 2vw, 1rem) 0; padding: .3rem .4rem .3rem .9rem;
  border-radius: 999px;
}
.brand { display: flex; align-items: center; gap: .55rem; font-weight: 700; letter-spacing: -.01em; flex: 1; min-width: 0; }
.brand img { border-radius: 8px; }

/* --- screens ----------------------------------------------------------------------- */
main { flex: 1; display: grid; align-content: start; justify-items: center; gap: 1rem; padding: clamp(.75rem, 3vw, 2rem) clamp(.75rem, 3vw, 1.5rem) 2rem; min-width: 0; }
.screen { width: min(100%, 44rem); min-width: 0; display: grid; gap: 1rem; view-transition-name: screen; }
.screen > *, .chat { min-width: 0; }
.glass { padding: clamp(1rem, 3.5vw, 1.75rem); }
.topbar.glass { padding: .3rem .4rem .3rem .9rem; }

/* Home */
.hero { display: grid; gap: 1.1rem; }
.hero .lede { font-size: 1.05rem; color: var(--text-muted); max-width: 38rem; }
.join { margin-top: .25rem; }
.hero .chip { justify-self: start; }
.features { list-style: none; margin: 0; padding: 0; display: grid; gap: .75rem; grid-template-columns: repeat(auto-fit, minmax(min(12rem, 100%), 1fr)); }
.features li { display: flex; gap: .75rem; align-items: flex-start; padding: 1rem; font-size: .92rem; color: var(--text-muted); border-radius: var(--radius-sm); }
.features .icon { color: var(--accent); margin-top: .15rem; }
.features strong { display: block; color: var(--text); }

/* Room: waiting for the other device, which is also the device check */
.room { width: min(100%, 64rem); }
@container app (min-width: 820px) {
  .room { grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); align-items: start; }
}
.invite { display: grid; gap: .9rem; }
.notice { padding: .7rem .9rem; border-radius: var(--radius-sm); background: color-mix(in oklch, var(--warn) 14%, transparent); font-weight: 550; }
.qr { margin: 0; display: flex; gap: 1rem; align-items: center; }
.qr svg { width: 9.5rem; height: 9.5rem; flex: none; border-radius: var(--radius-sm); background: #fff; }
.qr path { fill: #000; }
.qr figcaption { color: var(--text-muted); font-size: .9rem; }
.preview { display: grid; gap: .8rem; }
.preview-stage { position: relative; aspect-ratio: 4 / 3; border-radius: var(--radius-sm); overflow: hidden; background: oklch(12% 0.02 268); }
.preview-stage video { width: 100%; height: 100%; object-fit: cover; display: block; }
.preview-stage video.mirror { transform: scaleX(-1); }
.preview-off { position: absolute; inset: 0; display: grid; place-items: center; padding: 1.5rem; text-align: center; color: oklch(86% 0.02 262); }
.preview-stage:has(video:not([hidden])) .preview-off { display: none; }
.pre-controls { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.pre-controls .ctl[aria-pressed="false"] { background: color-mix(in oklch, var(--danger) 22%, transparent); }
.pre-controls .secondary { margin-left: auto; }

/* The devices popover, anchored where it can be. */
.popover { margin: auto; width: min(24rem, calc(100vw - 2rem)); color: var(--text); background: var(--glass-strong); display: none; gap: .35rem; }
.popover:popover-open { display: grid; }
.popover h2 { margin-bottom: .4rem; }
select { width: 100%; min-height: var(--tap); padding: .5rem .8rem; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--surface-solid); color: var(--text); margin-bottom: .5rem; }
@supports (appearance: base-select) {
  select { appearance: base-select; }
  select::picker(select) { border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--surface-solid); color: var(--text); }
}
.invite .status { font-size: 1.1rem; }
:root[data-room="lost"] .invite .status, :root[data-room="full"] .invite .status { color: var(--danger-text); }

/* Direct connection */
.direct-card { display: grid; gap: .9rem; }
.stepper { display: flex; gap: .5rem; list-style: none; margin: 0; padding: 0; counter-reset: step; }
.stepper li { flex: 1; counter-increment: step; display: flex; align-items: center; gap: .5rem; font-size: .85rem; font-weight: 600; color: var(--text-muted); padding-bottom: .5rem; border-bottom: 3px solid var(--line); }
.stepper li::before { content: counter(step); display: grid; place-items: center; width: 1.6rem; height: 1.6rem; border-radius: 50%; border: 2px solid currentColor; font-size: .8rem; flex: none; }
:root[data-screen="direct-invite"] [data-step="invite"],
:root[data-screen="direct-reply"] [data-step="reply"],
:root[data-screen="direct-verify"] [data-step="verify"] { color: var(--text); border-color: var(--accent); }
:root[data-screen="direct-verify"] [data-step]:not([data-step="verify"]),
:root[data-screen="direct-reply"] [data-step="invite"] { color: var(--good); border-color: color-mix(in oklch, var(--good) 60%, transparent); }
.safety { display: grid; gap: .4rem; justify-items: start; padding: 1rem; border-radius: var(--radius-sm); background: color-mix(in oklch, var(--accent) 10%, transparent); }
.qr-toggle summary { font-weight: 550; color: var(--text-muted); }
.qr-toggle .qr { margin-top: .5rem; }
#direct-qr { width: min(18rem, 100%); height: auto; aspect-ratio: 1; }
.verified { display: flex; gap: .5rem; align-items: center; color: var(--good); font-weight: 650; }
.danger-notice { background: color-mix(in oklch, var(--danger) 14%, transparent); display: grid; gap: .6rem; }
:root[data-verified="yes"] [data-step="verify"] { color: var(--good); border-color: var(--good); }
.code { font: 700 clamp(1.6rem, 1rem + 3vw, 2.4rem)/1.1 ui-monospace, "Cascadia Code", Menlo, monospace; letter-spacing: .12em; font-variant-numeric: tabular-nums; word-spacing: .4em; }

/* --- the call ---------------------------------------------------------------------- */
:root[data-screen="call"] main { padding: .5rem clamp(.5rem, 2vw, 1rem) .75rem; }
.call { width: 100%; position: relative; grid-template-rows: 1fr; min-height: 0; }
/* On a very wide screen the stage stays 16:9 of the height it has, centred:
   no letterbox bars wider than the picture. */
.call > .stage { width: min(100%, calc((100dvh - 6.5rem) * 16 / 9)); justify-self: center; }
.stage {
  position: relative; overflow: hidden; border-radius: var(--radius);
  background: oklch(10% 0.02 268);
  min-height: 12rem; height: calc(100dvh - 6.5rem - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  container-type: size;
  /* Room left for the quality chip (top), the controls (bottom) and, on
     landscape phones, the controls' rail (right). */
  --edge-t: 4.25rem; --edge-b: 6.25rem; --edge-r: .75rem;
}
.stage > video:not(.self) { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.stage-note { position: absolute; inset: 0; display: grid; place-items: center; padding: 2rem; text-align: center; color: oklch(88% 0.02 262); font-size: 1.05rem; }
.inset { position: absolute; right: var(--edge-r); bottom: var(--edge-b); width: min(30%, 18rem); aspect-ratio: 16 / 9; padding: 0; border-radius: var(--radius-sm); overflow: hidden; background: #000; border: 2px solid oklch(100% 0 0 / .35); box-shadow: 0 10px 30px oklch(0% 0 0 / .45); }
.inset:has(> video[hidden]) { display: none; }
.inset video { width: 100%; height: 100%; object-fit: cover; }
.self { position: absolute; right: .75rem; top: .75rem; width: min(22%, 12rem); max-height: 40%; aspect-ratio: 3 / 4; object-fit: cover; border-radius: var(--radius-sm); border: 2px solid oklch(100% 0 0 / .35); box-shadow: 0 10px 30px oklch(0% 0 0 / .45); background: #000; }
.self.mirror { transform: scaleX(-1); }
.reconnecting { position: absolute; inset: 0; display: none; place-content: center; justify-items: center; gap: .8rem; color: #fff; background: oklch(10% 0.02 268 / .55); -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px); font-weight: 600; }
:root[data-overlay="reconnecting"] .reconnecting { display: grid; }
.spinner { width: 2.4rem; height: 2.4rem; border-radius: 50%; border: 3px solid oklch(100% 0 0 / .25); border-top-color: #fff; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(1turn); } }

.controls {
  position: absolute; left: 50%; bottom: 1rem; translate: -50% 0; z-index: 3;
  display: flex; gap: .4rem; align-items: center; padding: .45rem; border-radius: 999px;
  background: var(--glass-strong); max-width: calc(100% - 1rem); overflow-x: auto; scrollbar-width: none;
}
.ctl { position: relative; width: 3.25rem; height: 3.25rem; padding: 0; flex: none; background: color-mix(in oklch, var(--text) 10%, transparent); color: var(--text); }
.ctl .label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.ctl .icon { width: 1.4rem; height: 1.4rem; }
.ctl .icon.off { display: none; }
.ctl[aria-pressed="false"] .icon.on { display: none; }
.ctl[aria-pressed="false"] .icon.off { display: block; }
#mic[aria-pressed="false"], #cam[aria-pressed="false"] { background: color-mix(in oklch, var(--danger) 22%, transparent); }
.ctl[aria-pressed="true"] { background: color-mix(in oklch, var(--text) 16%, transparent); }
#screen[aria-pressed="true"], #self-view[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }
.ctl.danger { width: 4rem; background: var(--danger); color: var(--on-danger); margin-left: .35rem; }
.ctl.attention { background: var(--accent); color: var(--on-accent); }
.ctl:hover:not(:disabled) { filter: brightness(1.12); }
.ctl:disabled { opacity: .4; }
/* The mic's level, as a bar under the button's icon: white silent, yellow
   quiet, green good, red too loud. */
.level { position: absolute; left: .55rem; right: .55rem; bottom: .45rem; height: .22rem; border-radius: 999px; background: color-mix(in oklch, var(--text) 18%, transparent); overflow: hidden; }
.level > span { display: block; height: 100%; width: 0; border-radius: inherit; transition: width 60ms linear; }
.level[data-zone="silent"] > span { background: oklch(97% 0 0); }
.level[data-zone="quiet"] > span { background: oklch(84% 0.16 90); }
.level[data-zone="good"] > span { background: oklch(74% 0.18 150); }
.level[data-zone="hot"] > span { background: oklch(64% 0.22 25); }

.layouts { position: absolute; top: .75rem; left: .75rem; z-index: 3; display: flex; flex-wrap: wrap; gap: .3rem; padding: .3rem; border-radius: 999px; background: var(--glass-strong); }
.layouts button { min-height: var(--tap); padding: .3rem .8rem; font-size: .85rem; background: transparent; color: var(--text); }
.layouts button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }
.error.floating { position: absolute; left: 50%; bottom: 6rem; translate: -50% 0; z-index: 3; max-width: min(34rem, calc(100% - 2rem)); padding: .7rem 1rem; border-radius: var(--radius-sm); background: var(--surface-solid); box-shadow: 0 10px 30px var(--shadow); }

/* The other side's video, filled out when its shape differs from the stage's. */
.fill { position: absolute; inset: -10%; width: 120%; height: 120%; object-fit: cover; filter: blur(32px) brightness(.55) saturate(1.2); }
/* The self-view: dragged anywhere, snapped to a corner (data-corner). */
.self { touch-action: none; cursor: grab; transition: top var(--normal) var(--ease-spring), left var(--normal) var(--ease-spring), right var(--normal) var(--ease-spring), bottom var(--normal) var(--ease-spring); }
.self.dragging { cursor: grabbing; transition: none; }
.self[data-corner="top-left"] { top: var(--edge-t); left: .75rem; right: auto; bottom: auto; }
.self[data-corner="top-right"] { top: var(--edge-t); right: var(--edge-r); left: auto; bottom: auto; }
.self[data-corner="bottom-left"] { bottom: var(--edge-b); left: .75rem; top: auto; right: auto; }
.self[data-corner="bottom-right"] { bottom: var(--edge-b); right: var(--edge-r); top: auto; left: auto; }
.self[data-corner="top-left"] ~ .quality { left: auto; right: .75rem; }

/* Quality: bars, and words only when it is poor. */
.quality { position: absolute; top: .75rem; left: .75rem; z-index: 3; min-height: var(--tap); padding: .35rem .8rem; gap: .5rem; border-radius: 999px; background: var(--glass-strong); color: var(--text); anchor-name: --quality; }
.bars { display: inline-flex; gap: 3px; align-items: flex-end; height: 1rem; }
.bars i { width: 4px; border-radius: 2px; background: color-mix(in oklch, var(--text) 30%, transparent); }
.bars i:nth-child(1) { height: 40%; }
.bars i:nth-child(2) { height: 70%; }
.bars i:nth-child(3) { height: 100%; }
.bars[data-bars="3"] i, .bars[data-bars="2"] i:nth-child(-n+2), .bars[data-bars="1"] i:first-child { background: var(--good); }
.bars[data-bars="1"] i:first-child { background: var(--warn); }
.bars[data-bars="0"] i:first-child { background: var(--danger-text); }
#quality-text:empty { display: none; }
#quality-text { font-size: .85rem; font-weight: 600; }
.popover.anchored { margin: 0; position: fixed; }
@supports (anchor-name: --a) {
  .popover.anchored { position-anchor: --quality; top: calc(anchor(bottom) + .4rem); left: anchor(left); }
}
@supports not (anchor-name: --a) {
  .popover.anchored { margin: auto; }
}
.popover.anchored p { margin: .2rem 0; }

.reconnecting .actions { justify-content: center; }
.banner { position: absolute; top: .75rem; left: 50%; translate: -50% 0; z-index: 4; max-width: calc(100% - 2rem); padding: .6rem 1rem; border-radius: 999px; background: var(--danger); color: var(--on-danger); font-weight: 600; text-align: center; }

/* The controls step aside while the video plays (data-idle), and come back
   on any touch, movement or key. Never while one of them has focus. */
.controls, .quality, .layouts { transition: opacity var(--normal) var(--ease-out), translate var(--normal) var(--ease-out); }
.call[data-idle] .controls:not(:focus-within), .call[data-idle] .quality:not(:focus), .call[data-idle] .layouts:not(:focus-within) { opacity: 0; pointer-events: none; }
.call[data-idle] .stage { cursor: none; }
.unread { position: absolute; top: .3rem; right: .3rem; min-width: 1.1rem; height: 1.1rem; padding: 0 .3rem; border-radius: 999px; background: var(--danger); color: var(--on-danger); font-size: .7rem; font-weight: 700; display: grid; place-items: center; }
#chat-toggle[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }

/* The More menu. */
.menu { position: fixed; margin: auto; width: min(20rem, calc(100vw - 2rem)); padding: .5rem; gap: .15rem; }
.menu-item { width: 100%; justify-content: flex-start; gap: .75rem; border-radius: var(--radius-sm); background: transparent; color: var(--text); font-weight: 550; }
.menu-item:hover { background: color-mix(in oklch, var(--text) 8%, transparent); }
.menu-item[aria-pressed="true"]::after { content: "✓"; margin-left: auto; color: var(--accent); }
.menu .hint { padding: .5rem .75rem; }
kbd { font: 600 .8em ui-monospace, monospace; padding: .05rem .35rem; border: 1px solid var(--line); border-radius: 5px; }
.danger-fill { background: var(--danger); color: var(--on-danger); }
#privacy-applies { display: flex; flex-wrap: wrap; gap: .6rem; align-items: center; justify-content: space-between; }

/* --- chat -------------------------------------------------------------------------- */
.chat { position: relative; width: min(100%, 44rem); display: grid; grid-template-rows: auto 1fr auto auto auto auto; gap: .5rem; min-height: 0; }
.chat-head { display: flex; align-items: center; justify-content: space-between; margin: -.4rem -.4rem 0 0; }
#chat-close { display: none; }
#chat-log { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .35rem; overflow-y: auto; max-height: 22rem; overscroll-behavior: contain; }
#chat-log li { max-width: 80%; padding: .5rem .8rem; border-radius: 18px 18px 18px 6px; background: var(--raised); overflow-wrap: anywhere; white-space: pre-wrap; animation: pop var(--normal) var(--ease-spring); }
#chat-log li.mine { align-self: flex-end; border-radius: 18px 18px 6px 18px; background: var(--accent); color: var(--on-accent); }
/* A group's time above its first message; ticks after mine (sent, delivered). */
#chat-log li[data-time] { margin-top: 1.4rem; position: relative; }
#chat-log li[data-time]::before { content: attr(data-time); position: absolute; top: -1.25rem; left: .2rem; font-size: .72rem; color: var(--text-muted); white-space: nowrap; }
#chat-log li.mine[data-time]::before { left: auto; right: .2rem; }
#chat-log li.mine::after { content: " ✓"; font-size: .75em; opacity: .75; }
#chat-log li.mine[data-state="delivered"]::after { content: " ✓✓"; }
/* Very long messages fold; a click or Enter unfolds them. */
#chat-log li.long:not(.expanded) { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 12; line-clamp: 12; overflow: hidden; cursor: pointer; mask-image: linear-gradient(#000 80%, transparent); }
.pill-button { position: absolute; left: 50%; bottom: 5rem; translate: -50% 0; z-index: 2; min-height: var(--tap); padding: .3rem .9rem; font-size: .85rem; background: var(--accent); color: var(--on-accent); box-shadow: 0 6px 20px var(--shadow); }
.typing { font-style: italic; }
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(.98); } }
.composer { display: flex; gap: .5rem; align-items: flex-end; }
.composer textarea {
  flex: 1; min-width: 0; min-height: var(--tap); max-height: 10rem; padding: .6rem 1rem; resize: none;
  border: 1px solid var(--line); border-radius: 22px; background: color-mix(in oklch, var(--surface-solid) 70%, transparent);
  font: inherit; line-height: 1.4; field-sizing: content;
}
.composer textarea::placeholder { color: var(--text-muted); opacity: 1; }

/* In a call: the chat is a panel beside the stage when there is room... */
@container app (min-width: 900px) {
  :root[data-screen="call"] main { grid-template-columns: minmax(0, 1fr) minmax(18rem, 24rem); align-items: stretch; }
  :root[data-screen="call"] .chat { width: auto; height: calc(100dvh - 6.5rem - env(safe-area-inset-top) - env(safe-area-inset-bottom)); }
  :root[data-screen="call"] #chat-log { max-height: none; }
}
/* ...closed, it gives the stage the whole width... */
@container app (min-width: 900px) {
  :root[data-screen="call"][data-chat="closed"] main { grid-template-columns: minmax(0, 1fr); }
  :root[data-screen="call"][data-chat="closed"] .chat { display: none; }
}
/* ...and on a narrow screen it is a sheet over the stage, closed until wanted
   (closed means moved away and inert, never hidden: it keeps its messages). */
@container app (max-width: 899px) {
  :root[data-screen="call"] .chat {
    position: fixed; z-index: 6; width: auto;
    left: calc(.5rem + env(safe-area-inset-left)); right: calc(.5rem + env(safe-area-inset-right));
    /* Above (or, on a landscape phone, beside) the controls: app.js measures them. */
    bottom: var(--sheet-bottom, calc(6rem + env(safe-area-inset-bottom))); height: min(64dvh, 34rem);
    background: var(--glass-strong); box-shadow: 0 -10px 40px var(--shadow);
    transition: transform var(--normal) var(--ease-out), visibility var(--normal) allow-discrete;
  }
  :root[data-screen="call"][data-chat="closed"] .chat { transform: translateY(calc(100% + 1.5rem)); visibility: hidden; }
  :root[data-screen="call"] #chat-log { max-height: none; }
  :root[data-screen="call"] #chat-close { display: inline-flex; }
}
@media (max-height: 500px) and (orientation: landscape) {
  :root[data-screen="call"] .chat { top: calc(.5rem + env(safe-area-inset-top)); bottom: var(--sheet-bottom, calc(.5rem + env(safe-area-inset-bottom))); height: auto; left: 35%; right: var(--sheet-right, calc(6rem + env(safe-area-inset-right))); }
}

/* --- sheets (dialogs) --------------------------------------------------------------- */
dialog.sheet {
  width: min(34rem, calc(100vw - 2rem)); max-height: min(88dvh, 46rem); padding: 0; border: 0;
  background: transparent; color: var(--text); overflow: visible;
  opacity: 1; transform: none;
  transition: opacity var(--normal) var(--ease-out), transform var(--normal) var(--ease-spring), overlay var(--normal) allow-discrete, display var(--normal) allow-discrete;
}
dialog.sheet:not([open]) { opacity: 0; transform: translateY(12px) scale(.98); }
@starting-style { dialog.sheet[open] { opacity: 0; transform: translateY(12px) scale(.98); } }
dialog.sheet::backdrop { background: oklch(10% 0.02 268 / .45); -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px); }
.sheet-body { display: grid; gap: 1.25rem; max-height: inherit; overflow-y: auto; overscroll-behavior: contain; background: var(--glass-strong); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin: -.4rem -.4rem 0 0; }
.sheet section { display: grid; gap: .6rem; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; margin: .6rem 0; }
#own-relay-form { display: grid; gap: .25rem; }
#own-relay-form .actions { margin-top: .5rem; }
#badge { justify-self: start; display: inline-block; font-weight: 650; padding: .35rem .85rem; border-radius: 999px; }
#badge[data-tone="good"] { color: var(--good); background: color-mix(in oklch, var(--good) 16%, transparent); }
#badge[data-tone="warn"] { color: var(--warn); background: color-mix(in oklch, var(--warn) 16%, transparent); }
#badge[data-tone="wait"] { color: var(--text-muted); background: color-mix(in oklch, var(--text) 10%, transparent); }
dl { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1rem; margin: .5rem 0 0; }
dt { color: var(--text-muted); }
dd { margin: 0; font-variant-numeric: tabular-nums; }
#nat { margin: 0; padding-left: 1.1rem; }
#log { font: .75rem/1.45 ui-monospace, Menlo, monospace; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 16rem; overflow-y: auto; margin: .5rem 0; padding: .75rem; border-radius: var(--radius-sm); background: color-mix(in oklch, var(--text) 6%, transparent); }

/* Phones: sheets come up from the bottom edge. */
@media (max-width: 599px) {
  dialog.sheet { width: 100%; max-width: 100%; margin: auto 0 0; max-height: 92dvh; }
  dialog.sheet:not([open]), dialog.sheet[open] { --from: translateY(100%); }
  dialog.sheet:not([open]) { transform: translateY(100%); }
  @starting-style { dialog.sheet[open] { transform: translateY(100%); } }
  .sheet-body { border-radius: var(--radius) var(--radius) 0 0; padding-bottom: calc(1.25rem + env(safe-area-inset-bottom)); }
  .two { grid-template-columns: 1fr; }
}

/* --- toast (install) ---------------------------------------------------------------- */
.toast { position: fixed; z-index: 20; left: 50%; bottom: calc(1rem + env(safe-area-inset-bottom)); translate: -50% 0; width: min(34rem, calc(100% - 1.5rem)); display: flex; flex-wrap: wrap; gap: .6rem; align-items: center; padding: .8rem 1rem; background: var(--glass-strong); }
.toast span { flex: 1 1 14rem; }

/* --- small and short screens ----------------------------------------------------------- */
/* Narrow: the call buttons tighten (still 44 px), and below 380 px screen
   sharing moves into More so the pill never scrolls. */
#screen-more { display: none; }
@container app (max-width: 520px) {
  .controls { gap: .25rem; padding: .35rem; }
  .ctl { width: 2.85rem; height: 2.85rem; }
  .ctl.danger { width: 3.3rem; margin-left: .2rem; }
}
@container app (max-width: 380px) {
  .controls #screen { display: none; }
  #screen-more:not([hidden]) { display: flex; }
}
@media (max-width: 380px) {
  .qr { flex-direction: column; align-items: flex-start; }
  .field-row { flex-wrap: wrap; }
  .field-row input { flex-basis: 100%; }
}
/* Landscape phones: the controls become a rail on the right, the bar gives up its height. */
@media (max-height: 500px) and (orientation: landscape) {
  :root[data-screen="call"] .topbar { display: none; }
  :root[data-screen="call"] .stage { height: calc(100dvh - 1rem - env(safe-area-inset-top) - env(safe-area-inset-bottom)); }
  :root[data-screen="call"] .controls { left: auto; right: .5rem; top: 50%; bottom: auto; translate: 0 -50%; flex-direction: column; max-height: calc(100% - 1rem); overflow-y: auto; }
  :root[data-screen="call"] .ctl.danger { margin: .35rem 0 0; width: 3.25rem; }
  :root[data-screen="call"] .stage { --edge-t: .75rem; --edge-b: .75rem; --edge-r: 5rem; }
  :root[data-screen="call"] .error.floating { bottom: .75rem; }
}

/* --- preferences and accessibility -------------------------------------------------------- */
:root[data-motion="reduce"] *, :root[data-motion="reduce"] *::before, :root[data-motion="reduce"] *::after { animation: none !important; transition: none !important; }
:root[data-motion="reduce"]::view-transition-group(*), :root[data-motion="reduce"]::view-transition-old(*), :root[data-motion="reduce"]::view-transition-new(*) { animation: none; }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  ::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) { animation: none; }
}
:root[data-transparency="reduce"] .aurora { display: none; }
@media (prefers-reduced-transparency: reduce) { :root:not([data-transparency="full"]) .aurora { display: none; } }
@media (prefers-contrast: more) {
  .aurora { display: none; }
  .secondary, input:not([type="checkbox"], [type="radio"]) { border-width: 2px; border-color: var(--text); }
}
@media (forced-colors: active) {
  .aurora { display: none; }
  button { border: 1px solid ButtonText; }
  .icon { stroke: currentColor; }
  #mic-level > span { background: Highlight !important; }
}
/* Screen changes cross-fade (same-document view transitions). */
::view-transition-old(screen), ::view-transition-new(screen) { animation-duration: var(--normal); animation-timing-function: var(--ease-out); }
