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

:root {
  --bg: #f5f5f7;
  --bg-secondary: #ffffff;
  --bg-tertiary: #e8e8ed;
  --surface: #ffffff;
  --border: #d1d1d6;
  --text: #1d1d1f;
  --text-muted: #6e6e73;
  --primary: #6c63ff;
  --primary-glow: rgba(108, 99, 255, 0.2);
  --accent: #00b894;
  --accent-glow: rgba(0, 184, 148, 0.15);
  --error: #ff6b6b;
  --warning: #ffa94d;
  --user-bubble: #e8e5ff;
  --ai-bubble: #e6f7f2;
  --correction: #e67e22;
  --radius: 16px;
  --radius-sm: 10px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
}

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* === Layout === */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
}

/* === Header === */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  box-shadow: var(--shadow);
}

.header-left h1 {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.3px;
  color: var(--text);
}

.subtitle {
  font-size: 12px;
  color: var(--text-muted);
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  transition: all 0.2s;
}

.icon-btn:hover {
  background: var(--bg-tertiary);
  color: var(--text);
}

/* === Settings Panel === */
#settings-panel {
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  overflow: hidden;
  transition: max-height 0.3s ease, opacity 0.3s ease;
  max-height: 300px;
  opacity: 1;
  flex-shrink: 0;
}

#settings-panel.hidden {
  max-height: 0;
  opacity: 0;
  border: none;
}

.settings-content {
  padding: 16px 20px;
}

.setting-group {
  margin-bottom: 14px;
}

.setting-group label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

.radio-group {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.radio-btn {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-family: var(--font);
  font-size: 13px;
  padding: 6px 14px;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.2s;
}

.radio-btn:hover {
  border-color: var(--primary);
  color: var(--text);
}

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

.btn-secondary {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  font-family: var(--font);
  font-size: 13px;
  padding: 8px 20px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-secondary:hover {
  background: var(--error);
  border-color: var(--error);
  color: #fff;
}

/* === Chat Area === */
main#chat {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  scroll-behavior: smooth;
  background: var(--bg);
}

/* Scrollbar */
main#chat::-webkit-scrollbar {
  width: 4px;
}
main#chat::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

/* Welcome */
.welcome-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  text-align: center;
  gap: 12px;
  opacity: 0.8;
}

.welcome-icon {
  font-size: 56px;
  margin-bottom: 8px;
}

.welcome-screen h2 {
  font-size: 22px;
  font-weight: 600;
  color: var(--text);
}

.welcome-screen p {
  font-size: 14px;
  color: var(--text-muted);
  line-height: 1.6;
}

.hint {
  margin-top: 8px;
}

kbd {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-family: var(--font);
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}

/* Messages */
#messages {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.message {
  max-width: 85%;
  animation: msgIn 0.3s ease;
}

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

.message.user {
  align-self: flex-end;
}

.message.assistant {
  align-self: flex-start;
}

.msg-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.message.user .msg-label {
  text-align: right;
}

.msg-bubble {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 15px;
  line-height: 1.55;
  word-wrap: break-word;
  box-shadow: var(--shadow);
}

.message.user .msg-bubble {
  background: var(--user-bubble);
  border-bottom-right-radius: 4px;
}

.message.assistant .msg-bubble {
  background: var(--ai-bubble);
  border-bottom-left-radius: 4px;
  border-left: 3px solid var(--accent);
}

/* Grammar corrections in parentheses */
.correction {
  color: var(--correction);
  font-size: 13px;
  font-style: italic;
  font-weight: 500;
}

/* === Status Bar === */
#status-bar {
  padding: 8px 20px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  transition: opacity 0.3s;
}

#status-bar.hidden {
  opacity: 0;
  height: 0;
  padding: 0;
  border: none;
  overflow: hidden;
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-muted);
}

.pulse-dot {
  width: 8px;
  height: 8px;
  background: var(--primary);
  border-radius: 50%;
  animation: pulse 1.2s ease infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.8); }
}

/* === Footer / Controls === */
footer {
  padding: 16px 20px 24px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  box-shadow: 0 -2px 8px rgba(0,0,0,0.04);
}

.controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.controls-row {
  display: flex;
  align-items: center;
  gap: 16px;
}

.back-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-btn:hover {
  background: var(--bg-tertiary);
}

.mic-mode-toggle {
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mic-mode-label {
  font-size: 10px;
  color: var(--text-muted);
  opacity: 0.6;
}

.next-stage-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 8px 16px;
  border-radius: 20px;
  font-family: var(--font);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}

.next-stage-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 2px 8px var(--accent-glow);
}

