:root {
  color-scheme: dark;
  --bg: #0b0d12;
  --bg-elevated: #131722;
  --bg-soft: #1a2030;
  --border: #232a3b;
  --text: #e6e9f2;
  --text-dim: #95a0bb;
  --accent: #4f8cff;
  --accent-strong: #6aa1ff;
  --user: #2f7ff0;
  --assistant: #2a3145;
  --danger: #ff5577;
  --success: #2dd28b;
  --warning: #ffb84d;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: radial-gradient(
      1200px 600px at 80% -10%,
      rgba(79, 140, 255, 0.18),
      transparent 60%
    ),
    radial-gradient(
      900px 500px at -10% 110%,
      rgba(45, 210, 139, 0.12),
      transparent 60%
    ),
    var(--bg);
  color: var(--text);
}

body {
  display: flex;
  justify-content: center;
  padding: 12px;
}

.app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
  width: min(1440px, 100%);
  height: calc(100dvh - 24px);
  max-height: calc(100vh - 24px);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 14px;
}

.app__body {
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(300px, 32vw, 420px);
  gap: 12px;
  min-height: 0;
}

.app__main {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 0;
  min-height: 0;
  overflow: hidden;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

/* Header ---------------------------------------------------------------- */

.app__header {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand h1 {
  font-size: 16px;
  margin: 0;
  letter-spacing: 0.2px;
}

.brand__dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 4px rgba(79, 140, 255, 0.18);
}

.connection {
  display: flex;
  align-items: end;
  gap: 8px;
  min-width: 0;
}

.connection__field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
  flex: 1;
}

.connection__field span {
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.8px;
}

.connection__field input {
  background: var(--bg-soft);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 7px 10px;
  font: inherit;
  font-size: 12px;
  outline: none;
  width: 100%;
  min-width: 0;
}

.connection__field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 140, 255, 0.18);
}

.status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-dim);
  white-space: nowrap;
}

.status__dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--text-dim);
}

.status__dot--idle {
  background: var(--text-dim);
}

.status__dot--connecting {
  background: var(--warning);
  animation: pulse 1.2s ease-in-out infinite;
}

.status__dot--connected {
  background: var(--success);
}

.status__dot--error {
  background: var(--danger);
}

/* Chat ------------------------------------------------------------------ */

.chat {
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat__log {
  flex: 1;
  overflow-y: auto;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}

.chat__empty {
  margin: auto;
  text-align: center;
  color: var(--text-dim);
}

.chat__empty p {
  margin: 4px 0;
}

.chat__hint code {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1px 6px;
  font-size: 12px;
}

.bubble {
  max-width: 78%;
  padding: 10px 14px;
  border-radius: 14px;
  line-height: 1.45;
  font-size: 14px;
  white-space: pre-wrap;
  word-wrap: break-word;
  animation: bubble-in 0.16s ease-out;
}

.bubble--user {
  background: var(--user);
  color: #fff;
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}

.bubble--assistant {
  background: var(--assistant);
  color: var(--text);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}

.bubble--assistant.bubble--interrupted {
  opacity: 0.75;
  border-left: 2px solid var(--warning);
}

.bubble--system {
  align-self: center;
  background: transparent;
  color: var(--text-dim);
  font-size: 12px;
  border: 1px dashed var(--border);
  padding: 6px 10px;
  border-radius: 999px;
}

.bubble__role {
  display: block;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  opacity: 0.7;
  margin-bottom: 4px;
}

/* WebSocket log --------------------------------------------------------- */

.ws-log {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.ws-log__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--bg-soft);
}

.ws-log__legend {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 10px;
  color: var(--text-dim);
}

.ws-log__legend-item {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.ws-log__legend-item::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 2px;
}

.ws-log__legend-item--send::before {
  background: var(--success);
}

.ws-log__legend-item--recv::before {
  background: var(--accent-strong);
}

.ws-log__header h2 {
  margin: 0;
  font-size: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.8px;
}

.ws-log__header-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.ws-log__body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 6px 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 10.5px;
  line-height: 1.4;
  color: var(--text-dim);
}

.ws-log__entry {
  display: grid;
  grid-template-columns: 58px 42px minmax(0, 1fr);
  gap: 6px;
  align-items: start;
  padding: 5px 4px;
  border-radius: 6px;
  white-space: pre-wrap;
  word-break: break-word;
}

.ws-log__entry:hover {
  background: rgba(255, 255, 255, 0.03);
}

