/* MathGPT UI — aligned with math-gpt.org */

:root {
  --mg-blue: #3490dc;
  --mg-blue-dark: #2b7bc0;
  --mg-blue-soft: rgba(52, 144, 220, 0.12);
  --mg-bg: #ffffff;
  --mg-sidebar-bg: #f9fafb;
  --mg-border: #e5e7eb;
  --mg-text: #111827;
  --mg-muted: #6b7280;
  --mg-surface: #ffffff;
  --mg-surface-muted: #fafafa;
  --mg-surface-subtle: #f3f4f6;
  --mg-hover: #eef2f7;
  --mg-hover-strong: #e8eef5;
  --mg-input-bg: #ffffff;
  --mg-placeholder: #9ca3af;
  --mg-shadow: rgba(15, 23, 42, 0.06);
  --mg-shadow-strong: rgba(15, 23, 42, 0.12);
  --mg-composer-fade: #ffffff;
  --mg-on-primary: #ffffff;
  --mg-sidebar-width: 272px;
  --mg-sidebar-panel-width: 272px;
  --mg-sidebar-rail-width: 60px;
  --mg-font: "Inter", "PingFang SC", "Microsoft YaHei", "Noto Sans SC", ui-sans-serif, system-ui, sans-serif;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --mg-blue: #5eb3f6;
  --mg-blue-dark: #3490dc;
  --mg-blue-soft: rgba(94, 179, 246, 0.16);
  --mg-bg: #0f1419;
  --mg-sidebar-bg: #151b23;
  --mg-border: #2a3441;
  --mg-text: #e7ecf3;
  --mg-muted: #9aa8b8;
  --mg-surface: #1a222d;
  --mg-surface-muted: #151b23;
  --mg-surface-subtle: #232d3a;
  --mg-hover: #232d3a;
  --mg-hover-strong: #2a3441;
  --mg-input-bg: #151b23;
  --mg-placeholder: #6b7785;
  --mg-shadow: rgba(0, 0, 0, 0.28);
  --mg-shadow-strong: rgba(0, 0, 0, 0.42);
  --mg-composer-fade: #0f1419;
}

html[data-theme="light"] {
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

html,
body.mg-body {
  margin: 0;
  min-height: 100%;
  background: var(--mg-bg);
  color: var(--mg-text);
  font-family: var(--mg-font);
  font-size: 16px;
  line-height: 1.5;
}

.mg-body a {
  color: var(--mg-blue);
  text-decoration: none;
}

.mg-body a:hover {
  text-decoration: underline;
}

/* 避免链接全局蓝色覆盖按钮文字 */
.mg-body a.mg-btn-primary,
.mg-btn-primary {
  color: #ffffff !important;
}

.mg-body a.mg-btn-primary:hover {
  color: #ffffff !important;
  text-decoration: none;
}

.mg-body a.mg-btn-outline,
.mg-btn-outline {
  color: var(--mg-blue) !important;
}

.mg-body a.mg-btn-outline:hover,
.mg-btn-outline:hover {
  color: var(--mg-blue-dark) !important;
  text-decoration: none;
}

.mg-body a.mg-nav-item,
.mg-body a.mg-recent-item,
.mg-body a.mg-tool-btn,
.mg-body a.mg-sidebar-link {
  color: var(--mg-text);
  text-decoration: none;
}

.mg-body a.mg-nav-item:hover,
.mg-body a.mg-recent-item:hover {
  text-decoration: none;
}

.mg-body a.mg-tool-btn {
  color: var(--mg-muted);
}

.mg-body a.mg-tool-btn:hover {
  color: var(--mg-text);
  text-decoration: none;
}

.mg-body a.mg-sidebar-link {
  color: var(--mg-muted);
}

.mg-foot-links a {
  color: var(--mg-muted);
}

.mg-foot-links a:hover {
  color: var(--mg-blue);
  text-decoration: none;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* ── App shell ── */
.mg-app {
  min-height: 100vh;
  margin-left: var(--mg-sidebar-width);
  display: flex;
  flex-direction: column;
  background: var(--mg-bg);
}

.mg-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 56px);
}

.mg-main-home {
  align-items: center;
}

.mg-main-thread .mg-hero {
  display: none;
}

.mg-main-thread .mg-seo {
  display: none;
}

/* ── Sidebar ── */
.mg-sidebar {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: var(--mg-sidebar-panel-width);
  background: var(--mg-sidebar-bg);
  border-right: 1px solid var(--mg-border);
  display: flex;
  flex-direction: column;
  padding: 12px;
  z-index: 110;
}

