:root {
  color-scheme: light;
  --bg: #f5f4ef;
  --panel: #ffffff;
  --panel-subtle: #fbfaf6;
  --text: #1d1c1b;
  --text-soft: #3d3c3a;
  --muted: #7a7872;
  --border: #e6e4dc;
  --border-strong: #d7d4c9;
  --accent: #c15f3c;
  --accent-bright: #d97757;
  --accent-bright-hover: #cc6a4a;
  --accent-soft: rgba(193, 95, 60, 0.08);
  --hover: #f0eee8;
  --kbd-bg: #eeece4;
  --shadow-sm: 0 1px 2px rgba(29, 28, 27, 0.04);

  --font-ui: 'Inter', ui-sans-serif, system-ui, sans-serif;
  --font-paragraph: 'Source Serif 4', 'Iowan Old Style', 'Georgia', serif;
  --font-h1: 'Source Serif 4', 'Iowan Old Style', 'Georgia', serif;
  --font-h2: 'Source Serif 4', 'Iowan Old Style', 'Georgia', serif;
  --font-h3: 'Source Serif 4', 'Iowan Old Style', 'Georgia', serif;
  --font-h4: 'Inter', ui-sans-serif, system-ui, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #262624;
    --panel: #30302e;
    --panel-subtle: #2b2b29;
    --text: #ededea;
    --text-soft: #d6d5d0;
    --muted: #98968d;
    --border: #3e3d3a;
    --border-strong: #4a4945;
    --accent: #d97757;
    --accent-bright: #e89377;
    --accent-bright-hover: #dc815f;
    --accent-soft: rgba(217, 119, 87, 0.14);
    --hover: #3a3a37;
    --kbd-bg: #3a3a37;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.app-shell {
  max-width: 1160px;
  margin: 0 auto;
  padding: 24px 32px 40px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 100vh;
}

/* Brand (inside toolbar) */
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px 0 6px;
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 0.8rem;
  letter-spacing: -0.01em;
  color: var(--text);
  white-space: nowrap;
}

.brand-logo {
  width: 18px;
  height: 18px;
  object-fit: contain;
  display: block;
}

.toolbar-spacer {
  flex: 1 1 auto;
  min-width: 8px;
}

/* Buttons */
button {
  appearance: none;
  cursor: pointer;
  font-family: var(--font-ui);
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-soft);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 7px 12px;
  transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease;
  line-height: 1.2;
}

button:hover {
  background: var(--hover);
  color: var(--text);
}

button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-soft);
}

button.ghost:hover {
  background: var(--hover);
}

button.primary {
  background: var(--accent-bright);
  color: #ffffff;
  border-color: var(--accent-bright);
}

button.primary:hover {
  background: var(--accent-bright-hover);
  border-color: var(--accent-bright-hover);
  color: #ffffff;
}

/* Toolbar */
.toolbar {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 2px;
  padding: 6px;
  border-radius: 10px;
  background: var(--panel);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  position: sticky;
  top: 12px;
  z-index: 10;
  backdrop-filter: saturate(1.2) blur(8px);
  overflow-x: auto;
  scrollbar-width: none;
}

.toolbar::-webkit-scrollbar { display: none; }

.toolbar > * { flex-shrink: 0; }

.toolbar button {
  min-width: 34px;
  height: 32px;
  padding: 0 10px;
  border-radius: 6px;
  font-size: 0.85rem;
  color: var(--text-soft);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.toolbar button strong,
.toolbar button em,
.toolbar button s {
  font-size: 0.9rem;
}

.toolbar-icon-btn svg {
  width: 14px;
  height: 14px;
  display: block;
}

.toolbar button.ghost,
.toolbar button.primary {
  min-width: 0;
  padding: 0 12px;
  font-size: 0.82rem;
  font-weight: 500;
}

.toolbar button.primary {
  background: var(--accent-bright);
  color: #ffffff;
  border-color: var(--accent-bright);
}

.toolbar button.primary:hover {
  background: var(--accent-bright-hover);
  color: #ffffff;
  border-color: var(--accent-bright-hover);
}

.toolbar .sep {
  width: 1px;
  align-self: stretch;
  margin: 4px 4px;
  background: var(--border);
}

/* Workspace — sidebar + editor (insets slightly so the toolbar
   reads as a shelf slightly wider than the content underneath) */
.workspace {
  flex: 1;
  display: flex;
  gap: 14px;
  min-height: 0;
  min-width: 0;
  margin: 0 18px;
}

/* Editor */
.editor-shell {
  flex: 1;
  display: flex;
  position: relative;
  margin: 0;
  min-width: 0;
}

.editor {
  flex: 1;
  width: 100%;
  min-height: 65vh;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--panel);
  padding: 64px 72px;
  outline: none;
  overflow: auto;
  line-height: 1.7;
  font-size: var(--font-size-base, 1.0625rem);
  font-family: var(--font-paragraph);
  color: var(--text);
  caret-color: var(--accent);
}

