/* ==========================================================================
   main.css — Shell 樣式（app grid / sidebar / topbar / 頁首 / 基礎 primitives）
   規格來源：docs/design-tokens.md。所有數值經 styles/tokens.css 嘅 variables。
   注意：原版 CSS 零 transition — hover/active 變色係即時跳變，唔好加 transition。
   ========================================================================== */

/* ---- Reset / base ---- */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

body {
  font: 500 var(--fs-base)/1.45 var(--font);
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
  color: var(--text);
  background: var(--bg);
}

button { font: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }
b { font-weight: 700; }

::selection { background: var(--selection); }
mark { background: var(--mark-bg); border-radius: 3px; padding: 0 1px; }

:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.sr {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.skip {
  position: fixed; top: -60px; left: 16px; z-index: 100;
  padding: 10px 16px; border-radius: 12px;
  background: var(--surface); border: 1px solid var(--line-2);
  box-shadow: var(--shadow-pop); font-weight: 600;
}
.skip:focus { top: 12px; }

/* 圖示慣例：24px viewBox 線性 icon，size 由 context 決定 */
.ico {
  width: 19px; height: 19px; flex: none;
  fill: none; stroke: currentColor;
  stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round;
}
.ico--sm { width: 15px; height: 15px; }

/* Route 切換：消滅 View Transitions 預設 cross-fade（原版行為：即時換頁，
   動畫全靠新 view 進場編排）。theme toggle 嘅 circle reveal 由 JS WAAPI 驅動
   ::view-transition-new(root)，呢度設定令佢唔會同預設動畫打架。 */
::view-transition-old(root),
::view-transition-new(root) { animation: none; mix-blend-mode: normal; }

/* ==========================================================================
   App grid：sidebar 固定 252px + 主區；成個 app max 1760px 置中
   ========================================================================== */
.app {
  display: grid;
  grid-template-columns: var(--side-w) minmax(0, 1fr);
  max-width: var(--app-max);
  margin: 0 auto;
  min-height: 100dvh;
}

.main { padding: 0 var(--gutter) 64px; min-width: 0; }

.view-host { display: block; outline: none; }
.view__body { display: block; }

/* ==========================================================================
   Sidebar（深綠欄，sticky 全高圓角面板）
   ========================================================================== */
.side {
  position: sticky; top: 0;
  align-self: start;
  height: 100dvh;
  padding: 14px 0 14px 14px;
  z-index: 30;
}

.side__panel {
  height: 100%;
  display: flex; flex-direction: column;
  min-height: 0;
  background: var(--side);
  color: var(--on-side);
  border-radius: var(--r-side-panel);
  padding: 18px 14px 14px;
  box-shadow: var(--shadow-side);
}

/* 品牌區：40px 米杏 mark + 21px/700 名 + 12px tag */
.side__head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 2px 4px 16px;
}
.brand { display: inline-flex; align-items: center; gap: 10px; min-width: 0; }
.brand__mark {
  width: 40px; height: 40px; flex: none;
  display: inline-grid; place-items: center;
  background: var(--side-pill); color: var(--on-side-pill);
  border-radius: var(--r-brand);
}
.brand__mark .ico { width: 22px; height: 22px; }
.brand__text { display: flex; flex-direction: column; min-width: 0; }
.brand__name {
  font-size: 21px; font-weight: 700; line-height: 1.05;
  letter-spacing: -.02em; color: var(--on-side);
}
.brand__tag { font-size: var(--fs-xs); color: var(--side-dim); }