.mg-sidebar-top {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.mg-sidebar-close {
  display: inline-flex;
}

.mg-sidebar-open {
  display: inline-flex;
}

.mg-app.is-sidebar-collapsed {
  margin-left: var(--mg-sidebar-rail-width);
}

@media (min-width: 961px) {
  .mg-sidebar {
    transition: width 0.25s ease, padding 0.25s ease;
  }

  .mg-sidebar.is-collapsed {
    width: var(--mg-sidebar-rail-width);
    padding: 10px 6px;
    overflow: hidden;
  }

  .mg-sidebar.is-collapsed .mg-nav-item > span:not(.mg-nav-icon),
  .mg-sidebar.is-collapsed .mg-sidebar-label,
  .mg-sidebar.is-collapsed .mg-sidebar-section,
  .mg-sidebar.is-collapsed .mg-sidebar-promo,
  .mg-sidebar.is-collapsed .mg-sidebar-foot {
    display: none;
  }

  .mg-sidebar.is-collapsed .mg-sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-bottom: 0;
  }

  .mg-sidebar.is-collapsed .mg-nav-item {
    justify-content: center;
    width: 100%;
    padding: 8px 0;
    gap: 0;
  }

  .mg-sidebar.is-collapsed .mg-nav-icon {
    margin: 0 auto;
  }

  .mg-sidebar.is-collapsed .mg-sidebar-top {
    flex-direction: column;
    align-items: center;
    gap: 6px;
    margin-bottom: 10px;
  }

  .mg-sidebar.is-collapsed .mg-sidebar-close svg {
    transform: rotate(180deg);
  }

  .mg-sidebar-close svg {
    transition: transform 0.2s ease;
  }

  .mg-topbar .mg-sidebar-open {
    display: none;
  }

  .mg-sidebar-backdrop {
    display: none !important;
  }
}

.mg-sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: 16px;
}

.mg-nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--mg-text);
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
  transition: background 0.15s ease;
}

.mg-nav-item:hover {
  background: var(--mg-hover);
  text-decoration: none;
}

.mg-nav-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.mg-nav-icon svg {
  display: block;
}

.mg-sidebar-section {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.mg-sidebar-label {
  margin: 0 0 8px;
  padding: 0 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--mg-muted);
}

.mg-recent-list {
  overflow: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.mg-recent-empty {
  margin: 0;
  padding: 8px;
  font-size: 13px;
  color: var(--mg-muted);
}

.mg-recent-item {
  display: block;
  padding: 8px 10px;
  border-radius: 8px;
  color: var(--mg-text);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-decoration: none;
}

.mg-recent-item:hover,
.mg-recent-item.active {
  background: var(--mg-hover-strong);
  text-decoration: none;
}

.mg-sidebar-promo {
  margin-top: 12px;
  padding: 14px;
  border: 1px solid var(--mg-border);
  border-radius: 12px;
  background: var(--mg-surface);
}

.mg-sidebar-promo h4 {
  margin: 0 0 6px;
  font-size: 14px;
}

.mg-sidebar-promo p {
  margin: 0 0 12px;
  font-size: 12px;
  color: var(--mg-muted);
  line-height: 1.45;
}

.mg-sidebar-foot {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--mg-border);
}

.mg-sidebar-link {
  font-size: 13px;
  color: var(--mg-muted);
}

.mg-sidebar-social {
  display: flex;
  gap: 10px;
  margin-top: 8px;
}

.mg-sidebar-social a {
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  background: var(--mg-hover);
  color: var(--mg-muted);
  font-size: 11px;
  font-weight: 700;
  text-decoration: none;
}

.mg-sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 100;
}

.mg-sidebar-backdrop[hidden] {
  display: none !important;
}

/* ── Top bar ── */
.mg-topbar {
  height: 56px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  border-bottom: 1px solid var(--mg-border);
  background: var(--mg-bg);
  position: sticky;
  top: 0;
  z-index: 20;
}

.mg-topbar-spacer {
  flex: 1;
}

.mg-topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.mg-topbar-credits {
  font-size: 13px;
  color: var(--mg-muted);
}

/* ── Buttons ── */
.mg-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px 16px;
  border-radius: 999px;
  border: 1px solid transparent;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.mg-btn-primary {
  background: var(--mg-blue);
  color: #ffffff !important;
  border-color: var(--mg-blue);
}

.mg-btn-primary:hover {
  background: var(--mg-blue-dark);
  border-color: var(--mg-blue-dark);
  text-decoration: none;
  color: #ffffff !important;
}

.mg-btn-outline {
  background: var(--mg-surface);
  color: var(--mg-blue) !important;
  border-color: var(--mg-blue);
}

.mg-btn-outline:hover {
  background: var(--mg-blue-soft);
  color: var(--mg-blue-dark) !important;
  text-decoration: none;
}

.mg-btn-sm {
  padding: 7px 14px;
  font-size: 13px;
}

.mg-btn-block {
  width: 100%;
}

.mg-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--mg-text);
  cursor: pointer;
}

.mg-icon-btn:hover {
  background: var(--mg-hover);
}