.editor:focus {
  border-color: var(--border-strong);
}

.editor[data-placeholder]:empty::before {
  content: attr(data-placeholder);
  color: var(--muted);
  pointer-events: none;
}

.source-editor {
  flex: 1;
  width: 100%;
  min-height: 65vh;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--panel);
  padding: 32px 40px;
  outline: none;
  overflow: auto;
  resize: none;
  line-height: 1.65;
  font-size: 0.9rem;
  font-family: var(--font-mono);
  color: var(--text);
  caret-color: var(--accent);
  tab-size: 2;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  display: none;
}

.source-editor::placeholder { color: var(--muted); }
.source-editor:focus { border-color: var(--border-strong); }

body.mode-source .editor { display: none; }
body.mode-source .source-editor { display: block; }

.source-highlight {
  position: absolute;
  pointer-events: none;
  background: rgba(250, 204, 21, 0.42);
  border-radius: 4px;
  opacity: 0;
  z-index: 2;
  display: none;
}
body.mode-source .source-highlight { display: block; }
.source-highlight.flash {
  animation: source-highlight-flash 1.8s ease-out forwards;
}
@keyframes source-highlight-flash {
  0%   { opacity: 0; }
  12%  { opacity: 1; }
  55%  { opacity: 0.92; }
  100% { opacity: 0; }
}
body.mode-source .toolbar [data-action],
body.mode-source .toolbar .sep {
  display: none;
}

.toolbar-toggle {
  display: inline-flex !important;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 12px !important;
  width: auto !important;
  min-width: 0 !important;
  font-size: 0.82rem;
  color: var(--text-soft);
}

.toolbar-toggle .toggle-icon {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--muted);
  letter-spacing: -0.02em;
}

.toolbar-toggle .toggle-label {
  font-family: var(--font-ui);
  font-weight: 500;
}

.toolbar-toggle[aria-pressed="true"] {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: transparent;
}

.toolbar-toggle[aria-pressed="true"] .toggle-icon {
  color: var(--accent);
}

/* Typography inside the editor */
.editor h1,
.editor h2,
.editor h3,
.editor h4,
.editor h5,
.editor h6 {
  margin: 1.6em 0 0.5em;
  line-height: 1.25;
  letter-spacing: -0.015em;
  color: var(--text);
  font-weight: 600;
}

.editor h1 { font-family: var(--font-h1); font-size: 2rem; margin-top: 0.4em; }
.editor h2 { font-family: var(--font-h2); font-size: 1.5rem; }
.editor h3 { font-family: var(--font-h3); font-size: 1.2rem; }
.editor h4 { font-family: var(--font-h4); font-size: 1.05rem; }

.editor p {
  margin: 0 0 1em;
}

.editor > *:first-child { margin-top: 0; }
.editor > *:last-child { margin-bottom: 0; }

.editor strong, .editor b { font-weight: 700; }
.editor em, .editor i { font-style: italic; }

.editor a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}

.editor blockquote {
  margin: 1.2em 0;
  padding: 2px 0 2px 20px;
  border-left: 3px solid var(--border-strong);
  color: var(--text-soft);
  font-style: italic;
}

.editor ul,
.editor ol {
  padding-left: 1.4rem;
  margin: 0 0 1em;
}

.editor li { margin: 0.2em 0; }

.editor pre {
  background: var(--panel-subtle);
  color: var(--text);
  padding: 16px 18px;
  border-radius: 10px;
  border: 1px solid var(--border);
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.875rem;
  line-height: 1.55;
  margin: 1em 0;
}

.editor :not(pre) > code {
  background: var(--panel-subtle);
  border: 1px solid var(--border);
  padding: 1px 6px;
  border-radius: 5px;
  font-family: var(--font-mono);
  font-size: 0.88em;
}

