:root {
  --bg: #eef2f6;
  --panel: #ffffff;
  --line: #d9e1e8;
  --line-strong: #c2ccd6;
  --text: #18212b;
  --muted: #667482;
  --accent: #125ea9;
  --accent-soft: #eaf3fb;
  --danger: #b42318;
  --page-width: 210mm;
  --page-min-height: 297mm;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--bg);
}

* { box-sizing: border-box; }

html {
  margin: 0;
  width: 100%;
  height: 100%;
  min-height: 100%;
  overflow: hidden;
  background: var(--bg);
}

body {
  position: fixed;
  inset: 0;
  margin: 0;
  width: auto;
  height: auto;
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
}

button, input, select { font: inherit; }

button {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 6px;
  cursor: pointer;
}

button:hover { border-color: #aab8c5; background: #f7f9fb; }

button:focus-visible,
input:focus-visible,
select:focus-visible,
[contenteditable="true"]:focus-visible {
  outline: 2px solid #79aee0;
  outline-offset: 1px;
}

.app-header {
  min-height: 58px;
  display: grid;
  grid-template-columns: 250px minmax(240px, 1fr) auto;
  align-items: center;
  gap: 20px;
  padding: 8px 14px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 10px; }

.brand-mark {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  font: 700 20px Georgia, serif;
}

.brand strong { display: block; letter-spacing: .04em; }
.brand span { display: block; color: var(--muted); font-size: 12px; margin-top: 1px; }

.document-title-wrap {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.document-title {
  width: min(560px, 75%);
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 7px 10px;
  text-align: center;
  font-weight: 600;
  background: transparent;
}

.document-title:hover, .document-title:focus { border-color: var(--line); background: #f9fbfc; }
.save-state { color: var(--muted); font-size: 12px; white-space: nowrap; }

.header-actions {
  display: flex;
  align-items: center;
  gap: 3px;
}

.header-separator {
  width: 1px;
  height: 24px;
  margin: 0 4px;
  background: var(--line);
}

.icon-action,
.toolbar-icon {
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  color: #344250;
  background: transparent;
  border-color: transparent;
}

.icon-action {
  width: 35px;
  height: 35px;
  padding: 0;
}

.toolbar-icon {
  width: 32px;
  height: 32px;
  padding: 0;
}

.icon-action:hover,
.icon-action:focus-visible,
.toolbar-icon:hover,
.toolbar-icon:focus-visible {
  color: #17212b;
  background: #f3f6f8;
  border-color: var(--line);
}

.icon-action svg,
.toolbar-icon svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}

.toolbar-symbol {
  min-width: 34px !important;
  padding: 0 6px !important;
  font-family: "Times New Roman", Cambria, serif;
  font-size: 17px;
}

.primary-button, .ghost-button { padding: 8px 12px; }

.primary-button {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.primary-button:hover { background: #0f518f; border-color: #0f518f; }
.ghost-button { background: #fff; }

.menu-bar {
  min-height: 44px;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px 10px;
  background: #f9fbfc;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
  white-space: nowrap;
}

.menu-bar > button, .menu-bar .export-menu > button, .menu-bar select {
  height: 32px;
  min-width: 34px;
  padding: 0 9px;
}

.menu-bar select {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 6px;
}

.font-size-select {
  width: 76px;
  flex: 0 0 auto;
}

.menu-separator {
  width: 1px;
  height: 24px;
  background: var(--line);
  margin: 0 4px;
  flex: 0 0 auto;
}

.math-toolbar {
  display: flex;
  align-items: center;
  gap: 3px;
  flex: 0 0 auto;
}

.math-tool {
  width: 34px;
  height: 32px;
  padding: 0 4px;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font-family: "Times New Roman", Cambria, serif;
  font-size: 21px;
  line-height: 1;
}

.math-tool:hover,
.math-tool:focus-visible {
  border-color: #aab8c5;
  background: #f7f9fb;
}

.math-tool-wide {
  width: 42px;
  font-size: 17px;
}

.fraction-tool {
  width: 38px;
  padding: 0;
}

.fraction-tool-icon {
  width: 25px;
  height: 25px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}

.fraction-template-button {
  display: grid;
  place-items: center;
}

.fraction-template-icon {
  width: 24px;
  display: inline-grid;
  grid-template-rows: auto 1px auto;
  align-items: center;
  justify-items: center;
  line-height: 1;
  font-family: "Times New Roman", Times, serif;
  font-size: 12px;
}

.fraction-template-icon i {
  width: 22px;
  height: 1px;
  background: currentColor;
}

.export-menu { position: relative; }

.header-export .popover {
  top: 40px;
}

.popover {
  position: absolute;
  top: 36px;
  right: 0;
  z-index: 50;
  min-width: 170px;
  padding: 6px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 12px 30px rgba(26, 40, 54, .15);
}

.popover button { width: 100%; border: 0; text-align: left; padding: 8px 10px; }

.find-popover {
  position: fixed;
  z-index: 90;
  width: min(430px, calc(100vw - 20px));
  transform-origin: top right;
  animation: find-popover-in 120ms ease-out;
  padding: 8px;
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  box-shadow: 0 12px 30px rgba(26, 40, 54, .16);
}

.find-popover[hidden] {
  display: none !important;
}

@keyframes find-popover-in {
  from {
    opacity: 0;
    transform: translateY(-4px) scale(.99);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.find-row,
.replace-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.replace-row {
  margin-top: 7px;
  padding-top: 7px;
  border-top: 1px solid var(--line);
}

.find-input {
  min-width: 0;
  flex: 1 1 auto;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 9px;
  background: #fff;
  color: var(--text);
}

.find-input:focus {
  border-color: #8eb8e3;
  outline: 1px solid #b7d2eb;
}

.find-count {
  min-width: 46px;
  color: var(--muted);
  font-size: 11px;
  text-align: center;
  white-space: nowrap;
}

.find-nav-button {
  width: 30px;
  height: 30px;
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  padding: 0;
  border-color: transparent;
  background: transparent;
  color: #40505e;
}

.find-nav-button:hover,
.find-nav-button:focus-visible {
  border-color: var(--line);
  background: #f5f7f9;
}

.find-nav-button svg {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.compact-button {
  height: 30px;
  padding: 0 9px;
  font-size: 11px;
  white-space: nowrap;
}

::highlight(exs-search) {
  background: #fff1a8;
  color: inherit;
}

::highlight(exs-search-active) {
  background: #ffd56a;
  color: inherit;
}

.app-shell {
  min-width: 0;
  min-height: 0;
  height: auto;
  align-self: stretch;
  overflow: hidden;
  display: grid;
  grid-template-columns: 238px minmax(0, 1fr) 278px;
}

.panel {
  min-width: 0;
  min-height: 0;
  background: var(--panel);
}

.left-sidebar {
  border-right: 1px solid var(--line);
  overflow: hidden;
}

.right-sidebar {
  border-left: 1px solid var(--line);
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}

.panel-heading {
  height: 43px;
  display: flex;
  align-items: center;
  padding: 0 12px;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}

.sidebar-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  border-bottom: 1px solid var(--line);
}

.tab-button {
  border: 0;
  border-radius: 0;
  padding: 9px 6px;
  color: var(--muted);
  font-size: 12px;
}

.tab-button.active {
  color: var(--accent);
  background: var(--accent-soft);
  box-shadow: inset 0 -2px 0 var(--accent);
}

.side-content {
  height: calc(100% - 78px);
  overflow-y: auto;
  padding: 8px;
}

.empty-state {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
  padding: 10px;
}

.outline-item, .equation-item {
  width: 100%;
  display: block;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 7px 8px;
  color: #334150;
  font-size: 12px;
  line-height: 1.3;
}

.outline-item.level-2 { padding-left: 20px; }
.outline-item.level-3 { padding-left: 32px; color: var(--muted); }

.equation-item {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 7px;
  align-items: center;
}

.equation-number-pill {
  min-width: 30px;
  padding: 2px 5px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  text-align: center;
  font-size: 11px;
}

.workspace {
  min-width: 0;
  min-height: 0;
  height: auto;
  align-self: stretch;
  overflow-x: auto;
  overflow-y: scroll;
  overscroll-behavior: contain;
  scrollbar-gutter: stable both-edges;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-x pan-y;
  padding: 12px 46px 96px;
  background:
    linear-gradient(#eef2f6 1px, transparent 1px),
    linear-gradient(90deg, #eef2f6 1px, transparent 1px),
    #e8edf2;
  background-size: 20px 20px;
}

.page {
  width: var(--page-width);
  min-height: var(--page-min-height);
  margin: 0 auto;
  background: #fff;
  box-shadow: 0 2px 13px rgba(31, 46, 61, .18);
  transform-origin: top center;
}

.editor {
  min-height: var(--page-min-height);
  padding: 24mm 22mm 25mm;
  font-family: "Times New Roman", Times, serif;
  font-size: 12pt;
  line-height: 1.55;
  color: #111;
  outline: none;
}

.editor h1, .editor h2, .editor h3 {
  font-family: Arial, Helvetica, sans-serif;
  line-height: 1.22;
  color: #111827;
}

.editor h1 { font-size: 22pt; margin: 0 0 18pt; }
.editor h2 { font-size: 16pt; margin: 20pt 0 9pt; }
.editor h3 { font-size: 13pt; margin: 15pt 0 7pt; }
.editor p { margin: 0 0 10pt; }

.editor blockquote {
  margin: 12pt 18pt;
  padding-left: 12pt;
  border-left: 3px solid #98a7b5;
  color: #46525f;
}

.editor table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  margin: 14pt 0;
}

.editor th, .editor td {
  min-width: 45px;
  border: 1px solid #333;
  padding: 5px 7px;
  vertical-align: top;
  overflow-wrap: anywhere;
}

.editor.table-resize-col,
.editor.table-resize-col * {
  cursor: col-resize !important;
}

.editor.table-resize-row,
.editor.table-resize-row * {
  cursor: row-resize !important;
}

.editor.table-resizing,
.editor.table-resizing * {
  user-select: none !important;
}

.editor img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 12pt auto;
}

.inline-equation {
  display: inline-flex;
  align-items: baseline;
  margin: 0 .08em;
  vertical-align: -0.12em;
}

.inline-equation math-field {
  display: inline-block;
  min-width: 1.2em;
  max-width: 100%;
  border: 0;
  padding: .06em .08em;
  font-size: 1em;
  background: transparent;
  vertical-align: middle;
}

.inline-equation math-field:focus {
  min-width: 2.2em;
}

.display-equation {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 12px;
  margin: 16pt 0;
  min-height: 36px;
}

.display-equation .equation-center {
  min-width: 0;
  display: flex;
  justify-content: center;
}

.display-equation math-field {
  max-width: 100%;
  border: 0;
  padding: 4px 8px;
  background: transparent;
  font-size: 1.1em;
}

.display-equation .equation-number {
  min-width: 38px;
  text-align: right;
  font-family: "Times New Roman", Times, serif;
}

math-field {
  /* Keep MathLive selection/highlight states subtle so formulas read like native text. */
  --selection-background-color: #e6edf4;
  --contains-highlight-background-color: #f2f5f8;
  --caret-color: #315d86;
  --smart-fence-color: #5f7790;
}

math-field:focus {
  outline: 1px solid #c7d6e4;
  border-radius: 3px;
  background: #fff;
}

/* EXS uses its own math toolbar; MathLive's on-screen keyboard is intentionally disabled. */
math-field::part(virtual-keyboard-toggle),
math-field::part(menu-toggle) {
  display: none !important;
}

.tool-section { padding: 12px; border-bottom: 1px solid var(--line); }

.tool-section h3 {
  margin: 0 0 9px;
  font-size: 12px;
  color: #3a4754;
  text-transform: uppercase;
  letter-spacing: .04em;
}

.template-grid, .symbol-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 5px;
}

.template-grid button, .symbol-grid button {
  height: 34px;
  padding: 0 3px;
  font-family: "Times New Roman", Times, serif;
  font-size: 16px;
}

.field-label {
  display: block;
  margin: 10px 0 5px;
  color: var(--muted);
  font-size: 12px;
}

.text-input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 9px;
  background: #fff;
}

.status-bar {
  min-height: 27px;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 12px;
  background: #fff;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 11px;
}

.status-spacer { flex: 1; }

.dialog {
  width: min(720px, calc(100vw - 32px));
  border: 0;
  padding: 0;
  border-radius: 10px;
  box-shadow: 0 20px 60px rgba(16, 28, 40, .28);
}

.dialog::backdrop { background: rgba(19, 31, 43, .45); }
.small-dialog { width: min(520px, calc(100vw - 32px)); }
.dialog-card { padding: 0; background: #fff; }

.dialog-card > header, .dialog-card > footer {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
}

.dialog-card > header {
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
}

.dialog-card > footer {
  justify-content: flex-end;
  border-top: 1px solid var(--line);
}

.dialog-card h2 { margin: 0 0 3px; font-size: 18px; }
.dialog-card p { margin: 0; color: var(--muted); font-size: 12px; }

.dialog-card > label, .dialog-fields label {
  display: block;
  padding: 10px 16px;
  font-size: 12px;
  color: var(--muted);
}

.dialog-card label span { display: block; margin-bottom: 5px; }
.equation-editor-wrap { padding: 18px 16px 8px; }

#equationEditor {
  width: 100%;
  min-height: 58px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  padding: 10px 12px;
  font-size: 20px;
}

.icon-button {
  width: 32px;
  height: 32px;
  border: 0;
  font-size: 22px;
  color: var(--muted);
}

.context-menu {
  position: fixed;
  z-index: 120;
  min-width: 190px;
  padding: 5px;
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  box-shadow: 0 12px 30px rgba(26, 40, 54, .18);
}

.context-menu[hidden] {
  display: none !important;
}

.context-menu-section {
  display: grid;
  gap: 2px;
}

.context-menu-section[hidden],
.context-menu-separator[hidden] {
  display: none !important;
}

.context-menu button {
  width: 100%;
  min-height: 32px;
  display: grid;
  grid-template-columns: 24px 1fr;
  align-items: center;
  gap: 8px;
  padding: 5px 8px;
  border-color: transparent;
  background: transparent;
  text-align: left;
  color: var(--text);
  font-size: 12px;
}

.context-menu button:hover,
.context-menu button:focus-visible {
  border-color: var(--line);
  background: #f5f7f9;
}

.context-menu button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.context-menu-separator {
  height: 1px;
  margin: 5px 4px;
  background: var(--line);
}

.toast {
  position: fixed;
  z-index: 100;
  left: 50%;
  bottom: 42px;
  transform: translateX(-50%);
  max-width: min(520px, calc(100vw - 40px));
  padding: 9px 13px;
  background: #1f2d3a;
  color: #fff;
  border-radius: 7px;
  font-size: 12px;
  box-shadow: 0 10px 24px rgba(0,0,0,.2);
}

@media (max-width: 1050px) {
  .app-shell { grid-template-columns: 190px minmax(0,1fr) 230px; }
  .workspace { padding-left: 26px; padding-right: 26px; }
}

@media (max-width: 820px) {
  html {
    height: auto;
    min-height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
  }

  body {
    position: static;
    inset: auto;
    width: 100%;
    height: auto;
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
    overflow-y: visible;
    display: block;
  }
  .app-header { grid-template-columns: auto minmax(100px, 1fr) auto; gap: 8px; padding-inline: 8px; }
  .brand span, .save-state { display: none; }
  .brand > div:last-child { display: none; }
  .document-title { width: 100%; min-width: 0; }
  .header-actions { gap: 1px; }
  .icon-action { width: 32px; height: 32px; }
  .header-separator { margin-inline: 2px; }
  .app-shell { display: block; }
  .left-sidebar { display: none; }
  .right-sidebar { border: 0; border-top: 1px solid var(--line); }
  .workspace {
    height: auto;
    overflow: visible;
    overscroll-behavior: auto;
    scrollbar-gutter: auto;
    padding: 16px 8px 48px;
  }
  .page { width: 100%; min-height: 0; }
  .editor { min-height: 72vh; padding: 28px 22px; }
  .status-bar { position: sticky; bottom: 0; }
}

@media print {
  html { overflow: visible; }
  body {
    position: static;
    inset: auto;
    width: auto;
    height: auto;
    overflow: visible;
    display: block;
    background: #fff;
  }
  .app-header, .menu-bar, .left-sidebar, .right-sidebar, .status-bar, .toast { display: none !important; }
  .app-shell, .workspace { display: block; overflow: visible; padding: 0; background: #fff; }
  .page { width: auto; min-height: 0; margin: 0; box-shadow: none; transform: none !important; }
  .editor { min-height: 0; padding: 0; }
  math-field { outline: none !important; background: transparent !important; }
  @page { size: A4; margin: 22mm; }
}
