/* ============================================================
   Cowork 演示 —— 一比一复刻 App 的深色界面
   配色与工具栏结构取自 apps/cowork（只读）。没有视频文件，整段动画是 CSS。
   基线 = 终态：AI 已改完、用户已改完。无 JS / 爬虫 / reduced-motion 都看到完整文档。
   ============================================================ */

/* 配色变量和布局分成两条写。往变量那条上顺手加一个类，会连 position 和
   margin 一起继承过去 —— 首屏面板就是这么被搞坏过一次的。 */
.cw-demo {
  --cw-bg: #17171c;
  --cw-surface: #1e1e25;
  --cw-line: #2c2c35;
  --cw-line-strong: #3d3d48;
  --cw-text: #f2f2f5;
  --cw-muted: #9a9aa6;
  --cw-ai: #6fb0ff;
  --cw-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;

  position: relative;
  margin-top: clamp(28px, 4vh, 44px);
}

/* ---------- 分段控件 ---------- */

.cw-tabs {
  display: flex;
  gap: 2px;
  width: max-content;
  padding: 3px;
  margin: 0 auto clamp(18px, 2.6vh, 26px);
  border: 1px solid var(--line);
  border-radius: 11px;
  background: rgba(255, 255, 255, .03);
}
.cw-tab {
  appearance: none; border: 0; padding: 7px 22px; border-radius: 8px;
  background: transparent; color: var(--text-muted);
  font: inherit; font-size: 14px; font-weight: 500; cursor: pointer;
  transition: background-color .22s var(--ease), color .22s var(--ease);
}
.cw-tab:hover { color: var(--text); }
.cw-tab.is-active { background: rgba(255, 255, 255, .09); color: var(--text); }

/* ---------- 窗口外壳 ---------- */

.cw-stage {
  position: relative;
  /* 高度由 JS 锁在最高的一态（见 cowork.js 的 lockStageHeight）。
     没有 JS 时按内容走，也不会抖 —— 因为那时根本不播动画。 */
}
.cw-panel { position: absolute; inset: 0 auto auto 0; width: 100%; }
.cw-panel[hidden] { display: none; }

.cw-win {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--cw-line);
  border-radius: 13px;
  background: var(--cw-bg);
  box-shadow: 0 26px 74px rgba(0, 0, 0, .55);
  color: var(--cw-text);
  font-size: 13px;
  -webkit-font-smoothing: antialiased;
}

.cw-bar {
  padding: 8px 12px;
  border-bottom: 1px solid var(--cw-line);
  background: var(--cw-surface);
}
.cw-bar-title { font-weight: 600; font-size: 12.5px; }

.cw-status {
  display: flex; justify-content: space-between; gap: 16px;
  padding: 7px 12px;
  border-top: 1px solid var(--cw-line);
  background: var(--cw-surface);
  color: var(--cw-muted); font-size: 11px;
}
.cw-hint { font-family: var(--cw-mono); opacity: .75; }

/* ---------- 工具栏（结构照抄 App） ---------- */

.cw-toolbar {
  display: flex; align-items: stretch; gap: 0;
  padding: 5px 8px 2px;
  border-bottom: 1px solid var(--cw-line);
  background: var(--cw-surface);
  /* 放不下就从右边裁掉 —— 真的表格软件窗口拉窄时也是这个行为，
     比把图标压到看不清要诚实得多。 */
  overflow: hidden;
  flex-wrap: nowrap;
}
/* 分组内边距压到 6px：8 个分组省下 48px，让整条工具栏进得了容器 */
.cw-group {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 0 6px;
  border-right: 1px solid var(--cw-line);
}
.cw-group:last-child { border-right: 0; }
.cw-trow { display: flex; align-items: center; gap: 1px; }
.cw-group-name {
  color: var(--cw-muted);
  font-size: 9px;
  letter-spacing: .02em;
  line-height: 1.4;
}

