:root {
  color-scheme: light;
  font-family: system-ui, sans-serif;

  --bg: #f7f8fa;
  --text: #1a1a1a;
  --text-muted: #666666;
  --border: #dddddd;
  --border-strong: #bbbbbb;
  --control-bg: #eceef2;
  --control-bg-hover: #e1e4e9;
  --error: #b00020;
  --field-bg: #ffffff;

  --title-math: #2979ff;
  --title-link: #ffd600;
  --title-morph: #ff00c8;

  --field-border: rgba(41, 121, 255, 0.45);
  --field-border-focus: #2979ff;
  --field-glow: rgba(41, 121, 255, 0.45);
  --field-selection-bg: rgba(41, 121, 255, 0.35);
  --field-contains-bg: rgba(41, 121, 255, 0.18);

  /* Light mode gets a slightly lighter header bar than dark mode, so it reads less harshly against the white canvas. */
  --header-bg: #2b2f38;
  --header-border: #3a3f49;
  --header-text: #e8e8ea;
  --header-control-bg: #343842;
  --header-control-bg-hover: #3f4450;
}

:root[data-theme='dark'] {
  color-scheme: dark;

  --bg: #14161a;
  --text: #e8e8ea;
  --text-muted: #9aa0a6;
  --border: #333942;
  --border-strong: #454c56;
  --control-bg: #262a33;
  --control-bg-hover: #323844;
  --error: #ff6b6b;
  --field-bg: #1b1e24;

  --header-bg: #1c1f26;
  --header-border: #333942;
  --header-control-bg: #262a33;
  --header-control-bg-hover: #323844;
}

body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
}

header {
  background: var(--header-bg);
  border-bottom: 1px solid var(--header-border);
}

header .header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem;
}

.title-group {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.title-group .logo {
  flex: none;
  display: block;
  position: relative;
  top: 5px;
}

header h1 {
  margin: 0;
  font-size: 1.9rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  filter: drop-shadow(0 0 6px rgba(255, 0, 200, 0.35));
}

.t-math {
  color: var(--title-math);
}

.t-link {
  color: var(--title-link);
}

.t-morph {
  color: var(--title-morph);
}

.header-menu-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  padding: 0.5rem 0.9rem;
  border: none;
  border-radius: 0.4rem;
  background: none;
  color: var(--header-text);
  font-size: 0.9rem;
  text-align: left;
  cursor: pointer;
}

.header-menu-item:hover {
  background: var(--header-control-bg-hover);
}

.header-menu-icon {
  flex: none;
  width: 1.1rem;
  height: 1.1rem;
  color: var(--header-text);
}

.header-menu-divider {
  height: 1px;
  margin: 0.3rem 0.2rem;
  background: var(--header-border);
}

.morph-menu {
  position: fixed;
  z-index: 1000;
  min-width: 10rem;
  max-height: calc(100vh - 16px);
  overflow-y: auto;
  margin: 0;
  padding: 0.35rem;
  list-style: none;
  border: 1px solid var(--border-strong);
  border-radius: 0.5rem;
  background: var(--field-bg);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  box-sizing: border-box;
}

.morph-menu[hidden] {
  display: none;
}

.morph-menu-divider {
  height: 1px;
  margin: 0.3rem 0.2rem;
  background: var(--border-strong);
}

.morph-menu-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.4rem 0.8rem;
  border: none;
  border-radius: 0.4rem;
  background: none;
  color: var(--text);
  font-size: 0.9rem;
  text-align: left;
  cursor: pointer;
}

.morph-menu-item:hover {
  background: var(--field-border-focus);
  color: #ffffff;
}

/* Reserved before every row's label (see createMenuIconSlot in app.js) so every row lines up at
   the same indent, whether or not it actually has an icon in it. */
.morph-menu-icon-slot {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 1.1rem;
  height: 1.1rem;
}

