/* ============ 变量 & 重置 ============ */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #fafbfc;
  --border: #e8eaee;
  --border-strong: #d8dbe0;
  --text: #1a1d24;
  --text-2: #5c6470;
  --text-3: #9aa1ad;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: #eef2ff;
  --success: #10b981;
  --success-soft: #ecfdf5;
  --danger: #ef4444;
  --danger-soft: #fef2f2;
  --warn: #f59e0b;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 2px rgba(16,24,40,.05);
  --shadow: 0 1px 3px rgba(16,24,40,.08), 0 1px 2px rgba(16,24,40,.04);
  --shadow-lg: 0 8px 24px rgba(16,24,40,.12);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
          Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --mono: "SF Mono", ui-monospace, Menlo, Consolas, "Liberation Mono", monospace;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }

/* ============ 按钮 / 输入 ============ */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font-family: var(--font); font-size: 13px; font-weight: 500;
  padding: 8px 14px; border-radius: var(--radius-sm);
  border: 1px solid transparent; cursor: pointer; transition: all .15s ease;
  background: var(--surface); color: var(--text); white-space: nowrap;
}
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: transparent; border-color: var(--border-strong); color: var(--text-2); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-block { width: 100%; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

/* ============ 登录视图 ============ */
.login-view {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 24px; background: var(--bg);
}
.login-card {
  width: 100%; max-width: 360px; background: var(--surface);
  border-radius: 16px; padding: 36px 32px; box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
}
.login-mark {
  width: 48px; height: 48px; border-radius: 12px; background: var(--primary);
  color: #fff; font-size: 22px; font-weight: 700; display: flex;
  align-items: center; justify-content: center; margin: 0 auto 16px;
}
.login-title { font-size: 20px; font-weight: 700; text-align: center; }
.login-sub { font-size: 13px; color: var(--text-3); text-align: center; margin: 4px 0 24px; }
.field { display: block; margin-bottom: 14px; }
.field span { display: block; font-size: 12px; font-weight: 500; color: var(--text-2); margin-bottom: 6px; }
.field input {
  width: 100%; padding: 10px 12px; font-size: 14px; font-family: var(--font);
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text); outline: none; transition: border .15s;
}
.field input:focus { border-color: var(--primary); background: #fff; box-shadow: 0 0 0 3px var(--primary-soft); }
.login-error { color: var(--danger); font-size: 12px; margin-top: 12px; text-align: center; min-height: 18px; }

/* ============ 主界面 ============ */
.app-view { display: flex; flex-direction: column; height: 100vh; }

/* 顶部栏 */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  height: 56px; padding: 0 16px; background: var(--surface);
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 8px; background: var(--primary);
  color: #fff; font-size: 15px; font-weight: 700; display: flex; align-items: center; justify-content: center;
}
.brand-name { font-weight: 600; font-size: 15px; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.topbar-user { font-size: 13px; color: var(--text-2); }
.badge {
  display: inline-flex; align-items: center; gap: 5px; font-size: 11px; font-weight: 500;
  padding: 2px 8px; border-radius: 999px;
}
.badge-idle { background: var(--surface-2); color: var(--text-3); border: 1px solid var(--border); }
.badge-idle::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: var(--text-3); }
.badge-running { background: var(--primary-soft); color: var(--primary); }
.badge-running::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: var(--primary); animation: pulse 1s infinite; }
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-fail { background: var(--danger-soft); color: var(--danger); }
@keyframes pulse { 50% { opacity: .3; } }

/* 布局：三栏 */
.layout { display: flex; flex: 1; min-height: 0; }
.pane { display: flex; flex-direction: column; min-width: 0; }
.pane-header { display: flex; align-items: baseline; gap: 8px; padding: 14px 16px 10px; }
.pane-header h2 { font-size: 14px; font-weight: 600; }
.pane-hint { font-size: 11px; color: var(--text-3); }

