/* === Reset & Base === */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #09090b;
  --bg-subtle: #0c0c0f;
  --surface: #131318;
  --surface-2: #1a1a22;
  --surface-hover: #22222e;
  --border: #1e1e2a;
  --border-light: #2a2a3a;
  --text: #ececf1;
  --text-secondary: #a1a1b5;
  --text-muted: #6b6b80;
  --accent: #6C5CE7;
  --accent-hover: #7c6ef7;
  --accent-glow: rgba(108, 92, 231, 0.15);
  --accent-2: #00B4D8;
  --danger: #ef4444;
  --danger-glow: rgba(239, 68, 68, 0.15);
  --success: #22c55e;
  --warning: #eab308;
  --user-bubble: rgba(108, 92, 231, 0.12);
  --user-bubble-border: rgba(108, 92, 231, 0.25);
  --model-bubble: rgba(255, 255, 255, 0.04);
  --model-bubble-border: rgba(255, 255, 255, 0.08);
  --radius: 10px;
  --radius-lg: 16px;
  --radius-xl: 20px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 860px;
  margin: 0 auto;
  width: 100%;
  position: relative;
  overflow: hidden;
}

/* When disconnected, config panel can be long — allow scroll */
#app:has(#control-bar.hidden) {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#app:has(#control-bar.hidden) #chat-container {
  flex: none;
  min-height: 120px;
}

/* === Header === */
#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
  z-index: 10;
}

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

.logo {
  display: flex;
  align-items: center;
}

.logo-img {
  height: 32px;
  width: auto;
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
  transition: all var(--transition);
}

.icon-btn:hover {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border-light);
}

/* === Status Badge === */
.badge {
  font-size: 0.72rem;
  padding: 4px 10px;
  border-radius: 20px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.badge.disconnected {
  background: rgba(239, 68, 68, 0.12);
  color: var(--danger);
  border: 1px solid rgba(239, 68, 68, 0.2);
}

.badge.connecting {
  background: rgba(234, 179, 8, 0.12);
  color: var(--warning);
  border: 1px solid rgba(234, 179, 8, 0.2);
  animation: pulse 1.5s ease-in-out infinite;
}

.badge.connected {
  background: rgba(34, 197, 94, 0.12);
  color: var(--success);
  border: 1px solid rgba(34, 197, 94, 0.2);
}

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

/* === History Panel === */
#history-panel {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 100;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
}

.history-header h2 {
  font-size: 1rem;
  font-weight: 600;
}

#history-list {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.history-item {
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: 8px;
  cursor: pointer;
  transition: all var(--transition);
  background: var(--surface);
}

.history-item:hover {
  background: var(--surface-hover);
  border-color: var(--border-light);
}

.history-item-date {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-bottom: 6px;
  font-weight: 500;
}

.history-item-preview {
  font-size: 0.85rem;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.4;
}

.history-item-config {
  font-size: 0.65rem;
  color: var(--text-muted);
  margin-top: 6px;
  font-weight: 500;
}

.history-detail { padding: 16px; }

.history-detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.history-detail-meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
  line-height: 1.6;
}

.history-back-btn {
  font-size: 0.85rem;
  color: var(--accent);
  background: none;
  border: none;
  cursor: pointer;
  font-weight: 500;
  font-family: var(--font);
}

.history-delete-btn {
  font-size: 0.75rem;
  color: var(--danger);
  background: none;
  border: none;
  cursor: pointer;
  font-family: var(--font);
}

.history-empty {
  text-align: center;
  color: var(--text-muted);
  padding: 60px 16px;
  font-size: 0.85rem;
}

/* === Config Panel === */
#config-panel {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

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

.config-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px;
}

.config-row {
  margin-bottom: 16px;
}

.config-row:last-child {
  margin-bottom: 0;
}

.config-row.center {
  text-align: center;
  margin-top: 12px;
}

.config-row label {
  display: block;
  font-size: 0.88rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
  font-weight: 500;
  letter-spacing: 0.01em;
}

.config-inline {
  display: flex;
  gap: 10px;
}

.config-field {
  flex: 1;
}

/* === Preset Chips === */
.preset-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 10px;
}

.preset-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--text-secondary);
  font-size: 0.8rem;
  font-family: var(--font);
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.preset-chip:hover {
  background: var(--accent-glow);
  border-color: var(--accent);
  color: var(--text);
}

.preset-chip.active {
  background: var(--accent-glow);
  border-color: var(--accent);
  color: var(--accent);
}

.preset-chip-icon {
  font-size: 0.9rem;
}

