/* ═══════════════════════════════════════════════════════
   AiLeks — Programming Tutor
   Dark theme UI  |  system-ui font stack
═══════════════════════════════════════════════════════ */

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

html, body {
  height: 100%;
}

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  background-color: #1a1a2e;
  color: #e0e0e0;
  -webkit-font-smoothing: antialiased;
}

/* ── CSS Custom Properties ────────────────────────── */
:root {
  --bg-primary:    #1a1a2e;
  --bg-secondary:  #16213e;
  --bg-tertiary:   #0f3460;
  --bg-surface:    #2d2d44;
  --bg-input:      #252540;

  --accent-primary:  #3a86ff;
  --accent-hover:    #2970e0;
  --accent-active:   #1f5cc7;

  --text-primary:    #e0e0e0;
  --text-secondary:  #a0a0b8;
  --text-muted:      #666680;
  --text-on-accent:  #ffffff;

  --bubble-user-bg:  #3a86ff;
  --bubble-user-text: #ffffff;
  --bubble-bot-bg:   #2d2d44;
  --bubble-bot-text: #e0e0e0;

  --border-color:    #3a3a5c;
  --border-radius:   10px;
  --border-radius-sm: 6px;

  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.5);

  --transition: 0.18s ease;
}

/* ── View System ──────────────────────────────────── */
.view {
  display: flex;
  min-height: 100vh;
}

.view[hidden] {
  display: none !important;
}

/* ── Shared Button Styles ─────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 20px;
  border: none;
  border-radius: var(--border-radius-sm);
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color var(--transition), opacity var(--transition), transform var(--transition);
  text-decoration: none;
  white-space: nowrap;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none !important;
}

.btn-primary {
  background-color: var(--accent-primary);
  color: var(--text-on-accent);
}

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

.btn-primary:active:not(:disabled) {
  background-color: var(--accent-active);
  transform: translateY(1px);
}

.btn-secondary {
  background-color: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
}

.btn-secondary:hover:not(:disabled) {
  background-color: #3a3a5c;
}

.btn-ghost {
  background-color: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
  padding: 6px 12px;
}

.btn-ghost:hover:not(:disabled) {
  background-color: var(--bg-surface);
  color: var(--text-primary);
}

.btn-sm {
  padding: 5px 10px;
  font-size: 13px;
}

.btn-full {
  width: 100%;
}

/* ── Form Elements ────────────────────────────────── */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 18px;
}

.form-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.form-input,
.form-select,
.form-textarea {
  background-color: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-sm);
  padding: 10px 13px;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  width: 100%;
  transition: border-color var(--transition), box-shadow var(--transition);
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

.form-input::placeholder,
.form-textarea::placeholder {
  color: var(--text-muted);
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus {
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 3px rgba(58, 134, 255, 0.18);
}

.form-select {
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%23a0a0b8' stroke-width='1.5' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 34px;
}

.form-select option {
  background-color: var(--bg-secondary);
}

.form-textarea {
  resize: vertical;
  min-height: 90px;
}

.char-counter {
  text-align: right;
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.char-counter.warn {
  color: #f4a261;
}

.char-counter.over {
  color: #e63946;
}

.form-error {
  background-color: rgba(230, 57, 70, 0.12);
  border: 1px solid rgba(230, 57, 70, 0.4);
  color: #ff6b6b;
  border-radius: var(--border-radius-sm);
  padding: 10px 13px;
  font-size: 13px;
  line-height: 1.5;
  margin-bottom: 14px;
}

/* ── Onboarding View ──────────────────────────────── */
.onboarding-container {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  width: 100%;
  padding: 40px 16px;
  background: var(--bg-primary);
}

.onboarding-card {
  width: 100%;
  max-width: 500px;
  background-color: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius);
  padding: 40px 36px;
  box-shadow: var(--shadow-md);
}

.onboarding-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 32px;
}

.onboarding-header-text {
  flex: 1;
  min-width: 0;
}

.onboarding-title {
  font-size: 26px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.onboarding-subtitle {
  font-size: 14px;
  color: var(--text-secondary);
}

.register-form {
  margin-bottom: 24px;
}

/* Language toggle button */
.lang-toggle {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background-color: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-sm);
  padding: 6px 14px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  letter-spacing: 0.05em;
  transition: background-color var(--transition), border-color var(--transition);
  margin-top: 4px;
}

