:root {
  color-scheme: dark;
  --bg: #0f1114;
  --panel: rgba(22, 25, 29, 0.9);
  --panel-solid: #171a1f;
  --line: rgba(236, 232, 224, 0.14);
  --line-strong: rgba(236, 232, 224, 0.28);
  --text: #f7f2e8;
  --muted: rgba(247, 242, 232, 0.67);
  --quiet: rgba(247, 242, 232, 0.45);
  --green: #42d1a3;
  --blue: #79a8ff;
  --amber: #e2b64d;
  --red: #f06d5e;
  --violet: #b58cff;
  --shadow: 0 18px 58px rgba(0, 0, 0, 0.38);
  --mono: "SFMono-Regular", "Cascadia Code", "Menlo", monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Inter", "Avenir Next", "Segoe UI", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  overflow-x: hidden;
  color: var(--text);
  background: var(--bg);
  font-family: var(--sans);
}

button,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

#arenaCanvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.app-shell {
  position: relative;
  z-index: 1;
  min-height: 100vh;
  padding: 16px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin: 0 auto 12px;
  max-width: 1440px;
}

.eyebrow {
  margin: 0 0 2px;
  color: var(--quiet);
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(1.55rem, 3vw, 2.35rem);
  line-height: 1;
  letter-spacing: 0;
}

h2 {
  font-size: 0.92rem;
  letter-spacing: 0;
}

.status-strip {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(16, 18, 21, 0.84);
  color: var(--muted);
  font-size: 0.84rem;
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.status-strip a {
  color: var(--green);
  text-decoration: none;
}

.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--amber);
  box-shadow: 0 0 18px rgba(226, 182, 77, 0.7);
}

.dot.connected {
  background: var(--green);
  box-shadow: 0 0 18px rgba(66, 209, 163, 0.7);
}

.dot.error {
  background: var(--red);
  box-shadow: 0 0 18px rgba(240, 109, 94, 0.7);
}

.divider {
  width: 1px;
  height: 16px;
  background: var(--line);
}

.ops-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  max-width: 1440px;
  margin: 0 auto 12px;
}

.metric-card {
  min-width: 0;
  padding: 12px 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(18, 21, 25, 0.82);
  box-shadow: 0 12px 34px rgba(0, 0, 0, 0.24);
  backdrop-filter: blur(18px);
}

.metric-card span,
.metric-card small {
  display: block;
  overflow: hidden;
  color: var(--quiet);
  font-family: var(--mono);
  font-size: 0.68rem;
  line-height: 1.3;
  text-overflow: ellipsis;
  text-transform: uppercase;
  white-space: nowrap;
}

