:root {
  color-scheme: dark;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, sans-serif;
}
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  /* Prevent iOS Safari rubber-band overscroll on the *page itself*.
     touch-action: none on the canvas/container blocks pinch and pan
     inside the viewport, but a 2F scroll gesture would still bounce
     the body around (visible as "shifts a little bit and returns
     back" — that's the page snapping back from the rubber-band).
     overscroll-behavior locks scrolling to inner containers; overflow:
     hidden on body removes the bouncy outer scrollable surface
     entirely. Combined, the page can't scroll or bounce. */
  overflow: hidden;
  overscroll-behavior: none;
}
body {
  background: #111;
  color: #eee;
  display: flex;
  flex-direction: column;
  /* `--rl-viewport-h` is set by install_viewport_height_var (lib.rs)
     to `visualViewport.height` so the body shrinks when the soft
     keyboard slides up — dvh/svh don't track the keyboard on most
     browsers, only visualViewport does. The fallback chain covers
     desktop (no visualViewport API) and dvh-capable mobile chromes
     until the JS handler fires. */
  height: var(--rl-viewport-h, 100dvh);
  min-height: 100vh;
  min-height: 100dvh;
}
#toolbar {
  padding: 8px 12px;
  display: flex;
  gap: 12px;
  align-items: center;
  border-bottom: 1px solid #222;
  font-size: 13px;
  flex: none;
  /* Horizontal-pan when the toolbar overflows on narrow screens.
     scrollbar-width: none + the webkit pseudo hides the visible
     scrollbar; users still drag with finger / shift-wheel. */
  overflow-x: auto;
  overflow-y: hidden;
  flex-wrap: nowrap;
  white-space: nowrap;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
#toolbar::-webkit-scrollbar {
  display: none;
}
#toolbar > * {
  flex: none;
}
.toolbar-group {
  display: flex;
  align-items: center;
  gap: 6px;
}
.toolbar-group label {
  color: #888;
  font-size: 12px;
}
.toolbar-group .ui-icon {
  color: #aaa;
  flex: none;
}
#toolbar button,
#toolbar select {
  background: #1c1c1c;
  color: #ddd;
  border: 1px solid #333;
  padding: 4px 10px;
  border-radius: 4px;
  font: inherit;
  cursor: pointer;
}
#toolbar button:hover:not(:disabled),
#toolbar select:hover:not(:disabled) {
  background: #2a2a2a;
  border-color: #555;
}
#toolbar button.active {
  background: #264a73;
  border-color: #3a6fa0;
  color: #fff;
}
#toolbar button:disabled,
#toolbar select:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ── Session info button (leftmost in toolbar) ─────────────────── */
#info-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 180px;
}
#info-btn-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
#info-btn[aria-expanded="true"] {
  background: #264a73;
  border-color: #3a6fa0;
  color: #fff;
}

/* ── Info side panel (slides in from left) ─────────────────────── */
#info-panel {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  width: min(340px, 88vw);
  background: #141414;
  border-right: 1px solid #2a2a2a;
  color: #ddd;
  font-size: 13px;
  z-index: 50;
  transform: translateX(-100%);
  transition: transform 0.18s ease-out;
  display: flex;
  flex-direction: column;
  box-shadow: 4px 0 24px rgba(0, 0, 0, 0.5);
}
#info-panel[data-open] {
  transform: translateX(0);
}
#info-panel[hidden] {
  display: none;
}
.info-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid #222;
  flex: none;
}
.info-panel-header h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.02em;
}
#info-panel-close {
  background: transparent;
  border: 0;
  color: #888;
  font-size: 20px;
  line-height: 1;
  padding: 0 6px;
  cursor: pointer;
}
#info-panel-close:hover {
  color: #fff;
}
.info-panel-body {
  flex: 1;
  overflow-y: auto;
  padding-bottom: 24px;
}

/* ── Accordion sections ────────────────────────────────────────── */
.info-section {
  border-bottom: 1px solid #1f1f1f;
}
.info-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  background: transparent;
  border: 0;
  padding: 12px 14px;
  color: #ddd;
  font: inherit;
  font-weight: 600;
  font-size: 13px;
  letter-spacing: 0.02em;
  cursor: pointer;
}
.info-section-header:hover {
  background: #1a1a1a;
}
.info-chevron {
  color: #777;
  transition: transform 0.15s ease-out;
}
.info-section[data-open] .info-chevron {
  transform: rotate(180deg);
}
.info-section .info-section-body {
  display: none;
  padding: 4px 14px 12px;
  margin: 0;
}
.info-section[data-open] .info-section-body {
  display: block;
}

