:root {
  color-scheme: light;
  --bg: #f5f7fa;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --border: #dce2ea;
  --border-strong: #c8d0db;
  --text: #172033;
  --muted: #657083;
  --accent: #1769e0;
  --accent-hover: #0f56bd;
  --success: #16815d;
  --warning: #a45d08;
  --warning-bg: #fff8e8;
  --info-bg: #eef6ff;
  --shadow: 0 8px 28px rgba(23, 32, 51, 0.09);
  --header-height: 56px;
  font-family: "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font-size: 15px;
  line-height: 1.7;
  letter-spacing: 0;
}

button,
input,
select {
  font: inherit;
  letter-spacing: 0;
}

button,
a,
select {
  -webkit-tap-highlight-color: transparent;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.topbar,
.admin-header {
  position: sticky;
  top: 0;
  z-index: 40;
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 24px;
  background: rgba(255, 255, 255, 0.96);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  color: var(--text);
  font-size: 16px;
  font-weight: 650;
  white-space: nowrap;
}

.brand:hover {
  text-decoration: none;
}

.brand-mark {
  width: 30px;
  height: 30px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #ffffff;
  background: var(--accent);
  border-radius: 6px;
}

.brand-mark svg,
.login-brand svg {
  width: 18px;
  height: 18px;
}

.topbar-meta {
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.reader-layout {
  min-height: calc(100vh - var(--header-height));
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
}

.toc-panel {
  position: sticky;
  top: var(--header-height);
  height: calc(100vh - var(--header-height));
  overflow-y: auto;
  padding: 24px 18px;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.toc-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 8px 12px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

#toc {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

#toc a {
  display: block;
  padding: 7px 10px;
  overflow-wrap: anywhere;
  color: #4d596c;
  border-left: 2px solid transparent;
  border-radius: 0 5px 5px 0;
  font-size: 14px;
  line-height: 1.45;
}

#toc a:hover,
#toc a.active {
  color: var(--accent);
  background: #f1f6fd;
  border-left-color: var(--accent);
  text-decoration: none;
}

#toc a.level-3 {
  padding-left: 24px;
  font-size: 13px;
}

.reader-main {
  min-width: 0;
  padding: 46px clamp(24px, 6vw, 88px) 72px;
}

.document-content {
  width: min(100%, 920px);
  margin: 0 auto;
  overflow-wrap: anywhere;
  font-size: 16px;
  line-height: 1.8;
}

.document-content h1,
.document-content h2,
.document-content h3,
.document-content h4 {
  color: #14233b;
  font-weight: 700;
  letter-spacing: 0;
  scroll-margin-top: 78px;
}

.document-content h1 {
  margin: 0 0 14px;
  font-size: 32px;
  line-height: 1.3;
}

.document-content h2 {
  margin: 52px 0 18px;
  padding-bottom: 9px;
  font-size: 24px;
  line-height: 1.4;
  border-bottom: 1px solid var(--border);
}

.document-content h3 {
  margin: 34px 0 14px;
  font-size: 19px;
  line-height: 1.5;
}

.document-content h4 {
  margin: 26px 0 10px;
  font-size: 16px;
  line-height: 1.55;
}

.document-content p {
  margin: 12px 0;
}

.document-content ol,
.document-content ul {
  margin: 14px 0;
  padding-left: 28px;
}

.document-content li {
  margin: 8px 0;
  padding-left: 5px;
}

.steps {
  counter-reset: tutorial-step;
  list-style: none;
  padding-left: 0 !important;
}

.steps li {
  position: relative;
  min-height: 34px;
  margin: 12px 0;
  padding-left: 46px;
  counter-increment: tutorial-step;
}

.steps li::before {
  content: counter(tutorial-step);
  position: absolute;
  top: 1px;
  left: 0;
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #ffffff;
  background: var(--accent);
  border-radius: 50%;
  font-size: 13px;
  font-weight: 700;
  line-height: 1;
}

.document-content blockquote {
  margin: 20px 0;
  padding: 10px 18px;
  color: #49556a;
  border-left: 4px solid #86a8d8;
  background: #f5f8fc;
}

.note,
.solution {
  margin: 20px 0;
  padding: 14px 18px;
  border: 1px solid;
  border-radius: 6px;
}

.note {
  color: #68420a;
  background: var(--warning-bg);
  border-color: #ecd39f;
}

.solution {
  color: #174c3b;
  background: #effaf5;
  border-color: #a9dbc7;
}