.metric-card strong {
  display: block;
  margin: 5px 0 3px;
  overflow: hidden;
  color: var(--text);
  font-size: 1.05rem;
  line-height: 1.15;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.metric-card[data-state="connected"],
.metric-card[data-state="active"] {
  border-color: rgba(66, 209, 163, 0.38);
}

.metric-card[data-state="building"],
.metric-card[data-state="connecting"] {
  border-color: rgba(226, 182, 77, 0.42);
}

.metric-card[data-state="error"] {
  border-color: rgba(240, 109, 94, 0.46);
}

.workspace {
  display: grid;
  grid-template-columns: minmax(220px, 0.72fr) minmax(420px, 1.7fr) minmax(240px, 0.86fr);
  gap: 12px;
  max-width: 1440px;
  margin: 0 auto;
}

.panel {
  min-height: calc(100vh - 162px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 54px;
  padding: 13px 14px;
  border-bottom: 1px solid var(--line);
}

.chat-head p,
.build-panel .panel-head p {
  margin-top: 4px;
  color: var(--quiet);
  font-family: var(--mono);
  font-size: 0.76rem;
}

.head-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.run-pill {
  min-width: 72px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.7rem;
  text-align: center;
}

.run-pill[data-state="building"] {
  border-color: rgba(226, 182, 77, 0.48);
  color: var(--amber);
}

.run-pill[data-state="error"] {
  border-color: rgba(240, 109, 94, 0.48);
  color: var(--red);
}

.tag {
  padding: 4px 7px;
  border: 1px solid rgba(121, 168, 255, 0.34);
  border-radius: 6px;
  color: var(--blue);
  font-family: var(--mono);
  font-size: 0.72rem;
}

.ghost-button,
.ghost-link,
.prompt-chips button,
#sendButton {
  min-height: 36px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  color: var(--text);
  background: rgba(244, 239, 227, 0.06);
}

.ghost-button {
  padding: 0 10px;
  color: var(--muted);
}

.ghost-link {
  display: inline-flex;
  align-items: center;
  padding: 0 10px;
  color: var(--muted);
  text-decoration: none;
}

.signal-list,
.build-list {
  display: grid;
  gap: 8px;
  padding: 12px;
}

.signal-item,
.build-item {
  display: grid;
  gap: 7px;
  padding: 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(247, 242, 232, 0.04);
}

.signal-item strong,
.build-item strong {
  font-size: 0.88rem;
}

.signal-item span,
.build-item span,
.build-item a {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.74rem;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.signal-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.signal-id {
  color: var(--text);
}

.signal-footer,
.build-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.signal-kind {
  min-width: 42px;
  padding: 3px 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--quiet);
  text-align: center;
}

.signal-state {
  color: var(--green);
}

.chat-panel {
  display: grid;
  grid-template-rows: auto 1fr auto auto;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 420px;
  max-height: calc(100vh - 288px);
  padding: 14px;
  overflow-y: auto;
}

.message {
  max-width: min(760px, 92%);
  padding: 12px 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
  line-height: 1.48;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message.user {
  align-self: flex-end;
  border-color: rgba(66, 209, 163, 0.32);
  background: rgba(66, 209, 163, 0.1);
}

.message.engine {
  align-self: flex-start;
  background: rgba(244, 239, 227, 0.055);
}

.message.system {
  align-self: center;
  max-width: 100%;
  padding: 7px 10px;
  color: var(--quiet);
  background: rgba(121, 168, 255, 0.08);
  font-family: var(--mono);
  font-size: 0.74rem;
}

.message.error {
  border-color: rgba(240, 109, 94, 0.42);
  background: rgba(240, 109, 94, 0.1);
}

.message a {
  color: var(--green);
}

.prompt-chips {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  padding: 0 14px 12px;
}

.prompt-chips button {
  padding: 9px 10px;
  color: var(--muted);
  text-align: left;
}

.prompt-chips button:hover,
.ghost-button:hover,
#sendButton:hover {
  border-color: rgba(66, 209, 163, 0.52);
  background: rgba(66, 209, 163, 0.09);
}

.composer {
  display: grid;
  grid-template-columns: 1fr 84px;
  gap: 10px;
  padding: 12px 14px 14px;
  border-top: 1px solid var(--line);
}

#promptInput {
  width: 100%;
  min-height: 48px;
  max-height: 140px;
  resize: vertical;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 12px;
  color: var(--text);
  background: rgba(5, 7, 9, 0.56);
  outline: none;
}

#promptInput:focus {
  border-color: rgba(66, 209, 163, 0.62);
}

#sendButton {
  min-height: 48px;
  color: #07110e;
  background: var(--green);
  border-color: var(--green);
  font-weight: 700;
}

#sendButton:disabled {
  cursor: wait;
  opacity: 0.62;
}

.build-item a {
  color: var(--green);
  text-decoration: none;
}

.build-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.build-open {
  width: max-content;
  padding: 6px 8px;
  border: 1px solid rgba(66, 209, 163, 0.42);
  border-radius: 7px;
  background: rgba(66, 209, 163, 0.08);
  color: var(--green);
  font-family: var(--mono);
  font-size: 0.74rem;
  line-height: 1.35;
  text-decoration: none;
}

.empty-state {
  padding: 18px 12px;
  color: var(--quiet);
  font-size: 0.86rem;
}

.game-preview {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(5, 7, 9, 0.62);
  backdrop-filter: blur(8px);
}

.game-preview.is-open {
  display: flex;
}

.preview-window {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  width: min(1180px, calc(100vw - 40px));
  height: min(760px, calc(100vh - 40px));
  overflow: hidden;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #080a0d;
  box-shadow: 0 34px 110px rgba(0, 0, 0, 0.58);
}

.preview-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 58px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  background: rgba(17, 20, 24, 0.96);
}

.preview-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

#gameFrame {
  width: 100%;
  height: 100%;
  border: 0;
  background: #050609;
}

@media (max-width: 1040px) {
  .ops-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .panel {
    min-height: auto;
  }

  .messages {
    max-height: 56vh;
  }

  .prompt-chips {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 620px) {
  .app-shell {
    padding: 12px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .status-strip {
    width: 100%;
    justify-content: space-between;
  }

  .ops-strip {
    grid-template-columns: 1fr;
  }

  .prompt-chips,
  .composer {
    grid-template-columns: 1fr;
  }

  .messages {
    min-height: 160px;
    max-height: 36vh;
  }

  .game-preview {
    padding: 10px;
  }

  .preview-window {
    width: calc(100vw - 20px);
    height: calc(100vh - 20px);
  }

  .preview-bar {
    align-items: flex-start;
    flex-direction: column;
  }

  .preview-actions {
    width: 100%;
    justify-content: space-between;
  }

  .message {
    max-width: 100%;
  }
}