/* Connection key/value list */
dl.info-section-body {
  display: grid;
  grid-template-columns: 92px 1fr;
  gap: 6px 12px;
  font-variant-numeric: tabular-nums;
}
dl.info-section-body dt {
  color: #888;
  font-weight: 400;
}
dl.info-section-body dd {
  margin: 0;
  color: #ddd;
  word-break: break-all;
}

/* Permissions list */
ul.info-perms {
  list-style: none;
  padding-left: 14px;
  padding-right: 14px;
}
ul.info-perms li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  color: #aaa;
}
ul.info-perms li::before {
  content: "—";
  color: #555;
  width: 14px;
  display: inline-block;
  text-align: center;
}
ul.info-perms li[data-granted]::before {
  content: "✓";
  color: #5eb16e;
}
ul.info-perms li[data-granted] {
  color: #ddd;
}

.info-placeholder {
  color: #666;
  font-style: italic;
}

/* ── Toolbar dropdown menu (Security) ──────────────────────────── */
.dropdown-menu {
  position: fixed;
  top: 44px;
  right: 12px;
  z-index: 60;
  min-width: 260px;
  background: #1b1b1b;
  border: 1px solid #2a2a2a;
  border-radius: 6px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.55);
  padding: 4px 0;
  color: #ddd;
  font-size: 13px;
}
.dropdown-menu[hidden] {
  display: none;
}
.dropdown-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 12px 8px 32px;
  background: transparent;
  border: 0;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  position: relative;
}
.dropdown-item:hover {
  background: #262626;
}
.dropdown-check {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 14px;
  font-size: 13px;
  color: #5eb16e;
  opacity: 0;
}
.dropdown-toggle[data-checked] .dropdown-check {
  opacity: 1;
}
.dropdown-divider {
  height: 1px;
  background: #2a2a2a;
  margin: 4px 0;
}
#security-btn.active {
  background: #264a73;
  border-color: #3a6fa0;
  color: #fff;
}

/* Clipboard buttons — brief green flash after successful send/import */
#clipboard-import-btn.flash,
#clipboard-paste-remote-btn.flash {
  background: #2d5a3d;
  border-color: #4a8b62;
  transition: background 0.4s ease-out, border-color 0.4s ease-out;
}

/* ── Confirm modal ────────────────────────────────────────────── */
#confirm-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
}
#confirm-modal[hidden] {
  display: none;
}
#confirm-modal .modal-card {
  max-width: min(420px, 90vw);
  background: #1c1c1c;
  border: 1px solid #2a2a2a;
  border-radius: 8px;
  padding: 16px 18px;
}
#confirm-modal h2 {
  margin: 0 0 8px;
  font-size: 16px;
  color: #eee;
}
#confirm-modal p {
  margin: 0 0 16px;
  color: #bbb;
  font-size: 13px;
  line-height: 1.4;
}
#confirm-modal .modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
#confirm-modal-cancel,
#confirm-modal-ok {
  background: #1c1c1c;
  color: #ddd;
  border: 1px solid #333;
  padding: 6px 14px;
  border-radius: 4px;
  font: inherit;
  cursor: pointer;
}
#confirm-modal-ok {
  background: #b34040;
  border-color: #c25555;
  color: #fff;
}
#confirm-modal-cancel:hover {
  background: #2a2a2a;
}
#confirm-modal-ok:hover {
  background: #c25555;
}

/* Footer toggle */
.info-panel-footer {
  border-top: 1px solid #1f1f1f;
  padding: 12px 14px;
  flex: none;
}
.info-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #ccc;
  cursor: pointer;
  user-select: none;
}
.info-toggle input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 32px;
  height: 18px;
  background: #2a2a2a;
  border: 1px solid #333;
  border-radius: 9px;
  position: relative;
  cursor: pointer;
  transition: background 0.15s;
  flex: none;
}
.info-toggle input[type="checkbox"]::before {
  content: "";
  position: absolute;
  top: 1px;
  left: 1px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #888;
  transition: left 0.15s, background 0.15s;
}
.info-toggle input[type="checkbox"]:checked {
  background: #264a73;
  border-color: #3a6fa0;
}
.info-toggle input[type="checkbox"]:checked::before {
  left: 15px;
  background: #fff;
}

