/* tab-online — Excel-ish light theme
   Layout: titlebar / toolbar / formulabar / stage / sheetbar  */

:root {
  --accent: #1a7f4b;
  --border: #d5dae1;
  --panel: #f7f8fa;
  --text: #1f2328;
  --muted: #6b7480;
  --sel: #eaf3ee;
  --hdr: #c8ced6;
  --danger: #b3261e;
}

/* ---------- reset / root layout ---------- */
* { box-sizing: border-box; }

html, body, #app {
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 12.5px;
  color: var(--text);
  background: #fff;
  user-select: none;
  -webkit-user-select: none;
}

input, textarea, .menu, .dlg-body { user-select: text; -webkit-user-select: text; }

[hidden] { display: none !important; }
.hidden { display: none !important; }

#app {
  display: grid;
  grid-template-rows: auto auto auto 1fr auto;
}

#fileInput { display: none; }

.spacer { flex: 1; }

/* ---------- titlebar ---------- */
.titlebar {
  height: 40px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.brand {
  font-weight: 700;
  font-size: 14px;
  color: var(--text);
  text-decoration: none;
  flex: none;
}
.brand span { color: var(--accent); }

.doc-name {
  font: inherit;
  font-weight: 600;
  color: var(--text);
  min-width: 120px;
  max-width: 340px;
  height: 26px;
  padding: 0 6px;
  border: 1px solid transparent;
  border-radius: 4px;
  background: transparent;
  outline: none;
}
.doc-name:hover { border-color: var(--border); background: #fff; }
.doc-name:focus { border-color: var(--accent); background: #fff; }

.doc-meta {
  color: var(--muted);
  font-size: 11.5px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  flex: none;
  display: inline-flex;
  align-items: center;
  height: 18px;
  padding: 0 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  background: #eceff3;
  color: var(--muted);
}
.badge[data-state="connecting"] { background: #fdf1d7; color: #9a6700; }
.badge[data-state="online"]     { background: #e0f0e7; color: var(--accent); }
.badge[data-state="poll"]       { background: #e2edf9; color: #2b62a8; }
.badge[data-state="offline"]    { background: #fbe3e1; color: var(--danger); }
.badge[data-state="error"]      { background: #fbe3e1; color: var(--danger); }

.users {
  display: flex;
  align-items: center;
  flex: none;
}
.av {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 1px solid #fff;
  box-shadow: 0 0 0 1px var(--hdr);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 600;
  color: #fff;
  background: #8a94a1;
  flex: none;
}
.av + .av { margin-left: -5px; }
.av.you { box-shadow: 0 0 0 2px var(--accent); }

/* buttons */
.btn {
  height: 28px;
  padding: 0 10px;
  font: inherit;
  font-size: 12px;
  color: var(--text);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
  flex: none;
}
.btn:hover { background: var(--sel); border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--sel); border-color: var(--border); }
.btn.sm { height: 22px; padding: 0 8px; font-size: 11.5px; border-radius: 4px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #166a3f; border-color: #166a3f; }
.btn.danger { background: #fff; border: 1px solid var(--danger); color: var(--danger); }
.btn.danger:hover { background: #fbe3e1; }

/* ---------- toolbar ---------- */
.toolbar {
  height: 34px;
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
  overflow-x: auto;
  padding: 0 6px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { height: 0; }

.group {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: none;
}
.group + .group {
  margin-left: 5px;
  padding-left: 7px;
  border-left: 1px solid var(--border);
}

.cmd {
  width: 26px;
  height: 24px;
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  font: inherit;
  font-size: 12.5px;
  color: var(--text);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
  overflow: hidden;
}
.cmd:hover { background: var(--sel); border-color: var(--border); }
.cmd:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; }
.cmd.on { background: var(--sel); border-color: var(--accent); color: var(--accent); }
.cmd:disabled, .cmd.off { opacity: .38; cursor: default; }
.cmd:disabled:hover, .cmd.off:hover { background: transparent; border-color: transparent; }

.cmd.color { position: relative; flex-direction: column; justify-content: center; gap: 0; }
.cmd.color .glyph { line-height: 1; font-size: 12px; }
.cmd.color .swatch {
  display: block;
  width: 16px;
  height: 8px;
  margin-top: 1px;
  border-radius: 1px;
  box-shadow: inset 0 0 0 1px rgba(31, 35, 40, .18);
  pointer-events: none;
}
.cmd.color .swatch.fg { background: #1f2328; }
.cmd.color .swatch.bg { background: #fff2a8; }
.cmd.color input[type="color"] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  border: 0;
  padding: 0;
  cursor: pointer;
}

.select {
  height: 24px;
  flex: none;
  font: inherit;
  font-size: 11.5px;
  color: var(--text);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0 4px;
  cursor: pointer;
  outline: none;
}
.select:focus { border-color: var(--accent); }
.select.wide { width: 108px; }
.select.size { width: 56px; }

.zoom {
  min-width: 38px;
  text-align: center;
  color: var(--muted);
  font-size: 11.5px;
  flex: none;
}

/* ---------- formula bar ---------- */
.formulabar {
  height: 26px;
  display: flex;
  align-items: stretch;
  background: #fff;
  border-bottom: 1px solid var(--border);
}

.name-box {
  width: 76px;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font: inherit;
  font-size: 11.5px;
  color: var(--muted);
  border-right: 1px solid var(--border);
}

.fx {
  width: 26px;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-style: italic;
  font-size: 11.5px;
  color: var(--muted);
  border-right: 1px solid var(--border);
}

#fxInput {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  outline: none;
  resize: none;
  overflow: hidden;
  padding: 4px 8px;
  font: inherit;
  font-size: 12px;
  line-height: 1.35;
  color: var(--text);
  background: var(--panel);
}
#fxInput:focus { background: #fff; border-bottom-color: var(--accent); }

/* ---------- stage / grid ---------- */
.stage {
  position: relative;
  overflow: hidden;
  min-height: 0;
  background: #fff;
}

#grid {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  outline: none;
  /* 手指拖选必须由自己的手势逻辑处理，否则会变成滚动页面 */
  touch-action: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

#editBox {
  position: absolute;
  margin: 0;
  padding: 0 3px;
  border: 2px solid var(--accent);
  border-radius: 0;
  outline: none;
  resize: none;
  overflow: hidden;
  background: #fff;
  color: var(--text);
  font: inherit;
  line-height: 1.35;
  white-space: pre;
  z-index: 5;
  box-shadow: 0 0 0 3px rgba(26, 127, 75, .12);
}

/* ---------- sheet bar ---------- */
.sheetbar {
  height: 31px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 6px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  overflow: hidden;
}

.tabs {
  display: flex;
  align-items: flex-end;
  height: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
  flex: 1;
  min-width: 0;
}

.tab {
  height: 30px;
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 0 12px;
  font-size: 12px;
  color: var(--text);
  background: transparent;
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: 4px 4px 0 0;
  cursor: pointer;
  white-space: nowrap;
}
.tab:hover { background: var(--sel); }
.tab.on {
  background: #fff;
  border-color: var(--border);
  border-bottom: 1px solid #fff;
  margin-bottom: -1px;
  font-weight: 700;
  box-shadow: inset 0 -2px 0 var(--accent);
}
.tab .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex: none;
}
.tab.hidden-sheet { color: var(--muted); font-style: italic; }

.tab-add {
  width: 24px;
  height: 24px;
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
}
.tab-add:hover { background: var(--sel); border-color: var(--border); }

.sel-info { color: var(--muted); font-size: 11.5px; white-space: nowrap; flex: none; }
.hint { color: var(--muted); font-size: 11.5px; white-space: nowrap; flex: none; padding-right: 4px; }

/* ---------- overlays ---------- */

/* context menu */
#menu, .menu {
  position: absolute;
  z-index: 30;
  min-width: 190px;
  max-height: 60vh;
  overflow: auto;
  padding: 4px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 6px 20px rgba(15, 20, 25, .14);
}

.mi {
  height: 26px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px;
  border-radius: 4px;
  font-size: 12.5px;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.mi:hover { background: var(--sel); }
.mi-ic { width: 16px; flex: none; text-align: center; color: var(--accent); font-size: 11px; }
.mi-t { flex: none; }
.mi-k { margin-left: auto; padding-left: 18px; color: var(--muted); font-size: 11px; }
.mi.on { background: var(--sel); color: var(--accent); font-weight: 600; }
.mi-sep { height: 1px; background: var(--border); margin: 4px 6px; }
.mi.head {
  height: 22px;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .4px;
  cursor: default;
}
.mi.head:hover { background: transparent; }
.mi.danger { color: var(--danger); }
.mi.danger:hover { background: #fbe3e1; }
.mi[disabled], .mi.dis { opacity: .45; pointer-events: none; }

/* tooltip */
.tip {
  position: absolute;
  z-index: 45;
  max-width: 320px;
  padding: 6px 8px;
  background: #1f2328;
  color: #fff;
  border-radius: 4px;
  font-size: 11.5px;
  line-height: 1.45;
  white-space: pre-wrap;
  pointer-events: none;
  box-shadow: 0 4px 12px rgba(15, 20, 25, .25);
}

/* mask */
.mask {
  position: absolute;
  inset: 0;
  z-index: 35;
  background: rgba(15, 20, 25, .35);
}

/* panels */
.panel {
  position: absolute;
  z-index: 40;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(15, 20, 25, .18);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  height: 34px;
  padding: 0 12px;
  font-weight: 600;
  font-size: 12.5px;
  border-bottom: 1px solid var(--border);
}
.panel-head .x {
  width: 22px;
  height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font: inherit;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 4px;
  cursor: pointer;
}
.panel-head .x:hover { background: var(--sel); color: var(--text); }

/* find & replace */
.find-panel {
  right: 16px;
  top: 12px;
  width: 330px;
  padding: 0 0 10px;
}
.find-panel .row {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 8px 12px 0;
}
.find-panel .row input {
  flex: 1;
  min-width: 0;
  height: 26px;
  font: inherit;
  padding: 0 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  outline: none;
}
.find-panel .row input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(26, 127, 75, .15); }
.find-panel .chk {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin: 8px 12px 0;
  font-size: 11.5px;
  color: var(--muted);
  cursor: pointer;
}
.find-panel .info {
  display: block;
  margin: 6px 12px 0;
  color: var(--muted);
  font-size: 11px;
}

/* dialog */
.panel.dlg {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(560px, 92vw);
  max-height: 84vh;
  display: flex;
  flex-direction: column;
}
.dlg-body {
  overflow: auto;
  padding: 12px 14px;
}
.dlg-foot {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
}

.fld { display: grid; gap: 3px; margin-bottom: 10px; }
.fld > label { color: var(--muted); font-size: 11.5px; }
.fld.inline { display: flex; flex-direction: row; align-items: center; gap: 8px; }
.fld input, .fld select, .fld textarea {
  width: 100%;
  min-width: 0;
  height: 26px;
  font: inherit;
  padding: 0 6px;
  color: var(--text);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 4px;
  outline: none;
}
.fld textarea { height: auto; min-height: 56px; padding: 5px 6px; resize: vertical; }
.fld input:focus, .fld select:focus, .fld textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(26, 127, 75, .15);
}
.fld.inline input[type="text"], .fld.inline select { width: auto; flex: 1; }

.dlg-body .grid2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 10px;
}
.dlg-body h4 {
  margin: 12px 0 6px;
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
}
.dlg-body h4:first-child { margin-top: 0; }
.hr { height: 1px; background: var(--border); border: 0; margin: 10px 0; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 20px;
  padding: 0 8px;
  font-size: 11.5px;
  color: var(--text);
  background: #eef1f5;
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
}
.chip:hover { background: var(--sel); }
.chip.on { background: var(--sel); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.chip .del {
  margin-left: 2px;
  font-size: 10px;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
}
.chip .del:hover { color: var(--danger); }

.list {
  max-height: 220px;
  overflow: auto;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 4px;
}
.li {
  height: 28px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}
.li:hover { background: var(--sel); }
.li.on { background: var(--sel); box-shadow: inset 2px 0 0 var(--accent); }

.swatches {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(20px, 20px));
  gap: 5px;
}
.sw {
  width: 20px;
  height: 20px;
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
}
.sw:hover { transform: scale(1.08); }
.sw.on { box-shadow: 0 0 0 2px var(--accent); }

.empty {
  padding: 12px;
  color: var(--muted);
  font-size: 11.5px;
  text-align: center;
}

/* toast */
.toast {
  position: absolute;
  z-index: 50;
  left: 50%;
  bottom: 20px;
  transform: translate(-50%, 10px);
  opacity: 0;
  max-width: 70vw;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 12px;
  color: #fff;
  background: #1f2328;
  box-shadow: 0 6px 18px rgba(15, 20, 25, .25);
  pointer-events: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: opacity .18s, transform .18s;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }
.toast[data-kind="ok"]   { background: var(--accent); }
.toast[data-kind="err"]  { background: var(--danger); }
.toast[data-kind="warn"] { background: #9a6700; }

/* ---------- scrollbars ---------- */
.tabs::-webkit-scrollbar,
.dlg-body::-webkit-scrollbar,
.menu::-webkit-scrollbar,
.list::-webkit-scrollbar {
  height: 10px;
  width: 10px;
}
.tabs::-webkit-scrollbar-thumb,
.dlg-body::-webkit-scrollbar-thumb,
.menu::-webkit-scrollbar-thumb,
.list::-webkit-scrollbar-thumb {
  background: #c8ced6;
  border: 2px solid transparent;
  border-radius: 6px;
  background-clip: content-box;
}
.tabs::-webkit-scrollbar-thumb:hover,
.dlg-body::-webkit-scrollbar-thumb:hover { background: #aab2bd; background-clip: content-box; }
.tabs::-webkit-scrollbar-track,
.dlg-body::-webkit-scrollbar-track,
.menu::-webkit-scrollbar-track,
.list::-webkit-scrollbar-track { background: transparent; }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .hint { display: none; }
}

@media (max-width: 820px) {
  .hint { display: none; }
  .users { display: none; }
  .toolbar { flex-wrap: wrap; height: auto; min-height: 34px; padding-bottom: 4px; }
  .titlebar { gap: 6px; }
  .doc-name { min-width: 90px; }
  .btn { padding: 0 7px; }
}

/* ---------- 手机端（小程序 web-view） ---------- */
@media (max-width: 640px) {
  .titlebar { height: 44px; padding: 0 8px; gap: 4px; }
  .brand { font-size: 13px; }
  .doc-meta,
  #connBadge { display: none; }
  .doc-name { min-width: 70px; font-size: 13px; }

  /* 工具栏改成横向滚动条，不要换行堆高，否则可视区被吃掉 */
  .toolbar {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    height: 42px;
    padding: 0 4px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .toolbar::-webkit-scrollbar { display: none; }

  /* 触摸目标不小于 32px，否则点不准 */
  .cmd { min-width: 34px; height: 34px; }

  /* 触屏没有 hover，提示条会永久钉在屏幕上 */
  .hint { display: none !important; }
}
