/* ═══════════════════════════════════════════════════════════════
   Kimi Math Tutor — Design System
   Premium dark theme with gold accent
   ═══════════════════════════════════════════════════════════════ */

@import url('https://fonts.googleapis.com/css2?family=Geist+Mono:wght@400;500&family=Instrument+Serif:ital@0;1&family=Inter:wght@300;400;500;600&display=swap');

/* ── CSS Variables ─────────────────────────────────────────────── */
:root {
  --bg:          #0a0a0a;
  --bg-subtle:   #0f0f0f;
  --surface:     #141414;
  --surface2:    #1a1a1a;
  --surface3:    #222222;
  --border:      rgba(255,255,255,0.06);
  --border-h:    rgba(255,255,255,0.12);
  --border-hh:   rgba(255,255,255,0.20);
  --text:        #ececec;
  --text-sec:    #b0b0b0;
  --muted:       #666666;
  --accent:      #d4a96a;
  --accent-dim:  rgba(212,169,106,0.10);
  --accent-mid:  rgba(212,169,106,0.18);
  --accent-glow: rgba(212,169,106,0.06);
  --success:     #4caf50;
  --warning:     #ff9800;
  --error:       #f05050;
  --radius:      16px;
  --radius-md:   12px;
  --radius-sm:   8px;
  --mono:        'Geist Mono', 'SF Mono', 'Cascadia Code', monospace;
  --serif:       'Instrument Serif', Georgia, 'Times New Roman', serif;
  --sans:        'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --shadow:      0 1px 3px rgba(0,0,0,0.4), 0 4px 16px rgba(0,0,0,0.2);
  --shadow-lg:   0 4px 12px rgba(0,0,0,0.5), 0 12px 40px rgba(0,0,0,0.3);
  --transition:  0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

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

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Scrollbar ─────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border-h);
  border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--border-hh);
}

/* ── App Layout ────────────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
}

/* ── Header ────────────────────────────────────────────────────── */
.header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  position: relative;
  z-index: 10;
}

.header::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--accent-dim), transparent);
}

.header-logo {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: linear-gradient(135deg, #d4a96a 0%, #a07840 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--serif);
  font-style: italic;
  font-size: 22px;
  color: #fff;
  flex-shrink: 0;
  box-shadow: 0 2px 12px rgba(212,169,106,0.25);
}

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

.header-title {
  font-family: var(--serif);
  font-size: 22px;
  font-weight: 400;
  color: var(--text);
  letter-spacing: -0.3px;
  line-height: 1.2;
}

.header-sub {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
  letter-spacing: 0.2px;
}

.header-badge {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--accent);
  background: var(--accent-dim);
  border: 1px solid rgba(212,169,106,0.15);
  padding: 5px 12px;
  border-radius: 20px;
  white-space: nowrap;
  letter-spacing: 0.5px;
}

.header-actions {
  display: flex;
  gap: 6px;
  margin-left: 8px;
}

.header-btn {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  transition: all var(--transition);
}

.header-btn:hover {
  color: var(--text);
  border-color: var(--border-h);
  background: var(--surface3);
}

.header-btn.danger:hover {
  color: var(--error);
  border-color: rgba(240,80,80,0.25);
  background: rgba(240,80,80,0.06);
}

/* ── Chat Area ─────────────────────────────────────────────────── */
.chat-area {
  flex: 1;
  overflow-y: auto;
  padding: 24px 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  scroll-behavior: smooth;
}

/* ── Welcome Screen ────────────────────────────────────────────── */
.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  text-align: center;
  padding: 40px 20px;
  animation: fadeIn 0.6s ease;
}

.welcome-icon {
  width: 72px;
  height: 72px;
  border-radius: 20px;
  background: linear-gradient(135deg, rgba(212,169,106,0.15), rgba(212,169,106,0.05));
  border: 1px solid rgba(212,169,106,0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  margin-bottom: 20px;
}

.welcome h2 {
  font-family: var(--serif);
  font-size: 26px;
  font-weight: 400;
  color: var(--text);
  margin-bottom: 8px;
  letter-spacing: -0.3px;
}

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

/* ── Quick Templates ───────────────────────────────────────────── */
.templates {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 24px;
  max-width: 500px;
}

.template-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--text-sec);
  font-size: 13px;
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.template-chip:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-dim);
  transform: translateY(-1px);
}

.template-chip .chip-icon {
  font-size: 14px;
}

/* ── Chat Messages ─────────────────────────────────────────────── */
.message {
  display: flex;
  gap: 12px;
  max-width: 100%;
  animation: slideUp 0.3s ease;
}

.message.user {
  flex-direction: row-reverse;
}

.msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
  margin-top: 2px;
}

