:root {
  --serif: 'Lora', Georgia, serif;
  --sans: 'Inter', system-ui, sans-serif;
  --mono: 'JetBrains Mono', monospace;
  --ink: #1a1a18; --ink2: #4a4a46; --ink3: #8a8a84; --ink4: #b8b8b0;
  --paper: #faf9f6; --paper2: #f2f0eb; --paper3: #e8e5de;
  --accent: #2d5a3d; --accent2: #1a3a28;
  --rule: rgba(26, 26, 24, .12); --rule2: rgba(26, 26, 24, .22);
  --info-bg: #eef4f0; --info-text: #1a4a2a;
  --warn-bg: #fdf4e7; --warn-text: #6b3d00;
  --danger-bg: #fdf0ee; --danger-text: #6b1a14;
  --success-bg: #eef6ee; --success-text: #1a4a1a;
  --max: 640px; --pad: clamp(1.5rem, 5vw, 3rem);
  --border-radius: 10px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 16px;
  line-height: 1.85;
  -webkit-font-smoothing: antialiased;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

header {
  padding: 0.75rem var(--pad);
  border-bottom: .5px solid var(--rule2);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--paper);
  z-index: 100;
  position: sticky;
  top: 0;
}

.logo {
  font-family: var(--sans);
  font-weight: 500;
  font-size: 13px;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--ink);
}

.proto-badge {
  font-family: var(--sans);
  font-size: 9px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 2px 6px;
  background: var(--paper3);
  color: var(--ink3);
  border-radius: 4px;
  margin-left: -4px;
  margin-right: 4px;
  user-select: none;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* Hamburger menu */
.menu-wrap { position: relative; }
.menu-btn {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 3.5px;
  width: 28px;
  height: 28px;
  padding: 6px;
  border: .5px solid var(--rule2);
  border-radius: 4px;
  background: var(--paper);
  cursor: pointer;
  transition: background 0.15s;
}
.menu-btn:hover { background: var(--paper2); }
.menu-btn span {
  display: block;
  height: 1.5px;
  background: var(--ink2);
  border-radius: 1px;
  transition: transform 0.2s, opacity 0.2s;
}
.menu-btn.open span:nth-child(1) { transform: translateY(5px) rotate(45deg); }
.menu-btn.open span:nth-child(2) { opacity: 0; }
.menu-btn.open span:nth-child(3) { transform: translateY(-5px) rotate(-45deg); }

.menu-dropdown {
  display: none;
  position: absolute;
  left: 0; top: calc(100% + 6px);
  background: var(--paper);
  border: .5px solid var(--rule2);
  border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0,0,0,.08);
  min-width: 190px;
  z-index: 200;
  overflow: hidden;
  flex-direction: column;
}
.menu-dropdown.open { display: flex; }
.menu-dropdown button,
.menu-dropdown a {
  font-family: var(--sans);
  font-size: 12px;
  padding: 9px 16px;
  border: none;
  background: none;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  text-decoration: none;
  display: block;
}
.menu-dropdown button:hover,
.menu-dropdown a:hover { background: var(--paper2); }
.menu-sep {
  height: 0;
  border-top: .5px solid var(--rule2);
  margin: 2px 0;
}

/* Manual overlay */
.manual-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 300;
  background: rgba(0,0,0,.3);
  justify-content: center;
  align-items: start;
  padding: 3vh 1rem;
  overflow-y: auto;
}
.manual-overlay.open { display: flex; }
.manual {
  background: var(--paper);
  border-radius: var(--border-radius);
  box-shadow: 0 8px 40px rgba(0,0,0,.12);
  max-width: 640px;
  width: 100%;
  max-height: 94vh;
  overflow-y: auto;
}
.manual-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  border-bottom: .5px solid var(--rule2);
  position: sticky;
  top: 0;
  background: var(--paper);
  z-index: 1;
}
.manual-title {
  font-family: var(--sans);
  font-weight: 600;
  font-size: 14px;
}
.manual-close {
  font-size: 20px;
  border: none;
  background: none;
  color: var(--ink3);
  cursor: pointer;
  line-height: 1;
  padding: 0 4px;
}
.manual-close:hover { color: var(--ink); }
.manual-body {
  padding: 20px;
  font-family: var(--sans);
  font-size: 13px;
  line-height: 1.7;
  color: var(--ink);
}
.manual-body h2 {
  font-size: 16px;
  font-weight: 600;
  margin: 1.5rem 0 0.5rem;
  padding-bottom: 4px;
  border-bottom: .5px solid var(--rule2);
}
.manual-body h2:first-child { margin-top: 0; }
.manual-body h3 {
  font-size: 13px;
  font-weight: 600;
  margin: 1.2rem 0 0.3rem;
  font-family: var(--mono);
  color: var(--accent);
}
.manual-body p { margin-bottom: 0.6rem; }
.manual-body pre {
  font-family: var(--mono);
  font-size: 11px;
  background: var(--paper2);
  padding: 8px 12px;
  border-radius: 4px;
  margin: 0.5rem 0 0.8rem;
  overflow-x: auto;
  line-height: 1.5;
}
.manual-body code {
  font-family: var(--mono);
  font-size: 11px;
  background: var(--paper2);
  padding: 1px 5px;
  border-radius: 3px;
}
.manual-body ul, .manual-body ol { padding-left: 1.2rem; margin-bottom: 0.6rem; }
.manual-body li { margin-bottom: 0.3rem; }
.manual-body dl { margin-bottom: 0.6rem; }
.manual-body dt { font-weight: 600; margin-top: 0.4rem; }
.manual-body dd { margin-left: 0; color: var(--ink2); }
.manual-body strong { font-weight: 600; }

