* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'IBM Plex Sans', 'Segoe UI', sans-serif;
  background:
    radial-gradient(circle at 12% 18%, #ffd9b8 0, #ffd9b800 38%),
    radial-gradient(circle at 82% 22%, #bae6fd 0, #bae6fd00 36%),
    linear-gradient(135deg, #faf7f2, #eef4ff 50%, #f6f7fb);
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}

body.is-locked {
  display: block;
  padding: 28px 18px;
}

.landing-shell {
  width: min(980px, 100%);
  margin: 0 auto;
}

.landing-card {
  background: #ffffffde;
  border: 1px solid #d9e4f5;
  border-radius: 24px;
  box-shadow: 0 26px 70px #0f172a1f;
  padding: clamp(20px, 4vw, 42px);
  position: relative;
  overflow: hidden;
}

.landing-card::before {
  content: '';
  position: absolute;
  inset: -30% auto auto -15%;
  width: 360px;
  height: 360px;
  background: radial-gradient(circle, #fcd9be, #fcd9be00 68%);
  pointer-events: none;
}

.landing-card::after {
  content: '';
  position: absolute;
  inset: auto -12% -30% auto;
  width: 320px;
  height: 320px;
  background: radial-gradient(circle, #bae6fd, #bae6fd00 66%);
  pointer-events: none;
}

.landing-kicker,
.landing-card h1,
.landing-copy,
.landing-note,
.student-access-form {
  position: relative;
  z-index: 1;
}

.landing-kicker {
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 0.78rem;
  font-weight: 700;
  color: #334155;
}

.landing-card h1 {
  margin: 10px 0 12px;
  max-width: 19ch;
  line-height: 1.1;
  font-size: clamp(1.7rem, 4.1vw, 3rem);
  color: #0f172a;
}

.landing-copy {
  margin: 0;
  max-width: 66ch;
  color: #334155;
  line-height: 1.65;
}

.student-access-form {
  margin-top: 22px;
  display: grid;
  gap: 8px;
}

.student-access-form label {
  font-size: 0.85rem;
  color: #334155;
  font-weight: 600;
}

.student-access-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
}

#studentCodeInput {
  width: 100%;
  border: 1px solid #c8d6eb;
  border-radius: 12px;
  padding: 12px 13px;
  font-size: 0.96rem;
}

#studentAccessBtn {
  border: none;
  border-radius: 12px;
  padding: 0 18px;
  font-size: 0.92rem;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(135deg, #0369a1, #1d4ed8);
  cursor: pointer;
}

.student-code-error {
  min-height: 1.2em;
  margin: 0;
  color: #b91c1c;
  font-size: 0.84rem;
}

.landing-note {
  margin: 16px 0 0;
  max-width: 72ch;
  border-left: 3px solid #60a5fa;
  padding-left: 10px;
  font-size: 0.9rem;
  color: #1e3a8a;
  line-height: 1.55;
}

.chat-app {
  width: min(1380px, 100%);
  height: min(95vh, 980px);
  background: #ffffffd9;
  border: 1px solid #d8dfed;
  border-radius: 22px;
  backdrop-filter: blur(8px);
  box-shadow: 0 24px 70px #1f293726;
  display: grid;
  grid-template-rows: auto 1fr;
  overflow: hidden;
}

.chat-app.drag-active {
  border-color: #2563eb;
  box-shadow: 0 0 0 3px #93c5fd99, 0 24px 70px #1f293726;
}

.chat-header {
  padding: 16px 18px;
  border-bottom: 1px solid #e4e7ec;
  display: flex;
  gap: 16px;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  background: #ffffffd1;
}

.app-body {
  min-height: 0;
  display: grid;
  grid-template-columns: 292px 1fr;
  position: relative;
}

.mobile-topbar,
.mobile-menu-toggle,
.mobile-avatar-toggle {
  display: none;
}

.topic-sidebar {
  border-right: 1px solid #e4e7ec;
  background: #fbfdff;
  padding: 12px;
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 10px;
  min-height: 0;
}

.topic-sidebar-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.topic-sidebar-head h2 {
  margin: 0;
  font-size: 0.92rem;
  color: #334155;
}

#newTopicBtn {
  border: 1px solid #bfdbfe;
  background: #eff6ff;
  color: #1e3a8a;
  border-radius: 9px;
  font-size: 0.78rem;
  padding: 6px 10px;
  cursor: pointer;
}

#newTopicBtn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.topic-count {
  font-size: 0.74rem;
  color: #64748b;
}

.topic-list {
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 7px;
  padding-right: 2px;
}

.topic-item {
  border: 1px solid #dbe5f3;
  border-radius: 12px;
  background: white;
  padding: 6px;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: start;
  gap: 6px;
}

.topic-item.active {
  border-color: #60a5fa;
  background: #eff6ff;
  color: #1d4ed8;
}

