*, *::before, *::after { box-sizing: border-box; }

/* Quire Microsoft 365 網頁版風格共用外殼（亮色系、左側應用側邊欄＋手機底部導覽列） */
:root {
  --m365-bg: #f5f5f5;
  --m365-surface: #ffffff;
  --m365-surface-hover: #f0f0f0;
  --m365-surface-active: #e6e6e6;
  --m365-border: #e6e6e6;
  --m365-border-strong: #d1d1d1;
  --m365-text: #242424;
  --m365-text-muted: #616161;
  --m365-text-subtle: #8a8886;
  --m365-quire: #c43120;
  --m365-word: #185abd;
  --m365-word-soft: #ebf3fc;
  --m365-excel: #107c41;
  --m365-excel-soft: #e6f4ea;
  --m365-ppt: #c43e1c;
  --m365-ppt-soft: #fdf0ec;
  --m365-shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04), 0 0 2px rgba(0, 0, 0, 0.03);
  --m365-shadow-md: 0 2px 8px rgba(0, 0, 0, 0.06), 0 0 2px rgba(0, 0, 0, 0.04);
  --m365-radius: 8px;
  --m365-rail-w: 60px;
  --m365-mobile-bar-h: 56px;
  --m365-font: "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Microsoft JhengHei", "Noto Sans TC", sans-serif;
}

/* 外層工作區容器：桌面左側側邊欄 + 右側主工作區
   高度錨定採 dvh（動態視埠高）並保留 vh 退回線：真機瀏覽器網址列／工具列收合時
   vh（大型視埠）會比可視高大，導致滿高容器與底部列被網址列蓋住。 */
.m365-shell {
  display: flex;
  flex-direction: row;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--m365-bg);
  font-family: var(--m365-font);
  color: var(--m365-text);
}

.m365-shell.m365-scroll-page {
  width: 100%;
  max-width: 100vw;
  height: auto;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: clip;
  /* 必寫項（t_ff2e1c51 實測揪出的真缺陷）：基底 .m365-shell 是 overflow:hidden，
     此變體若只覆蓋 overflow-x，overflow-y 會繼承 hidden ⇒ 外殼 div 自己變成巢狀
     滾動容器（index.html 的 shell 是 div），sticky 的 scrollport 變成永不捲動的
     外殼本身——文件一捲，整條側欄跟著捲走（實錄 railTop=-400）。templates 的 shell
     是 body，overflow 傳播到 viewport 才倖免。顯式放行 overflow-y，滾動主權還給
     文件；clip＋visible 是合法組合（clip 非滾動值，visible 不會被強制轉為 auto）。 */
  overflow-y: visible;
}

/* 左側垂直應用切換欄 (App Rail)
   桌面（≥769px）五頁一致釘住左側滿高：先前只有 .m365-scroll-page 變體（index／templates）
   拿到 sticky，word／sheet／slides 三頁的 computed position 是 static——即用戶實測回報的
   「側邊欄沒有 fixed」。釘住語意於此改由基底規則統一給予，不再分頁面變體；
   ≤768px 由檔尾手機段覆蓋回底部固定列（hotfix 已上線行為，零退化）。 */
.m365-rail {
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
  width: var(--m365-rail-w);
  min-width: var(--m365-rail-w);
  background: var(--m365-surface);
  border-right: 1px solid var(--m365-border);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 6px;
  gap: 6px;
  z-index: 90;
  flex-shrink: 0;
  user-select: none;
}

.m365-rail-divider {
  width: 28px;
  height: 1px;
  background: var(--m365-border);
  margin: 4px 0;
}

.m365-rail-item {
  position: relative;
  width: 46px;
  height: 46px;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: var(--m365-text-muted);
  text-decoration: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  font-family: var(--m365-font);
  font-size: 10px;
  font-weight: 500;
  transition: background 0.14s ease, color 0.14s ease;
}

.m365-rail-item svg {
  width: 24px;
  height: 24px;
  flex-shrink: 0;
}

.m365-rail-item:hover:not(.is-disabled) {
  background: var(--m365-surface-hover);
  color: var(--m365-text);
}

.m365-rail-item.active {
  font-weight: 600;
}

.m365-rail-item.active::before {
  content: "";
  position: absolute;
  left: -6px;
  top: 10px;
  bottom: 10px;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--m365-quire);
}

.m365-rail-item.app-home.active {
  background: #fdf2f0;
  color: var(--m365-quire);
}

.m365-rail-item.app-word.active {
  background: var(--m365-word-soft);
  color: var(--m365-word);
}
.m365-rail-item.app-word.active::before { background: var(--m365-word); }