.lang-toggle:hover {
  background-color: #3a3a5c;
  border-color: var(--accent-primary);
}

.lang-flag {
  font-size: 16px;
  line-height: 1;
}

/* Token display after registration */
.token-display {
  background-color: rgba(58, 134, 255, 0.08);
  border: 1px solid rgba(58, 134, 255, 0.3);
  border-radius: var(--border-radius);
  padding: 20px;
  margin-bottom: 24px;
}

.token-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--accent-primary);
  margin-bottom: 6px;
}

.token-instruction {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 14px;
  line-height: 1.5;
}

.token-copy-row {
  display: flex;
  gap: 8px;
}

.token-input {
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: 13px;
  letter-spacing: 0.02em;
  background-color: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-sm);
  padding: 8px 12px;
  flex: 1;
  min-width: 0;
  outline: none;
  cursor: text;
  user-select: all;
  -webkit-user-select: all;
}

/* Accordion */
.accordion {
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-sm);
  overflow: hidden;
}

.accordion-summary {
  padding: 13px 16px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  user-select: none;
  background-color: var(--bg-surface);
  transition: background-color var(--transition), color var(--transition);
}

.accordion-summary::-webkit-details-marker {
  display: none;
}

.accordion-summary::after {
  content: '';
  display: inline-block;
  width: 10px;
  height: 6px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%23a0a0b8' stroke-width='1.5' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: center;
  transition: transform var(--transition);
}

details[open] > .accordion-summary::after {
  transform: rotate(180deg);
}

.accordion-summary:hover {
  background-color: #3a3a5c;
  color: var(--text-primary);
}

.accordion-body {
  padding: 16px;
  background-color: var(--bg-input);
  border-top: 1px solid var(--border-color);
}

/* ── Chat View ────────────────────────────────────── */
.chat-shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100vh;
  background-color: var(--bg-primary);
}

/* Chat header */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 56px;
  background-color: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
  z-index: 10;
}

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

.chat-bot-name {
  font-size: 17px;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: -0.01em;
}

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

.token-display-mini {
  display: flex;
  align-items: center;
  gap: 8px;
}