/* === Info Buttons & Hints === */
.info-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--border);
  border: none;
  color: var(--text-muted);
  font-size: 0.65rem;
  font-weight: 700;
  font-family: var(--font);
  cursor: pointer;
  margin-left: 6px;
  vertical-align: middle;
  transition: all var(--transition);
  line-height: 1;
}

.info-btn:hover, .info-btn.active {
  background: var(--accent);
  color: white;
}

.field-hint {
  margin-top: 8px;
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.82rem;
  line-height: 1.65;
  color: var(--text-secondary);
  animation: fadeIn 0.2s ease-out;
}

.field-hint p {
  margin-bottom: 8px;
}

.field-hint p:last-child {
  margin-bottom: 0;
}

.hint-jp {
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  padding-top: 8px;
  font-size: 0.78rem;
}

/* === Voice Detail Card === */
.voice-detail {
  margin-top: 8px;
  padding: 10px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.82rem;
  line-height: 1.6;
  display: flex;
  align-items: center;
  gap: 10px;
}

.voice-detail:empty {
  display: none;
}

.voice-gender {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  border-radius: 6px;
  font-size: 0.7rem;
  font-weight: 700;
  flex-shrink: 0;
}

.voice-gender.male {
  background: rgba(79, 139, 249, 0.15);
  color: #4F8BF9;
}

.voice-gender.female {
  background: rgba(236, 112, 182, 0.15);
  color: #EC70B6;
}

.voice-info {
  flex: 1;
}

.voice-info-name {
  font-weight: 600;
  color: var(--text);
  font-size: 0.85rem;
}

.voice-info-desc {
  color: var(--text-muted);
  font-size: 0.78rem;
}

.voice-info-jp {
  color: var(--text-muted);
  font-size: 0.72rem;
  opacity: 0.7;
}

textarea, select {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 16px;
  font-family: var(--font);
  transition: border-color var(--transition);
}

textarea:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

textarea {
  resize: vertical;
  min-height: 100px;
  line-height: 1.5;
}

select {
  cursor: pointer;
}

/* Custom arrow only on desktop — mobile uses native picker */
@media (hover: hover) and (pointer: fine) {
  select {
    appearance: none;
    -webkit-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1L5 5L9 1' stroke='%236b6b80' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 32px;
  }
}

input[type="range"] {
  width: 100%;
  height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--border);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg);
  box-shadow: 0 0 0 3px var(--accent-glow);
  cursor: pointer;
}

.slider-value {
  float: right;
  color: var(--accent);
  font-weight: 600;
  font-size: 0.8rem;
}

.toggle-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.88rem;
  font-weight: 500;
  padding: 4px 0;
  display: flex;
  align-items: center;
  gap: 6px;
  font-family: var(--font);
  transition: color var(--transition);
}

.toggle-btn:hover {
  color: var(--text-secondary);
}

.toggle-arrow {
  transition: transform var(--transition);
}

.toggle-btn.open .toggle-arrow {
  transform: rotate(90deg);
}

.connect-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: linear-gradient(135deg, var(--accent), #5a4bd6);
  color: white;
  border: none;
  padding: 14px 36px;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  font-family: var(--font);
  letter-spacing: -0.01em;
  transition: all var(--transition);
  box-shadow: 0 2px 12px var(--accent-glow);
}

.connect-btn:hover {
  background: linear-gradient(135deg, var(--accent-hover), #6a5be6);
  box-shadow: 0 4px 20px rgba(108, 92, 231, 0.3);
  transform: translateY(-1px);
}

.connect-btn:active {
  transform: translateY(0);
}

.connect-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
}

.hidden {
  display: none !important;
}

/* === Chat Transcript === */
#chat-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  background: var(--bg-subtle);
}

#chat-container::-webkit-scrollbar {
  width: 4px;
}

#chat-container::-webkit-scrollbar-track {
  background: transparent;
}

#chat-container::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

#chat-messages {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: auto;
}

.chat-bubble {
  max-width: 78%;
  padding: 14px 18px;
  border-radius: var(--radius-lg);
  font-size: 0.95rem;
  line-height: 1.65;
  word-wrap: break-word;
  animation: fadeIn 0.2s ease-out;
}

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

.chat-bubble.user {
  align-self: flex-end;
  background: var(--user-bubble);
  border: 1px solid var(--user-bubble-border);
  border-bottom-right-radius: 4px;
}

.chat-bubble.model {
  align-self: flex-start;
  background: var(--model-bubble);
  border: 1px solid var(--model-bubble-border);
  border-bottom-left-radius: 4px;
}

.chat-bubble .sender {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-bottom: 5px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.chat-bubble.user .sender {
  text-align: right;
  color: var(--accent);
}

/* Empty state — content set dynamically by i18n */
#chat-messages:empty::after {
  content: 'Start a session to begin talking';
  color: var(--text-muted);
  text-align: center;
  margin: auto;
  font-size: 0.95rem;
  font-weight: 500;
}

