:root {
  --bg: #0f1115;
  --panel-2: #1e2330;
  --border: #2a3142;
  --text: #e8ecf4;
  --muted: #93a0b8;
  --primary: #4f8cff;
  --primary-hover: #6ea0ff;
  --danger: #ff6b6b;
  --assistant: #2d3748;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: radial-gradient(circle at top, #1a2233, var(--bg));
  color: var(--text);
  min-height: 100vh;
}

.app {
  display: grid;
  grid-template-columns: 320px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: rgba(23, 26, 33, 0.95);
  border-right: 1px solid var(--border);
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  max-height: 100vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.brand h1 { margin: 0; font-size: 24px; }
.brand p { margin: 8px 0 0; color: var(--muted); font-size: 13px; }

.panel {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
}

.panel h2 {
  margin: 0 0 10px;
  font-size: 14px;
  color: var(--muted);
}

select, textarea, input[type="text"], input[type="tel"], input[type="password"], input[type="file"] {
  width: 100%;
  background: #11151d;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font: inherit;
}

.field-row input {
  min-width: 0;
}

.field-row {
  display: grid;
  grid-template-columns: 96px 1fr;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

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

.field-row:last-of-type {
  margin-bottom: 0;
}

.script-panel textarea {
  min-height: 180px;
  resize: vertical;
  line-height: 1.55;
  font-size: 12px;
  pointer-events: auto;
  user-select: text;
}

.script-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 8px;
}

.phone-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

.phone-actions .btn {
  flex: 1;
}

#fsPhoneStatus.on-call {
  color: #7ee787;
}

#fsPhoneStatus.error {
  color: var(--danger);
}

.input-with-btn {
  display: flex;
  gap: 6px;
  align-items: center;
}

.field-row.fs-app-host-row {
  grid-template-columns: 96px 1fr;
}

.field-row.fs-app-host-row .input-with-btn input {
  min-width: 120px;
}

.input-with-btn input {
  flex: 1;
  min-width: 0;
}

.input-with-btn .btn {
  flex: 0 0 auto;
  white-space: nowrap;
  padding: 8px 10px;
}

.fs-connect-status {
  margin-top: 10px;
  padding: 10px;
  border-radius: 10px;
  background: #11151d;
  border: 1px solid var(--border);
  font-size: 12px;
  line-height: 1.55;
  color: var(--muted);
  white-space: pre-wrap;
  word-break: break-all;
}

.fs-connect-status.ok {
  color: #7ee787;
  border-color: rgba(126, 231, 135, 0.35);
}

.fs-connect-status.error {
  color: var(--danger);
  border-color: rgba(255, 107, 107, 0.35);
}

.fs-reg-list {
  margin: 8px 0 0;
  padding-left: 16px;
}

.fs-reg-list li {
  margin-bottom: 4px;
}

.script-saved {
  font-size: 11px;
  color: var(--muted);
}

input[type="range"] { width: 100%; }

.range-label, .slider-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 12px;
}

.mini-hint {
  margin: 10px 0 0;
  font-size: 11px;
  color: var(--muted);
  line-height: 1.5;
}

.custom-voice-link {
  margin-top: 10px;
}

.custom-voice-link label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}

.custom-voice-list {
  list-style: none;
  padding: 0;
  margin: 10px 0 0;
}

.custom-voice-list li {
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
}

.custom-voice-list a {
  color: var(--accent, #8ab4f8);
}

.voice-speaker-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-bottom: 10px;
}

.voice-speaker-toolbar select,
.voice-speaker-toolbar input[type="text"] {
  min-width: 0;
}

.voice-speaker-table-wrap {
  overflow-x: auto;
  margin: 10px 0;
}

.voice-speaker-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

.voice-speaker-table th,
.voice-speaker-table td {
  padding: 8px 6px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.voice-speaker-table th {
  color: var(--muted);
  font-weight: 500;
}

.voice-speaker-table code {
  font-size: 11px;
  word-break: break-all;
}

.voice-speaker-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.voice-speaker-actions .btn {
  padding: 4px 8px;
  font-size: 11px;
}

.slot-badge {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 11px;
}

.slot-badge.active {
  background: rgba(52, 168, 83, 0.15);
  color: #81c995;
}

.slot-badge.deprecated {
  background: rgba(154, 160, 166, 0.15);
  color: var(--muted);
}

.enroll-prompt-box {
  margin-top: 12px;
}

.enroll-prompt-box textarea {
  width: 100%;
  min-height: 72px;
  resize: vertical;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.62);
  backdrop-filter: blur(4px);
}

