:root {
  --bg: #0f1114;
  --surface: #1a1d22;
  --surface2: #232830;
  --text: #e6e8eb;
  --muted: #9aa0a6;
  --primary: #4fc3f7;
  --green: #4caf50;
  --yellow: #ffc107;
  --red: #ff5252;
  --border: #2f343c;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5f7fa;
    --surface: #ffffff;
    --surface2: #eef1f5;
    --text: #202124;
    --muted: #5f6368;
    --border: #d5d9df;
  }
}
* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-size: 14px;
  height: 100vh;
  height: 100svh;
  overflow: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
}

#map-area {
  flex: 1 1 0;
  position: relative;
  min-height: 0;
  overflow: hidden;
}
#map {
  position: absolute;
  inset: 0;
  z-index: 0;
}

.map-loading {
  position: absolute;
  inset: 0;
  z-index: 5;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  pointer-events: none;
  transition: opacity 250ms ease;
}
.map-loading.hidden {
  opacity: 0;
}
.map-loading[hidden] {
  display: none;
}
.map-loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--surface2);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.map-loading-text {
  font-size: 13px;
}

.ol-viewport {
  touch-action: none;
}
.ol-control button {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}
.ol-control button:hover {
  background: var(--surface2);
}
.ol-attribution {
  background: var(--surface);
  color: var(--text);
  font-size: 11px;
  border-radius: 4px 0 0 0;
}
.ol-attribution a {
  color: var(--primary);
}

#menu-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 20;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  opacity: 0.9;
  font-size: 20px;
  cursor: pointer;
}

#locate-btn {
  position: absolute;
  bottom: 40px;
  right: 12px;
  z-index: 20;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  opacity: 0.9;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
#locate-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}
#locate-btn svg {
  display: block;
  width: 24px;
  height: 24px;
}
#locate-btn .locate-ring {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
}
#locate-btn .locate-dot {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
}
#locate-btn .locate-tick {
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

/*
  Two icon groups live inside the locate button: a crosshair and a
  direction arrow. Only one is shown at a time. The crosshair is the
  default; the arrow replaces it while self-follow is engaged.
*/
#locate-btn .locate-crosshair { display: block; }
#locate-btn .locate-arrow     { display: none; }

/*
  Off-screen: the fix exists but is outside the viewport. The dot is
  filled and coloured, matching the OsmAnd / Google Maps convention.
*/
#locate-btn.is-off-screen {
  color: var(--primary);
}
#locate-btn.is-off-screen .locate-dot {
  fill: var(--primary);
  stroke: none;
}

/*
  On-screen: the fix exists and is inside the viewport. The button is
  hollow, so the user can tell at a glance that their position is
  already visible.
*/
#locate-btn.is-on-screen {
  color: var(--primary);
}
#locate-btn.is-on-screen .locate-dot {
  fill: none;
  stroke: var(--primary);
}

/*
  Following: the camera is locked to self. The crosshair is replaced
  by a direction arrow, matching the app, OsmAnd, and Google Maps.
*/
#locate-btn.is-following {
  color: var(--primary);
}
#locate-btn.is-following .locate-crosshair { display: none; }
#locate-btn.is-following .locate-arrow     { display: block; }
#locate-btn.is-following.is-waiting-location {
  color: var(--muted);
}

/*
  Unavailable: no permission, or no fix yet. Everything is muted.
  A "denied" state remains clickable (the click retries the
  permission prompt), so the button is not disabled in that case.
*/
#locate-btn.is-unavailable {
  color: var(--muted);
}
#locate-btn.is-unavailable .locate-dot {
  fill: none;
  stroke: var(--muted);
}
/*
  Waiting: no fix has arrived yet in this session. Muted, hollow dot,
  no distinct colour. The blue filled dot is reserved for a real,
  off-screen fix.
*/
#locate-btn.is-waiting {
  color: var(--muted);
}
#locate-btn.is-waiting .locate-dot {
  fill: none;
  stroke: var(--muted);
}

#recentre-btn {
  position: absolute;
  bottom: 96px;
  right: 12px;
  z-index: 20;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 50%;
  background: var(--surface);
  color: var(--primary);
  border: 1px solid var(--border);
  opacity: 0.9;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
