/* milkidea CI 色票（取自公司 logo：水滴深橘＋暖黃背景） */
:root {
  --primary: #de8f1f;         /* 主色：logo 水滴橘 */
  --primary-dark: #b9750d;    /* hover／深色字 */
  --primary-light: #fdf4e3;   /* 淺色底（chip、圖示底） */
  --primary-border: #f3d9a8;  /* 淺色框線 */
  --primary-hover-bg: #f8e9cb;
}

* { box-sizing: border-box; }
body {
  margin: 0; background: #f2f4f7; color: #1a1a1a;
  font-family: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  font-size: 14px;
}
.hidden { display: none !important; }

/* 登入 */
.login-wrap { display: flex; min-height: 100vh; align-items: center; justify-content: center; }
.login-box {
  background: #fff; padding: 36px 32px; border-radius: 12px; width: 320px;
  box-shadow: 0 8px 30px rgba(0,0,0,.08); display: flex; flex-direction: column; gap: 12px;
}
.login-box h1 { margin: 0 0 8px; font-size: 20px; color: var(--primary); text-align: center; }
.login-box button {
  background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600;
}
.login-box button:hover { background: var(--primary-dark); color: #fff; }

/* 版頭 */
header {
  display: flex; align-items: center; gap: 24px; background: var(--primary); color: #fff;
  padding: 0 20px; height: 52px; position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 700; letter-spacing: 1px; display: flex; align-items: center; gap: 8px; }
.brand img { width: 26px; height: 26px; object-fit: contain; }
.logo-mark { width: 76px; height: 76px; align-self: center; }
nav { display: flex; gap: 4px; flex: 1; }
nav button {
  background: none; border: none; color: rgba(255,255,255,.75); padding: 8px 14px;
  cursor: pointer; font-size: 14px; border-radius: 6px;
}
nav button.active, nav button:hover { background: rgba(255,255,255,.15); color: #fff; }
.userbox { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.menu-btn { display: none; color: #fff; font-size: 20px; padding: 4px 10px; }
/* z-index 必須低於 header(10)：抽屜是 header 的子元素，只能靠 header 的堆疊層級浮在遮罩上 */
#nav-backdrop { position: fixed; inset: 0; background: rgba(16, 24, 40, .45); z-index: 9; }

main { max-width: 1200px; margin: 0 auto; padding: 20px; }
.toolbar { display: flex; align-items: center; gap: 20px; margin-bottom: 12px; flex-wrap: wrap; }
.toolbar h2 { margin: 0; font-size: 18px; flex: none; }
.inline { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.hint { color: #667085; font-size: 12.5px; }
code { background: #f6ead2; padding: 1px 6px; border-radius: 4px; }

/* 元件 */
input, select, button, textarea {
  font: inherit; padding: 7px 10px; border: 1px solid #d0d5dd; border-radius: 6px; background: #fff;
}
button {
  cursor: pointer; background: #fff; color: #1a1a1a;
  -webkit-appearance: none; appearance: none;   /* iOS Safari 會用原生按鈕外觀蓋掉自訂樣式，導致文字消失 */
}
button:hover { border-color: var(--primary); color: var(--primary); }
button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.primary:hover { background: var(--primary-dark); color: #fff; }
button.ghost { background: transparent; border-color: transparent; color: inherit; }
button.danger { color: #b00020; }
button svg { pointer-events: none; vertical-align: -3px; }  /* 點擊事件一律落在按鈕本身，事件代理才收得到 data-* */
button:has(> svg) {   /* 純圖示按鈕（編輯/刪除）固定 1:1 正方形 */
  width: 36px; height: 36px; aspect-ratio: 1; padding: 0;
  display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.error { color: #b00020; font-size: 13px; min-height: 18px; }

.w-xs { width: 70px; }
.w-lg { width: 320px; max-width: 100%; }

.table-wrap { background: #fff; border-radius: 8px; overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; background: #fff; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid #eef0f3; }
th { background: #f8fafc; font-weight: 600; color: #475467; font-size: 13px; }
tr:last-child td { border-bottom: none; }

.badge { display: inline-block; padding: 2px 10px; border-radius: 99px; font-size: 12px; }
.dev-cmds { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.dev-cmds button { font-size: 13.5px; padding: 7px 12px; }
.badge.on { background: #e6f6ec; color: #067647; }
.badge.off { background: #fef3f2; color: #b00020; }

.panel {
  background: #fff; border-radius: 8px; padding: 14px 16px; margin-bottom: 14px;
  display: flex; gap: 14px; align-items: center; flex-wrap: wrap;
}
.panel label { display: flex; gap: 6px; align-items: center; color: #475467; font-size: 13px; }
.fieldrow { display: contents; }   /* 桌機：包裹層不影響原本排版；手機媒體查詢內改為並排列 */

/* 上傳拖曳區 */
.dropzone {
  border: 2px dashed #c3d0e0; border-radius: 10px; padding: 24px; text-align: center;
  color: #667085; cursor: pointer; background: #fff; margin-bottom: 10px;
  transition: border-color .15s, background .15s;
}
.dropzone b { color: var(--primary); }
.dropzone.over { border-color: var(--primary); background: var(--primary-light); color: var(--primary); }
.dz-mobile { display: none; }

/* 上傳進度：文字（第幾個／總數＋百分比）＋整體進度條 */
.dropzone.uploading { pointer-events: none; }
.dropzone.uploading .dz-desktop, .dropzone.uploading .dz-mobile { display: none; }
.dz-progress { max-width: 420px; margin: 0 auto; }
#dz-progress-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dz-bar { height: 6px; background: #e5eaf1; border-radius: 3px; margin-top: 8px; overflow: hidden; }
#dz-bar-fill { height: 100%; width: 0; background: var(--primary); transition: width .2s; }

/* 素材格 */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
.card { background: #fff; border-radius: 8px; overflow: hidden; box-shadow: 0 1px 4px rgba(0,0,0,.06); }
.card .thumb {
  height: 100px; background: #101828; color: #fff; display: flex;
  align-items: center; justify-content: center; font-size: 28px; overflow: hidden;
}
.card .thumb img { width: 100%; height: 100%; object-fit: cover; }
.card .meta { padding: 8px 10px; font-size: 12px; }
.card .meta .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .meta .row { display: flex; justify-content: space-between; align-items: center; color: #667085; margin-top: 4px; }

/* 節目編輯器 */
.editor { background: #fff; border-radius: 10px; padding: 16px; margin-top: 16px; box-shadow: 0 4px 20px rgba(0,0,0,.08); }
.editor h3 { margin: 0 0 10px; }
.editor-head { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.editor-head label { display: flex; gap: 6px; align-items: center; font-size: 13px; color: #475467; }
.editor-head .spacer { flex: 1; }
.editor-body { display: flex; gap: 16px; align-items: flex-start; }
#elem-list { flex: 1; min-width: 380px; display: flex; flex-direction: column; gap: 10px; }
.elem {
  border: 1px solid #e4e7ec; border-radius: 8px; padding: 10px 12px;
  display: flex; flex-direction: column; gap: 8px; background: #fcfcfd;
  transition: transform .15s;
}
.elem.dragging {
  transition: none; position: relative; z-index: 20;
  box-shadow: 0 10px 28px rgba(0,0,0,.25); background: #fff;
}
.drag-handle {
  cursor: grab; touch-action: none; user-select: none; -webkit-user-select: none;
  color: #98a2b3; font-size: 20px; line-height: 1; padding: 4px 10px 4px 2px;
}
.drag-handle:active { cursor: grabbing; color: var(--primary); }
.chip {
  background: var(--primary-light); border: 1px solid var(--primary-border); color: var(--primary);
  border-radius: 99px; padding: 5px 14px; font-size: 12.5px;
}
.chip:hover { background: var(--primary-hover-bg); }

/* 彈出式子選單（元素設定、裝置選擇共用） */
.sheet-modal .modal-box {
  background: #fff; width: min(520px, 100%);
}
.sheet-modal .modal-head {
  color: #1a1a1a; border-bottom: 1px solid #eef0f3; font-weight: 600;
}
.sheet-modal .modal-head span { flex: 1; padding: 0 10px; }
.sheet-body {
  padding: 16px; display: flex; flex-direction: column; gap: 14px;
  font-size: 13.5px; max-height: 60vh; overflow-y: auto;
}
.sheet-body label { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; color: #475467; }
.sheet-body input[type=number] { width: 90px; }
.sheet-body select[multiple] { width: 100%; }

/* 裝置選擇列 */
.dev-row {
  border: 1px solid #e4e7ec; border-radius: 8px; padding: 10px 12px;
  background: #fcfcfd; cursor: pointer;
}
.dev-row input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--primary); }
.dev-row .ml-name { font-size: 14px; color: #1a1a1a; }
.fgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.fgrid label { display: flex; gap: 8px; align-items: center; color: #475467; min-width: 0; }
.fgrid input { flex: 1; min-width: 0; width: auto !important; }
#elem-modal-back { color: var(--primary); font-weight: 600; }
.sheet-divider {
  border-top: 1px solid #eef0f3; padding-top: 10px; margin-top: 2px;
  color: #98a2b3; font-size: 12px; letter-spacing: 1px;
}

/* 素材清單（子選單第二層） */
.media-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.media-list li {
  display: flex; align-items: center; gap: 10px; border: 1px solid #e4e7ec;
  border-radius: 8px; padding: 6px 10px; background: #fcfcfd;
  transition: transform .15s;
}
.media-list li.dragging {
  transition: none; position: relative; z-index: 20;
  box-shadow: 0 8px 20px rgba(0,0,0,.25); background: #fff;
}
.ml-thumb {
  width: 56px; height: 36px; border-radius: 4px; background: #101828; color: #fff;
  display: flex; align-items: center; justify-content: center; overflow: hidden;
  flex: none; font-size: 16px;
}
.ml-thumb img { width: 100%; height: 100%; object-fit: cover; }
.ml-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* 素材挑選（子選單第三層，樣式比照素材庫） */
.sheet-body [data-pick-confirm] {
  position: sticky; top: 0; z-index: 5;      /* 置頂不隨素材網格捲動 */
  box-shadow: 0 4px 12px rgba(0, 0, 0, .12);
}
.pick-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }
.pick-grid .card { cursor: pointer; position: relative; }
.pick-grid .card:hover, .pick-grid .card:active { outline: 2px solid var(--primary-border); }
.pick-grid .card.picked { outline: 3px solid var(--primary); }
.pick-grid .card.picked::after {
  content: '✓'; position: absolute; top: 6px; right: 6px; width: 22px; height: 22px;
  background: var(--primary); color: #fff; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 700;
}
.type-thumb { font-size: 32px; background: var(--primary-light) !important; }
.elem .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 13px; }
.elem .row label { display: flex; gap: 4px; align-items: center; color: #475467; }
.elem input[type=number] { width: 62px; }
.elem .title { font-weight: 700; color: var(--primary); }
.preview-wrap { width: 420px; flex: none; }
#preview {
  position: relative; width: 100%; aspect-ratio: 16/9; background: #000;
  border-radius: 6px; overflow: hidden; outline: 1px solid #d0d5dd;
}
#preview.portrait { aspect-ratio: 9/16; width: 240px; margin: 0 auto; }
#preview.ultrawide { aspect-ratio: 32/9; }
.pv-elem {
  position: absolute; border: 1px dashed rgba(255,255,255,.6); color: #fff;
  font-size: 11px; display: flex; align-items: center; justify-content: center;
  background: rgba(222, 143, 31, .45); overflow: hidden; text-align: center;
}

/* 影片預覽 */
.modal {
  position: fixed; inset: 0; z-index: 100; background: rgba(16,24,40,.75);
  display: flex; align-items: center; justify-content: center; padding: 32px;
}
.modal-box {
  background: #101828; border-radius: 12px; overflow: hidden;
  width: min(960px, 100%); box-shadow: 0 12px 48px rgba(0,0,0,.5);
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; color: #fff; font-size: 14px;
}
.modal-head span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.modal-head button { color: #fff; font-size: 14px; }
.modal-box video { display: block; width: 100%; max-height: 70vh; background: #000; }
.card .thumb.playable { cursor: pointer; position: relative; }
.card .thumb.playable::after {
  content: '▶'; position: absolute; inset: 0; display: flex; align-items: center;
  justify-content: center; font-size: 22px; color: #fff;
  background: rgba(0,0,0,.25); opacity: 0; transition: opacity .15s;
}
.card .thumb.playable:hover::after { opacity: 1; }

#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #101828; color: #fff; padding: 10px 18px; border-radius: 8px;
  font-size: 13px; opacity: 0; pointer-events: none; transition: opacity .25s;
}
#toast.show { opacity: 1; }

/* ─── RWD：平板與手機 ─────────────────────────────── */

@media (max-width: 900px) {
  .editor-body { flex-direction: column; }
  #elem-list { min-width: 0; width: 100%; }
  /* 預覽移到最上方並釘住，下方元素清單獨立捲動 */
  .preview-wrap {
    width: 100%; order: -1;
    position: sticky; top: calc(var(--header-h, 52px) + 6px); z-index: 5;
    background: #fff; padding: 8px; border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0,0,0,.12);
  }
  .preview-wrap .hint { text-align: center; margin-top: 4px; }
  #preview { margin: 0 auto; }
  #preview:not(.portrait) { width: min(100%, calc(34vh * 16 / 9)); }
  #preview.portrait { width: min(160px, 40vw); }
}

@media (max-width: 640px) {
  /* 版頭：☰ 按鈕在左，導覽列改為左側抽屜選單 */
  header { padding: 0 10px; gap: 8px; }
  .brand { font-size: 15px; flex: 1; }
  .userbox { margin-left: auto; }
  #whoami { display: none; }
  .menu-btn { display: inline-flex; align-items: center; }
  nav {
    position: fixed; top: 0; left: 0; bottom: 0; width: min(250px, 72vw);
    background: var(--primary); flex-direction: column; align-items: stretch;
    padding: 64px 12px 12px; gap: 6px; z-index: 40;
    transform: translateX(-100%); transition: transform .22s ease;
    box-shadow: 4px 0 24px rgba(0, 0, 0, .25);
  }
  nav.open { transform: translateX(0); }
  nav button { text-align: left; font-size: 16px; padding: 12px 14px; width: 100%; }

  main { padding: 12px; }

  /* 觸控目標與 iOS 防自動縮放（字級需 ≥16px） */
  input, select, button, textarea { font-size: 16px; padding: 10px 12px; }
  .w-xs { width: 84px; }

  /* 工具列與表單直向堆疊 */
  .toolbar { flex-direction: column; align-items: stretch; gap: 10px; }
  .inline { width: 100%; }
  .inline input:not([type=file]), .inline select { flex: 1 1 140px; min-width: 0; }
  .inline input[type=file] { min-width: 0; flex: 1 1 100%; }
  .panel { flex-direction: column; align-items: stretch; gap: 10px; }
  .panel label { flex-wrap: wrap; }
  /* 小欄位兩兩並排，減少表單高度 */
  .panel .fieldrow { display: flex; gap: 12px; align-items: center; }
  .panel .fieldrow label { flex: 1; justify-content: space-between; }
  .panel .fieldrow .w-xs { flex: 1; width: auto; max-width: 120px; }
  .panel label > input:not([type=color]):not(.w-xs), .panel label > select { flex: 1; min-width: 0; }
  .w-lg { width: 100%; }

  /* 表格：容器內橫滑，欄位不擠壓；.fit 表格則收進螢幕寬 */
  .table-wrap:not(.fit) table { min-width: 560px; }
  .table-wrap:not(.fit) th, .table-wrap:not(.fit) td { white-space: nowrap; }
  .table-wrap.fit td { word-break: break-word; }
  .table-wrap.fit button { white-space: nowrap; }

  /* 上傳區改成按鈕樣式（手機沒有拖曳） */
  .dropzone {
    border: none; background: var(--primary); color: #fff; font-weight: 600;
    padding: 13px; border-radius: 8px;
  }
  .dz-desktop { display: none; }
  .dz-mobile { display: block; }
  .dz-bar { background: rgba(255,255,255,.35); }
  #dz-bar-fill { background: #fff; }

  /* 素材格與編輯器 */
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
  .editor { padding: 12px; }
  .editor-head { gap: 8px; }
  .editor-head label { flex: 1 1 45%; }
  .editor-head label > input:not([type=color]), .editor-head label > select { flex: 1; min-width: 0; }
  .elem input[type=number] { width: 74px; }

  .modal { padding: 8px; }
  .login-box { width: min(320px, 92vw); }

  /* 子選單改為底部彈出面板 */
  .sheet-modal { align-items: flex-end; padding: 0; }
  .sheet-modal .modal-box { width: 100%; border-radius: 16px 16px 0 0; }
  .sheet-body { padding-bottom: max(20px, env(safe-area-inset-bottom)); }

  /* 編輯節目時，儲存/關閉固定在畫面底部（App 動作列），免捲回頂端 */
  .editor:not(.hidden) #btn-prog-save,
  .editor:not(.hidden) #btn-prog-cancel {
    position: fixed; bottom: max(12px, env(safe-area-inset-bottom)); z-index: 30;
    box-shadow: 0 4px 16px rgba(0,0,0,.25); border-radius: 99px; padding: 12px 22px;
  }
  .editor:not(.hidden) #btn-prog-save { right: 104px; }
  .editor:not(.hidden) #btn-prog-cancel { right: 12px; background: #fff; color: #475467; border: 1px solid #d0d5dd; }
  #elem-list { padding-bottom: 64px; }   /* 避免最後一張卡被動作列蓋住 */
}

/* 多租戶：品牌管理員專屬元件（分店切換器、分店/帳號分頁） */
.admin-only { display: none; }
body.is-brand-admin .admin-only { display: revert; }
body.is-brand-admin nav button.admin-only { display: block; }
#store-switch {
  max-width: 160px; font-size: 13px; padding: 5px 8px;
  border-radius: 6px; border: 1px solid var(--primary-border); background: #fff; color: var(--primary-dark);
}
@media (max-width: 640px) { #store-switch { max-width: 120px; } }

/* 品牌專屬登入連結：已帶入的品牌代號欄（唯讀） */
#login-brand.prefilled { background: var(--primary-light); color: var(--primary-dark); cursor: not-allowed; font-weight: 600; }