/* ── Hero / empty state ── */
.mg-hero {
  width: min(100%, 760px);
  padding: 48px 20px 20px;
  text-align: center;
}

.mg-hero-brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
}

.mg-logo-icon {
  width: 56px;
  height: 56px;
}

.mg-hero h1 {
  margin: 0;
  font-size: clamp(24px, 4vw, 34px);
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--mg-text);
}

.mg-hero-sub {
  margin: 0;
  font-size: 16px;
  color: var(--mg-muted);
  max-width: 520px;
}

.mg-subject-tabs {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.mg-subject-tab {
  border: 1px solid var(--mg-border);
  background: var(--mg-surface);
  color: var(--mg-text);
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.mg-subject-tab:hover:not(:disabled) {
  border-color: #cbd5e1;
}

.mg-subject-tab.active {
  background: rgba(52, 144, 220, 0.9);
  border-color: rgba(52, 144, 220, 0.9);
  color: #f9fafb;
}

.mg-subject-tab:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* ── Messages ── */
.mg-thread {
  flex: 1;
  width: min(100%, 820px);
  margin: 0 auto;
  padding: 24px 20px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.mg-main-home .mg-thread {
  flex: 0;
  padding: 0;
  overflow: visible;
}

.mg-msg {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.mg-msg-user {
  flex-direction: row-reverse;
}

.mg-msg-avatar .mg-logo-icon {
  width: 32px;
  height: 32px;
}

.mg-msg-body {
  max-width: min(100%, 680px);
}

.mg-msg-content,
.chat-message-content {
  padding: 14px 16px;
  border-radius: 16px;
  line-height: 1.65;
  font-size: 15px;
  white-space: pre-wrap;
  word-break: break-word;
}

.mg-msg-assistant .mg-msg-content {
  background: var(--mg-surface-subtle);
  border: 1px solid var(--mg-border);
}

.mg-msg-user .mg-msg-content {
  background: var(--mg-blue);
  color: #fff;
}

.mg-msg-asset-link {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.mg-msg-streaming .mg-msg-content::after {
  content: "▍";
  animation: mg-blink 1s step-end infinite;
}

@keyframes mg-blink {
  50% { opacity: 0; }
}

.chat-message-attachments,
.mg-msg-body .chat-message-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.chat-message-image,
.mg-upload-preview img {
  max-width: 180px;
  max-height: 140px;
  border-radius: 10px;
  object-fit: cover;
  border: 1px solid var(--mg-border);
}

/* ── Composer ── */
.mg-composer-wrap {
  width: min(100%, 760px);
  margin: 0 auto;
  padding: 0 20px 24px;
  position: relative;
}

.mg-main-thread .mg-composer-wrap {
  position: sticky;
  bottom: 0;
  background: linear-gradient(180deg, transparent, var(--mg-composer-fade) 24%);
  padding-top: 12px;
}

.mg-composer-card {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--mg-border);
  border-radius: 20px;
  background: var(--mg-surface);
  box-shadow: 0 8px 30px var(--mg-shadow);
  overflow: hidden;
}

.mg-composer-card textarea {
  width: 100%;
  min-height: 48px;
  max-height: 200px;
  border: none;
  outline: none;
  resize: none;
  padding: 16px 20px 8px;
  font: inherit;
  font-size: 16px;
  line-height: 1.5;
  background: transparent;
  color: var(--mg-text);
  flex: 1 1 auto;
  border-radius: 20px 20px 0 0;
}

.mg-composer-card textarea::placeholder {
  color: var(--mg-placeholder);
}

.mg-upload-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  flex-shrink: 0;
  padding: 0 16px 4px;
}

.mg-upload-preview[hidden] {
  display: none !important;
}

.mg-composer-toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 12px 10px;
  flex-shrink: 0;
  margin-top: auto;
  flex-wrap: wrap;
  overflow: visible;
}

.mg-tool-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  padding: 0 10px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--mg-muted);
  font-size: 13px;
  cursor: pointer;
}

.mg-tool-btn:hover {
  background: var(--mg-surface-subtle);
  color: var(--mg-text);
}

.mg-tool-text {
  font-weight: 500;
}

.mg-tool-select {
  position: relative;
}

