:root {
  color-scheme: light;
  --bg: #f6f8fb;
  --surface: #ffffff;
  --surface-muted: #eef3f7;
  --surface-strong: #e7eef4;
  --text: #182331;
  --muted: #617080;
  --soft: #7d8a99;
  --border: #d8e1ea;
  --accent: #0e7490;
  --accent-strong: #0b5f77;
  --accent-soft: #d9f2f6;
  --warm: #b7791f;
  --mark: #fff0ad;
  --mark-active: #ffd36e;
  --shadow: 0 18px 44px rgba(25, 38, 55, 0.12);
  --radius: 8px;
  --font: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111820;
  --surface: #17212b;
  --surface-muted: #1f2d38;
  --surface-strong: #263642;
  --text: #edf4f8;
  --muted: #aab8c4;
  --soft: #8393a2;
  --border: #334552;
  --accent: #4db6c6;
  --accent-strong: #8bd7e3;
  --accent-soft: #173c46;
  --warm: #e0ad5a;
  --mark: #665318;
  --mark-active: #946d14;
  --shadow: 0 18px 44px rgba(0, 0, 0, 0.3);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  background:
    linear-gradient(180deg, rgba(14, 116, 144, 0.06), transparent 260px),
    var(--bg);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
}

button,
input,
textarea {
  font: inherit;
}

button {
  color: inherit;
}

a {
  color: var(--accent-strong);
}

.app-shell {
  display: grid;
  min-height: 100vh;
  grid-template-rows: auto 1fr auto;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 70px;
  padding: 14px clamp(16px, 3vw, 34px);
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(14px);
}

.brand {
  display: inline-flex;
  align-items: center;
  min-width: 0;
  gap: 11px;
  color: inherit;
  text-decoration: none;
}

.brand img {
  flex: 0 0 auto;
}

.brand span {
  display: grid;
  min-width: 0;
  gap: 1px;
}

.brand strong {
  overflow: hidden;
  font-size: 16px;
  line-height: 1.2;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.brand small {
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toolbar,
.workspace-bar,
.search-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.toolbar {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.button,
.icon-button,
.segmented-control button {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.45) inset;
  cursor: pointer;
  transition: background 150ms ease, border-color 150ms ease, color 150ms ease, transform 150ms ease;
}

.button:hover,
.icon-button:hover,
.segmented-control button:hover {
  border-color: color-mix(in srgb, var(--accent) 42%, var(--border));
  background: var(--surface-muted);
}

.button:active,
.icon-button:active,
.segmented-control button:active {
  transform: translateY(1px);
}

.button:focus-visible,
.icon-button:focus-visible,
.drop-zone:focus-visible,
.segmented-control button:focus-visible,
.search-field input:focus-visible,
.source-pane textarea:focus-visible,
.outline-list button:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 34%, transparent);
  outline-offset: 2px;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 38px;
  padding: 0 13px;
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
}

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

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

.button svg,
.icon-button svg,
.drop-zone svg,
.search-field svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
}

.icon-button {
  position: relative;
  display: inline-grid;
  width: 38px;
  height: 38px;
  place-items: center;
  padding: 0;
}

.icon-button-small {
  width: 32px;
  height: 32px;
}

.icon-button[data-tooltip]::after {
  position: absolute;
  bottom: -34px;
  left: 50%;
  z-index: 30;
  padding: 5px 8px;
  border-radius: 6px;
  background: var(--text);
  color: var(--surface);
  content: attr(data-tooltip);
  font-size: 11px;
  font-weight: 700;
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) translateY(-3px);
  transition: opacity 140ms ease, transform 140ms ease;
  white-space: nowrap;
}

.icon-button[data-tooltip]:hover::after {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.reader-layout {
  display: grid;
  grid-template-columns: minmax(230px, 280px) minmax(0, 1fr);
  gap: 16px;
  width: min(1520px, 100%);
  min-height: 0;
  margin: 0 auto;
  padding: 16px clamp(16px, 3vw, 34px) 18px;
}

.side-panel {
  display: grid;
  align-content: start;
  gap: 12px;
  min-width: 0;
}

.drop-zone,
.panel-section,
.workspace {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.55) inset;
}

.drop-zone {
  display: grid;
  gap: 6px;
  width: 100%;
  min-height: 136px;
  padding: 18px;
  text-align: left;
}

.drop-zone.is-dragging {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.drop-zone-icon {
  display: inline-grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--accent);
  background: var(--surface-muted);
}

