/* ============ 基础 ============ */
:root {
  --font-ui: system-ui, -apple-system, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --font-serif: Georgia, "Times New Roman", "Songti SC", "SimSun", serif;
  --font-mono: "JetBrains Mono", "Fira Code", Consolas, "Courier New", monospace;
  --card-radius: 12px;

  /* 亮色:黑白灰 */
  --bg: #ffffff;
  --bg-soft: #fafbfc;
  --surface: #ffffff;
  --ink: #1f2328;
  --ink-soft: #57606a;
  --ink-faint: #8b949e;
  --line: #d8dee4;
  --line-soft: #eaeef2;
  --pill: #f3f4f6;
  --pill-hover: #e7e9ec;
  --btn-active: #dcdee2;
  --accent: #2563eb;
  --focus-ring: 0 0 0 3px rgba(37, 99, 235, 0.12);
  --sel-ac-bg: #e8f0fe;
  --sel-ac-ink: #1d4ed8;
  --sel-bg: #cfe0ff;
  --sel-ink: #1e3a8a;
  --error: #b42318;
  --toast-bg: #24292f;
  --toast-ink: #ffffff;
  --switch-off: #d1d5db;
  --switch-on: #1f2328;
  --switch-knob: #ffffff;
  --shadow: rgba(0, 0, 0, 0.14);
}

/* 暗色:Tokyo Night Storm */
:root[data-theme="dark"] {
  --bg: #16161e;
  --bg-soft: #11121a;
  --surface: #1a1b26;
  --ink: #c0caf5;
  --ink-soft: #9aa5ce;
  --ink-faint: #565f89;
  --line: #2f334d;
  --line-soft: #1f2335;
  --pill: #1f2335;
  --pill-hover: #24283b;
  --btn-active: #2a2f45;
  --accent: #7aa2f7;
  --focus-ring: 0 0 0 3px rgba(122, 162, 247, 0.22);
  --sel-ac-bg: #283457;
  --sel-ac-ink: #7aa2f7;
  --sel-bg: #33415e;
  --sel-ink: #c0caf5;
  --error: #f7768e;
  --toast-bg: #24283b;
  --toast-ink: #c0caf5;
  --switch-off: #2f334d;
  --switch-on: #c0caf5;
  --switch-knob: #16161e;
  --shadow: rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }

/* hidden 属性必须压过自定义 display,否则工具栏折叠/候选条隐藏会失效 */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-ui);
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

.container {
  max-width: 980px;
  margin: 0 auto;
  padding: 28px 20px 48px;
}

/* ============ 头部 ============ */
.site-header {
  position: relative;
  text-align: center;
  margin-bottom: 22px;
}

/* 亮/暗主题切换:亮色显示月亮(点击转暗),暗色显示太阳(点击转亮) */
.theme-btn {
  position: absolute;
  right: 0;
  top: 4px;
}

[data-theme="light"] .theme-btn .icon-sun { display: none; }
[data-theme="dark"] .theme-btn .icon-moon { display: none; }

.logo {
  margin: 0;
  font-family: var(--font-serif);
  font-weight: 700;
  font-size: 44px;
  letter-spacing: 1px;
  line-height: 1.15;
}

.latex-mark { font-family: var(--font-serif); }

.mark-sup,
.mark-sub {
  font-size: 0.68em;
  position: relative;
}

.mark-sup { vertical-align: 0.42em; margin-left: 0.06em; }
.mark-sub { vertical-align: -0.18em; margin-right: 0.06em; }

.logo-rest {
  font-family: var(--font-serif);
  font-style: italic;
  font-weight: 600;
  margin-left: 10px;
}

.subtitle {
  margin: 8px 0 0;
  color: var(--ink-soft);
  font-size: 14px;
}

/* ============ 符号工具栏 ============ */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px 10px;
  margin-bottom: 14px;
}

.sym-group {
  display: inline-flex;
  align-items: stretch;
  background: var(--pill);
  border-radius: 10px;
  padding: 3px;
  position: relative;
}

/* 所有符号按钮统一固定尺寸,图标等比缩放居中 */
.sym-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 36px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
  color: var(--ink);
  transition: background 0.12s ease;
}