.mg-tool-select-btn span {
  max-width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mg-tool-menu {
  position: absolute;
  left: 0;
  bottom: calc(100% + 8px);
  min-width: 160px;
  background: var(--mg-surface);
  border: 1px solid var(--mg-border);
  border-radius: 12px;
  box-shadow: 0 12px 32px var(--mg-shadow-strong);
  padding: 6px;
  z-index: 30;
}

.mg-tool-option {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
}

.mg-tool-option:hover,
.mg-tool-option.active {
  background: var(--mg-blue-soft);
  color: var(--mg-blue-dark);
}

.mg-send-btn {
  margin-left: auto;
  width: 36px;
  height: 36px;
  border: 1px solid var(--mg-blue);
  border-radius: 999px;
  background: var(--mg-blue);
  color: #f8fafc;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s ease;
}

.mg-send-btn:hover:not(:disabled) {
  background: var(--mg-blue-dark);
  border-color: var(--mg-blue-dark);
}

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

/* ── Math keyboard ── */
.mg-math-panel[hidden],
.mg-calc-popup[hidden] {
  display: none !important;
}

.mg-math-panel {
  display: block;
  margin-top: 10px;
  padding: 12px;
  border: 1px solid var(--mg-border);
  border-radius: 12px;
  background: var(--mg-surface-muted);
}

.mg-math-field {
  display: block;
  width: 100%;
  min-height: 56px;
  border: 1px solid var(--mg-border);
  border-radius: 10px;
  padding: 8px;
  background: var(--mg-surface);
}

.mg-math-panel-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

/* ── Calculator popup ── */
.mg-calc-popup {
  position: fixed;
  z-index: 120;
  width: 280px;
  max-width: calc(100vw - 24px);
  padding: 12px;
  border: 1px solid var(--mg-border);
  border-radius: 16px;
  background: var(--mg-surface);
  box-shadow: 0 16px 40px var(--mg-shadow-strong);
}

.mg-calc-popup.mg-calc-dragging {
  user-select: none;
  box-shadow: 0 20px 48px rgba(15, 23, 42, 0.2);
}

.mg-calc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: grab;
  touch-action: none;
}

.mg-calc-header:active {
  cursor: grabbing;
}

.mg-calc-title {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.mg-calc-drag-icon {
  color: var(--mg-placeholder);
  flex-shrink: 0;
}

.mg-calc-close {
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 8px;
  background: var(--mg-surface-subtle);
  color: var(--mg-muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.mg-calc-close:hover {
  background: var(--mg-border);
  color: var(--mg-text);
}

.mg-calc-display {
  padding: 12px 14px;
  margin-bottom: 10px;
  border: 1px solid var(--mg-border);
  border-radius: 10px;
  background: var(--mg-surface-muted);
  font-size: 22px;
  font-weight: 600;
  text-align: right;
  word-break: break-all;
  min-height: 48px;
  line-height: 1.3;
}

.mg-calc-keys {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.mg-calc-keys button {
  height: 40px;
  border: 1px solid var(--mg-border);
  border-radius: 10px;
  background: var(--mg-surface);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s ease;
}

.mg-calc-keys button:hover {
  background: var(--mg-surface-subtle);
}

.mg-calc-keys button[data-calc-key="="] {
  background: var(--mg-blue);
  border-color: var(--mg-blue);
  color: #fff;
}

.mg-calc-keys button[data-calc-key="="]:hover {
  background: var(--mg-blue-dark);
}

.mg-calc-foot {
  margin-top: 10px;
}

/* ── Alerts ── */
.mg-alert {
  width: min(100%, 760px);
  margin: 12px auto 0;
  padding: 10px 14px;
  border-radius: 10px;
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  font-size: 14px;
}

.mg-alert a {
  color: var(--mg-blue-dark);
  font-weight: 500;
}

.mg-alert-warn {
  background: #fff7ed;
  border-color: #fed7aa;
}

/* ── SEO sections ── */
.mg-seo {
  width: min(100%, 920px);
  padding: 48px 20px 80px;
  color: var(--mg-text);
}

.mg-seo h2 {
  font-size: 28px;
  margin: 0 0 12px;
  text-align: center;
}

.mg-seo > p {
  text-align: center;
  color: var(--mg-muted);
  max-width: 720px;
  margin: 0 auto 32px;
}

.mg-seo-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 20px;
  margin-bottom: 40px;
}

.mg-seo-grid article {
  padding: 20px;
  border: 1px solid var(--mg-border);
  border-radius: 16px;
  background: var(--mg-surface-muted);
}

.mg-seo-grid h3 {
  margin: 0 0 8px;
  font-size: 18px;
}

.mg-seo-grid p {
  margin: 0;
  color: var(--mg-muted);
  font-size: 14px;
  line-height: 1.6;
}

.mg-seo details {
  border: 1px solid var(--mg-border);
  border-radius: 12px;
  padding: 14px 16px;
  margin-bottom: 10px;
  background: var(--mg-surface);
}

.mg-seo summary {
  cursor: pointer;
  font-weight: 600;
}

.mg-seo details p {
  margin: 10px 0 0;
  color: var(--mg-muted);
  font-size: 14px;
}

/* ── Footer ── */
.mg-site-foot {
  border-top: 1px solid var(--mg-border);
  padding: 20px;
  text-align: center;
  color: var(--mg-muted);
  font-size: 13px;
}

.mg-disclaimer {
  margin: 0 0 10px;
}

.mg-foot-links {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-bottom: 8px;
}

.mg-foot-links a {
  color: var(--mg-muted);
}

.mg-copy {
  margin: 0;
}

/* ── Mobile ── */
@media (max-width: 960px) {
  .mg-app {
    margin-left: 0;
  }

  .mg-sidebar {
    transform: translate3d(-100%, 0, 0);
    transition: transform 0.25s ease;
    box-shadow: none;
    pointer-events: none;
  }

  .mg-sidebar.is-open {
    transform: translate3d(0, 0, 0);
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.18);
    pointer-events: auto;
  }

  .mg-sidebar.is-collapsed {
    transform: translate3d(-100%, 0, 0);
    pointer-events: none;
  }

  .mg-topbar {
    z-index: 120;
  }

  body.mg-sidebar-drawer-open {
    overflow: hidden;
  }

  .mg-tool-text {
    display: none;
  }

  .mg-seo-grid {
    grid-template-columns: 1fr;
  }
}

/* Toast (from app.js JptLayer) */
.jpt-layer-root {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9999;
}

.jpt-toast-stack {
  position: fixed;
  top: 16px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: auto;
}

.jpt-toast {
  min-width: 220px;
  max-width: 360px;
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--mg-surface);
  border: 1px solid var(--mg-border);
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.12);
  font-size: 14px;
  cursor: pointer;
}

