:root {
  color-scheme: light;
  --bg: #f6f7f4;
  --code-bg: #1f2420;
  --code-text: #f4f7f2;
  --field-bg: #fbfcfa;
  --hover-bg: #f7faf7;
  --panel-soft: #fbfcfa;
  --panel: #ffffff;
  --raised: #ffffff;
  --reader-quote-bg: #f7faf7;
  --resizer-bg: #f3f7f2;
  --text: #1f2420;
  --muted: #677067;
  --line: #d9dfd7;
  --accent: #6d3fd1;
  --accent-strong: #4f2aa6;
  --accent-soft: #ece7fb;
  --blue: #355c9a;
  --danger: #a63838;
  --danger-hover: #f0d1ce;
  --danger-line: #e3b4b1;
  --danger-soft: #f5e0df;
  --image-bg: #f5f7f4;
  --math-bg: #f2f6fb;
  --math-text: #18365f;
  --shadow: 0 18px 50px rgba(31, 36, 32, 0.12);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #08070d;
  --code-bg: #030306;
  --code-text: #f4f1ff;
  --field-bg: #0f0d16;
  --hover-bg: #1a1626;
  --panel-soft: #0f0d16;
  --panel: #14111d;
  --raised: #1a1624;
  --reader-quote-bg: #1a1626;
  --resizer-bg: #171320;
  --text: #f5f2fb;
  --muted: #aaa2ba;
  --line: #2c2638;
  --accent: #a98cff;
  --accent-strong: #c8b8ff;
  --accent-soft: #2a2142;
  --blue: #9bbcff;
  --danger: #ff9c9c;
  --danger-hover: #523036;
  --danger-line: #72424a;
  --danger-soft: #412529;
  --image-bg: #0f0d16;
  --math-bg: #1a2235;
  --math-text: #c8d8ff;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.34);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
  gap: 16px;
  min-height: 100vh;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.app-shell.sidebar-collapsed {
  grid-template-columns: minmax(0, 1fr);
}

.app-shell.sidebar-collapsed .library-panel {
  display: none;
}

.library-panel,
.editor-panel,
.preview-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.library-panel {
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 32px);
  border-radius: 8px;
  overflow: hidden;
}

.brand-row,
.editor-topbar,
.preview-header,
.status-row,
.stats-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand-row {
  padding: 22px 22px 16px;
}

.sidebar-actions {
  display: flex;
  gap: 8px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(1.55rem, 4vw, 2rem);
  line-height: 1.05;
}

h2 {
  margin-bottom: 0;
  font-size: 1.45rem;
  line-height: 1.2;
}

label {
  display: grid;
  gap: 8px;
  color: var(--muted);
  font-size: 0.83rem;
  font-weight: 700;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--field-bg);
  color: var(--text);
  outline: none;
  transition: border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

input {
  min-height: 44px;
  padding: 0 12px;
}

textarea {
  min-height: 320px;
  resize: vertical;
  padding: 14px;
  line-height: 1.65;
}

#bodyInput {
  min-height: clamp(460px, 58vh, 760px);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

input:focus,
textarea:focus {
  border-color: var(--accent);
  background: var(--raised);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.search-field {
  padding: 0 22px 16px;
}

.stats-row {
  padding: 0 22px 14px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

.auth-panel {
  display: grid;
  gap: 10px;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: var(--panel-soft);
  padding: 14px 22px;
}

.auth-panel span {
  color: var(--text);
  font-size: 0.84rem;
  font-weight: 800;
}

.auth-panel p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.auth-email-field {
  gap: 6px;
}

.auth-email-field input {
  min-height: 38px;
}

.auth-actions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px;
}

.post-list {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  padding: 0 12px 14px;
}

.post-item {
  display: grid;
  gap: 6px;
  width: 100%;
  min-height: 92px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  padding: 12px;
  color: inherit;
  text-align: left;
}

.post-item:hover,
.post-item:focus-visible,
.post-item.active {
  border-color: var(--line);
  background: var(--hover-bg);
}

.post-item.active {
  border-color: var(--accent);
  box-shadow: inset 4px 0 0 var(--accent);
}

.post-item-title {
  overflow: hidden;
  color: var(--text);
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.post-item-meta,
.post-item-tags {
  overflow: hidden;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.post-item-tags {
  color: var(--blue);
}

.editor-panel {
  display: grid;
  grid-template-columns: minmax(0, var(--editor-pane, 62%)) 8px minmax(280px, 1fr);
  gap: 0;
  height: calc(100vh - 32px);
  min-height: calc(100vh - 32px);
  border-radius: 8px;
  overflow: hidden;
}

.reader-panel {
  display: flex;
  grid-column: 1 / -1;
  align-items: center;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  padding: 32px clamp(18px, 5vw, 56px);
}

.reader-topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  width: min(100%, 900px);
  border-bottom: 1px solid var(--line);
  padding-bottom: 18px;
}

.reader-meta {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 800;
}

.reader-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  width: min(100%, 900px);
  min-height: 28px;
  padding: 16px 0 4px;
}

.reader-tags span {
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  padding: 4px 9px;
  font-size: 0.78rem;
  font-weight: 800;
}

.reader-content {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  width: min(100%, 900px);
  max-width: 900px;
  padding: 22px 0 0;
}

.editor-form {
  display: flex;
  flex-direction: column;
  gap: 18px;
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  padding: 24px;
}

.editor-topbar {
  align-items: flex-start;
  border-bottom: 1px solid var(--line);
  padding-bottom: 18px;
}

.split-resizer {
  position: relative;
  min-width: 8px;
  border-right: 1px solid var(--line);
  border-left: 1px solid var(--line);
  background: var(--resizer-bg);
  cursor: col-resize;
}

.split-resizer::before {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 2px;
  height: 46px;
  border-radius: 999px;
  background: var(--muted);
  content: "";
  opacity: 0.42;
  transform: translate(-50%, -50%);
}

.split-resizer:hover,
.split-resizer:focus-visible {
  background: var(--accent-soft);
  outline: none;
}

.split-resizer:hover::before,
.split-resizer:focus-visible::before {
  background: var(--accent);
  opacity: 1;
}

.button-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.editor-toolbar {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(38px, 1fr));
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-soft);
  padding: 6px;
}