.m365-rail-item.app-excel.active {
  background: var(--m365-excel-soft);
  color: var(--m365-excel);
}
.m365-rail-item.app-excel.active::before { background: var(--m365-excel); }

.m365-rail-item.app-ppt.active {
  background: var(--m365-ppt-soft);
  color: var(--m365-ppt);
}
.m365-rail-item.app-ppt.active::before { background: var(--m365-ppt); }

.m365-rail-item.app-ppt.is-disabled {
  opacity: 0.52;
  cursor: not-allowed;
}

.m365-rail-soon {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: var(--m365-ppt);
  opacity: 0.7;
}

.m365-main {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.m365-shell.m365-scroll-page .m365-main {
  overflow: visible;
}

/* 手機版響應式（< 768px）：左側欄自動轉為底部固定導覽列 */
@media (max-width: 768px) {
  .m365-shell {
    flex-direction: column;
  }

  .m365-rail {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    width: 100%;
    min-width: 100%;
    /* 列高＝內容高 + 底部安全區：安全區（瀏海／手勢列）加在列高上而非吃進內容高，
       否則 34px 安全區會把 46px 項目壓成 14px（box-sizing: border-box 全域生效）。 */
    height: calc(var(--m365-mobile-bar-h) + env(safe-area-inset-bottom, 0px));
    flex-direction: row;
    justify-content: space-around;
    padding: 4px 8px calc(4px + env(safe-area-inset-bottom, 0px));
    border-right: none;
    border-top: 1px solid var(--m365-border);
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.06);
    gap: 0;
  }

  .m365-rail-divider {
    display: none;
  }

  .m365-rail-item {
    flex: 1;
    max-width: 84px;
    height: 46px;
    border-radius: 10px;
  }

  .m365-rail-item.active::before {
    left: 18px;
    right: 18px;
    top: -4px;
    bottom: auto;
    width: auto;
    height: 3px;
    border-radius: 0 0 3px 3px;
  }

  .m365-main {
    width: 100%;
    max-width: 100vw;
    /* 預留高度同樣要含安全區，否則列加高後內容被列蓋住最後一屏。 */
    padding-bottom: calc(var(--m365-mobile-bar-h) + 8px + env(safe-area-inset-bottom, 0px));
  }
}

/* 模板市場側邊欄項目（固定於左側側邊欄最下方） */
:root {
  --m365-tpl: #6b4eff;
  --m365-tpl-soft: #f2efff;
}
.m365-rail-spacer {
  flex: 1;
  min-height: 8px;
}
.m365-rail-item.app-templates.active {
  background: var(--m365-tpl-soft);
  color: var(--m365-tpl);
}
.m365-rail-item.app-templates.active::before {
  background: var(--m365-tpl);
}
@media (max-width: 768px) {
  .m365-rail-spacer {
    display: none;
  }
}

/* 零外部圖片微型模板預覽縮圖（三套件設計頁籤＋模板市場共用） */
.qtpl-mini {
  width: 100%;
  height: 100%;
  border-radius: 5px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: 6px;
  gap: 4px;
  border: 1px solid rgba(0, 0, 0, 0.08);
  box-sizing: border-box;
}
.qtpl-mini-bar {
  height: 4px;
  border-radius: 2px;
  width: 100%;
  flex-shrink: 0;
}
.qtpl-mini-doc-title {
  height: 6px;
  width: 68%;
  border-radius: 2px;
  opacity: 0.85;
}
.qtpl-mini-line {
  height: 3px;
  width: 100%;
  border-radius: 2px;
  background: rgba(15, 23, 42, 0.14);
}
.qtpl-mini-line.short { width: 48%; height: 4px; }
.qtpl-mini-line.med { width: 72%; }
.qtpl-mini-sheet-hdr {
  height: 7px;
  border-radius: 2px;
  width: 100%;
}
.qtpl-mini-sheet-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
}
.qtpl-mini-sheet-cell {
  border-radius: 1px;
  background: rgba(15, 23, 42, 0.07);
}
.qtpl-mini-slide-strip {
  height: 5px;
  width: 100%;
  border-radius: 2px;
}
.qtpl-mini-slide-title {
  font-size: 9.5px;
  font-weight: 700;
  line-height: 1.25;
  margin-top: auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.qtpl-mini-slide-sub {
  height: 3px;
  width: 44%;
  border-radius: 2px;
  opacity: 0.7;
  margin-bottom: auto;
}

/* 外部嵌入模式（?embed=1）：隱藏外殼側邊欄，僅呈現純編輯器 */
html.is-embedded .m365-rail,
html[data-quire-embed="1"] .m365-rail { display: none !important; }
