:root {
  --bg: #0e0e10;
  --fg: #e8e8ea;
  --muted: #8a8a92;
  --surface: #1a1a1e;
  --surface-2: #23232a;
  --border: #2a2a31;
  --accent: #d4b896;
  --accent-strong: #e7c79a;
  --danger: #e07a6b;
  --ok: #7bbf86;
  --warn: #e3c267;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

button, input, select {
  font: inherit;
  color: inherit;
}

.app {
  display: grid;
  grid-template-rows: auto auto auto 1fr auto;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  padding: calc(var(--safe-top) + 0.25rem) 0.75rem calc(var(--safe-bottom) + 0.75rem);
  gap: 0.5rem;
}

.topbar {
  display: grid;
  grid-template-columns: 2.5rem 1fr 2.5rem;
  align-items: center;
  padding: 0.25rem 0;
}
.topbar h1 {
  margin: 0;
  text-align: center;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}
.back {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--fg);
  text-decoration: none;
  font-size: 1.4rem;
  line-height: 1;
}

.icon-btn {
  width: 2.25rem;
  height: 2.25rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  cursor: pointer;
}
.icon-btn.small { width: 2rem; height: 2rem; font-size: 0.9rem; }

.lang-bar {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.lang-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 0.5rem;
  align-items: center;
}
.lang-label {
  font-size: 0.85rem;
  color: var(--muted);
  padding-left: 0.25rem;
  min-width: 4.5rem;
}
.lang {
  appearance: none;
  -webkit-appearance: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.6rem;
  padding: 0.65rem 0.75rem;
  font-size: 0.95rem;
  width: 100%;
}
.speaker-btn {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  cursor: pointer;
  font-size: 1.05rem;
  user-select: none;
}
.speaker-btn.muted { background: var(--surface-2); opacity: 0.6; }
.two-way-toggle {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--muted);
  padding-left: 0.25rem;
  user-select: none;
  cursor: pointer;
}
.two-way-toggle input { width: 1.05rem; height: 1.05rem; }

.status {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  color: var(--muted);
  padding: 0 0.25rem;
}
.status .link-btn {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.8rem;
  text-decoration: underline;
  padding: 0.15rem 0.25rem;
}
.status .link-btn:hover { color: var(--fg); }
.dot {
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 999px;
  background: var(--muted);
  display: inline-block;
}
.dot.connecting { background: var(--warn); animation: pulse 1.2s infinite; }
.dot.connected { background: var(--ok); }
.dot.error { background: var(--danger); }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:0.35} }

.panes {
  display: grid;
  grid-template-rows: 1fr 1fr;
  gap: 0.5rem;
  min-height: 0;
}
.pane {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 0.65rem 0.85rem;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.target-pane { border-color: rgba(212, 184, 150, 0.35); }
.pane-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 0.25rem;
}
.target-pane .pane-label { color: var(--accent); }
.pane-body {
  flex: 1;
  overflow-y: auto;
  font-size: 1.05rem;
  line-height: 1.4;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.pane-body .partial { opacity: 0.55; }

.controls {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.text-row {
  display: grid;
  grid-template-columns: 1fr 2.5rem;
  gap: 0.5rem;
}
.text-row input {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.75rem 1rem;
  font-size: 1rem;
  outline: none;
}
.text-row input:focus { border-color: var(--accent); }
.send {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  cursor: pointer;
  font-size: 1.1rem;
}
.send:disabled { opacity: 0.4; cursor: not-allowed; }

.device-row {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.5rem;
  align-items: center;
}
.device-label {
  font-size: 0.85rem;
  color: var(--muted);
  padding-left: 0.25rem;
}
.mic-row { display: flex; justify-content: center; align-items: center; gap: 0.6rem; }
.end {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 2.6rem;
  padding: 0 1.1rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--fg);
  border: 1px solid var(--border);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}
.end:hover { background: var(--danger); color: #1a1a1e; border-color: var(--danger); }
.mic {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.85rem 1.4rem;
  border-radius: 999px;
  background: var(--accent);
  color: #1a1a1e;
  font-weight: 600;
  border: none;
  cursor: pointer;
  min-width: 10rem;
  justify-content: center;
  box-shadow: 0 6px 18px rgba(212, 184, 150, 0.2);
  user-select: none;
}
.mic .mic-icon { font-size: 0.9rem; }
.mic.active {
  background: var(--danger);
  color: #1a1a1e;
  animation: mic-pulse 1.4s infinite;
}
.mic.muted {
  background: var(--surface-2);
  color: var(--fg);
  box-shadow: none;
}
@keyframes mic-pulse {
  0%,100% { box-shadow: 0 0 0 0 rgba(224, 122, 107, 0.6); }
  50%     { box-shadow: 0 0 0 14px rgba(224, 122, 107, 0); }
}
.hint {
  text-align: center;
  color: var(--muted);
  font-size: 0.72rem;
  letter-spacing: 0.01em;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 10;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  width: 100%;
  max-width: 720px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-radius: 1rem 1rem 0 0;
  padding: 1rem 1rem calc(var(--safe-bottom) + 1rem);
  max-height: 92dvh;
  overflow-y: auto;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}
.modal-header h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
}
.modal-body { display: flex; flex-direction: column; gap: 0.9rem; }

.field { display: flex; flex-direction: column; gap: 0.35rem; }
.field > span { font-size: 0.85rem; color: var(--muted); }
.field small { color: var(--muted); font-size: 0.75rem; }
.field input[type=password],
.field input[type=text],
.field select {
  appearance: none;
  -webkit-appearance: none;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 0.55rem;
  padding: 0.6rem 0.7rem;
  font-size: 0.95rem;
  width: 100%;
}
.field code {
  background: var(--surface-2);
  padding: 0 0.25rem;
  border-radius: 0.25rem;
  font-size: 0.85em;
}
.footnote {
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.45;
  margin: 0;
}
.field.checkbox {
  flex-direction: row;
  align-items: center;
  gap: 0.6rem;
}
.field.checkbox > span { color: var(--fg); font-size: 0.95rem; }
.field.checkbox input { width: 1.1rem; height: 1.1rem; }

.key-row { display: grid; grid-template-columns: 1fr 2rem; gap: 0.4rem; }

.primary {
  margin-top: 0.5rem;
  background: var(--accent);
  color: #1a1a1e;
  border: none;
  border-radius: 0.55rem;
  padding: 0.75rem 1rem;
  font-weight: 600;
  cursor: pointer;
}

@media (min-width: 640px) {
  .panes { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  .dot.connecting, .mic.active { animation: none; }
}