.topic-select {
  border: none;
  background: transparent;
  color: inherit;
  text-align: left;
  border-radius: 8px;
  padding: 5px 6px;
  cursor: pointer;
  min-width: 0;
  width: 100%;
}

.topic-delete {
  border: 1px solid #e2e8f0;
  background: #f8fafc;
  color: #64748b;
  border-radius: 8px;
  width: 24px;
  height: 24px;
  cursor: pointer;
}

.topic-item.active .topic-delete {
  border-color: #bfdbfe;
  background: #dbeafe;
  color: #1d4ed8;
}

.topic-title {
  font-size: 0.84rem;
  font-weight: 600;
  display: block;
  line-height: 1.3;
  overflow-wrap: anywhere;
  word-break: break-word;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.topic-meta {
  margin-top: 4px;
  font-size: 0.72rem;
  color: #64748b;
  display: block;
}

.chat-panel {
  min-height: 0;
  display: grid;
  grid-template-rows: 1fr auto auto;
}

.brand h1 {
  margin: 0;
  font-size: 1.15rem;
  letter-spacing: 0.01em;
}

.brand p {
  margin: 4px 0 0;
  color: #4b5563;
  font-size: 0.84rem;
}

.transparency-note {
  margin-top: 7px;
  border-left: 3px solid #60a5fa;
  padding-left: 8px;
  font-size: 0.78rem;
  color: #1d4ed8;
}

.chat-controls {
  display: flex;
  gap: 10px;
  align-items: end;
  flex-wrap: nowrap;
}

.chat-controls label {
  font-size: 0.76rem;
  color: #4b5563;
  display: flex;
  flex-direction: column;
  gap: 5px;
  flex: 0 0 218px;
  min-width: 218px;
}

.chat-controls select,
#clearChatBtn {
  border: 1px solid #c9d4e7;
  border-radius: 10px;
  background: white;
  padding: 8px 10px;
  font-size: 0.86rem;
  height: 42px;
}

.chat-controls select {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#clearChatBtn {
  cursor: pointer;
  background: #fff7ed;
  border-color: #fdba74;
  flex: 0 0 168px;
  min-width: 168px;
}

.auto-context {
  border: 1px solid #bbf7d0;
  background: #f0fdf4;
  color: #166534;
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 0.8rem;
}

.chat-window {
  padding: 18px 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.message {
  max-width: min(90%, 980px);
  border-radius: 14px;
  animation: pop-in 180ms ease;
}

.message-body {
  line-height: 1.55;
  font-size: 0.96rem;
  padding: 12px 13px;
}

.message-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 13px 12px;
}

.message-attachment-chip {
  font-size: 0.75rem;
  border-radius: 999px;
  padding: 4px 8px;
  border: 1px solid #cbd5e1;
  background: #f8fafc;
  color: #334155;
}

.message.user .message-attachment-chip {
  background: #ffffff22;
  color: #f8fafc;
  border-color: #ffffff55;
}

.message.user {
  align-self: flex-end;
  background: linear-gradient(135deg, #0f766e, #2563eb);
  color: white;
  box-shadow: 0 12px 22px #2563eb33;
}

.message.ai {
  align-self: flex-start;
  background: #f8fafc;
  color: #1e293b;
  border: 1px solid #e5e7eb;
}

.typing-message {
  min-width: 72px;
}

.typing-indicator {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.typing-indicator span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #64748b;
  animation: typing-blink 1.1s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 0.15s;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 0.3s;
}

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

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

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

.message-body li + li {
  margin-top: 4px;
}

.message-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 10px 0;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  overflow: hidden;
  display: block;
  overflow-x: auto;
}

.message-body thead {
  background: #e2e8f0;
}

.message-body th,
.message-body td {
  border: 1px solid #cbd5e1;
  padding: 8px 10px;
  text-align: left;
  vertical-align: top;
  min-width: 120px;
}

.message-body th {
  font-weight: 700;
  color: #1e293b;
}

.message-body code {
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: 0.89em;
  background: #0f172a10;
  border-radius: 6px;
  padding: 0.14em 0.33em;
}

.code-wrap {
  border: 1px solid #d6dde8;
  border-radius: 12px;
  overflow: hidden;
  margin: 10px 0;
  background: #0f172a;
}

.code-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #111827;
  color: #d1d5db;
  padding: 7px 10px;
  font-size: 0.78rem;
}

.code-lang {
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.copy-code-btn {
  border: 1px solid #4b5563;
  background: #1f2937;
  color: #f9fafb;
  border-radius: 8px;
  font-size: 0.76rem;
  padding: 4px 10px;
  cursor: pointer;
}

.code-wrap pre {
  margin: 0;
  overflow-x: auto;
  padding: 12px;
}

.code-wrap pre code {
  background: transparent;
  color: #e2e8f0;
  padding: 0;
}

#fileInput {
  display: none;
}

