:root {
  color-scheme: light;
  --bg: #f3f3f0;
  --surface: #fbfbf8;
  --surface-2: #efefea;
  --text: #1c1c1c;
  --muted: #666660;
  --border: #d8d8d1;
  --accent: #356a9a;
  --accent-soft: #e3edf6;
  --danger: #a92f28;
  --ok: #2f6b45;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 6px 20px rgba(0, 0, 0, .14);
  --radius: 10px;
  --topbar-h: 56px;
  --coursebar-h: 44px;
  --sidebar-w: 280px;
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121212;
  --surface: #1b1b1b;
  --surface-2: #242424;
  --text: #e6e6e6;
  --muted: #9b9b96;
  --border: #343432;
  --accent: #7fa9d4;
  --accent-soft: #1e2c3a;
  --danger: #e0756d;
  --ok: #7fc29a;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .5);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 system-ui, -apple-system, "Segoe UI", "Noto Sans CJK SC", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}

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

button, input, select, textarea { font: inherit; color: inherit; }

/* ---------------- 顶栏 ---------------- */

.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 40;
}

.topbar nav { display: flex; align-items: center; gap: 4px; }
.topbar nav a {
  color: var(--muted);
  padding: 6px 10px;
  border-radius: 7px;
  text-decoration: none;
}
.topbar nav a:hover { background: var(--surface-2); color: var(--text); }
.topbar nav a.active { color: var(--text); background: var(--surface-2); font-weight: 600; }
.topbar .spacer { flex: 1; }

.search-wrap { position: relative; }
.search-wrap input {
  width: 230px;
  padding: 7px 11px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
}
.search-wrap input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.search-results {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  width: min(440px, 80vw);
  max-height: 60vh;
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 6px;
}
.search-results[hidden] { display: none; }
.search-result {
  display: block;
  padding: 9px 10px;
  border-radius: 7px;
  color: var(--text);
  text-decoration: none;
}
.search-result:hover { background: var(--surface-2); text-decoration: none; }
.search-result strong { display: block; font-size: 14px; }
.search-result span { display: block; color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.search-empty { padding: 12px; color: var(--muted); font-size: 13px; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  border-radius: 8px;
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; }
.menu-only { display: none; }

/* ---------------- 公共视图 ---------------- */

.page {
  max-width: 820px;
  margin: 0 auto;
  padding: 48px 24px 96px;
}
.page h1 { font-size: 30px; line-height: 1.25; margin: 0 0 22px; letter-spacing: -.01em; }
.prose { white-space: pre-wrap; font-size: 16px; line-height: 1.85; color: var(--text); }
.empty {
  color: var(--muted);
  text-align: center;
  padding: 80px 20px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

/* ---------------- 课件视图 ---------------- */

#courseView {
  height: calc(100vh - var(--topbar-h));
  display: flex;
  flex-direction: column;
  min-height: 0;
}
#courseView[hidden] { display: none; }

.coursebar {
  height: var(--coursebar-h);
  flex: 0 0 var(--coursebar-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.coursebar .title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13.5px;
  color: var(--muted);
}
.coursebar .title strong { color: var(--text); font-weight: 600; }
.course-nav { display: flex; align-items: center; gap: 4px; }

.course-body { flex: 1; display: flex; min-height: 0; position: relative; }

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  overflow: auto;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 10px 8px 40px;
}

.tree, .tree ul { list-style: none; margin: 0; padding: 0; }
.tree ul { padding-left: 14px; }
.tree-row {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 32px;
  border-radius: 7px;
  padding: 0 4px;
}
.tree-row:hover { background: var(--surface-2); }
.tree-row .twist {
  width: 20px;
  height: 24px;
  flex: 0 0 20px;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  border-radius: 4px;
  font-size: 10px;
}
.tree-row .twist:hover { background: var(--border); }
.tree-row .folder {
  flex: 1;
  min-width: 0;
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  padding: 4px 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tree-row .count {
  font-size: 11px;
  color: var(--muted);
  background: var(--surface-2);
  border-radius: 9px;
  padding: 0 6px;
}
.tree-row .folder.active, .tree-row .page-link.active {
  color: var(--accent);
  font-weight: 600;
}
.tree-row .page-link {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  text-align: left;
  color: var(--muted);
  cursor: pointer;
  padding: 4px 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tree-row .page-link:hover { color: var(--text); }
.tree-empty { padding: 14px 10px; color: var(--muted); font-size: 13px; }

#stage {
  flex: 1;
  min-width: 0;
  padding: 16px;
  overflow: hidden;
  background: var(--bg);
}
#view {
  display: block;
  width: 100%;
  height: 100%;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color-scheme: light;
}
#view[hidden] { display: none; }

.stage-empty {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

.backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .45);
  z-index: 45;
}