.primary-button,
.ghost-button,
.danger-button,
.icon-button,
.tool-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 0 14px;
  font-weight: 800;
  transition: transform 140ms ease, background 140ms ease, border-color 140ms ease;
}

.primary-button svg,
.ghost-button svg,
.danger-button svg,
.icon-button svg,
.tool-button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.primary-button {
  background: var(--accent);
  color: #fff;
}

.primary-button:hover {
  background: var(--accent-strong);
}

.ghost-button {
  border-color: var(--line);
  background: var(--raised);
  color: var(--text);
}

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

.danger-button {
  background: var(--danger-soft);
  color: var(--danger);
}

.danger-button:hover {
  border-color: var(--danger-line);
  background: var(--danger-hover);
}

.icon-button {
  width: 42px;
  min-width: 42px;
  padding: 0;
  background: var(--accent);
  color: #fff;
}

.icon-button:hover {
  background: var(--accent-strong);
}

.tool-button {
  min-height: 36px;
  border-color: transparent;
  background: transparent;
  color: var(--text);
  padding: 0;
}

.tool-button:hover,
.tool-button:focus-visible {
  border-color: var(--line);
  background: var(--raised);
}

.tool-button strong,
.tool-button em {
  font-size: 0.95rem;
}

.primary-button:active,
.ghost-button:active,
.danger-button:active,
.icon-button:active,
.tool-button:active {
  transform: translateY(1px);
}

.form-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(180px, 0.8fr);
  gap: 14px;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 9px;
  color: var(--text);
}

.checkbox-row input {
  width: 18px;
  min-height: 18px;
  accent-color: var(--accent);
}

.attachment-panel,
.version-panel {
  display: grid;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
  padding: 14px;
}

.attachment-header,
.version-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.attachment-header span,
.version-header span {
  color: var(--text);
  font-size: 0.86rem;
  font-weight: 800;
}

.attachment-header p,
.version-header p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.attachment-list {
  display: grid;
  gap: 8px;
}

.version-list {
  display: grid;
  gap: 8px;
  max-height: 220px;
  overflow-y: auto;
}

.version-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--raised);
  padding: 10px;
}

.version-info {
  display: grid;
  min-width: 0;
  gap: 4px;
}

.version-title {
  overflow: hidden;
  color: var(--text);
  font-size: 0.88rem;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.version-meta {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 700;
}

.attachment-item {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) 36px;
  align-items: center;
  gap: 10px;
  min-height: 54px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--raised);
  padding: 8px;
}

.attachment-icon,
.attachment-remove {
  width: 36px;
  height: 36px;
  min-width: 36px;
}

.attachment-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  background: var(--accent-soft);
  color: var(--accent);
}

.attachment-icon svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.attachment-info {
  display: grid;
  min-width: 0;
  gap: 3px;
}

