:root {
  color-scheme: light;
  --canvas: #f1f3f0;
  --surface: #ffffff;
  --surface-muted: #e6e9e5;
  --ink: #202621;
  --ink-soft: #626a63;
  --line: #cfd4ce;
  --green: #2f6a4f;
  --green-dark: #1f4c39;
  --amber: #cf8731;
  --teal: #347d82;
  --danger: #a4493f;
  --shadow: 0 16px 44px rgba(37, 43, 37, 0.11);
  font-family: "Noto Sans SC", "Microsoft YaHei UI", "Microsoft YaHei", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--canvas);
  color: var(--ink);
}

button,
textarea,
input,
select {
  font: inherit;
}

button {
  color: inherit;
}

button:focus-visible,
textarea:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 3px solid rgba(52, 125, 130, 0.25);
  outline-offset: 2px;
}

.app-shell {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  height: 100dvh;
}

.sidebar {
  position: relative;
  z-index: 30;
  display: flex;
  min-width: 0;
  flex-direction: column;
  border-right: 1px solid var(--line);
  background: #e5e8e4;
  padding: 18px 14px 14px;
}

.brand-row {
  display: grid;
  grid-template-columns: 38px 1fr 36px;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 0 4px 14px;
}

.brand-mark {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border: 1px solid var(--ink);
  background: var(--amber);
}

.brand-mark svg {
  width: 22px;
  height: 22px;
  stroke-width: 1.8;
}

.brand-row strong,
.brand-row span {
  display: block;
  letter-spacing: 0;
}

.brand-row strong {
  font-family: Georgia, "Noto Serif SC", serif;
  font-size: 20px;
  line-height: 1.1;
}

.brand-row span {
  margin-top: 3px;
  color: var(--ink-soft);
  font-size: 11px;
}

.icon-button {
  display: inline-grid;
  width: 36px;
  height: 36px;
  place-items: center;
  border: 0;
  background: transparent;
  cursor: pointer;
}

.icon-button:hover {
  background: rgba(32, 38, 33, 0.07);
}

.icon-button svg {
  width: 19px;
  height: 19px;
  stroke-width: 1.8;
}

.sidebar-close {
  visibility: hidden;
}

.new-chat-button,
.sidebar-action {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  background: var(--surface);
  padding: 0 12px;
  cursor: pointer;
  text-align: left;
}

.new-chat-button:hover,
.sidebar-action:hover {
  border-color: #aaa99f;
}

.new-chat-button svg,
.sidebar-action svg {
  width: 18px;
  height: 18px;
}

.history-label {
  margin: 24px 8px 8px;
  color: var(--ink-soft);
  font-size: 12px;
}

.conversation-list {
  min-height: 0;
  flex: 1;
  overflow-y: auto;
}

.conversation-item {
  display: grid;
  grid-template-columns: 1fr 32px;
  align-items: center;
  width: 100%;
  min-height: 42px;
  margin-bottom: 3px;
  border: 0;
  background: transparent;
}