/* ---------------- 表单与按钮 ---------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  cursor: pointer;
  font-size: 13.5px;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.danger:hover { background: rgba(169, 47, 40, .08); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.small { padding: 4px 8px; font-size: 12.5px; }

input[type="text"], input[type="password"], textarea, select {
  width: 100%;
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
}
input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea { resize: vertical; min-height: 220px; line-height: 1.7; }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.field { margin-bottom: 16px; }

/* ---------------- 登录 ---------------- */

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  width: min(380px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 30px 28px;
}
.login-card h1 { margin: 0 0 6px; font-size: 21px; }
.login-card p { margin: 0 0 22px; color: var(--muted); font-size: 13.5px; }
.login-error {
  background: rgba(169, 47, 40, .1);
  color: var(--danger);
  border: 1px solid rgba(169, 47, 40, .3);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  margin-bottom: 14px;
}
.login-error[hidden] { display: none; }

/* ---------------- 管理后台 ---------------- */

.admin-shell { max-width: 1500px; margin: 0 auto; padding: 18px; }
.admin-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}
.admin-head h1 { font-size: 19px; margin: 0; }
.admin-head .spacer { flex: 1; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; flex-wrap: wrap; }
.tab {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 9px 13px;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  margin-bottom: -1px;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

.admin-grid {
  display: grid;
  grid-template-columns: 330px minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-width: 0;
}
.panel-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.panel-head h2 { font-size: 14px; margin: 0; flex: 1; }
.panel-body { padding: 12px; }
.panel-body.flush { padding: 0; }

.admin-tree { max-height: 62vh; overflow: auto; padding: 8px; }
.admin-tree .tree-row { min-height: 34px; }
.admin-tree .tree-row.selected { background: var(--accent-soft); }
.admin-tree .tree-row.drag-over { outline: 2px dashed var(--accent); outline-offset: -2px; }
.admin-tree .badge {
  font-size: 10.5px;
  padding: 1px 5px;
  border-radius: 5px;
  border: 1px solid var(--border);
  color: var(--muted);
}
.admin-tree .badge.pub { color: var(--ok); border-color: var(--ok); }
.admin-tree .badge.priv { color: var(--danger); border-color: var(--danger); }

.list { display: flex; flex-direction: column; }
.list-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.list-row:last-child { border-bottom: 0; }
.list-row:hover { background: var(--surface-2); }
.list-row.drag-over { outline: 2px dashed var(--accent); outline-offset: -2px; }
.list-main { flex: 1; min-width: 0; }
.list-title { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-sub { font-size: 12px; color: var(--muted); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-actions { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.toolbar .spacer { flex: 1; }

.notice {
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 12px;
  white-space: pre-wrap;
}
.notice.ok { border-color: rgba(47, 107, 69, .4); color: var(--ok); }
.notice.err { border-color: rgba(169, 47, 40, .4); color: var(--danger); }

.table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.table th, .table td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { color: var(--muted); font-weight: 500; font-size: 12px; }
.table tr:last-child td { border-bottom: 0; }
.mono { font-family: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace; font-size: 12.5px; }
.muted { color: var(--muted); }
.nowrap { white-space: nowrap; }

.ctx-menu {
  position: fixed;
  z-index: 100;
  min-width: 168px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  box-shadow: var(--shadow);
  padding: 5px;
}
.ctx-menu[hidden] { display: none; }
.ctx-menu button {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 7px 9px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
}
.ctx-menu button:hover { background: var(--surface-2); }
.ctx-menu .sep { height: 1px; background: var(--border); margin: 4px 3px; }
.ctx-menu .danger { color: var(--danger); }

.panel-hint {
  margin: 8px 12px 12px;
  font-size: 12px;
  color: var(--muted);
}

/* 内联编辑：树里的新建/改名、课件标题 */
input.inline-input {
  flex: 1;
  min-width: 0;
  padding: 4px 6px;
  font-size: 13px;
  background: var(--surface);
  border-color: var(--accent);
}
.tree-row.editing { padding-left: 24px; }
.list-title input.inline-input { font-size: 14px; }

/* 两段式危险操作：第一次点击后的“确认”状态 */
.btn.armed,
.btn.armed:hover {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}
.ctx-menu button.armed {
  background: var(--danger);
  color: #fff;
}

.hidden { display: none !important; }

/* ---------------- 移动端 ---------------- */

@media (max-width: 768px) {
  .topbar { padding: 0 10px; gap: 8px; }
  .topbar nav { gap: 0; }
  .topbar nav a { padding: 6px 7px; font-size: 14px; }
  .search-wrap input { width: 130px; }
  .search-wrap input:focus { width: 160px; }
  .page { padding: 28px 18px 72px; }
  .page h1 { font-size: 24px; }

  body.course-mode .topbar { display: none; }
  #courseView { height: 100vh; }
  .menu-only { display: inline-flex; }
  .coursebar { padding: 0 6px; gap: 6px; }
  .coursebar .title { font-size: 12.5px; }
  .course-nav .btn { padding: 6px 8px; font-size: 12.5px; }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 50;
    width: min(86vw, 330px);
    transform: translateX(-102%);
    transition: transform .2s ease;
    box-shadow: var(--shadow);
    padding-top: 56px;
  }
  body.drawer-open .sidebar { transform: translateX(0); }
  body.drawer-open .backdrop { display: block; }
  #stage { padding: 8px; }
  .admin-grid { grid-template-columns: 1fr; }
  .admin-tree { max-height: 38vh; }
  .list-row { flex-wrap: wrap; }
  .list-actions { width: 100%; justify-content: flex-start; }
}