/* Collapse 按鈕：32px、10px 圓角；hover 即時變色（冇 transition） */
.side__collapse {
  width: 32px; height: 32px; flex: none;
  display: inline-grid; place-items: center;
  background: #ffffff0d; color: var(--side-dim);
  border: 1px solid #ffffff29; border-radius: 10px;
  cursor: pointer;
}
.side__collapse:hover { background: #ffffff1a; }
.side__collapse .ico { width: 16px; height: 16px; }

/* Nav：JS render（router 同步 active），scrollbar 收埋 */
.nav { flex: 1 1 auto; min-height: 0; overflow-y: auto; scrollbar-width: none; }
.nav::-webkit-scrollbar { display: none; }

.nav__track { position: relative; }

/* 活躍指示 pill：--side-pill 底，位置由 JS 用 clip-path FLIP（見 scripts/main.js）。
   首次 render / resize 直接 set 唔郁；hover 唔會令佢郁。 */
.nav__pill {
  position: absolute; inset: 0; z-index: 0;
  background: var(--side-pill);
  border-radius: var(--r-nav-item);
  clip-path: inset(100% 0 100% round var(--r-nav-item));
  opacity: 0;
  pointer-events: none;
}

.nav__group {
  margin: 0;
  padding: 16px 12px 6px;
  font-size: var(--fs-2xs); font-weight: 700;
  letter-spacing: .08em; text-transform: uppercase;
  color: var(--side-dim);
}
.nav__sep { height: 1px; margin: 0 12px 4px; background: #ffffff14; }

.nav__item {
  position: relative; z-index: 1;
  display: flex; align-items: center; gap: 12px;
  height: 44px; padding: 0 12px;
  border-radius: var(--r-nav-item);
  font-size: var(--fs-md); font-weight: 600;
  color: var(--side-dim);
}
.nav__item:hover { color: var(--on-side); background: var(--side-hover); }
.nav__item[aria-current="page"] { color: var(--on-side-pill); background: none; }
.nav__label { flex: 1 1 auto; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.nav__item .ico { width: 19px; height: 19px; }
.nav__chev { margin-left: -2px; width: 15px; height: 15px; opacity: .7; }
.nav__item.is-open .nav__chev { transform: rotate(180deg); }

/* 計數膠囊：22×20px；active 時變 --side 底（喺米杏 pill 上面） */
.nav__count {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 22px; height: 20px; padding: 0 6px;
  border-radius: 7px;
  background: #ffffff1a;
  font-size: var(--fs-2xs); font-weight: 700;
  color: var(--on-side);
}
.nav__item[aria-current="page"] .nav__count { background: var(--side); color: var(--side-pill); }

/* 子選單：43px 縮入 + 左邊 1px 豎線；active 圓點 9px + 3px ring */
.nav__sub { display: none; position: relative; padding: 2px 0 8px; }
.nav__item.is-open + .nav__sub { display: block; }
.nav__sub::before {
  content: ""; position: absolute;
  left: 21px; top: 2px; bottom: 10px; width: 1px;
  background: #ffffff24;
}
.nav__subitem {
  position: relative; z-index: 1;
  display: flex; align-items: center;
  height: 34px; margin-left: 43px; padding: 0 12px;
  border-radius: var(--r-nav-sub);
  font-size: var(--fs-sm); font-weight: 600;
  color: var(--side-dim);
}
.nav__subitem:hover { color: var(--on-side); background: var(--side-hover); }
/* [fix] active subitem 文字色照原版 bundle（index--oEMGL0U.css）係 --on-side；
   舊值 --on-side-pill（墨綠）喺深綠欄上令 "Stock" 隱形（inventory agent 修正，
   詳見 docs/build-log.md） */
.nav__subitem[aria-current="page"] { color: var(--on-side); background: none; }
.nav__subitem[aria-current="page"]::before {
  content: ""; position: absolute; left: -26px; top: 50%;
  width: 9px; height: 9px; margin-top: -4.5px;
  border-radius: 50%;
  background: var(--side-pill);
  box-shadow: 0 0 0 3px var(--side);
}

/* Foot：Settings / Support / 用戶卡 */
.side__foot { flex: none; border-top: 1px solid #ffffff17; padding-top: 10px; }
.side__foot .nav__item { width: 100%; }

.me {
  display: flex; align-items: center; gap: 10px;
  margin-top: 4px; padding: 8px;
  border-radius: 12px;
}
.me:hover { background: var(--side-hover); }
.me__text { display: flex; flex-direction: column; min-width: 0; }
.me__name { font-size: var(--fs-md); font-weight: 600; color: var(--on-side); }
.me__role { font-size: var(--fs-xs); color: var(--side-dim); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }

/* Avatar：gradient 圓 + 字母（唔用外連圖片） */
.avatar {
  display: inline-grid; place-items: center; flex: none;
  border-radius: 50%;
  background: var(--grad-avatar);
  color: #fff; font-weight: 700;
  user-select: none;
}
.avatar--36 { width: 36px; height: 36px; font-size: var(--fs-xs); }
.avatar--44 { width: 44px; height: 44px; font-size: var(--fs-sm); }

/* ==========================================================================
   Topbar：sticky、--bg 底、search pill + clock + New + theme + bell + avatar
   ========================================================================== */
.top {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 16px 0 14px;
  background: var(--bg);
}
.top__spacer { flex: 1 1 auto; }

/* Mobile brand（≤760px 先出現） */
.top__brand { display: none; }

/* Search pill：46px 高、23px 圓角、max 460px */
.search {
  flex: 0 1 460px; min-width: 0;
  display: flex; align-items: center; gap: 10px;
  height: 46px; padding: 0 10px 0 16px;
  background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--r-search);
  box-shadow: var(--shadow);
  color: var(--muted); font-size: var(--fs-md);
  cursor: pointer; text-align: left;
}
.search:hover { border-color: var(--line-2); }
.search .ico { width: 18px; height: 18px; }
.search > span { flex: 1 1 auto; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }

.kbd {
  padding: 4px 7px;
  background: var(--well); border-radius: 7px;
  font-family: inherit; font-size: var(--fs-2xs); font-weight: 700;
  color: var(--muted);
}

/* 時鐘（≤1279px 收） */
.top__clock { display: grid; justify-items: end; line-height: 1.25; }
.top__clock b { font-size: var(--fs-md); font-weight: 700; }
.top__clock small { font-size: var(--fs-xs); color: var(--muted); }

/* ibtn：44px 圓鈕 */
.ibtn {
  position: relative;
  width: 44px; height: 44px; flex: none;
  display: inline-grid; place-items: center;
  background: var(--surface);
  border: 1px solid var(--line); border-radius: 50%;
  box-shadow: var(--shadow);
  color: var(--text);
  cursor: pointer; padding: 0;
}
.ibtn:hover { border-color: var(--line-2); }
.ibtn__dot {
  position: absolute; top: 2px; right: 2px;
  min-width: 16px; height: 16px; padding: 0 4px;
  display: inline-grid; place-items: center;
  background: var(--bad); color: #fff;
  border-radius: 8px;
  box-shadow: 0 0 0 2px var(--bg);
  font-size: 10px; font-weight: 700;
}

/* Theme toggle：light 顯示月亮、dark 顯示太陽 */
.top__theme .ico-sun { display: none; }
[data-theme="dark"] .top__theme .ico-sun { display: block; }
[data-theme="dark"] .top__theme .ico-moon { display: none; }

.top__me {
  width: 48px; height: 48px; flex: none;
  padding: 0; border: 2px solid var(--surface);
  border-radius: 50%; background: none;
  box-shadow: var(--shadow);
  cursor: pointer; line-height: 0;
}
.top__me .avatar { width: 44px; height: 44px; font-size: var(--fs-sm); }

/* ==========================================================================
   頁首（.head）：crumbs + 大標題 + sub + actions（router render）
   ========================================================================== */
.head {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 16px 24px;
  margin: 8px 0 22px;
}
.head__text { min-width: 0; }

.crumbs {
  display: flex; align-items: center; gap: 6px;
  margin-bottom: 10px;
  font-size: var(--fs-sm); color: var(--muted);
}
.crumbs .ico { width: 13px; height: 13px; opacity: .6; }
.crumbs a:hover { text-decoration: underline; text-underline-offset: 3px; }

.head__title {
  margin: 0;
  font-size: var(--fs-title);
  font-weight: 600; line-height: 1.04; letter-spacing: -.025em;
  outline: none;
}
/* 逐字結構：.word 做遮罩（overflow hidden），.ch 係逐字 span 由動畫升起 */
.head__title .word {
  display: inline-block; overflow: hidden;
  padding-bottom: .14em; margin-bottom: -.14em;
  vertical-align: top;
}
.head__title .ch { display: inline-block; will-change: transform; }

.head__sub { margin: 8px 0 0; max-width: 64ch; color: var(--muted); }
.head__actions { display: flex; align-items: center; gap: 10px; flex: none; }

/* Tabs（共用底線組件；tabs__bar 由 JS FLIP，round 2px） */
.tabs { margin: -6px 0 22px; border-bottom: 1px solid var(--line); }
.tabs__track { position: relative; display: flex; gap: 4px; }
.tab {
  display: inline-flex; align-items: center; gap: 4px;
  height: 42px; padding: 0 14px;
  font-size: var(--fs-base); font-weight: 600;
  color: var(--muted);
}
.tab:hover, .tab[aria-selected="true"] { color: var(--text); }
.tab b {
  padding: 2px 6px;
  background: var(--well); border-radius: 9px;
  font-size: var(--fs-2xs); font-weight: 700;
}
.tabs__bar {
  position: absolute; left: 0; right: 0; bottom: -1px; height: 2px;
  background: linear-gradient(to top, var(--text) 2px, transparent 2px);
  border-radius: 2px;
  opacity: 0;
  pointer-events: none;
}

/* ==========================================================================
   基礎 primitives（shell 級共用：btn / card / kpi / meter / pill / link）
   頁面 agent 可以直接用，唔使重新定義。
   ========================================================================== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 42px; padding: 0 16px;
  background: var(--surface);
  border: 1px solid var(--line-2); border-radius: var(--r-btn);
  font-size: var(--fs-md); font-weight: 600;
  color: var(--text);
  cursor: pointer;
}
.btn .ico { width: 16px; height: 16px; }
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn--primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn--primary:hover { background: color-mix(in srgb, var(--primary) 88%, var(--text)); }
.btn--ink { background: var(--text); border-color: var(--text); color: var(--bg); }
.btn--ink:hover { background: color-mix(in srgb, var(--text) 86%, var(--bg)); }
.btn--soft { background: var(--well); }
.btn--sm { height: 34px; padding: 0 12px; border-radius: var(--r-btn-sm); font-size: var(--fs-sm); }
.btn__count {
  padding: 2px 7px;
  background: var(--well); border-radius: 9px;
  font-size: var(--fs-2xs); font-weight: 700;
}
.btn--ink .btn__count { background: color-mix(in srgb, var(--bg) 16%, transparent); }

.link { color: inherit; }
.link:hover { text-decoration: underline; text-underline-offset: 3px; }

.muted { color: var(--muted); }

/* 卡：22px 圓角、--line 邊、--shadow、padding 20px */
.card {
  background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--r-card);
  box-shadow: var(--shadow);
  padding: 20px;
}
.card__title { margin: 0; font-size: var(--fs-lg); font-weight: 600; }

/* KPI 瓷磚 */
.kpi {
  position: relative;
  display: flex; flex-direction: column; gap: 4px;
  padding: 18px 20px;
  background: var(--surface);
  border-radius: var(--r-card);
  box-shadow: var(--shadow);
  color: var(--on-tile);
}
.kpi[data-tile="sage"] { background: var(--tile-sage); }
.kpi[data-tile="apricot"] { background: var(--tile-apricot); }
.kpi[data-tile="mist"] { background: var(--tile-mist); }
.kpi[data-tile="clay"] { background: var(--tile-clay); }
.kpi__label { display: flex; align-items: center; gap: 8px; font-size: var(--fs-sm); font-weight: 600; color: var(--tile-dim); }
.kpi__value { font-size: var(--fs-3xl); font-weight: 600; line-height: 1.15; letter-spacing: -.01em; }
.kpi__foot { font-size: var(--fs-xs); color: var(--tile-dim); }

/* Meter：6px 軌 + scaleX 填充（進場由 anim.js 驅動 --fill） */
.meter {
  display: block; height: 6px;
  background: var(--well); border-radius: var(--r-meter);
  overflow: hidden;
}
.meter > i {
  display: block; height: 100%;
  background: var(--primary); border-radius: var(--r-meter);
  transform-origin: 0 50%;
  transform: scaleX(var(--fill, 1));
}
.meter[data-tone="bad"] > i { background: var(--bad); }
.meter[data-tone="warn"] > i { background: var(--amber-fill); }

/* Tone pill */
.pill {
  display: inline-flex; align-items: center;
  height: 24px; padding: 0 8px;
  border-radius: var(--r-pill);
  font-size: var(--fs-xs); font-weight: 600;
  background: color-mix(in srgb, var(--info) var(--soft), transparent); color: var(--info);
}
.pill[data-tone="good"] { background: color-mix(in srgb, var(--good) var(--soft), transparent); color: var(--good); }
.pill[data-tone="warn"] { background: color-mix(in srgb, var(--warn) var(--soft), transparent); color: var(--warn); }
.pill[data-tone="bad"] { background: color-mix(in srgb, var(--bad) var(--soft), transparent); color: var(--bad); }

/* Stub（骨架 demo 用，頁面 agent 填完可刪） */
.stub { min-height: 140px; display: grid; place-content: center; gap: 6px; text-align: center; }
.stub h2 { margin: 0; font-size: var(--fs-lg); }
.stub p { margin: 0; }

/* ==========================================================================
   進場動畫（keyframes + class，由 scripts/anim.js 掛 class + inline delay）
   規格：design-tokens.md §5.1 Route 進場編排
   ========================================================================== */
@keyframes enter-char {           /* 標題逐字：yPercent 115 → 0 */
  from { transform: translateY(115%); }
  to { transform: translateY(0); }
}
.is-enter-ch { animation: enter-char .85s var(--ease-power4-out) both; }

@keyframes enter-head {           /* header 元素 / 明細行：y 10px → 0 */
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}
.is-enter-head { animation: enter-head .55s var(--ease-power3-out) both; }
.is-enter-file { animation: enter-head .55s var(--ease-power3-out) both; }

@keyframes enter-card {           /* 卡片：y34 + scale .985 → 0/1 */
  from { opacity: 0; transform: translateY(34px) scale(.985); }
  to { opacity: 1; transform: none; }
}
.is-enter-card { animation: enter-card .9s var(--ease-power3-out) both; }

@keyframes enter-pop {            /* 小元素：scale .6 → 1，back.out(3) */
  from { opacity: 0; transform: scale(.6); }
  to { opacity: 1; transform: scale(1); }
}
.is-enter-pop { animation: enter-pop .6s var(--ease-back) both; }

@keyframes enter-fill {           /* meter：scaleX 0 → var(--fill) */
  from { transform: scaleX(0); }
  to { transform: scaleX(var(--fill, 1)); }
}
.is-enter-fill { animation: enter-fill 1s var(--ease-power3-out) both; }

@media (prefers-reduced-motion: reduce) {
  .is-enter-ch, .is-enter-head, .is-enter-file,
  .is-enter-card, .is-enter-pop, .is-enter-fill { animation: none; }
}

/* ==========================================================================
   Responsive（shell 級最小處理；完整 mobile 版等後續 agent）
   ========================================================================== */
@media (max-width: 1279px) {
  .top__clock { display: none; }
}

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .side { display: none; }
  .top__brand {
    display: inline-flex; align-items: center; gap: 8px;
    font-weight: 700; font-size: 17px; color: var(--text);
  }
  .top__brand .brand__mark { width: 34px; height: 34px; }
  .search { flex: 0 0 44px; width: 44px; padding: 0; justify-content: center; border-radius: 50%; overflow: hidden; }
  .search > span, .search .kbd { display: none; }
  .head { flex-direction: column; align-items: stretch; }
}
