:root {
  color: #222126;
  background: #ebe8e5;
  font-family: Inter, "Segoe UI", system-ui, sans-serif;
  font-synthesis: none;
  --bordeaux: #6d1a28;
  --bordeaux-dark: #4d101a;
  --paper: #fffdf9;
  --line: #d8d2cd;
  --muted: #706a67;
  --success: #2f6b50;
  --error: #a22b2b;
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at 12% 5%, rgba(109, 26, 40, 0.08), transparent 27rem),
    #ebe8e5;
}

.app-header {
  min-height: 86px;
  padding: 18px clamp(18px, 3vw, 42px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  color: white;
  background: linear-gradient(115deg, var(--bordeaux-dark), var(--bordeaux));
  box-shadow: 0 8px 28px rgba(51, 18, 24, 0.18);
}

.eyebrow {
  margin: 0 0 3px;
  font-size: 0.7rem;
  font-weight: 750;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  opacity: 0.72;
}

h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(1.3rem, 2vw, 1.75rem); }
h2 { margin: 0; font-size: 1rem; }

.header-actions, .panel-header, .panel-header > div {
  display: flex;
  align-items: center;
}

.header-actions { gap: 16px; }

.header-secondary-button {
  border-color: rgba(255, 255, 255, 0.48);
  color: white;
  background: rgba(255, 255, 255, 0.1);
}

.header-secondary-button:hover { background: rgba(255, 255, 255, 0.18); }

button {
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 7px;
  padding: 10px 15px;
  color: var(--bordeaux-dark);
  background: white;
  font: inherit;
  font-size: 0.86rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 120ms ease, opacity 120ms ease;
}

button:hover { transform: translateY(-1px); }
button:disabled { cursor: wait; opacity: 0.65; transform: none; }

