:root {
  --font-ui: "Avenir Next", "Nunito Sans", "Segoe UI", sans-serif;
  --font-editor: "Iowan Old Style", "Book Antiqua", "Palatino Linotype", serif;
  --font-mono: "IBM Plex Mono", "Consolas", monospace;
  --sidebar-width: 286px;
  --sidebar-resizer-width: 8px;
  --bg: #edf3ff;
  --bg-accent: #eefaf2;
  --surface: rgba(255, 255, 255, 0.83);
  --surface-strong: #ffffff;
  --surface-muted: #f2f6fa;
  --text: #173147;
  --muted: #607585;
  --border: #d7e0ea;
  --accent: #136fa7;
  --accent-soft: #e3f2fc;
  --danger: #bf3f4a;
  --code-bg: #f0f7ff;
  --success: #0a7b61;
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: var(--font-ui);
  color: var(--text);
  background: linear-gradient(145deg, var(--bg) 0%, var(--bg-accent) 100%);
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image: radial-gradient(circle at 10% 20%, rgba(255, 255, 255, 0.45) 0%, transparent 33%),
    radial-gradient(circle at 90% 80%, rgba(255, 255, 255, 0.3) 0%, transparent 25%);
}

.app {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: var(--sidebar-width) var(--sidebar-resizer-width) minmax(0, 1fr);
  height: 100vh;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 180ms ease, transform 180ms ease;
}

body.ready .app {
  opacity: 1;
  transform: translateY(0);
}

.sidebar {
  background: var(--surface-strong);
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.sidebar-resizer {
  position: relative;
  cursor: col-resize;
  user-select: none;
  touch-action: none;
}

.sidebar-resizer::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 1px;
  background: var(--border);
  transition: background-color 120ms ease, width 120ms ease;
}

.sidebar-resizer:hover::before,
.sidebar-resizer:focus-visible::before,
body.resizing-sidebar .sidebar-resizer::before {
  width: 2px;
  background: color-mix(in srgb, var(--accent) 58%, var(--border));
}

.sidebar-resizer:focus-visible {
  outline: none;
}

body.resizing-sidebar,
body.resizing-sidebar * {
  cursor: col-resize !important;
  user-select: none !important;
}

.sidebar-header {
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header-actions {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
}

.sidebar-header .autosave-folder-btn {
  flex: 1;
  min-width: 0;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar button,
.workspace button,
.workspace select,
.menu > summary {
  border: 1px solid var(--border);
  background: var(--surface-muted);
  color: var(--text);
  border-radius: 8px;
  font: inherit;
  font-size: 13px;
  line-height: 1;
  padding: 8px 10px;
}

.sidebar button:hover,
.workspace button:hover,
.workspace select:hover,
.menu > summary:hover {
  border-color: color-mix(in srgb, var(--accent) 50%, var(--border));
}

.primary-btn {
  background: color-mix(in srgb, var(--accent) 20%, white);
}

.danger-btn {
  color: var(--danger);
}

.page-tree {
  flex: 1;
  overflow: auto;
  padding: 8px 8px 16px;
  --drop-gap-size: 20px;
}

.tree-branch {
  list-style: none;
  margin: 0;
  padding: 0;
}

.tree-node {
  margin: 2px 0;
  transition:
    margin-top 150ms cubic-bezier(0.22, 0.76, 0.24, 1),
    margin-bottom 150ms cubic-bezier(0.22, 0.76, 0.24, 1);
}

.page-row {
  display: flex;
  align-items: center;
  gap: 6px;
  border-radius: 8px;
  transition:
    transform 150ms cubic-bezier(0.22, 0.76, 0.24, 1),
    box-shadow 120ms ease,
    background-color 120ms ease,
    opacity 120ms ease;
}

.page-row.dragging {
  opacity: 0.45;
  transform: scale(0.985);
}

.tree-node.drop-before {
  margin-top: calc(2px + var(--drop-gap-size));
}

.tree-node.drop-before > .page-row {
  box-shadow: inset 0 2px 0 var(--accent);
  transform: translateY(1px);
}

.page-row.drop-before {
  box-shadow: inset 0 2px 0 var(--accent);
}

.tree-node.drop-after {
  margin-bottom: calc(2px + var(--drop-gap-size));
}

.tree-node.drop-after > .page-row {
  box-shadow: inset 0 -2px 0 var(--accent);
  transform: translateY(-1px);
}

.page-row.drop-after {
  box-shadow: inset 0 -2px 0 var(--accent);
}

.tree-node.drop-inside > .page-row {
  background: color-mix(in srgb, var(--accent-soft) 70%, transparent);
  transform: translateX(12px);
}

.page-row.drop-inside {
  background: color-mix(in srgb, var(--accent-soft) 70%, transparent);
}

.page-tree.drop-root-end > .tree-branch:last-child::after {
  content: "";
  display: block;
  height: var(--drop-gap-size);
  margin: 6px 2px 0;
  border-radius: 8px;
  border: 1px dashed color-mix(in srgb, var(--accent) 60%, var(--border));
  background: color-mix(in srgb, var(--accent-soft) 35%, transparent);
}

.page-link {
  flex: 1;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 8px;
  padding: 8px 8px;
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}

.page-link:hover {
  background: color-mix(in srgb, var(--accent-soft) 44%, transparent);
}

.page-link.active {
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 40%, var(--border));
  font-weight: 600;
}

.page-link.pinned {
  font-weight: 600;
}

.page-title-input {
  flex: 1;
  border: 1px solid color-mix(in srgb, var(--accent) 45%, var(--border));
  background: var(--surface-strong);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 8px;
  font: inherit;
  font-size: 13px;
  line-height: 1.2;
  outline: none;
}

.page-title-input:focus {
  border-color: color-mix(in srgb, var(--accent) 65%, var(--border));
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent-soft) 65%, transparent);
}