.conversation-open {
  min-width: 0;
  height: 100%;
  overflow: hidden;
  border: 0;
  background: transparent;
  padding: 0 10px;
  cursor: pointer;
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-item.active {
  background: var(--surface);
  box-shadow: inset 3px 0 0 var(--green);
}

.conversation-delete {
  opacity: 0;
}

.conversation-item:hover .conversation-delete,
.conversation-delete:focus-visible {
  opacity: 1;
}

.sidebar-footer {
  display: grid;
  gap: 6px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.sidebar-action {
  border-color: transparent;
  background: transparent;
}

.status-dot {
  width: 8px;
  height: 8px;
  margin-left: auto;
  border-radius: 50%;
  background: #999b94;
}

.status-dot.ready {
  background: #258054;
  box-shadow: 0 0 0 3px rgba(37, 128, 84, 0.12);
}

.workspace {
  position: relative;
  display: grid;
  min-width: 0;
  height: 100dvh;
  grid-template-rows: 58px 50px minmax(0, 1fr) auto;
  background: var(--canvas);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  background: var(--canvas);
  padding: 0 22px;
}

.menu-button,
.mobile-settings {
  display: none;
}

.course-context {
  min-width: 0;
}

.course-context span,
.course-context strong {
  display: block;
  letter-spacing: 0;
}

.course-context span {
  color: var(--ink-soft);
  font-size: 10px;
}

.course-context strong {
  overflow: hidden;
  font-size: 14px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
}

.model-switch {
  display: inline-flex;
  max-width: 220px;
  height: 36px;
  align-items: center;
  gap: 7px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: var(--surface);
  padding: 0 10px;
  cursor: pointer;
}

.model-switch:hover { border-color: var(--green); }
.model-switch span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-switch svg { width: 15px; height: 15px; flex: 0 0 auto; }

.mode-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  padding: 6px 16px;
}

.mode-button {
  display: flex;
  height: 36px;
  align-items: center;
  gap: 7px;
  border: 0;
  background: transparent;
  padding: 0 13px;
  color: var(--ink-soft);
  cursor: pointer;
}

.mode-button:hover {
  background: var(--surface-muted);
}

.mode-button.active {
  background: var(--ink);
  color: #fff;
}

.mode-button svg {
  width: 16px;
  height: 16px;
}

.messages {
  min-height: 0;
  overflow-y: auto;
  padding: 30px max(28px, calc((100% - 860px) / 2)) 180px;
  scroll-behavior: smooth;
}

.message-row {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  gap: 13px;
  margin-bottom: 28px;
}

.message-row.user {
  grid-template-columns: minmax(0, 1fr);
  justify-items: end;
}

.message-avatar {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border: 1px solid var(--line);
  background: var(--surface);
}

.message-avatar svg {
  width: 18px;
  height: 18px;
}

.message-content {
  min-width: 0;
  line-height: 1.75;
}

.message-row.user .message-content {
  max-width: min(680px, 88%);
  border: 1px solid #c9d9cd;
  background: #e4eee6;
  padding: 12px 15px;
}

.message-content > :first-child {
  margin-top: 0;
}

.message-content > :last-child {
  margin-bottom: 0;
}

.message-content p,
.message-content ul,
.message-content ol {
  margin: 0 0 13px;
}

.message-content ul,
.message-content ol {
  padding-left: 23px;
}

.message-content h1,
.message-content h2,
.message-content h3 {
  margin: 24px 0 10px;
  letter-spacing: 0;
  line-height: 1.35;
}

.message-content h1 {
  font-size: 21px;
}

.message-content h2 {
  font-size: 18px;
}

.message-content h3 {
  font-size: 16px;
}

.message-content code {
  border: 1px solid #d7d4ca;
  background: #ebe8df;
  padding: 1px 5px;
  font-family: Consolas, "Cascadia Code", monospace;
  font-size: 0.92em;
}

.code-shell {
  position: relative;
  margin: 14px 0;
  border: 1px solid #333b35;
  background: #202521;
}

.code-shell pre {
  margin: 0;
  overflow-x: auto;
  padding: 38px 16px 16px;
  color: #e9eee8;
  line-height: 1.55;
}

.code-shell code {
  border: 0;
  background: transparent;
  padding: 0;
  color: inherit;
}

.copy-code {
  position: absolute;
  top: 5px;
  right: 6px;
  display: inline-flex;
  height: 28px;
  align-items: center;
  gap: 6px;
  border: 0;
  background: #363d37;
  padding: 0 9px;
  color: #dfe5df;
  cursor: pointer;
  font-size: 12px;
}

.copy-code svg {
  width: 14px;
  height: 14px;
}

.typing-indicator {
  display: inline-flex;
  gap: 4px;
  padding: 9px 0;
}

.typing-indicator span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--green);
  animation: pulse 1.1s infinite ease-in-out;
}

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

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

@keyframes pulse {
  0%, 70%, 100% { opacity: 0.25; transform: translateY(0); }
  35% { opacity: 1; transform: translateY(-3px); }
}

