:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --line: #e4e7ec;
  --line-strong: #d3d8e0;
  --ink: #191c22;
  --ink-2: #5b6472;
  --ink-3: #8a93a2;
  --brand: #1a5fd0;
  --brand-soft: #eaf1fe;
  --ok: #1e9e6a;
  --warn: #d9822b;
  --danger: #d84545;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .05);
  --shadow-lg: 0 8px 24px rgba(16, 24, 40, .10);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 13px;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea { font-family: inherit; font-size: inherit; color: inherit; }

.view { position: absolute; inset: 0; display: none; }
.view.active { display: flex; }

/* ============ 首页 ============ */
#view-home { flex-direction: column; overflow-y: auto; }

.home-top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 32px; border-bottom: 1px solid var(--line);
  background: var(--panel); position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand .logo { width: 38px; height: 38px; }
.brand h1 { font-size: 17px; font-weight: 650; letter-spacing: -.3px; }
.brand p { font-size: 11.5px; color: var(--ink-3); margin-top: 1px; }

.health { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--ink-2); }
.health .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ink-3); transition: background .2s; }
.health.ok .dot { background: var(--ok); box-shadow: 0 0 0 3px rgba(30, 158, 106, .14); }
.health.bad .dot { background: var(--danger); box-shadow: 0 0 0 3px rgba(216, 69, 69, .14); }

.home-main { max-width: 1080px; width: 100%; margin: 0 auto; padding: 32px 32px 60px; }

.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.section-head h2 { font-size: 15px; font-weight: 600; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--line-strong); background: var(--panel);
  cursor: pointer; font-weight: 550; transition: all .15s;
}
.btn:hover { border-color: var(--brand); color: var(--brand); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: #1750b3; border-color: #1750b3; color: #fff; }

.sheet-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px;
}