.cw-tool {
  display: inline-flex; align-items: center; gap: 3px;
  height: 22px; padding: 0 3.5px;
  border-radius: 5px;
  color: var(--cw-text);
  font-size: 11.5px;
}
.cw-tool.cw-labelled { padding: 0 6px; }
.cw-tool.cw-letter { width: 19px; justify-content: center; font-size: 12px; }
.cw-tool.cw-letter code { font-family: var(--cw-mono); font-size: 10px; }
.cw-tool.cw-swatch { flex-direction: column; gap: 0; height: 22px; padding: 0 4px; }
.cw-swatch-bar { display: block; width: 13px; height: 3px; border-radius: 1px; }
.cw-icon { width: 14px; height: 14px; flex: none; }

.cw-select {
  display: inline-flex; align-items: center; gap: 2px;
  height: 22px; padding: 0 4px 0 7px;
  border: 1px solid var(--cw-line-strong); border-radius: 5px;
  background: var(--cw-bg);
  font-size: 11px; white-space: nowrap;
}
.cw-select .cw-icon { width: 13px; height: 13px; }

/* ---------- 文档 ---------- */

/* 视图停在文档中段：上下都用渐隐切掉，看不到标题 —— 真在编辑时就是这样 */
.cw-doc {
  position: relative;
  max-height: 372px;
  overflow: hidden;
  padding: 0 clamp(18px, 3.5vw, 44px);
}
.cw-doc::before, .cw-doc::after {
  content: ''; position: absolute; left: 0; right: 0; height: 26px; z-index: 2; pointer-events: none;
}
.cw-doc::before { top: 0; background: linear-gradient(var(--cw-bg), transparent); }
.cw-doc::after { bottom: 0; background: linear-gradient(transparent, var(--cw-bg)); }

.cw-page { padding: 14px 0 20px; }

.cw-day {
  margin: 13px 0 5px;
  font-size: 13.5px; font-weight: 650; letter-spacing: -.01em;
}
.cw-page > .cw-day:first-child { margin-top: 4px; }

/* AI 加出来的小节标题 —— 布局重组的可见证据 */
.cw-sub {
  margin: 9px 0 3px 11px;
  font-size: 11px; font-weight: 650; letter-spacing: .09em; text-transform: uppercase;
  color: var(--cw-muted);
}
html[lang="zh-Hans"] .cw-sub { text-transform: none; letter-spacing: .14em; font-size: 11.5px; }

/* 新增的行/小节：终态存在；播放开场时收起来，AI 那一步再长出来 */
.js .cw-demo.is-armed .cw-item--add,
.js .cw-demo.is-armed .cw-sub { max-height: 0; opacity: 0; overflow: hidden; margin-top: 0; margin-bottom: 0; }
.js .cw-demo.step-1 [data-add="1"] {
  max-height: 44px; opacity: 1;
  transition: max-height .42s var(--ease), opacity .38s var(--ease) .1s, margin .42s var(--ease);
}
.js .cw-demo.step-1 .cw-sub[data-add="1"] { margin: 9px 0 3px 11px; }

.cw-item {
  position: relative;
  display: flex; gap: 14px;
  padding: 2.5px 0 2.5px 11px;
  font-size: 13px; line-height: 1.55;
}
.cw-item::before, .cw-row::before {
  content: ''; position: absolute; left: 0; top: 2px; bottom: 2px;
  width: 2px; border-radius: 2px; background: var(--cw-ai);
  opacity: 0; transition: opacity .55s var(--ease);
}
.cw-time {
  flex: none; width: 52px;
  color: var(--cw-muted);
  font-family: var(--cw-mono); font-size: 11.5px; font-variant-numeric: tabular-nums;
  padding-top: 1px;
}
.cw-text { flex: 1; }

/* ---------- 表格 ---------- */

.cw-formula {
  display: flex; align-items: center; gap: 9px;
  padding: 5px 12px;
  border-bottom: 1px solid var(--cw-line);
  font-family: var(--cw-mono); font-size: 11px;
}
.cw-addr {
  min-width: 42px; padding: 3px 7px;
  border: 1px solid var(--cw-line-strong); border-radius: 5px; color: var(--cw-muted);
}
.cw-sigma, .cw-fx { color: var(--cw-muted); }
.cw-fx { font-style: italic; }
.cw-formula-text { color: var(--cw-ai); }