.solution > strong {
  display: block;
  margin-bottom: 4px;
}

.solution p,
.note p {
  margin: 0;
}

.doc-figure {
  width: 100%;
  margin: 30px 0 10px;
}

.doc-figure img,
.document-content > img {
  display: block;
  width: auto;
  max-width: 100%;
  max-height: 780px;
  margin: 0 auto;
  object-fit: contain;
  border: 1px solid #d7dde6;
  border-radius: 6px;
  box-shadow: 0 5px 20px rgba(23, 32, 51, 0.08);
}

.figure-caption,
figcaption {
  margin: 8px auto 28px !important;
  color: var(--muted);
  text-align: center;
  font-size: 13px;
  line-height: 1.55;
}

.table-wrap {
  width: 100%;
  margin: 22px 0;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
}

.document-content table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
}

.document-content th,
.document-content td {
  min-width: 150px;
  padding: 12px 14px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: middle;
}

.document-content th {
  color: #344158;
  background: #f2f5f8;
  font-weight: 700;
}

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

.document-content th:last-child,
.document-content td:last-child {
  border-right: 0;
}

.document-footer {
  width: min(100%, 920px);
  margin: 56px auto 0;
  padding-top: 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  color: var(--muted);
  border-top: 1px solid var(--border);
  font-size: 13px;
}

.document-footer span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.document-footer svg {
  width: 16px;
  color: var(--success);
}

.icon-button,
.primary-button,
.secondary-button {
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border-radius: 6px;
  cursor: pointer;
}

.icon-button {
  width: 36px;
  flex: 0 0 36px;
  padding: 0;
  color: #4c586a;
  background: transparent;
  border: 1px solid transparent;
}

.icon-button:hover,
.icon-button.active {
  color: var(--accent);
  background: #edf4fd;
  border-color: #cbdcf3;
}

.icon-button svg,
.primary-button svg,
.secondary-button svg,
.save-state svg,
.back-link svg {
  width: 17px;
  height: 17px;
  stroke-width: 2;
}

.primary-button,
.secondary-button {
  padding: 0 14px;
  font-weight: 650;
  border: 1px solid;
}

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

.primary-button:hover {
  color: #ffffff;
  background: var(--accent-hover);
  text-decoration: none;
}

.secondary-button {
  color: #344158;
  background: var(--surface);
  border-color: var(--border-strong);
}

.secondary-button:hover {
  color: var(--accent);
  background: #f7faff;
  border-color: #9cbce8;
  text-decoration: none;
}

.mobile-only {
  display: none;
}

.toc-backdrop,
.drawer-backdrop {
  display: none;
}

/* Login */
.login-page {
  min-height: 100vh;
  background: #eef2f7;
}

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(100%, 400px);
  padding: 34px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.login-brand {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  margin-bottom: 18px;
  color: #ffffff;
  background: var(--accent);
  border-radius: 7px;
}

.login-panel h1 {
  margin: 0 0 6px;
  font-size: 23px;
  line-height: 1.4;
}

.login-panel > p {
  margin: 0 0 24px;
  color: var(--muted);
}

.login-panel label {
  display: block;
  margin: 14px 0 6px;
  color: #39465a;
  font-weight: 650;
}

.login-panel input,
.title-input,
.editor-toolbar select {
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  outline: none;
}

.login-panel input {
  width: 100%;
  height: 42px;
  padding: 0 12px;
  color: var(--text);
  background: #ffffff;
}

.login-panel input:focus,
.title-input:focus,
.editor-toolbar select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(23, 105, 224, 0.12);
}

.login-button {
  width: 100%;
  height: 42px;
  margin-top: 18px;
}

.form-error {
  min-height: 23px;
  margin-top: 8px;
  color: #b42318;
  font-size: 13px;
}

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 22px;
  color: var(--muted);
  font-size: 13px;
}

/* Admin */
.admin-page {
  min-height: 100vh;
  background: #edf1f6;
}

.admin-header {
  position: sticky;
}

.admin-actions,
.publish-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.save-state {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.save-state.saving {
  color: var(--warning);
}

.save-state.error {
  color: #b42318;
}

.editor-shell {
  width: min(100%, 1220px);
  margin: 0 auto;
  padding: 24px 26px 60px;
}

.editor-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 14px;
}

.title-input {
  min-width: 0;
  flex: 1;
  height: 42px;
  padding: 0 13px;
  color: var(--text);
  background: var(--surface);
  font-size: 18px;
  font-weight: 700;
}

