/* lightmetal.css — default styles for the lightmetal editor.
 *
 * Every rule is scoped under .lightmetal. Theme via the --lightmetal-*
 * custom properties. Pin a theme on the element with .lightmetal-dark or
 * .lightmetal-light; without a pin, follow data-lightmetal-theme or the system.
 *
 * Force dark:      <div class="lightmetal lightmetal-dark">
 * Force light:     <div class="lightmetal lightmetal-light">
 * Follow system:   <div class="lightmetal">
 */

/* This small contract also ships in polish.css; neither stylesheet needs the other. */
[data-lightmetal-theme="light"] { --lightmetal-color-scheme: light; color-scheme: light; }
[data-lightmetal-theme="dark"] { --lightmetal-color-scheme: dark; color-scheme: dark; }
[data-lightmetal-theme="auto"] { --lightmetal-color-scheme: light dark; color-scheme: light dark; }

.lightmetal {
  color-scheme: var(--lightmetal-color-scheme, light dark);
  --lightmetal-bg: light-dark(#ffffff, #16181c);
  --lightmetal-bg-toolbar: light-dark(#f7f8fa, #1e2126);
  --lightmetal-border: light-dark(#e3e6eb, #2c313a);
  --lightmetal-color: light-dark(#24292f, #e6e9ee);
  --lightmetal-color-dim: light-dark(#6a737d, #9aa4b2);
  --lightmetal-hover: light-dark(#edf0f4, #262b33);
  --lightmetal-accent: light-dark(#556ee7, #8fa0ff);
  --lightmetal-accent-soft: light-dark(rgba(85, 110, 231, 0.12), rgba(143, 160, 255, 0.18));
  --lightmetal-selection-bg: light-dark(#c4e3eb, #315663);
  --lightmetal-selection-color: var(--lightmetal-color);
  --lightmetal-error-fg: light-dark(#b42318, #ffb4ad);
  --lightmetal-error-bg: light-dark(#fdecea, rgba(255, 99, 99, 0.16));

  border: 1px solid var(--lightmetal-border);
  border-radius: 10px;
  background-color: var(--lightmetal-bg);
  color: var(--lightmetal-color);
  box-sizing: border-box;
  overflow: hidden;
}

/* Keep the same positioned block in both layouts. Switching to fixed/absolute
 * positioning or rebuilding scroll frames can reset Firefox textarea Undo. */
.lightmetal.lightmetal-html { position: relative; }
.lightmetal.lightmetal-html[hidden] { display: none; }

/* In-page fullscreen keeps the writing nodes and their native history intact. */
/* clip locks page scrolling without rebuilding Firefox's root scroll frame. */
html.lightmetal-page-fullscreen { overflow: clip; }
.lightmetal.lightmetal-fullscreen {
  /* Override embedded sizing without rewriting application-owned inline styles. */
  z-index: 2147483646;
  translate: var(--lightmetal-fullscreen-x, 0px) var(--lightmetal-fullscreen-y, 0px);
  width: 100vw !important;
  height: 100dvh !important;
  min-width: 0 !important;
  min-height: 0 !important;
  max-width: none !important;
  max-height: none !important;
  margin: 0 !important;
  border-radius: 0;
  background: var(--lightmetal-bg);
}
.lightmetal.lightmetal-fullscreen > .lightmetal-fullscreen-surface {
  width: 100%;
  height: var(--lightmetal-fullscreen-writing-height, 300px) !important;
  min-height: 0;
  overflow: auto;
}
.lightmetal.lightmetal-fullscreen > .lightmetal-fullscreen-surface[hidden] { display: none; }

/* Existing host-level pins also pass their mode to nested highlighted code. */
.lightmetal.lightmetal-light { --lightmetal-color-scheme: light; }
.lightmetal.lightmetal-dark { --lightmetal-color-scheme: dark; }

/* Toolbar. */

.lightmetal .lightmetal-actionbar {
  display: flex;
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  gap: 2px;
  padding: 6px;
  background-color: var(--lightmetal-bg-toolbar);
  border-bottom: 1px solid var(--lightmetal-border);
}

.lightmetal .lightmetal-toolbar-separator,
.lightmetal .lightmarkdown-separator {
  flex: 0 0 1px;
  width: 1px;
  height: 18px;
  align-self: center;
  margin: 0 4px;
  background: var(--lightmetal-border);
  opacity: .65;
}
.lightmetal .lightmetal-toolbar-separator[hidden] { display: none; }

.lightmetal .lightmetal-button {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 30px;
  padding: 0;
  border: none;
  border-radius: 6px;
  background-color: transparent;
  color: var(--lightmetal-color-dim);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease;
}

.lightmetal .lightmetal-button:hover,
.lightmetal .lightmetal-table-button:hover:not(:disabled) {
  background-color: var(--lightmetal-hover);
  color: var(--lightmetal-color);
}

.lightmetal .lightmetal-button:focus-visible,
.lightmetal .lightmetal-table-button:focus-visible {
  outline: 2px solid var(--lightmetal-accent);
  outline-offset: -2px;
}

.lightmetal .lightmetal-button-selected {
  background-color: var(--lightmetal-accent-soft);
  color: var(--lightmetal-accent);
}

.lightmetal .lightmetal-button svg {
  display: block;
  /* CSS sizes the icons; the 15px in the markup is a fallback. */
  width: 18px;
  height: 18px;
}

/* HTML-only refresh. MaiDEn retains its existing toolbar geometry. */
.lightmetal.lightmetal-html .lightmetal-actionbar { overflow: hidden; align-items: center; min-width: 0; }
.lightmetal.lightmetal-html .lightmetal-actionbar > .lightmetal-button { width: 36px; height: 36px; font-size: 18px; }
.lightmetal.lightmetal-html .lightmetal-button svg { width: 20px; height: 20px; }
.lightmetal.lightmetal-html .lightmetal-button[hidden] { display: none; }
.lightmetal.lightmetal-html .lightmetal-actionbar > .lightmetal-overflow-trigger { flex-shrink: 1; min-width: 0; }
.lightmetal.lightmetal-html .lightmetal-actionbar > .lightmetal-group-start { position: relative; margin-left: 8px; }
.lightmetal.lightmetal-html .lightmetal-actionbar > .lightmetal-group-start::before { content: ''; position: absolute; left: -5px; height: 18px; border-left: 1px solid var(--lightmetal-border); }
.lightmetal.lightmetal-html .lightmetal-overflow .lightmetal-group-start { margin-left: 0; }
.lightmetal.lightmetal-html .lightmetal-actionbar > .lightmetal-button[aria-haspopup]:not(.lightmetal-overflow-trigger)::after { content: ''; width: 4px; height: 4px; margin-left: 3px; border-right: 1px solid currentColor; border-bottom: 1px solid currentColor; transform: rotate(45deg); }
.lightmetal.lightmetal-html .lightmetal-button[aria-expanded="true"] { background: var(--lightmetal-accent-soft); color: var(--lightmetal-accent); }
.lightmetal.lightmetal-html .lightmetal-table-menu button { min-height: 36px; }
.lightmetal.lightmetal-html .lightmetal-format-popup button > span { display: inline-flex; vertical-align: middle; margin-right: 10px; }
.lightmetal.lightmetal-html .lightmetal-format-popup button svg { width: 20px; height: 20px; }
.lightmetal.lightmetal-html .lightmetal-menu-label { padding: 10px; border-top: 1px solid var(--lightmetal-border); color: var(--lightmetal-color-dim); }
.lightmetal.lightmetal-html .lightmetal-emoji-grid { display: grid; grid-template-columns: repeat(4,minmax(0,1fr)); }
.lightmetal.lightmetal-html .lightmetal-emoji-grid button { font-size: 22px; text-align: center; }
.lightmetal.lightmetal-html .lightmetal-button:disabled { opacity: .4; cursor: default; }
.lightmetal.lightmetal-html .lightmetal-button:disabled:hover { background: transparent; color: var(--lightmetal-color-dim); }
.lightmetal.lightmetal-html .lightmetal-overflow .lightmetal-button { width: 100%; height: 36px; justify-content: flex-start; gap: 10px; }
.lightmetal.lightmetal-html .lightmetal-overflow .lightmetal-button::after { content: attr(aria-label); font-size: 14px; }
@media (pointer: coarse) {
  .lightmetal.lightmetal-html .lightmetal-actionbar > .lightmetal-button { width: 44px; height: 44px; }
  .lightmetal.lightmetal-html :is(.lightmetal-table-menu,.lightmetal-overflow) button { min-height: 44px; }
}

/* Link and Image forms share the toolbar's local theme and popover surface. */
.lightmetal .lightmetal-table-dropdown.lightmetal-insertion-popup { width: 320px; font-size: 14px; }
.lightmetal .lightmetal-insertion-popup h3 { margin: 0 0 12px; font-size: 15px; }
.lightmetal .lightmetal-insertion-popup label { display: grid; gap: 5px; margin: 10px 0; }
.lightmetal .lightmetal-insertion-popup input[type="text"] { box-sizing: border-box; width: 100%; min-width: 0; min-height: 36px; padding: 7px 9px; border: 1px solid var(--lightmetal-border); border-radius: 5px; background: var(--lightmetal-bg); color: inherit; font: inherit; }
.lightmetal .lightmetal-insertion-popup .lightmetal-insertion-check { display: flex; align-items: center; gap: 8px; }
.lightmetal .lightmetal-insertion-popup button { min-height: 36px; padding: 7px 10px; border: 1px solid var(--lightmetal-border); border-radius: 5px; background: var(--lightmetal-bg-toolbar); color: inherit; font: inherit; cursor: pointer; }
.lightmetal .lightmetal-insertion-popup button:enabled:hover,
.lightmetal .lightmetal-insertion-popup button[aria-selected="true"] { background: var(--lightmetal-accent-soft); }
.lightmetal .lightmetal-insertion-popup :is(button,input):focus-visible { outline: 2px solid var(--lightmetal-accent); outline-offset: -2px; }
.lightmetal .lightmetal-insertion-popup button:disabled { opacity: .45; cursor: default; }
.lightmetal .lightmetal-insertion-footer { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; margin-top: 14px; }
.lightmetal .lightmetal-insertion-tabs { display: flex; gap: 6px; margin-bottom: 12px; }
.lightmetal .lightmetal-insertion-popup .lightmetal-image-dropzone { width: 100%; min-height: 110px; padding: 20px; border-style: dashed; }
.lightmetal .lightmetal-insertion-error { margin: 10px 0 0; color: var(--lightmetal-color); font-weight: 600; }
.lightmetal .lightmetal-insertion-popup [hidden] { display: none; }
@media (pointer: coarse) {
  .lightmetal .lightmetal-insertion-popup :is(button,input[type="text"]) { min-height: 44px; }
}

/* Table insertion dropdown. Popover support lifts it above clipped hosts. */

.lightmetal :is(.lightmetal-table-dropdown,.lightmetal-toolbar-popup) {
  position: fixed;
  inset: auto;
  margin: 0;
  z-index: 1000;
  box-sizing: border-box;
  width: 270px;
  padding: 12px;
  overflow: auto;
  border: 1px solid var(--lightmetal-border);
  border-radius: 8px;
  background: var(--lightmetal-bg);
  color: var(--lightmetal-color);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.18);
  font: inherit;
  font-size: 13px;
}

.lightmetal :is(.lightmetal-table-menu,.lightmetal-overflow) { width: 240px; padding: 6px; }
.lightmetal :is(.lightmetal-table-menu,.lightmetal-overflow) button {
  display: block;
  box-sizing: border-box;
  width: 100%;
  padding: 7px 10px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.lightmetal :is(.lightmetal-table-menu,.lightmetal-overflow) button:enabled:hover,
.lightmetal :is(.lightmetal-table-menu,.lightmetal-overflow) button:focus-visible { background: var(--lightmetal-accent-soft); }
.lightmetal :is(.lightmetal-table-menu,.lightmetal-overflow) button:disabled { opacity: .4; cursor: default; }
.lightmetal :is(.lightmetal-table-menu,.lightmetal-overflow) button[aria-checked="true"] { background: var(--lightmetal-accent-soft); font-weight: 600; }
.lightmetal :is(.lightmetal-table-menu,.lightmetal-overflow) [role="separator"] { margin: 5px 4px; border-top: 1px solid var(--lightmetal-border); }

.lightmetal-format-popup label { display: grid; gap: 5px; padding: 8px; }
.lightmetal-format-popup select { width: 100%; min-height: 32px; font: inherit; color: inherit; background: var(--lightmetal-bg); border: 1px solid var(--lightmetal-border); border-radius: 4px; }
.lightmetal-format-popup .lightmetal-color-field { display: flex; align-items: center; gap: 4px; padding: 5px; }
.lightmetal-format-popup input[type="color"] { flex: 0 0 40px; width: 40px; height: 32px; padding: 2px; border: 1px solid var(--lightmetal-border); background: var(--lightmetal-bg); }
.lightmetal-format-popup :is(button,select,input):focus-visible { outline: 2px solid var(--lightmetal-accent); outline-offset: -2px; }

.lightmetal :is(.lightmetal-table-dropdown,.lightmetal-toolbar-popup)[hidden] {
  display: none;
}

.lightmetal .lightmetal-table-dimensions {
  margin-bottom: 8px;
  font-weight: 600;
}

.lightmetal .lightmetal-table-grid {
  display: grid;
  gap: 3px;
  margin-bottom: 12px;
}

.lightmetal .lightmetal-table-grid button {
  aspect-ratio: 1;
  min-width: 0;
  padding: 0;
  border: 1px solid var(--lightmetal-border);
  border-radius: 2px;
  background: var(--lightmetal-bg-toolbar);
  cursor: pointer;
}

.lightmetal .lightmetal-table-grid .lightmetal-grid-selected {
  border-color: var(--lightmetal-accent);
  background: var(--lightmetal-accent-soft);
}

.lightmetal .lightmetal-table-form {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 8px;
}

.lightmetal .lightmetal-table-form label {
  flex: 1;
  min-width: 65px;
}

.lightmetal .lightmetal-table-form input {
  box-sizing: border-box;
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 5px;
  border: 1px solid var(--lightmetal-border);
  border-radius: 4px;
  background: var(--lightmetal-bg);
  color: var(--lightmetal-color);
  font: inherit;
}

.lightmetal .lightmetal-table-form button {
  padding: 6px 10px;
  border: none;
  border-radius: 4px;
  background: var(--lightmetal-accent);
  color: var(--lightmetal-bg);
  font: inherit;
  cursor: pointer;
}

.lightmetal .lightmetal-table-dropdown button:focus-visible,
.lightmetal .lightmetal-table-dropdown input:focus-visible {
  outline: 2px solid var(--lightmetal-accent);
  outline-offset: 2px;
}

.lightmetal .lightmetal-table-message {
  margin-top: 8px;
  color: var(--lightmetal-error-fg);
}

.lightmetal .lightmetal-table-message:empty {
  display: none;
}

/* Contextual table controls stay outside the saved HTML. */

.lightmetal .lightmetal-tablebar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  padding: 6px;
  background-color: var(--lightmetal-bg-toolbar);
  border-bottom: 1px solid var(--lightmetal-border);
}

.lightmetal .lightmetal-tablebar[hidden] {
  display: none;
}

.lightmetal .lightmetal-table-button {
  padding: 5px 8px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--lightmetal-color-dim);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

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

.lightmetal .lightmetal-table-note {
  color: var(--lightmetal-color-dim);
  font-size: 12px;
}

/* WYSIWYG content. */

.lightmetal .lightmetal-content {
  box-sizing: border-box;
  height: 300px;
  overflow-y: auto;
  outline: none;
  padding: 12px 14px;
  background-color: var(--lightmetal-bg);
  color: var(--lightmetal-color);
  scrollbar-width: thin;
}

/* Tables use the editor theme. Wide tables scroll with the content. */

.lightmetal .lightmetal-content table {
  border-collapse: collapse;
  margin: 0.75em 0;
}

.lightmetal .lightmetal-content td,
.lightmetal .lightmetal-content th {
  border: 1px solid var(--lightmetal-border);
  padding: 6px 10px;
  min-width: 40px;
  vertical-align: top;
}

.lightmetal .lightmetal-content th {
  background-color: var(--lightmetal-bg-toolbar);
  text-align: left;
}

.lightmetal .lightmetal-content caption {
  padding: 6px 0;
  color: var(--lightmetal-color-dim);
  text-align: left;
}

/* Source-mode textarea. Same box as the content so the height does not
 * jump when the mode toggles. */

.lightmetal .lightmetal-sources {
  box-sizing: border-box;
  display: block;
  width: 100%;
  height: 300px;
  padding: 12px 14px;
  border: none;
  outline: none;
  resize: none;
  /* Keep the scroll frame stable when fullscreen changes the field's height. */
  overflow: auto;
  font-family: ui-monospace, "Cascadia Mono", "SF Mono", Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
  background-color: var(--lightmetal-bg);
  color: var(--lightmetal-color);
}

.lightmetal .lightmetal-sources[hidden] {
  display: none;
}

/* HTML editing fields are direct children; MaiDEn owns its stage outline. */
.lightmetal > .lightmetal-content:focus-visible,
.lightmetal > .lightmetal-sources:focus-visible {
  outline: 2px solid var(--lightmetal-accent);
  outline-offset: -2px;
}

/* Image drop highlight. */

.lightmetal .lightmetal-drop {
  outline: 2px dashed var(--lightmetal-accent);
  outline-offset: -6px;
}

/* Inline error note shown in the toolbar. */

.lightmetal .lightmetal-error {
  margin-left: auto;
  padding: 3px 10px;
  font-size: 12px;
  line-height: 1.4;
  border-radius: 6px;
  color: var(--lightmetal-error-fg);
  background-color: var(--lightmetal-error-bg);
}