.drop-zone strong {
  font-size: 14px;
}

.drop-zone span:last-child {
  color: var(--muted);
  font-size: 12px;
}

.panel-section {
  overflow: hidden;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 43px;
  padding: 0 14px;
  border-bottom: 1px solid var(--border);
}

.panel-heading h2 {
  margin: 0;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.detail-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  margin: 0;
  background: var(--border);
}

.detail-list div {
  min-width: 0;
  padding: 13px 14px;
  background: var(--surface);
}

.detail-list dt {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}

.detail-list dd {
  overflow: hidden;
  margin: 4px 0 0;
  font-size: 14px;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.outline-section {
  min-height: 260px;
}

.outline-list {
  display: grid;
  gap: 2px;
  max-height: min(54vh, 560px);
  overflow: auto;
  padding: 8px;
}

.outline-list button,
.outline-empty {
  width: 100%;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.25;
  text-align: left;
}

.outline-list button {
  display: block;
  padding: 8px 10px;
  cursor: pointer;
}

.outline-list button:hover {
  background: var(--surface-muted);
  color: var(--text);
}

.outline-list button[data-level="2"] {
  padding-left: 20px;
}

.outline-list button[data-level="3"] {
  padding-left: 30px;
}

.outline-list button[data-level="4"],
.outline-list button[data-level="5"],
.outline-list button[data-level="6"] {
  padding-left: 40px;
}

.outline-empty {
  padding: 10px;
}

.workspace {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0;
  min-height: calc(100vh - 124px);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.workspace-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.search-field {
  display: flex;
  align-items: center;
  flex: 1 1 260px;
  min-width: min(100%, 220px);
  min-height: 38px;
  gap: 8px;
  padding: 0 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  background: var(--surface-muted);
}

.search-field input {
  width: 100%;
  min-width: 0;
  border: 0;
  outline: 0;
  color: var(--text);
  background: transparent;
  font-size: 13px;
}

.search-count {
  min-width: 72px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-align: right;
}

.segmented-control {
  display: inline-grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  min-width: 236px;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-muted);
}

.segmented-control button {
  min-height: 30px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.segmented-control button.is-active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(15, 25, 36, 0.08);
}

.document-surface {
  display: grid;
  min-height: 0;
  background: var(--surface-muted);
}

.preview-pane,
.source-pane {
  min-width: 0;
  min-height: 0;
}

.preview-pane {
  overflow: auto;
  padding: clamp(22px, 4vw, 54px);
  background: var(--surface);
}

.source-pane {
  display: none;
  border-left: 1px solid var(--border);
  background: var(--surface);
}

.source-pane textarea {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 520px;
  resize: none;
  border: 0;
  outline: 0;
  padding: clamp(18px, 3vw, 34px);
  color: var(--text);
  background: var(--surface);
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.65;
  tab-size: 2;
}

.workspace[data-mode="source"] .preview-pane {
  display: none;
}

.workspace[data-mode="source"] .source-pane,
.workspace[data-mode="split"] .source-pane {
  display: block;
}

.workspace[data-mode="split"] .document-surface {
  grid-template-columns: minmax(0, 1fr) minmax(320px, 0.82fr);
}

.markdown-body {
  max-width: 900px;
  color: var(--text);
}

.workspace[data-mode="split"] .markdown-body {
  max-width: none;
}

.markdown-body > :first-child {
  margin-top: 0;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  margin: 1.65em 0 0.55em;
  color: var(--text);
  line-height: 1.18;
}

.markdown-body h1 {
  font-size: clamp(30px, 4vw, 48px);
  letter-spacing: 0;
}

.markdown-body h2 {
  padding-bottom: 0.35em;
  border-bottom: 1px solid var(--border);
  font-size: clamp(23px, 3vw, 32px);
}

.markdown-body h3 {
  font-size: 22px;
}

.markdown-body h4 {
  font-size: 18px;
}

.markdown-body p,
.markdown-body li,
.markdown-body td,
.markdown-body th {
  font-size: 16px;
}

.markdown-body p {
  max-width: 74ch;
  margin: 0.9em 0;
}

.markdown-body ul,
.markdown-body ol {
  padding-left: 1.35em;
}

.markdown-body li + li {
  margin-top: 0.3em;
}

.markdown-body blockquote {
  margin: 1.25em 0;
  padding: 0.8em 1em;
  border-left: 4px solid var(--accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  background: var(--accent-soft);
  color: var(--text);
}

.markdown-body code {
  border-radius: 5px;
  background: var(--surface-muted);
  color: color-mix(in srgb, var(--warm) 80%, var(--text));
  font-family: var(--mono);
  font-size: 0.92em;
}

.markdown-body :not(pre) > code {
  padding: 0.12em 0.35em;
}

.markdown-body pre {
  overflow: auto;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #101820;
  color: #e8f1f6;
  line-height: 1.6;
}

.markdown-body pre code {
  padding: 0;
  background: transparent;
  color: inherit;
  font-size: 13px;
}

.markdown-body table {
  display: block;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
  margin: 1.4em 0;
}

.markdown-body th,
.markdown-body td {
  padding: 10px 12px;
  border: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.markdown-body th {
  background: var(--surface-muted);
  font-size: 13px;
  font-weight: 800;
}

.markdown-body img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius);
}

.markdown-body hr {
  height: 1px;
  margin: 2em 0;
  border: 0;
  background: var(--border);
}

.markdown-body input[type="checkbox"] {
  width: 16px;
  height: 16px;
  margin-right: 8px;
  vertical-align: -2px;
}

.search-hit {
  border-radius: 3px;
  background: var(--mark);
  color: inherit;
}

.search-hit.is-active {
  background: var(--mark-active);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--mark-active) 45%, transparent);
}