.sym-btn:hover { background: var(--pill-hover); }
.sym-btn:active { background: var(--btn-active); }
.sym-btn img {
  max-width: 34px;
  max-height: 22px;
  width: auto;
  height: auto;
  display: block;
  pointer-events: none;
}
.sym-btn .fb {
  font-family: var(--font-mono);
  font-size: 10px;
  max-width: 36px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* MathJax 就绪前的文字占位 */
.thumb-ph {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--ink-faint);
  max-width: 36px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sym-btn svg.thumb-svg { display: block; }

.svg-btn { border-radius: 10px; }

/* 悬停展开的完整符号面板(仿源站) */
.sym-panel {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  display: grid;
  grid-template-columns: repeat(4, 42px);
  gap: 4px;
  padding: 7px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 10px 26px var(--shadow);
  z-index: 60;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.12s ease 0.25s, visibility 0s linear 0.35s;
}

.sym-group:hover .sym-panel,
.sym-group:focus-within .sym-panel {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transition-delay: 0.12s, 0.12s;
}

.menu-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
  color: var(--ink);
  font-size: 14px;
  transition: background 0.12s ease;
}

.menu-btn:hover { background: var(--pill-hover); }
.menu-btn .caret { font-size: 10px; color: var(--ink-faint); }

.menu-wrap { position: relative; }

/* 下拉菜单(工具栏与操作栏共用) */
.split-group { position: relative; display: inline-flex; }

.dropdown {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 50;
  min-width: 218px;
  padding: 5px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 8px 24px var(--shadow);
}

.dropdown.open { display: block; }

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 10px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  cursor: pointer;
  font-size: 13.5px;
  color: var(--ink);
  text-align: left;
}

.dropdown-item:hover { background: var(--pill); }
.dropdown-item code {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-soft);
  background: var(--pill);
  padding: 1px 5px;
  border-radius: 4px;
  margin-left: auto;
}

.dropdown-item .check {
  visibility: hidden;
  color: var(--accent);
  font-weight: 700;
  width: 12px;
}

.dropdown-item.active .check { visibility: visible; }

/* ============ 操作栏 ============ */
.actionbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 12px;
}

.actionbar-left,
.actionbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.action-label {
  font-size: 13px;
  color: var(--ink-faint);
  margin-right: -2px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 10px;
  background: var(--pill);
  color: var(--ink);
  cursor: pointer;
  transition: background 0.12s ease, opacity 0.12s ease;
}

.icon-btn:hover:not(:disabled) { background: var(--pill-hover); }
.icon-btn:disabled { opacity: 0.35; cursor: not-allowed; }

/* 括号自动补全开关(仿 CodeCogs:绿色拨动开关 + {} 标签) */
.pair-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 38px;
  padding: 0 10px;
  border-radius: 10px;
  background: var(--pill);
  cursor: pointer;
  user-select: none;
}

.pair-toggle input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.switch-ui {
  width: 34px;
  height: 18px;
  border-radius: 9px;
  background: var(--switch-off);
  position: relative;
  transition: background 0.15s ease;
  flex: none;
}

.switch-ui::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--switch-knob);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
  transition: left 0.15s ease;
}

.pair-toggle input:checked + .switch-ui { background: var(--switch-on); }
.pair-toggle input:checked + .switch-ui::after { left: 18px; }
.pair-toggle input:focus-visible + .switch-ui {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.switch-tag {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--ink-soft);
}

.pill-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 38px;
  padding: 0 14px;
  border: 0;
  border-radius: 10px 0 0 10px;
  background: var(--pill);
  color: var(--ink);
  font-size: 13.5px;
  font-family: var(--font-ui);
  cursor: pointer;
  transition: background 0.12s ease, opacity 0.12s ease;
  white-space: nowrap;
}

.pill-btn:hover:not(:disabled) { background: var(--pill-hover); }
.pill-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.caret-btn {
  border-radius: 0 10px 10px 0;
  padding: 0 9px;
  border-left: 1px solid var(--line-soft);
}

.mode-chip {
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--ink-soft);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 1px 5px;
}

.pill-select {
  height: 38px;
  border: 0;
  border-left: 1px solid var(--line-soft);
  border-radius: 0 10px 10px 0;
  background: var(--pill);
  color: var(--ink);
  font-size: 13px;
  font-family: var(--font-ui);
  padding: 0 8px;
  cursor: pointer;
  outline: none;
}

.pill-select:hover { background: var(--pill-hover); }

/* ============ 代码输入区 ============ */
.editor-card {
  border: 1px solid var(--line);
  border-radius: var(--card-radius);
  overflow: hidden;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.editor-card:focus-within {
  border-color: var(--accent);
  box-shadow: var(--focus-ring);
}

#latex-input {
  display: block;
  width: 100%;
  min-height: 170px;
  padding: 14px 16px;
  border: 0;
  outline: none;
  resize: vertical;
  font-family: var(--font-mono);
  font-size: 15px;
  line-height: 1.6;
  color: var(--ink);
  background: var(--surface);
}