/* 第五列（毛利率）是 AI 插进来的：开场时轨道宽度为 0fr，AI 那一步展开到 1fr。
   fr 值可以插值，所以整个网格是平滑重排的，不是硬跳。 */
.cw-row {
  position: relative;
  display: grid;
  grid-template-columns: 34px 1.1fr 1fr 1fr 1fr var(--col5, 1fr);
  transition: grid-template-columns .5s var(--ease);
  border-bottom: 1px solid var(--cw-line);
}
.cw-row:last-child { border-bottom: 0; }
.cw-cell {
  padding: 6px 10px;
  border-right: 1px solid var(--cw-line);
  font-size: 12px; font-variant-numeric: tabular-nums;
  text-align: right; white-space: nowrap; overflow: hidden;
}
.cw-cell:last-child { border-right: 0; }
.cw-cell--label, .cw-th { text-align: left; }
.cw-rowhead, .cw-colhead, .cw-corner {
  background: var(--cw-surface); color: var(--cw-muted);
  font-size: 10.5px; text-align: center; padding: 4px 8px;
}
.cw-th { font-weight: 600; background: rgba(255, 255, 255, .02); }
.cw-row--total .cw-cell { font-weight: 650; background: rgba(255, 255, 255, .025); }

.cw-tabsbar {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 12px;
  border-top: 1px solid var(--cw-line);
  background: var(--cw-surface);
  font-size: 11px;
}
.cw-sheettab {
  padding: 3px 10px; border-radius: 5px;
  background: var(--cw-bg); border: 1px solid var(--cw-line-strong);
}
.cw-plus { color: var(--cw-muted); }

/* AI 插入的整列：开场收起，AI 那一步撑开 */
.js .cw-demo.is-armed .cw-row { --col5: 0fr; }
.js .cw-demo.is-armed .cw-col--new { opacity: 0; padding-left: 0; padding-right: 0; }
.js .cw-demo.step-1 .cw-row { --col5: 1fr; }
.js .cw-demo.step-1 .cw-col--new { opacity: 1; padding-left: 10px; padding-right: 10px;
  transition: opacity .4s var(--ease) .18s, padding .5s var(--ease); }

/* 被删掉的行 */
.cw-row--del { display: none; }
.js .cw-demo.is-armed .cw-row--del { display: grid; max-height: 30px; overflow: hidden; opacity: 1; }
.js .cw-demo.step-1 .cw-row--del {
  max-height: 0; opacity: 0;
  transition: max-height .4s var(--ease) .3s, opacity .28s var(--ease) .32s;
}

/* 算出来的格子比数据格晚一拍 —— 先有数据，才有结果。 */
.js .cw-demo.step-1 [data-derived] { animation-delay: .2s !important; }
/* 真表格是「回车提交」之后才重算，不是边打边算。
   所以派生格要等打字动画跑完（--ch * 95ms）再加一个提交停顿，才更新。 */
.js .cw-demo.is-edited [data-derived] > .cw-new,
.js .cw-demo.is-edited [data-derived] > .cw-ai { transition-delay: calc(var(--type-ms, 570ms) + 260ms); }
.js .cw-demo.is-edited [data-derived] { --type-ms: 570ms; }

/* ---------- 新旧值交叉淡入（不产生回流） ---------- */

.cw-swap { display: inline-grid; vertical-align: bottom; }
.cw-swap > i { grid-area: 1 / 1; font-style: normal; transition: opacity .3s var(--ease); }
.cw-swap > i:first-child { opacity: 0; }   /* 旧值：终态不显示 */
.cw-swap > i:last-child  { opacity: 1; }   /* 新值：终态显示 */
.cw-cell .cw-swap { justify-items: end; }
.cw-time .cw-swap { justify-items: start; }