/* 左栏：文件树 */
.pane-tree { width: 280px; flex-shrink: 0; border-right: 1px solid var(--border); background: var(--surface); }
.tree-scroll { flex: 1; overflow-y: auto; padding: 4px 8px 16px; }
.tree-group { margin-bottom: 4px; }
.tree-group-title { font-size: 11px; font-weight: 600; color: var(--text-3); padding: 8px 8px 4px; text-transform: uppercase; letter-spacing: .04em; }
.tree-item {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: left;
  padding: 8px 10px; border-radius: var(--radius-sm); border: none; background: transparent;
  cursor: pointer; font-family: var(--font); font-size: 13px; color: var(--text-2); transition: background .12s;
}
.tree-item:hover { background: var(--surface-2); }
.tree-item.active { background: var(--primary-soft); color: var(--primary); font-weight: 500; }
.tree-item .tree-icon { font-size: 14px; flex-shrink: 0; }
.tree-item .tree-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-item .tree-chap { font-size: 11px; color: var(--text-3); flex-shrink: 0; }

/* 中栏：内容 */
.pane-content { flex: 1; background: var(--surface); position: relative; border-right: 1px solid var(--border); }
.content-toolbar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 16px; border-bottom: 1px solid var(--border); min-height: 52px;
}
.content-title { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.content-filename { font-family: var(--mono); font-size: 13px; font-weight: 600; }
.content-chaptername { font-size: 12px; color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.content-actions { display: flex; gap: 6px; }
.content-body { flex: 1; overflow: auto; position: relative; }
.empty-state { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; color: var(--text-3); }
.empty-icon { font-size: 40px; margin-bottom: 12px; }
.empty-sub { font-size: 12px; margin-top: 4px; }

/* 代码视图 */
.code-view { font-family: var(--mono); font-size: 12.5px; line-height: 1.6; padding: 16px; white-space: pre-wrap; word-break: break-word; }
.code-view .c-comment { color: #9aa1ad; }
.code-view .c-command { color: #7c3aed; }
.code-view .c-brace { color: #0f766e; }
.code-editor {
  width: 100%; height: 100%; min-height: 400px; border: none; outline: none; resize: none;
  font-family: var(--mono); font-size: 12.5px; line-height: 1.6; padding: 16px; color: var(--text); background: var(--surface);
}

/* 抽屉 */
.drawer {
  position: absolute; inset: 0; background: var(--surface); z-index: 5;
  display: flex; flex-direction: column; border-left: 1px solid var(--border);
}
.drawer-header { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.drawer-header h3 { font-size: 14px; font-weight: 600; }
.drawer-body { flex: 1; overflow-y: auto; padding: 12px 16px; }
.log-entry { border: 1px solid var(--border); border-radius: var(--radius-sm); margin-bottom: 12px; overflow: hidden; }
.log-entry-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; background: var(--surface-2); font-size: 12px; }
.log-entry-head .time { color: var(--text-3); }
.log-entry .diff { font-family: var(--mono); font-size: 11.5px; padding: 10px 12px; overflow-x: auto; white-space: pre; }
.diff-add { color: var(--success); }
.diff-del { color: var(--danger); }

/* 右栏 */
.pane-side { width: 360px; flex-shrink: 0; background: var(--surface); }
.side-tabs { display: flex; border-bottom: 1px solid var(--border); }
.side-tab {
  flex: 1; padding: 11px; border: none; background: transparent; cursor: pointer;
  font-family: var(--font); font-size: 13px; font-weight: 500; color: var(--text-2);
  border-bottom: 2px solid transparent; transition: all .15s;
}
.side-tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.side-body { flex: 1; overflow-y: auto; padding: 14px; display: none; }
.side-body.active { display: flex; flex-direction: column; gap: 14px; }

.build-controls { display: flex; flex-direction: column; gap: 8px; }
.build-estimate { font-size: 12px; color: var(--text-3); text-align: center; min-height: 16px; }
.log-wrap { flex: 1; display: flex; flex-direction: column; min-height: 160px; }
.log-title { font-size: 12px; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }
.log-view {
  flex: 1; background: #0f1117; color: #d4d7dd; border-radius: var(--radius-sm);
  padding: 12px; font-family: var(--mono); font-size: 11.5px; line-height: 1.55;
  overflow-y: auto; white-space: pre-wrap; word-break: break-word; min-height: 140px;
}
.history-list { display: flex; flex-direction: column; gap: 6px; max-height: 220px; overflow-y: auto; }
.history-item {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: var(--radius-sm); cursor: pointer; transition: background .12s;
  font-size: 12px;
}
.history-item:hover { background: var(--surface-2); }
.history-item .dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.dot-success { background: var(--success); }
.dot-fail { background: var(--danger); }
.history-item .h-meta { flex: 1; min-width: 0; }
.history-item .h-time { color: var(--text-3); font-size: 11px; }
.history-item .h-dur { color: var(--text-2); flex-shrink: 0; }

/* PDF 面板 */
.pdf-section { display: flex; flex-direction: column; gap: 8px; }
.pdf-card {
  display: flex; align-items: center; gap: 12px; width: 100%; padding: 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2);
  cursor: pointer; transition: all .15s; text-align: left; font-family: var(--font);
}
.pdf-card:hover { border-color: var(--primary); box-shadow: var(--shadow-sm); }
.pdf-icon { font-size: 22px; }
.pdf-info { display: flex; flex-direction: column; min-width: 0; }
.pdf-name { font-size: 13px; font-weight: 500; color: var(--text); }
.pdf-meta { font-size: 11px; color: var(--text-3); }
.chapter-list { display: flex; flex-direction: column; gap: 6px; }
.chapter-item {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface);
  cursor: pointer; transition: all .15s; text-align: left; font-family: var(--font);
}
.chapter-item:hover { border-color: var(--primary); background: var(--surface-2); }
.chapter-no {
  width: 26px; height: 26px; border-radius: 7px; background: var(--primary-soft); color: var(--primary);
  font-size: 12px; font-weight: 600; display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.chapter-title { flex: 1; font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chapter-size { font-size: 11px; color: var(--text-3); flex-shrink: 0; }

/* 底部 tab（手机） */
.tabbar { display: none; }

/* toast */
.toast {
  position: fixed; bottom: 76px; left: 50%; transform: translateX(-50%);
  background: #1a1d24; color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 13px; z-index: 100; box-shadow: var(--shadow-lg); opacity: 0; transition: opacity .2s;
}
.toast.show { opacity: 1; }
.toast.error { background: var(--danger); }

/* ============ 响应式（手机） ============ */
@media (max-width: 820px) {
  .layout { flex-direction: column; }
  .pane-tree, .pane-side { width: 100%; border-right: none; }
  .pane-content { border-right: none; }
  /* 手机：三栏变成按 tab 切换的单栏 */
  .pane { display: none; }
  .pane.mobile-visible { display: flex; flex: 1; }
  .tabbar {
    display: flex; height: 58px; background: var(--surface); border-top: 1px solid var(--border);
    flex-shrink: 0; padding-bottom: env(safe-area-inset-bottom);
  }
  .tabbar-item {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 2px; border: none; background: transparent; cursor: pointer; font-family: var(--font);
    font-size: 11px; color: var(--text-3);
  }
  .tabbar-item span { font-size: 18px; }
  .tabbar-item.active { color: var(--primary); }
  .app-view { height: 100dvh; }
  .code-view, .code-editor { font-size: 12px; }
  .topbar { height: 50px; }
}

/* ============ 面板错误提示 ============ */
/* 接口失败时在面板内显示，避免"空白一片、看不出是出错还是没数据" */
.panel-error {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; padding: 26px 18px; margin: 8px; text-align: center;
  background: var(--danger-soft); border: 1px solid #fbd5d5;
  border-radius: var(--radius-sm);
}
.panel-error-icon { font-size: 22px; line-height: 1; }
.panel-error-msg {
  font-size: 12.5px; line-height: 1.55; color: var(--danger);
  word-break: break-word; max-width: 100%;
}
.panel-error-retry { margin-top: 2px; }