main#viewport-container {
  flex: 1;
  min-height: 0;
  padding: 0;
  background: #000;
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  /* All touch handling is custom in trackpad.rs. Block the browser's
     default gestures (page panning, pinch-zoom) on the *container*
     too, not just the canvas — touches landing in fit-mode letterbox
     or zoom-mode empty area would otherwise reach the browser. */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* ── Fit mode: scale canvas to fit viewport, preserve aspect ratio. ─────── */
main#viewport-container.fit-mode {
  overflow: hidden;
  align-items: center;
  justify-content: center;
}
main#viewport-container.fit-mode canvas {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  image-rendering: auto;
}

/* ── Native mode: 1:1 pixels, pan via container scrollbars. ────────────── */
/* `display: block` (not flex) — flex containers on iOS Safari suppress
   programmatic vertical scrolling when a child overflows the cross-axis,
   which broke 3-finger vertical pan. Block layout lets `overflow: auto`
   work normally on both axes. */
main#viewport-container.native-mode {
  display: block;
  overflow: auto;
}
main#viewport-container.native-mode canvas {
  max-width: none;
  max-height: none;
  image-rendering: pixelated;
}

canvas {
  background: #000;
  display: block;
  /*
   * Block browser-default touch behaviour on the canvas: with
   * `touch-action: none` the browser stops trying to pan the page
   * or pinch-zoom on touches, leaving every gesture for us. Also
   * suppresses the iOS callout on long-press.
   */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* ── Zoom mode: canvas sized in CSS pixels, container scrollable. ────── */
/* See native-mode comment: block layout lets vertical overflow scroll
   work; flex was eating it on iOS Safari. */
main#viewport-container.zoom-mode {
  display: block;
  overflow: auto;
}
main#viewport-container.zoom-mode canvas {
  /* width/height set inline by trackpad::apply_zoom_anchored */
  max-width: none;
  max-height: none;
}

/* ── Virtual cursor (trackpad mode) ────────────────────────────────── */
#virtual-cursor {
  position: absolute;
  top: 0;
  left: 0;
  width: 16px;
  height: 16px;
  pointer-events: none;
  /* Without `will-change`, every transform update would invalidate
     the cursor's compositor layer and stutter on slower mobiles. */
  will-change: transform;
  z-index: 10;
  /* `display: block` is set inline by trackpad::update_cursor_dom on
     first move; the `hidden` HTML attribute keeps it off-screen
     until then. */
}
main#viewport-container {
  /* The cursor is positioned relative to this container so the
     transform-translate values sit in the same coord space as the
     canvas's CSS box. */
  position: relative;
}

/* ── Mobile keyboard capture textarea ──────────────────────────────
   This is xterm.js's production-tested pattern, the most
   Android-Chrome-compatible hidden-input layout we found:
     * <textarea>, not <input> — Android Chrome's IME framework
       summons the keyboard reliably for hidden textareas but
       silently refuses for hidden <input type="text"> in many
       configurations.
     * Wrapper with a positive z-index forms a stacking context so
       the textarea's negative z-index doesn't take it out of the
       page entirely (just behind its siblings).
     * Offscreen via `left: -9999em`, NOT transform: translate —
       transform interacts badly with Android Chrome's keyboard
       summon path. The em offset keeps the layout pixels far
       enough out that nothing visible leaks.
     * width/height: 0 — xterm.js does this; Android still summons
       the keyboard.
   ≥16px font on iOS suppresses focus-zoom (page would otherwise
   jump-zoom when the keyboard opens). */
#keyboard-capture-wrap {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 5;
}
#keyboard-capture {
  position: absolute;
  top: 0;
  left: -9999em;
  width: 0;
  height: 0;
  padding: 0;
  margin: 0;
  border: 0;
  opacity: 0;
  z-index: -5;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  resize: none;
  background: transparent;
  color: transparent;
  caret-color: transparent;
}

/* ── Trigger ripple: short pulse at the cursor when long-press
      promotes drag, or when 2F gesture locks into scroll/zoom. ──── */
#trigger-ripple {
  position: absolute;
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  border: 2px solid rgba(255, 255, 255, 0.85);
  border-radius: 50%;
  pointer-events: none;
  z-index: 11;
  opacity: 0;
}
#trigger-ripple.active {
  animation: trigger-ripple-pulse 480ms ease-out;
}
@keyframes trigger-ripple-pulse {
  0%   { transform: scale(0.4); opacity: 1; }
  60%  { opacity: 0.55; }
  100% { transform: scale(3.6); opacity: 0; }
}

