:root {
  --blue: #1565C0;
  --blue-dark: #0D47A1;
  --bg: #f4f7fb;
  --card: #ffffff;
  --text: #1a2333;
  --muted: #6b7a90;
  --border: #d7e1ee;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10161f;
    --card: #1a2330;
    --text: #e8eef7;
    --muted: #8fa1b8;
    --border: #2a3a50;
  }
}
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html { height: 100%; }
body {
  /* min-height (not fixed height) so the page can grow and SCROLL when the
     controls don't fit the mobile viewport; dvh accounts for the URL bar. */
  min-height: 100vh;
  min-height: 100dvh;
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
header {
  background: var(--blue);
  color: #fff;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  margin: calc(-1 * env(safe-area-inset-top)) calc(-1 * env(safe-area-inset-right)) 0 calc(-1 * env(safe-area-inset-left));
  display: flex;
  align-items: baseline;
  gap: 12px;
}
header h1 { font-size: 1.15rem; }
.status { font-size: 0.8rem; opacity: 0.85; }
.status.ok { color: #9ff3b1; }
.status.bad { color: #ffc9c9; }
.spacer { flex: 1; }
.hdr-btn {
  border: 1px solid rgba(255, 255, 255, 0.5);
  background: transparent;
  color: #fff;
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 0.9rem;
  cursor: pointer;
}
.hdr-btn[hidden] { display: none; }
.menu-item {
  text-align: left;
  width: 100%;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  font-size: 0.95rem;
}
.prose { font-size: 0.9rem; line-height: 1.5; user-select: text; -webkit-user-select: text; }
.prose p { margin-bottom: 10px; }
.prose code {
  display: inline-block;
  background: color-mix(in srgb, var(--blue) 12%, var(--card));
  padding: 3px 7px;
  border-radius: 6px;
  font-size: 0.82em;
  word-break: break-all;
  user-select: all;
  -webkit-user-select: all;
}
.prose a, .hint a { color: var(--blue); font-weight: 600; }
@media (prefers-color-scheme: dark) { .prose a, .hint a { color: #7db3f0; } }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  margin: 16px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.hint { color: var(--muted); font-size: 0.8rem; }

#padUI {
  flex: 1 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
}

.row { display: flex; gap: 8px; }
.grow { flex: 1; }

button {
  font: inherit;
  font-size: 0.9rem;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  cursor: pointer;
}
button:active { background: var(--blue); color: #fff; }
button.primary { background: var(--blue); border-color: var(--blue); color: #fff; }
button.mod.on { background: var(--blue-dark); border-color: var(--blue-dark); color: #fff; }
button:disabled { cursor: default; opacity: 0.55; }
#voiceBtn { flex: none; white-space: nowrap; }
#voiceBtn.listening {
  color: #fff;
  background: #c62828;
  border-color: #c62828;
  animation: voice-pulse 1.4s ease-in-out infinite;
}
@keyframes voice-pulse {
  50% { box-shadow: 0 0 0 5px rgba(198, 40, 40, 0.18); }
}
@media (prefers-reduced-motion: reduce) {
  #voiceBtn.listening { animation: none; }
}

input[type="text"], input[type="password"] {
  font: inherit;
  flex: 1;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  user-select: text;
  -webkit-user-select: text;
}

.row input { min-width: 0; }

#touchpad {
  /* Grows to fill spare space but has a sensible floor; on short screens the
     page scrolls rather than the touchpad crushing the rows below it. */
  flex: 1 1 auto;
  min-height: 22vh;
  height: 30vh;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: color-mix(in srgb, var(--blue) 8%, var(--card));
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.8rem;
  text-align: center;
  touch-action: none;
}

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chips.scroll { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
.chips.scroll::-webkit-scrollbar { display: none; }
.chips button { white-space: nowrap; font-size: 0.8rem; padding: 8px 10px; }
.chips button.active { background: var(--blue); border-color: var(--blue); color: #fff; }

/* Overlays / bottom sheets (emoji picker, add snippet) */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
}
.overlay[hidden] { display: none; }
.sheet {
  width: 100%;
  max-width: 520px;
  background: var(--card);
  border-radius: 16px 16px 0 0;
  padding: 14px 14px calc(14px + env(safe-area-inset-bottom));
  max-height: 70vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 700;
}
.sheet-head button {
  border: none;
  background: transparent;
  font-size: 1.1rem;
  padding: 4px 8px;
}
.emoji-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
}
.emoji-grid button {
  font-size: 1.6rem;
  padding: 8px 0;
  border: none;
  background: transparent;
  border-radius: 10px;
}
.emoji-grid button:active { background: var(--blue); }

/* Quick-keys manager list */
.mgr-list { display: flex; flex-direction: column; gap: 6px; max-height: 40vh; overflow-y: auto; }
.mgr-item { display: flex; gap: 6px; align-items: stretch; }
.mgr-item .mgr-send {
  flex: 1;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.mgr-item .mgr-icon { width: 44px; flex: none; }
.mgr-empty { color: var(--muted); font-size: 0.85rem; padding: 6px 2px; }

/* ---- Fullscreen input modes ---- */
.mode-launchers { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.mode-launchers .span-all { grid-column: 1 / -1; }
body.input-open { overflow: hidden; }
#kbView {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: #0d1522;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
#kbView[hidden] { display: none; }
.kb-bar { display: flex; align-items: center; gap: 8px; padding: 6px 8px; }
.kb-status { color: #bbd6f5; font-size: 0.8rem; }
#inputMode {
  min-width: 0; max-width: 75%; min-height: 44px;
  font: inherit; font-size: 0.9rem; padding: 6px;
  color: #e8eef7; background: #1a2330; border: 1px solid #2a3a50; border-radius: 8px;
}
#kbClose, #gpPreset { min-height: 44px; white-space: nowrap; }
#kbView [hidden] { display: none !important; }
#kbView button { touch-action: manipulation; }
.kb-body { flex: 1; display: flex; gap: 6px; padding: 6px; min-height: 0; }
.kb-keys { flex: 2.4; display: flex; flex-direction: column; gap: 3px; }
.kb-krow { flex: 1; display: flex; gap: 3px; }
.kb-krow button {
  flex: 1; min-width: 0; padding: 0;
  font-size: 0.8rem; line-height: 1; border-radius: 7px;
  white-space: nowrap; overflow: hidden;
  border: 1px solid #2a3a50; background: #1a2330; color: #e8eef7;
}
.kb-krow button:active { background: var(--blue); }
.kb-krow button.wide { flex: 1.7; }
.kb-krow button.space { flex: 4; }
.kb-krow button.mod.on { background: var(--blue-dark); border-color: var(--blue); color: #fff; }
.kb-right { flex: 1; display: flex; flex-direction: column; gap: 6px; min-height: 0; }
.kb-pad {
  flex: 1;
  border: 1px solid #2a3a50; border-radius: 12px;
  background: #12233b;
  display: flex; align-items: center; justify-content: center;
  color: #a5b8d0; font-size: 0.75rem; text-align: center; padding: 8px;
  touch-action: none;
}
.kb-pad.held { background: #1c3355; }
.kb-clicks { display: flex; gap: 6px; }
.kb-clicks button {
  padding: 8px; border-radius: 8px;
  border: 1px solid #2a3a50; background: #1a2330; color: #e8eef7;
}
.kb-clicks button:active { background: var(--blue); }
#kbView[data-mode="touchpad"] .kb-clicks button { min-height: 64px; font-size: 1.1rem; }
#focusedPad { flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; gap: 6px; }
#focusedKeys {
  flex: 1; min-height: 0; overflow-y: auto;
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px;
}
#focusedKeys button {
  min-width: 0; min-height: 48px; padding: 4px;
  font-size: clamp(1.3rem, 5vmin, 2.4rem);
  border-color: #2a3a50; background: #1a2330; color: #e8eef7;
}
#focusedKeys button:active { background: var(--blue); }
#focusedEdits button { flex: 1; min-width: 0; min-height: 52px; font-size: 1.1rem; }
#kbView[data-mode="numbers"] #focusedKeys { grid-template-rows: repeat(4, minmax(48px, 1fr)); }
#kbView[data-mode="symbols"] #focusedKeys { grid-template-rows: repeat(8, minmax(48px, 1fr)); }
#kbView[data-mode="emoji"] #focusedKeys { grid-auto-rows: minmax(64px, 1fr); }
#kbView[data-mode="emoji"] #focusedKeys button { font-size: 2rem; }
@media (orientation: landscape) {
  #kbView[data-mode="symbols"] #focusedKeys,
  #kbView[data-mode="emoji"] #focusedKeys { grid-template-columns: repeat(8, minmax(0, 1fr)); }
  #kbView[data-mode="symbols"] #focusedKeys { grid-template-rows: repeat(4, minmax(48px, 1fr)); }
}

/* ---- Game pad: D-pad + face cluster mirrored, sized to fit both axes ---- */
#gamePad {
  flex: 1; min-width: 0; min-height: 0;
  display: grid; gap: 8px;
  grid-template-columns: 1fr auto 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas: "l . r" "dpad mid face";
  touch-action: none; -webkit-touch-callout: none;
  --gp: min(40vw, calc(100vh - 150px));
  --gp: min(40vw, calc(100dvh - 150px));
}
#gamePad button, #gamePad button:active {
  padding: 0; min-width: 0; touch-action: none;
  border: 1px solid #2a3a50; background: #1a2330; color: #e8eef7;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  line-height: 1.1; font-weight: 600;
}
#gamePad button.on { background: var(--blue); border-color: var(--blue); color: #fff; }
#gamePad small { display: block; font-size: 0.75em; font-weight: 400; color: #a5b8d0; }
#gamePad button.on small { color: #fff; }
.gp-shoulder { width: min(26vw, 100%); min-height: 48px; font-size: 1.1rem; border-radius: 12px; }
.gp-l { grid-area: l; justify-self: start; }
.gp-r { grid-area: r; justify-self: end; }
.gp-mid { grid-area: mid; display: flex; gap: 8px; align-self: center; justify-content: center; }
#gamePad .gp-pill { min-width: 76px; min-height: 44px; border-radius: 22px; font-size: 0.85rem; }
.gp-dpad, .gp-face {
  position: relative; width: var(--gp); height: var(--gp);
  align-self: end; justify-self: center;
}
.gp-dpad { grid-area: dpad; border-radius: 50%; background: #12233b; }
.gp-face { grid-area: face; }
/* Arms: 36% wide, reaching 46% from the center (matches GamepadView). */
#gamePad .gp-arm { position: absolute; font-size: calc(var(--gp) * 0.13); }
#gamePad .gp-arm small { font-size: 1em; font-weight: 700; color: #e8eef7; }
.gp-up { left: 32%; top: 4%; width: 36%; height: 46%; border-radius: 10px 10px 0 0; }
.gp-down { left: 32%; bottom: 4%; width: 36%; height: 46%; border-radius: 0 0 10px 10px; }
.gp-left { top: 32%; left: 4%; width: 46%; height: 36%; border-radius: 10px 0 0 10px; }
.gp-right { top: 32%; right: 4%; width: 46%; height: 36%; border-radius: 0 10px 10px 0; }
.gp-dpad::after {   /* hub over the arms' inner ends */
  content: ''; position: absolute; left: 32%; top: 32%; width: 36%; height: 36%;
  background: #1a2330; pointer-events: none;
}
/* Face buttons: 34% circles in a diamond, Xbox colors. */
#gamePad .gp-btn {
  position: absolute; width: 34%; height: 34%; border-radius: 50%;
  border: 3px solid var(--c); color: var(--c); font-size: calc(var(--gp) * 0.13); font-weight: 700;
}
#gamePad .gp-btn small { font-size: 0.42em; }
#gamePad .gp-btn.on { background: var(--c); border-color: var(--c); color: #fff; }
.gp-y { --c: #eab308; left: 33%; top: 2%; }
.gp-x { --c: #3b82f6; left: 2%; top: 33%; }
.gp-b { --c: #e5484d; left: 64%; top: 33%; }
.gp-a { --c: #3ba55c; left: 33%; top: 64%; }
/* Portrait (no orientation lock, e.g. iOS): everything stacked at the bottom,
   within thumb reach — shoulders, Select/Start, then the clusters. */
@media (orientation: portrait) {
  #gamePad {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr auto auto auto;
    grid-template-areas: ". ." "l r" "mid mid" "dpad face";
  }
  .gp-shoulder { width: min(40vw, 100%); }
}
