@import url('https://fonts.googleapis.com/css2?family=DM+Mono:wght@500&family=DM+Sans:opsz,wght@9..40,400;9..40,500;9..40,600;9..40,700&family=Fraunces:opsz,wght@9..144,600;9..144,700&display=swap');

:root {
  color-scheme: light;
  --ink: #1c1e38;
  --muted: #626783;
  --paper: #faf9f5;
  --surface: #ffffff;
  --surface-raised: #ffffff;
  --surface-soft: #f5f4fa;
  --lav: #e9e5ff;
  --violet: #7664d8;
  --violet-strong: #5142ad;
  --orange: #ff7956;
  --mint: #d9f4e8;
  --line: #deddea;
  --success: #2d755b;
  --danger: #962f20;
  --danger-surface: #fff0ec;
  --user-bubble: #302d57;
  --user-ink: #ffffff;
  --shadow: #e9e5ff;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { min-width: 0; scroll-behavior: smooth; }
body {
  min-height: 100dvh;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 9% 4%, rgb(233 229 255 / .55), transparent 25rem),
    var(--paper);
  font-family: 'DM Sans', system-ui, sans-serif;
}
button,
input,
textarea { font: inherit; }
button { cursor: pointer; }
button:focus-visible,
input:focus-visible,
textarea:focus-visible,
a:focus-visible {
  outline: 3px solid var(--orange);
  outline-offset: 3px;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.topbar {
  position: relative;
  z-index: 2;
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px max(22px, calc((100vw - 1180px) / 2));
  border-bottom: 1px solid var(--line);
  background: rgb(250 249 245 / .9);
  backdrop-filter: blur(14px);
}
.brand {
  min-width: max-content;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  font-weight: 700;
  letter-spacing: -.04em;
  text-decoration: none;
}
.brand-mark {
  width: 32px;
  height: 32px;
  display: grid;
  flex: 0 0 auto;
  place-items: center;
  border-radius: 10px;
  color: #ffffff;
  background: var(--ink);
  font-family: Fraunces, serif;
  font-size: 1rem;
  transform: rotate(-7deg);
}
.back {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--ink);
  background: rgb(255 255 255 / .82);
  font-size: .86rem;
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
}

.app {
  width: min(1120px, calc(100% - 40px));
  margin: 0 auto;
  padding: 38px 0 64px;
}
.intro {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(300px, 420px);
  align-items: end;
  gap: 44px;
  margin-bottom: 28px;
}
.eyebrow {
  margin: 0 0 10px;
  color: var(--violet-strong);
  font: 500 .72rem/1 'DM Mono', monospace;
  letter-spacing: .09em;
  text-transform: uppercase;
}
.intro h1 {
  margin: 0;
  font: 700 clamp(2.55rem, 5vw, 4.25rem) / .98 Fraunces, serif;
  letter-spacing: -.06em;
}
.intro-meta > p:first-child {
  max-width: 410px;
  margin: 0;
  color: var(--muted);
  font-size: .97rem;
  line-height: 1.6;
}

.workspace {
  min-height: 660px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: var(--surface);
  box-shadow: 12px 12px 0 var(--shadow);
}
.chat {
  min-height: 660px;
  display: flex;
  flex-direction: column;
  padding: 26px;
}
.mode-tabs {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 7px;
  padding: 5px;
  margin-bottom: 24px;
  border-radius: 14px;
  background: var(--surface-soft);
}
.mode-tab {
  min-height: 46px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  padding: 9px 14px;
  border: 0;
  border-radius: 10px;
  color: var(--muted);
  background: transparent;
  font-size: .85rem;
  font-weight: 700;
}
.mode-tab.active {
  color: var(--ink);
  background: var(--surface-raised);
  box-shadow: 0 2px 8px rgb(38 34 70 / .08);
}
.mode-tab--coming-soon:disabled {
  cursor: not-allowed;
  opacity: .72;
}
.coming-soon {
  padding: 5px 7px;
  border: 1px solid color-mix(in srgb, var(--violet) 45%, var(--line));
  border-radius: 999px;
  color: var(--violet-strong);
  background: var(--lav);
  font: 500 .62rem/1 'DM Mono', monospace;
  letter-spacing: .03em;
  text-transform: uppercase;
  white-space: nowrap;
}
.panel { display: none; }
.panel.active {
  min-height: 0;
  display: flex;
  flex: 1;
  flex-direction: column;
}
.chat-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
}

