@font-face {
  font-family: "Latin Modern Roman";
  src: url("/fonts/lmroman10-regular.otf") format("opentype");
  font-style: normal;
  font-weight: 400;
  font-display: swap;
}

@font-face {
  font-family: "Latin Modern Roman";
  src: url("/fonts/lmroman10-bold.otf") format("opentype");
  font-style: normal;
  font-weight: 700;
  font-display: swap;
}

@font-face {
  font-family: "Latin Modern Roman";
  src: url("/fonts/lmroman10-italic.otf") format("opentype");
  font-style: italic;
  font-weight: 400;
  font-display: swap;
}

@font-face {
  font-family: "Latin Modern Roman";
  src: url("/fonts/lmroman10-bolditalic.otf") format("opentype");
  font-style: italic;
  font-weight: 700;
  font-display: swap;
}

@font-face {
  font-family: "Latin Modern Mono";
  src: url("/fonts/lmmono10-regular.otf") format("opentype");
  font-style: normal;
  font-weight: 400;
  font-display: swap;
}

:root {
  color-scheme: light;
  font-family: "Latin Modern Roman", "Times New Roman", Times, serif;
  color: #171717;
  background: #ffffff;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  --border: #e2e2e2;
  --border-strong: #c9c9c9;
  --muted: #737373;
  --text: #171717;
  --soft: #f7f7f7;
  --soft-hover: #eeeeee;
  --accent: #171717;
  --error: #b91c1c;
  --success: #166534;
  --working: #92400e;
}

* {
  box-sizing: border-box;
}

html,
body,
#app {
  min-height: 100%;
}

body {
  min-width: 320px;
  min-height: 100vh;
  min-height: 100svh;
  margin: 0;
  background: #f7f7f7;
}

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

button {
  border: 0;
}

button:focus-visible,
label.file-button:focus-within {
  outline: 2px solid #9b9b9b;
  outline-offset: 1px;
}

button:not(:disabled),
label.file-button {
  cursor: pointer;
}

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

h2,
p {
  margin: 0;
}

.workspace {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 100vh;
  gap: 8px;
  padding: 8px;
  background: #f7f7f7;
}

.workspace.has-closed {
  grid-template-rows: auto auto minmax(0, 1fr);
}

.workspace.no-open {
  align-content: start;
  grid-template-rows: auto auto;
}

.toolbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  min-height: 38px;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.toolbar-actions {
  justify-content: flex-end;
}

.file-button,
.ghost-button,
.icon-button,
.pane-toggle,
.closed-pane-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid var(--border-strong);
  border-radius: 2px;
  color: var(--text);
  background: #fbfbfb;
  font-size: 0.9rem;
  font-weight: 400;
  transition:
    background 140ms ease,
    border-color 140ms ease;
}

.file-button:hover,
.ghost-button:hover,
.icon-button:hover,
.pane-toggle:hover,
.closed-pane-button:hover {
  background: var(--soft-hover);
  border-color: #a7a7a7;
}

.icon-button,
.pane-toggle,
.zoom-button {
  min-width: 32px;
  padding: 0;
}

.file-button input {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.closed-panes {
  display: none;
  align-items: center;
  gap: 6px;
  min-width: 0;
  min-height: 34px;
  padding: 0 2px;
}

.workspace.has-closed .closed-panes {
  display: flex;
}

.closed-panes span {
  color: var(--muted);
  font-size: 0.86rem;
}

.closed-pane-button {
  min-height: 30px;
  padding: 0 10px;
  background: #ffffff;
}

.closed-pane-button.hidden {
  display: none;
}

.panes {
  display: flex;
  align-items: stretch;
  min-height: 0;
}

.panes.open-0 {
  display: none;
}

.pane {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #ffffff;
  transition:
    border-color 140ms ease,
    box-shadow 140ms ease;
  flex: 1 1 0;
}

.source-pane {
  grid-template-rows: auto auto minmax(0, 1fr);
}

.latex-pane {
  grid-template-rows: auto minmax(0, 1fr);
}

.pane.hidden {
  display: none;
}

.pane-gutter {
  position: relative;
  flex: 0 0 8px;
  min-width: 8px;
  cursor: col-resize;
  touch-action: none;
}

.pane-gutter::before {
  content: "";
  position: absolute;
  top: 10px;
  bottom: 10px;
  left: 3px;
  width: 1px;
  background: #d6d6d6;
}

.pane-gutter:hover::before {
  background: #a8a8a8;
}

.pane-gutter.hidden {
  display: none;
}

.pane:focus-within {
  border-color: #b8b8b8;
  box-shadow: inset 0 0 0 1px #d4d4d4;
}

.pane-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 48px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  background: #fafafa;
}

.pane-header h2 {
  color: var(--text);
  font-size: 0.98rem;
  line-height: 1.15;
}

.pane-header p {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.8rem;
}

.pane-toggle {
  width: 28px;
  min-width: 28px;
  min-height: 28px;
  padding: 0;
  font-size: 1rem;
  line-height: 1;
}