.empty-state {
  position: absolute;
  top: 50%;
  left: 50%;
  width: min(640px, calc(100% - 40px));
  transform: translate(-50%, -47%);
  text-align: center;
}

.empty-state[hidden] {
  display: none;
}

.empty-visual {
  position: relative;
  display: grid;
  width: 92px;
  height: 92px;
  margin: 0 auto 18px;
  place-items: center;
  border: 1px solid var(--ink);
  background: var(--surface);
  box-shadow: 8px 8px 0 var(--amber);
}

.empty-visual svg {
  width: 46px;
  height: 46px;
  color: var(--green-dark);
  stroke-width: 1.35;
}

.trace {
  position: absolute;
  height: 1px;
  background: var(--teal);
}

.trace::after {
  position: absolute;
  top: -3px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--teal);
  content: "";
}

.trace-a { top: 20px; right: -35px; width: 42px; }
.trace-a::after { right: 0; }
.trace-b { bottom: 18px; left: -40px; width: 48px; }
.trace-b::after { left: 0; }
.trace-c { bottom: -24px; left: 45px; width: 1px; height: 30px; }
.trace-c::after { bottom: 0; left: -3px; top: auto; }

.empty-state h1 {
  margin: 0;
  font-family: Georgia, "Noto Serif SC", serif;
  font-size: clamp(25px, 4vw, 34px);
  font-weight: 600;
  letter-spacing: 0;
}

.empty-state p {
  margin: 10px 0 22px;
  color: var(--ink-soft);
}

.starter-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.starter-grid button {
  min-height: 42px;
  border: 1px solid var(--line);
  background: var(--surface);
  padding: 7px 12px;
  cursor: pointer;
}

.starter-grid button:hover {
  border-color: var(--green);
}

.composer-wrap {
  position: absolute;
  z-index: 10;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 20px max(24px, calc((100% - 860px) / 2)) max(12px, env(safe-area-inset-bottom));
  background: var(--canvas);
  pointer-events: none;
}

.composer {
  border: 1px solid #aeb2aa;
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 12px 12px 9px;
  pointer-events: auto;
}

.composer:focus-within {
  border-color: var(--green);
  box-shadow: 0 16px 44px rgba(37, 43, 37, 0.13), 0 0 0 3px rgba(47, 106, 79, 0.08);
}

.composer textarea {
  display: block;
  width: 100%;
  max-height: 180px;
  resize: none;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
  font-size: 16px;
  line-height: 1.55;
}

.composer textarea::placeholder {
  color: #8b918b;
}

.composer-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 8px;
}

.thinking-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--ink-soft);
  cursor: pointer;
  font-size: 12px;
}

.thinking-control input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.toggle-track {
  position: relative;
  width: 31px;
  height: 18px;
  border: 1px solid #aeb2aa;
  border-radius: 12px;
  background: #deddd7;
  transition: background 150ms ease;
}

.toggle-track span {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
  transition: transform 150ms ease;
}

.thinking-control input:checked + .toggle-track {
  background: var(--teal);
}

.thinking-control input:checked + .toggle-track span {
  transform: translateX(13px);
}

.send-button {
  display: grid;
  width: 36px;
  height: 36px;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: var(--green);
  color: #fff;
  cursor: pointer;
}

.send-button:hover {
  background: var(--green-dark);
}

.send-button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.send-button svg {
  width: 19px;
  height: 19px;
}

.composer-note {
  margin: 7px 0 0;
  color: var(--ink-soft);
  font-size: 10px;
  text-align: center;
  pointer-events: auto;
}

.settings-dialog {
  width: min(460px, calc(100% - 28px));
  border: 0;
  background: transparent;
  padding: 0;
}

.settings-dialog::backdrop {
  background: rgba(30, 34, 30, 0.45);
  backdrop-filter: blur(3px);
}

.dialog-shell {
  max-height: min(760px, calc(100dvh - 30px));
  overflow-y: auto;
  border: 1px solid var(--ink);
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 22px;
}

