* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: Figtree, Roboto, -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: var(--fg);
  background: var(--bg);
  --bg: #ffffff; --surface: #f6f7fb; --fg: #323338; --muted: #676879; --border: #d0d4e4; --primary: #0073ea; --primary-hover: #0060b9;
  --ok: #00854d; --danger: #d83a52; --danger-bg: #fdeeee; --warn-bg: #fff6df; --code-bg: #f6f7fb;
}
body[data-theme="dark"] { --bg: #181b34; --surface: #30324e; --fg: #d5d8df; --muted: #9699a6; --border: #4b4e69; --primary: #0073ea; --primary-hover: #3d8ff0; --ok: #33d391; --danger: #ff7575; --danger-bg: #3a2a35; --warn-bg: #3b3627; --code-bg: #1c1f3b; }
body[data-theme="black"], body[data-theme="hacker"] { --bg: #111111; --surface: #1f1f1f; --fg: #d5d8df; --muted: #9699a6; --border: #3b3b3b; --primary: #0073ea; --primary-hover: #3d8ff0; --ok: #33d391; --danger: #ff7575; --danger-bg: #3a2222; --warn-bg: #332e1f; --code-bg: #181818; }

#app { display: flex; flex-direction: column; height: 100vh; min-height: 0; }
.loading { padding: 24px; color: var(--muted); }
.spacer { flex: 1; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
button, select, textarea { font: inherit; color: inherit; }
button { padding: 7px 14px; border-radius: 4px; border: 0; cursor: pointer; font-weight: 500; line-height: 1.2; }
button.small { padding: 5px 10px; font-size: 13px; }
button:disabled { opacity: .5; cursor: not-allowed; }
.primary { background: var(--primary); color: #fff; }
.primary:hover:not(:disabled) { background: var(--primary-hover); }
.secondary { background: transparent; color: var(--fg); border: 1px solid var(--border); }
.secondary:hover:not(:disabled) { background: var(--surface); }
.subtle { background: transparent; color: var(--muted); border: 1px solid transparent; }
.subtle:hover:not(:disabled) { background: var(--surface); color: var(--fg); }
.select { padding: 5px 8px; border: 1px solid var(--border); border-radius: 4px; background: var(--bg); max-width: 190px; }

/* View mode */
.toolbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--border); }
.brand { display: inline-flex; color: var(--primary); }
.badge { font-size: 12px; color: var(--muted); border: 1px solid var(--border); border-radius: 12px; padding: 2px 8px; }
.menu { display: flex; gap: 2px; }
.banner { padding: 8px 12px; background: var(--warn-bg); font-size: 13px; border-bottom: 1px solid var(--border); }
.stage { flex: 1; min-height: 0; overflow: auto; padding: 16px; display: flex; }
.diagram { width: 100%; margin: auto; }
.align-center { text-align: center; }
.align-left { text-align: left; }
.diagram svg { max-width: 100%; height: auto; display: inline-block; }
.diagram.paper-light { background: #ffffff; color: #323338; border-radius: 8px; padding: 12px; }
.diagram.paper-dark { background: #181b34; color: #d5d8df; border-radius: 8px; padding: 12px; }
body[data-view="dashboard_widget"] .stage { padding: 8px; }
body[data-view="dashboard_widget"] .diagram svg { max-height: calc(100vh - 60px); }
body[data-view="dashboard_widget"] .toolbar { padding: 4px 8px; }
.latex { padding: 8px 0; overflow-x: auto; }
.latex .katex-display { margin: .6em 0; }
.empty { margin: auto; max-width: 520px; text-align: center; padding: 32px 16px; }
.empty-icon { color: var(--primary); }
.empty-icon svg { width: 44px; height: 44px; }
.empty h2 { font-size: 18px; margin: 8px 0 4px; }
.empty p { color: var(--muted); margin: 0 0 16px; }
.empty-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.error { background: var(--danger-bg); color: var(--danger); border-radius: 4px; padding: 12px 16px; text-align: left; margin: 12px; }
.error pre { white-space: pre-wrap; font-family: SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; margin: 8px 0 0; }

/* Edit mode */
header { display: flex; align-items: center; gap: 12px; padding: 8px 12px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.tabs { display: flex; gap: 2px; background: var(--surface); border-radius: 6px; padding: 3px; }
.tab { background: transparent; color: var(--muted); padding: 5px 12px; border-radius: 4px; font-weight: 500; }
.tab.active { background: var(--bg); color: var(--fg); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.extra:empty { display: none; }
main { flex: 1; display: grid; grid-template-columns: minmax(260px, 1fr) minmax(260px, 1.3fr); min-height: 0; }
.pane { min-height: 0; display: flex; flex-direction: column; }
.preview-pane { border-left: 1px solid var(--border); overflow: auto; padding: 16px; display: flex; }
#code { flex: 1; resize: none; border: 0; padding: 14px 16px; outline: none; tab-size: 2; min-height: 120px;
  font-family: SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-size: 13px; line-height: 1.5;
  background: var(--code-bg); color: var(--fg); }
#code::placeholder { color: var(--muted); }
.status { padding: 6px 16px; font-size: 12px; min-height: 28px; max-height: 120px; overflow: auto; white-space: pre-wrap; border-top: 1px solid var(--border); color: var(--muted); }
.status.ok { color: var(--ok); }
.status.error { color: var(--danger); }
footer { display: flex; align-items: center; gap: 12px; padding: 8px 12px; border-top: 1px solid var(--border); }
.hint { color: var(--muted); font-size: 12px; }
@media (max-width: 720px) {
  main { grid-template-columns: 1fr; grid-template-rows: minmax(160px, 1fr) minmax(160px, 1fr); }
  .preview-pane { border-left: 0; border-top: 1px solid var(--border); }
  .hint { display: none; }
}

/* AI panel */
.ai-toggle.active { background: var(--surface); border-color: var(--primary); color: var(--primary); }
.ai-panel { padding: 10px 12px; border-bottom: 1px solid var(--border); background: var(--surface); }
.ai-row { display: flex; gap: 10px; align-items: flex-start; flex-wrap: wrap; }
.ai-prompt { flex: 1; min-width: 240px; resize: vertical; padding: 8px 10px; border: 1px solid var(--border); border-radius: 4px; background: var(--bg); color: var(--fg); font-size: 13px; line-height: 1.4; }
.ai-prompt::placeholder { color: var(--muted); }
.ai-actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.ai-status { font-size: 12px; color: var(--muted); margin-top: 6px; white-space: pre-wrap; max-height: 140px; overflow: auto; }
.ai-status:empty { display: none; }
.ai-status.ok { color: var(--ok); }
.ai-status.error { color: var(--danger); }
