:root {
  --primary: #6366f1;
  --primary-dark: #4f46e5;
  --accent: #ec4899;
  --bg: #f0f4ff;
  --card: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --bubble-tutor: #f3f4f6;
  --error-bg: #fef2f2;
  --error-text: #b91c1c;
  --focus: rgba(99, 102, 241, 0.35);
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

[hidden] { display: none !important; }

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-weight: 700; font-size: 18px; }
.logo {
  width: 32px; height: 32px; border-radius: 8px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  color: #fff; font-size: 18px;
}
.brand-ai {
  margin-left: 2px; padding: 1px 6px; border-radius: 6px;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  color: #fff; font-size: 12px; vertical-align: 2px;
}
.topbar-actions { display: flex; gap: 4px; }

main {
  flex: 1;
  display: flex;
  justify-content: center;
  padding: 16px;
  min-height: 0;
}

.card {
  width: 100%;
  max-width: 720px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(17, 24, 39, 0.06);
}

h1 { margin: 0; font-size: 22px; line-height: 1.3; }
.muted { color: var(--muted); margin: 4px 0 0; }
.small { font-size: 14px; }
.tiny { font-size: 12px; margin: 8px 0 0; }

/* Controls */
button { font: inherit; cursor: pointer; border-radius: 10px; border: 0; white-space: nowrap; }
.suggestion { white-space: normal; }
button:disabled { opacity: 0.5; cursor: default; }
button:focus-visible, input:focus-visible, textarea:focus-visible, a:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--focus);
}
.primary { background: var(--primary); color: #fff; padding: 10px 16px; font-weight: 600; }
.primary:hover:not(:disabled) { background: var(--primary-dark); }
.secondary { background: #e5e7eb; color: #374151; padding: 10px 16px; font-weight: 600; }
.ghost { background: transparent; color: var(--primary); padding: 6px 10px; font-size: 14px; }
.ghost:hover { background: rgba(99, 102, 241, 0.08); }
.link-button { background: transparent; color: var(--muted); padding: 6px 10px; font-size: 14px; }
.link-button:hover { color: var(--text); background: #f3f4f6; }

input, textarea {
  width: 100%;
  font: inherit;
  color: inherit;
  padding: 10px 12px;
  border: 1px solid #d1d5db;
  border-radius: 10px;
  background: #fff;
}
input:focus, textarea:focus { border-color: var(--primary); }

a { color: var(--primary); }

.error {
  margin: 12px 0 0;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--error-bg);
  color: var(--error-text);
  font-size: 14px;
}

/* Login */
.login { align-self: flex-start; max-width: 420px; margin-top: 8vh; padding: 28px 24px; }
.login form { display: flex; flex-direction: column; margin-top: 20px; }
.login label { font-size: 14px; font-weight: 600; margin: 12px 0 6px; }
.login .primary { margin-top: 20px; width: 100%; padding: 12px; }
.login .small { margin-top: 16px; text-align: center; }

/* Chat */
.chat {
  display: flex;
  flex-direction: column;
  padding: 16px;
  height: calc(100dvh - 57px - 32px);
  min-height: 420px;
}
.chat-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.topic { display: flex; flex-direction: column; gap: 6px; margin: 12px 0; font-size: 14px; font-weight: 600; }
.topic input { font-weight: 400; padding: 8px 12px; }

.messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 4px 2px 12px;
  min-height: 0;
}
.bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 16px;
  overflow-wrap: anywhere;
}
.bubble.user {
  align-self: flex-end;
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}
.bubble.tutor {
  align-self: flex-start;
  background: var(--bubble-tutor);
  border-bottom-left-radius: 4px;
}
.bubble .gap { height: 8px; }
.bubble .li { display: flex; gap: 6px; padding-left: 2px; }
.bubble .li > span:first-child { flex-shrink: 0; }

.suggestions { display: flex; flex-wrap: wrap; gap: 8px; }
.suggestion {
  background: rgba(99, 102, 241, 0.06);
  border: 1px solid rgba(99, 102, 241, 0.3);
  color: var(--primary-dark);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 14px;
  text-align: left;
}
.suggestion:hover { background: rgba(99, 102, 241, 0.12); }

.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing span {
  width: 6px; height: 6px; border-radius: 50%;
  background: #9ca3af;
  animation: bounce 1s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes bounce { 0%, 80%, 100% { transform: translateY(0); } 40% { transform: translateY(-4px); } }
@media (prefers-reduced-motion: reduce) { .typing span { animation: none; } }

.composer { display: flex; gap: 8px; align-items: flex-end; border-top: 1px solid var(--border); padding-top: 12px; }
.composer textarea { resize: none; max-height: 140px; min-height: 44px; }

@media (max-width: 600px) {
  main { padding: 0; }
  .chat { border-radius: 0; border-left: 0; border-right: 0; height: calc(100dvh - 57px); padding: 12px 16px; }
  .login { margin: 16px; }
  .brand-text { font-size: 16px; }
}
