:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f2f5;
  --surface-3: #e8eaee;
  --text: #1f2430;
  --muted: #6b7280;
  --border: #e2e5ea;
  --primary: #4f46e5;
  --primary-soft: #eef0ff;
  --danger: #dc2626;
  --success: #16a34a;
  --star: #f59e0b;
  --shadow: 0 6px 24px rgba(24, 28, 40, .08);
  --radius: 14px;
}

[data-theme="dark"] {
  --bg: #14161b;
  --surface: #1e2128;
  --surface-2: #272b34;
  --surface-3: #303541;
  --text: #e8eaf0;
  --muted: #9ca3af;
  --border: #343945;
  --primary: #8b8cf7;
  --primary-soft: #2c2c48;
  --danger: #f87171;
  --success: #4ade80;
  --star: #fbbf24;
  --shadow: 0 6px 24px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.6;
  -webkit-tap-highlight-color: transparent;
}

button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--primary); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error-text { color: var(--danger); min-height: 22px; margin: 8px 0 0; font-size: 13px; }
.small { font-size: 13px; }
.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }

/* ---------- 登录 ---------- */
.login-screen {
  min-height: 100vh; display: grid; place-items: center; padding: 20px;
  background: radial-gradient(circle at 50% 15%, var(--primary-soft), var(--bg) 60%);
}
.login-card {
  width: min(400px, 100%); background: var(--surface); border: 1px solid var(--border);
  border-radius: 20px; padding: 36px 32px; box-shadow: var(--shadow); text-align: center;
}
.login-logo { font-size: 40px; }
.login-card h1 { margin: 8px 0 4px; font-size: 24px; }
.login-card p { margin-top: 0; }
.login-card .field { text-align: left; margin: 22px 0 16px; }

/* ---------- 顶栏 ---------- */
.topbar {
  height: 60px; padding: 0 24px; display: flex; align-items: center; justify-content: space-between;
  gap: 12px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 8px; background: none; border: 0; cursor: pointer; font-size: 17px; font-weight: 700; padding: 4px; }
.brand-icon { font-size: 22px; }
.top-actions { display: flex; gap: 6px; align-items: center; }
.icon-btn { width: 36px; justify-content: center; padding: 0; }

/* ---------- 通用控件 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 7px 13px; border-radius: 9px; cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s, transform .1s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); border-color: transparent; background: transparent; }
.btn.danger:hover { background: rgba(220, 38, 38, .1); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.block { width: 100%; padding: 11px; }
.btn.sm { padding: 4px 9px; font-size: 13px; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: 13px; color: var(--muted); }
input, textarea, select {
  width: 100%; border: 1px solid var(--border); border-radius: 9px;
  background: var(--surface); padding: 9px 11px; outline: none;
}
textarea { resize: vertical; min-height: 92px; line-height: 1.6; }
input:focus, textarea:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.field-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.field-inline { display: flex; align-items: center; gap: 8px; }
.field-inline input[type="checkbox"] { width: auto; }

.tag {
  display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px;
  background: var(--surface-3); color: var(--muted); margin: 2px 4px 2px 0;
}
.tag.dot::before { content: '●'; margin-right: 4px; font-size: 8px; }
.badge { padding: 2px 9px; border-radius: 999px; font-size: 12px; background: var(--primary-soft); color: var(--primary); }

/* ---------- 主区域 / 书架 ---------- */
.main { max-width: 1200px; margin: 0 auto; padding: 26px 24px 60px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 24px; }
.page-head .sub { color: var(--muted); font-size: 13px; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden;
}
.card.clickable { cursor: pointer; transition: transform .15s, border-color .15s; }
.card.clickable:hover { transform: translateY(-3px); border-color: var(--primary); }
.card-cover {
  height: 110px; background: var(--surface-3); display: grid; place-items: center;
  font-size: 42px; overflow: hidden;
}
.card-cover img { width: 100%; height: 100%; object-fit: cover; }
.card-body { padding: 14px 16px; }
.card-title { font-size: 17px; font-weight: 700; margin: 0 0 4px; }
.card-summary { color: var(--muted); font-size: 13px; min-height: 38px; margin: 6px 0 12px; }
.stats { display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--muted); border-top: 1px solid var(--border); padding-top: 10px; }
.card-actions { display: flex; gap: 2px; padding: 6px 10px 12px; }
.section-title { font-size: 15px; font-weight: 700; margin: 26px 0 12px; }
.empty { text-align: center; padding: 50px 20px; color: var(--muted); border: 1px dashed var(--border); border-radius: var(--radius); }