.morph-menu-icon {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.morph-submenu-toggle {
  position: relative;
  padding-right: 1.6rem;
}

.morph-submenu-toggle::after {
  content: '\203a';
  position: absolute;
  right: 0.8rem;
  top: 50%;
  transform: translateY(-50%);
  font-size: 1rem;
}

.morph-submenu-toggle[aria-expanded='true']::after {
  content: '\2039';
}

.morph-submenu {
  list-style: none;
  margin: 0;
  padding: 0 0 0 0.9rem;
}

.morph-submenu[hidden] {
  display: none;
}

.morph-submenu-checkable .morph-menu-item {
  position: relative;
  padding-left: 1.7rem;
}

.morph-submenu-checkable .morph-menu-item-checked::before {
  content: '\2713';
  position: absolute;
  left: 0.6rem;
  top: 50%;
  transform: translateY(-50%);
}

.morph-submenu-heading {
  padding: 0.4rem 0.8rem 0.1rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

/* Spacing for the math-template previews reused from MathLive's own "Insert" menu (see
   mathlive-static.css, linked in index.html, for the KaTeX classes rendering the previews
   themselves) - these two classes are the only pieces of that layout it doesn't already cover. */
.ML__insert-template {
  display: inline-block;
  min-width: 2.5rem;
  margin-right: 0.6rem;
}

.ML__insert-label {
  color: var(--text-muted);
}

.morph-menu-item:hover .ML__insert-label {
  color: #ffffff;
}

.matrix-picker-label {
  padding: 0.3rem 0.8rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.matrix-picker-grid {
  display: grid;
  grid-template-columns: repeat(5, 1.1rem);
  grid-template-rows: repeat(5, 1.1rem);
  gap: 2px;
  padding: 0.2rem 0.8rem 0.4rem;
}

.matrix-picker-cell {
  border: 1px solid var(--border-strong);
  border-radius: 0.15rem;
  background: var(--control-bg);
  cursor: pointer;
}

.matrix-picker-cell.active {
  background: var(--field-border-focus);
  border-color: var(--field-border-focus);
}

.header-menu {
  position: relative;
}

.header-menu-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  border: 1px solid var(--header-border);
  border-radius: 0.4rem;
  background: var(--header-control-bg);
  color: var(--header-text);
  cursor: pointer;
}

.header-menu-toggle:hover {
  background: var(--header-control-bg-hover);
}

.header-menu-dropdown {
  position: absolute;
  top: calc(100% + 0.4rem);
  right: 0;
  z-index: 10;
  min-width: 10rem;
  margin: 0;
  padding: 0.35rem;
  list-style: none;
  border: 1px solid var(--header-border);
  border-radius: 0.5rem;
  background: var(--header-bg);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

.header-menu-dropdown[hidden] {
  display: none;
}

main {
  max-width: 90rem;
  margin: 0 auto;
  padding: 2rem 2rem;
  box-sizing: border-box;
}

.hint {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text-muted);
  font-size: 1.1rem;
  line-height: 1.5;
  margin-top: 0;
}

/* `.hint`'s own "display: flex" above is an author-stylesheet rule, which (regardless of
   specificity) always wins the cascade over the browser's built-in "[hidden] { display: none }"
   UA rule - without this, setting the "hidden" attribute on a .hint element (see examples-hint)
   would have no visible effect at all. */
.hint[hidden] {
  display: none;
}

.hint-icon {
  flex: none;
  width: 1.5em;
  height: 1.5em;
}

.hint-morph {
  color: var(--title-morph);
}

.hint-examples {
  font-size: 0.95rem;
  font-style: italic;
  /* No icon of its own - indent past the primary hint's icon + gap so the two lines of text
     still line up. */
  padding-left: calc(1.5em + 0.5rem);
  margin-top: 0.3rem;
}

math-field {
  font-size: 1.4rem;
  padding: 0.75rem;
  border: 1px solid var(--field-border);
  border-radius: 0.5rem;
  box-sizing: border-box;
  background: var(--field-bg);
  color: var(--text);
  outline: none;
  max-width: 100%;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  --selection-background-color: var(--field-selection-bg);
  --selection-color: var(--text);
  --contains-highlight-background-color: var(--field-contains-bg);
  --primary: var(--field-border-focus);
}

math-field:focus-within {
  border: 1.5px solid var(--field-border-focus);
  box-shadow: 0 0 0 2px rgba(41, 121, 255, 0.25), 0 0 8px rgba(41, 121, 255, 0.3);
}

math-field::part(virtual-keyboard-toggle),
math-field::part(menu-toggle) {
  color: var(--field-border-focus);
}

/* Our own right-click menu fully replaces MathLive's own field menu (see openFieldMenu in
   app.js), and our own keyboard-toggle-btn/field-menu-btn (below .doc-line) fully replace its
   virtual keyboard toggle - hide both of MathLive's now-unused in-field buttons. */
math-field::part(menu-toggle),
math-field::part(virtual-keyboard-toggle) {
  display: none;
}

math-field::part(menu-item):hover {
  background: var(--field-border-focus);
  color: #ffffff;
}

.document {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: 0.6rem;
  background: var(--control-bg);
}

.doc-line {
  display: flex;
  align-items: stretch;
  gap: 0.5rem;
}

.doc-line math-field {
  flex: 1;
  min-width: 0;
}

/* Delete (top), Board mode (middle), and the keyboard/menu toggle (bottom) are spread across
   the field's full height - exactly 3 items are ever visible at once (keyboard-toggle-btn and
   field-menu-btn are mutually exclusive via the media query below), so space-between lands the
   middle one dead center regardless of row height. */
.doc-line-actions {
  flex: none;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 0.35rem;
}

.doc-line .delete-line,
.doc-line .keyboard-toggle-btn,
.doc-line .field-menu-btn,
.doc-line .board-mode-btn {
  flex: none;
  width: 1.75rem;
  height: 1.75rem;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border-strong);
  border-radius: 0.4rem;
  background: var(--field-bg);
  color: var(--text-muted);
  cursor: pointer;
}

.doc-line .delete-line svg,
.doc-line .keyboard-toggle-btn svg,
.doc-line .field-menu-btn svg,
.doc-line .board-mode-btn svg {
  width: 65%;
  height: 65%;
}

.doc-line .delete-line:hover {
  background: var(--error);
  border-color: var(--error);
  color: #ffffff;
}

.doc-line .keyboard-toggle-btn:hover,
.doc-line .field-menu-btn:hover,
.doc-line .board-mode-btn:hover {
  background: var(--field-border-focus);
  border-color: var(--field-border-focus);
  color: #ffffff;
}

/* Our own replacements for MathLive's in-field buttons: the keyboard toggle for desktop/mouse
   use, and the menu button as a touch-device fallback for opening the field menu (see
   createLine in app.js) - right-click already does that job on desktop. */
.doc-line .field-menu-btn {
  display: none;
}

@media (hover: none) and (pointer: coarse) {
  .doc-line .keyboard-toggle-btn {
    display: none;
  }

  .doc-line .field-menu-btn {
    display: inline-flex;
  }
}

.document-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.add-line {
  align-self: flex-start;
  padding: 0.4rem 0.8rem;
  border: 1px dashed var(--border-strong);
  border-radius: 0.4rem;
  background: none;
  color: var(--text-muted);
  cursor: pointer;
}

.add-line:hover {
  background: var(--control-bg);
}

.app-dialog {
  padding: 1.25rem;
  border: 1px solid var(--border-strong);
  border-radius: 0.5rem;
  background: var(--field-bg);
  color: var(--text);
  min-width: 16rem;
}

.app-dialog::backdrop {
  background: rgba(0, 0, 0, 0.5);
}

.app-dialog h2 {
  margin: 0 0 1rem;
  font-size: 1.05rem;
}

.app-dialog-field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.app-dialog-field input {
  width: 5rem;
  padding: 0.3rem;
  border: 1px solid var(--border-strong);
  border-radius: 0.3rem;
  text-align: center;
  color: var(--text);
  background: var(--control-bg);
}

.app-dialog-field input:focus {
  outline: none;
  border-color: var(--field-border-focus);
  box-shadow: 0 0 0 2px rgba(41, 121, 255, 0.25);
}

.app-dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1rem;
}