.editor-toolbar {
  position: sticky;
  top: calc(var(--header-height) + 8px);
  z-index: 30;
  min-height: 48px;
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 6px 8px;
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 7px 7px 0 0;
  box-shadow: 0 4px 18px rgba(23, 32, 51, 0.06);
}

.editor-toolbar select {
  height: 34px;
  min-width: 116px;
  padding: 0 30px 0 9px;
  color: #3c485b;
  background: #ffffff;
}

.toolbar-separator {
  width: 1px;
  height: 24px;
  flex: 0 0 1px;
  margin: 0 4px;
  background: var(--border);
}

.editor-stage {
  min-height: 740px;
  padding: 52px clamp(24px, 7vw, 92px) 80px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: 0;
  border-radius: 0 0 7px 7px;
}

.editor-content {
  min-height: 620px;
  outline: none;
}

.editor-content:focus {
  caret-color: var(--accent);
}

.editor-content:empty::before {
  content: "开始编辑教程…";
  color: #9aa3b1;
}

.history-drawer {
  position: fixed;
  z-index: 70;
  top: 0;
  right: 0;
  width: min(90vw, 380px);
  height: 100vh;
  overflow-y: auto;
  padding: 20px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: -10px 0 32px rgba(23, 32, 51, 0.12);
  transform: translateX(105%);
  transition: transform 180ms ease;
}

.history-drawer.open {
  transform: translateX(0);
}

.drawer-backdrop.open {
  position: fixed;
  z-index: 60;
  inset: 0;
  display: block;
  background: rgba(15, 23, 42, 0.26);
}

.drawer-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
}

.drawer-header h2,
.drawer-header p {
  margin: 0;
}

.drawer-header h2 {
  font-size: 18px;
}

.drawer-header p {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.history-list {
  padding: 12px 0;
}

.history-item {
  padding: 12px 4px;
  border-bottom: 1px solid #edf0f4;
}

.history-item strong,
.history-item span {
  display: block;
}

.history-item strong {
  margin-bottom: 3px;
  font-size: 14px;
}

.history-item span {
  color: var(--muted);
  font-size: 12px;
}

.history-item button {
  margin-top: 8px;
}

.toast {
  position: fixed;
  z-index: 90;
  right: 24px;
  bottom: 24px;
  max-width: min(360px, calc(100vw - 32px));
  padding: 11px 15px;
  color: #ffffff;
  background: #243247;
  border-radius: 6px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transform: translateY(10px);
  transition: 160ms ease;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 860px) {
  .mobile-only {
    display: inline-flex;
  }

  .topbar {
    justify-content: flex-start;
    padding: 0 14px;
  }

  .topbar-meta {
    margin-left: auto;
    font-size: 12px;
  }

  .reader-layout {
    display: block;
  }

  .toc-panel {
    position: fixed;
    z-index: 70;
    top: 0;
    left: 0;
    width: min(86vw, 310px);
    height: 100vh;
    transform: translateX(-105%);
    transition: transform 180ms ease;
  }

  .toc-panel.open {
    transform: translateX(0);
  }

  .toc-backdrop.open {
    position: fixed;
    z-index: 60;
    inset: 0;
    display: block;
    background: rgba(15, 23, 42, 0.28);
  }

  .reader-main {
    padding: 30px 18px 56px;
  }

  .document-content h1 {
    font-size: 26px;
  }

  .document-content h2 {
    margin-top: 42px;
    font-size: 21px;
  }

  .document-content h3 {
    font-size: 18px;
  }

  .document-content {
    font-size: 15px;
  }

  .admin-header {
    padding: 0 12px;
  }

  .admin-actions .save-state span,
  .admin-actions .secondary-button span {
    display: none;
  }

  .admin-actions .secondary-button {
    width: 36px;
    padding: 0;
  }

  .editor-shell {
    padding: 14px 10px 40px;
  }

  .editor-title-row {
    align-items: stretch;
    flex-direction: column;
    gap: 10px;
  }

  .publish-actions {
    justify-content: flex-end;
  }

  .editor-toolbar {
    top: calc(var(--header-height) + 4px);
  }

  .editor-stage {
    min-height: 660px;
    padding: 34px 18px 60px;
  }
}

@media (max-width: 480px) {
  .brand span:last-child {
    max-width: 122px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .topbar-meta {
    display: none;
  }

  .login-panel {
    padding: 26px 22px;
  }

  .publish-actions .secondary-button,
  .publish-actions .primary-button {
    flex: 1;
  }

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