#recentre-btn[hidden] {
  display: none;
}
#recentre-btn svg {
  display: block;
  width: 22px;
  height: 22px;
}

#live-indicator {
  position: absolute;
  top: 12px;
  right: 64px;
  z-index: 20;
  height: 44px;
  line-height: 44px;
  padding: 0 14px;
  border-radius: 22px;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  display: none;
  max-width: calc(100% - 128px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}
#live-indicator.live {
  display: block;
  background: var(--red);
  color: #fff;
}
#live-indicator.ended {
  display: block;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
}
#live-indicator.hidden { display: none; }

#share-dot {
    position: absolute;
    top: 64px;
    right: 12px;
    z-index: 20;

    width: 44px;
    height: 44px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background-color: var(--surface);
    box-sizing: border-box;

    display: inline-flex;
    align-items: center;
    justify-content: center;

    color: var(--muted);
    opacity: 0.5;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);

    cursor: pointer;
    transition: color 120ms ease, opacity 120ms ease, transform 120ms ease;
    -webkit-tap-highlight-color: transparent;
}

#share-dot:hover {
    opacity: 0.75;
}

#share-dot:active {
    transform: scale(0.92);
}

#share-dot:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

#share-dot.idle {
    color: var(--muted);
    opacity: 0.5;
}
#share-dot.ready {
    color: var(--green);
    opacity: 0.75;
}
#share-dot.active {
    color: var(--red);
    opacity: 1;
}
#share-dot.attention {
    color: var(--yellow);
    opacity: 1;
}

#share-dot.attention::after {
    content: '!';
    position: absolute;
    top: 3px;
    right: 3px;
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background: var(--yellow);
    color: #000;
    font-size: 10px;
    font-weight: 700;
    line-height: 15px;
    text-align: center;
    pointer-events: none;
}

#group-prompt,
#group-name-prompt {
  position: absolute;
  top: 64px;
  left: 12px;
  right: 12px;
  z-index: 22;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.35);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#group-prompt[hidden],
#group-name-prompt[hidden] { display: none; }
.group-prompt-title {
  font-size: 15px;
  color: var(--text);
  font-weight: 700;
  padding-bottom: 2px;
}
.group-prompt-text {
  color: var(--text);
  font-size: 13px;
  line-height: 1.4;
}
.group-prompt-count {
  color: var(--primary);
  font-size: 13px;
  line-height: 1.4;
  padding-top: 2px;
}
#group-prompt input,
#group-name-prompt input {
  padding: 10px 12px;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  outline: none;
}
#group-prompt input::placeholder,
#group-name-prompt input::placeholder {
  color: var(--text);
  opacity: 0.6;
}
#group-prompt button,
#group-name-prompt button {
  margin-top: 4px;
  padding: 12px 14px;
  background: var(--primary);
  color: #000;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
}
#group-prompt .group-prompt-actions,
#group-name-prompt .group-prompt-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}
#group-prompt .group-prompt-actions button,
#group-name-prompt .group-prompt-actions button {
  flex: 1;
  margin-top: 0;
}
#group-prompt button.secondary,
#group-name-prompt button.secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  font-weight: 600;
}