.ws-log__time {
  color: var(--text-dim);
  opacity: 0.7;
  font-size: 10px;
}

.ws-log__direction {
  font-weight: 700;
  font-size: 9px;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  padding: 2px 0;
}

.ws-log__detail {
  min-width: 0;
  overflow-wrap: anywhere;
}

.ws-log__entry--send .ws-log__direction {
  color: var(--success);
}

.ws-log__entry--recv .ws-log__direction {
  color: var(--accent-strong);
}

.ws-log__entry--system .ws-log__direction {
  color: var(--text-dim);
}

.ws-log__entry--error .ws-log__direction {
  color: var(--danger);
}

.ws-log__empty {
  color: var(--text-dim);
  opacity: 0.7;
  padding: 8px 4px;
}

/* Controls -------------------------------------------------------------- */

.controls {
  display: grid;
  gap: 8px;
  padding: 10px 12px 8px;
  border-top: 1px solid var(--border);
  background: var(--bg-elevated);
}

.meter {
  height: 6px;
  background: var(--bg-soft);
  border-radius: 999px;
  overflow: hidden;
}

.meter__fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--success), var(--accent));
  transition: width 80ms linear;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.composer__input {
  flex: 1;
  resize: none;
  background: var(--bg-soft);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  font: inherit;
  font-size: 14px;
  line-height: 1.4;
  outline: none;
  min-height: 42px;
  max-height: 180px;
  overflow-y: auto;
}

.composer__input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 140, 255, 0.18);
}

.composer__input:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.composer__send {
  padding: 10px 18px;
  border-radius: 12px;
  min-width: 84px;
  align-self: flex-end;
}

.composer__send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.controls__row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.device-picker {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  flex: 1 1 160px;
  max-width: 240px;
}

.device-picker__label {
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.8px;
}

.device-picker__select {
  appearance: none;
  background: var(--bg-soft);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 7px 28px 7px 10px;
  font: inherit;
  font-size: 12px;
  outline: none;
  width: 100%;
  cursor: pointer;
}

.device-picker__select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 140, 255, 0.18);
}

.device-picker__select:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.mic-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--bg-soft);
  color: var(--text);
  border: 1px solid var(--border);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.08s ease, background 0.15s ease, color 0.15s ease,
    border-color 0.15s ease;
}

.mic-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.mic-btn:not(:disabled):hover {
  border-color: var(--accent);
}

.mic-btn:not(:disabled):active {
  transform: scale(0.98);
}

.mic-btn[aria-pressed="true"] {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
  box-shadow: 0 0 0 6px rgba(255, 85, 119, 0.18);
}

.mic-btn__icon {
  display: block;
}

.indicators {
  display: flex;
  gap: 12px;
  margin-left: auto;
}

.indicator {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
}

.indicator__dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--bg-soft);
  border: 1px solid var(--border);
}

.indicator.is-active .indicator__dot--mic {
  background: var(--success);
  border-color: var(--success);
  box-shadow: 0 0 0 4px rgba(45, 210, 139, 0.2);
}

.indicator.is-active .indicator__dot--bot {
  background: var(--accent);
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(79, 140, 255, 0.22);
  animation: pulse 1s ease-in-out infinite;
}

.indicator.is-active .indicator__label {
  color: var(--text);
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 10px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.btn:hover {
  border-color: var(--accent);
}

.btn--primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.btn--primary:hover {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}

.btn--primary.is-disconnect {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger);
}

.btn--ghost {
  background: transparent;
}

.hint {
  margin: 0;
  font-size: 11px;
  color: var(--text-dim);
  opacity: 0.85;
}

.hint kbd {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  color: var(--text);
}

/* Animations ------------------------------------------------------------ */

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.55;
  }
}

@keyframes bubble-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Responsive ------------------------------------------------------------ */

@media (max-width: 820px) {
  .app__body {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) min(240px, 32vh);
  }

  .ws-log__legend {
    display: none;
  }

  .hint {
    display: none;
  }
}

@media (max-width: 720px) {
  body {
    padding: 0;
  }

  .app {
    height: 100dvh;
    max-height: 100vh;
    border-radius: 0;
    border: none;
    padding: 10px;
  }

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

  .connection {
    flex-direction: column;
    align-items: stretch;
  }

  .ws-log__entry {
    grid-template-columns: 54px 38px minmax(0, 1fr);
  }

  .status {
    justify-content: flex-end;
  }

  .device-picker {
    flex: 1 1 100%;
    max-width: none;
  }

  .indicators {
    margin-left: 0;
  }
}