.pane-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.zoom-fit {
  min-width: 44px;
  padding: 0 8px;
}

.insert-tools {
  display: flex;
  align-items: center;
  min-width: 0;
  min-height: 44px;
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  background: #fafafa;
}

.insert-control {
  display: grid;
  grid-template-columns: auto minmax(150px, 1fr);
  align-items: center;
  gap: 8px;
  width: 100%;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 400;
}

.insert-control select {
  min-width: 0;
  min-height: 30px;
  border: 1px solid var(--border-strong);
  border-radius: 2px;
  padding: 0 8px;
  color: var(--text);
  background: #ffffff;
  font-size: 0.86rem;
  font-weight: 400;
}

textarea {
  width: 100%;
  min-width: 0;
  min-height: 0;
  height: 100%;
  resize: none;
  border: 0;
  outline: none;
  padding: 16px;
  color: var(--text);
  background: #ffffff;
  font-size: 1rem;
  line-height: 1.65;
  tab-size: 2;
}

textarea:focus {
  background: #ffffff;
}

textarea::placeholder {
  color: #a3a3a3;
}

#latex-editor {
  position: absolute;
  inset: 0;
  z-index: 1;
  color: transparent;
  caret-color: var(--text);
  background: transparent;
  font-family: "Latin Modern Mono", "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.92rem;
  line-height: 1.62;
  white-space: pre;
  overflow: auto;
}

#latex-editor::selection {
  color: transparent;
  background: rgba(115, 115, 115, 0.22);
}

#latex-editor::placeholder {
  color: #a3a3a3;
}

.latex-editor-wrap {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: #ffffff;
}

.latex-highlight {
  position: absolute;
  inset: 0;
  z-index: 0;
  min-width: 100%;
  min-height: 100%;
  margin: 0;
  padding: 16px;
  overflow: hidden;
  color: #262626;
  background: #ffffff;
  font-family: "Latin Modern Mono", "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.92rem;
  line-height: 1.62;
  tab-size: 2;
  white-space: pre;
}

.tex-command {
  color: #5f5f5f;
}

.tex-brace,
.tex-script {
  color: #111111;
}

.tex-math,
.tex-special {
  color: #4b4b4b;
}

.tex-comment {
  color: #8f8f8f;
  font-style: italic;
}

#source-editor {
  font-family: "Latin Modern Roman", "Times New Roman", Times, serif;
  color: var(--text);
}

.pdf-preview {
  display: block;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  background: #ffffff;
}

.empty-preview {
  display: grid;
  place-items: center;
  min-height: 100%;
  padding: 24px;
  color: var(--muted);
  text-align: center;
  font-size: 0.93rem;
}

.pdf-canvas-viewer {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  min-width: max-content;
  min-height: 100%;
  padding: 18px;
  background: #f2f2f2;
}

.pdf-thumbnail-strip {
  position: sticky;
  top: 18px;
  display: grid;
  gap: 10px;
  width: 102px;
  max-height: calc(100vh - 100px);
  overflow: auto;
  flex: 0 0 102px;
}

.pdf-thumbnail-strip.hidden {
  display: none;
}

.pdf-thumbnail {
  display: grid;
  justify-items: center;
  gap: 4px;
  padding: 5px;
  border: 1px solid var(--border-strong);
  border-radius: 2px;
  background: #ffffff;
}

.pdf-thumbnail.active {
  border-color: #6b6b6b;
  box-shadow: inset 0 0 0 1px #6b6b6b;
}

.pdf-thumbnail canvas {
  display: block;
  max-width: 86px;
  height: auto;
  background: #ffffff;
}

.pdf-page-stack {
  display: grid;
  justify-content: center;
  gap: 18px;
  min-width: max-content;
}

.pdf-canvas-page {
  display: block;
  padding: 0;
  background: #ffffff;
  border: 1px solid #d8d8d8;
}

.pdf-canvas-page canvas {
  display: block;
  background: #ffffff;
}

.error-copy {
  color: var(--error);
}

.toast {
  position: fixed;
  right: 14px;
  bottom: 14px;
  z-index: 20;
  max-width: min(340px, calc(100vw - 28px));
  padding: 10px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 2px;
  color: var(--text);
  background: #ffffff;
  box-shadow: 0 12px 30px rgba(17, 24, 39, 0.12);
  opacity: 0;
  transform: translateY(10px);
  pointer-events: none;
  transition:
    opacity 140ms ease,
    transform 140ms ease;
}

.toast.visible {
  opacity: 1;
  transform: translateY(0);
}

svg {
  width: 17px;
  height: 17px;
  flex: 0 0 auto;
}

.reader-dialog {
  width: min(1320px, calc(100vw - 28px));
  height: calc(100vh - 28px);
  max-width: none;
  max-height: none;
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  background: #f4f4f4;
  overflow: hidden;
}

.reader-dialog::backdrop {
  background: rgba(17, 24, 39, 0.42);
}

.reader-shell {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  width: 100%;
  height: 100%;
}

.reader-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 48px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  background: #ffffff;
}