/*
  Not position: absolute. OpenLayers puts the marker into its own
  wrapper and shifts that wrapper by -50% / -100% of its size, so the
  tail tip sits on the position. An absolute marker gives the wrapper
  no size, the shift does nothing, and the marker hangs 28 px right and
  64 px below the true position (more ground the further one zooms out).
*/
.peer-marker {
  position: relative;
  z-index: 1;
  width: 56px;
  height: 64px;
  background: transparent;
  border: none;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  pointer-events: auto;
  cursor: pointer;
}
.peer-marker.selected {
  z-index: 2;
}
.peer-marker.followed {
  z-index: 3;
}
.peer-marker-body {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px solid #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 16px;
  font-family: system-ui, sans-serif;
  background: var(--peer-color, var(--primary));
  box-shadow: 0 1px 4px rgba(0,0,0,0.4);
  text-shadow: 0 1px 2px rgba(0,0,0,0.5);
}
.peer-marker.selected .peer-marker-body {
  width: 46px;
  height: 46px;
  border-width: 3px;
}
.peer-marker.followed .peer-marker-body {
  box-shadow: 0 0 0 4px var(--yellow),
              0 1px 4px rgba(0,0,0,0.4);
}
.peer-marker.external .peer-marker-body {
  border-style: dashed;
}
.peer-marker-tail {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 8px solid var(--peer-color, #fff);
}

/*
  Marker tiers. peerState() in live-freshness.js produces one of
  'live', 'recent', 'old', 'gone'. The tier is added as a class on the
  same element as .peer-marker, so the visual rule is expressed in CSS
  and nothing else in the codebase needs to know what a tier looks
  like.

    live    colour, solid, with tail.  Publishing right now.
    recent  colour, dimmed.           Stopped less than an hour ago.
    old     dashed outline, no tail.  Stopped between one and 24 h.
    gone    hidden by map.js; the tier still exists for completeness.
*/

.peer-marker.recent .peer-marker-body,
.peer-marker.recent .peer-marker-tail {
  opacity: 0.55;
}

.peer-marker.old .peer-marker-body,
.peer-marker.gone .peer-marker-body {
  background: transparent;
  border-color: var(--muted);
  border-style: dashed;
  color: var(--muted);
  text-shadow: none;
}
.peer-marker.old .peer-marker-tail,
.peer-marker.gone .peer-marker-tail {
  border-top-color: var(--muted);
  opacity: 0.6;
}

.self-marker {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--primary);
  border: 2px solid #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.4);
  cursor: pointer;
}

#bottom-area {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-top: 1px solid var(--border);
  position: relative;
  z-index: 1;
}

#tab-bar {
  position: relative;
  display: flex;
  flex: 0 0 auto;
  align-items: stretch;
  padding-top: 12px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
#tab-bar[hidden] { display: none; }
#tab-bar.dragging {
  cursor: grabbing;
}
#tab-bar::before {
  content: '';
  position: absolute;
  top: 4px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 4px;
  border-radius: 2px;
  background: var(--muted);
  opacity: 0.55;
  pointer-events: none;
}

.tab-btn {
  flex: 1;
  padding: 10px 0;
  background: var(--surface2);
  color: var(--text);
  border: none;
  border-bottom: 2px solid transparent;
  font-size: 14px;
  cursor: pointer;
}
.tab-btn.active {
  color: var(--text);
  border-bottom-color: var(--primary);
  background: var(--surface);
}

#tab-collapse-btn {
  flex: 0 0 48px;
  padding: 0;
  background: var(--surface2);
  color: var(--primary);
  border: none;
  border-left: 1px solid var(--border);
  border-bottom: 2px solid transparent;
  font-size: 20px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
#tab-collapse-btn:active {
  background: var(--surface);
}

#tab-content {
  max-height: 55vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
#tab-content[hidden] { display: none; }

.tab-panel {
  display: none;
}
.tab-panel.active { display: block; }

#chart-container {
  padding: 0;
  height: 240px;
  position: relative;
}
#chart-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

/*
  Live / Last panel.

  The panel is a status readout. It always shows a header row
  (Last update) and, below it, one row per field that the current
  status carries. The row set is rebuilt from the status on every
  render, so the panel's height and content reflect the sender's
  current state.

  Row order is fixed: Last update is the header, then Heart rate,
  Speed, Altitude, Distance, HRV. Hidden rows are skipped; visible
  rows keep this relative order.
*/
#live-panel { padding: 16px; }

#live-panel .live-header {
  padding: 4px 0 8px;
  margin-bottom: 8px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

#live-panel .metric-row {
  padding: 4px 0;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

/*
  Summary panel.

  Two logical groups, laid out as flex children of #summary that
  wrap when the available width is too small to hold both at their
  natural minimum. Each group is a .summary-col holding its rows in
  a vertical stack. The break point is set by the flex-basis on
  .summary-col, in em so it scales with the user's font size and
  zoom. There are no media queries and no fixed pixel widths: the
  same rule serves every viewport and every browser.

  Column contents:
    left   TRIMP, the explorer link, then Low / Moderate / High.
           The link sits between TRIMP and the zone block so it
           reads as a footnote to the score.
    right  Avg HR, Max HR, Distance, Ascent, Avg speed.

  A message branch (renderSummary's message or no-data path, or
  overlay.js's showError) is a .summary-message child and spans the
  full width via flex-basis 100%.
*/
#summary {
  padding: 10px 14px;
  display: none;
  gap: 12px 24px;
  flex-wrap: wrap;
}
#summary.active { display: flex; }

