/* Page foundation and design tokens */
body.tex-page {
  color-scheme: light;
  --tex-page: #f3f6f9;
  --tex-panel: #ffffff;
  --tex-control: #f7f9fb;
  --tex-editor: #ffffff;
  --tex-text: #172033;
  --tex-muted: #5d6b7d;
  --tex-border: #cbd5e1;
  --tex-border-strong: #aebaca;
  --tex-primary: #086bd8;
  --tex-primary-hover: #075bb6;
  --tex-focus: #1473e6;
  --tex-success: #087a45;
  --tex-warning: #9a5a00;
  --tex-error: #b42318;
  --tex-info-bg: #e8f4ff;
  --tex-message-bg: #f5f7fa;
  --tex-info-text: #07579e;
  --tex-on-primary: #ffffff;
  --tex-gutter: #f1f4f8;
  --tex-current-line-bg: rgba(9, 105, 218, .075);
  --tex-selection-bg: rgba(9, 105, 218, .28);
  --tex-syntax-command: #0550ae;
  --tex-syntax-comment: #6e7781;
  --tex-syntax-math: #8250df;
  --tex-syntax-brace: #cf222e;
  --tex-syntax-match-text: #ffffff;
  --tex-syntax-match-bg: #0969da;
  --tex-preview-bg: #ffffff;
  --tex-privacy-border: #9dd5ff;
  --tex-content-width: 1500px;
  --tex-page-gutter: 32px;
  --tex-panel-gap: 14px;
  --tex-panel-padding: 14px;
  --tex-radius-large: 10px;
  --tex-radius-small: 6px;
  --tex-control-height: 36px;
  --tex-editor-heading-height: 48px;
  --tex-editor-padding: 14px;
  --tex-editor-line-height: 1.65;
  --tex-line-number-width: 54px;
  box-sizing: border-box;
  margin: 0;
  color: var(--tex-text);
  background: var(--tex-page);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.6;
}

.tex-page * {
  box-sizing: border-box;
}

.tex-main {
  width: min(var(--tex-content-width), calc(100% - var(--tex-page-gutter)));
  margin: 0 auto;
  padding: 26px 0 48px;
}

.tex-page-header {
  margin-bottom: 16px;
}

.tex-page-header h1 {
  margin: 8px 0 2px;
  font-size: clamp(24px, 3vw, 34px);
  line-height: 1.25;
}

.tex-page-header p, .tex-panel-heading p {
  margin: 4px 0 0;
  color: var(--tex-muted);
}

.tex-page .breadcrumb {
  display: flex;
  gap: 7px;
  align-items: center;
  font-size: 13px;
}

.tex-page .breadcrumb a {
  color: var(--tex-primary);
}

.tex-page .visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Shared panels */
.tex-notice, .tex-panel {
  border: 1px solid var(--tex-border);
  border-radius: var(--tex-radius-large);
  background: var(--tex-panel);
}

.tex-notice {
  margin-bottom: var(--tex-panel-gap);
  padding: 12px var(--tex-panel-padding);
  color: var(--tex-info-text);
  background: var(--tex-info-bg);
}

.tex-panel {
  margin-bottom: var(--tex-panel-gap);
  overflow: hidden;
}

.tex-panel-heading {
  display: flex;
  gap: 16px;
  align-items: center;
  justify-content: space-between;
  padding: var(--tex-panel-padding);
  border-bottom: 1px solid var(--tex-border);
}

.tex-panel-heading h2, .tex-editor-heading h3, .tex-issues h3, .tex-about h2, .tex-about h3 {
  margin: 0;
}

.tex-panel-heading h2 {
  font-size: 18px;
}

.tex-actions, .tex-toolbar, .tex-source-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

/* Buttons and form controls */
.tex-control, .tex-symbol-palette button {
  border: 1px solid var(--tex-border-strong);
  border-radius: var(--tex-radius-small);
  color: var(--tex-text);
  font: inherit;
}

.tex-control {
  min-height: var(--tex-control-height);
  background: var(--tex-control);
}