.editor hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2em 0;
}

.editor img {
  max-width: 100%;
  border-radius: 8px;
  margin: 1em 0;
}

.editor table {
  border-collapse: collapse;
  margin: 1em 0;
  width: 100%;
}

.editor th, .editor td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}

.editor th { background: var(--panel-subtle); font-weight: 600; }

/* Footer */
.footer-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 4px 2px;
  font-size: 0.8rem;
  color: var(--muted);
}

.footer-bar .hint {
  color: var(--muted);
}

kbd {
  display: inline-block;
  padding: 1px 6px;
  background: var(--kbd-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--text-soft);
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(29, 28, 27, 0.32);
  backdrop-filter: blur(4px);
  z-index: 50;
  padding: 20px;
}

.modal.open { display: flex; }

.modal-panel {
  width: min(580px, 100%);
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.18);
  overflow: hidden;
  animation: modalIn 180ms ease-out;
}

@keyframes modalIn {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.005em;
}

.modal-body {
  padding: 18px 20px 20px;
  display: grid;
  gap: 18px;
}

.font-section {
  display: grid;
  gap: 10px;
}

.font-section-title {
  margin: 0;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.font-ribbon {
  display: grid;
  grid-template-columns: 1fr 140px;
  gap: 10px;
}

.heading-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

@media (max-width: 520px) {
  .font-ribbon,
  .heading-grid {
    grid-template-columns: 1fr;
  }
}

.modal-footer {
  padding: 14px 20px;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  border-top: 1px solid var(--border);
  background: var(--panel-subtle);
}

.font-setting-row {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.font-setting-row label {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.font-select {
  appearance: none;
  -webkit-appearance: none;
  width: 100%;
  padding: 9px 34px 9px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background-color: var(--panel-subtle);
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path fill='none' stroke='%237a7872' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round' d='M3 4.5l3 3 3-3'/></svg>");
  background-repeat: no-repeat;
  background-position: right 10px center;
  color: var(--text);
  font-size: 0.9rem;
  font-family: var(--font-ui);
  cursor: pointer;
  transition: border-color 120ms ease, background-color 120ms ease;
}

.font-select:hover { border-color: var(--border-strong); }

.font-select:focus {
  outline: none;
  border-color: var(--accent);
  background-color: var(--panel);
}

@media (prefers-color-scheme: dark) {
  .font-select {
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path fill='none' stroke='%2398968d' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round' d='M3 4.5l3 3 3-3'/></svg>");
  }
}

.icon-button {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
}

.icon-button:hover {
  background: var(--hover);
  color: var(--text);
}

/* Scrollbar */
.editor::-webkit-scrollbar { width: 10px; }
.editor::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 10px;
  border: 2px solid var(--panel);
}
.editor::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* ---------------------------------------------------------
   File manager (sidebar + tree + empty states + menu)
---------------------------------------------------------- */
.sidebar {
  flex: 0 0 260px;
  max-width: 260px;
  display: none;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}

body.sidebar-open .sidebar { display: flex; }

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 10px 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-subtle);
}

.sidebar-title {
  margin: 0;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: -0.005em;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
}

.sidebar-actions {
  display: inline-flex;
  gap: 2px;
  flex-shrink: 0;
  align-items: center;
}

.sidebar-actions-sep {
  width: 1px;
  align-self: stretch;
  margin: 4px 4px;
  background: var(--border);
}

.sidebar-actions .icon-button {
  width: 26px;
  height: 26px;
  min-width: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  color: var(--muted);
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 0;
}

.sidebar-actions .icon-button:hover:not(:disabled) {
  background: var(--hover);
  color: var(--text);
}

.sidebar-actions .icon-button:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.sidebar-body {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.sidebar-body::-webkit-scrollbar { width: 8px; }
.sidebar-body::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 6px;
  border: 2px solid var(--panel);
}