.view-toggle-btn {
  background: var(--paper);
  border: .5px solid var(--rule2);
  border-radius: 6px;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--ink2);
  transition: all 0.2s;
}

.view-toggle-btn:hover {
  background: var(--paper2);
  border-color: var(--rule);
  color: var(--ink);
}

.view-toggle-btn.active {
  background: var(--accent);
  border-color: var(--accent2);
  color: #fff;
}

.status {
  font-family: var(--sans);
  font-size: 11px;
  color: var(--ink3);
}

main {
  flex: 1;
  overflow: hidden;
}

#editor-container {
  display: none;
}

#canvas-container {
  width: 100%;
  background: var(--paper);
  overflow-y: auto;
  height: 100%;
  padding: 4rem var(--pad);
  scroll-behavior: smooth;
}

.document-page {
  max-width: var(--max);
  margin: 0 auto;
}

/* Typography matching the manuscript */
.prose {
  font-size: 15px;
  line-height: 1.9;
  margin-bottom: 1.35rem;
  color: var(--ink);
  padding: 8px;
  border-radius: 4px;
  transition: background 0.15s, box-shadow 0.15s;
  outline: none;
}

.prose:hover {
  background: var(--paper2);
}

.prose[data-empty="true"]:not(:focus)::before {
  content: 'Type here...';
  color: var(--ink4);
  font-style: italic;
}

/* Widgets & Directives - Styled like the book's figures */
.directive-widget {
  margin: 1.5rem 0;
  border: .5px solid var(--rule2);
  border-radius: var(--border-radius);
  background: var(--paper);
  overflow: hidden;
  transition: border-color 0.15s;
}

.directive-widget:hover {
  border-color: var(--rule);
}

.directive-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 12px;
  background: var(--paper2);
  border-bottom: .5px solid var(--rule2);
}

.directive-tag {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink3);
}

.directive-pills {
  display: flex;
  gap: 4px;
}

.directive-pill {
  font-size: 11px;
  padding: 2px 9px;
  border-radius: 10px;
  border: .5px solid var(--rule2);
  background: transparent;
  color: var(--ink2);
  font-family: var(--sans);
  cursor: pointer;
}

.directive-pill.active {
  background: var(--paper);
  color: var(--ink);
  border-color: var(--rule);
  font-weight: 500;
}

.directive-body {
  padding: 14px;
  background: var(--paper);
}

/* Status Badges */
.badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 8px;
  font-family: var(--sans);
  display: inline-block;
}

.badge-warn { background: var(--warn-bg); color: var(--warn-text); }
.badge-info { background: var(--info-bg); color: var(--info-text); }
.badge-success { background: var(--success-bg); color: var(--success-text); }

/* Viewport-driven directive slots */
.directive-slot {
  min-height: 40px;          /* Ensures IntersectionObserver has something to observe */
  margin: 1rem 0;
}

.directive-fallback {
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.6;
  color: var(--ink3);
  padding: 10px 14px;
  background: var(--paper2);
  border: .5px dashed var(--rule2);
  border-radius: 0 0 var(--border-radius) var(--border-radius);
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  outline: none;
  transition: background 0.15s, box-shadow 0.15s;
}

.directive-fallback:focus {
  background: #fff;
  color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--rule2);
}

/* Table renderer */
.table-rendered {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--sans);
  font-size: 12px;
}
.table-rendered th {
  padding: 10px 14px;
  text-align: left;
  font-weight: 500;
  background: var(--paper2);
  border-bottom: .5px solid var(--rule2);
}
.table-rendered td {
  padding: 10px 14px;
  border-bottom: .5px solid var(--rule2);
  outline: none;
}
.table-rendered td[contenteditable="true"]:focus {
  box-shadow: inset 0 0 0 1.5px var(--accent);
  border-radius: 2px;
}

