:root {
  --fg: #1f2328;
  --muted: #6e7781;
  --faint: #afb8c1;
  --line: #d0d7de;
  --soft: #f6f8fa;
  --accent: #0969da;
  color-scheme: light;
}

[hidden] { display: none !important; }
* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  display: flex;
  flex-direction: column;
  background: #fff;
  color: var(--fg);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}

button {
  font: inherit;
  color: var(--fg);
  background: none;
  border: 0;
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
}

button:hover { background: var(--soft); }
button.is-active { color: var(--accent); background: #ddf4ff; }


/* Top bar and tabs */
.topbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 12px 0 16px;
  border-bottom: 1px solid var(--line);
  min-height: 44px;
}

.brand { font-weight: 600; margin-right: 8px; white-space: nowrap; }
.status { color: var(--muted); font-size: 13px; white-space: nowrap; margin: 0 8px; }

.tabs {
  flex: 1;
  display: flex;
  align-items: flex-end;
  gap: 2px;
  align-self: stretch;
  min-width: 0;
  overflow-x: auto;
  white-space: nowrap;
}

.tab {
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 240px;
  margin-top: 6px;
  padding: 6px 6px 6px 12px;
  border-radius: 8px 8px 0 0;
  color: var(--muted);
  cursor: pointer;
}

.tab:hover { background: var(--soft); }
.tab.is-active { color: var(--fg); background: var(--soft); box-shadow: inset 0 -2px 0 var(--accent); }
.tab-kind { font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.tab-title { overflow: hidden; text-overflow: ellipsis; }
.tab-close, .tab-new { padding: 0 6px; color: var(--muted); }

/* The area under the top bar: one view at a time */
main#view {
  flex: 1;
  min-height: 0;
  position: relative;
  display: flex;
  flex-direction: column;
}

.view-host { flex: 1; min-height: 0; width: 100%; border: 0; }

/* Rich text */
.rich-host { position: relative; }
.rich-scroll { position: absolute; inset: 0; overflow-y: auto; }
.rich-surface { max-width: 720px; margin: 0 auto; padding: 48px 24px 40vh; }
.ProseMirror { outline: none; min-height: 70vh; }
.ProseMirror > * + * { margin-top: 0.75em; }
.ProseMirror h1 { font-size: 2.1em; line-height: 1.2; margin-top: 1.2em; }
.ProseMirror h2 { font-size: 1.5em; margin-top: 1.1em; }
.ProseMirror h3 { font-size: 1.25em; margin-top: 1em; }
.ProseMirror blockquote { margin-left: 0; padding-left: 16px; border-left: 3px solid var(--line); color: var(--muted); }
.ProseMirror pre { background: var(--soft); padding: 12px 16px; border-radius: 6px; overflow-x: auto; font: 14px/1.5 ui-monospace, Menlo, monospace; }
.ProseMirror code { background: var(--soft); padding: 2px 5px; border-radius: 4px; font: 0.9em ui-monospace, Menlo, monospace; }
.ProseMirror pre code { background: none; padding: 0; }
.ProseMirror hr { border: none; border-top: 1px solid var(--line); margin: 1.5em 0; }
.ProseMirror ul, .ProseMirror ol { padding-left: 1.5em; }

.ProseMirror p.is-editor-empty:first-child::before {
  content: attr(data-placeholder);
  float: left;
  height: 0;
  color: var(--faint);
  pointer-events: none;
}

.tableWrapper { overflow-x: auto; margin: 1em 0; }
.ProseMirror table { border-collapse: collapse; width: 100%; }
.ProseMirror td, .ProseMirror th { min-width: 110px; border: 1px solid var(--line); padding: 8px 10px; vertical-align: top; position: relative; overflow-wrap: break-word; }
.ProseMirror th { background: var(--soft); font-weight: 600; text-align: left; }
.ProseMirror .selectedCell::after { content: ""; position: absolute; inset: 0; background: rgba(9, 105, 218, 0.1); pointer-events: none; }
.column-resize-handle { position: absolute; right: -2px; top: 0; bottom: 0; width: 4px; background: var(--accent); pointer-events: none; }

.doc-image {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 1em 0;
  border: 1px solid var(--line);
  border-radius: 10px;
}