.summary-col {
  flex: 1 1 10em;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.summary-message {
  flex: 1 1 100%;
  color: var(--text);
}
.summary-message.is-error {
  color: var(--red);
}

@media (prefers-color-scheme: light) {
  .summary-message.is-error { color: #c62828; }
}

/*
  The explorer link is the one element in the summary that navigates
  away from the page, so it uses the system link colour (LinkText)
  rather than the app palette. LinkText is what the browser and the
  operating system guarantee is readable against Canvas in the
  current theme, and it also honours a user's custom link colour if
  they have set one. The brackets around the text are the non-colour
  affordance: the link is distinguishable without relying on colour
  alone, which is why no default underline is needed. Hover and
  keyboard focus are signalled, as platform convention requires.
*/
.summary-explore-link {
  display: block;
  color: LinkText;
  font-size: 14px;
  text-decoration: none;
  padding: 4px 0;
}
.summary-explore-link:hover {
  text-decoration: underline;
}
.summary-explore-link:focus-visible {
  outline: 2px solid LinkText;
  outline-offset: 2px;
  border-radius: 2px;
}

#summary .metric {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 2px 0;
  min-width: 0;
}
#summary .metric .label {
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#summary .metric .value {
  font-weight: 600;
  white-space: nowrap;
}
#summary .low  .value { color: var(--green); }
#summary .mod  .value { color: var(--yellow); }
#summary .high .value { color: var(--red); }

#menu-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99;
}
#menu-backdrop[hidden] { display: none; }

#menu-sheet {
  position: fixed;
  top: 0; right: 0; bottom: 0; left: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
  pointer-events: none;
}
#menu-sheet[hidden] { display: none; }

#menu-content {
  width: 100%;
  max-height: 85%;
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -4px 16px rgba(0,0,0,0.35);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  pointer-events: auto;
}

#menu-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

#menu-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
}

#menu-sheet #menu-close-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 50%;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  font-size: 16px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
#menu-sheet #menu-close-btn:active {
  background: var(--surface);
}

#menu-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  pointer-events: auto;
}

/* ---------- Section structure ---------- */

.menu-section {
  border-bottom: 2px solid var(--border);
  border-left: 3px solid var(--border);
  transition: border-left-color 0.15s ease;
}
.menu-section:last-of-type {
  border-bottom: none;
}
.menu-section:not(.collapsed) {
  border-left-color: var(--primary);
}
@media (prefers-reduced-motion: reduce) {
  .menu-section {
    transition: none;
  }
}

#menu-sheet .menu-section > button.menu-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 52px;
  margin: 0;
  padding: 14px 16px;
  background: var(--surface2);
  color: var(--text);
  border: none;
  border-radius: 0;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  cursor: pointer;
  flex-shrink: 1;
  white-space: normal;
  pointer-events: auto;
}
#menu-sheet .menu-section > button.menu-section-header:active {
  background: var(--surface);
}
@media (hover: hover) {
  #menu-sheet .menu-section > button.menu-section-header:hover {
    background: var(--surface);
  }
}

.menu-section-title {
  flex: 1 1 auto;
  text-align: left;
}

.menu-section-chevron {
  flex: 0 0 auto;
  margin-left: 12px;
  font-size: 18px;
  line-height: 1;
  color: currentColor;
  display: inline-block;
  transition: transform 0.15s ease;
  transform: rotate(90deg);
}
.menu-section.collapsed .menu-section-chevron {
  transform: rotate(0);
}
@media (prefers-reduced-motion: reduce) {
  .menu-section-chevron {
    transition: none;
  }
}

.menu-section.collapsed .menu-section-content {
  display: none;
}

.menu-section-content {
  padding: 12px 16px 16px;
}