.homework-auth-gate {
  margin: 0 0 24px;
  padding: 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  border: 1px solid #d5cff5;
  border-radius: 15px;
  background: var(--lav);
}
.homework-auth-gate[hidden] { display: none; }
.homework-auth-gate h3 { margin: 3px 0 5px; font: 700 1.08rem Fraunces, serif; }
.homework-auth-gate p { max-width: 510px; margin: 0; color: var(--muted); font-size: .82rem; line-height: 1.5; }
.auth-gate-action {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  gap: 7px;
  padding: 10px 14px;
  border-radius: 11px;
  color: #fff;
  background: var(--violet);
  font-size: .82rem;
  font-weight: 800;
  text-decoration: none;
}
.auth-gate-action:hover { background: #5142ad; }
.chat-heading h2 {
  margin: 0 0 4px;
  font: 700 clamp(1.35rem, 2.5vw, 1.65rem) / 1.2 Fraunces, serif;
  letter-spacing: -.035em;
}
.chat-heading p {
  margin: 0;
  color: var(--muted);
  font-size: .86rem;
  line-height: 1.5;
}
.new-chat {
  min-width: 112px;
  min-height: 44px;
  display: inline-flex;
  flex: 0 0 auto;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 13px;
  border: 1px solid var(--line);
  border-radius: 11px;
  color: var(--ink);
  background: var(--surface-raised);
  font-size: .82rem;
  font-weight: 700;
}
.new-chat span { color: var(--violet-strong); font-size: 1.05rem; }
.new-chat.confirming {
  border-color: color-mix(in srgb, var(--danger) 45%, var(--line));
  color: var(--danger);
  background: var(--danger-surface);
}
.new-chat.confirming span { color: currentColor; }

.subject-picker {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 16px;
}
.field-label {
  color: var(--muted);
  font-size: .78rem;
  font-weight: 700;
}
.subject-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
}
.subject-chip {
  min-height: 44px;
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: 11px;
  color: var(--ink);
  background: var(--surface-raised);
  font-size: .8rem;
  font-weight: 700;
  transition: border-color .16s ease, background .16s ease, color .16s ease, transform .16s ease;
}
.subject-chip.selected,
.subject-chip[aria-pressed='true'] {
  border-color: var(--violet);
  color: var(--violet-strong);
  background: var(--lav);
}