.jpt-toast.error {
  border-color: #fecaca;
  background: #fef2f2;
}

.jpt-toast.info {
  border-color: #bfdbfe;
  background: #eff6ff;
}

.server-flash.hidden {
  display: none;
}

/* ── Markdown 消息 ── */
.mg-msg-content.is-markdown {
  white-space: normal;
}

.mg-msg-content.is-markdown h1,
.mg-msg-content.is-markdown h2,
.mg-msg-content.is-markdown h3 {
  margin: 0.8em 0 0.4em;
  line-height: 1.35;
}

.mg-msg-content.is-markdown p,
.mg-msg-content.is-markdown ul,
.mg-msg-content.is-markdown ol {
  margin: 0.5em 0;
}

.mg-msg-content.is-markdown ul,
.mg-msg-content.is-markdown ol {
  padding-left: 1.4em;
}

.mg-msg-content.is-markdown code {
  padding: 0.1em 0.35em;
  border-radius: 4px;
  background: rgba(15, 23, 42, 0.06);
  font-size: 0.92em;
}

.mg-msg-content.is-markdown pre {
  margin: 0.8em 0;
  padding: 12px;
  border-radius: 10px;
  background: #1f2937;
  color: #f9fafb;
  overflow: auto;
}

.mg-msg-content.is-markdown pre code {
  background: transparent;
  padding: 0;
  color: inherit;
}

.chat-message-file {
  display: inline-flex;
  align-items: center;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--mg-hover);
  font-size: 13px;
}

/* ── 函数图像 ── */
.mg-graph-wrap {
  margin-top: 12px;
  padding: 12px;
  border: 1px solid var(--mg-border);
  border-radius: 12px;
  background: var(--mg-surface);
}

.mg-graph-title {
  margin: 0 0 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--mg-muted);
}

.mg-graph-plot {
  width: 100%;
  min-height: 280px;
  overflow: visible;
}

/* ── 公开内容页（计算器/文章） ── */
.mg-main-content {
  align-items: stretch;
}

.mg-content-page {
  width: min(100%, 920px);
  margin: 0 auto;
  padding: 32px 20px 48px;
}

.mg-content-head {
  margin-bottom: 24px;
}

.mg-content-kicker {
  margin: 0 0 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--mg-blue);
}

.mg-content-head h1 {
  margin: 0 0 10px;
  font-size: clamp(24px, 4vw, 34px);
}

.mg-content-desc {
  margin: 0;
  color: var(--mg-muted);
  line-height: 1.65;
}

.mg-filter-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 24px;
}

.mg-filter-tab {
  padding: 6px 12px;
  border: 1px solid var(--mg-border);
  border-radius: 999px;
  color: var(--mg-text);
  font-size: 14px;
  text-decoration: none;
}

.mg-filter-tab:hover,
.mg-filter-tab.active {
  background: var(--mg-blue-soft);
  border-color: var(--mg-blue);
  color: var(--mg-blue-dark);
  text-decoration: none;
}

.mg-card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 16px;
}

.mg-card {
  border: 1px solid var(--mg-border);
  border-radius: 16px;
  background: var(--mg-surface);
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
}

.mg-card:hover {
  border-color: #cbd5e1;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
}

.mg-card-link {
  display: block;
  padding: 18px;
  color: var(--mg-text);
  text-decoration: none;
}

.mg-card-link:hover {
  text-decoration: none;
}

