:root { color-scheme: dark; }
* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; background: #000; overflow: hidden;
  font: 13px/1.45 "Cascadia Code", Consolas, monospace;
}
#app { position: fixed; inset: 0 auto auto 0; width: 100%; height: 100%; overflow: hidden; }
#app:fullscreen { position: fixed; inset: 0; width: 100% !important; height: 100% !important; }
#app button, #app button *, #ctl, #viewTools, #scrollTools {
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
#stage {
  height: 100%; padding: 0; overflow: hidden;
}
#viewport {
  width: 100%; height: 100%; overflow: hidden; position: relative;
  display: flex; align-items: center; justify-content: center;
  touch-action: none; overscroll-behavior: none;
}
#screen {
  flex-shrink: 0;
  outline: none; background: #111;
  touch-action: none;              /* we own all touch gestures */
  transform-origin: center; will-change: transform;
}
#screen:focus-visible { box-shadow: 0 0 0 2px #4f8cff; }
#hud {
  display: none; position: absolute;
  top: max(10px, env(safe-area-inset-top));
  left: max(10px, env(safe-area-inset-left));
  z-index: 10;
  padding: 6px 10px; border-radius: 6px;
  background: rgba(0, 0, 0, .55); color: #9ecbff;
  max-width: calc(100vw - 130px); font-size: 11px;
  white-space: normal; pointer-events: none; backdrop-filter: blur(4px);
}
#app.show-stats #hud { display: block; }
#btnStats {
  position: absolute; top: max(10px, env(safe-area-inset-top));
  right: max(10px, env(safe-area-inset-right)); z-index: 10;
  min-height: 36px; padding: 6px 10px; border: 1px solid #31425a;
  border-radius: 8px; background: #111b2bee; color: #dceaff; font: inherit;
}
#btnStats[aria-pressed="true"] { background: #245caa; }
/* Touch controls are only needed when the viewer uses a touch pointer. */
#ctl {
  position: absolute;
  bottom: max(10px, env(safe-area-inset-bottom));
  left: 50%; transform: translateX(-50%);
  z-index: 10;
  display: none; gap: 8px;
  padding: 6px; border: 1px solid #31425a; border-radius: 14px;
  background: #111b2bee;
}
#ctl.show { visibility: visible; opacity: 1; }
#btnLeft, #btnRight { display: none; }
#ctl button {
  min-width: 44px; height: 44px; padding: 0 12px; border: 0; border-radius: 8px;
  background: #1c2b42; color: #dceaff; font: inherit; white-space: nowrap;
  backdrop-filter: blur(4px);
}
#ctl button:active { background: rgba(79, 140, 255, .5); }
#ctl button[aria-pressed="true"] { background: #245caa; box-shadow: inset 0 0 0 1px #83b7ff; }
@media (pointer: coarse) {
  #ctl { display: flex; visibility: visible; opacity: 1; }
  #btnLeft, #btnRight { display: block; }
  #ctl { gap: 4px; padding: 4px; max-width: calc(100vw - 12px); }
  #ctl button { min-width: 44px; padding: 0 6px; font-size: 11px; }
}
#viewTools {
  display: none; position: absolute; z-index: 10;
  top: calc(58px + env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
  max-width: calc(100vw - 20px); gap: 4px; align-items: center;
  padding: 5px; border: 1px solid #31425a; border-radius: 12px; background: #111b2bee;
}
#viewTools button {
  min-width: 44px; min-height: 44px; padding: 6px 10px; border: 0; border-radius: 8px;
  font: inherit; background: #1c2b42; color: #dceaff; white-space: nowrap;
}
#zoomLevel { min-width: 42px; text-align: center; color: #c7dcff; font-size: 11px; }
#viewTools button[aria-pressed="true"] { background: #245caa; }
#viewTools button:disabled { opacity: .4; cursor: default; }
@media (pointer: coarse) {
  #viewTools { display: flex; }
  #stage { padding: calc(116px + env(safe-area-inset-top)) 0 calc(80px + env(safe-area-inset-bottom)); }
}
@media (max-width: 360px) {
  #viewTools { gap: 2px; padding: 4px; }
  #viewTools button { padding: 6px; font-size: 11px; }
}
#scrollTools {
  display: none; position: absolute; z-index: 10;
  right: max(8px, env(safe-area-inset-right)); top: 50%;
  transform: translateY(-50%); flex-direction: column; gap: 8px;
}
#scrollTools button {
  width: 48px; height: 48px; border: 1px solid #31425a; border-radius: 12px;
  background: #111b2bee; color: #dceaff; font: 24px/1 system-ui, sans-serif;
  touch-action: none;
}
#scrollTools button:active { background: #245caa; }
@media (pointer: coarse) { #scrollTools { display: flex; } }
#keyPanel {
  position: absolute; z-index: 11; bottom: calc(78px + env(safe-area-inset-bottom));
  right: max(10px, env(safe-area-inset-right)); width: min(560px, calc(100vw - 20px));
  max-height: calc(75% - 78px); overflow-y: auto; overscroll-behavior: contain;
  padding: 16px; border: 1px solid #31425a; border-radius: 16px;
  color: #dceaff; background: #101a2bf5; box-shadow: 0 8px 30px #0008;
  font-family: system-ui, sans-serif;
}
#keyPanel.hidden { display: none; }
@media (pointer: fine) {
  #keyPanel { display: none; }
}
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
#keyPanel button, #keyPanel select, #keyPanel textarea {
  min-height: 44px; border: 1px solid #354a69; border-radius: 8px;
  background: #1a2940; color: #eaf3ff; padding: 8px; font: inherit;
}
button, summary, select { cursor: pointer; }
button:focus-visible, summary:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid #78aeff; outline-offset: 2px;
}
#keyPanel button:hover { background: #2a4162; }
#keyPanel button[aria-pressed="true"], #ctl button[aria-expanded="true"] { background: #245caa; border-color: #83b7ff; }
.quality-label { display: flex; align-items: center; gap: 12px; margin: 12px 0; }
.quality-label select { flex: 1; min-width: 0; }
.key-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin: 10px 0; }
.key-grid small { display: block; font-size: 10px; color: #b8cce8; }
.keyboard { grid-template-columns: repeat(6, minmax(0, 1fr)); }
#keyPanel details { border-top: 1px solid #31425a; padding: 12px 0 0; margin-top: 12px; }
#keyPanel summary { padding: 6px 0; }
.panel-hint { font-size: 12px; line-height: 1.6; color: #9aafcb; margin: 8px 0; }
#remoteText { width: 100%; margin: 10px 0; resize: vertical; font-size: 16px !important; }
#controlStatus { min-height: 20px; color: #9ecbff; font-size: 12px; }
@media (max-width: 420px) {
  #keyPanel { padding: 12px; }
  .shortcuts { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .keyboard { grid-template-columns: repeat(5, minmax(0, 1fr)); }
}
/* The capture field stays inside the visible viewport before focus. */
#kb {
  position: absolute; bottom: calc(60px + env(safe-area-inset-bottom)); left: 8px;
  width: calc(100% - 16px); height: 44px; opacity: 0; pointer-events: none;
  border: 0; padding: 0; resize: none; background: transparent; color: transparent;
}
#ov {
  position: absolute; inset: 0; z-index: 20;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, .82); color: #ddd;
}
#ov.hidden { display: none; }
#ovtext { max-width: 34em; padding: 1em; text-align: center; white-space: pre-wrap; }