/* ── Diagnostic overlay (?debug=1) ─────────────────────────────────── */
#debug-log {
  position: fixed;
  top: 48px;
  left: 8px;
  right: 8px;
  max-height: 45vh;
  overflow-y: auto;
  background: rgba(0, 0, 0, 0.78);
  color: #d6f3a0;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 11px;
  line-height: 1.35;
  padding: 6px 8px;
  border: 1px solid #2c3a14;
  border-radius: 4px;
  z-index: 1000;
  pointer-events: auto;
  white-space: pre-wrap;
  word-break: break-all;
}
#debug-log[hidden] {
  display: none;
}
#debug-log .line {
  display: block;
  padding: 1px 0;
}
#debug-log .send {
  color: #ffe082;
}
#debug-log .err {
  color: #ff8a80;
}

/* ── Connection status overlay ──────────────────────────────────────
   Shown at page load and whenever we're waiting on the connect
   pipeline (WS open → relay pair → handshake → first frame). On error
   / mid-session disconnect, `data-state="error"` swaps the spinner
   for an error glyph and turns the card red. Sits below the auth
   modal (z-index 50 vs 100) so a password prompt always wins. */
#connection-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.78);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
}
#connection-overlay[hidden] {
  display: none;
}
#connection-overlay .overlay-card {
  background: #1c1c1c;
  border: 1px solid #333;
  border-radius: 8px;
  padding: 28px 36px;
  min-width: 280px;
  max-width: min(440px, calc(100vw - 32px));
  text-align: center;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
}
#connection-overlay .overlay-spinner {
  width: 36px;
  height: 36px;
  margin: 0 auto 16px;
  border: 3px solid rgba(255, 255, 255, 0.12);
  border-top-color: #5b9aff;
  border-radius: 50%;
  animation: overlay-spin 900ms linear infinite;
}
#connection-overlay[data-state="error"] .overlay-card {
  border-color: #5a2828;
}
#connection-overlay[data-state="error"] .overlay-spinner {
  animation: none;
  border: none;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ef6464;
  font-size: 28px;
  line-height: 1;
}
#connection-overlay[data-state="error"] .overlay-spinner::before {
  content: "\26A0"; /* ⚠ */
}
#connection-overlay .overlay-status {
  color: #fff;
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 6px;
}
#connection-overlay .overlay-detail {
  color: #aaa;
  font-size: 13px;
  min-height: 18px;
  line-height: 1.45;
}
@keyframes overlay-spin {
  to {
    transform: rotate(360deg);
  }
}

/* ── Auth (password) modal ─────────────────────────────────────────── */
#auth-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  /* `hidden` HTML attribute keeps it off until JS toggles. */
}
#auth-modal[hidden] {
  display: none;
}
#auth-modal .modal-card {
  background: #1c1c1c;
  border: 1px solid #333;
  border-radius: 8px;
  padding: 20px 24px;
  min-width: 320px;
  max-width: min(480px, calc(100vw - 32px));
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
}
#auth-modal h2 {
  margin: 0 0 4px;
  font-size: 18px;
  color: #fff;
}
#auth-modal .muted {
  margin: 0 0 14px;
  font-size: 13px;
  color: #888;
}
#auth-modal input[type="password"] {
  width: 100%;
  box-sizing: border-box;
  background: #111;
  border: 1px solid #333;
  border-radius: 4px;
  color: #eee;
  padding: 10px 12px;
  font-size: 16px; /* ≥16px to suppress iOS focus-zoom */
  font-family: inherit;
}
#auth-modal input[type="password"]:focus {
  outline: none;
  border-color: #3a6fa0;
}
#auth-modal .error {
  margin: 8px 0 0;
  color: #ef6464;
  font-size: 13px;
}
#auth-modal .modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}
#auth-modal button {
  background: #1c1c1c;
  color: #ddd;
  border: 1px solid #333;
  padding: 8px 16px;
  border-radius: 4px;
  font: inherit;
  cursor: pointer;
}
#auth-modal button:hover {
  background: #2a2a2a;
  border-color: #555;
}
#auth-modal button[type="submit"] {
  background: #264a73;
  border-color: #3a6fa0;
  color: #fff;
}
#auth-modal button[type="submit"]:hover {
  background: #2e5a8b;
}