.upload-trigger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  min-width: 42px;
  height: 42px;
  border: none;
  border-right: 1px solid #dbe4f0;
  cursor: pointer;
  font-size: 1.2rem;
  color: #334155;
  background: transparent;
  white-space: nowrap;
  line-height: 1;
}

.attachment-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  grid-column: 1 / -1;
}

.attachment-chip {
  background: #eff6ff;
  color: #1e3a8a;
  border: 1px solid #bfdbfe;
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 0.8rem;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.attachment-chip button {
  border: none;
  background: transparent;
  color: #1e3a8a;
  cursor: pointer;
  font-size: 0.9rem;
}

.chat-form {
  border-top: 1px solid #e4e7ec;
  padding: 12px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  background: #ffffffcf;
}

.composer-input {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  background: #fff;
  overflow: hidden;
  min-height: 44px;
}

#messageInput {
  width: 100%;
  padding: 11px 12px;
  border: none;
  border-radius: 0;
  outline: none;
  background: transparent;
  font-size: 15px;
}

#sendBtn {
  border: none;
  border-radius: 10px;
  min-height: 44px;
  padding: 0 18px;
  font-size: 15px;
  background: linear-gradient(135deg, #1d4ed8, #2563eb);
  color: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

#sendBtn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

@keyframes pop-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes typing-blink {
  0%,
  80%,
  100% {
    transform: translateY(0);
    opacity: 0.35;
  }

  40% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

@media (max-width: 740px) {
  body {
    padding: 0;
  }

  body.is-locked {
    padding: 12px;
  }

  .student-access-row {
    grid-template-columns: 1fr;
  }

  #studentAccessBtn {
    min-height: 44px;
  }

  .chat-app {
    width: 100%;
    height: 100vh;
    border-radius: 0;
    position: relative;
  }

  .chat-header {
    padding: 56px 12px 10px;
    gap: 10px;
    align-items: flex-start;
    min-height: 80px;
    position: relative;
  }

  .mobile-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: absolute;
    top: 10px;
    left: 12px;
    right: 12px;
    z-index: 6;
  }

  .mobile-menu-toggle,
  .mobile-avatar-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #cbd5e1;
    background: #fff;
    color: #334155;
    box-shadow: 0 6px 14px #0f172a1a;
    cursor: pointer;
  }

  .mobile-menu-toggle {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    font-size: 1rem;
    line-height: 1;
  }

  .mobile-avatar-toggle {
    width: 38px;
    height: 38px;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.03em;
    background: linear-gradient(135deg, #0f766e, #1d4ed8);
    color: #fff;
    border-color: #1d4ed8;
  }

  .brand {
    width: 100%;
    padding-right: 42px;
  }

  .brand h1 {
    font-size: 1rem;
  }

  .brand p {
    font-size: 0.8rem;
  }

  .app-body {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
    min-height: 0;
  }

  .topic-sidebar {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(78vw, 320px);
    border-right: 1px solid #e4e7ec;
    border-bottom: none;
    grid-template-rows: auto auto 1fr;
    transform: translateX(-108%);
    transition: transform 220ms ease;
    z-index: 3;
    box-shadow: 8px 0 24px #0f172a24;
  }

  .chat-controls {
    position: absolute;
    top: 56px;
    right: 10px;
    width: min(82vw, 360px);
    background: #ffffff;
    border: 1px solid #dbe5f3;
    border-radius: 14px;
    box-shadow: 0 14px 30px #0f172a26;
    padding: 10px;
    display: none;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    z-index: 5;
  }

  .chat-controls label,
  .auto-context,
  .chat-controls select,
  #clearChatBtn {
    width: 100%;
    min-width: 0;
    flex: 1 1 auto;
  }

  #clearChatBtn {
    height: 42px;
  }

  .chat-app.mobile-topics-open .topic-sidebar {
    transform: translateX(0);
  }

  .chat-app.mobile-settings-open .chat-controls {
    display: flex;
  }

  .chat-app.mobile-topics-open .mobile-menu-toggle {
    border-color: #60a5fa;
    background: #dbeafe;
    color: #1d4ed8;
  }

  .chat-app.mobile-settings-open .mobile-avatar-toggle {
    border-color: #60a5fa;
    box-shadow: 0 0 0 3px #bfdbfe;
  }

  .chat-app.mobile-settings-open .topic-sidebar {
    transform: translateX(-108%);
  }

  .chat-form {
    grid-template-columns: 1fr auto;
    gap: 8px;
  }

  .upload-trigger {
    width: 40px;
    min-width: 40px;
    height: 40px;
  }

  #sendBtn {
    padding: 0 14px;
    min-height: 44px;
    min-width: 86px;
  }
}