/*
  Public share view. The Group and Share sections are about a group
  the viewer does not have. Nothing inside them can succeed for a
  public viewer, so they are hidden entirely. The mode-switch block
  in the About section is the only way to reach the encrypted side.

  The class is set on <body> synchronously at the top of init(),
  before any await, so the menu is already correct on the first
  frame the user can interact with.
*/
body.public-mode .menu-section[data-section="group"],
body.public-mode .menu-section[data-section="share"] {
  display: none;
}
/*
  Public share view. Publishing is a group action, and the viewer
  has no group. The share button is hidden entirely, along with the
  Group and Share sections it would lead to.
*/
body.public-mode #share-dot {
  display: none;
}

/* ---------- Rows and controls ---------- */

#menu-sheet .row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  flex-wrap: wrap;
}

#menu-sheet button {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;

  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  flex-shrink: 0;
  white-space: nowrap;
}

#menu-sheet label {
  color: var(--text);
}

#menu-sheet select {
  display: none;
}

.menu-group {
  margin-top: 12px;
}
.menu-group:first-child {
  margin-top: 0;
}
.menu-group-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.8px;
  text-transform: uppercase;
  color: var(--text);
  padding: 2px 0 8px;
}

/* ---------- Field: label, control, help ---------- */

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
  flex: 1 1 120px;
}

/* Restores the meaning of the hidden attribute on fields. Without
   this rule, the .field rule above wins the cascade against the
   browser's default [hidden] { display: none }, and a field marked
   hidden by JavaScript stays visible. The compound selector beats
   .field on specificity, so the hidden attribute wins as intended. */
.field[hidden] {
  display: none;
}

.field.field-fit {
  flex: 0 0 auto;
}

.field-label {
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: 0.2px;
}

.field-help {
  color: var(--text);
  font-size: 12px;
  line-height: 1.35;
  padding-top: 2px;
}

.field input {
  padding: 8px 10px;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
  width: 100%;
}

.field input::placeholder {
  color: var(--text);
  opacity: 0.6;
}

.field-value {
  padding: 8px 10px;
  color: var(--text);
  font-size: 13px;
}

.field .picker-btn {
  width: 100%;
  justify-content: space-between;
  padding: 8px 10px;
}

.picker-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
}
.picker-btn .picker-value {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.picker-btn .picker-chevron {
  color: var(--text);
  opacity: 0.7;
  font-size: 11px;
  flex: 0 0 auto;
}
.picker-btn[hidden] {
  display: none;
}
.picker-btn.static {
  background: transparent;
  border-color: transparent;
  cursor: default;
}
.picker-btn.static:disabled {
  opacity: 1;
}

.picker-value.loading::before {
  content: '';
  display: inline-block;
  width: 10px;
  height: 10px;
  border: 1.5px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
}
.picker-value.loading {
  color: var(--text);
}

.download-btn {
  min-width: 56px;
  text-align: center;
  font-weight: 600;
  letter-spacing: 0.4px;
}
.download-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ---------- Status blocks ---------- */

.status-block {
  display: block;
  font-size: 13px;
  line-height: 1.4;
  padding: 8px 10px 8px 12px;
  border-left: 3px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  background: transparent;
  margin: 6px 0 0;
  overflow-wrap: break-word;
}

.status-block.is-idle {
  border-left-color: var(--border);
  color: var(--text);
}

.status-block.is-ready {
  border-left-color: var(--primary);
}

.status-block.is-active {
  border-left-color: var(--green);
}

.status-block.is-warning {
  border-left-color: var(--yellow);
  background: rgba(255, 193, 7, 0.10);
}

.status-block.is-error {
  border-left-color: var(--red);
  background: rgba(211, 47, 47, 0.10);
}

@media (prefers-color-scheme: dark) {
  .status-block.is-warning {
    background: rgba(255, 193, 7, 0.12);
  }
  .status-block.is-error {
    background: rgba(255, 82, 82, 0.12);
  }
}

.member-summary {
  color: var(--text);
  font-size: 13px;
  line-height: 1.4;
  margin-top: 6px;
}
.member-summary[hidden] { display: none; }

.sharing-help {
  margin-top: 6px;
  padding: 8px 10px;
  background: rgba(211, 47, 47, 0.12);
  border: 1px solid rgba(211, 47, 47, 0.40);
  color: #c62828;
  border-radius: 6px;
  font-size: 13px;
  line-height: 1.4;
}
@media (prefers-color-scheme: dark) {
  .sharing-help {
    background: rgba(255, 82, 82, 0.15);
    border-color: rgba(255, 82, 82, 0.50);
    color: #ff8a80;
  }
}
.sharing-help[hidden] { display: none; }

/* ---------- Share toggle button ---------- */

#share-toggle {
  flex: 1 1 100%;
  min-height: 48px;
  margin-top: 4px;
  font-size: 15px;
  font-weight: 600;
}

