/* ---------- 主题变量 ---------- */
:root {
  --bg: #fbfbfa;
  --surface: #ffffff;
  --border: #e4e2dd;
  --text: #23211d;
  --muted: #8a857c;
  --accent: #3a6ea5;
  --accent-soft: #eaf1f8;
  --code-bg: #f4f2ee;
  --danger: #c0392b;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17181a;
    --surface: #1e2023;
    --border: #2f3235;
    --text: #e6e4e0;
    --muted: #8b8f95;
    --accent: #7aa7d4;
    --accent-soft: #232c35;
    --code-bg: #141618;
    --danger: #e07a6c;
  }
}
:root[data-theme="dark"] {
  --bg: #17181a;
  --surface: #1e2023;
  --border: #2f3235;
  --text: #e6e4e0;
  --muted: #8b8f95;
  --accent: #7aa7d4;
  --accent-soft: #232c35;
  --code-bg: #141618;
  --danger: #e07a6c;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 -apple-system, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

/* ---------- 通用控件 ---------- */
input, textarea, select, button { font-family: inherit; }
button {
  padding: 7px 13px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text);
  font-size: 14px; cursor: pointer; white-space: nowrap;
}
button:hover { border-color: var(--accent); color: var(--accent); }
button.ghost { background: transparent; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.08); color: #fff; }
input[type="search"], input[type="text"], input[type="password"], input:not([type]) {
  padding: 7px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text); font-size: 14px;
}
.spacer { flex: 1; }
.msg { font-size: 13px; color: var(--muted); }

/* ---------- 登录 ---------- */
.login-wrap {
  min-height: 100vh; min-height: 100dvh; display: flex; align-items: center; justify-content: center;
  background: var(--bg); padding: 20px;
}
.login-panel {
  display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 40px; align-items: center;
  width: 100%; max-width: 780px;
}
.login-promo { min-width: 0; }
.login-promo-brand {
  display: flex; align-items: center; gap: 10px;
  font-size: 24px; font-weight: 700; letter-spacing: .5px;
}
.login-promo-lead { margin: 10px 0 16px; color: var(--muted); font-size: 14px; }
.login-promo-list { margin: 0 0 16px; padding-left: 18px; font-size: 14px; line-height: 1.9; }
.login-promo-mcp { margin: 0; color: var(--accent); font-size: 13px; }
.login-card {
  width: 320px; display: flex; flex-direction: column; gap: 12px;
  padding: 28px 26px; border: 1px solid var(--border); border-radius: 14px;
  background: var(--surface);
}
.login-tip { margin: 0 0 6px; text-align: center; color: var(--muted); font-size: 13px; }
.pwd-form { display: flex; flex-direction: column; gap: 12px; }
.login-msg { min-height: 18px; font-size: 13px; color: var(--danger); text-align: center; }
.brand-slogan { font-size: 13px; font-weight: 400; color: var(--muted); letter-spacing: 0; }

/* ---------- 顶栏 ---------- */
#app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 0 18px; height: 54px; flex: none;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.brand {
  display: inline-flex; align-items: center; gap: 8px;
  font-weight: 700; letter-spacing: .5px; font-size: 17px;
}
.brand.clickable { cursor: pointer; }
.brand.clickable:hover { color: var(--accent); }
.logo { width: 26px; height: 26px; flex: none; object-fit: contain; display: block; }
.logo-login { width: 36px; height: 36px; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; padding: 0; border-radius: 8px; color: var(--muted);
}
.icon-btn:hover { color: var(--accent); border-color: var(--accent); }
.user { display: flex; align-items: center; gap: 8px; }
.avatar {
  width: 28px; height: 28px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent-soft); color: var(--accent);
  font-size: 13px; font-weight: 600; text-transform: uppercase;
}
.user-name { font-size: 13px; color: var(--text); }