.token-mini-text {
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: 12px;
  color: var(--text-muted);
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Messages container */
.messages-container {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 24px 20px;
  scroll-behavior: smooth;
}

/* Scrollbar styling */
.messages-container::-webkit-scrollbar {
  width: 6px;
}

.messages-container::-webkit-scrollbar-track {
  background: transparent;
}

.messages-container::-webkit-scrollbar-thumb {
  background-color: var(--border-color);
  border-radius: 3px;
}

/* Message list */
.messages {
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

/* Individual message row */
.message-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  animation: fadeSlideIn 0.18s ease;
}

.message-row.user {
  align-items: flex-end;
}

.message-row.bot {
  align-items: flex-start;
}

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

/* Message bubble */
.message-bubble {
  max-width: 75%;
  padding: 11px 15px;
  border-radius: var(--border-radius);
  line-height: 1.6;
  word-break: break-word;
  overflow-wrap: break-word;
}

.message-row.user .message-bubble {
  background-color: var(--bubble-user-bg);
  color: var(--bubble-user-text);
  border-bottom-right-radius: 3px;
}

.message-row.bot .message-bubble {
  background-color: var(--bubble-bot-bg);
  color: var(--bubble-bot-text);
  border-bottom-left-radius: 3px;
}

/* Sender label */
.message-sender {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 0 4px;
}

/* Markdown content inside bot bubbles */
.message-bubble h1,
.message-bubble h2,
.message-bubble h3,
.message-bubble h4 {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 12px 0 6px;
  line-height: 1.4;
}

.message-bubble h1:first-child,
.message-bubble h2:first-child,
.message-bubble h3:first-child {
  margin-top: 0;
}

.message-bubble p {
  margin: 0 0 10px;
}

.message-bubble p:last-child {
  margin-bottom: 0;
}

.message-bubble ul,
.message-bubble ol {
  margin: 6px 0 10px;
  padding-left: 22px;
}

.message-bubble li {
  margin-bottom: 3px;
}

.message-bubble strong {
  font-weight: 600;
  color: var(--text-primary);
}

.message-bubble em {
  font-style: italic;
  color: var(--text-secondary);
}

/* Inline code */
.message-bubble code {
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: 13px;
  background-color: rgba(0, 0, 0, 0.35);
  padding: 1px 5px;
  border-radius: 4px;
  color: #a8d8ff;
}

/* Code blocks */
.message-bubble pre {
  background-color: #1e1e2e;
  border: 1px solid #2a2a40;
  border-radius: 8px;
  padding: 14px 16px;
  overflow-x: auto;
  margin: 10px 0;
  font-size: 13px;
  line-height: 1.5;
}

.message-bubble pre:last-child {
  margin-bottom: 0;
}

.message-bubble pre code {
  background: none;
  padding: 0;
  border-radius: 0;
  color: #cdd6f4;
  font-size: inherit;
}

/* Code block scrollbar */
.message-bubble pre::-webkit-scrollbar {
  height: 4px;
}

.message-bubble pre::-webkit-scrollbar-track {
  background: transparent;
}

.message-bubble pre::-webkit-scrollbar-thumb {
  background-color: #3a3a5c;
  border-radius: 2px;
}

/* Thinking indicator */
.thinking-indicator {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 15px;
  background-color: var(--bubble-bot-bg);
  border-radius: var(--border-radius);
  border-bottom-left-radius: 3px;
  color: var(--text-secondary);
  font-size: 13px;
}

.thinking-dots {
  display: flex;
  gap: 4px;
}

.thinking-dots span {
  display: inline-block;
  width: 6px;
  height: 6px;
  background-color: var(--text-muted);
  border-radius: 50%;
  animation: thinkingBounce 1.2s infinite ease-in-out;
}

.thinking-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

.thinking-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes thinkingBounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-5px);
    opacity: 1;
  }
}

/* System message (welcome back, errors) */
.message-row.system .message-bubble {
  background-color: transparent;
  color: var(--text-muted);
  font-size: 13px;
  font-style: italic;
  border: 1px solid var(--border-color);
  text-align: center;
  max-width: 100%;
}

.message-row.error .message-bubble {
  background-color: rgba(230, 57, 70, 0.1);
  border: 1px solid rgba(230, 57, 70, 0.3);
  color: #ff6b6b;
  max-width: 100%;
}

/* ── Chat Input Area ──────────────────────────────── */
.chat-input-area {
  flex-shrink: 0;
  background-color: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  padding: 14px 20px 16px;
}

.chat-input-wrapper {
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.message-textarea {
  width: 100%;
  background-color: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-sm);
  padding: 10px 13px;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  resize: none;
  outline: none;
  overflow-y: hidden;
  max-height: calc(4 * 1.5em + 20px); /* 4 rows */
  transition: border-color var(--transition), box-shadow var(--transition);
}

.message-textarea::placeholder {
  color: var(--text-muted);
}

.message-textarea:focus {
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 3px rgba(58, 134, 255, 0.18);
}

.input-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

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

.chat-char-counter.warn {
  color: #f4a261;
}

.chat-char-counter.over {
  color: #e63946;
}

.send-btn {
  padding: 8px 22px;
  font-size: 14px;
  font-weight: 500;
}

/* ── Utility ──────────────────────────────────────── */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ── Responsive ───────────────────────────────────── */
@media (max-width: 600px) {
  .onboarding-card {
    padding: 28px 20px;
  }

  .onboarding-title {
    font-size: 22px;
  }

  .message-bubble {
    max-width: 90%;
  }

  .chat-header {
    padding: 0 12px;
    height: 50px;
  }

  .messages-container {
    padding: 16px 12px;
  }

  .chat-input-area {
    padding: 10px 12px 12px;
  }

  .token-display-mini {
    display: none;
  }

  .chat-bot-name {
    font-size: 15px;
  }
}

@media (max-width: 400px) {
  .lang-toggle {
    padding: 5px 10px;
    font-size: 12px;
  }
}