#share-toggle:disabled,
#share-toggle.is-blocked {
  opacity: 0.4;
  cursor: not-allowed;
}

#share-toggle.is-ready {
  background: var(--primary);
  color: #000;
  border-color: var(--primary);
}

#share-toggle.is-running {
  background: var(--green);
  color: #000;
  border-color: var(--green);
}

/* ---------- About section ---------- */

.about-text {
  font-size: 13px;
  line-height: 1.5;
  color: var(--text);
  margin: 6px 0;
}
.about-text:first-child {
  margin-top: 2px;
}
.about-text:last-child {
  margin-bottom: 4px;
}
.about-text a {
  color: LinkText;
  overflow-wrap: anywhere;
}
.about-text strong {
  color: var(--text);
}

/*
  About prose varies by mode. The encrypted-mode paragraph explains
  what a browser peer does in a group; the public-mode paragraph
  names the state the viewer is in. The class on <body> is set
  synchronously at the top of init(), so the correct paragraph is
  visible from the first interactive frame.
*/
body.public-mode .about-encrypted-only { display: none; }
body:not(.public-mode) .about-public-only { display: none; }

/* ---------- Server blocks (About section) ---------- */

.server-key,
.server-limits,
.server-stats {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.server-key-title {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.4px;
  color: var(--text);
  margin-bottom: 6px;
}

.server-key-help,
.server-key-hint {
  font-size: 13px;
  line-height: 1.5;
  color: var(--text);
  margin: 0 0 10px;
}

.server-key-hint {
  margin: 10px 0 0;
}

.server-key-help code {
  background: var(--surface2);
  padding: 1px 4px;
  border-radius: 3px;
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

#server-key-input {
  width: 100%;
  padding: 10px 12px;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  outline: none;
}

#server-key-input:focus {
  border-color: var(--primary);
}

#server-key-input::placeholder {
  color: var(--text);
  opacity: 0.6;
}

.server-key-error {
  margin-top: 8px;
  padding: 8px 10px;
  background: rgba(211, 47, 47, 0.12);
  border: 1px solid rgba(211, 47, 47, 0.40);
  color: #c62828;
  border-radius: 6px;
  font-size: 13px;
  line-height: 1.4;
}

.server-key-error[hidden] {
  display: none;
}

@media (prefers-color-scheme: dark) {
  .server-key-error {
    background: rgba(255, 82, 82, 0.15);
    border-color: rgba(255, 82, 82, 0.50);
    color: #ff8a80;
  }
}

.server-stats,
.server-limits {
  font-size: 13px;
  line-height: 1.5;
  color: var(--text);
}
.server-stats[hidden],
.server-limits[hidden] {
  display: none;
}

#menu-sheet .mode-switch-block {
  margin-top: 16px;
  padding: 12px 0 0;
  border-top: 1px solid var(--border);
}

#menu-sheet .mode-switch-block[hidden] {
  display: none;
}

#menu-sheet .mode-switch-title {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.4px;
  color: var(--text);
  margin-bottom: 6px;
}

#menu-sheet .mode-switch-text {
  font-size: 13px;
  line-height: 1.5;
  color: var(--text);
  margin: 0 0 12px;
}

#menu-sheet .mode-switch-link {
  display: inline-block;
  padding: 10px 14px;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  white-space: normal;
  cursor: pointer;
}

#menu-sheet .mode-switch-link:hover,
#menu-sheet .mode-switch-link:active {
  background: var(--surface);
}

/* ---------- Picker overlay ---------- */

