:root {
  --page-gap: clamp(8px, 2vh, 18px);
  --bg: #ece9e2;
  --card: #fffdfa;
  --surface-alt: #f7f4ee;
  --border: #d8d0c3;
  --border-light: #e8e1d7;
  --text: #1d1b18;
  --text-sub: #6d6963;
  --accent: #b98221;
  --accent-strong: #956316;
  --accent-soft: #fbf1db;
  --accent-contrast: #fffdf8;
  --btn-bg: #f5f1ea;
  --btn-hover: #fffdfa;
  --shadow: 0 20px 55px rgba(72, 59, 39, .10), 0 2px 8px rgba(72, 59, 39, .05);
  --radius: 22px;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
}

* { box-sizing: border-box; }

button, input, select, textarea { font: inherit; }
button:focus-visible, a:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 28%, transparent);
  outline-offset: 2px;
}

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "PingFang SC", "Microsoft YaHei", "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
}

html { scroll-behavior: smooth; }
body {
  overflow-x: hidden;
  overflow-y: auto;
  display: block;
}

body.latex-editor-page .main-container {
  min-height: calc(100dvh - 50px);
  height: calc(100dvh - 50px);
}

a { color: var(--accent); text-decoration: none; }


.nav-btn {
  background: var(--card);
  border: none;
  color: var(--text);
  padding: 9px 16px;
  border-radius: 12px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 3px 10px rgba(72, 59, 39, .05);
}
.nav-btn:hover { color: var(--accent); }
.workspace-settings { flex-shrink: 0; }


.main-container {
  position: relative;
  z-index: 2;
  flex: 1 1 auto;
  width: 100%;
  max-width: none;
  min-height: 0;
  margin: var(--page-gap) 0 0;
  padding: 0 20px;
  display: flex;
  flex-direction: column;
  scroll-margin-top: 100px;
  overflow: hidden;
}


.area-card {
  min-width: 0;
  min-height: 0;
  height: 100%;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 3px 12px rgba(72, 59, 39, .07);
  padding: 16px;
}
.input-card,
.output-card {
  display: flex;
  flex-direction: column;
}
.input-card {
  flex: 1 1 auto;
  width: 100%;
}