/* === Video Preview === */
#video-preview {
  position: fixed;
  bottom: 88px;
  right: 20px;
  width: 160px;
  height: 120px;
  border-radius: var(--radius-lg);
  overflow: hidden;
  border: 2px solid var(--border-light);
  z-index: 10;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

#video-preview video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.video-label {
  position: absolute;
  top: 8px;
  left: 8px;
  font-size: 0.55rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: white;
  background: var(--danger);
  padding: 2px 6px;
  border-radius: 4px;
}

/* === Control Bar === */
#control-bar {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.controls-group {
  display: flex;
  gap: 8px;
  padding: 4px;
  background: var(--bg);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
}

.ctrl-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 54px;
  height: 50px;
  background: transparent;
  border: none;
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
  transition: all var(--transition);
}

.ctrl-btn:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.ctrl-btn.active {
  background: var(--accent-glow);
  color: var(--accent);
}

.ctrl-btn.end {
  width: 54px;
  height: 50px;
  background: var(--danger-glow);
  color: var(--danger);
  border: 1px solid rgba(239, 68, 68, 0.2);
  border-radius: var(--radius-lg);
}

.ctrl-btn.end:hover {
  background: rgba(239, 68, 68, 0.25);
}

/* === Mobile: Small phones === */
@media (max-width: 380px) {
  #header {
    padding: 10px 12px;
  }

  .logo-img {
    height: 22px;
  }

  #config-panel {
    padding: 10px 12px;
  }

  .config-card {
    padding: 14px;
  }

  .config-inline {
    flex-direction: column;
    gap: 8px;
  }

  #chat-container {
    padding: 10px;
  }

  .chat-bubble {
    max-width: 92%;
    padding: 10px 12px;
    font-size: 0.82rem;
  }

  .controls-group {
    gap: 4px;
    padding: 3px;
  }

  .ctrl-btn {
    width: 40px;
    height: 38px;
  }

  .ctrl-btn svg {
    width: 18px;
    height: 18px;
  }

  .ctrl-btn.end {
    width: 40px;
    height: 38px;
  }

  #control-bar {
    padding: 10px 12px;
    gap: 10px;
  }

  .connect-btn {
    padding: 10px 22px;
    font-size: 0.82rem;
  }

  #video-preview {
    width: 100px;
    height: 75px;
    bottom: 70px;
    right: 10px;
  }

  .badge {
    font-size: 0.58rem;
    padding: 2px 6px;
  }
}

/* === Mobile: Standard phones === */
@media (max-width: 600px) {
  #app {
    max-width: 100%;
  }

  #header {
    padding: 10px 14px;
  }

  .logo-img {
    height: 24px;
  }

  #config-panel {
    padding: 10px 14px;
  }

  .config-card {
    padding: 16px;
    border-radius: var(--radius);
  }

  .config-inline {
    flex-direction: column;
    gap: 8px;
  }

  textarea {
    min-height: 44px;
  }

  select {
    padding: 10px 12px;
  }

  #chat-container {
    padding: 12px;
  }

  .chat-bubble {
    max-width: 88%;
  }

  .ctrl-btn {
    width: 44px;
    height: 42px;
  }

  .ctrl-btn.end {
    width: 44px;
    height: 42px;
  }

  #control-bar {
    padding: 10px 14px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
    gap: 12px;
  }

  #video-preview {
    width: 120px;
    height: 90px;
    bottom: calc(75px + env(safe-area-inset-bottom));
    right: 12px;
  }

  .history-item {
    padding: 12px 14px;
  }
}

/* === Tablet === */
@media (min-width: 601px) and (max-width: 900px) {
  .config-inline {
    gap: 8px;
  }
}

/* === Safe areas for notched devices === */
@supports (padding: env(safe-area-inset-bottom)) {
  #control-bar {
    padding-bottom: calc(14px + env(safe-area-inset-bottom));
  }

  #header {
    padding-top: calc(14px + env(safe-area-inset-top));
  }
}

/* === Touch improvements === */
@media (hover: none) and (pointer: coarse) {
  .ctrl-btn {
    min-width: 44px;
    min-height: 44px;
  }

  .icon-btn {
    min-width: 44px;
    min-height: 44px;
  }

  .ctrl-btn:hover {
    background: transparent;
    color: var(--text-muted);
  }

  .ctrl-btn.active:hover {
    background: var(--accent-glow);
    color: var(--accent);
  }

  .ctrl-btn.end:hover {
    background: var(--danger-glow);
  }
}