.mic-btn {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--primary);
  border: none;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  position: relative;
  box-shadow: 0 4px 16px var(--primary-glow);
}

.mic-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 24px var(--primary-glow);
}

.mic-btn:active,
.mic-btn.recording {
  background: var(--error);
  transform: scale(1.1);
  box-shadow: 0 6px 24px rgba(255, 107, 107, 0.3);
}

.mic-btn.recording .mic-icon { display: none; }
.mic-btn.recording .stop-icon { display: block; }
.mic-btn:not(.recording) .stop-icon { display: none; }

.mic-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.mic-hint {
  font-size: 12px;
  color: var(--text-muted);
}

.hidden {
  display: none !important;
}

/* === Responsive === */
@media (max-width: 480px) {
  header {
    padding: 10px 14px;
  }

  .header-left h1 {
    font-size: 16px;
  }

  main#chat {
    padding: 14px;
  }

  .msg-bubble {
    font-size: 14px;
    padding: 10px 14px;
  }

  .mic-btn {
    width: 64px;
    height: 64px;
  }

  footer {
    padding: 12px 14px 20px;
  }

  .welcome-screen h2 {
    font-size: 18px;
  }

  .welcome-icon {
    font-size: 44px;
  }

  .radio-btn {
    font-size: 12px;
    padding: 5px 12px;
  }
}

/* === Lesson selector === */
.lesson-selector {
  display: flex;
  align-items: center;
  gap: 12px;
}

.lesson-input {
  width: 70px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: var(--font);
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  text-align: center;
}

.lesson-input:focus {
  outline: none;
  border-color: var(--primary);
}

.lesson-info {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.4;
}

/* === Progress card === */
.progress-card {
  width: 100%;
  max-width: 320px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 8px;
  box-shadow: var(--shadow);
}

.progress-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.progress-level {
  font-size: 14px;
  font-weight: 700;
  color: var(--primary);
}

.progress-count {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}

.progress-bar-container {
  width: 100%;
  height: 8px;
  background: var(--bg-tertiary);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 10px;
}

.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  border-radius: 4px;
  transition: width 0.5s ease;
}

.progress-details {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
}

.progress-details strong {
  color: var(--text);
}

/* === Welcome buttons === */
.welcome-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 16px;
  width: 100%;
  max-width: 320px;
}

.btn-primary-lg {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 20px;
  background: var(--primary);
  color: #fff;
  border: 2px solid var(--primary);
  border-radius: 14px;
  cursor: pointer;
  font-family: var(--font);
  text-align: left;
  transition: all 0.2s;
  box-shadow: var(--shadow-md);
}

.btn-primary-lg:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px var(--primary-glow);
}

.btn-primary-lg.btn-outline {
  background: var(--bg-secondary);
  color: var(--text);
  border-color: var(--border);
}

.btn-primary-lg.btn-outline:hover {
  border-color: var(--primary);
  box-shadow: 0 6px 20px var(--primary-glow);
}

.btn-primary-lg strong {
  display: block;
  font-size: 15px;
  font-weight: 600;
}

.btn-primary-lg small {
  display: block;
  font-size: 12px;
  opacity: 0.8;
  margin-top: 2px;
}

.btn-icon {
  font-size: 28px;
  flex-shrink: 0;
}

/* === Lesson Progress Bar === */
#lesson-progress {
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  flex-shrink: 0;
}

.progress-stages {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
}

.stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  position: relative;
}

.stage span {
  font-size: 10px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  white-space: nowrap;
}

.stage-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--bg-tertiary);
  border: 2px solid var(--border);
  transition: all 0.3s;
}

.stage.active .stage-dot {
  background: var(--primary);
  border-color: var(--primary);
  box-shadow: 0 0 8px var(--primary-glow);
}

.stage.active span {
  color: var(--primary);
}

.stage.completed .stage-dot {
  background: var(--accent);
  border-color: var(--accent);
}

.stage.completed span {
  color: var(--accent);
}

.stage-line {
  width: 32px;
  height: 2px;
  background: var(--border);
  margin: 0 4px;
  margin-bottom: 18px;
  transition: background 0.3s;
}

.stage-line.completed {
  background: var(--accent);
}

/* === Stage Divider === */
.stage-divider {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px 0 8px;
}

.stage-divider span {
  background: var(--bg-tertiary);
  color: var(--primary);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  padding: 4px 16px;
  border-radius: 20px;
}

/* === Safe area for notch devices === */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  footer {
    padding-bottom: calc(24px + env(safe-area-inset-bottom));
  }
}