.preview-float {
  position: absolute;
  right: 20px;
  bottom: 20px;
  left: auto;
  top: auto;
  z-index: 55;
  width: min(420px, calc(100% - 32px));
  height: min(280px, calc(100% - 40px));
  min-width: 280px;
  min-height: 200px;
  max-width: calc(100% - 16px);
  max-height: calc(100% - 16px);
  margin: 0;
  box-shadow: var(--shadow);
  user-select: none;
}
.preview-float .card-title-row {
  cursor: grab;
  touch-action: none;
}
.preview-float.dragging .card-title-row,
.preview-float.resizing .card-title-row {
  cursor: grabbing;
}
.preview-float .zoom-ctrl,
.preview-float .zoom-ctrl * {
  cursor: pointer;
  user-select: none;
}
.preview-float-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  flex-shrink: 0;
}
.preview-share-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  height: 24px;
  padding: 0 10px 0 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #f3f3f3;
  color: #111;
  cursor: pointer;
  flex-shrink: 0;
}
.preview-share-btn:hover {
  background: #ebebeb;
  border-color: color-mix(in srgb, var(--text) 18%, var(--border));
  color: #000;
}
.preview-share-icon {
  display: block;
  flex-shrink: 0;
  width: 15px;
  height: 14px;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
}
.preview-share-label {
  font: 700 12px/1 "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
  letter-spacing: -.01em;
}
.preview-float .card-title-row {
  padding: 6px 10px;
  margin: -16px -16px 10px;
}
.preview-float .product-script {
  font-size: 12px;
  line-height: 1.2;
}
.preview-float .zoom-ctrl {
  gap: 3px;
  border-radius: 6px;
  padding: 1px 4px;
}
.preview-float .zoom-ctrl button {
  width: 14px;
  height: 14px;
  font-size: 11px;
  border-radius: 3px;
  line-height: 1;
}
.preview-float .zoom-ctrl span {
  min-width: 28px;
  font-size: 10px;
}
.preview-float .output-stage,
.preview-float .export-row,
.preview-float .export-row * {
  user-select: auto;
}
.preview-float .export-menu {
  z-index: 60;
}
.preview-resize-handle {
  position: absolute;
  width: 16px;
  height: 16px;
  z-index: 10;
  touch-action: none;
  opacity: .75;
  background:
    linear-gradient(135deg, transparent 45%, var(--border) 46%, var(--border) 54%, transparent 55%),
    linear-gradient(135deg, transparent 60%, var(--border) 61%, var(--border) 69%, transparent 70%),
    linear-gradient(135deg, transparent 75%, var(--accent) 76%, var(--accent) 84%, transparent 85%);
}
.preview-resize-handle:hover { opacity: 1; }
.preview-resize-handle.se {
  right: 2px;
  bottom: 2px;
  cursor: nwse-resize;
  border-radius: 0 0 8px 0;
}
.preview-resize-handle.sw {
  left: 2px;
  bottom: 2px;
  cursor: nesw-resize;
  border-radius: 0 0 0 8px;
  transform: scaleX(-1);
}
.preview-resize-handle.ne {
  right: 2px;
  top: 2px;
  cursor: nesw-resize;
  border-radius: 0 8px 0 0;
  transform: scaleY(-1);
}
.preview-resize-handle.nw {
  left: 2px;
  top: 2px;
  cursor: nwse-resize;
  border-radius: 8px 0 0 0;
  transform: scale(-1);
}
body.preview-float-active {
  cursor: grabbing;
}
body.preview-float-active .preview-float {
  transition: none;
}
.card-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: -16px -16px 12px;
  padding: 11px 14px;
  border-bottom: 1px solid var(--border-light);
  border-radius: 10px 10px 0 0;
  background: var(--surface-alt);
}
.title-toolbar-row {
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 12px;
}
.header-toolbar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 8px;
  margin-left: auto;
  min-width: 0;
}
.header-toolbar .control-groups {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  position: relative;
  margin: 0;
  max-width: none;
}
.header-tabs {
  display: flex;
  align-items: center;
  gap: 8px;
}
.header-toolbar .nav-btn,
.header-toolbar .ctrl-btn,
.header-toolbar .tab-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  white-space: nowrap;
}
.header-toolbar .tab-btn.active {
  color: var(--accent-strong);
  background: var(--accent-soft);
}
.card-title {
  display: flex;
  align-items: baseline;
  gap: 6px;
  color: var(--text);
  font: 700 16px/1.25 "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
}
.card-title b { color: var(--accent); font-weight: 750; }
.card-title small { color: var(--text-sub); font: 600 10px/1.2 "PingFang SC", "Microsoft YaHei", sans-serif; margin-left: 4px; text-transform: uppercase; letter-spacing: .8px; }
.card-title .ti { font-style: normal; color: var(--accent); margin-left: 2px; font-family: sans-serif; font-size: 14px; }
.product-title {
  display: inline-flex;
  align-items: baseline;
  gap: 9px;
  white-space: nowrap;
}
.product-latex {
  color: var(--accent);
  font: 700 24px/1 Georgia, "Times New Roman", serif;
  letter-spacing: -.5px;
}
.product-script {
  color: var(--text);
  font: italic 600 24px/1.1 Georgia, "Times New Roman", serif;
  letter-spacing: -.2px;
}
.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-sub);
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  font-size: 12px;
}
.ghost-btn:hover { color: var(--accent); border-color: var(--accent); }
.ghost-btn.active { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }


.tabs-row { display: none; }
.tab-btn.disabled { color: var(--text-sub); opacity: .55; cursor: not-allowed; }

.panel { position: relative; flex: 0 0 auto; min-height: 0; z-index: 30; }