.message.assistant .msg-avatar {
  background: linear-gradient(135deg, #d4a96a, #a07840);
  color: #fff;
  font-family: var(--serif);
  font-style: italic;
}

.message.user .msg-avatar {
  background: var(--surface3);
  border: 1px solid var(--border);
  color: var(--text-sec);
}

.msg-content {
  max-width: 85%;
  min-width: 0;
}

.msg-bubble {
  padding: 14px 18px;
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.7;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

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

.message.assistant .msg-bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

/* ── Markdown Styling inside bubbles ───────────────────────────── */
.msg-bubble h1, .msg-bubble h2, .msg-bubble h3,
.msg-bubble h4, .msg-bubble h5, .msg-bubble h6 {
  margin: 16px 0 8px;
  color: var(--text);
  font-family: var(--sans);
}

.msg-bubble h1 { font-size: 1.3em; }
.msg-bubble h2 { font-size: 1.15em; }
.msg-bubble h3 { font-size: 1.05em; }

.msg-bubble p {
  margin: 8px 0;
}

.msg-bubble p:first-child { margin-top: 0; }
.msg-bubble p:last-child  { margin-bottom: 0; }

.msg-bubble ul, .msg-bubble ol {
  margin: 8px 0;
  padding-left: 20px;
}

.msg-bubble li {
  margin: 4px 0;
}

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

.msg-bubble em {
  color: var(--accent);
  font-style: italic;
}

.msg-bubble code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--border);
  padding: 2px 6px;
  border-radius: 4px;
  color: var(--accent);
}

.msg-bubble pre {
  margin: 12px 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow-x: auto;
  position: relative;
}

.msg-bubble pre code {
  display: block;
  padding: 14px 16px;
  background: none;
  border: none;
  color: var(--text-sec);
  font-size: 13px;
  line-height: 1.6;
}

.msg-bubble blockquote {
  margin: 12px 0;
  padding: 10px 16px;
  border-left: 3px solid var(--accent);
  background: var(--accent-dim);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text-sec);
}

.msg-bubble hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 16px 0;
}

.msg-bubble table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 13px;
}

.msg-bubble th, .msg-bubble td {
  padding: 8px 12px;
  border: 1px solid var(--border);
  text-align: left;
}

.msg-bubble th {
  background: var(--surface3);
  color: var(--text);
  font-weight: 600;
}

/* ── KaTeX overrides ───────────────────────────────────────────── */
.msg-bubble .katex-display {
  margin: 16px 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 12px 0;
}

.msg-bubble .katex { color: var(--text); }

/* ── Image in chat ─────────────────────────────────────────────── */
.msg-image {
  max-width: 280px;
  max-height: 200px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  margin-bottom: 8px;
  object-fit: contain;
  background: var(--bg);
}

/* ── Status indicator ──────────────────────────────────────────── */
.msg-status {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  font-size: 11px;
  font-family: var(--mono);
  color: var(--muted);
}

.msg-status .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
}

/* ── Typing indicator ──────────────────────────────────────────── */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 4px;
  animation: slideUp 0.3s ease;
}

.typing-dots {
  display: flex;
  gap: 4px;
  padding: 14px 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  border-bottom-left-radius: 4px;
}

.typing-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.4;
  animation: typingBounce 1.4s infinite;
}

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

/* ── Cursor blink ──────────────────────────────────────────────── */
.streaming-cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--accent);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: cursorBlink 1s step-end infinite;
}

/* ── Input Area ────────────────────────────────────────────────── */
.input-area {
  padding: 16px 24px 20px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  position: relative;
}

.input-area::before {
  content: '';
  position: absolute;
  top: -20px;
  left: 0;
  right: 0;
  height: 20px;
  background: linear-gradient(to top, var(--bg), transparent);
  pointer-events: none;
}

.input-container {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px;
  transition: border-color var(--transition);
}

.input-container:focus-within {
  border-color: var(--border-hh);
}

.input-container textarea {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
  padding: 8px 10px;
  resize: none;
  outline: none;
  min-height: 24px;
  max-height: 150px;
  overflow-y: auto;
}

.input-container textarea::placeholder {
  color: var(--muted);
}

.input-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: all var(--transition);
  flex-shrink: 0;
}

.input-btn.upload-btn {
  background: var(--surface3);
  color: var(--muted);
  border: 1px solid var(--border);
}

.input-btn.upload-btn:hover {
  color: var(--text-sec);
  border-color: var(--border-h);
}

.input-btn.upload-btn.has-image {
  color: var(--success);
  border-color: rgba(76,175,80,0.3);
  background: rgba(76,175,80,0.08);
}

.input-btn.send-btn {
  background: linear-gradient(135deg, #d4a96a, #a07840);
  color: #fff;
  box-shadow: 0 2px 8px rgba(212,169,106,0.25);
}

.input-btn.send-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 3px 12px rgba(212,169,106,0.35);
}

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

.input-btn.stop-btn {
  background: var(--surface3);
  color: var(--error);
  border: 1px solid rgba(240,80,80,0.3);
}

.input-btn.stop-btn:hover {
  background: rgba(240,80,80,0.1);
  border-color: rgba(240,80,80,0.5);
}

/* Hidden file input */
#image-input {
  display: none;
}