.attachment-name {
  overflow: hidden;
  color: var(--blue);
  font-size: 0.9rem;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attachment-meta,
.attachment-empty {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.attachment-empty {
  margin: 0;
}

.attachment-remove {
  background: var(--raised);
  color: var(--danger);
}

.attachment-remove:hover {
  background: var(--danger-soft);
}

.body-stats {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.preview-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  border-width: 0 0 0 1px;
  border-radius: 0;
  box-shadow: none;
}

.preview-header {
  border-bottom: 1px solid var(--line);
  padding: 20px 22px 14px;
}

.preview-header span {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
}

.preview {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 24px 22px;
  line-height: 1.75;
}

.preview h1,
.preview h2,
.preview h3 {
  margin: 0 0 12px;
  line-height: 1.22;
}

.preview h1 {
  font-size: 1.8rem;
}

.post-subtitle {
  color: var(--muted);
  font-size: 1.02rem;
  font-weight: 700;
  line-height: 1.6;
}

.preview h2 {
  margin-top: 22px;
  font-size: 1.3rem;
}

.preview p,
.preview ul,
.preview ol,
.preview blockquote,
.preview pre,
.preview table {
  margin-bottom: 16px;
}

.preview .blank-line {
  height: 1em;
}

.preview ul,
.preview ol {
  padding-left: 1.2rem;
}

.preview li > ul,
.preview li > ol {
  margin-top: 6px;
  margin-bottom: 4px;
  padding-left: 1.35rem;
}

.preview li {
  margin-bottom: 6px;
}

.preview a {
  color: var(--blue);
  font-weight: 800;
}

.preview blockquote {
  border-left: 4px solid var(--accent);
  margin-left: 0;
  padding: 10px 14px;
  background: var(--hover-bg);
  color: var(--text);
}

.toggle-block {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
  margin-bottom: 16px;
}

.toggle-block summary {
  cursor: pointer;
  padding: 12px 14px;
  color: var(--text);
  font-weight: 800;
}

.toggle-block summary:hover {
  background: var(--resizer-bg);
}

.toggle-content {
  border-top: 1px solid var(--line);
  padding: 14px;
}

.preview pre,
.preview code {
  border-radius: 6px;
  background: var(--code-bg);
  color: var(--code-text);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}

.preview code {
  padding: 2px 5px;
  font-size: 0.9em;
}

.preview pre {
  overflow-x: auto;
  padding: 14px;
}

.preview pre code {
  padding: 0;
  background: transparent;
}

.preview table {
  width: 100%;
  border-collapse: collapse;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 0.92rem;
}

.preview th,
.preview td {
  border-bottom: 1px solid var(--line);
  padding: 9px 10px;
  text-align: left;
  vertical-align: top;
}

.preview th {
  background: var(--resizer-bg);
  font-weight: 800;
}

.preview tr:last-child td {
  border-bottom: 0;
}

.inline-image {
  display: block;
  width: 100%;
  max-height: 520px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--image-bg);
}

.math {
  border-radius: 4px;
  background: var(--math-bg);
  color: var(--math-text);
  padding: 0.02em 0.28em;
}

.math-inline {
  display: inline-flex;
  align-items: center;
  vertical-align: -0.12em;
}

.math-block {
  display: flex;
  justify-content: center;
  overflow-x: auto;
  margin: 18px 0;
  padding: 14px;
}

.math math {
  color: inherit;
  font-size: 1.05em;
}

.math-block math {
  min-width: max-content;
  font-size: 1.2rem;
}

.math mtd {
  padding: 0.12em 0.45em;
}

.math-error {
  color: var(--danger);
  background: var(--danger-soft);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  white-space: pre-wrap;
}

.preview-check {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: inherit;
  font-size: inherit;
  font-weight: inherit;
}

.preview-check input {
  width: 16px;
  min-height: 16px;
  accent-color: var(--accent);
}

.preview-attachments {
  border-top: 1px solid var(--line);
  margin-top: 26px;
  padding-top: 18px;
}

.preview-attachments h2 {
  margin: 0 0 12px;
  font-size: 1.05rem;
}

.preview-attachments ul {
  display: grid;
  gap: 8px;
  padding: 0;
  list-style: none;
}

.preview-attachments li {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
  padding: 10px 12px;
}

.preview-attachments a {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preview-attachments span {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
}

.preview-empty {
  color: var(--muted);
}

@media (max-width: 980px) {
  .app-shell,
  .editor-panel {
    grid-template-columns: 1fr;
  }

  .split-resizer {
    display: none;
  }

  .library-panel,
  .editor-panel {
    height: auto;
    min-height: auto;
  }

  .post-list {
    max-height: 360px;
  }

  .preview-panel {
    border-top: 1px solid var(--line);
    border-left: 0;
  }
}

@media (max-width: 640px) {
  .app-shell {
    padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  }

  .brand-row,
  .editor-form,
  .preview {
    padding-right: 16px;
    padding-left: 16px;
  }

  .editor-topbar,
  .reader-topbar,
  .status-row,
  .attachment-header,
  .version-header,
  .preview-attachments li {
    align-items: stretch;
    flex-direction: column;
  }

  .editor-toolbar {
    grid-template-columns: repeat(6, minmax(36px, 1fr));
  }

  .button-group,
  .status-row .danger-button {
    width: 100%;
  }

  .primary-button,
  .ghost-button,
  .danger-button {
    flex: 1;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }

  .reader-panel {
    padding: 18px 16px;
  }

  .preview-attachments li {
    display: flex;
  }
}
