*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --user-msg: #2563eb;
  --user-text: #ffffff;
  --bot-msg: #ffffff;
  --bot-text: #1e293b;
  --debug-bg: #f8fafc;
  --debug-border: #cbd5e1;
  --success: #16a34a;
  --error: #dc2626;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
  --radius: 12px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 16px;
  background: var(--bg);
  color: var(--text);
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 960px;
  margin: 0 auto;
  background: var(--surface);
  box-shadow: var(--shadow);
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--primary);
  color: white;
  flex-shrink: 0;
}

header h1 {
  font-size: 1.2rem;
  font-weight: 600;
}

.debug-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 0.85rem;
  user-select: none;
}

.debug-toggle input { display: none; }

.toggle-slider {
  width: 36px;
  height: 20px;
  background: rgba(255,255,255,0.3);
  border-radius: 10px;
  position: relative;
  transition: background 0.2s;
}

.toggle-slider::after {
  content: '';
  position: absolute;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: white;
  top: 2px;
  left: 2px;
  transition: transform 0.2s;
}

.debug-toggle input:checked + .toggle-slider {
  background: rgba(255,255,255,0.8);
}

.debug-toggle input:checked + .toggle-slider::after {
  transform: translateX(16px);
}

.docs-link {
  color: white;
  text-decoration: none;
  font-size: 1.3rem;
  margin-left: 12px;
  opacity: 0.7;
  transition: opacity 0.2s;
}

.docs-link:hover {
  opacity: 1;
}

.main-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.chat-area {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  max-width: 80%;
  padding: 12px 16px;
  border-radius: var(--radius);
  line-height: 1.5;
  animation: fadeIn 0.3s ease;
}

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

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

.msg.bot {
  align-self: flex-start;
  background: var(--bot-msg);
  color: var(--bot-text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.msg .meta {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 6px;
  display: flex;
  gap: 8px;
}

.msg.bot .meta { color: var(--text-muted); }
.msg.user .meta { color: rgba(255,255,255,0.7); }

.msg .typing {
  display: flex;
  gap: 4px;
  padding: 4px 0;
}

.msg .typing span {
  width: 8px;
  height: 8px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: bounce 1.4s ease infinite;
}

.msg .typing span:nth-child(2) { animation-delay: 0.2s; }
.msg .typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); }
  40% { transform: translateY(-6px); }
}

.debug-panel {
  width: 320px;
  flex-shrink: 0;
  border-left: 1px solid var(--border);
  background: var(--debug-bg);
  overflow-y: auto;
  padding: 16px;
  font-size: 0.8rem;
}

.debug-panel.hidden { display: none; }

.debug-panel h3 {
  font-size: 0.9rem;
  margin-bottom: 12px;
  color: var(--text);
}

.debug-empty {
  color: var(--text-muted);
  font-style: italic;
}

.debug-step {
  background: var(--surface);
  border: 1px solid var(--debug-border);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 8px;
}

.debug-step .step-num {
  display: inline-block;
  background: var(--primary);
  color: white;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  text-align: center;
  line-height: 22px;
  font-size: 0.75rem;
  font-weight: 700;
  margin-right: 6px;
}

.debug-step .step-action {
  font-weight: 600;
  color: var(--text);
}

.debug-step .step-result {
  margin-top: 4px;
  color: var(--text-muted);
  white-space: pre-wrap;
  word-break: break-word;
}

.debug-step .step-result.ok { color: var(--success); }
.debug-step .step-result.err { color: var(--error); }

.input-area {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  background: var(--surface);
}

.input-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.input-row input[type="text"] {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 24px;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.2s;
}

.input-row input[type="text"]:focus {
  border-color: var(--primary);
}

.btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.1s;
  flex-shrink: 0;
}

.btn:active { transform: scale(0.92); }

.btn-file {
  background: var(--bg);
  color: var(--text);
}

.btn-file:hover { background: var(--border); }

.btn-import {
  background: #16a34a;
  color: white;
}

.btn-import:hover { background: #15803d; }

.btn-mic {
  background: var(--bg);
  color: var(--text);
}

.btn-mic:hover { background: var(--border); }
.btn-mic.recording {
  background: var(--error);
  color: white;
  animation: pulse 0.8s ease infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(220,38,38,0.4); }
  50% { box-shadow: 0 0 0 8px rgba(220,38,38,0); }
}

.btn-send {
  background: var(--primary);
  color: white;
}

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

.btn-stop {
  background: var(--error);
  color: white;
  border: none;
  padding: 10px 24px;
  border-radius: 24px;
  cursor: pointer;
  font-size: 1rem;
}

.status-bar {
  margin-top: 6px;
  font-size: 0.8rem;
  color: var(--text-muted);
  text-align: center;
  min-height: 1.2em;
}

.mic-dialog {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.mic-dialog.hidden { display: none; }

.mic-dialog-content {
  background: var(--surface);
  border-radius: 24px;
  padding: 32px;
  text-align: center;
  min-width: 280px;
}

.mic-icon {
  font-size: 3rem;
  margin-bottom: 12px;
}

.mic-wave {
  height: 40px;
  margin: 16px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.mic-wave span {
  width: 4px;
  background: var(--primary);
  border-radius: 2px;
  animation: wave 0.5s ease infinite;
}

@keyframes wave {
  0%, 100% { height: 8px; }
  50% { height: 32px; }
}

@media (max-width: 640px) {
  .app { max-width: 100%; }
  .msg { max-width: 90%; }
  .debug-panel {
    position: fixed;
    top: 56px; right: 0; bottom: 64px;
    width: 280px;
    z-index: 50;
    box-shadow: -4px 0 12px rgba(0,0,0,0.15);
  }
}