.layer1-grid {
  display: flex;
  flex-wrap: nowrap;
  align-items: stretch;
  justify-content: flex-start;
  gap: 8px;

  padding-bottom: 4px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
  scrollbar-color: transparent transparent;
  -webkit-overflow-scrolling: touch;
}
.layer1-grid::-webkit-scrollbar {
  height: 4px;
  background: transparent;
}
.layer1-grid::-webkit-scrollbar-track {
  background: transparent;
}
.layer1-grid::-webkit-scrollbar-thumb {
  background: transparent;
  border-radius: 999px;
}
.layer1-grid::-webkit-scrollbar-button {
  display: none;
  width: 0;
  height: 0;
}
.layer1-grid.is-scrolling {
  scrollbar-color: color-mix(in srgb, var(--accent) 55%, var(--border)) transparent;
}
.layer1-grid.is-scrolling::-webkit-scrollbar-thumb {
  background: color-mix(in srgb, var(--accent) 55%, var(--border));
}
.layer1-grid.is-scrolling::-webkit-scrollbar-thumb:hover {
  background: color-mix(in srgb, var(--accent-strong) 65%, var(--border));
}
.layer1-btn {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  flex: 0 0 auto;
  width: 118px;
  min-width: 118px;
  height: 72px;
  gap: 4px;
  padding: 6px 8px;
  background: var(--surface-alt);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  color: var(--text);
  font: 400 14px/1.3 ui-sans-serif, system-ui, sans-serif;
  transition: background .15s ease, color .15s ease;
}
.layer1-btn:hover { background: var(--card); }
.layer1-btn.active {
  background: var(--accent-soft);
  color: var(--accent-strong);
  box-shadow: inset 0 -3px 0 var(--accent);
}
.layer1-btn.active .l1-icon,
.layer1-btn.active .l1-icon mjx-container {
  color: var(--accent-strong);
}
.layer1-btn.active .l1-label {
  color: var(--accent-strong);
  font-weight: 600;
}
.layer1-btn .l1-main {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}
.layer1-btn .l1-icon {
  pointer-events: none;
  width: 100%;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  color: var(--accent);
  font-size: 16px;
}
.layer1-btn .l1-icon mjx-container {
  color: var(--accent);
  max-width: none;
  max-height: none;
  font-size: 13px !important;
  line-height: 1;
}
.layer1-btn .l1-icon mjx-container svg {
  max-width: none;
  max-height: none;
}
.layer1-btn .l1-label {
  width: 100%;
  min-height: 0;
  max-height: 28px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
  color: #111111;
  font: 400 10px/1.25 ui-sans-serif, system-ui, sans-serif;
  text-align: center;
  overflow-wrap: anywhere;
}
.layer1-btn .l1-arrow {
  width: 0;
  height: 0;
  margin: 0;
  flex-shrink: 0;
  align-self: center;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 6px solid #111111;
  border-bottom: 0;
  color: transparent;
  font-size: 0;
  line-height: 0;
}
.layer1-btn:hover .l1-arrow,
.layer1-btn.active .l1-arrow { border-top-color: var(--accent-strong); }