.conversation {
  min-height: 300px;
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 15px;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 17px;
  background:
    linear-gradient(rgb(255 255 255 / .54), rgb(255 255 255 / .54)),
    var(--surface-soft);
  scrollbar-color: var(--line) transparent;
  scroll-behavior: smooth;
}
.message {
  width: fit-content;
  max-width: min(78%, 720px);
  padding: 13px 15px;
  border: 1px solid var(--line);
  border-radius: 16px 16px 16px 5px;
  color: var(--ink);
  background: var(--surface-raised);
  box-shadow: 0 2px 6px rgb(35 33 59 / .04);
  overflow-wrap: anywhere;
}
.message--assistant { align-self: flex-start; }
.message--user {
  align-self: flex-end;
  border-color: var(--user-bubble);
  border-radius: 16px 16px 5px 16px;
  color: var(--user-ink);
  background: var(--user-bubble);
}
.message--error {
  border-color: color-mix(in srgb, var(--danger) 35%, var(--line));
  color: var(--danger);
  background: var(--danger-surface);
}
.message--typing {
  min-width: 88px;
  animation: tutor-pulse 1.35s ease-in-out infinite;
}
.message-speaker {
  display: block;
  margin: 0 0 5px;
  color: var(--violet-strong);
  font: 500 .65rem/1.2 'DM Mono', monospace;
  letter-spacing: .05em;
  text-transform: uppercase;
}
.message--user .message-speaker { color: #ded9ff; }
.message--error .message-speaker { color: var(--danger); }
.message-body {
  min-width: 0;
  max-width: 100%;
  color: inherit;
  font-size: .91rem;
  line-height: 1.58;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.message-body > :first-child { margin-top: 0; }
.message-body > :last-child { margin-bottom: 0; }
.markdown-body {
  min-width: 0;
  max-width: 100%;
  white-space: normal;
}
.markdown-body .katex { color: inherit; }
.markdown-body .katex-display {
  max-width: 100%;
  margin: .9em 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding: .28em 0 .48em;
  scrollbar-width: thin;
}
.markdown-body .katex-display > .katex {
  min-width: max-content;
  padding: 0 .25em;
}
.markdown-body .katex-error {
  color: inherit !important;
  font-family: 'DM Mono', monospace;
  white-space: pre-wrap;
}
.markdown-body p { margin: .72em 0; }
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  margin: 1em 0 .42em;
  color: inherit;
  font-family: Fraunces, serif;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -.025em;
}
.markdown-body h1 { font-size: 1.45rem; }
.markdown-body h2 { font-size: 1.28rem; }
.markdown-body h3 { font-size: 1.14rem; }
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 { font-size: 1rem; }
.markdown-body ul,
.markdown-body ol {
  margin: .65em 0;
  padding-left: 1.45rem;
}
.markdown-body li { margin: .24em 0; }
.markdown-body li > ul,
.markdown-body li > ol { margin: .2em 0; }
.markdown-body blockquote {
  margin: .8em 0;
  padding: .55em .8em;
  border-left: 3px solid var(--violet);
  border-radius: 0 8px 8px 0;
  color: color-mix(in srgb, currentColor 82%, var(--muted));
  background: color-mix(in srgb, var(--lav) 58%, transparent);
}
.markdown-body blockquote > :first-child { margin-top: 0; }
.markdown-body blockquote > :last-child { margin-bottom: 0; }
.markdown-body a {
  color: var(--violet-strong);
  font-weight: 700;
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}
.message--user .markdown-body a { color: #ffffff; }
.markdown-body :not(pre) > code {
  padding: .14em .34em;
  border: 1px solid color-mix(in srgb, var(--line) 72%, transparent);
  border-radius: 5px;
  background: var(--surface-soft);
  font: 500 .88em/1.45 'DM Mono', monospace;
}
.message--user .markdown-body :not(pre) > code {
  border-color: rgb(255 255 255 / .25);
  background: rgb(18 16 45 / .24);
}
.markdown-body pre {
  max-width: 100%;
  margin: .8em 0;
  overflow-x: auto;
  padding: 12px 13px;
  border: 1px solid color-mix(in srgb, var(--line) 75%, transparent);
  border-radius: 9px;
  color: #f5f3ff;
  background: #242332;
  scrollbar-color: #68647c transparent;
  white-space: pre;
}
.markdown-body pre code {
  padding: 0;
  border: 0;
  color: inherit;
  background: transparent;
  font: 500 .82rem/1.55 'DM Mono', monospace;
  overflow-wrap: normal;
  word-break: normal;
}
.markdown-table-scroll {
  max-width: 100%;
  margin: .8em 0;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 9px;
}
.markdown-table-scroll:focus-visible {
  outline: 3px solid var(--orange);
  outline-offset: 2px;
}
.markdown-body table {
  width: max-content;
  min-width: 100%;
  border-collapse: collapse;
  font-size: .85rem;
}
.markdown-body th,
.markdown-body td {
  padding: 8px 10px;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}
.markdown-body th:last-child,
.markdown-body td:last-child { border-right: 0; }
.markdown-body tr:last-child td { border-bottom: 0; }
.markdown-body th { background: var(--surface-soft); }
.markdown-body hr {
  margin: 1em 0;
  border: 0;
  border-top: 1px solid var(--line);
}
.markdown-image-reference {
  display: inline-block;
  padding: .12em .42em;
  border: 1px dashed currentColor;
  border-radius: 5px;
  font-size: .82em;
  opacity: .82;
}
.message-attachment {
  width: max-content;
  max-width: min(100%, 340px);
  display: block;
  margin: 9px 0 0;
  padding: 6px 8px;
  border: 1px solid rgb(255 255 255 / .28);
  border-radius: 8px;
  font-size: .73rem;
  font-weight: 700;
}
.retry-message {
  min-height: 44px;
  margin-top: 10px;
  padding: 8px 12px;
  border: 1px solid currentColor;
  border-radius: 10px;
  color: inherit;
  background: transparent;
  font-size: .78rem;
  font-weight: 700;
}

.composer {
  margin-top: 14px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface-raised);
  box-shadow: 0 7px 22px rgb(42 38 73 / .08);
  transition: border-color .16s ease, box-shadow .16s ease;
}
.composer:focus-within {
  border-color: var(--violet);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--violet) 18%, transparent), 0 7px 22px rgb(42 38 73 / .08);
}
.composer textarea {
  width: 100%;
  min-height: 92px;
  max-height: 230px;
  display: block;
  resize: vertical;
  padding: 15px 16px 8px;
  border: 0;
  outline: 0;
  color: var(--ink);
  background: transparent;
  font-size: .94rem;
  line-height: 1.55;
}
.composer textarea::placeholder { color: color-mix(in srgb, var(--muted) 78%, transparent); }
.attachment-status {
  min-height: 24px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px 7px;
}
.file-status {
  min-width: 0;
  margin: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: .74rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.file-status.has-file { color: var(--success); font-weight: 700; }
.file-status.has-error { color: var(--danger); font-weight: 700; }
.remove-attachment {
  min-height: 44px;
  flex: 0 0 auto;
  margin: -10px 0;
  padding: 7px 4px;
  border: 0;
  color: var(--danger);
  background: transparent;
  font-size: .74rem;
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 3px;
}
.remove-attachment:disabled { cursor: wait; opacity: .58; }
.composer-notice {
  margin: 0;
  padding: 0 16px 9px;
  color: var(--muted);
  font-size: .76rem;
  line-height: 1.45;
}
.composer-notice:empty { display: none; }
.composer-notice.error { color: var(--danger); font-weight: 700; }
.composer-actions {
  min-height: 62px;
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 8px 10px;
  border-top: 1px solid var(--line);
  background: var(--surface-soft);
}
.attach-button {
  min-height: 44px;
  display: inline-flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 7px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 11px;
  color: var(--ink);
  background: var(--surface-raised);
  font-size: .79rem;
  font-weight: 700;
  cursor: pointer;
}
.attach-button input {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}
.attach-button:has(input:focus-visible) {
  outline: 3px solid var(--orange);
  outline-offset: 3px;
}
.attach-button:has(input:disabled) { cursor: wait; opacity: .58; }
.attach-icon { color: var(--violet-strong); font-size: 1.08rem; }
.composer-hint {
  margin-right: auto;
  color: var(--muted);
  font-size: .7rem;
  line-height: 1.35;
}
.send {
  min-width: 96px;
  min-height: 46px;
  display: inline-flex;
  flex: 0 0 auto;
  align-items: center;
  justify-content: center;
  gap: 9px;
  padding: 10px 15px;
  border: 0;
  border-radius: 12px;
  color: var(--paper);
  background: var(--ink);
  font-size: .84rem;
  font-weight: 700;
  transition: background .16s ease, opacity .16s ease, transform .16s ease;
}
.send span:last-child { font-size: 1rem; }
.send:disabled { cursor: wait; opacity: .58; }
.privacy-note {
  margin: 18px 0 0;
  color: var(--muted);
  font-size: .82rem;
  line-height: 1.55;
  text-align: center;
}

@keyframes tutor-pulse {
  0%, 100% { opacity: .55; }
  50% { opacity: 1; }
}

@media (hover: hover) {
  .back:hover,
  .new-chat:hover,
  .attach-button:hover { border-color: var(--violet); background: var(--lav); }
  .subject-chip:hover { border-color: var(--violet); transform: translateY(-1px); }
  .send:hover:not(:disabled) { background: var(--violet-strong); transform: translateY(-1px); }
  .retry-message:hover { background: color-mix(in srgb, currentColor 8%, transparent); }
}

:root[data-theme='dark'] {
  color-scheme: dark;
  --ink: #f5f3ff;
  --muted: #c8c5d7;
  --paper: #171723;
  --surface: #23232f;
  --surface-raised: #2a2a38;
  --surface-soft: #1e1e29;
  --lav: #3b375b;
  --violet: #b8aaff;
  --violet-strong: #d6ceff;
  --orange: #ff967a;
  --mint: #224a3f;
  --line: #4b4a5d;
  --success: #9ee0c7;
  --danger: #ffb7a5;
  --danger-surface: #482a28;
  --user-bubble: #554aa0;
  --user-ink: #ffffff;
  --shadow: #29273f;
}
:root[data-theme='dark'] body {
  background:
    radial-gradient(circle at 9% 4%, rgb(66 60 104 / .35), transparent 25rem),
    var(--paper);
}
:root[data-theme='dark'] .topbar { background: rgb(23 23 35 / .9); }
:root[data-theme='dark'] .brand-mark { color: #1c1e38; background: #f5f3ff; }
:root[data-theme='dark'] .back { background: rgb(42 42 56 / .86); }
:root[data-theme='dark'] .conversation {
  background:
    linear-gradient(rgb(24 24 34 / .2), rgb(24 24 34 / .2)),
    var(--surface-soft);
}
:root[data-theme='dark'] .mode-tab.active { box-shadow: 0 2px 8px rgb(0 0 0 / .22); }
:root[data-theme='dark'] .composer { box-shadow: 0 7px 22px rgb(0 0 0 / .2); }
:root[data-theme='dark'] .homework-auth-gate { border-color: #514f75; background: #2b2944; }
:root[data-theme='dark'] .auth-gate-action { color: #1c1e38; background: #c7bdff; }

@media (max-width: 760px) {
  .topbar {
    min-height: 64px;
    gap: 8px;
    padding: 9px 13px;
  }
  .brand { font-size: .88rem; }
  .back { padding: 9px 10px; font-size: .76rem; }
  .homework-auth-gate { align-items: stretch; flex-direction: column; }
  .auth-gate-action { width: 100%; }
  .app {
    width: min(100% - 26px, 1120px);
    padding: 28px 0 42px;
  }
  .intro {
    grid-template-columns: 1fr;
    gap: 15px;
    margin-bottom: 23px;
  }
  .intro h1 { font-size: clamp(2.4rem, 12vw, 3.5rem); }
  .intro-meta > p:first-child { max-width: 560px; }
  .workspace {
    min-height: 650px;
    border-radius: 19px;
    box-shadow: 7px 7px 0 var(--shadow);
  }
  .chat { min-height: 650px; padding: 18px; }
  .mode-tabs { margin-bottom: 20px; }
  .mode-tab { padding-inline: 8px; }
  .chat-heading { align-items: flex-start; }
  .subject-picker { align-items: flex-start; flex-direction: column; gap: 8px; }
  .conversation { min-height: 340px; padding: 15px; }
  .message { max-width: 90%; }
  .composer-hint { display: none; }
  .attach-button { margin-right: auto; }
}

@media (max-width: 430px) {
  .topbar .auth-control--topbar {
    width: 62px !important;
    min-width: 62px !important;
    max-width: 62px !important;
  }
  .topbar .back {
    width: 46px;
    min-width: 46px;
    padding: 0;
    font-size: 0;
  }
  .topbar .back::before {
    content: '←';
    font-size: 1rem;
  }
}

@media (max-width: 520px) {
  .topbar .brand { font-size: 0; }
  .topbar .brand-mark { font-size: 1rem; }
  .intro h1 { font-size: clamp(2.25rem, 13vw, 3.1rem); }
  .mode-tab {
    min-height: 52px;
    flex-wrap: wrap;
    gap: 4px 7px;
    font-size: .78rem;
  }
  .coming-soon { font-size: .54rem; }
  .chat-heading { flex-direction: column; gap: 12px; }
  .new-chat { width: 100%; }
  .subject-grid {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .subject-chip { width: 100%; }
  .conversation { min-height: 360px; }
  .message { max-width: 94%; }
  .composer textarea { min-height: 104px; }
  .composer-actions { gap: 8px; }
  .attach-button { padding-inline: 10px; }
  .send { min-width: 86px; }
  .privacy-note { text-align: left; }
}

@media (prefers-reduced-motion: reduce) {
  html,
  .conversation { scroll-behavior: auto; }
  *,
  *::before,
  *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}