.sheet-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; cursor: pointer; transition: all .18s; position: relative;
}
.sheet-card:hover { border-color: #b9c6dd; box-shadow: var(--shadow-lg); transform: translateY(-1px); }
.sheet-card .ico {
  width: 34px; height: 34px; border-radius: 9px; background: var(--brand-soft);
  display: flex; align-items: center; justify-content: center; margin-bottom: 12px; color: var(--brand);
}
.sheet-card h3 { font-size: 13.5px; font-weight: 600; margin-bottom: 6px; word-break: break-all; }
.sheet-card .meta { font-size: 11.5px; color: var(--ink-3); display: flex; gap: 10px; }
.sheet-card .del {
  position: absolute; top: 12px; right: 12px; width: 24px; height: 24px;
  border: none; background: transparent; border-radius: 6px; cursor: pointer;
  color: var(--ink-3); opacity: 0; transition: all .15s; display: flex; align-items: center; justify-content: center;
}
.sheet-card:hover .del { opacity: 1; }
.sheet-card .del:hover { background: #feecec; color: var(--danger); }

.home-tips { margin-top: 34px; display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.tip {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 13px 15px; font-size: 12px; color: var(--ink-2); line-height: 1.55;
}
.tip b { color: var(--ink); font-weight: 600; }

/* ============ 表格页 ============ */
#view-sheet { flex-direction: column; background: var(--panel); }

.topbar {
  height: 46px; flex: 0 0 46px; display: flex; align-items: center; gap: 10px;
  padding: 0 12px; border-bottom: 1px solid var(--line); background: var(--panel); z-index: 30;
}
.icon-btn {
  width: 30px; height: 30px; border-radius: 7px; border: none; background: transparent;
  cursor: pointer; display: flex; align-items: center; justify-content: center; color: var(--ink-2);
}
.icon-btn:hover { background: #f0f2f5; color: var(--ink); }

.doc-title { display: flex; align-items: center; gap: 8px; min-width: 0; }
.doc-title input {
  border: 1px solid transparent; border-radius: 6px; padding: 4px 6px;
  font-size: 13.5px; font-weight: 600; background: transparent; width: 220px; outline: none;
}
.doc-title input:hover { border-color: var(--line); }
.doc-title input:focus { border-color: var(--brand); background: #fff; }
.saved-tag { font-size: 11px; color: var(--ink-3); white-space: nowrap; }

.toolbar-spacer { flex: 1; }

.presence { display: flex; align-items: center; margin-right: 4px; }
.presence .avatar {
  width: 26px; height: 26px; border-radius: 50%; margin-left: -7px;
  border: 2px solid #fff; display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 600; color: #fff; position: relative; cursor: default;
  transition: transform .15s;
}
.presence .avatar:first-child { margin-left: 0; }
.presence .avatar:hover { transform: translateY(-2px); z-index: 5; }
.presence .avatar.me { box-shadow: 0 0 0 2px rgba(26, 95, 208, .25); }

.metrics { display: flex; align-items: center; gap: 2px; padding: 0 8px; border-left: 1px solid var(--line); }
.metrics .m { display: flex; align-items: baseline; gap: 3px; padding: 3px 7px; border-radius: 6px; }
.metrics .mk { font-size: 10.5px; color: var(--ink-3); }
.metrics .mv { font-size: 11.5px; font-weight: 650; font-variant-numeric: tabular-nums; color: var(--ink); min-width: 20px; text-align: right; }
.metrics .mu { font-size: 10px; color: var(--ink-3); }
.metrics .m.warn .mv { color: var(--warn); }
.metrics .m.bad .mv { color: var(--danger); }

.conn { display: flex; align-items: center; gap: 6px; font-size: 11.5px; color: var(--ink-2); padding-left: 8px; border-left: 1px solid var(--line); }
.conn .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ink-3); transition: all .2s; }
.conn.ok .dot { background: var(--ok); }
.conn.bad .dot { background: var(--danger); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* 表格画布区 */
.sheet-wrap { position: relative; flex: 1; overflow: hidden; background: #fff; }
#grid { position: absolute; inset: 0; pointer-events: none; z-index: 1; }

.scroller {
  position: absolute; inset: 0; overflow: auto; z-index: 2;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: #c8ccd4 transparent;
}
.scroller:focus { outline: none; }
.scroller::-webkit-scrollbar { width: 11px; height: 11px; }
.scroller::-webkit-scrollbar-track { background: transparent; }
.scroller::-webkit-scrollbar-thumb { background: #cfd4dc; border-radius: 6px; border: 3px solid #fff; }
.scroller::-webkit-scrollbar-thumb:hover { background: #b3bac6; }
.scroller::-webkit-scrollbar-corner { background: transparent; }
.spacer { width: 1px; height: 1px; }

.cell-editor {
  position: absolute; z-index: 20; display: none;
  border: 2px solid var(--brand); border-radius: 2px; outline: none;
  padding: 0 5px; font-size: 13px; background: #fff;
  box-shadow: 0 2px 8px rgba(26, 95, 208, .18);
  line-height: normal;
}
.clipboard-helper {
  position: absolute; left: -9999px; top: 0; width: 1px; height: 1px; opacity: 0;
}

/* 状态栏 */
.statusbar {
  height: 28px; flex: 0 0 28px; display: flex; align-items: center; justify-content: space-between;
  padding: 0 12px; border-top: 1px solid var(--line); background: #fafbfc;
  font-size: 11.5px; color: var(--ink-2); z-index: 30;
}
.sb-left, .sb-right { display: flex; align-items: center; gap: 9px; }
.sb-sep { width: 1px; height: 11px; background: var(--line-strong); }
#sbCell { font-weight: 650; color: var(--ink); font-variant-numeric: tabular-nums; }
.sb-value { max-width: 420px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--ink-3); }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 46px; transform: translate(-50%, 12px);
  background: #1f2430; color: #fff; padding: 9px 18px; border-radius: 8px;
  font-size: 12.5px; opacity: 0; pointer-events: none; transition: all .22s; z-index: 999;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .2);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 860px) {
  .metrics { display: none; }
  .doc-title input { width: 130px; }
}