/* Split: rich text and canvas side by side. Left pane is clamped so the canvas keeps 460px. */
.split { flex: 1; display: flex; min-height: 0; width: 100%; }
.pane { position: relative; display: flex; flex-direction: column; flex: 1 1 0; min-width: 0; min-height: 0; }
.split > .pane:first-child { flex: 0 0 50%; min-width: 260px; max-width: calc(100% - 466px); }  /* width set by the divider */
.divider { flex: 0 0 6px; cursor: col-resize; background: var(--soft); border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
.divider:hover { background: #ddf4ff; }

/* Mermaid: source on the left, rendered diagram on the right */
.mermaid-host { display: grid; grid-template-columns: 1fr 1fr; min-height: 0; }
.mermaid-source {
  border: 0;
  border-right: 1px solid var(--line);
  padding: 20px;
  resize: none;
  outline: none;
  color: var(--fg);
  font: 14px/1.6 ui-monospace, Menlo, monospace;
  white-space: pre;
  overflow: auto;
}
.mermaid-preview { overflow: auto; padding: 20px; display: flex; justify-content: center; align-items: flex-start; }
.mermaid-preview svg { max-width: 100%; height: auto; }
.mermaid-error { color: #cf222e; background: #ffebe9; padding: 12px; border-radius: 6px; white-space: pre-wrap; font: 13px/1.5 ui-monospace, Menlo, monospace; margin: 0; }

/* Document tab: read-only Word, PDF and text */
.doc-host { overflow: auto; background: #fff; }
.doc-page { max-width: 720px; margin: 0 auto; padding: 48px 24px; }
.doc-page table { border-collapse: collapse; margin: 1em 0; }
.doc-page td, .doc-page th { border: 1px solid var(--line); padding: 6px 8px; }
.doc-page img { max-width: 100%; height: auto; }
.doc-pdf { width: 100%; height: 100%; border: 0; }
.doc-text { max-width: 720px; margin: 0 auto; padding: 48px 24px; white-space: pre-wrap; font: 14px/1.6 ui-monospace, Menlo, monospace; }

/* Sheet tab: editable grid with a sheet switcher underneath */
.sheet-host { display: flex; flex-direction: column; }
.sheet-grid { flex: 1; min-height: 0; overflow: auto; }
.sheet-grid table { border-collapse: collapse; font-size: 14px; }
.sheet-grid th { position: sticky; top: 0; background: var(--soft); color: var(--muted); font-weight: 500; font-size: 12px; padding: 2px 8px; border: 1px solid var(--line); }
.sheet-grid tbody th { position: sticky; top: auto; left: 0; }
.sheet-grid td { min-width: 120px; height: 28px; padding: 2px 8px; border: 1px solid var(--line); white-space: nowrap; outline: none; }
.sheet-grid td:focus { box-shadow: inset 0 0 0 2px var(--accent); }
.sheet-bar { display: flex; align-items: center; gap: 4px; padding: 6px 8px; border-top: 1px solid var(--line); overflow-x: auto; }
.sheet-download { margin-left: auto; color: var(--muted); }

/* Floating bars and the "/" menu share one look */
.floating,
.slash-menu {
  position: absolute;
  z-index: 30;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(31, 35, 40, 0.12);
}

.floating { display: flex; flex-wrap: wrap; max-width: min(560px, calc(100% - 16px)); padding: 4px; gap: 2px; }
.floating .group { display: flex; flex-wrap: wrap; align-items: center; gap: 2px; }
.floating .sep { width: 1px; height: 18px; margin: 0 4px; background: var(--line); }
.floating button { min-width: 32px; padding: 6px 8px; font-size: 13px; }

.slash-menu { width: 260px; max-height: 340px; overflow-y: auto; padding: 6px; }
.slash-menu button { display: block; width: 100%; text-align: left; padding: 8px 10px; border-radius: 6px; }
.slash-menu button strong { display: block; font-weight: 500; }
.slash-menu button span { display: block; font-size: 12px; color: var(--muted); }
.slash-menu button.is-selected { background: var(--soft); }
.slash-menu .empty { padding: 8px 10px; color: var(--muted); font-size: 14px; }

/* Chooser for a new tab */
.chooser {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  background: rgba(31, 35, 40, 0.25);
}

.chooser-card {
  width: min(680px, calc(100vw - 32px));
  padding: 24px;
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 16px 48px rgba(31, 35, 40, 0.2);
}

.chooser-card h2 { margin: 0; font-size: 20px; }
.chooser-card p { margin: 4px 0 16px; color: var(--muted); }

.options { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }

.options button {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  text-align: left;
}

.options button:hover { border-color: var(--accent); }
.options strong { font-size: 15px; font-weight: 600; }
.options span { color: var(--muted); font-size: 13px; line-height: 1.4; }

.cancel { margin-top: 12px; color: var(--muted); }
body:not(.has-tabs) #chooser-cancel { display: none; }

/* Narrow windows: stack the split panes, rich text above the canvas. */
@media (max-width: 759px) {
  .brand, .status { display: none; }
  .options { grid-template-columns: 1fr; }
  .split { flex-direction: column; }
  .mermaid-host { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
  .mermaid-source { border-right: 0; border-bottom: 1px solid var(--line); }
  .split > .pane:first-child { flex: 1 1 0 !important; max-width: none !important; }
  .divider { display: none; }
}