.reader-bar strong {
  margin-right: 8px;
  font-size: 0.95rem;
}

.reader-bar span {
  color: var(--muted);
  font-size: 0.86rem;
}

.reader-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.page-control {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 32px;
  padding: 0 8px;
  border: 1px solid var(--border-strong);
  border-radius: 2px;
  background: #fbfbfb;
  color: var(--muted);
  font-size: 0.86rem;
}

.page-control input {
  width: 48px;
  min-width: 0;
  height: 24px;
  border: 0;
  border-bottom: 1px solid var(--border-strong);
  outline: 0;
  background: transparent;
  color: var(--text);
  text-align: center;
}

.reader-preview {
  width: 100%;
  height: 100%;
  min-height: 0;
  background: #ffffff;
}

.print-frame {
  position: fixed;
  right: 0;
  bottom: 0;
  width: 1px;
  height: 1px;
  border: 0;
  opacity: 0;
  pointer-events: none;
}

@media (max-width: 1180px) {
  .panes {
    flex-direction: column;
  }

  .pane {
    min-height: 420px;
  }

  .pane-gutter {
    flex-basis: 8px;
    min-width: 0;
    min-height: 8px;
    cursor: row-resize;
  }

  .pane-gutter::before {
    top: 3px;
    right: 10px;
    bottom: auto;
    left: 10px;
    width: auto;
    height: 1px;
  }

  .toolbar {
    grid-template-columns: 1fr;
  }

  .toolbar-actions,
  .toolbar-group {
    justify-content: center;
  }
}

@media (max-width: 640px) {
  html,
  body,
  #app {
    height: 100%;
  }

  body {
    overflow: hidden;
  }

  .workspace {
    height: 100svh;
    height: 100dvh;
    min-height: 0;
    gap: 6px;
    padding: max(6px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right))
      max(6px, env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
  }

  .workspace.no-open {
    height: auto;
    min-height: 100svh;
    min-height: 100dvh;
  }

  .toolbar {
    gap: 6px;
    min-height: 0;
  }

  .toolbar-group {
    display: grid;
    width: 100%;
    gap: 6px;
    justify-content: initial;
  }

  .toolbar-group:first-child {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .toolbar-actions {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .toolbar .file-button,
  .toolbar .ghost-button {
    width: 100%;
    min-width: 0;
    flex: none;
  }

  .file-button,
  .ghost-button,
  .icon-button,
  .pane-toggle,
  .zoom-button,
  .closed-pane-button {
    min-height: 38px;
    padding: 0 8px;
    font-size: 0.86rem;
    white-space: nowrap;
  }

  .closed-panes {
    display: none;
    min-height: 38px;
    padding: 0;
    overflow-x: auto;
  }

  .workspace.has-closed .closed-panes {
    display: flex;
  }

  .closed-panes span {
    flex: 0 0 auto;
    font-size: 0.78rem;
  }

  .closed-pane-button {
    flex: 0 0 auto;
  }

  .panes {
    min-height: 0;
  }

  .pane {
    min-height: 0;
    border-radius: 3px;
  }

  .pane-gutter {
    flex-basis: 6px;
    min-height: 6px;
  }

  .pane-header {
    align-items: center;
    flex-direction: row;
    min-height: 40px;
    gap: 6px;
    padding: 6px 8px;
  }

  .pane-header h2 {
    font-size: 0.92rem;
  }

  .pane-header p {
    font-size: 0.74rem;
  }

  .pane-toggle {
    width: 34px;
    min-width: 34px;
    min-height: 34px;
    padding: 0;
  }

  .insert-tools {
    min-height: 38px;
    padding: 5px 8px;
  }

  .insert-control {
    grid-template-columns: auto minmax(0, 1fr);
    gap: 6px;
    font-size: 0.75rem;
  }

  .insert-control select {
    min-height: 32px;
    font-size: 0.82rem;
  }

  textarea {
    padding: 10px;
    font-size: 0.95rem;
    line-height: 1.48;
  }

  #latex-editor {
    font-size: 0.82rem;
    line-height: 1.45;
  }

  .latex-highlight {
    padding: 10px;
    font-size: 0.82rem;
    line-height: 1.45;
  }

  .empty-preview {
    padding: 12px;
    font-size: 0.84rem;
  }

  .pdf-canvas-viewer {
    padding: 10px;
  }

  .pdf-page-stack {
    gap: 10px;
  }

  .reader-dialog {
    inset: 0;
    width: 100vw;
    width: 100dvw;
    height: 100vh;
    height: 100dvh;
    margin: 0;
    border: 0;
    border-radius: 0;
  }

  .reader-bar {
    min-height: 44px;
    align-items: center;
    flex-direction: row;
    padding: 6px 8px;
  }

  .reader-bar span {
    display: none;
  }

  .reader-actions {
    gap: 6px;
  }

  .reader-actions .ghost-button {
    flex: 0 0 auto;
  }

  .page-control {
    min-height: 38px;
    padding: 0 6px;
  }

  .page-control input {
    width: 42px;
  }
}
