:root {
  color-scheme: dark;
  --bg: #111319;
  --panel: #1b1f2a;
  --panel-border: #2f3647;
  --text: #e6e9ef;
  --muted: #9ca7ba;
  --accent: #7fc4ff;
  --accent-strong: #4fa9ff;
  --danger: #ff7f7f;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background:
    radial-gradient(circle at top, #24314f 0%, transparent 35%),
    linear-gradient(180deg, #161a23 0%, var(--bg) 60%);
  color: var(--text);
  font: 14px/1.4 "SF Mono", "IBM Plex Mono", "Cascadia Code", monospace;
}

.shell {
  width: min(1280px, 100%);
  margin: 0 auto;
  padding: 24px;
  display: grid;
  gap: 20px;
  grid-template-columns: 320px minmax(340px, 420px) 1fr;
}

.panel {
  background: rgba(27, 31, 42, 0.92);
  border: 1px solid var(--panel-border);
  border-radius: 18px;
  padding: 18px;
  box-shadow: 0 18px 60px rgba(0, 0, 0, 0.32);
}

h1, h2 {
  margin: 0 0 14px;
  font-weight: 700;
}

.field {
  display: grid;
  gap: 6px;
  margin-bottom: 14px;
}

.field span {
  color: var(--muted);
}

input[type="text"],
input[type="file"],
select {
  width: 100%;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  background: #121621;
  color: var(--text);
  padding: 10px 12px;
}

.field-hint {
  color: var(--muted);
  font-size: 12px;
}

.actions,
.serial-header,
.serial-actions,
.controls-grid,
.action-buttons,
.dpad {
  display: flex;
  gap: 10px;
}

.actions {
  margin-top: 8px;
}

button {
  border: 1px solid var(--panel-border);
  background: #161b27;
  color: var(--text);
  border-radius: 12px;
  padding: 10px 14px;
  cursor: pointer;
  font: inherit;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

button:not(:disabled):hover {
  border-color: var(--accent);
}

button.control-active:not(:disabled),
button[aria-pressed="true"]:not(:disabled) {
  border-color: var(--accent);
  background: #203246;
  box-shadow: inset 0 0 0 1px rgba(127, 196, 255, 0.22);
}

.status {
  margin: 12px 0 0;
  color: var(--muted);
  min-height: 2.8em;
}

.screen-panel {
  display: grid;
  gap: 16px;
  justify-items: center;
}

.screen-wrap {
  width: 100%;
  display: grid;
  place-items: center;
  padding: 10px;
  border-radius: 18px;
  background:
    linear-gradient(180deg, rgba(255,255,255,0.03), rgba(0,0,0,0.14)),
    #0d111a;
}

.device-frame {
  position: relative;
  width: min(100%, 460px);
  aspect-ratio: 1024 / 1601;
  filter: drop-shadow(0 28px 50px rgba(0, 0, 0, 0.35));
}

.screen-viewport {
  position: absolute;
  left: 18.2617%;
  top: 13.6165%;
  width: 62.8906%;
  height: 53.6540%;
  display: grid;
  place-items: center;
  overflow: hidden;
  background: #000;
  z-index: 1;
}

.device-frame-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
  z-index: 2;
}

#screen {
  width: auto;
  max-width: 100%;
  height: 100%;
  image-rendering: pixelated;
  background: #000;
  touch-action: none;
}

.controls-grid {
  width: 100%;
  justify-content: space-between;
  align-items: center;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 70px);
  grid-template-rows: repeat(3, 50px);
}

.dpad button:nth-child(1) { grid-column: 2; grid-row: 1; }
.dpad button:nth-child(2) { grid-column: 1; grid-row: 2; }
.dpad button:nth-child(3) { grid-column: 3; grid-row: 2; }
.dpad button:nth-child(4) { grid-column: 2; grid-row: 3; }

.action-buttons {
  flex-direction: column;
}

.action-buttons button {
  min-width: 100px;
}

.serial-panel {
  height: clamp(320px, 70vh, 620px);
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
  overflow: hidden;
}

.serial-header {
  justify-content: space-between;
  align-items: center;
}

.serial-actions {
  align-items: center;
}

.serial-panel.collapsed {
  height: auto;
  grid-template-rows: auto;
}

.serial-panel.collapsed #serial {
  display: none;
}

#serial {
  margin: 0;
  min-height: 0;
  height: 100%;
  background: #0d111a;
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 14px;
  overflow-y: auto;
  overflow-x: hidden;
  white-space: pre-wrap;
  word-break: break-word;
}

@media (max-width: 1080px) {
  .shell {
    grid-template-columns: 1fr;
  }

  .controls-grid {
    flex-direction: column;
  }

  .serial-panel {
    height: clamp(260px, 40vh, 420px);
  }
}