#latex-input::placeholder { color: var(--ink-faint); }

.editor-status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 14px;
  border-top: 1px solid var(--line-soft);
  background: var(--bg-soft);
  font-size: 12px;
  color: var(--ink-faint);
}

.status-hint { text-align: center; flex: 1; }

/* ============ 渲染预览区 ============ */
.preview-card {
  margin-top: 16px;
  border: 1px solid var(--line);
  border-radius: var(--card-radius);
  overflow: hidden;
}

.preview-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 9px 14px;
  border-bottom: 1px solid var(--line-soft);
  background: var(--bg-soft);
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-soft);
}

.preview-hint { font-weight: 400; font-size: 12px; color: var(--ink-faint); }

.preview-body {
  min-height: 180px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 26px 18px;
  overflow-x: auto;
}

#preview-img {
  max-width: 100%;
  transition: opacity 0.18s ease;
}

#preview-img.loading { opacity: 0.25; }

#preview-katex {
  width: 100%;
  text-align: center;
  line-height: 1.4;
}

.preview-empty {
  color: var(--ink-faint);
  font-size: 14px;
}

.preview-error {
  color: var(--error);
  font-size: 14px;
}

/* ============ 页脚 ============ */
.site-footer {
  margin-top: 26px;
  text-align: center;
  font-size: 12.5px;
  color: var(--ink-faint);
}

.site-footer a { color: var(--ink-soft); }

/* ============ Toast ============ */
.toast {
  position: fixed;
  left: 50%;
  bottom: 30px;
  transform: translateX(-50%) translateY(12px);
  background: var(--toast-bg);
  color: var(--toast-ink);
  font-size: 13.5px;
  padding: 9px 18px;
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 100;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ============ 下拉自动补全 ============ */
.ac-dd {
  position: fixed;
  z-index: 90;
  width: 320px;
  max-width: 92vw;
  max-height: 262px;
  overflow-y: auto;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 10px 28px var(--shadow);
  font-size: 13px;
}

.ac-row {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 5px 9px;
  border-radius: 6px;
  cursor: pointer;
}

.ac-row:hover { background: var(--pill); }
.ac-row.active { background: var(--sel-ac-bg); }

.ac-row .cap {
  font-family: var(--font-mono);
  font-size: 12.5px;
  color: var(--ink);
  white-space: nowrap;
}

.ac-row.active .cap { color: var(--sel-ac-ink); font-weight: 700; }

.ac-row .meta {
  margin-left: auto;
  font-size: 11.5px;
  color: var(--ink-faint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ac-tip {
  padding: 5px 9px 3px;
  border-top: 1px solid var(--line-soft);
  margin-top: 3px;
  font-size: 11px;
  color: var(--ink-faint);
  text-align: center;
}

/* 补全候选占位在输入框内的选中效果 */
#latex-input::selection {
  background: var(--sel-bg);
  color: var(--sel-ink);
}

/* ============ 响应式 ============ */
@media (max-width: 700px) {
  .logo { font-size: 30px; }
  .logo-rest { margin-left: 7px; }
  .actionbar { justify-content: center; }
  .action-label { display: none; }
  .preview-body { min-height: 120px; padding: 16px 8px; }
  #latex-input { min-height: 120px; font-size: 13.5px; }
  .sym-btn { width: 38px; height: 34px; }
  .sym-btn img { max-width: 30px; max-height: 18px; }
  .sym-panel { grid-template-columns: repeat(4, 38px); }
  .menu-btn { padding: 2px 8px; font-size: 13px; }
  .toolbar { gap: 6px 8px; }
  .status-hint { display: none; }
  .preview-hint { display: none; }
  .ac-dd { width: 280px; }
}

@media (max-width: 420px) {
  .container { padding: 18px 10px 32px; }
  .logo { font-size: 25px; }
  .subtitle { font-size: 12px; }
  .pill-btn { padding: 0 10px; font-size: 12.5px; }
  .pill-select { font-size: 12px; padding: 0 5px; }
  .mode-chip { display: none; }
  .ac-dd .ac-tip { display: none; }
}

@media (max-height: 520px) and (orientation: landscape) {
  .container { padding-top: 12px; }
  .site-header { margin-bottom: 10px; }
  .logo { font-size: 24px; }
  .subtitle { margin-top: 4px; font-size: 12px; }
  .toolbar { margin-bottom: 8px; }
  #latex-input { min-height: 80px; }
  .preview-body { min-height: 90px; padding: 10px; }
}