/* ---------- 左右布局 ---------- */
.shell { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 232px; flex: none; overflow: auto;
  border-right: 1px solid var(--border); background: var(--surface);
  padding: 12px 8px;
}
.side-section { margin-bottom: 14px; }
.side-title {
  font-size: 12px; color: var(--muted); letter-spacing: .5px;
  padding: 4px 10px; margin-bottom: 2px;
}
.side-list { list-style: none; margin: 0; padding: 0; }
.side-item {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 10px; border-radius: 7px; cursor: pointer;
  font-size: 14px; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.side-item:hover { background: var(--accent-soft); }
.side-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.side-item .n { margin-left: auto; font-size: 12px; color: var(--muted); }
.side-empty { padding: 4px 10px; font-size: 13px; color: var(--muted); }

.content { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow: hidden; }

/* ---------- 列表 ---------- */
#view-list { flex: 1; overflow: auto; padding: 16px 20px; }
.listbar { display: flex; gap: 10px; align-items: center; margin-bottom: 12px; }
.listbar .search { flex: 1; min-width: 120px; max-width: 460px; }
.list-head { font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.list { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.card {
  position: relative;
  padding: 13px 15px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); cursor: pointer;
  display: flex; flex-direction: column; gap: 6px;
}
.card:hover { border-color: var(--accent); }
.card .t { font-weight: 600; padding-right: 26px; }
/* 删除按钮：默认隐藏，悬停卡片时出现 */
.card-del {
  position: absolute; top: 8px; right: 8px;
  width: 26px; height: 26px; padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 6px; background: var(--surface); color: var(--muted);
  opacity: 0; pointer-events: none;
  transition: opacity .12s ease;
}
.card:hover .card-del, .card-del:focus-visible { opacity: 1; pointer-events: auto; }
.card-del:hover { color: var(--danger); border-color: var(--danger); }
/* 触屏没有 hover：直接常显，否则删不掉 */
@media (hover: none) {
  .card-del { opacity: 1; pointer-events: auto; }
}
.card .m { font-size: 12px; color: var(--muted); }
.card .tags { display: flex; flex-wrap: wrap; gap: 4px; }
.card .tags span {
  font-size: 12px; padding: 1px 8px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent);
}
.empty { padding: 60px 20px; text-align: center; color: var(--muted); }
/* 空列表提示语：窄屏没有「新建」按钮也拖不了文件，换成可行的说法 */
.empty-narrow { display: none; }

/* ---------- 文档视图 ---------- */
#view-doc { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.docbar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap; flex: none;
  padding: 10px 18px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.doc-title { font-size: 15px; font-weight: 600; max-width: 40ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.title-input { flex: 1; min-width: 180px; font-size: 15px; font-weight: 600; }
.meta-input { width: 160px; font-size: 13px; }

.pane-preview { flex: 1; overflow: auto; padding: 24px 32px; background: var(--surface); }
.panes { flex: 1; display: flex; min-height: 0; }
.source {
  flex: 1; min-width: 0; padding: 16px 18px;
  border: none; border-right: 1px solid var(--border);
  background: var(--bg); color: var(--text);
  font: 13.5px/1.7 "SF Mono", Menlo, Consolas, monospace;
  resize: none; outline: none;
}
.preview { flex: 1; min-width: 0; padding: 16px 22px; overflow: auto; background: var(--surface); }
.detailfoot { padding: 6px 18px; border-top: 1px solid var(--border); background: var(--surface); flex: none; }
.meta { font-size: 12px; color: var(--muted); }
.pane-preview .markdown, .preview { max-width: 880px; }

/* ---------- 下拉菜单 ---------- */
.dropdown { position: relative; }
.dropdown-menu {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 20;
  display: flex; flex-direction: column; min-width: 190px;
  padding: 4px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); box-shadow: 0 8px 24px rgba(0, 0, 0, .12);
}
.dropdown-menu button { border: none; text-align: left; background: transparent; }
.dropdown-menu button:hover { background: var(--accent-soft); }

/* ---------- 弹窗 ---------- */
.modal {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, .45); padding: 20px;
}
.modal-card {
  width: 560px; max-width: 100%; max-height: 88vh; overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.modal-close { border: none; background: transparent; font-size: 20px; line-height: 1; padding: 0 6px; }
.modal-body { padding: 16px 18px; }
.modal-tip { margin: 0 0 14px; color: var(--muted); font-size: 13px; }
.code-label { font-size: 12px; color: var(--muted); margin: 12px 0 6px; }
.code-block {
  margin: 0; padding: 12px 14px; border-radius: 8px; overflow: auto;
  background: var(--code-bg); font: 12.5px/1.6 "SF Mono", Menlo, Consolas, monospace;
  white-space: pre;
}
.copy-btn { margin-top: 8px; font-size: 13px; padding: 5px 10px; }
.switch-row { display: flex; align-items: flex-start; gap: 8px; font-size: 14px; cursor: pointer; }
.switch-row input { margin-top: 3px; }
.share-copy { display: flex; gap: 8px; align-items: center; }
.share-link { flex: 1; min-width: 0; font-size: 13px; }

/* 通用对话框 */
.dlg-card { width: 420px; }
.dlg-msg { margin: 2px 0 20px; white-space: pre-wrap; line-height: 1.75; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; }
/* 危险操作按钮（放在 primary/ghost 之后，确保同优先级下胜出） */
button.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
button.danger:hover { filter: brightness(1.08); color: #fff; }

/* ---------- 分享页 ---------- */
.share-wrap { min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; }
.share-body { flex: 1; overflow: auto; background: var(--surface); }
.share-inner { max-width: 820px; margin: 0 auto; padding: 40px 20px; }
.share-brand-tip { font-size: 12px; color: var(--muted); }

/* ---------- Markdown 渲染 ---------- */
.markdown { overflow-wrap: break-word; }
.markdown h1, .markdown h2, .markdown h3 { line-height: 1.35; margin: 1.2em 0 .6em; }
.markdown h1 { font-size: 1.5em; border-bottom: 1px solid var(--border); padding-bottom: .3em; }
.markdown h2 { font-size: 1.25em; }
.markdown h3 { font-size: 1.1em; }
.markdown p, .markdown ul, .markdown ol { margin: .7em 0; }
.markdown code {
  background: var(--code-bg); padding: 2px 5px; border-radius: 4px;
  font: 13px "SF Mono", Menlo, Consolas, monospace;
}
.markdown pre {
  background: var(--code-bg); padding: 12px 14px; border-radius: 8px; overflow: auto;
}
.markdown pre code { background: none; padding: 0; }
.markdown blockquote {
  margin: .8em 0; padding: 2px 14px;
  border-left: 3px solid var(--border); color: var(--muted);
}
.markdown table { border-collapse: collapse; margin: .8em 0; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 6px 10px; }
.markdown img { max-width: 100%; }
.markdown hr { border: none; border-top: 1px solid var(--border); margin: 1.5em 0; }
.markdown a { color: var(--accent); }

/* ---------- 窄屏 / 移动端 ---------- */
/* 这三个元素只在窄屏出现，桌面端一律隐藏 */
.side-toggle, .side-backdrop, .side-foot { display: none; }

@media (max-width: 720px) {
  /* —— 顶栏：☰ MDBox … ☾ —— */
  .topbar { height: 54px; padding: 0 12px; gap: 6px; }
  .side-toggle { display: inline-flex; }
  .user-name, #pwd-btn, #logout-btn, #mcp-btn { display: none; }

  /* —— 侧栏 → 左侧抽屉 —— */
  .sidebar {
    position: fixed; z-index: 40;
    left: 0; top: 54px; bottom: 0;
    width: min(78vw, 300px);
    padding: 12px 8px 0;
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: 0 0 24px rgba(0, 0, 0, .18);
  }
  body.nav-open .sidebar { transform: translateX(0); }
  /* 纵深防御：body 自身并不滚动，真正挡住背景滑动的是遮罩的 pointer-events。
     这里保留 overflow:hidden 以防将来布局改成 body 可滚动。 */
  body.nav-open { overflow: hidden; }

  .side-backdrop {
    display: block; position: fixed; inset: 54px 0 0 0; z-index: 39;
    background: rgba(0, 0, 0, .4);
    opacity: 0; pointer-events: none;
    transition: opacity .22s ease;
  }
  body.nav-open .side-backdrop { opacity: 1; pointer-events: auto; }

  /* —— 编辑相关：编辑不可达，以下规则仅为「万一面板被恢复」兜底 —— */
  .panes { flex-direction: column; }
  .source { border-right: none; border-bottom: 1px solid var(--border); min-height: 40vh; }
  .preview { min-height: 40vh; }
  .meta-input { width: 110px; }

  /* —— 抽屉底部账号区（桌面端隐藏）—— */
  .side-foot {
    display: block;
    position: sticky; bottom: 0;
    margin: 12px -8px 0;
    padding: 8px 8px 8px;
    background: var(--surface);
    border-top: 1px solid var(--border);
  }
  .side-foot-user {
    display: flex; align-items: center; gap: 8px;
    padding: 4px 8px 10px; font-size: 14px; color: var(--text);
  }
  .side-foot-btn { display: block; width: 100%; text-align: left; margin-bottom: 6px; }

  /* —— 窄屏只读：隐藏一切编辑/新建/上传/删除入口 —— */
  /* 注意 .card-del 在基础区有一条 @media (hover:none) 规则把它设为常显，
     那条只改 opacity/pointer-events，不会覆盖这里的 display:none。 */
  #btn-new, #btn-upload, #p-edit, #e-archive, .card-del { display: none; }

  /* —— 列表页 —— */
  #view-list { padding: 12px 14px; }
  .listbar { gap: 8px; }
  .listbar .search { max-width: none; }
  /* 基础规则是 repeat(auto-fill, minmax(280px,1fr))，在 600-720px 区间会出两列；
     手机上锁定单列，避免卡片被压得过窄。 */
  .list { grid-template-columns: 1fr; }

  /* —— 文档工具条：预览条压成单行，标题占满剩余宽度并截断 —— */
  #preview-bar { flex-wrap: nowrap; padding: 8px 12px; gap: 6px; }
  #preview-bar .spacer { display: none; }
  .doc-title { flex: 1; min-width: 0; max-width: none; font-size: 14px; }
  .docbar .msg { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  /* 编辑条在窄屏不可达，保留换行兜底 */
  #edit-bar { flex-wrap: wrap; }

  /* —— 正文阅读排版 —— */
  /* 注意：这里的选择器是「阅读容器」而不是裸 .markdown。
     原因是 .pdf-export 的包裹层也带 markdown 类（见 web/style.css 末尾），
     若写成 .markdown 会连带影响手机端导出 PDF —— 尤其 table 会被加上
     display:block + overflow-x:auto，html2canvas 会按容器边缘裁切，正好
     重新引入 web/CODEBUDDY.md 里明确警告过的「右侧被静默裁掉」问题。 */
  .pane-preview .markdown, .preview, .share-inner .markdown { font-size: 16px; line-height: 1.75; }
  .pane-preview { padding: 16px 14px; }
  .preview { padding: 14px; }

  .pane-preview pre, .preview pre, .share-inner pre { padding: 12px; border-radius: 8px; }
  .pane-preview blockquote, .preview blockquote, .share-inner blockquote { padding: 2px 10px; }

  /* 宽表格在正文容器内横向滚动，不再把页面撑宽。
     多列时靠内在最小宽度触发滚动；少列长文本仍照常换行。 */
  .pane-preview table, .preview table, .share-inner table {
    display: block; overflow-x: auto; max-width: 100%;
  }
  .pane-preview th, .pane-preview td,
  .preview th, .preview td,
  .share-inner th, .share-inner td { padding: 6px 8px; }

  /* —— 触控尺寸：可点元素至少 44px —— */
  button { min-height: 44px; padding: 8px 14px; }
  .icon-btn { width: 44px; height: 44px; }
  .side-item { min-height: 44px; padding: 10px 12px; }
  .modal-close { min-height: 44px; padding: 0 10px; }

  /* 输入框字号 <16px 时 iOS Safari 会在聚焦时自动放大整页，这里统一提到 16px */
  input[type="search"], input[type="text"], input[type="password"], input:not([type]), textarea {
    font-size: 16px; min-height: 44px;
  }

  /* —— 弹窗与安全区 —— */
  .modal { padding: 14px; }
  .modal-card { max-height: 88vh; max-height: 88dvh; }
  .modal-body { padding: 14px; }
  .detailfoot { padding: 6px 12px calc(6px + env(safe-area-inset-bottom)); }

  /* —— 分享页 —— */
  .share-brand-tip { display: none; }
  .topbar .brand-slogan { display: none; }
  .share-inner { padding: 20px 14px calc(20px + env(safe-area-inset-bottom)); }

  /* —— 登录页 —— */
  .login-wrap { padding: 16px; }
  .login-panel { grid-template-columns: 1fr; gap: 20px; max-width: 340px; }
  .login-card { width: 100%; padding: 22px 18px; }

  /* 窄屏空列表提示：隐藏含「新建 / 拖入」的桌面说法，只留可行提示 */
  .empty-wide { display: none; }
  .empty-narrow { display: inline; }
}

/* PDF 导出时临时包裹层 */
.pdf-export {
  background: #fff; color: #23211d;
  padding: 24px 28px;
  /* 不要写死宽度：html2pdf 会把克隆体塞进一个宽 = A4 内容宽(约718px)的容器，
     元素若比容器宽，右侧会被静默裁掉。用 auto 填满容器即可。 */
  width: auto;
  /* 长串（URL、行内代码、长英文单词）强制换行，避免右侧被截断 */
  overflow-wrap: anywhere;
  word-break: break-word;
}
.pdf-export a { color: #3a6ea5; }
.pdf-export code, .pdf-export pre { background: #f4f2ee; }
/* 代码块改为折行，而不是像预览那样横向滚动（滚动会被截图裁掉） */
.pdf-export pre { white-space: pre-wrap; word-break: break-word; overflow: visible; }
/* 表格：固定布局 + 100% 宽，保证不撑破页面（否则右侧会被裁掉） */
.pdf-export table { width: 100%; table-layout: fixed; }
.pdf-export th, .pdf-export td { overflow-wrap: anywhere; word-break: break-word; }
.pdf-export img { max-width: 100%; }

/* 分页：整块元素尽量不被拆到两页（供 html2pdf 的 css 模式读取） */
.pdf-export h1, .pdf-export h2, .pdf-export h3, .pdf-export h4,
.pdf-export h5, .pdf-export h6, .pdf-export p, .pdf-export li,
.pdf-export blockquote, .pdf-export pre, .pdf-export tr, .pdf-export img {
  break-inside: avoid;
  page-break-inside: avoid;
}
/* 标题不要留在页底，尽量跟着后续内容 */
.pdf-export h1, .pdf-export h2, .pdf-export h3, .pdf-export h4 {
  break-after: avoid;
  page-break-after: avoid;
}