.layer2-wrap {
  position: relative;
  left: auto !important;
  top: auto !important;
  width: 100%;
  max-width: none;
  height: auto;
  min-height: 90px;
  max-height: 180px;
  margin: 8px 0 0;
  border: none;
  border-radius: 6px;
  padding: 6px 4px 6px 8px;
  overflow: hidden;
  background: var(--surface-alt);
  box-shadow: none;
  z-index: 20;
}
.layer2-wrap.has-custom-scroll {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  gap: 4px;
}
.layer2-body {
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
  overflow-x: hidden;
  overflow-y: scroll;
  padding-right: 2px;

  scrollbar-width: none;
  -ms-overflow-style: none;
}
.layer2-body::-webkit-scrollbar {
  width: 0 !important;
  height: 0 !important;
  display: none;
}
.layer2-scroll-rail {
  flex: 0 0 10px;
  width: 10px;
  position: relative;
  align-self: stretch;
  margin: 2px 2px 2px 0;
  border-radius: 999px;
  background: color-mix(in srgb, var(--border) 75%, #fff);
  cursor: pointer;

  opacity: 1;
  visibility: visible;
}
.layer2-scroll-thumb {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  min-height: 28px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--text) 42%, var(--border));
  cursor: grab;
  touch-action: none;
}
.layer2-scroll-thumb:active { cursor: grabbing; }
.layer2-scroll-thumb:hover {
  background: color-mix(in srgb, var(--text) 55%, var(--border));
}
.layer2-section { width: 100%; }
.layer2-divider {
  height: 1px;
  margin: 4px 0;
  border-top: 1px solid var(--border-light);
}
.layer2-heading {
  margin: 2px 0;
  color: #111111;
  font: 600 11px/1.4 ui-sans-serif, system-ui, sans-serif;
}
.layer2-grid {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0;
  width: 100%;
}
.sym-btn {
  position: relative;
  flex: 0 0 var(--layer2-item-width, var(--layer2-default-width, 35px));
  width: var(--layer2-item-width, var(--layer2-default-width, 35px));
  min-width: 0;
  max-width: 100%;
  height: var(--layer2-item-height, var(--layer2-default-height, 35px));
  min-height: var(--layer2-item-height, var(--layer2-default-height, 35px));
  margin: 3px;
  padding: var(--layer2-item-padding, var(--layer2-default-padding, 2px));
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--card);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  color: #111111;
  line-height: 1;
  overflow: hidden;
  box-sizing: border-box;
}
.sym-btn:hover { background: color-mix(in srgb, var(--accent-soft) 70%, var(--card)); color: var(--accent-strong); }
.sym-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  overflow: hidden;
  color: #111111;
}
.sym-icon.sym-caption-fallback {
  padding: 8px 12px;
  font: 600 13px/1.35 ui-sans-serif, system-ui, sans-serif;
  text-align: center;
  color: var(--text);
  white-space: normal;
  word-break: break-word;
}
.sym-btn mjx-container {
  pointer-events: none;
  font-size: 14px !important;
}
.sym-btn mjx-container svg { max-height: none; max-width: none; }
.shortcut-popup .sym-btn mjx-container svg {
  max-height: 100%;
  max-width: 100%;
}
.shortcut-popup .sym-icon mjx-container {
  width: auto;
  min-height: 0;
  display: flex !important;
  align-items: center;
  justify-content: center;
  transform-origin: center;
}
.layer2-ribbon.shortcut-popup .sym-btn {
  background: var(--card);
}
.template-popup .layer2-grid { align-items: stretch; }
.template-popup .tpl-btn {
  flex: 0 0 var(--layer2-item-width, calc(50% - 8px));
  width: var(--layer2-item-width, calc(50% - 8px));
  height: var(--layer2-item-height, 100px);
  min-height: var(--layer2-item-height, 100px);
  flex-direction: row;
  gap: 0;
  padding: var(--layer2-item-padding, 6px);
  overflow: hidden;
}
.template-popup .tpl-btn .tpl-name { display: none; }
.template-popup .tpl-btn .sym-icon {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.template-popup .tpl-btn mjx-container {
  max-width: none !important;
  max-height: none !important;
  overflow: visible;
  font-size: 13px !important;
}
.template-popup .tpl-btn mjx-container svg {
  max-width: none !important;
  max-height: none !important;
}


.control-groups { display: flex; gap: 8px; flex-wrap: wrap; position: relative; min-width: 0; max-width: 100%; }
.ctrl-group {
  position: relative;
  display: inline-flex;
  flex-shrink: 0;
}
.ctrl-btn .swatch { width: 14px; height: 14px; border-radius: 3px; background: linear-gradient(135deg,#ef4444,#3b82f6,#22c55e); flex-shrink: 0; }
.ctrl-pop .ctrl-btn {
  display: flex;
  width: 100%;
  margin: 3px 0;
  background: var(--surface-alt);
  border: 1px solid var(--border-light);
  border-radius: 10px;
  padding: 8px 13px;
  box-shadow: none;
  font-weight: 500;
}
.ctrl-pop .ctrl-btn:hover {
  border-color: var(--accent);
  color: var(--accent-strong);
  background: var(--accent-soft);
}
.ctrl-pop {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: auto;
  z-index: 40;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 8px 24px rgba(16,24,40,.15);
  padding: 8px;
  min-width: 220px;
  max-width: min(340px, calc(100vw - 24px));
  max-height: min(360px, 60vh);
  overflow-y: auto;
}
.ctrl-pop.align-end {
  left: auto;
  right: 0;
}
.ctrl-grid {
  display: grid;
  gap: 6px;
}
.ctrl-grid-color { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.ctrl-grid-fontfamily { grid-template-columns: 1fr; }
.ctrl-grid-fontsize { grid-template-columns: 1fr; }
.ctrl-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  margin: 0;
  padding: 8px 10px;
  border: none;
  border-radius: 10px;
  background: var(--surface-alt);
  color: var(--text);
  cursor: pointer;
  text-align: left;
  font: 500 13px/1.3 "PingFang SC", "Microsoft YaHei", sans-serif;
}
.ctrl-item:hover {
  background: var(--accent-soft);
  color: var(--accent-strong);
}
.ctrl-color-swatch {
  width: 18px;
  height: 18px;
  border-radius: 4px;
  flex-shrink: 0;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.12);
}
.ctrl-item-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ctrl-font-sample {
  width: 34px;
  flex-shrink: 0;
  text-align: center;
  font-size: 16px;
  color: var(--accent-strong);
}
.ctrl-font-sample[data-font="font1"] { font-family: "Times New Roman", Times, serif; }
.ctrl-font-sample[data-font="font2"] { font-weight: 700; }
.ctrl-font-sample[data-font="font3"] { font-style: italic; }
.ctrl-font-sample[data-font="font4"] { text-decoration: underline; }
.ctrl-font-sample[data-font="font5"] { font-family: "Helvetica Neue", Arial, sans-serif; }
.ctrl-font-sample[data-font="font6"] { font-family: "Segoe UI Symbol", "Arial Unicode MS", sans-serif; letter-spacing: .04em; }
.ctrl-font-sample[data-font="font7"] { font-family: "Palatino Linotype", Palatino, serif; font-style: italic; }
.ctrl-font-sample[data-font="font8"] { font-family: "UnifrakturCook", "Times New Roman", serif; }
.ctrl-size-sample {
  display: block;
  width: 100%;
  color: var(--text);
}
.ctrl-size-sample[data-size="size1"] { font-size: 10px; }
.ctrl-size-sample[data-size="size2"] { font-size: 11px; }
.ctrl-size-sample[data-size="size3"] { font-size: 12px; }
.ctrl-size-sample[data-size="size4"] { font-size: 14px; }
.ctrl-size-sample[data-size="size5"] { font-size: 16px; }
.ctrl-size-sample[data-size="size6"] { font-size: 18px; }
.ctrl-size-sample[data-size="size7"] { font-size: 20px; }
.ctrl-size-sample[data-size="size8"] { font-size: 22px; }
.ctrl-size-sample[data-size="size9"] { font-size: 24px; }

.font-size-ctrl, .zoom-ctrl {
  display: flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px 7px;
}
.editor-zoom-ctrl {
  position: absolute;
  top: 8px;
  right: 10px;
  z-index: 4;
  gap: 3px;
  border-radius: 6px;
  padding: 1px 4px;
  background: color-mix(in srgb, var(--card) 92%, #fff);
  box-shadow: 0 1px 4px rgba(72, 59, 39, .08);
}
.editor-zoom-ctrl button {
  width: 14px;
  height: 14px;
  font-size: 11px;
  border-radius: 3px;
  line-height: 1;
}
.editor-zoom-ctrl span {
  min-width: 36px;
  font-size: 10px;
}
.font-size-ctrl button, .zoom-ctrl button {
  border: none;
  background: transparent;
  width: 22px;
  height: 24px;
  cursor: pointer;
  color: var(--text);
  font-size: 15px;
  border-radius: 4px;
}
.font-size-ctrl button:hover, .zoom-ctrl button:hover { background: var(--btn-bg); color: var(--accent); }
.font-size-ctrl span, .zoom-ctrl span { min-width: 30px; text-align: center; font-size: 12px; color: var(--text-sub); }


.editor-box { position: relative; flex: 1 1 auto; min-height: 110px; margin-top: 6px; }
.editor-stack {
  position: relative;
  height: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--card);
  box-shadow: inset 0 1px 0 var(--surface-alt);
}
.editor-stack:focus-within { border-color: var(--accent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 11%, transparent); }
.highlight-layer, .latex-input {
  margin: 0;
  padding: 14px 16px;
  padding-right: 96px;
  font-family: Consolas, "JetBrains Mono", Menlo, monospace;
  font-size: 16px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  height: 100%;
  min-height: 0;
  max-height: none;
  overflow-y: auto;
}
.highlight-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  color: var(--text);
  overflow: hidden;
  visibility: visible;
}
.latex-input {
  position: relative;
  display: block;
  width: 100%;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: transparent;
  caret-color: var(--text);
}
.hl-cmd { color: #7c3aed; }
.hl-brace { color: #b45309; }
.hl-dollar { color: #0891b2; }
.hl-num { color: #16a34a; }
.hl-comment { color: #9ca3af; font-style: italic; }


.autocomplete {
  position: absolute;
  z-index: 60;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 8px 24px rgba(16, 24, 40, .18);
  min-width: 260px;
  max-height: 260px;
  overflow-y: auto;
}
.ac-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 12px;
  cursor: pointer;
  font-size: 13px;
}
.ac-item:hover, .ac-item.selected { background: var(--accent-soft); }
.ac-icon {
  min-width: 52px;
  text-align: center;
  font-family: Consolas, monospace;
  font-size: 12px;
  color: var(--accent);
  border: 1px solid var(--border-light);
  border-radius: 4px;
  padding: 2px 4px;
  background: var(--btn-bg);
}
.ac-cmd { font-family: Consolas, monospace; }
.ac-desc { color: var(--text-sub); font-size: 11px; margin-left: auto; }


.output-stage {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 8px;
  min-height: 110px;
  max-height: none;
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(circle at 1px 1px, var(--border-light) 1px, transparent 1.1px) 0 0 / 24px 24px,
    var(--surface-alt);
  scroll-margin-top: 100px;
}
.output-stage { justify-content: flex-start; }
.output-scale { transform-origin: left center; }
.output-placeholder { color: var(--text-sub); font-size: 13px; }
.output-error {
  color: var(--danger);
  font-family: Consolas, monospace;
  font-size: 12px;
  white-space: pre-wrap;
  width: 100%;
}
.preview-float .export-row {
  margin-top: 10px;
  gap: 6px;
}
.preview-float .export-btn {
  padding: 6px 10px;
  font-size: 12px;
  line-height: 1.2;
}
.preview-float .output-stage {
  flex: 1 1 auto;
  min-height: 0;
  padding: 16px;
}


.export-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
  scroll-margin-top: 100px;
  flex: 0 0 auto;
}
.export-group { position: relative; }
.export-btn {
  background: var(--btn-bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 13px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
.export-btn:hover { border-color: var(--accent); color: var(--accent); }
.export-btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); font-weight: 650; }
.export-btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); color: var(--accent-contrast); }
.export-menu {
  display: none;
  position: absolute;
  bottom: calc(100% + 6px);
  left: 0;
  z-index: 45;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 13px;
  box-shadow: 0 8px 24px rgba(16, 24, 40, .18);
  padding: 6px;
  min-width: 190px;
}
.export-group.open .export-menu { display: block; }
.export-menu button {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  padding: 7px 10px;
  border-radius: 5px;
  cursor: pointer;
  color: var(--text);
  font-size: 13px;
  white-space: nowrap;
}
.export-menu button:hover { background: var(--accent-soft); color: var(--accent); }
.export-menu button i { font-style: normal; color: var(--text-sub); width: 14px; text-align: center; }
.export-menu button.backend-only { opacity: .55; cursor: not-allowed; }
.export-menu button.backend-only:hover { background: var(--btn-bg); color: var(--text); }


.modal-mask {
  position: fixed;
  inset: 0;
  background: rgba(29, 27, 24, .42);
  backdrop-filter: blur(7px);
  z-index: 1200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.settings-dialog {
  background: #fffdfa;
  color: #1d1b18;
  border: 1px solid #e8e1d7;
  border-radius: 22px;
  width: 560px;
  max-width: 100%;
  max-height: 86vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 28px 80px rgba(29, 27, 24, .28);
  position: relative;
}
.settings-dialog .modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 22px;
  border-bottom: 1px solid #e8e1d7;
  font: 600 21px/1.2 Georgia, serif;
  color: #1d1b18;
}
.settings-dialog .modal-close {
  position: static;
  border: none;
  background: transparent;
  font-size: 22px;
  cursor: pointer;
  color: #6d6963;
  width: 32px;
  height: 32px;
  border-radius: 8px;
}
.settings-dialog .modal-close:hover {
  background: #f5f1ea;
  color: #1d1b18;
}
.settings-dialog .modal-body {
  padding: 18px 22px;
  overflow-y: auto;
  color: #1d1b18;
}
.settings-dialog .modal-tip,
.settings-dialog .sec-tip {
  color: #5f5b56;
  font-size: 13px;
  line-height: 1.55;
  margin: 4px 0 10px;
}
.settings-dialog .modal-body h4 {
  margin: 20px 0 4px;
  font-size: 15px;
  font-weight: 700;
  color: #7a5214;
}
.settings-dialog .modal-body h4:first-of-type { margin-top: 0; }
.ext-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.ext-item {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid #e8e1d7;
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
  font-size: 13px;
  color: #1d1b18;
  background: #fff;
}
.ext-item:hover { border-color: #b98221; background: #fbf1db; }
.ext-item input { accent-color: #b98221; }
.ext-item .ext-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.settings-dialog .modal-foot {
  padding: 14px 22px 18px;
  border-top: 1px solid #e8e1d7;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  background: #fffdfa;
}
.settings-dialog .ghost-btn {
  color: #3f3c38;
  border-color: #d8d0c3;
  background: #fff;
}
.settings-dialog .ghost-btn:hover {
  color: #7a5214;
  border-color: #b98221;
  background: #fbf1db;
}


.toast-wrap {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}
.toast {
  background: var(--card);
  border: 1px solid var(--border);
  border-left: 3px solid var(--ok);
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(16, 24, 40, .15);
  padding: 10px 18px;
  font-size: 13px;
  animation: toast-in .25s ease;
}
.toast.warning { border-left-color: var(--warn); }
.toast.error { border-left-color: var(--danger); }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }

@media (min-width: 981px) {
  html, body { height: 100%; min-height: 100%; }
}


@media (max-width: 980px) {
  body { overflow: auto; }
  .main-container {
    height: auto;
    min-height: 100dvh;
  }
  .area-card:not(.preview-float) { height: auto; min-height: 100dvh; }
  .editor-box { min-height: 260px; }
  .preview-float {
    width: min(360px, calc(100% - 24px));
    height: min(260px, calc(100% - 24px));
  }
  .preview-float .output-stage { min-height: 0; }
}

@media (min-width: 981px) and (max-height: 820px) {
  .layer1-btn { width: 110px; min-width: 110px; height: 64px; padding: 4px 6px; }
  .layer1-btn .l1-icon { height: 36px; }
  .layer1-btn .l1-label { max-height: 24px; font-size: 9px; }
}

@media (max-width: 760px) {
  .main-container { margin-top: 14px; padding: 0 14px; gap: 16px; }
  .area-card { padding: 16px; border-radius: 10px; }
  .card-title { flex-wrap: wrap; font-size: 15px; }
  .ext-grid { grid-template-columns: 1fr; }
  .preview-float { right: 12px; bottom: 12px; }
}

@media (max-width: 520px) {
  .nav-btn { padding: 8px 12px; }
  .product-title { gap: 6px; }
  .product-latex { font-size: 19px; }
  .product-script { font-size: 20px; }
  .layer1-grid {
    display: flex;
    flex-wrap: nowrap;
    gap: 6px;
    overflow-x: auto;
    padding-bottom: 4px;
  }
  .layer1-btn {
    width: 108px;
    min-width: 108px;
    height: 64px;
    padding: 4px 6px;
  }
  .layer1-btn .l1-icon { height: 36px; }
  .layer1-btn .l1-label { max-height: 24px; font-size: 9px; }
  .layer2-wrap {
    width: 100%;
    height: auto;
    min-height: 90px;
    max-height: 180px;
    overflow: hidden;
  }
  .card-title small { display: none; }
  .header-toolbar { width: 100%; justify-content: flex-start; margin-left: 0; }
  .control-groups { width: auto; }
  .preview-float {
    width: calc(100% - 16px);
    right: 8px;
    bottom: 8px;
    min-width: 240px;
  }
  .preview-float .export-row { display: grid; grid-template-columns: 1fr 1fr; }
  .preview-float .export-group { min-width: 0; }
  .preview-float .export-btn { width: 100%; }
}