.mg-card-tag {
  display: inline-block;
  margin-bottom: 8px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--mg-blue-soft);
  color: var(--mg-blue-dark);
  font-size: 12px;
}

.mg-card h2 {
  margin: 0 0 8px;
  font-size: 18px;
}

.mg-card p {
  margin: 0;
  color: var(--mg-muted);
  font-size: 14px;
  line-height: 1.55;
}

.mg-empty-state {
  padding: 48px 20px;
  text-align: center;
  color: var(--mg-muted);
  border: 1px dashed var(--mg-border);
  border-radius: 16px;
}

.mg-solve-card {
  margin-bottom: 32px;
  padding: 20px;
  border: 1px solid var(--mg-border);
  border-radius: 16px;
  background: var(--mg-surface-muted);
}

.mg-field {
  display: block;
  margin-bottom: 16px;
}

.mg-field span {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 500;
}

.mg-field textarea {
  width: 100%;
  min-height: 120px;
  padding: 12px 14px;
  border: 1px solid var(--mg-border);
  border-radius: 12px;
  font: inherit;
  resize: vertical;
}

.mg-solve-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.mg-related h2 {
  margin: 0 0 12px;
  font-size: 20px;
}

.mg-related-list {
  margin: 0;
  padding-left: 1.2em;
  color: var(--mg-muted);
}

.mg-related-list a {
  color: var(--mg-blue-dark);
}

/* ── 学习资源详情 ── */
.mg-video-sections { display: flex; flex-direction: column; gap: 12px; }
.mg-video-section { padding: 16px; border: 1px solid var(--mg-border); border-radius: 12px; background: var(--mg-surface-muted); }
.mg-video-time { display: inline-block; margin-bottom: 8px; padding: 2px 8px; border-radius: 999px; background: var(--mg-blue-soft); color: var(--mg-blue-dark); font-size: 12px; font-weight: 600; }
.mg-video-narration { margin: 0 0 8px; line-height: 1.65; }
.mg-video-screen { margin: 0; color: var(--mg-muted); font-size: 14px; }
.mg-quiz-list { display: flex; flex-direction: column; gap: 16px; }
.mg-quiz-item { padding: 16px; border: 1px solid var(--mg-border); border-radius: 12px; }
.mg-quiz-item h3 { margin: 0 0 8px; font-size: 16px; }
.mg-quiz-options { margin: 8px 0; padding-left: 1.2em; }
.mg-quiz-answer { margin: 8px 0 0; font-weight: 600; color: var(--mg-blue-dark); }
.mg-quiz-explain { margin: 6px 0 0; color: var(--mg-muted); font-size: 14px; }
.mg-flashcard-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.mg-flashcard { border: 1px solid var(--mg-border); border-radius: 12px; overflow: hidden; }
.mg-flashcard-front, .mg-flashcard-back { padding: 14px; }
.mg-flashcard-front { background: var(--mg-surface); font-weight: 600; }
.mg-flashcard-back { background: var(--mg-surface-subtle); color: var(--mg-muted); font-size: 14px; border-top: 1px solid var(--mg-border); }
.mg-asset-raw, .mg-asset-json { padding: 16px; border: 1px solid var(--mg-border); border-radius: 12px; background: var(--mg-surface-muted); overflow: auto; }

/* ── 文章页 ── */
.mg-breadcrumb { margin-bottom: 16px; color: var(--mg-muted); font-size: 14px; }
.mg-breadcrumb a { color: var(--mg-muted); }
.mg-article-cover { height: 220px; margin-bottom: 20px; border-radius: 16px; background-size: cover; background-position: center; }
.mg-article-summary { margin: 0 0 20px; padding: 14px 16px; border-left: 3px solid var(--mg-blue); background: var(--mg-blue-soft); border-radius: 0 10px 10px 0; }
.mg-article-body { line-height: 1.75; }
.mg-article-body h2, .mg-article-body h3 { margin: 1.2em 0 0.6em; }
.mg-article-body p, .mg-article-body ul, .mg-article-body ol { margin: 0.6em 0; }
.mg-article-body img { max-width: 100%; border-radius: 10px; }
.mg-article-grid time { display: block; margin-top: 8px; color: var(--mg-muted); font-size: 13px; }
.mg-pagination { display: flex; align-items: center; gap: 12px; margin-top: 24px; }