#menu-picker {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 0;
  padding: 16px 12px 12px;
}
#menu-picker[hidden] { display: none; }
#picker-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  padding: 2px 4px 6px;
}
#picker-search {
  padding: 10px 14px;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  width: 100%;
  outline: none;
}
#picker-search[hidden] { display: none; }
#picker-search::placeholder {
  color: var(--text);
  opacity: 0.6;
}
#picker-options {
  max-height: 40vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.picker-option {
  display: block;
  width: 100%;
  text-align: left;
  padding: 12px 14px;
  background: transparent;
  color: var(--text);
  border: none;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
}
.picker-option:hover {
  background: var(--surface2);
}
.picker-option.selected {
  background: var(--surface2);
  color: var(--text);
  font-weight: 600;
}
.picker-option:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.picker-more {
  padding: 12px 14px;
  font-size: 13px;
  color: var(--text);
  font-style: italic;
}
.picker-note {
  padding: 10px 14px;
  margin-bottom: 6px;
  font-size: 13px;
  line-height: 1.4;
  color: var(--text);
  background: var(--surface2);
  border-radius: 8px;
}
#picker-back {
  margin-top: 6px;
  padding: 10px 14px;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
}

/* ---------- Loading ---------- */

#loading-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  z-index: 1000;
}
#loading-overlay[hidden] { display: none; }
.spinner {
  width: 42px; height: 42px;
  border: 4px solid var(--surface2);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#loading-text { margin-top: 12px; color: #fff; }

/* ---------- Runtime overlays ---------- */

.notice {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 12px;
  z-index: 101;
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  font-size: 13px;
  line-height: 1.4;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25);
  pointer-events: none;
}

.fatal-error {
  position: fixed;
  inset: 0;
  z-index: 10000;
  background: var(--surface);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  white-space: pre-wrap;
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ---------- Misc ---------- */

.tab-btn[hidden] {
    display: none;
}

.tab-presence-label {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    padding: 10px 16px;
    color: var(--text);
    font-size: 14px;
    font-weight: 600;
}
.tab-presence-label[hidden] {
    display: none;
}

#menu-sheet button.link-btn {
  background: transparent;
  border: none;
  padding: 4px 0;
  color: var(--primary);
  font-size: 13px;
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
}
#menu-sheet button.link-btn:hover,
#menu-sheet button.link-btn:active {
  color: var(--text);
}

/* ---------- Chat button ---------- */

#chat-btn {
    position: absolute;
    top: 116px;
    right: 12px;
    z-index: 20;
    width: 44px;
    height: 44px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background-color: var(--surface);
    color: var(--text);
    box-sizing: border-box;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
    opacity: 0.9;
    -webkit-tap-highlight-color: transparent;
}
#chat-btn:hover { opacity: 1; }
#chat-btn:active { transform: scale(0.92); }
#chat-btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
#chat-btn[hidden] { display: none; }

.chat-badge {
    position: absolute;
    top: -2px;
    right: -2px;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 9px;
    background: var(--red);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    line-height: 18px;
    text-align: center;
    pointer-events: none;
}
.chat-badge[hidden] { display: none; }

body.public-mode #chat-btn { display: none; }

/* ---------- Chat backdrop ---------- */

#chat-backdrop {
    position: fixed;
    inset: 0;
    z-index: 999;
    background: rgba(0, 0, 0, 0.3);
}
#chat-backdrop[hidden] { display: none; }

/* ---------- Chat overlay ---------- */

#chat-overlay {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: 85vh;
    height: 85dvh;
    max-height: 85vh;
    max-height: 85dvh;
    z-index: 1000;
    background: var(--bg);
    color: var(--text);
    display: flex;
    flex-direction: column;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.25);
    overflow: hidden;
}
#chat-overlay[hidden] { display: none; }

#chat-header {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}

#chat-close {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    gap: 2px;
    height: 40px;
    padding: 0 14px 0 6px;
    border: none;
    border-radius: 20px;
    background: transparent;
    color: var(--text);
    font: inherit;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
#chat-close:hover { background: var(--surface2); }
#chat-close:active { transform: scale(0.96); }
#chat-close:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}
#chat-close svg { flex: 0 0 auto; }

#chat-recipient {
    flex: 1 1 auto;
    min-width: 0;
    height: 40px;
    padding: 0 10px;
    background: var(--surface2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
    font-family: inherit;
    box-sizing: border-box;
}