.app-dialog-actions button {
  padding: 0.4rem 0.9rem;
  border: 1px solid var(--border-strong);
  border-radius: 0.4rem;
  background: none;
  color: var(--text);
  cursor: pointer;
}

.app-dialog-actions button:hover {
  background: var(--control-bg);
}

.app-dialog-actions button:focus {
  outline: none;
  border-color: var(--field-border-focus);
  box-shadow: 0 0 0 2px rgba(41, 121, 255, 0.25);
}

.app-dialog-actions .app-dialog-primary {
  border-color: var(--field-border-focus);
  background: var(--field-border-focus);
  color: #ffffff;
}

/* A snippet's name can be much longer than a single variable/number, so (unlike Series' fields)
   it gets its own full-width row instead of the compact label-left/input-right layout. */
.snippet-dialog {
  min-width: 22rem;
}

.snippet-dialog .app-dialog-field {
  flex-direction: column;
  align-items: stretch;
}

.snippet-dialog .app-dialog-field input {
  width: 100%;
  text-align: left;
}

.manage-snippets-dialog {
  width: 32rem;
  max-width: 90vw;
  max-height: 80vh;
}

.manage-snippets-dialog[open] {
  display: flex;
  flex-direction: column;
}

.manage-snippets-list {
  overflow-y: auto;
  flex: 1;
  margin: 0 -0.25rem;
  padding: 0 0.25rem;
}