/* ---------- 作品内页布局 ---------- */
.work-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.work-head h1 { margin: 0; font-size: 23px; }
.back-link { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 4px; }
.work-layout { display: grid; grid-template-columns: 168px minmax(0, 1fr); gap: 20px; align-items: start; }
.side-nav { display: flex; flex-direction: column; gap: 4px; position: sticky; top: 80px; }
.side-nav button {
  text-align: left; border: 0; background: transparent; padding: 10px 13px; border-radius: 10px;
  cursor: pointer; color: var(--muted);
}
.side-nav button:hover { background: var(--surface-2); }
.side-nav button.active { background: var(--primary-soft); color: var(--primary); font-weight: 700; }
.module-area { min-width: 0; }
.module-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.module-head h2 { margin: 0; font-size: 19px; }
.search-box { flex: 1; min-width: 160px; }
.search-box input { padding-left: 12px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar select { width: auto; min-width: 110px; padding: 6px 9px; }

/* ---------- 列表 / 卡片 ---------- */
.list { display: flex; flex-direction: column; gap: 10px; }
.list-item {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 13px 15px; display: flex; gap: 12px; align-items: flex-start;
}
.list-item .item-main { flex: 1; min-width: 0; }
.item-title { font-weight: 700; font-size: 16px; }
.item-content { white-space: pre-wrap; color: var(--muted); font-size: 13px; margin-top: 5px; }
.item-meta { display: flex; gap: 8px; align-items: center; margin-top: 7px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.item-actions { display: flex; gap: 2px; }
.star-btn { border: 0; background: none; cursor: pointer; font-size: 20px; line-height: 1; color: var(--border); }
.star-btn.on { color: var(--star); }

.char-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 14px; }
.char-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; cursor: pointer; }
.char-card:hover { border-color: var(--primary); }
.char-avatar { height: 120px; background: var(--surface-3); display: grid; place-items: center; font-size: 46px; overflow: hidden; }
.char-avatar img { width: 100%; height: 100%; object-fit: cover; }
.char-info { padding: 11px 13px; }
.char-info .name { font-weight: 700; }
.char-info .identity { font-size: 12px; color: var(--muted); margin-top: 2px; }

/* ---------- 画布 ---------- */
.canvas-wrap {
  position: relative; height: min(64vh, 600px); min-height: 380px;
  border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden;
  background: var(--surface-2); touch-action: none;
}
#graph-canvas, #map-canvas { width: 100%; height: 100%; display: block; }
.canvas-hint {
  position: absolute; left: 10px; bottom: 10px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 4px 9px; font-size: 12px; color: var(--muted); pointer-events: none;
}
.canvas-tools { position: absolute; right: 10px; top: 10px; display: flex; flex-direction: column; gap: 5px; }
.canvas-tools .btn { background: var(--surface); box-shadow: var(--shadow); }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin-top: 10px; }
.legend i { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 4px; }

/* ---------- 时间线 ---------- */
.timeline { position: relative; overflow-x: auto; padding: 26px 8px 20px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.timeline-line { position: absolute; top: 46px; left: 20px; right: 20px; height: 3px; background: var(--surface-3); }
.timeline-track { display: flex; gap: 16px; min-width: max-content; padding: 0 12px; position: relative; z-index: 1; }
.event-node { width: 210px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px; cursor: pointer; }
.event-node:hover { border-color: var(--primary); }
.event-node.dragging { opacity: .45; }
.event-dot { width: 15px; height: 15px; border-radius: 50%; background: var(--primary); border: 3px solid var(--surface); box-shadow: 0 0 0 2px var(--primary); margin: 0 0 10px 16px; }
.event-time { font-size: 12px; color: var(--primary); font-weight: 700; }
.event-node h4 { margin: 3px 0 5px; font-size: 14px; }
.event-node p { margin: 0; color: var(--muted); font-size: 12px; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.event-node .move { display: flex; gap: 4px; margin-top: 8px; }

/* ---------- 弹窗 / 提示 ---------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 100; background: rgba(10, 12, 18, .55);
  display: flex; align-items: center; justify-content: center; padding: 18px;
}
.modal {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  width: min(560px, 100%); max-height: min(86vh, 900px); display: flex; flex-direction: column;
  box-shadow: 0 18px 60px rgba(0, 0, 0, .25);
}
.modal.wide { width: min(820px, 100%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 15px 20px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 17px; }
.modal-close { border: 0; background: none; cursor: pointer; font-size: 22px; color: var(--muted); line-height: 1; }
.modal-body { padding: 18px 20px; overflow: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 13px 20px; border-top: 1px solid var(--border); }
.detail-grid { display: grid; gap: 12px; }
.detail-field .k { color: var(--muted); font-size: 12px; }
.detail-field .v { white-space: pre-wrap; }
.link-groups { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-top: 10px; }
.link-group { border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; max-height: 160px; overflow: auto; }
.link-group b { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.link-group label { display: flex; gap: 6px; align-items: center; font-size: 13px; padding: 2px 0; cursor: pointer; }
.link-group input { width: auto; }

.toast-root { position: fixed; right: 18px; bottom: 18px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--text); color: var(--bg); padding: 9px 14px; border-radius: 9px; box-shadow: var(--shadow); font-size: 13px; animation: fade .2s; }
.toast.error { background: var(--danger); color: #fff; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } }

.search-results { display: flex; flex-direction: column; gap: 8px; }
.search-result { display: flex; gap: 10px; align-items: center; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; cursor: pointer; }
.search-result:hover { border-color: var(--primary); }
.search-result .src { font-size: 12px; color: var(--muted); }
.autosave-status { font-size: 12px; color: var(--success); }

@media (max-width: 760px) {
  .topbar { padding: 0 12px; height: 54px; }
  .hide-sm { display: none; }
  .main { padding: 18px 12px 100px; }
  .work-layout { display: block; }
  .side-nav {
    position: fixed; bottom: 0; left: 0; right: 0; top: auto; z-index: 30;
    flex-direction: row; justify-content: space-around; background: var(--surface);
    border-top: 1px solid var(--border); padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .side-nav button { flex: 1; text-align: center; padding: 7px 2px; font-size: 12px; border-radius: 8px; }
  .side-nav button .nav-icon { display: block; font-size: 17px; }
  .field-row { grid-template-columns: 1fr; }
  .module-head .spacer, .work-head .spacer { display: none; }
  .module-head h2, .work-head h1 { font-size: 18px; }
  .module-head .btn, .work-head .btn { margin-left: 0; }
  .canvas-wrap { height: 52vh; min-height: 320px; }
  .modal { max-height: 92vh; }
}