.dialog-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 25px;
}

.dialog-heading span {
  color: var(--ink-soft);
  font-size: 11px;
}

.dialog-heading h2 {
  margin: 3px 0 0;
  font-family: Georgia, serif;
  font-size: 25px;
  letter-spacing: 0;
}

.field-label {
  display: block;
  margin: 15px 0 7px;
  font-size: 13px;
  font-weight: 700;
}

.secret-field {
  display: grid;
  grid-template-columns: 1fr 42px;
  border: 1px solid var(--line);
  background: #fff;
}

.secret-field input,
.settings-dialog select,
.settings-dialog input:not(#apiKey) {
  width: 100%;
  height: 44px;
  border: 1px solid var(--line);
  background: #fff;
  padding: 0 12px;
  color: var(--ink);
}

.settings-dialog input[readonly] {
  background: var(--surface-muted);
  color: var(--ink-soft);
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.secret-field input {
  border: 0;
  outline: 0;
}

.field-hint {
  margin: 6px 0 0;
  color: var(--ink-soft);
  font-size: 11px;
}

.dialog-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 26px;
}

.primary-button {
  min-height: 40px;
  border: 0;
  background: var(--green);
  padding: 0 18px;
  color: #fff;
  cursor: pointer;
}

.danger-text {
  margin-right: auto;
  border: 0;
  background: transparent;
  color: var(--danger);
  cursor: pointer;
}

.dialog-error {
  min-height: 18px;
  margin: 10px 0 0;
  color: var(--danger);
  font-size: 12px;
}

.toast {
  position: fixed;
  z-index: 100;
  right: 20px;
  bottom: 20px;
  max-width: min(360px, calc(100% - 40px));
  border: 1px solid var(--ink);
  background: var(--ink);
  padding: 10px 14px;
  color: #fff;
  box-shadow: var(--shadow);
  font-size: 13px;
}

.scrim {
  position: fixed;
  z-index: 20;
  inset: 0;
  background: rgba(20, 24, 21, 0.42);
}

@media (max-width: 760px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(310px, 88vw);
    transform: translateX(-103%);
    box-shadow: var(--shadow);
    transition: transform 190ms ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-close {
    visibility: visible;
  }

  .workspace {
    grid-template-rows: 56px 48px minmax(0, 1fr) auto;
  }

  .topbar {
    gap: 6px;
    padding: 0 10px;
  }

  .course-context {
    min-width: 0;
    flex: 1 1 auto;
  }

  .course-context strong {
    font-size: 12px;
  }

  .menu-button,
  .mobile-settings {
    display: inline-grid;
  }

  .topbar-actions {
    min-width: 0;
    flex: 0 1 auto;
    gap: 2px;
  }

  .model-switch { min-width: 0; max-width: 112px; padding: 0 7px; }

  .mode-bar {
    justify-content: flex-start;
    overflow-x: auto;
    padding: 6px 10px;
  }

  .mode-button {
    flex: 0 0 auto;
    padding: 0 11px;
    font-size: 13px;
  }

  .messages {
    padding: 22px 15px 178px;
  }

  .message-row {
    grid-template-columns: 30px minmax(0, 1fr);
    gap: 9px;
  }

  .message-avatar {
    width: 30px;
    height: 30px;
  }

  .message-row.user .message-content {
    max-width: 92%;
  }

  .empty-state {
    top: 46%;
  }

  .empty-state h1 {
    font-size: 25px;
  }

  .starter-grid {
    grid-template-columns: 1fr;
    width: min(330px, 100%);
    margin: 0 auto;
  }

  .composer-wrap {
    padding: 18px 10px max(9px, env(safe-area-inset-bottom));
  }

  .composer textarea {
    font-size: 16px;
  }

  .toast {
    right: 12px;
    bottom: max(12px, env(safe-area-inset-bottom));
  }

  .form-grid { grid-template-columns: 1fr; gap: 0; }
}

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