.tex-button, .tex-step-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 7px 12px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}

:is(.tex-button, .tex-step-button):hover:not(:disabled) {
  border-color: var(--tex-primary);
}

.tex-button--primary {
  border-color: var(--tex-primary);
  color: var(--tex-on-primary);
  background: var(--tex-primary);
}

.tex-button--primary:hover:not(:disabled) {
  background: var(--tex-primary-hover);
}

.tex-page button:disabled {
  opacity: .48;
  cursor: not-allowed;
}

.tex-page :is(button, select, input, textarea, iframe):focus-visible {
  outline: 2px solid var(--tex-focus);
  outline-offset: 2px;
}

/* Display and file settings */
.tex-toolbar {
  min-height: 56px;
  padding: 9px var(--tex-panel-padding);
  border-bottom: 1px solid var(--tex-border);
  background: var(--tex-control);
}

.tex-toolbar--secondary {
  min-height: 50px;
  background: var(--tex-panel);
}

.tex-setting, .tex-check {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  min-height: var(--tex-control-height);
  color: var(--tex-muted);
}

.tex-select {
  min-width: 140px;
  padding: 6px 34px 6px 10px;
}

.tex-check, .tex-stepper, .tex-range-setting {
  border: 1px solid var(--tex-border);
  border-radius: var(--tex-radius-small);
  background: var(--tex-control);
}

.tex-check {
  padding: 5px 8px;
}

.tex-check input {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--tex-primary);
}

.tex-stepper {
  padding-block: 0;
  padding-inline: 10px 4px;
}

.tex-step-button {
  min-width: 32px;
  padding: 4px;
}