.status { font-size: 0.8rem; color: rgba(255,255,255,0.8); }
.status[data-state="success"] { color: #c8f3dc; }
.status[data-state="error"] { color: #ffd0d0; }

.workspace {
  height: calc(100vh - 86px);
  padding: 18px;
  display: grid;
  grid-template-columns: minmax(320px, 0.9fr) minmax(420px, 1.1fr);
  gap: 16px;
}

.panel {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
  box-shadow: 0 8px 24px rgba(55, 42, 38, 0.07);
}

.panel-header {
  min-height: 51px;
  justify-content: space-between;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
}

.panel-header > div { gap: 9px; }

.step {
  display: inline-grid;
  place-items: center;
  width: 25px;
  height: 25px;
  border-radius: 50%;
  color: white;
  background: var(--bordeaux);
  font-size: 0.66rem;
  font-weight: 800;
}

.meta { color: var(--muted); font-size: 0.72rem; }

.editor-toolbar {
  min-height: 46px;
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 6px 9px;
  border-bottom: 1px solid var(--line);
  background: #f8f5f1;
}

.history-tools,
.markup-tools {
  display: flex;
  align-items: center;
  gap: 5px;
}

.history-tools { flex: none; }

.markup-tools {
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 1px 2px;
  scrollbar-width: none;
  overscroll-behavior-inline: contain;
}

.markup-tools::-webkit-scrollbar { display: none; }

.toolbar-divider {
  flex: none;
  width: 1px;
  height: 25px;
  background: #d8cfca;
}

.toolbar-button {
  flex: none;
  width: 32px;
  height: 32px;
  display: inline-grid;
  place-items: center;
  padding: 0;
  border-color: #d5cbc6;
  border-radius: 6px;
  color: #554b4d;
  background: #fffdfa;
  font-size: 0.82rem;
  font-weight: 700;
  box-shadow: 0 1px 2px rgba(55, 42, 38, 0.05);
}

.toolbar-button:hover {
  border-color: #b8949b;
  color: var(--bordeaux);
  background: #fff;
}

.toolbar-button:focus-visible { outline: 2px solid var(--bordeaux); outline-offset: 1px; }
.toolbar-button:disabled { cursor: default; opacity: 0.38; }
.toolbar-button span { line-height: 1; }
.history-button span { font-size: 1.2rem; }
.layout-icon { font-size: 0.68rem; letter-spacing: -0.18em; padding-right: 0.18em; }

.callout-hint { color: #8c6717; }
.callout-info { color: #236a87; font-family: Georgia, serif; font-size: 1rem; }
.callout-warning, .callout-critical, .callout-forbidden { color: #a22b2b; }
.callout-attention { color: #b5631a; }
.callout-remember { color: #2f6b50; }

.toolbar-tooltip {
  position: fixed;
  z-index: 1000;
  max-width: min(260px, calc(100vw - 16px));
  padding: 6px 9px;
  border-radius: 5px;
  color: white;
  background: #332d2f;
  box-shadow: 0 4px 14px rgba(32, 24, 26, 0.22);
  font-size: 0.72rem;
  font-weight: 650;
  line-height: 1.3;
  pointer-events: none;
  white-space: nowrap;
}

.toolbar-tooltip[hidden] { display: none; }

.editor-workspace {
  flex: 1;
  min-height: 0;
  min-width: 0;
  display: grid;
  grid-template-columns: clamp(150px, 22%, 205px) minmax(0, 1fr);
}

.document-navigator {
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--line);
  background: #f8f5f1;
}

.document-navigator-header {
  display: grid;
  gap: 3px;
  padding: 14px 12px 11px;
  border-bottom: 1px solid #e4ddd8;
}

.navigator-label {
  color: var(--muted);
  font-size: 0.62rem;
  font-weight: 750;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

#document-navigator-title {
  overflow: hidden;
  color: var(--bordeaux-dark);
  font-size: 0.8rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.document-outline {
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 8px 6px 18px;
}

.outline-entry {
  width: 100%;
  min-width: 0;
  display: grid;
  grid-template-columns: 14px minmax(0, 1fr);
  align-items: start;
  gap: 2px;
  padding: 6px 5px;
  border: 0;
  border-radius: 5px;
  color: #4e4748;
  background: transparent;
  font-size: 0.72rem;
  font-weight: 650;
  line-height: 1.3;
  text-align: left;
}

.outline-entry:hover,
.outline-entry:focus-visible,
.outline-entry.is-active {
  color: var(--bordeaux);
  background: #efe5e6;
  transform: none;
}

.outline-entry.is-active { box-shadow: inset 2px 0 var(--bordeaux); }

.outline-entry-level-3 { padding-left: 17px; color: #746b69; font-weight: 550; }
.outline-marker { color: #98747c; font-family: monospace; }
.outline-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.outline-empty {
  margin: 8px;
  color: var(--muted);
  font-size: 0.7rem;
  line-height: 1.45;
}

.editor-shell {
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: #fffdfa;
}

#source-editor {
  width: 100%;
  height: 100%;
}

#source-editor .cm-editor { height: 100%; color: #39353a; background: #fffdfa; }
#source-editor .cm-editor.cm-focused { outline: none; box-shadow: inset 3px 0 var(--bordeaux); }
#source-editor .cm-scroller {
  overflow: auto;
  font-family: "JetBrains Mono", "Cascadia Code", Consolas, monospace;
  font-size: 13px;
  line-height: 1.62;
}
#source-editor .cm-content { padding: 18px 12px 32px 6px; caret-color: #28252a; }
#source-editor .cm-line { padding-left: 4px; }
#source-editor .cm-gutters {
  border-right: 1px solid #e5dfda;
  color: #a09791;
  background: #f8f5f1;
}
#source-editor .cm-lineNumbers .cm-gutterElement { min-width: 34px; padding: 0 8px 0 5px; }
#source-editor .cm-foldGutter .cm-gutterElement { width: 18px; color: #8c5661; }
#source-editor .cm-foldPlaceholder {
  border: 1px solid #d6c3c7;
  border-radius: 4px;
  padding: 0 5px;
  color: var(--bordeaux);
  background: #f7ecee;
}
#source-editor .cm-activeLine,
#source-editor .cm-activeLineGutter { background: rgba(109, 26, 40, 0.055); }
#source-editor .cm-selectionBackground { background: rgba(109, 26, 40, 0.18) !important; }

.sop-heading-marker { color: #9b7480; }
.sop-heading-1 { color: var(--bordeaux-dark); font-weight: 800; }
.sop-heading-2 { color: var(--bordeaux); font-weight: 750; }
.sop-heading-3 { color: #77525b; font-weight: 700; }
.sop-metadata-key { color: #315f65; font-weight: 700; }
.sop-metadata-colon { color: #8da1a3; }
.sop-command { color: #854088; font-weight: 700; }
.sop-command-end { color: #a65e20; font-weight: 750; }
.sop-command-unknown { color: #b4232f; text-decoration: wavy underline; text-decoration-thickness: 1px; }
.sop-list-marker { color: var(--bordeaux); font-weight: 800; }
.sop-table-separator { color: #b06d27; font-weight: 800; }
.sop-footnote { color: #236a87; }
.sop-link { color: #1d6685; text-decoration: underline; text-decoration-thickness: 1px; }
.sop-decision-label { color: #315f65; font-weight: 700; }
.sop-decision-arrow { color: var(--bordeaux); font-weight: 800; }

.error-panel {
  max-height: 34%;
  overflow: auto;
  padding: 12px 16px;
  border-top: 1px solid #e7bcbc;
  color: #701d1d;
  background: #fff0f0;
  font-size: 0.82rem;
  line-height: 1.45;
}

.preview-frame { position: relative; flex: 1; min-height: 0; background: #d4d0cc; }

#pdf-preview { width: 100%; height: 100%; border: 0; background: white; }

.preview-placeholder {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 16px;
  padding: 30px;
  color: var(--muted);
  text-align: center;
}

.preview-placeholder[hidden] { display: none; }
.preview-placeholder p { max-width: 20rem; margin-bottom: 0; font-size: 0.86rem; }

.document-icon {
  width: 54px;
  height: 68px;
  border: 2px solid #9e9690;
  border-radius: 4px;
  background: linear-gradient(#fff 0 0) padding-box;
  box-shadow: 7px 7px 0 rgba(109, 26, 40, 0.1);
}

.drawer-backdrop {
  position: fixed;
  z-index: 90;
  inset: 0;
  background: rgba(35, 25, 27, 0.34);
  backdrop-filter: blur(1px);
}

.drawer-backdrop[hidden] { display: none; }

.image-manager {
  position: fixed;
  z-index: 100;
  top: 0;
  right: 0;
  width: min(390px, calc(100vw - 24px));
  height: 100vh;
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--line);
  background: var(--paper);
  box-shadow: -14px 0 38px rgba(39, 26, 29, 0.2);
  transform: translateX(105%);
  transition: transform 180ms ease;
}

.image-manager.is-open { transform: translateX(0); }

.image-manager-header,
.dialog-header,
.dialog-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.image-manager-header {
  min-height: 76px;
  padding: 15px 18px;
  color: white;
  background: linear-gradient(115deg, var(--bordeaux-dark), var(--bordeaux));
}

.image-manager-header .eyebrow { margin-bottom: 4px; }

.icon-button {
  width: 34px;
  height: 34px;
  display: inline-grid;
  place-items: center;
  padding: 0;
  border-color: currentColor;
  color: inherit;
  background: transparent;
  font-size: 1.45rem;
  font-weight: 400;
  line-height: 1;
}

.image-manager-actions { padding: 16px 18px 12px; border-bottom: 1px solid var(--line); }

.upload-button,
.primary-small-button {
  display: inline-flex;
  align-items: center;
  min-height: 38px;
  padding: 8px 13px;
  border: 1px solid var(--bordeaux);
  border-radius: 7px;
  color: white;
  background: var(--bordeaux);
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
}

.primary-small-button { border-color: var(--bordeaux); }

.image-manager-message {
  min-height: 1.2em;
  margin: 9px 0 0;
  color: var(--muted);
  font-size: 0.75rem;
}

.image-manager-message[data-state="error"] { color: var(--error); }

.image-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: grid;
  align-content: start;
  gap: 10px;
  padding: 14px 18px 24px;
}

.image-empty { padding: 28px 10px; color: var(--muted); text-align: center; font-size: 0.84rem; }

.image-card {
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr);
  gap: 12px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}

.image-card-preview {
  width: 72px;
  height: 72px;
  display: grid;
  place-items: center;
  overflow: hidden;
  border-radius: 5px;
  color: var(--muted);
  background: #eee9e5;
  font-size: 0.72rem;
  font-weight: 800;
}

.image-card-preview img { width: 100%; height: 100%; object-fit: cover; }
.image-card-content { min-width: 0; display: flex; flex-direction: column; align-items: flex-start; }
.image-card-title { margin: 1px 0 3px; font-size: 0.88rem; }
.image-card-path { width: 100%; overflow: hidden; color: var(--muted); font-size: 0.71rem; text-overflow: ellipsis; white-space: nowrap; }
.image-insert-button { margin-top: auto; padding: 6px 10px; color: white; background: var(--bordeaux); font-size: 0.75rem; }

.text-card {
  display: grid;
  gap: 5px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}

.text-card-title { margin: 0; color: #443c3e; font-size: 0.88rem; }
.text-card-path { overflow: hidden; color: var(--muted); font-size: 0.71rem; text-overflow: ellipsis; white-space: nowrap; }
.text-load-button { justify-self: start; margin-top: 5px; padding: 6px 11px; color: white; background: var(--bordeaux); font-size: 0.75rem; }

.image-dialog {
  width: min(520px, calc(100vw - 28px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 11px;
  color: #332d2f;
  background: var(--paper);
  box-shadow: 0 20px 60px rgba(35, 25, 27, 0.28);
}

.image-dialog::backdrop { background: rgba(35, 25, 27, 0.45); }
.dialog-header { padding: 15px 18px; border-bottom: 1px solid var(--line); }
.dialog-header .icon-button { color: var(--muted); }
.dialog-body { display: grid; gap: 14px; padding: 18px; }
.dialog-body label { display: grid; gap: 6px; color: #51494b; font-size: 0.77rem; font-weight: 700; }
.dialog-body input, .dialog-body select {
  width: 100%;
  min-width: 0;
  min-height: 39px;
  padding: 8px 10px;
  border: 1px solid #cfc6c1;
  border-radius: 6px;
  color: #332d2f;
  background: white;
  font: inherit;
}
.dialog-body input[readonly] { color: var(--muted); background: #f4f0ed; }
.dialog-message { min-height: 1.2em; margin: 0; color: var(--muted); font-size: 0.75rem; }
.dialog-message[data-state="error"] { color: var(--error); }
.width-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.dialog-actions { gap: 9px; padding: 13px 18px; border-top: 1px solid var(--line); }
.dialog-actions button[type="submit"] { margin-left: auto; color: white; background: var(--bordeaux); }
.secondary-button { border-color: #cfc6c1; color: #51494b; background: white; }

@media (max-width: 850px) {
  .app-header { min-height: 104px; align-items: flex-start; }
  .header-actions { align-items: flex-end; flex-direction: column-reverse; gap: 6px; }
  .workspace { height: auto; min-height: calc(100vh - 104px); grid-template-columns: 1fr; }
  .panel { min-height: 70vh; }
}

@media (max-width: 560px) {
  .header-actions { gap: 5px; }
  .header-actions button { padding: 8px 10px; }
  .status { display: none; }
  .width-fields { grid-template-columns: 1fr; }
  .editor-workspace { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); }
  .document-navigator { max-height: 150px; border-right: 0; border-bottom: 1px solid var(--line); }
  .document-navigator-header { padding: 8px 12px; }
  .document-outline { max-height: 96px; }
}