/* code-entry screen (relay mode) */
#codebox {
  position: fixed; inset: 0; z-index: 30; width: 100vw; height: 100dvh;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 18px;
  background:
    radial-gradient(900px 500px at 50% 12%, rgba(79, 140, 255, .14), transparent 60%),
    radial-gradient(1200px 700px at 50% 105%, #0b1526, #04060b 70%);
}
/* Keep the remote field and the text being sent visible above the phone keyboard. */
#app.typing #hud { display: none; }
#app.typing #viewTools { display: none; }
#app.typing #stage { padding-top: max(8px, env(safe-area-inset-top)); padding-bottom: calc(116px + env(safe-area-inset-bottom)); }
#app.typing #ctl { bottom: max(6px, env(safe-area-inset-bottom)); padding: 4px; }
#app.typing #scrollTools {
  top: auto; bottom: calc(60px + env(safe-area-inset-bottom)); right: 8px;
  transform: none; flex-direction: row;
}
#app.typing #kb {
  z-index: 11; width: calc(100% - 128px); opacity: 1; pointer-events: auto;
  border: 1px solid #4f8cff; border-radius: 10px; padding: 10px 12px;
  background: #101a2b; color: #eaf3ff; font: 16px/1.4 system-ui, sans-serif;
}
#app.typing #keyPanel { bottom: calc(64px + env(safe-area-inset-bottom)); max-height: calc(100% - 128px); }
#codebox.hidden { display: none; }
#codeform {
  display: flex; flex-direction: column; gap: 14px; align-items: center;
  width: min(92vw, 24em);
  padding: 2.4em 2.2em 1.8em; border-radius: 18px;
  background: rgba(13, 22, 38, .72);
  border: 1px solid rgba(120, 170, 255, .14);
  box-shadow: 0 24px 70px rgba(0, 0, 0, .55),
              inset 0 1px 0 rgba(255, 255, 255, .05);
  backdrop-filter: blur(8px);
}
#codeform .brand {
  display: flex; align-items: center; gap: 10px;
  font-size: 26px; color: #dceaff; letter-spacing: .32em; margin-left: .32em;
}
#codeform .brand svg { filter: drop-shadow(0 0 10px rgba(79, 140, 255, .5)); }
#codeform .tagline { margin: 0; color: #8fa3bd; font-size: 13px; }
#codeform label {
  color: #9ecbff; font-size: 12px; letter-spacing: .18em; text-transform: uppercase;
}
#codeinput {
  width: 100%; box-sizing: border-box;
  padding: .62em 0; text-align: center;
  font: inherit; font-size: 30px; letter-spacing: .5em; text-indent: .5em; color: #eaf3ff;
  background: rgba(2, 6, 14, .6); border: 1px solid #24395a; border-radius: 12px;
  outline: none; transition: border-color .15s, box-shadow .15s;
}
#codeinput:focus {
  border-color: #4f8cff;
  box-shadow: 0 0 0 3px rgba(79, 140, 255, .22);
}
#codeform button {
  width: 100%; padding: .85em 2.4em; font: inherit; font-size: 15px;
  color: #eaf3ff; letter-spacing: .08em; cursor: pointer;
  background: linear-gradient(180deg, #2c5aa0, #1d3f78);
  border: 1px solid rgba(140, 190, 255, .28); border-radius: 12px;
  transition: filter .15s, transform .06s;
}
#codeform button:hover { filter: brightness(1.15); }
#codeform button:disabled, #codeinput:disabled { opacity: .55; cursor: not-allowed; }
#codeform button:active { transform: translateY(1px); }
#codeerr { min-height: 1.3em; color: #ff9c8a; font-size: 13px; }
#codeform .hint { margin: .2em 0 0; color: #5d7290; font-size: 12px; text-align: center; }
#codebox .foot { margin: 0; color: #465a78; font-size: 11px; letter-spacing: .06em; }