.tex-stepper output {
  min-width: 30px;
  color: var(--tex-text);
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.tex-stepper input[type="range"] {
  width: 72px;
}

.tex-range-setting {
  padding: 0 8px;
}

.tex-range-setting input {
  width: 110px;
}

.tex-range-setting output {
  min-width: 48px;
  color: var(--tex-text);
  font-variant-numeric: tabular-nums;
}

/* Search, replace, and insertion palette */
.tex-source-tools {
  padding: 9px var(--tex-panel-padding);
  border-bottom: 1px solid var(--tex-border);
}

.tex-source-tools label {
  color: var(--tex-muted);
}

.tex-search-input {
  width: min(210px, 100%);
  padding: 6px 9px;
}

.tex-symbol-palette {
  padding: 8px var(--tex-panel-padding);
  border-bottom: 1px solid var(--tex-border);
  background: var(--tex-control);
}

.tex-symbol-palette summary {
  width: fit-content;
  color: var(--tex-muted);
  font-weight: 600;
  cursor: pointer;
}

.tex-symbol-palette > div {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding-top: 8px;
}

.tex-symbol-palette button {
  min-height: 32px;
  padding: 4px 10px;
  background: var(--tex-panel);
  cursor: pointer;
}

.tex-symbol-palette button:hover {
  border-color: var(--tex-primary);
}

/* Editor and preview workspace */
.tex-workspace {
  --tex-font-size: 16px;
  --tex-font-family: "Cascadia Code", Consolas, monospace;
  --tex-editor-height: 680px;
  --tex-split-ratio: 50%;
  display: grid;
  grid-template-columns: var(--tex-split-ratio) minmax(0, 1fr);
  min-height: var(--tex-editor-height);
  background: var(--tex-editor);
}

.tex-workspace:is([data-view-mode="editor"], [data-view-mode="preview"]) {
  grid-template-columns: 1fr;
}

.tex-workspace[data-view-mode="editor"] .tex-preview-pane, .tex-workspace[data-view-mode="preview"] .tex-editor-pane {
  display: none;
}

.tex-editor-pane, .tex-preview-pane {
  min-width: 0;
}

.tex-preview-pane {
  border-inline-start: 1px solid var(--tex-border);
}

.tex-editor-heading {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  height: var(--tex-editor-heading-height);
  padding: 0 var(--tex-panel-padding);
  border-bottom: 1px solid var(--tex-border);
  background: var(--tex-control);
}

.tex-editor-heading h3 {
  font-size: 15px;
}

.tex-editor-heading span {
  color: var(--tex-muted);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tex-source-wrap {
  display: grid;
  grid-template-columns: var(--tex-line-number-width) minmax(0, 1fr);
  height: calc(var(--tex-editor-height) - var(--tex-editor-heading-height));
  overflow: hidden;
  background: var(--tex-editor);
}

.tex-line-number-viewport {
  position: relative;
  z-index: 3;
  height: 100%;
  padding: var(--tex-editor-padding) 10px;
  margin: 0;
  overflow: hidden;
  border-inline-end: 1px solid var(--tex-border);
  color: var(--tex-muted);
  background: var(--tex-gutter);
  font: var(--tex-font-size)/var(--tex-editor-line-height) var(--tex-font-family);
  text-align: end;
  user-select: none;
}

.tex-line-number-viewport code {
  display: block;
  will-change: transform;
}

.tex-editor-stack {
  position: relative;
  min-width: 0;
  height: 100%;
  overflow: hidden;
}

.tex-highlight-viewport, .tex-source-input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  padding: var(--tex-editor-padding);
  margin: 0;
  border: 0;
  border-radius: 0;
  font: var(--tex-font-size)/var(--tex-editor-line-height) var(--tex-font-family);
  tab-size: 4;
  white-space: pre;
}

.tex-highlight-viewport {
  z-index: 1;
  overflow: hidden;
  color: var(--tex-text);
  background: var(--tex-editor);
  pointer-events: none;
}

.tex-highlight-viewport code {
  display: block;
  width: max-content;
  min-width: 100%;
  will-change: transform;
}

.tex-source-input {
  z-index: 2;
  resize: none;
  overflow: auto;
  color: transparent;
  caret-color: var(--tex-text);
  background: linear-gradient(var(--tex-current-line-bg), var(--tex-current-line-bg)) 0 var(--tex-current-line-y, -100px) / 100% calc(var(--tex-font-size) * var(--tex-editor-line-height)) no-repeat;
  -webkit-text-fill-color: transparent;
}

.tex-source-input::selection {
  background: var(--tex-selection-bg);
}

.tex-source-input::placeholder {
  color: var(--tex-muted);
  -webkit-text-fill-color: var(--tex-muted);
}

.tex-word-wrap .tex-highlight-viewport, .tex-word-wrap .tex-source-input {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.tex-word-wrap .tex-highlight-viewport code {
  width: 100%;
}

.tex-token-command {
  color: var(--tex-syntax-command);
  font-weight: 600;
}

.tex-token-comment {
  color: var(--tex-syntax-comment);
  font-style: italic;
}

.tex-token-math {
  color: var(--tex-syntax-math);
}

.tex-token-brace {
  color: var(--tex-syntax-brace);
}

.tex-token-brace--match {
  border-radius: 2px;
  color: var(--tex-syntax-match-text);
  background: var(--tex-syntax-match-bg);
}

.tex-preview-frame {
  display: block;
  width: 100%;
  height: calc(var(--tex-editor-height) - var(--tex-editor-heading-height));
  border: 0;
  background: var(--tex-preview-bg);
}

/* Workspace dark theme */
.tex-workspace[data-theme="dark"] {
  --tex-editor: #22272e;
  --tex-control: #2d333b;
  --tex-text: #e6edf3;
  --tex-muted: #9da7b3;
  --tex-border: #444c56;
  --tex-gutter: #1c2128;
  --tex-syntax-command: #6cb6ff;
  --tex-syntax-comment: #768390;
  --tex-syntax-math: #dcbdfb;
  --tex-syntax-brace: #f47067;
  color-scheme: dark;
}

/* Status, statistics, and diagnostics */
.tex-message {
  min-height: 43px;
  padding: 10px var(--tex-panel-padding);
  margin: 0;
  border-top: 1px solid var(--tex-border);
  color: var(--tex-muted);
  background: var(--tex-message-bg);
}

.tex-message[data-type="success"] {
  color: var(--tex-success);
}

.tex-message[data-type="warning"] {
  color: var(--tex-warning);
}

.tex-message[data-type="error"] {
  color: var(--tex-error);
}

.tex-stats {
  --tex-stats-columns: 6;
  display: grid;
  grid-template-columns: repeat(var(--tex-stats-columns), minmax(0, 1fr));
  margin: 0;
  border-bottom: 1px solid var(--tex-border);
}

.tex-stats div {
  padding: 12px var(--tex-panel-padding);
  border-inline-end: 1px solid var(--tex-border);
}

.tex-stats div:last-child {
  border-inline-end: 0;
}

.tex-stats dt {
  color: var(--tex-muted);
  font-size: 12px;
}

.tex-stats dd {
  margin: 2px 0 0;
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.tex-issues {
  padding: var(--tex-panel-padding);
}

.tex-issues ul {
  display: grid;
  gap: 7px;
  padding: 0;
  margin: 10px 0 0;
  list-style: none;
}

.tex-issue {
  border-inline-start: 4px solid var(--tex-success);
  border-radius: 4px;
  background: var(--tex-control);
}

.tex-issue--warning {
  border-color: var(--tex-warning);
}

.tex-issue--error {
  border-color: var(--tex-error);
}

.tex-issue button, .tex-issue--success {
  width: 100%;
  padding: 8px 10px;
  border: 0;
  color: inherit;
  background: transparent;
  font: inherit;
  text-align: start;
}

.tex-issue button {
  cursor: pointer;
}

.tex-issue button:hover {
  text-decoration: underline;
}

/* Tool description */
.tex-about {
  padding: 22px var(--tex-panel-padding);
}

.tex-about > p {
  color: var(--tex-muted);
}

.tex-about-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 28px;
}

.tex-about li {
  margin: 4px 0;
}

.tex-privacy {
  padding: 10px 12px;
  border: 1px solid var(--tex-privacy-border);
  border-radius: 6px;
  color: var(--tex-info-text) !important;
  background: var(--tex-info-bg);
}

/* Responsive layout */
@media (max-width: 900px) {
  .tex-main {
    width: min(100% - 20px, var(--tex-content-width));
    padding-top: 18px;
  }

  .tex-panel-heading {
    align-items: flex-start;
    flex-direction: column;
  }

  .tex-workspace {
    grid-template-columns: 1fr;
  }

  .tex-preview-pane {
    border-top: 1px solid var(--tex-border);
    border-inline-start: 0;
  }

  .tex-workspace[data-view-mode="split"] .tex-source-wrap, .tex-workspace[data-view-mode="split"] .tex-preview-frame {
    height: max(360px, calc(var(--tex-editor-height) / 2));
  }

  .tex-stats {
    --tex-stats-columns: 3;
  }

  .tex-stats div:nth-child(3n) {
    border-inline-end: 0;
  }

  .tex-stats div:nth-child(-n + 3) {
    border-bottom: 1px solid var(--tex-border);
  }

  .tex-about-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

}

@media (max-width: 560px) {
  .tex-page-header h1 {
    font-size: 24px;
  }

  .tex-actions, .tex-actions > *, .tex-button {
    width: 100%;
  }

  .tex-toolbar, .tex-source-tools {
    align-items: stretch;
    flex-direction: column;
  }

  .tex-setting, .tex-check, .tex-select, .tex-search-input {
    width: 100%;
  }

  .tex-stepper, .tex-range-setting {
    justify-content: space-between;
  }

  .tex-stats {
    --tex-stats-columns: 2;
  }

  .tex-stats div:nth-child(3) {
    border-inline-end: 1px solid var(--tex-border);
  }

  .tex-stats div:nth-child(even) {
    border-inline-end: 0;
  }

  .tex-stats div:nth-child(-n + 4) {
    border-bottom: 1px solid var(--tex-border);
  }

}