.empty-state {
  display: grid;
  min-height: 360px;
  place-items: center;
  color: var(--muted);
  text-align: center;
}

.empty-state strong {
  display: block;
  color: var(--text);
  font-size: 20px;
}

.status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 34px;
  gap: 16px;
  padding: 0 clamp(16px, 3vw, 34px);
  border-top: 1px solid var(--border);
  color: var(--muted);
  background: var(--surface);
  font-size: 12px;
  font-weight: 700;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 40;
  max-width: min(360px, calc(100vw - 36px));
  padding: 11px 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--text);
  color: var(--surface);
  box-shadow: var(--shadow);
  font-size: 13px;
  font-weight: 700;
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 980px) {
  .app-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .toolbar {
    width: 100%;
    justify-content: flex-start;
  }

  .reader-layout {
    grid-template-columns: 1fr;
  }

  .side-panel {
    grid-template-columns: minmax(190px, 0.7fr) minmax(260px, 1fr);
  }

  .outline-section {
    grid-column: 1 / -1;
    min-height: 0;
  }

  .outline-list {
    max-height: 220px;
  }

  .workspace {
    min-height: 72vh;
  }

  .workspace[data-mode="split"] .document-surface {
    grid-template-columns: 1fr;
  }

  .workspace[data-mode="split"] .source-pane {
    border-top: 1px solid var(--border);
    border-left: 0;
  }
}

@media (max-width: 640px) {
  body {
    font-size: 14px;
  }

  .app-header {
    min-height: 0;
    padding: 12px;
  }

  .toolbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) repeat(3, 34px);
    gap: 6px;
  }

  .toolbar .icon-button {
    width: 34px;
  }

  .button {
    min-width: 0;
    padding: 0 10px;
  }

  .reader-layout {
    gap: 12px;
    padding: 12px;
  }

  .side-panel {
    grid-template-columns: 1fr;
  }

  .workspace {
    order: -1;
  }

  .workspace-bar {
    align-items: stretch;
  }

  .search-field {
    flex-basis: 100%;
  }

  .search-count {
    flex: 1;
    min-width: 0;
    text-align: left;
  }

  .segmented-control {
    width: 100%;
    min-width: 0;
  }

  .preview-pane {
    padding: 22px 16px 34px;
  }

  .markdown-body h1 {
    font-size: 31px;
  }

  .markdown-body h2 {
    font-size: 24px;
  }

  .markdown-body p,
  .markdown-body li,
  .markdown-body td,
  .markdown-body th {
    font-size: 15px;
  }
}

@media print {
  body {
    background: #fff;
  }

  .app-header,
  .side-panel,
  .workspace-bar,
  .source-pane,
  .status-bar,
  .toast {
    display: none !important;
  }

  .app-shell,
  .reader-layout,
  .workspace,
  .document-surface,
  .preview-pane {
    display: block;
    min-height: 0;
    width: auto;
    margin: 0;
    padding: 0;
    border: 0;
    background: #fff;
    box-shadow: none;
  }

  .markdown-body {
    max-width: none;
    color: #111;
  }

  .markdown-body a {
    color: #111;
  }
}
