:root {
  --bg: #0f1115;
  --panel: #181b22;
  --key: #232833;
  --key-down: #343b4a;
  --line: #2c3240;
  --text: #eef1f6;
  --muted: #9aa3b2;
  --accent: #4f8cff;
  --ok: #2fbf71;
  --warn: #f0b429;
  --danger: #e5484d;
  --radius: 18px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding: env(safe-area-inset-top) 0 calc(24px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}

.top {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 14px 16px 6px;
  max-width: 480px; margin: 0 auto;
}
.top h1 { font-size: 20px; margin: 0; }

.pill {
  font-size: 13px; padding: 5px 11px; border-radius: 999px;
  background: var(--key); color: var(--muted); white-space: nowrap;
}
.pill[data-state="connecting"] { color: var(--warn); }
.pill[data-state="pairing"] { background: var(--warn); color: #1a1400; }
.pill[data-state="connected"] { background: var(--ok); color: #03170b; }

main { max-width: 480px; margin: 0 auto; padding: 0 16px; }

.card {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px; margin: 10px 0;
}

label { display: block; font-size: 14px; color: var(--muted); margin-bottom: 6px; }

.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; margin-top: 10px; }

input {
  flex: 1; min-width: 0; font-size: 18px; padding: 12px 14px;
  border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text);
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

button, .ghost {
  font: inherit; color: inherit; cursor: pointer;
  border: 0; border-radius: 12px; touch-action: manipulation;
}
.primary { background: var(--accent); color: #fff; padding: 12px 16px; font-weight: 600; white-space: nowrap; }
.ghost {
  background: transparent; border: 1px solid var(--line); padding: 9px 12px;
  text-decoration: none; font-size: 14px; display: inline-block;
}
.ghost[aria-disabled="true"] { opacity: .5; }

.message { margin: 10px 0 0; font-size: 15px; }
.message[data-kind="error"] { color: #ffb4b6; }
.message[data-kind="info"] { color: var(--muted); }

/* Kumanda */
.remote { padding: 6px 0 4px; user-select: none; -webkit-user-select: none; }
body:not([data-state="connected"]) .remote .key { opacity: .55; }

.key {
  background: var(--key); border-radius: var(--radius);
  min-height: 64px; font-size: 24px; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px;
  box-shadow: inset 0 -2px 0 rgba(0,0,0,.35);
  transition: background .08s, transform .08s;
}
.key small { font-size: 12px; color: var(--muted); }
.key.pressed, .key:active { background: var(--key-down); transform: scale(.96); }
.key.power { color: var(--danger); }

.row3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 10px 0; }

.dpad {
  display: grid; margin: 14px auto;
  grid-template-columns: 1fr 1.15fr 1fr; grid-template-rows: 1fr 1.15fr 1fr;
  grid-template-areas: ". up ." "left ok right" ". down .";
  gap: 8px; width: min(100%, 320px); aspect-ratio: 1;
}
.dpad .key { min-height: 0; font-size: 26px; }
.dpad .up { grid-area: up; }
.dpad .down { grid-area: down; }
.dpad .left { grid-area: left; }
.dpad .right { grid-area: right; }
.dpad .ok { grid-area: ok; border-radius: 50%; background: var(--accent); color: #fff; font-weight: 700; font-size: 22px; }
.dpad .ok.pressed, .dpad .ok:active { background: #3b73de; }

.rockers { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin: 12px 0; }
.rocker {
  display: grid; grid-template-rows: 72px auto 72px; gap: 6px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 26px; padding: 8px;
  text-align: center;
}
.rocker span { font-size: 12px; letter-spacing: .08em; color: var(--muted); }
.rocker .key { font-size: 30px; border-radius: 20px; }

.note { font-size: 13px; color: var(--muted); margin: 8px 2px; }

details summary { cursor: pointer; font-weight: 600; }
details ol { padding-left: 20px; margin: 10px 0 0; }
details li { margin-bottom: 8px; }
pre {
  white-space: pre-wrap; word-break: break-all; font-size: 12px; color: var(--muted);
  max-height: 240px; overflow: auto; margin: 10px 0 0;
}

dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--line);
  border-radius: var(--radius); width: min(92vw, 420px); padding: 16px;
}
dialog::backdrop { background: rgba(0,0,0,.6); }
dialog h2 { margin: 0 0 10px; font-size: 18px; }
.list { display: grid; gap: 8px; margin-bottom: 12px; }
.item { background: var(--key); padding: 14px; text-align: left; font-size: 17px; }

.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom));
  max-width: 448px; margin: 0 auto; background: #2a1215; color: #ffd4d6;
  border: 1px solid var(--danger); border-radius: 12px; padding: 12px 14px; font-size: 14px;
}