.manage-snippets-empty {
  color: var(--text-muted);
  margin: 0;
  padding: 1rem 0;
}

.manage-snippet-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
}

.manage-snippet-row:last-child {
  border-bottom: none;
}

.manage-snippet-preview {
  flex: none;
  width: 9rem;
  padding: 0.3rem 0.4rem;
  border: 1px solid var(--border);
  border-radius: 0.3rem;
  background: none;
  font-size: 0.75rem;
  pointer-events: none;
}

.manage-snippet-preview:focus-within {
  border: 1px solid var(--border);
  box-shadow: none;
}

.manage-snippet-preview::part(virtual-keyboard-toggle) {
  display: none;
}

.manage-snippet-name {
  flex: 1;
  min-width: 0;
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border-strong);
  border-radius: 0.3rem;
  color: var(--text);
  background: var(--control-bg);
}

.manage-snippet-name:focus {
  outline: none;
  border-color: var(--field-border-focus);
  box-shadow: 0 0 0 2px rgba(41, 121, 255, 0.25);
}

.manage-snippet-delete {
  flex: none;
  padding: 0.35rem 0.7rem;
  border: 1px solid var(--border-strong);
  border-radius: 0.3rem;
  background: none;
  color: var(--text-muted);
  cursor: pointer;
}

.manage-snippet-delete:hover {
  background: var(--error);
  border-color: var(--error);
  color: #ffffff;
}

.about-dialog {
  max-width: 24rem;
}

.about-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.about-logo {
  flex: none;
  width: 2.5rem;
  height: 2.5rem;
}

.about-header h2 {
  margin: 0;
  font-size: 1.4rem;
  font-weight: 800;
  letter-spacing: 0.02em;
}

.about-description {
  margin: 0 0 1rem;
  color: var(--text);
  line-height: 1.5;
}

.about-copyright {
  margin: 0 0 0.25rem;
  color: #ffffff;
  font-size: 0.85rem;
  font-weight: bold;
}

.about-link {
  margin: 0 0 1rem;
  font-size: 0.85rem;
}

.about-link a {
  color: var(--field-border-focus);
}

.about-startup-check {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  color: var(--text-muted);
  cursor: pointer;
}

.about-startup-check input {
  margin: 0;
  accent-color: var(--field-border-focus);
  cursor: pointer;
}

.help-dialog {
  max-width: 28rem;
}

.help-shortcuts {
  list-style: none;
  margin: 0 0 1rem;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}

.help-shortcuts li {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
}

.help-shortcuts code {
  flex: none;
  padding: 0.15rem 0.45rem;
  border: 1px solid var(--border-strong);
  border-radius: 0.3rem;
  background: var(--control-bg);
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 0.8rem;
  white-space: nowrap;
  color: var(--text);
}

.help-shortcuts span {
  color: var(--text-muted);
  font-size: 0.9rem;
  line-height: 1.4;
}

/* Fills as much of the viewport as reasonably possible (rather than .app-dialog's small
   content-hugging min-width) so the equation inside can be scaled up to the largest size that
   still fits without clipping - see fitBoardField in app.js. */
.board-dialog {
  position: relative;
  box-sizing: border-box;
  width: 96vw;
  height: 92vh;
  max-width: 96vw;
  max-height: 92vh;
  padding: 1rem;
}

/* display must stay scoped to [open] - setting it unconditionally on .board-dialog would
   override the browser's own default `dialog:not([open]) { display: none }` rule (author
   styles always win over UA styles, regardless of selector specificity), making the dialog
   visible even while closed. */
.board-dialog[open] {
  display: flex;
  flex-direction: column;
}

.board-dialog-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  z-index: 1;
  width: 2.5rem;
  height: 2.5rem;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border-strong);
  border-radius: 0.4rem;
  background: var(--field-bg);
  color: var(--text-muted);
  cursor: pointer;
}

.board-dialog-close svg {
  width: 60%;
  height: 60%;
}

.board-dialog-close:hover {
  background: var(--error);
  border-color: var(--error);
  color: #ffffff;
}

.board-content {
  flex: 1;
  min-height: 0;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

/* Read-only and non-interactive - this is purely a big display, not an editable field, so it
   gets none of the usual math-field chrome (border/background/padding/focus ring). */
.board-content math-field {
  pointer-events: none;
  border: none;
  box-shadow: none;
  padding: 0;
  background: none;
}

.board-content math-field:focus-within {
  border: none;
  box-shadow: none;
}

#status {
  min-height: 1.2em;
  color: var(--text-muted);
}

#status.error {
  color: var(--error);
}