/* File tree */
.file-tree {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.tree-node {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.82rem;
  color: var(--text-soft);
  user-select: none;
  line-height: 1.2;
}

.tree-node:hover { background: var(--hover); color: var(--text); }

.tree-node.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.tree-node.active .tree-name { font-weight: 600; }

.tree-caret {
  width: 10px;
  flex: 0 0 auto;
  text-align: center;
  color: var(--muted);
  font-size: 0.68rem;
  line-height: 1;
}

.tree-icon {
  flex: 0 0 auto;
  width: 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
}

.tree-node.active .tree-icon { color: var(--accent); }

.tree-name {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tree-dirty {
  flex: 0 0 auto;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0;
}

.tree-node.dirty .tree-dirty { opacity: 1; }

/* Empty states */
.sidebar-empty {
  padding: 28px 18px;
  text-align: center;
  color: var(--text-soft);
}

.sidebar-empty .empty-icon {
  color: var(--muted);
  margin-bottom: 12px;
  display: inline-flex;
  font-size: 1.2rem;
}

.sidebar-empty h3 {
  margin: 0 0 6px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
}

.sidebar-empty p {
  margin: 0 0 8px;
  font-size: 0.82rem;
  line-height: 1.5;
}

.sidebar-empty code { font-size: 0.78em; padding: 1px 5px; }

.sidebar-empty .empty-hint {
  margin-top: 14px;
  font-size: 0.75rem;
  color: var(--muted);
}

.sidebar-empty .empty-cta {
  margin-top: 10px;
  padding: 8px 14px;
  font-size: 0.85rem;
  font-weight: 500;
  border-radius: 8px;
  background: var(--text);
  color: var(--bg);
  border: 1px solid var(--text);
  cursor: pointer;
  font-family: var(--font-ui);
}

.sidebar-empty .empty-cta:hover {
  background: var(--text-soft);
  border-color: var(--text-soft);
}

.sidebar-empty-unsupported .empty-icon {
  color: var(--accent);
}

/* Context menu */
.tree-menu {
  position: fixed;
  z-index: 60;
  min-width: 160px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.14);
  padding: 4px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

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

.tree-menu button {
  background: transparent;
  border: none;
  text-align: left;
  padding: 7px 12px;
  border-radius: 6px;
  color: var(--text);
  font-size: 0.85rem;
  font-family: var(--font-ui);
  cursor: pointer;
}

.tree-menu button:hover:not(:disabled) { background: var(--hover); }
.tree-menu button:disabled { color: var(--muted); cursor: not-allowed; }

/* Toggle button active state */
.sidebar-toggle-btn[aria-pressed="true"] {
  background: var(--accent-soft);
  color: var(--accent);
}

/* Hide entirely in demo mode (landing iframe) */
body.demo-mode .sidebar,
body.demo-mode .sidebar-toggle-btn,
body.demo-mode .tree-menu {
  display: none !important;
}

/* Responsive */
@media (max-width: 900px) {
  .editor { padding: 48px 40px; }
}

@media (max-width: 720px) {
  .app-shell { padding: 16px; }
  .editor { padding: 32px 20px; font-size: 1rem; }
  .topbar { padding-bottom: 10px; }
  .toolbar { top: 8px; }
  body.sidebar-open .sidebar {
    flex-basis: 200px;
    max-width: 200px;
  }
}

@media (max-width: 560px) {
  body.sidebar-open .workspace {
    flex-direction: column;
  }
  body.sidebar-open .sidebar {
    flex-basis: auto;
    max-width: 100%;
    max-height: 40vh;
  }
}

/* Demo mode — editor rendered inside the landing iframe.
   Keeps the toolbar on a single row and trims chrome. */
body.demo-mode .app-shell {
  padding: 12px 16px 16px;
  max-width: 100%;
  gap: 10px;
  min-height: 100vh;
}

body.demo-mode .toolbar {
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  top: 0;
}

body.demo-mode .toolbar::-webkit-scrollbar { display: none; }

body.demo-mode .toolbar button,
body.demo-mode .toolbar .brand {
  flex: 0 0 auto;
}

body.demo-mode .toolbar button {
  padding: 0 8px;
  min-width: 30px;
}

body.demo-mode .toolbar button.ghost,
body.demo-mode .toolbar button.primary,
body.demo-mode .toolbar-toggle {
  padding: 0 10px !important;
}

body.demo-mode .toolbar .sep {
  margin: 4px 2px;
}

body.demo-mode .brand {
  padding: 0 6px 0 4px;
}

body.demo-mode .editor-shell {
  margin: 0;
}

body.demo-mode .editor,
body.demo-mode .source-editor {
  min-height: 52vh;
}

body.demo-mode .editor {
  padding: 40px 48px;
}