/* Divergence reconcile bar */
.table-reconcile {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: var(--warn-bg);
  border-bottom: .5px solid var(--rule2);
  font-size: 12px;
  font-family: var(--sans);
  color: var(--warn-text);
}

/* Email renderer */
.email-list { font-family: var(--sans); }
.email-item {
  padding: 10px 14px;
  border-bottom: .5px solid var(--rule2);
  display: flex;
  flex-direction: column;
  gap: 3px;
  transition: opacity 0.2s;
}
.email-item:hover { background: var(--paper2); }
.email-item.unread .email-from { font-weight: 600; }
.email-item-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.email-from { font-family: var(--sans); font-size: 13px; }
.email-meta { display: flex; align-items: center; gap: 6px; }
.email-date { font-family: var(--sans); font-size: 11px; color: var(--ink3); }
.email-subject { font-family: var(--sans); font-size: 12px; color: var(--ink2); }
.email-snippet { font-family: var(--sans); font-size: 12px; color: var(--ink3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.email-item-actions { margin-top: 4px; }

.email-thread { display: flex; flex-direction: column; }
.email-message { padding: 14px; border-bottom: .5px solid var(--rule2); }
.email-message-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.email-message-body { font-family: var(--sans); font-size: 13px; line-height: 1.6; color: var(--ink2); white-space: pre-wrap; }

.email-reply-bar { padding: 14px; border-top: .5px solid var(--rule2); display: flex; flex-direction: column; gap: 8px; }

.email-compose { display: flex; flex-direction: column; gap: 10px; padding: 14px; }
.email-field { display: flex; align-items: center; gap: 8px; font-family: var(--sans); font-size: 13px; }
.email-field label { width: 56px; color: var(--ink3); font-size: 12px; flex-shrink: 0; }
.email-field input {
  flex: 1;
  font-family: var(--sans);
  font-size: 13px;
  padding: 6px 10px;
  border: .5px solid var(--rule2);
  border-radius: 4px;
  outline: none;
  background: var(--paper);
}
.email-field input:focus { border-color: var(--accent); }
.email-compose-body {
  width: 100%;
  font-family: var(--sans);
  font-size: 13px;
  line-height: 1.6;
  padding: 10px;
  border: .5px solid var(--rule2);
  border-radius: 4px;
  outline: none;
  resize: vertical;
  background: var(--paper);
}
.email-compose-body:focus { border-color: var(--accent); }
.email-compose-actions { display: flex; align-items: center; gap: 8px; justify-content: flex-end; }
.email-status { font-family: var(--sans); font-size: 12px; color: var(--ink3); margin-right: auto; }
.email-btn-send {
  font-family: var(--sans);
  font-size: 12px;
  padding: 5px 16px;
  border-radius: 6px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-weight: 500;
  cursor: pointer;
}
.email-btn-send:hover { background: var(--accent2); }
.email-btn-send:disabled { opacity: 0.5; cursor: default; }

.email-log-area { border-top: .5px solid var(--rule2); }
.email-log {
  font-family: var(--sans);
  font-size: 11px;
  color: var(--ink3);
  padding: 6px 14px;
  background: var(--paper2);
  border-bottom: .5px solid var(--rule2);
}

/* Task renderer */
.task-label {
  font-family: var(--sans);
  font-size: 14px;
}
.task-done {
  text-decoration: line-through;
  color: var(--ink3);
}
.task-high {
  font-weight: 600;
  color: var(--danger-text);
}
.task-blocked {
  color: var(--ink4);
}

/* Calendar renderer */
.cal-list { font-family: var(--sans); }
.cal-event {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: .5px solid var(--rule2);
}
.cal-time {
  font-size: 13px;
  color: var(--ink3);
  width: 44px;
  flex-shrink: 0;
}
.cal-title {
  font-size: 13px;
  flex: 1;
}
.cal-meta {
  display: flex;
  gap: 4px;
  align-items: center;
}
.cal-actions {
  display: flex;
  gap: 4px;
  margin-left: 4px;
}
.cal-log-area { border-top: .5px solid var(--rule2); }
.cal-log {
  font-family: var(--sans);
  font-size: 11px;
  color: var(--ink3);
  padding: 6px 14px;
  background: var(--paper2);
  border-bottom: .5px solid var(--rule2);
}

/* Note renderer */
.note-content {
  font-family: var(--sans);
  font-size: 13px;
  line-height: 1.7;
  padding: 12px 14px;
  min-height: 40px;
  outline: none;
  white-space: pre-wrap;
  word-break: break-word;
}
.note-content:focus {
  background: #fff;
  box-shadow: inset 0 0 0 1.5px var(--accent);
  border-radius: 0 0 var(--border-radius) var(--border-radius);
}
.note-content[data-empty="true"]:not(:focus)::before {
  content: 'Write a note...';
  color: var(--ink4);
}

/* Contact renderer */
.contact-card { padding: 12px 14px; font-family: var(--sans); font-size: 13px; }
.contact-field { display: flex; align-items: baseline; gap: 8px; margin-bottom: 4px; }
.contact-label { width: 50px; font-size: 11px; color: var(--ink3); flex-shrink: 0; text-align: right; }
.contact-value { font-size: 13px; }
.contact-input {
  flex: 1; font-family: var(--sans); font-size: 13px;
  padding: 4px 8px; border: .5px solid var(--rule2); border-radius: 4px;
  outline: none; background: var(--paper);
}
.contact-input:focus { border-color: var(--accent); }
.contact-actions { display: flex; gap: 4px; padding: 8px 14px; border-top: .5px solid var(--rule2); }
.contact-log-area { border-top: .5px solid var(--rule2); }
.contact-log {
  font-family: var(--sans); font-size: 11px; color: var(--ink3);
  padding: 6px 14px; background: var(--paper2); border-bottom: .5px solid var(--rule2);
}

/* File renderer */
.file-card { display: flex; align-items: center; gap: 12px; padding: 4px 0; }
.file-icon { font-size: 28px; flex-shrink: 0; }
.file-info { flex: 1; font-family: var(--sans); }
.file-name { font-size: 13px; font-weight: 500; }
.file-path { font-size: 11px; color: var(--ink3); margin-top: 2px; }
.file-preview {
  margin-top: 8px; padding: 20px; text-align: center;
  background: var(--paper2); border-radius: 4px;
  font-size: 12px; color: var(--ink3);
}

/* Chat renderer */
.chat-messages { font-family: var(--sans); }
.chat-message {
  padding: 8px 14px; border-bottom: .5px solid var(--rule2);
  position: relative;
}
.chat-message:hover { background: var(--paper2); }
.chat-message:hover .chat-promote { opacity: 1; }
.chat-msg-header { display: flex; align-items: center; gap: 8px; margin-bottom: 2px; }
.chat-user { font-size: 12px; font-weight: 600; }
.chat-time { font-size: 11px; color: var(--ink3); }
.chat-msg-text { font-size: 13px; line-height: 1.5; color: var(--ink2); }
.chat-promote {
  position: absolute; right: 10px; top: 8px;
  opacity: 0; transition: opacity 0.15s; cursor: pointer; font-size: 10px;
}
.chat-log-area { border-top: .5px solid var(--rule2); }
.chat-log {
  font-family: var(--sans); font-size: 11px; color: var(--ink3);
  padding: 6px 14px; background: var(--paper2); border-bottom: .5px solid var(--rule2);
}
.chat-compose {
  display: flex; gap: 8px; padding: 10px 14px;
  border-top: .5px solid var(--rule2); align-items: center;
}
.chat-input {
  flex: 1; font-family: var(--sans); font-size: 13px;
  padding: 6px 10px; border: .5px solid var(--rule2); border-radius: 4px;
  outline: none; background: var(--paper);
}
.chat-input:focus { border-color: var(--accent); }

/* Web renderer */
.web-article { padding: 14px; font-family: var(--sans); }
.web-title { font-size: 15px; font-weight: 600; margin-bottom: 4px; }
.web-url { font-size: 11px; color: var(--accent); margin-bottom: 8px; word-break: break-all; }
.web-summary { font-size: 13px; color: var(--ink2); line-height: 1.6; margin-bottom: 10px; }
.web-reader {
  padding: 16px; background: var(--paper2); border-radius: 4px;
  font-size: 12px; color: var(--ink3); text-align: center;
}
.web-annotations { border-top: .5px solid var(--rule2); }
.web-anno-label {
  font-family: var(--sans); font-size: 11px; color: var(--ink3);
  padding: 6px 14px; background: var(--paper2); border-bottom: .5px solid var(--rule2);
  font-weight: 500;
}
.web-anno-content {
  font-family: var(--sans); font-size: 13px; line-height: 1.6;
  padding: 10px 14px; min-height: 30px; outline: none; white-space: pre-wrap;
}
.web-anno-content:focus {
  background: #fff; box-shadow: inset 0 0 0 1.5px var(--accent);
  border-radius: 0 0 var(--border-radius) var(--border-radius);
}
.web-anno-content[data-empty="true"]:not(:focus)::before {
  content: 'Add a note about this page...';
  color: var(--ink4);
}

/* CodeMirror overrides for "Substrate" feel */
.cm-editor {
  height: 100%;
  font-family: var(--mono);
  font-size: 13px;
}
.cm-scroller {
  line-height: 1.6;
}