.cw-ai {
  position: relative;
  /* 荧光笔常驻 —— 白字，底下一条蓝色标记，永久保留。
     它是「这段是 AI 写的」的持久标识，不是一闪而过的特效。 */
  background: rgba(111, 176, 255, .19);
  box-shadow: 0 0 0 2px rgba(111, 176, 255, .19);
  border-radius: 2px;
}

/* 被 AI 删掉的行：终态里它不存在。播放时先在、然后划掉并把高度收拢。 */
.cw-item--del { display: none; }
.js .cw-demo.is-armed .cw-item--del {
  display: flex;
  overflow: hidden;
  max-height: 34px;
  opacity: 1;
}

/* ---------- 鼠标 ---------- */

.cw-cursor {
  position: absolute;
  left: var(--x, 50%); top: var(--y, 50%);
  opacity: 0; pointer-events: none;
  transform: translate(48px, 56px);
  filter: drop-shadow(0 3px 7px rgba(0, 0, 0, .6));
}
/* 落点由 JS 运行时实测写入；下面只是 JS 未跑时的兜底 */


.cw-replay {
  position: absolute; right: 0; bottom: -32px;
  appearance: none; border: 0; padding: 5px 12px; border-radius: 8px;
  background: transparent; color: var(--text-dim);
  font: inherit; font-size: 12.5px; cursor: pointer;
  opacity: 0; transition: opacity .3s var(--ease), color .22s var(--ease);
}
.cw-replay:hover { color: var(--text); }
.cw-demo.is-done .cw-replay { opacity: 1; }

/* ============================================================
   播放时序
   ============================================================ */

.js .cw-demo.is-armed .cw-ai { clip-path: inset(0 100% 0 0); }
.js .cw-demo.is-armed .cw-swap > i:first-child { opacity: 1; }
.js .cw-demo.is-armed .cw-swap > i:last-child  { opacity: 0; }
.js .cw-demo.is-armed .cw-row--new { opacity: 0; }
.js .cw-demo.is-armed .cw-formula-text { opacity: 0; }

/* 从左往右抹出来，像打字 */
@keyframes cw-wipe {
  from { clip-path: inset(0 100% 0 -3px); }
  to   { clip-path: inset(0 -3px 0 -3px); }
}

/* 一抹亮蓝高光跟着前沿扫过 —— 右缘是最亮的一条，就是"正在写"的笔尖 */
@keyframes cw-sweep {
  0%   { transform: translateX(-108%); opacity: 1; }
  72%  { opacity: 1; }
  100% { transform: translateX(108%); opacity: 0; }
}
@keyframes cw-row-in { from { opacity: 0; transform: translateY(-5px); } to { opacity: 1; transform: none; } }

