:root {
  --cream: #fbf5ea;
  --paper: rgba(253, 249, 241, 0.86);
  --paper-solid: #fdf8ef;
  --ink: #574a5c;
  --ink-soft: #8a7b8c;
  --line: rgba(170, 150, 190, 0.35);
  --mist: #cfd9f2;
  --lav: #b7a3dc;
  --lav-deep: #7d68b3;
  --warm: #f3c77a;
  --ok: #7fb58b;
  --warn: #e7a45c;
  --bad: #d9727a;
  --attn: #8f9fe0;
  --test: #f0a040;
  --shadow: 0 6px 20px rgba(110, 90, 140, 0.16);
  --radius: 16px;
  --lw: 288px;
  --rw: 300px;
  --top: 56px;
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  color: var(--ink);
  background: linear-gradient(180deg, #dfe6f7 0%, #f3ecf3 60%, var(--cream) 100%);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }

/* ---------- 頂列 ---------- */
.topbar {
  height: var(--top); display: flex; align-items: center; gap: 10px;
  padding: 0 calc(12px + var(--safe-r)) 0 calc(12px + var(--safe-l));
  background: var(--paper); border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  position: relative; z-index: 50;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: .06em; }
.brand img { border-radius: 10px; }
.topbar-mid { flex: 1; display: flex; justify-content: center; }
.clock { font-variant-numeric: tabular-nums; color: var(--ink-soft); font-size: 14px; }
.icon-btn {
  min-width: 44px; height: 44px; border-radius: 12px; border: 1px solid transparent;
  background: transparent; font-size: 20px; display: inline-grid; place-items: center;
}
.icon-btn:hover { background: rgba(183, 163, 220, .18); }
.conn-pill {
  padding: 4px 12px; border-radius: 999px; font-size: 13px; font-weight: 600;
  background: #eee; color: #777; border: 1px solid #ddd;
}
.conn-pill[data-mode="live"] { background: #e3f3e6; color: #3e7a4c; border-color: #bfe0c6; }
.conn-pill[data-mode="test"] { background: #fff0db; color: #a35f0f; border-color: #f4cf98; }
.conn-pill[data-mode="offline"] { background: #f3e6e8; color: #9b4a55; border-color: #e6c4c9; }

/* ---------- 版面 ---------- */
.layout {
  height: calc(100% - var(--top));
  display: grid;
  grid-template-columns: var(--lw) minmax(0, 1fr) var(--rw);
  transition: grid-template-columns .25s ease;
}
.layout.left-collapsed { grid-template-columns: 0 minmax(0, 1fr) var(--rw); }
.layout.right-collapsed { grid-template-columns: var(--lw) minmax(0, 1fr) 0; }
.layout.left-collapsed.right-collapsed { grid-template-columns: 0 minmax(0, 1fr) 0; }
.sidebar {
  overflow: hidden auto; min-width: 0;
  background: var(--paper); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
}
.sidebar.left { border-right: 1px solid var(--line); padding-left: var(--safe-l); }
.sidebar.right { border-left: 1px solid var(--line); padding-right: var(--safe-r); }
.layout.left-collapsed .sidebar.left, .layout.right-collapsed .sidebar.right { visibility: hidden; }
.tab-body { padding: 12px 14px calc(24px + var(--safe-b)); }

.tabs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; padding: 10px 10px 0; position: sticky; top: 0; background: var(--paper-solid); z-index: 2; }
.tabs button {
  min-height: 44px; border: 0; border-radius: 12px 12px 0 0; background: transparent; font-weight: 600; color: var(--ink-soft);
  border-bottom: 3px solid transparent;
}
.tabs button[aria-selected="true"] { color: var(--lav-deep); border-bottom-color: var(--lav); background: rgba(183, 163, 220, .12); }

/* ---------- 卡片與清單 ---------- */
.card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin-bottom: 12px; box-shadow: var(--shadow); }
.card h3 { margin: 0 0 8px; font-size: 15px; display: flex; align-items: center; gap: 6px; }
.card h3 .sub { font-weight: 400; font-size: 12px; color: var(--ink-soft); margin-left: auto; }
.muted { color: var(--ink-soft); font-size: 13px; }
.empty { color: var(--ink-soft); font-size: 13px; padding: 8px 0; text-align: center; }
.list { list-style: none; margin: 0; padding: 0; }
.list > li { border-top: 1px dashed var(--line); padding: 8px 2px; }
.list > li:first-child { border-top: 0; }
.row-btn { width: 100%; text-align: left; background: none; border: 0; padding: 6px 4px; border-radius: 10px; min-height: 44px; }
.row-btn:hover { background: rgba(183, 163, 220, .12); }
.task-title { font-weight: 600; font-size: 14px; display: flex; gap: 6px; align-items: center; }
.task-meta { font-size: 12px; color: var(--ink-soft); display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-top: 4px; }
.mono { font-family: ui-monospace, "SF Mono", Consolas, monospace; font-size: 12px; }
.chip { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: #eee; white-space: nowrap; }
.chip[data-tone="neutral"] { background: #eeeaf2; color: #6d6276; }
.chip[data-tone="active"] { background: #e2e8fb; color: #4b5fb0; }
.chip[data-tone="attention"] { background: #efe6fb; color: #6e4fae; }
.chip[data-tone="warn"] { background: #fcebd7; color: #a1601b; }
.chip[data-tone="ok"] { background: #e2f2e5; color: #3f7a4d; }
.chip[data-tone="bad"] { background: #f8e0e2; color: #a2404b; }
.chip.src-test { background: #fff0db; color: #a35f0f; }
.chip.src-claude { background: #fbe9df; color: #a2532a; }
.chip.src-codex { background: #e3ecf8; color: #35598f; }
.chip.src-user { background: #eef0e6; color: #5d6a3c; }

/* 階段步驟（取代假百分比） */
.stepper { display: flex; gap: 3px; margin-top: 6px; }
.stepper span { flex: 1; height: 6px; border-radius: 3px; background: #ece6f2; }
.stepper span.done { background: #cdbfe8; }
.stepper span.now { background: var(--lav-deep); }
.stepper.failed span.now { background: var(--bad); }

.usage-grid { display: grid; grid-template-columns: 1fr; gap: 0; font-size: 13px; margin: 0; }
.usage-grid dt { color: var(--ink-soft); font-size: 12px; padding-top: 6px; }
.usage-grid dd { margin: 0; font-variant-numeric: tabular-nums; padding-bottom: 4px; border-bottom: 1px dashed var(--line); }
.usage-grid dd:last-of-type { border-bottom: 0; }
.na { color: #b2a4b6; }
.src-line { font-size: 11px; color: var(--ink-soft); margin-top: 8px; line-height: 1.5; }

.field { display: grid; gap: 4px; margin-bottom: 10px; font-size: 13px; }
.field select, .field input { min-height: 40px; border-radius: 10px; border: 1px solid var(--line); padding: 0 10px; font: inherit; background: #fff; }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 44px; font-size: 14px; }
.switch input { width: 22px; height: 22px; accent-color: var(--lav-deep); }
.btn { min-height: 40px; padding: 0 14px; border-radius: 12px; border: 1px solid var(--line); background: #fff; font-weight: 600; }
.btn.primary { background: var(--lav-deep); color: #fff; border-color: var(--lav-deep); }
.btn.danger { color: var(--bad); }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }
.avatar { width: 34px; height: 34px; object-fit: contain; flex: none; }

/* ---------- 舞台 ---------- */
.stage-wrap {
  position: relative; min-width: 0; min-height: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 10px; container-type: size;
}
.stage {
  position: relative;
  width: min(100cqw, calc((100cqh - 56px) * 16 / 9));
  aspect-ratio: 16 / 9;
  border-radius: 22px; overflow: hidden; box-shadow: 0 14px 40px rgba(100, 90, 150, .22);
  background: #dfe6f7;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.stage .bg { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; filter: saturate(.92) brightness(1.02); }
/* 遠景弱化：頂部輕霧 */
.stage::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; z-index: 1;
  background: linear-gradient(180deg, rgba(240, 236, 250, .28) 0%, rgba(240, 236, 250, 0) 42%);
}
.layer { position: absolute; inset: 0; z-index: 2; }
.sprite-box { position: absolute; transform: translate(-50%, -100%); pointer-events: none; }
.sprite-box img { display: block; width: 100%; height: auto; pointer-events: none; }
.sprite-box.fg { opacity: .92; }

.cat { pointer-events: auto; cursor: pointer; background: none; border: 0; padding: 0; }
.cat img.sprite { filter: drop-shadow(0 2px 2px rgba(90, 70, 120, .18)); transform-origin: 50% 100%; }
.cat.facing-left img.sprite { transform: scaleX(-1); }
.cat.walking.bob img.sprite { animation: bob .42s ease-in-out infinite; }
.cat.facing-left.walking.bob img.sprite { animation-name: bob-left; }
@keyframes bob { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-4%); } }
@keyframes bob-left { 0%,100% { transform: scaleX(-1) translateY(0); } 50% { transform: scaleX(-1) translateY(-4%); } }
/* 待機動畫（只在沒走路時；減少動態與背景分頁時停止） */
.cat:not(.walking) img.sprite { animation-delay: var(--idle-delay, 0s); }
.cat:not(.walking)[data-pose="work"] img.sprite { animation: idle-work 1.4s ease-in-out infinite; animation-delay: var(--idle-delay, 0s); }
.cat:not(.walking)[data-pose="read"] img.sprite { animation: idle-read 4.2s ease-in-out infinite; animation-delay: var(--idle-delay, 0s); }
.cat:not(.walking)[data-pose="rest"] img.sprite { animation: idle-rest 3.6s ease-in-out infinite; animation-delay: var(--idle-delay, 0s); }
.cat:not(.walking)[data-pose="move"] img.sprite { animation: idle-stand 2.6s ease-in-out infinite; animation-delay: var(--idle-delay, 0s); }
@keyframes idle-work { 0%, 100% { transform: translateY(0); } 25% { transform: translateY(-1.6%); } 50% { transform: translateY(0); } 75% { transform: translateY(-1%); } }
@keyframes idle-read { 0%, 100% { transform: rotate(-1.2deg); } 50% { transform: rotate(1.2deg); } }
@keyframes idle-rest { 0%, 100% { transform: scale(1, 1); } 50% { transform: scale(1.015, 1.035); } }
@keyframes idle-stand { 0%, 100% { transform: translateY(0); } 40% { transform: translateY(-2.5%); } 60% { transform: translateY(-2.5%); } }
.cat.selected img.sprite { filter: drop-shadow(0 0 6px rgba(255, 220, 140, .95)) drop-shadow(0 2px 2px rgba(90, 70, 120, .2)); }
.cat.unbound img.sprite { opacity: .78; filter: saturate(.7); }
.cat:focus-visible { outline: 3px solid var(--lav-deep); outline-offset: 2px; border-radius: 12px; }
.cat-tag {
  position: absolute; left: 50%; top: -6%; transform: translate(-50%, -100%);
  font-size: clamp(9px, 1.05cqw, 13px); white-space: nowrap; padding: 2px 8px; border-radius: 999px;
  background: rgba(255, 252, 246, .92); border: 1px solid var(--line); box-shadow: var(--shadow);
  opacity: 0; transition: opacity .2s; pointer-events: none;
}
.cat:hover .cat-tag, .cat.selected .cat-tag, .stage.show-tags .cat-tag { opacity: 1; }
.cat-bubble {
  position: absolute; right: 2%; top: 4%; width: 26%; aspect-ratio: 1; border-radius: 50%;
  display: grid; place-items: center; font-size: clamp(9px, 1.1cqw, 15px); font-weight: 700;
  background: #fff; border: 2px solid var(--lav); color: var(--lav-deep); box-shadow: var(--shadow);
}
.cat-bubble[data-k="waiting_user"] { border-color: var(--attn); }
.cat-bubble[data-k="needs_rework"] { border-color: var(--warn); color: var(--warn); }
.cat-bubble[data-k="failed"] { border-color: var(--bad); color: var(--bad); }
.cat-bubble[data-k="awaiting_review"] { border-color: var(--ok); color: var(--ok); }

.zone-label {
  position: absolute; transform: translate(-50%, -50%); z-index: 88; pointer-events: auto;
  display: inline-flex; align-items: center; gap: 4px; white-space: nowrap;
  padding: .35em .9em; border-radius: 999px; min-height: 30px;
  font-size: clamp(10px, 1.15cqw, 15px); font-weight: 700; color: var(--ink);
  background: rgba(255, 251, 243, .9); border: 1.5px solid rgba(214, 190, 140, .8);
  box-shadow: 0 3px 10px rgba(120, 100, 150, .18);
}
.zone-label .count { font-size: .85em; background: var(--lav); color: #fff; border-radius: 999px; padding: 0 .5em; }
.zone-label:hover, .zone-label.selected { background: #fff; border-color: var(--warm); }
.zone-label::before { content: ""; position: absolute; inset: -8px; } /* 擴大觸控範圍 */

.pair-box { pointer-events: auto; cursor: pointer; background: none; border: 0; padding: 0; }
.pair-note { position: absolute; left: 50%; bottom: -4%; transform: translate(-50%, 100%); font-size: clamp(9px, .95cqw, 12px); white-space: nowrap; background: rgba(255,252,246,.9); border-radius: 999px; padding: 1px 8px; border: 1px solid var(--line); }

.test-banner, .offline-banner {
  position: absolute; left: 50%; top: 10px; transform: translateX(-50%); z-index: 95;
  padding: 6px 14px; border-radius: 999px; font-size: clamp(11px, 1.1cqw, 14px); font-weight: 700; white-space: nowrap;
  max-width: calc(100% - 20px); overflow: hidden; text-overflow: ellipsis;
}
.test-banner {
  color: #7a4300; border: 2px dashed var(--test);
  background: repeating-linear-gradient(135deg, #fff4e2 0 10px, #ffe7c4 10px 20px);
}
.offline-banner { top: auto; bottom: 10px; background: #f8e8ea; color: #9b4a55; border: 1px solid #e6c4c9; }
.stage.test-mode { box-shadow: 0 0 0 4px var(--test), 0 14px 40px rgba(100, 90, 150, .22); }

.test-controls {
  margin-top: 10px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: center;
  padding: 6px 10px; border-radius: 999px; background: #fff7ea; border: 2px dashed var(--test);
}
.test-controls button { min-height: 40px; border-radius: 999px; border: 1px solid #f1d2a2; background: #fff; padding: 0 12px; font-weight: 600; }
.test-controls button[aria-pressed="true"] { background: var(--test); color: #fff; }
.tc-label { font-weight: 700; color: #a35f0f; font-size: 13px; }

/* ---------- 詳情面板 ---------- */
.scrim { position: fixed; inset: 0; background: rgba(60, 50, 80, .25); z-index: 60; }
.sheet {
  border: 0; padding: 0; border-radius: 20px; width: min(520px, calc(100vw - 24px)); max-height: min(80vh, 720px);
  background: var(--paper-solid); color: var(--ink); box-shadow: 0 20px 60px rgba(80, 60, 120, .35);
}
.sheet::backdrop { background: rgba(60, 50, 80, .25); }
.sheet-head { display: flex; align-items: center; padding: 10px 10px 6px 18px; position: sticky; top: 0; background: var(--paper-solid); }
.sheet-head h2 { font-size: 18px; margin: 0; flex: 1; }
.sheet-body { padding: 4px 18px 20px; overflow: auto; max-height: calc(min(80vh, 720px) - 60px); }
.kv { display: grid; grid-template-columns: 7em 1fr; gap: 6px 10px; font-size: 14px; margin: 8px 0 14px; }
.kv dt { color: var(--ink-soft); }
.kv dd { margin: 0; word-break: break-all; }
.detail-hero { display: flex; gap: 12px; align-items: center; }
.detail-hero img { width: 96px; height: 96px; object-fit: contain; }

/* ---------- 窄螢幕（iPad 橫向/直向、手機）：側欄改為抽屜 ---------- */
@media (max-width: 1199px) {
  .layout, .layout.left-collapsed, .layout.right-collapsed, .layout.left-collapsed.right-collapsed { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; top: var(--top); bottom: 0; width: min(340px, 86vw); z-index: 70;
    box-shadow: var(--shadow); transition: transform .25s ease; visibility: visible !important;
    background: var(--paper-solid);
  }
  .sidebar.left { left: 0; transform: translateX(-105%); }
  .sidebar.right { right: 0; transform: translateX(105%); }
  .layout.left-open .sidebar.left, .layout.right-open .sidebar.right { transform: none; }
  .stage-wrap { grid-column: 1; }
}
/* 直向：舞台在上、右側欄內容改放下方（可收合），左側欄仍為抽屜 */
@media (max-width: 1199px) and (orientation: portrait) {
  .layout, .layout.left-collapsed, .layout.right-collapsed, .layout.left-collapsed.right-collapsed {
    grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr);
  }
  .stage-wrap { container-type: inline-size; grid-row: 1; padding-bottom: 4px; }
  .stage { width: 100%; }
  .sidebar.right {
    position: static; transform: none; width: auto; grid-row: 2; box-shadow: none;
    border-left: 0; border-top: 1px solid var(--line); padding-right: 0;
  }
  .sidebar.right .tab-body { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 0 12px; align-items: start; }
  .layout.right-collapsed .sidebar.right { display: none; }
}
@media (max-width: 560px) {
  .brand span { display: none; }
  .clock { display: none; }
}

/* ---------- 減少動態 ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}
body.reduce-motion *, body.reduce-motion *::before, body.reduce-motion *::after { animation: none !important; transition: none !important; }
body.page-hidden * { animation-play-state: paused !important; }