.workspace {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.menu {
  position: relative;
}

.menu > summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
}

.menu > summary::-webkit-details-marker {
  display: none;
}

.menu[open] > summary {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
  background: color-mix(in srgb, var(--accent-soft) 50%, var(--surface-muted));
}

.compact-menu > summary {
  width: 34px;
  min-width: 34px;
  padding: 8px 0;
  text-align: center;
  line-height: 1;
}

.menu-pop {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 176px;
  background: var(--surface-strong);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px;
  box-shadow: 0 12px 30px rgba(10, 30, 52, 0.13);
  display: grid;
  gap: 4px;
  z-index: 80;
}

.menu-pop button,
.menu-pop select {
  width: 100%;
  text-align: left;
}

.theme-menu-pop {
  min-width: 260px;
}

.theme-option.is-active {
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
  font-weight: 600;
}

.menu-pop-form {
  min-width: 214px;
}

.menu-pop-form label {
  color: var(--muted);
  font-size: 12px;
  display: grid;
  gap: 4px;
  padding: 4px;
}

.full-btn {
  width: 100%;
}

.dark-mode-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 7px 10px;
  text-align: center;
}

.dark-mode-toggle .mode-icon {
  width: 18px;
  height: 18px;
  display: block;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.full-screen-toggle,
.view-toggle {
  white-space: nowrap;
}

.topbar {
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  backdrop-filter: blur(8px);
}

.topbar-group {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.mode-toggle {
  border-radius: 999px;
  font-weight: 600;
  min-width: 118px;
  background: var(--accent-soft);
  color: var(--accent);
}

.wp-btn {
  background: color-mix(in srgb, var(--accent) 22%, var(--surface-muted));
}

.active-path {
  color: var(--muted);
  font-size: 12px;
  max-width: 240px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

.topbar-actions {
  margin-left: auto;
}

.status {
  color: var(--muted);
  font-size: 12px;
  min-width: 160px;
  text-align: right;
}

.format-toolbar {
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 6px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-wrap: wrap;
}

.format-toolbar button {
  min-width: 34px;
}

.picker {
  position: relative;
  display: inline-flex;
  flex-direction: column;
  align-items: stretch;
}

.picker > button {
  min-width: 58px;
  width: 100%;
}

.picker.open > button {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

.picker-menu {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  min-width: 0;
  display: grid;
  gap: 0;
  padding: 0;
  border: 1px solid var(--border);
  border-top: 0;
  border-radius: 0 0 10px 10px;
  background: var(--surface-strong);
  box-shadow: 0 10px 24px rgba(10, 30, 52, 0.13);
  z-index: 90;
}

.picker-menu[hidden] {
  display: none !important;
}

.picker-menu button {
  width: 100%;
  border: 0;
  border-top: 1px solid var(--border);
  border-radius: 0;
  text-align: center;
}

.picker-menu button:last-child {
  border-bottom-left-radius: 10px;
  border-bottom-right-radius: 10px;
}

.emphasis-picker > button strong,
.emphasis-menu button em,
.emphasis-menu button .format-glyph {
  display: inline-block;
  min-width: 1ch;
}

.emphasis-menu button .format-glyph {
  text-decoration: underline;
}

.emphasis-menu button[data-emphasis-action="strike"] .format-glyph {
  text-decoration: line-through;
}

.toolbar-menu > summary {
  white-space: nowrap;
}

.toolbar-menu .menu-pop {
  left: 0;
  right: auto;
  min-width: 184px;
}

.toolbar-hint {
  margin-left: auto;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.page-title-bar {
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  padding: 10px 18px 9px;
}

.page-title-heading {
  margin: 0;
  font-family: var(--font-editor);
  font-size: 20px;
  line-height: 1.2;
  color: var(--text);
  font-weight: 600;
}

.editor-layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}

.panel {
  min-height: 0;
  overflow: auto;
}

.editor-panel {
  border-right: 1px solid var(--border);
  background: var(--surface-strong);
}

.editor {
  width: 100%;
  height: 100%;
  border: 0;
  outline: 0;
  color: var(--text);
  padding: 28px clamp(16px, 4vw, 58px) 42px;
}

.wysiwyg-editor {
  font-family: var(--font-editor);
  font-size: 19px;
  line-height: 1.55;
}

.wysiwyg-editor:empty::before {
  content: attr(data-placeholder);
  color: color-mix(in srgb, var(--muted) 68%, white);
}

.markdown-editor {
  resize: none;
  background: color-mix(in srgb, var(--surface-strong) 75%, #f6fbff);
  font-family: var(--font-mono);
  font-size: 15px;
  line-height: 1.6;
}

.preview-panel {
  background: color-mix(in srgb, var(--surface-strong) 78%, #f7fffb);
  font-family: var(--font-editor);
  font-size: 19px;
  line-height: 1.55;
  padding: 28px clamp(16px, 4vw, 58px) 42px;
}

.preview-panel h1,
.preview-panel h2,
.preview-panel h3,
.wysiwyg-editor h1,
.wysiwyg-editor h2,
.wysiwyg-editor h3 {
  margin: 1.2em 0 0.4em;
  line-height: 1.2;
}

.preview-panel h1:first-child,
.preview-panel h2:first-child,
.preview-panel h3:first-child,
.wysiwyg-editor h1:first-child,
.wysiwyg-editor h2:first-child,
.wysiwyg-editor h3:first-child {
  margin-top: 0;
}

.preview-panel p,
.wysiwyg-editor p {
  margin: 0 0 0.9em;
}

.preview-panel ul,
.preview-panel ol,
.wysiwyg-editor ul,
.wysiwyg-editor ol {
  margin: 0 0 0.9em;
  padding-left: 1.25em;
}

.preview-panel code,
.wysiwyg-editor code {
  font-family: var(--font-mono);
  background: var(--code-bg);
  border-radius: 5px;
  padding: 0.08em 0.25em;
}

.preview-panel pre,
.wysiwyg-editor pre {
  font-family: var(--font-mono);
  font-size: 14px;
  border: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface-muted) 75%, white);
  padding: 12px 14px;
  border-radius: 8px;
  overflow: auto;
}

.preview-panel pre code,
.wysiwyg-editor pre code {
  background: none;
  padding: 0;
}

.preview-panel blockquote,
.wysiwyg-editor blockquote {
  margin: 0 0 1em;
  border-left: 4px solid color-mix(in srgb, var(--accent) 40%, var(--border));
  padding: 0.2em 0 0.2em 1em;
  color: var(--muted);
}

.preview-panel a,
.wysiwyg-editor a {
  color: var(--accent);
}

.preview-panel table,
.wysiwyg-editor table {
  width: 100%;
  border-collapse: collapse;
  margin: 0 0 1em;
  font-size: 16px;
}

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

.preview-panel th,
.wysiwyg-editor th {
  background: color-mix(in srgb, var(--accent-soft) 40%, var(--surface-muted));
}

.task-list {
  list-style: none;
  padding-left: 0.3em;
}

.task-list li {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin: 0.2em 0;
}

.task-list input[type="checkbox"] {
  margin-top: 0.32em;
}

.empty-note {
  color: color-mix(in srgb, var(--muted) 75%, white);
}

.wp-footer {
  display: none;
}

body[data-editor-mode="wysiwyg"] .wysiwyg-editor {
  display: block;
}

body[data-editor-mode="wysiwyg"] .markdown-editor {
  display: none;
}

body[data-editor-mode="markdown"] .wysiwyg-editor {
  display: none;
}

body[data-editor-mode="markdown"] .markdown-editor {
  display: block;
}

body[data-view="edit"] .editor-layout {
  grid-template-columns: minmax(0, 1fr);
}

body[data-view="edit"] .preview-panel {
  display: none;
}

body[data-view="preview"] .editor-layout {
  grid-template-columns: minmax(0, 1fr);
}

body[data-view="preview"] .editor-panel {
  display: none;
}

body[data-view="split"] .editor-layout {
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}

body[data-view="preview"] .format-toolbar {
  display: none;
}

body[data-fullscreen="on"] .sidebar,
body[data-fullscreen="on"] .sidebar-resizer,
body[data-fullscreen="on"] .topbar,
body[data-fullscreen="on"] .format-toolbar,
body[data-fullscreen="on"] .page-title-bar,
body[data-fullscreen="on"] .wp-footer {
  display: none !important;
}

body[data-fullscreen="on"] .app {
  grid-template-columns: 1fr;
  height: 100vh;
}

body[data-fullscreen="on"] .workspace {
  min-height: 100vh;
}

body[data-fullscreen="on"] .editor-layout {
  height: 100vh;
}

body[data-theme="paper"] {
  --bg: #ece2d3;
  --bg-accent: #f7efe3;
  --surface: rgba(255, 249, 241, 0.88);
  --surface-strong: #fff8ee;
  --surface-muted: #f5ebdc;
  --text: #3b2a1c;
  --muted: #7a6654;
  --border: #e0cfb6;
  --accent: #9a5b24;
  --accent-soft: #f4dfc6;
  --font-editor: "Palatino Linotype", "Book Antiqua", serif;
  --code-bg: #f5e5d2;
}

body[data-theme="focus"] {
  --sidebar-width: 238px;
  --bg: #f2f6f8;
  --bg-accent: #edf7f1;
  --surface: rgba(255, 255, 255, 0.86);
  --surface-strong: #ffffff;
  --surface-muted: #f2f7fa;
  --text: #1c2e39;
  --muted: #667b89;
  --border: #d7e0e8;
  --accent: #0f7f67;
  --accent-soft: #dbf3ed;
  --code-bg: #d9efe8;
}

body[data-theme="focus"] .editor,
body[data-theme="focus"] .preview-panel {
  width: min(100%, 860px);
  margin: 0 auto;
}

body[data-dark="on"]:not([data-theme="wp51"]) {
  --bg: #0f1621;
  --bg-accent: #111d19;
  --surface: rgba(14, 22, 31, 0.88);
  --surface-strong: #121c27;
  --surface-muted: #1a2736;
  --text: #e2eef7;
  --muted: #9db3c6;
  --border: #253649;
  --accent: #5ca9e0;
  --accent-soft: #183046;
  --danger: #f27a81;
  --code-bg: #1d3042;
}

body[data-dark="on"][data-theme="paper"] {
  --bg: #211a15;
  --bg-accent: #1e241d;
  --surface: rgba(35, 30, 25, 0.9);
  --surface-strong: #2a231d;
  --surface-muted: #342d25;
  --text: #f2e8da;
  --muted: #c8b8a6;
  --border: #4b3f33;
  --accent: #d79d67;
  --accent-soft: #4e3825;
  --code-bg: #4a3b2f;
}

body[data-dark="on"][data-theme="focus"] {
  --bg: #0d1417;
  --bg-accent: #0c1712;
  --surface: rgba(13, 21, 25, 0.9);
  --surface-strong: #101d22;
  --surface-muted: #17292f;
  --text: #ddf2ed;
  --muted: #9dc2bc;
  --border: #274045;
  --accent: #58c5ae;
  --accent-soft: #173739;
  --code-bg: #16373a;
}

body[data-theme="wp51"] {
  --sidebar-width: 270px;
  --font-ui: "IBM Plex Mono", "Courier New", monospace;
  --font-editor: "IBM Plex Mono", "Courier New", monospace;
  --bg: #0000aa;
  --bg-accent: #0000aa;
  --surface: #0000aa;
  --surface-strong: #0000aa;
  --surface-muted: #00008b;
  --text: #c9d7ff;
  --muted: #85b7ff;
  --border: #55ffff;
  --accent: #ffff55;
  --accent-soft: #0034bf;
  --danger: #ffff55;
  --code-bg: #00008b;
}

body[data-theme="wp51"]::before {
  display: none;
}

body[data-theme="wp51"] .sidebar,
body[data-theme="wp51"] .topbar,
body[data-theme="wp51"] .format-toolbar,
body[data-theme="wp51"] .page-title-bar,
body[data-theme="wp51"] .editor-panel,
body[data-theme="wp51"] .preview-panel,
body[data-theme="wp51"] .workspace {
  background: #0000aa;
  border-color: #55ffff;
  border-radius: 0;
  backdrop-filter: none;
}

body[data-theme="wp51"] button,
body[data-theme="wp51"] select,
body[data-theme="wp51"] .menu > summary {
  color: #55ffff;
  background: #00008b;
  border-color: #55ffff;
  border-radius: 0;
  font-family: var(--font-ui);
}

body[data-theme="wp51"] .menu-pop {
  background: #00008b;
  border-color: #55ffff;
  border-radius: 0;
  box-shadow: none;
}

body[data-theme="wp51"] .picker-menu {
  background: #00008b;
  border-color: #55ffff;
  border-radius: 0;
  box-shadow: none;
}

body[data-theme="wp51"] .page-link.active {
  background: #55ffff;
  border-color: #55ffff;
  color: #0000aa;
}

body[data-theme="wp51"] .page-title-input {
  background: #00008b;
  color: #55ffff;
  border-color: #55ffff;
  border-radius: 0;
  box-shadow: none;
}

body[data-theme="wp51"] .status {
  color: #ffff55;
}

body[data-theme="wp51"] .preview-panel a,
body[data-theme="wp51"] .wysiwyg-editor a {
  color: #ffff55;
}

body[data-theme="wp51"] .preview-panel code,
body[data-theme="wp51"] .wysiwyg-editor code,
body[data-theme="wp51"] .preview-panel pre,
body[data-theme="wp51"] .wysiwyg-editor pre {
  color: #55ffff;
  border-color: #55ffff;
  background: #00008b;
}

body[data-theme="wp51"] .preview-panel th,
body[data-theme="wp51"] .preview-panel td,
body[data-theme="wp51"] .wysiwyg-editor th,
body[data-theme="wp51"] .wysiwyg-editor td {
  border-color: #55ffff;
}

body[data-theme="wp51"] .page-title-heading {
  color: #55ffff;
}

body[data-theme="wp51"] .wp-footer {
  display: flex;
  align-items: center;
  gap: 14px;
  border-top: 1px solid #55ffff;
  background: #55ffff;
  color: #0000aa;
  font-family: var(--font-mono);
  font-size: 12px;
  padding: 6px 10px;
}

.wp-key {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.wp-key span {
  background: #0000aa;
  color: #55ffff;
  padding: 1px 4px;
  line-height: 1;
}

@media (max-width: 1100px) {
  .toolbar-hint {
    display: none;
  }

  .active-path {
    max-width: 160px;
  }
}

@media (max-width: 960px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(190px, 34vh) 1fr;
  }

  .sidebar-resizer {
    display: none;
  }

  .sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .status {
    text-align: left;
    min-width: auto;
  }

  .active-path {
    max-width: 100%;
  }

  body[data-view="split"] .editor-layout {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(260px, 1fr) minmax(260px, 1fr);
  }
}