.cw-ai::after {
  content: ''; position: absolute; inset: -3px -8px;
  border-radius: 3px;
  background: linear-gradient(90deg,
      rgba(111, 176, 255, 0) 0%,
      rgba(111, 176, 255, .28) 55%,
      rgba(111, 176, 255, .85) 93%,
      #d6e9ff 100%);
  opacity: 0; pointer-events: none;
  transform: translateX(-108%);
}

.js .cw-demo.step-1 .cw-ai[data-step="1"],
.js .cw-demo.step-2 .cw-ai[data-step="2"],
.js .cw-demo.step-3 .cw-ai[data-step="3"] {
  animation: cw-wipe .34s cubic-bezier(.4, 0, .15, 1) both;
  animation-delay: 0s;
}
.js .cw-demo.step-1 .cw-ai[data-step="1"]::after,
.js .cw-demo.step-2 .cw-ai[data-step="2"]::after,
.js .cw-demo.step-3 .cw-ai[data-step="3"]::after {
  animation: cw-sweep .58s cubic-bezier(.4, 0, .15, 1) both;
}

.js .cw-demo.step-1 .cw-rewrite[data-step="1"] > i:first-child,
.js .cw-demo.step-2 .cw-rewrite[data-step="2"] > i:first-child,
.js .cw-demo.step-3 .cw-rewrite[data-step="3"] > i:first-child { opacity: 0; }
.js .cw-demo.step-1 .cw-rewrite[data-step="1"] > i:last-child,
.js .cw-demo.step-2 .cw-rewrite[data-step="2"] > i:last-child,
.js .cw-demo.step-3 .cw-rewrite[data-step="3"] > i:last-child { opacity: 1; }

.js .cw-demo.step-1 .cw-row--new[data-step="1"] { animation: cw-row-in .45s var(--ease) both; }
.js .cw-demo.step-2 .cw-formula-text { animation: cw-wipe .45s cubic-bezier(.4,0,.15,1) both; opacity: 1; }

/* 改动过的行亮起左边条 */
.js .cw-demo.step-1 [data-del="1"] {
  max-height: 0; opacity: 0;
  text-decoration: line-through; text-decoration-color: var(--cw-ai);
  transition: max-height .42s var(--ease) .28s, opacity .3s var(--ease) .3s;
}
.js .cw-demo.step-1 [data-touched="1"]::before,
.js .cw-demo.step-2 [data-touched="2"]::before,
.js .cw-demo.step-3 [data-touched="3"]::before { opacity: .85; }

/* 落定 */
.js .cw-demo.is-settled [data-touched]::before { opacity: 0; }

/* 鼠标进场 → 用户改一处 */
.js .cw-demo.is-cursor .cw-panel.is-active .cw-cursor {
  opacity: 1; transform: translate(0, 0);
  transition: opacity .3s var(--ease), transform .95s cubic-bezier(.3, .85, .3, 1);
}
.js .cw-demo.is-edited .cw-swap--user > i:first-child { opacity: 0; }
.js .cw-demo.is-edited .cw-swap--user > i:last-child  { opacity: 1; }

/* 用户是逐字敲的 —— steps() 按字符数一格一格推出来，末尾一道闪烁的插入符。
   跟 AI「一整片瞬变」形成对比：AI 一次改一片，人一次改一个字。 */
.cw-typed > .cw-new { overflow: hidden; white-space: nowrap; }
.js .cw-demo.is-edited .cw-typed > .cw-new {
  animation: cw-type calc(var(--ch, 5) * 95ms) steps(var(--ch, 5), end) both,
             cw-caret .62s steps(1, end) calc(var(--ch, 5) * 95ms + 500ms) 3;
  max-width: calc(var(--ch, 5) * 1ch);
  border-right: 1.5px solid var(--cw-ai);
}
@keyframes cw-type { from { max-width: 0; } to { max-width: calc(var(--ch, 5) * 1ch); } }
@keyframes cw-caret { 0%, 50% { border-right-color: var(--cw-ai); } 51%, 100% { border-right-color: transparent; } }
.js .cw-demo.is-done .cw-typed > .cw-new { border-right-color: transparent; }

@media (prefers-reduced-motion: reduce) {
  .js .cw-demo.is-armed .cw-ai { clip-path: none; }
  .js .cw-demo.is-armed .cw-row--new,
  .js .cw-demo.is-armed .cw-formula-text { opacity: 1; }
  .js .cw-demo.is-armed .cw-swap > i:first-child { opacity: 0; }
  .js .cw-demo.is-armed .cw-swap > i:last-child  { opacity: 1; }
  .cw-cursor { display: none; }
  .cw-demo *, .cw-demo *::after { animation: none !important; }
}

/* ---------- 响应式 ---------- */

@media (max-width: 900px) {
  .cw-toolbar { overflow-x: auto; }
  .cw-group { padding: 0 7px; }
}
@media (max-width: 760px) {
  .cw-doc { max-height: 330px; padding: 0 15px; }
  .cw-item { gap: 9px; padding-left: 9px; font-size: 12.5px; }
  .cw-time { width: 45px; font-size: 11px; }
  .cw-row { grid-template-columns: 28px 1fr 1fr 1fr 1fr; }
  .cw-cell { padding: 5px 6px; font-size: 11px; }
  .cw-hint { display: none; }
  .cw-replay { bottom: -30px; }
}