.modal-overlay[hidden] {
  display: none !important;
}

.modal-dialog {
  width: min(920px, 100%);
  max-height: min(88vh, 900px);
  display: flex;
  flex-direction: column;
  background: var(--panel, #1a1d24);
  border: 1px solid var(--border, #2a2f38);
  border-radius: 12px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border, #2a2f38);
  flex-shrink: 0;
}

.modal-header h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.modal-close {
  min-width: 36px;
  padding: 4px 10px;
}

.modal-body {
  padding: 14px 18px 18px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.voice-manage-dialog .voice-speaker-table-wrap {
  max-height: min(42vh, 360px);
  overflow: auto;
}

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.voice-stage {
  padding: 28px 24px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  border-bottom: 1px solid var(--border);
}

.voice-orb {
  width: 160px;
  height: 160px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  text-align: center;
  font-size: 18px;
  font-weight: 600;
  box-shadow: var(--shadow);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.voice-orb.idle {
  background: radial-gradient(circle at 30% 30%, #3a4458, #222833);
  color: var(--muted);
}

.voice-orb.listening {
  background: radial-gradient(circle at 30% 30%, #3d8bfd, #245ecf);
  color: white;
  animation: pulse 1.4s ease-in-out infinite;
}

.voice-orb.thinking {
  background: radial-gradient(circle at 30% 30%, #ffb347, #e68a00);
  color: #1a1200;
}

.voice-orb.speaking {
  background: radial-gradient(circle at 30% 30%, #7b61ff, #4f46e5);
  color: white;
  animation: pulse 1.8s ease-in-out infinite;
}

.voice-orb.interrupted {
  background: radial-gradient(circle at 30% 30%, #ff7b7b, #e04545);
  color: white;
}

.live-transcript {
  min-height: 48px;
  max-width: 680px;
  text-align: center;
  color: var(--muted);
  line-height: 1.6;
  margin: 0;
}

.live-transcript.active {
  color: var(--text);
  font-size: 18px;
}

.voice-actions {
  display: flex;
  gap: 12px;
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 180px;
  -webkit-overflow-scrolling: touch;
}

.msg {
  max-width: 78%;
  padding: 12px 14px;
  border-radius: 16px;
  line-height: 1.6;
  box-shadow: var(--shadow);
  animation: fadeIn 0.25s ease;
}

.msg.user {
  align-self: flex-end;
  background: linear-gradient(135deg, #3569b8, #2b6cb0);
}

.msg.assistant {
  align-self: flex-start;
  background: var(--assistant);
  border: 1px solid var(--border);
}

.msg .role {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.65);
  margin-bottom: 4px;
}

.text-fallback {
  border-top: 1px solid var(--border);
  padding: 0 20px 16px;
  color: var(--muted);
}

.text-fallback summary {
  cursor: pointer;
  padding: 12px 0;
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: end;
}

.btn {
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  cursor: pointer;
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
}

.btn:hover:not(:disabled) { filter: brightness(1.08); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

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

.btn.primary:hover:not(:disabled) { background: var(--primary-hover); }

.btn.danger {
  background: rgba(255, 107, 107, 0.12);
  color: #ffb4b4;
  border-color: rgba(255, 107, 107, 0.35);
}

.btn.large {
  padding: 12px 22px;
  font-size: 15px;
}

.btn.small {
  padding: 6px 10px;
  font-size: 12px;
}

.btn.ghost {
  background: transparent;
  position: relative;
  z-index: 1;
}

.status-panel { margin-top: auto; }

.status {
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 13px;
}

.status.idle { background: #142018; color: #7dcea0; }
.status.busy { background: #1f2433; color: #ffd166; }
.status.error { background: #30161a; color: #ff9aa2; }

.hint {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(79, 140, 255, 0.25); }
  50% { transform: scale(1.03); box-shadow: 0 0 0 18px rgba(79, 140, 255, 0); }
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.mobile-header,
.sidebar-backdrop,
.sidebar-drawer-head {
  display: none;
}

@media (max-width: 900px) {
  .app {
    grid-template-columns: 1fr;
    display: block;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 210;
    width: min(100vw - 40px, 380px);
    max-height: 100dvh;
    max-height: 100svh;
    padding: max(12px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
    border-right: 1px solid var(--border);
    border-bottom: none;
    transform: translateX(-105%);
    transition: transform 0.24s ease;
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.45);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-drawer-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 8px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
  }

  .sidebar-drawer-head strong {
    font-size: 15px;
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 200;
    background: rgba(0, 0, 0, 0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.24s ease;
  }

  .sidebar-backdrop.visible {
    opacity: 1;
    pointer-events: auto;
  }

  .mobile-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: max(10px, env(safe-area-inset-top)) 12px 10px;
    border-bottom: 1px solid var(--border);
    background: rgba(15, 17, 21, 0.92);
    backdrop-filter: blur(10px);
    position: sticky;
    top: 0;
    z-index: 50;
  }

  .mobile-menu-btn {
    flex: 0 0 auto;
    min-width: 44px;
    min-height: 44px;
    padding: 0;
    font-size: 20px;
    line-height: 1;
  }

  .mobile-header-center {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
  }

  .mobile-title {
    font-size: 15px;
    font-weight: 600;
  }

  .mobile-status {
    font-size: 11px;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .mobile-header-actions {
    display: flex;
    gap: 6px;
    flex-shrink: 0;
  }

  .mobile-header-actions .btn {
    min-height: 40px;
    padding: 8px 12px;
  }

  .main {
    min-height: calc(100dvh - 56px);
    min-height: calc(100svh - 56px);
    padding-bottom: env(safe-area-inset-bottom);
  }

  .voice-stage {
    padding: 16px 14px 10px;
  }

  .voice-orb {
    width: 120px;
    height: 120px;
    font-size: 15px;
  }

  .live-transcript {
    font-size: 14px;
    padding: 0 4px;
  }

  .live-transcript.active {
    font-size: 16px;
  }

  .voice-actions {
    flex-direction: column;
    width: 100%;
    max-width: 320px;
  }

  .voice-actions .btn.large {
    width: 100%;
    min-height: 48px;
  }

  .chat-list {
    padding: 14px 12px;
    min-height: 120px;
  }

  .msg {
    max-width: 92%;
    padding: 10px 12px;
    font-size: 14px;
  }

  .text-fallback {
    padding: 0 12px max(12px, env(safe-area-inset-bottom));
  }

  .field-row {
    grid-template-columns: 1fr;
    gap: 4px;
  }

  .field-row.fs-app-host-row {
    grid-template-columns: 1fr;
  }

  .field-row.fs-app-host-row .input-with-btn {
    flex-direction: column;
    align-items: stretch;
  }

  .field-row.fs-app-host-row .input-with-btn .btn {
    width: 100%;
    min-height: 44px;
  }

  .phone-actions {
    flex-wrap: wrap;
  }

  .phone-actions .btn {
    min-height: 44px;
    flex: 1 1 calc(50% - 4px);
  }

  .script-panel textarea {
    min-height: 140px;
  }

  select,
  textarea,
  input[type="text"],
  input[type="tel"],
  input[type="password"] {
    font-size: 16px;
    min-height: 44px;
  }

  input[type="range"] {
    min-height: 32px;
  }

  .btn {
    min-height: 44px;
  }

  .btn.small {
    min-height: 40px;
  }

  .status-panel {
    margin-top: 0;
  }
}

.mobile-status.on-call {
  color: #7ee787;
}

.mobile-status.error {
  color: var(--danger);
}

body.sidebar-open {
  overflow: hidden;
}

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

  .composer .btn {
    width: 100%;
    min-height: 48px;
  }

  .brand h1 {
    font-size: 20px;
  }

  .sidebar {
    width: 100vw;
    max-width: 100vw;
  }
}

@media (max-width: 900px) and (orientation: landscape) {
  .voice-stage {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    padding: 10px 12px;
  }

  .voice-orb {
    width: 88px;
    height: 88px;
    font-size: 13px;
  }

  .voice-actions {
    flex-direction: row;
    max-width: none;
    width: auto;
  }
}