#chat-refresh {
    flex: 0 0 auto;
    width: 40px;
    height: 40px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--text);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
#chat-refresh:hover { background: var(--surface2); }
#chat-refresh:active { transform: scale(0.92); }
#chat-refresh:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}
#chat-refresh:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}
#chat-refresh.spinning svg {
    animation: chat-spin 0.8s linear infinite;
}

/* ---------- Chat list and bubbles ---------- */

#chat-list {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 8px 8px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.chat-bubble {
    max-width: 80%;
    padding: 8px 10px;
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    gap: 2px;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

.chat-bubble.received {
    align-self: flex-start;
    background: var(--surface2);
    color: var(--text);
    border: 1px solid var(--border);
    border-top-left-radius: 4px;
}

.chat-bubble.sent {
    align-self: flex-end;
    background: var(--primary);
    color: #000;
    border-top-right-radius: 4px;
}

.chat-bubble-header {
    font-size: 12px;
    font-weight: 700;
    color: inherit;
}

.chat-bubble.sent .chat-bubble-header {
    font-size: 11px;
    font-weight: 600;
}

.chat-bubble-body {
    font-size: 15px;
    line-height: 1.35;
    white-space: pre-wrap;
    text-align: start;
    color: inherit;
}

.chat-bubble-time {
    font-size: 11px;
    color: inherit;
    align-self: flex-end;
    opacity: 0.85;
}

/* ---------- Chat input row ---------- */

#chat-input-row {
    flex: 0 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 8px 12px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--border);
    background: var(--surface);
}

#chat-input {
    flex: 1 1 auto;
    min-width: 0;
    resize: none;
    padding: 10px 12px;
    background: var(--surface2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 18px;
    font-family: inherit;
    font-size: 15px;
    line-height: 1.35;
    max-height: 120px;
    outline: none;
}
#chat-input:focus { border-color: var(--primary); }

#chat-send {
    flex: 0 0 auto;
    padding: 10px 16px;
    background: var(--primary);
    color: #000;
    border: none;
    border-radius: 18px;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
}
#chat-send:hover { filter: brightness(1.05); }
#chat-send:active { transform: scale(0.96); }
#chat-send:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

@media (prefers-color-scheme: light) {
    #chat-input {
        background: var(--surface);
    }
}

@keyframes chat-spin {
    to { transform: rotate(360deg); }
}
/* ---- Chat attachments ------------------------------------------------ */
#chat-attach {
  flex: 0 0 auto;
  min-width: 44px;
  min-height: 44px;
  font-size: 1.5rem;
  line-height: 1;
}
.chat-attachment {
  margin: 4px 0;
}
.chat-attachment-thumb,
.chat-attachment-video {
  display: block;
  max-width: 240px;
  max-height: 240px;
  border-radius: 6px;
}
.chat-attachment-label {
  font-size: 0.9rem;
  margin-top: 2px;
}
.chat-attachment-actions {
  display: flex;
  gap: 6px;
  margin-top: 4px;
  flex-wrap: wrap;
}
.chat-attachment-actions button {
  min-height: 36px;
  padding: 4px 10px;
  font-size: 0.9rem;
}

/* ---- Chat: my name, asked inside the chat until it is set ---------- */
#chat-name-row {
  flex: 0 0 auto;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
#chat-name-row[hidden] { display: none; }
.chat-name-label {
  display: block;
  font-weight: 700;
  font-size: 15px;
  margin-bottom: 6px;
}
.chat-name-line {
  display: flex;
  gap: 8px;
}
#chat-name {
  flex: 1 1 auto;
  min-width: 0;
  padding: 10px 12px;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 18px;
  font-family: inherit;
  font-size: 15px;
}
#chat-name:focus { border-color: var(--primary); outline: none; }
#chat-name-save {
  flex: 0 0 auto;
  padding: 10px 16px;
  background: var(--primary);
  color: #000;
  border: none;
  border-radius: 18px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}
.chat-name-help {
  font-size: 13px;
  margin-top: 6px;
  color: var(--text);
}
#chat-send:disabled,
#chat-attach:disabled {
  cursor: not-allowed;
  filter: grayscale(1);
}