/* ── Auth pages ── */
.mg-main-auth { align-items: center; justify-content: center; }
.mg-auth-wrap { width: min(100%, 480px); margin: 0 auto; padding: 32px 20px 48px; }
.mg-auth-card { padding: 28px; border: 1px solid var(--mg-border); border-radius: 20px; background: var(--mg-surface); box-shadow: 0 8px 30px var(--mg-shadow); }
.mg-auth-brand { text-align: center; margin-bottom: 20px; }
.mg-auth-brand h1 { margin: 12px 0 8px; font-size: 24px; }
.mg-auth-brand p { margin: 0; color: var(--mg-muted); font-size: 14px; line-height: 1.6; }
.mg-auth-tabs { display: flex; gap: 8px; margin-bottom: 20px; }
.mg-auth-tabs a { flex: 1; text-align: center; padding: 10px; border-radius: 10px; border: 1px solid var(--mg-border); color: var(--mg-text); text-decoration: none; font-weight: 500; }
.mg-auth-tabs a.active, .mg-auth-tabs a:hover { background: var(--mg-blue-soft); border-color: var(--mg-blue); color: var(--mg-blue-dark); text-decoration: none; }
.mg-auth-form { display: flex; flex-direction: column; gap: 14px; }
.mg-auth-form input { width: 100%; padding: 12px 14px; border: 1px solid var(--mg-border); border-radius: 10px; font: inherit; }
.mg-field input, .mg-field textarea, .mg-field select { width: 100%; padding: 12px 14px; border: 1px solid var(--mg-border); border-radius: 10px; font: inherit; }

/* ── Account / credits ── */
.mg-content-head-row { display: flex; flex-wrap: wrap; align-items: flex-start; justify-content: space-between; gap: 16px; }
.mg-page-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.mg-account-grid { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(240px, 0.8fr); gap: 16px; }
.mg-panel { padding: 20px; border: 1px solid var(--mg-border); border-radius: 16px; background: var(--mg-surface); }
.mg-panel-muted { background: var(--mg-surface-muted); }
.mg-panel h2 { margin: 0 0 14px; font-size: 18px; }
.mg-tip-list { margin: 0; padding-left: 1.2em; color: var(--mg-muted); line-height: 1.7; }

.mg-balance-banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 28px;
  padding: 22px 24px;
  border: 1px solid var(--mg-border);
  border-radius: 16px;
  background: linear-gradient(135deg, var(--mg-blue-soft) 0%, var(--mg-surface) 55%);
}

.mg-balance-main { min-width: 140px; }
.mg-balance-label { display: block; margin-bottom: 4px; color: var(--mg-muted); font-size: 14px; }
.mg-balance-value { display: block; font-size: clamp(28px, 5vw, 36px); font-weight: 700; color: var(--mg-blue-dark); line-height: 1.1; }
.mg-balance-note { margin: 0; color: var(--mg-muted); font-size: 14px; }

.mg-trust-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 18px;
  margin: 0;
  padding: 0;
  list-style: none;
  color: var(--mg-muted);
  font-size: 13px;
}

.mg-trust-list li {
  position: relative;
  padding-left: 16px;
}

.mg-trust-list li::before {
  content: "✓";
  position: absolute;
  left: 0;
  color: var(--mg-blue);
  font-weight: 700;
}

.mg-section-head { margin-bottom: 16px; }
.mg-section-head h2 { margin: 0 0 6px; font-size: 20px; }
.mg-section-head p { margin: 0; color: var(--mg-muted); font-size: 14px; }

.mg-package-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

.mg-package-card {
  position: relative;
  display: flex;
  flex-direction: column;
  padding: 20px;
  border: 1px solid var(--mg-border);
  border-radius: 16px;
  background: var(--mg-surface);
  transition: box-shadow 0.15s ease, border-color 0.15s ease, transform 0.15s ease;
}

.mg-package-card:hover {
  border-color: #cbd5e1;
  box-shadow: 0 10px 28px rgba(15, 23, 42, 0.07);
}

.mg-package-card.is-featured {
  border-color: var(--mg-blue);
  box-shadow: 0 12px 32px rgba(37, 99, 235, 0.12);
}

.mg-package-badge {
  position: absolute;
  top: 14px;
  right: 14px;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--mg-blue);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
}

.mg-package-top h3 { margin: 0 0 10px; font-size: 18px; }
.mg-package-credits { display: flex; align-items: baseline; gap: 6px; margin-bottom: 12px; }
.mg-package-num { font-size: 34px; font-weight: 700; color: var(--mg-blue-dark); line-height: 1; }
.mg-package-unit { color: var(--mg-muted); font-size: 14px; }

.mg-package-desc {
  flex: 1;
  margin: 0 0 16px;
  padding-left: 1.1em;
  color: var(--mg-muted);
  font-size: 13px;
  line-height: 1.6;
}

.mg-package-foot { margin-top: auto; }
.mg-package-price-row { display: flex; align-items: baseline; gap: 8px; margin-bottom: 4px; }
.mg-package-price { font-size: 24px; font-weight: 700; color: var(--mg-text); }
.mg-package-original { color: var(--mg-muted); font-size: 14px; }
.mg-package-unit-price { display: block; margin-bottom: 14px; color: var(--mg-muted); font-size: 13px; }