/* ── Image Preview ─────────────────────────────────────────────── */
.image-preview-bar {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  margin-bottom: 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.image-preview-bar.visible {
  display: flex;
}

.image-preview-bar img {
  width: 48px;
  height: 48px;
  border-radius: 6px;
  object-fit: cover;
  border: 1px solid var(--border);
}

.image-preview-bar .preview-info {
  flex: 1;
  min-width: 0;
}

.image-preview-bar .preview-name {
  font-size: 12px;
  color: var(--text-sec);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.image-preview-bar .preview-size {
  font-size: 10px;
  color: var(--muted);
  font-family: var(--mono);
}

.image-preview-bar .preview-remove {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface3);
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  transition: all var(--transition);
}

.image-preview-bar .preview-remove:hover {
  color: var(--error);
  border-color: rgba(240,80,80,0.3);
  background: rgba(240,80,80,0.06);
}

/* ── Toast Notifications ───────────────────────────────────────── */
.toast-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 12px 18px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: var(--sans);
  display: flex;
  align-items: center;
  gap: 8px;
  animation: toastIn 0.3s ease, toastOut 0.3s ease 2.7s;
  animation-fill-mode: forwards;
  box-shadow: var(--shadow-lg);
}

.toast.success {
  background: rgba(76,175,80,0.15);
  border: 1px solid rgba(76,175,80,0.25);
  color: var(--success);
}

.toast.error {
  background: rgba(240,80,80,0.15);
  border: 1px solid rgba(240,80,80,0.25);
  color: var(--error);
}

.toast.info {
  background: var(--accent-dim);
  border: 1px solid rgba(212,169,106,0.25);
  color: var(--accent);
}

/* ── Export Modal ───────────────────────────────────────────────── */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
  z-index: 100;
  align-items: center;
  justify-content: center;
}

.modal-overlay.visible {
  display: flex;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 400px;
  width: 90%;
  box-shadow: var(--shadow-lg);
  animation: slideUp 0.3s ease;
}

.modal h3 {
  font-family: var(--serif);
  font-size: 18px;
  margin-bottom: 12px;
  color: var(--text);
}

.modal p {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 20px;
  line-height: 1.6;
}

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.btn {
  padding: 8px 18px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  font-family: var(--sans);
  cursor: pointer;
  transition: all var(--transition);
  border: 1px solid var(--border);
}

.btn-ghost {
  background: var(--surface2);
  color: var(--text-sec);
}

.btn-ghost:hover {
  color: var(--text);
  border-color: var(--border-h);
}

.btn-primary {
  background: var(--accent-dim);
  color: var(--accent);
  border-color: rgba(212,169,106,0.25);
}

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

/* ── Animations ────────────────────────────────────────────────── */
@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

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

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

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

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translateX(40px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes toastOut {
  from {
    opacity: 1;
    transform: translateX(0);
  }
  to {
    opacity: 0;
    transform: translateX(40px);
  }
}

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

/* ── Login Overlay ─────────────────────────────────────────────── */
.login-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px 32px;
  width: 100%;
  max-width: 380px;
  text-align: center;
  box-shadow: var(--shadow-lg);
  animation: slideUp 0.6s cubic-bezier(0.16, 1, 0.3, 1);
}

.login-icon {
  width: 64px;
  height: 64px;
  margin: 0 auto 20px;
  border-radius: 16px;
  background: linear-gradient(135deg, #d4a96a, #a07840);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--serif);
  font-style: italic;
  font-size: 32px;
  color: #fff;
  box-shadow: 0 4px 16px rgba(212,169,106,0.3);
}

.login-card h2 {
  font-family: var(--serif);
  font-size: 28px;
  font-weight: 400;
  margin-bottom: 8px;
  color: var(--text);
}

.login-card p {
  font-size: 14px;
  color: var(--muted);
  margin-bottom: 24px;
}

#login-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#login-password {
  width: 100%;
  padding: 12px 16px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  outline: none;
  transition: all var(--transition);
  text-align: center;
}

#login-password:focus {
  border-color: var(--border-hh);
  background: var(--surface3);
}

#login-btn {
  width: 100%;
  padding: 12px;
  font-size: 15px;
  border-radius: var(--radius-md);
}

.login-error {
  color: var(--error);
  font-size: 13px;
  margin-top: 12px;
  min-height: 18px;
}

/* ── Responsive ────────────────────────────────────────────────── */
@media (max-width: 640px) {
  .header { padding: 12px 16px; gap: 10px; }
  .header-logo { width: 36px; height: 36px; font-size: 18px; }
  .header-title { font-size: 18px; }
  .header-badge { display: none; }
  .chat-area { padding: 16px 16px 12px; gap: 16px; }
  .input-area { padding: 12px 16px 16px; }
  .msg-bubble { padding: 12px 14px; font-size: 13.5px; }
  .msg-content { max-width: 90%; }
  .welcome h2 { font-size: 22px; }
  .templates { gap: 6px; }
  .template-chip { font-size: 12px; padding: 6px 12px; }
}

@media (max-width: 380px) {
  .header-actions { gap: 4px; }
  .header-btn { width: 30px; height: 30px; font-size: 13px; }
  .msg-avatar { width: 28px; height: 28px; font-size: 12px; }
}