.mg-empty-state h2 { margin: 0 0 8px; color: var(--mg-text); font-size: 18px; }
.mg-empty-state p { margin: 0 0 16px; line-height: 1.6; }
.mg-empty-state .mg-btn { margin: 0 4px; }

.mg-pay-card { max-width: 560px; margin: 0 auto; text-align: center; }
.mg-pay-summary {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 24px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--mg-border);
  text-align: left;
}

.mg-pay-label { display: block; margin-bottom: 4px; color: var(--mg-muted); font-size: 12px; }
.mg-pay-desc { margin: 6px 0 0; color: var(--mg-muted); font-size: 13px; line-height: 1.5; }
.mg-pay-amount { color: var(--mg-blue-dark); font-size: 20px; }
.mg-pay-qrcode-wrap { margin-bottom: 20px; }
.mg-pay-qrcode { display: block; width: 240px; height: 240px; margin: 0 auto 12px; border: 1px solid var(--mg-border); border-radius: 12px; background: var(--mg-surface); }
.mg-pay-tip { margin: 0 0 6px; color: var(--mg-muted); font-size: 14px; }
.mg-pay-countdown { margin: 0 0 8px; color: var(--mg-muted); font-size: 13px; }
.mg-pay-status { margin: 0; font-weight: 600; }
.mg-pay-status.is-success { color: #059669; }
.mg-pay-status.is-error { color: #dc2626; }

.mg-table-wrap {
  overflow-x: auto;
  border: 1px solid var(--mg-border);
  border-radius: 16px;
  background: var(--mg-surface);
}

.mg-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.mg-table th,
.mg-table td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--mg-border);
  text-align: left;
  vertical-align: middle;
}

.mg-table th {
  background: var(--mg-surface-muted);
  color: var(--mg-muted);
  font-size: 13px;
  font-weight: 600;
}

.mg-table tbody tr:last-child td { border-bottom: none; }
.mg-table-muted { color: var(--mg-muted); white-space: nowrap; }
.mg-text-positive { color: #059669; font-weight: 600; }
.mg-text-negative { color: #dc2626; font-weight: 600; }

.mg-code {
  padding: 2px 6px;
  border-radius: 6px;
  background: var(--mg-surface-subtle);
  font-size: 12px;
}

.mg-pill {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

.mg-pill-purchase, .mg-pill-order-credited { background: #dcfce7; color: #166534; }
.mg-pill-consume { background: #fee2e2; color: #991b1b; }
.mg-pill-redeem { background: var(--mg-blue-soft); color: var(--mg-blue-dark); }
.mg-pill-refund { background: #fef3c7; color: #92400e; }
.mg-pill-admin_adjust { background: #ede9fe; color: #5b21b6; }
.mg-pill-order-pending, .mg-pill-order-paying { background: #fef3c7; color: #92400e; }
.mg-pill-order-paid { background: #dbeafe; color: #1d4ed8; }
.mg-pill-order-failed, .mg-pill-order-closed, .mg-pill-order-expired, .mg-pill-order-cancelled { background: var(--mg-surface-subtle); color: var(--mg-muted); }
.mg-pill-order-refunded { background: #fef3c7; color: #92400e; }

.mg-main-admin { align-items: stretch; }
.mg-admin-nav { margin: 16px 20px 0; }
.mg-admin-body { padding: 16px 20px 48px; max-width: 1200px; }
.mg-sitemap-sections { display: grid; gap: 16px; }

/* ── Theme toggle ── */
.mg-theme-toggle {
  position: relative;
}

.mg-theme-icon {
  display: block;
}

html[data-theme="light"] .mg-theme-icon-moon,
html:not([data-theme]) .mg-theme-icon-moon {
  display: none;
}

html[data-theme="dark"] .mg-theme-icon-sun {
  display: none;
}

html[data-theme="dark"] .katex,
html[data-theme="dark"] .katex-display {
  color: var(--mg-text);
}

html[data-theme="dark"] .mg-msg-content pre,
html[data-theme="dark"] .chat-message-content pre {
  background: var(--mg-surface-subtle);
}

html[data-theme="dark"] .mg-field input,
html[data-theme="dark"] .mg-field textarea,
html[data-theme="dark"] .mg-field select,
html[data-theme="dark"] .mg-auth-form input {
  background: var(--mg-input-bg);
  color: var(--mg-text);
  border-color: var(--mg-border);
}

html[data-theme="dark"] .mg-composer-card textarea {
  background: transparent;
  border-color: transparent;
}

html[data-theme="dark"] .mg-code {
  background: var(--mg-surface-subtle);
  color: var(--mg-text);
}

@media (max-width: 768px) {
  .mg-account-grid { grid-template-columns: 1fr; }
  .mg-pay-summary { grid-template-columns: 1fr; }
  .mg-trust-list { width: 100%; }
}
