/* app.css — 牙科案例頁排版工具 主樣式 */
/* 視覺基調：白底 + 藍綠色系（#3b7d9f）醫療風 */

/* ── CSS Variables ─────────────────────────────────────────────────────────── */
:root {
  --primary: #3b7d9f;
  --primary-dark: #2d6284;
  --primary-light: #eaf4f9;
  --accent: #2aa0a0;
  --bg: #f4f7f9;
  --surface: #ffffff;
  --border: #d0dde5;
  --text: #1a2733;
  --text-muted: #6b7f8d;
  --error: #c0392b;
  --success: #27ae60;
  --sidebar-width: 200px;
  --radius: 6px;
  --shadow: 0 1px 4px rgba(0,0,0,0.08);
}

/* ── Reset ─────────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang TC', 'Microsoft JhengHei', sans-serif; font-size: 14px; color: var(--text); background: var(--bg); height: 100vh; overflow: hidden; }

/* ── Layout ────────────────────────────────────────────────────────────────── */
#app { display: flex; height: 100vh; }

#sidebar {
  width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
}

#main-content {
  flex: 1;
  overflow-y: auto;
  padding: 28px 32px;
}

/* ── Sidebar ───────────────────────────────────────────────────────────────── */
.sidebar-header { padding: 20px 16px 16px; border-bottom: 1px solid var(--border); }
.app-logo { display: flex; align-items: center; gap: 10px; }
.logo-icon { width: 32px; height: 32px; background: var(--primary); color: #fff; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 16px; flex-shrink: 0; }
.logo-text { font-weight: 600; font-size: 13px; color: var(--text); line-height: 1.3; }

.nav-list { list-style: none; padding: 8px 0; }
.nav-link {
  display: block;
  padding: 9px 16px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 13.5px;
  border-radius: 0;
  transition: background 0.12s, color 0.12s;
}
.nav-link:hover { background: var(--primary-light); color: var(--primary); }
.nav-link.active { background: var(--primary-light); color: var(--primary); font-weight: 600; border-left: 3px solid var(--primary); padding-left: 13px; }

.nav-bottom { margin-top: auto; border-top: 1px solid var(--border); }

/* ── Buttons ───────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 7px 18px; border-radius: var(--radius); border: 1px solid transparent;
  font-size: 13.5px; font-weight: 500; cursor: pointer; text-decoration: none;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
  line-height: 1.4;
}
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn-primary:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-secondary { background: var(--surface); color: var(--primary); border-color: var(--border); }
.btn-secondary:hover { background: var(--primary-light); border-color: var(--primary); }
.btn-secondary:disabled { opacity: 0.40; cursor: not-allowed; }
.btn-secondary:disabled:hover { background: var(--surface); border-color: var(--border); }
/* undo/redo 按鈕左側留一點空間，與匯出鈕分組 */
#ed-undo-btn { margin-left: 6px; }
.btn-lg { padding: 12px 28px; font-size: 15px; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-icon { background: none; border: none; cursor: pointer; font-size: 14px; color: var(--text-muted); padding: 2px 6px; border-radius: 3px; }
.btn-icon:hover { background: #fde8e8; color: var(--error); }

/* ── Typography ────────────────────────────────────────────────────────────── */
h2 { font-size: 20px; font-weight: 600; margin-bottom: 20px; color: var(--text); }
h3 { font-size: 15px; font-weight: 600; margin-bottom: 12px; color: var(--text); }
h4 { font-size: 13px; font-weight: 600; margin-bottom: 8px; color: var(--text-muted); }
.hint { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.loading-text { color: var(--text-muted); }
.empty-hint { color: var(--text-muted); padding: 40px 0; text-align: center; }
.error-text { color: var(--error); font-size: 13px; }
.hidden { display: none !important; }

/* ── Forms ─────────────────────────────────────────────────────────────────── */
.field-label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 500; color: var(--text); }
.field-label.required::after { content: ' *'; color: var(--error); }
input[type="text"], input[type="date"], input[type="password"], select, textarea {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 7px 10px;
  font-size: 13.5px; color: var(--text); background: var(--surface);
  transition: border-color 0.12s;
  font-family: inherit;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); }
.search-input { width: 280px; padding: 8px 12px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 16px; }
.form-group { margin-bottom: 16px; }
.treatment-select-wrap { display: flex; gap: 8px; align-items: center; }
.treatment-select-wrap select { flex: 1; }

/* ── Welcome View ──────────────────────────────────────────────────────────── */
.welcome-view { display: flex; align-items: center; justify-content: center; min-height: 60vh; }
.welcome-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 48px 56px; max-width: 520px; text-align: center; box-shadow: var(--shadow); }
.welcome-card h1 { font-size: 22px; margin-bottom: 16px; color: var(--primary); }
.welcome-card p { margin-bottom: 12px; line-height: 1.7; color: var(--text-muted); font-size: 14px; }
.welcome-card .btn-lg { margin-top: 8px; }
.welcome-card hr { margin: 24px 0; border: none; border-top: 1px solid var(--border); }
.welcome-requirements p { font-size: 12px; }

/* ── Drop Zone ─────────────────────────────────────────────────────────────── */
.drop-zone { border: 2px dashed var(--border); border-radius: 12px; padding: 48px 32px; text-align: center; background: var(--surface); transition: border-color 0.15s, background 0.15s; cursor: pointer; }
.drop-zone.drag-over { border-color: var(--primary); background: var(--primary-light); }
.drop-icon { font-size: 36px; color: var(--border); margin-bottom: 12px; font-weight: 200; }
.import-buttons { display: flex; gap: 12px; justify-content: center; margin-top: 16px; }
.import-progress { margin-top: 20px; }
.progress-bar-wrap { background: #e8ecef; border-radius: 4px; height: 6px; overflow: hidden; margin-bottom: 8px; }
.progress-bar { height: 6px; background: var(--primary); border-radius: 4px; transition: width 0.2s; width: 0; }
.import-result { margin-top: 20px; }
.result-summary { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 20px; }
.result-summary ul { margin-top: 8px; padding-left: 20px; }
.result-summary li { font-size: 13px; margin-bottom: 4px; }

/* ── Photo Library ─────────────────────────────────────────────────────────── */
.library-view h2 { margin-bottom: 20px; }
.date-group { margin-bottom: 28px; }
.date-group h3 { font-size: 14px; font-weight: 600; color: var(--primary); margin-bottom: 12px; }
.photo-count { font-weight: 400; color: var(--text-muted); }
.thumb-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.thumb-item { cursor: pointer; border-radius: var(--radius); overflow: hidden; background: var(--surface); border: 1px solid var(--border); transition: transform 0.12s, box-shadow 0.12s; }
.thumb-item:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.12); }
.thumb-item img { width: 100%; aspect-ratio: 4/3; object-fit: cover; display: block; }
.thumb-name { display: block; padding: 4px 6px; font-size: 10px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Lightbox */
.lightbox-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.85); display: flex; align-items: center; justify-content: center; z-index: 9999; cursor: pointer; }
.lightbox-inner { max-width: 90vw; max-height: 90vh; text-align: center; }
#lightbox-img { max-width: 100%; max-height: 85vh; object-fit: contain; border-radius: 4px; }
#lightbox-name { color: rgba(255,255,255,0.7); font-size: 12px; margin-top: 8px; }

/* ── Case Library ──────────────────────────────────────────────────────────── */
.case-search-row { margin-bottom: 20px; }
.treatment-group { margin-bottom: 28px; }
.case-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.case-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; cursor: pointer; transition: box-shadow 0.12s, border-color 0.12s; }
.case-card:hover { box-shadow: var(--shadow); border-color: var(--primary); }
.case-card-header { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.patient-name { font-weight: 600; font-size: 14px; }
.chart-no { font-size: 12px; color: var(--text-muted); }
.case-card-meta { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; font-size: 12px; color: var(--text-muted); }
.stage-badge, .template-badge { background: var(--primary-light); color: var(--primary); border-radius: 3px; padding: 1px 6px; font-size: 11px; }
.case-note { margin-top: 8px; font-size: 12px; color: var(--text-muted); border-top: 1px solid var(--border); padding-top: 6px; }

/* ── Case Detail ───────────────────────────────────────────────────────────── */
.case-detail-view h2 small { font-weight: 400; font-size: 14px; color: var(--text-muted); margin-left: 8px; }
.case-info-grid { display: grid; grid-template-columns: 100px 1fr; gap: 6px 12px; margin: 20px 0; }
.case-info-grid dt { font-weight: 600; color: var(--text-muted); font-size: 12px; }
.case-info-grid dd { font-size: 13px; }
.editor-placeholder { margin-top: 24px; }
.placeholder-box { border: 2px dashed var(--border); border-radius: 8px; padding: 40px; text-align: center; color: var(--text-muted); }
.placeholder-box p { margin-bottom: 4px; }
.back-btn { margin-bottom: 16px; }

/* ── New Case Wizard ───────────────────────────────────────────────────────── */
.new-case-view { max-width: 720px; }
.wizard-steps { display: flex; align-items: center; gap: 8px; margin-bottom: 24px; }
.step { font-size: 13px; color: var(--text-muted); padding: 4px 12px; border-radius: 20px; border: 1px solid var(--border); }
.step.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.step-sep { color: var(--border); font-size: 16px; }
.case-form { display: flex; flex-direction: column; gap: 0; }
.wizard-nav { margin-top: 24px; display: flex; gap: 12px; }

/* Tooth Chart（十字象限盤） */
.tooth-chart { display: flex; flex-direction: column; gap: 12px; margin-top: 8px; padding: 14px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); max-width: 720px; }
.tooth-region-row { display: flex; align-items: center; gap: 6px; }
.region-gap { width: 28px; flex-shrink: 0; }
.tooth-cross { position: relative; display: grid; grid-template-columns: repeat(9, 1fr) 20px repeat(9, 1fr); grid-template-rows: 30px 30px 12px 30px 30px; gap: 4px; align-items: center; justify-items: center; }
.tooth-midline-v { grid-row: 1 / 6; grid-column: 10; width: 2px; height: 100%; background: #1c2b3a; justify-self: center; }
.tooth-midline-h { grid-row: 3; grid-column: 1 / 20; height: 2px; width: 100%; background: #1c2b3a; align-self: center; }
.tooth-btn { width: 100%; min-width: 26px; height: 28px; border: 1px solid transparent; border-radius: 5px; background: #eef1f6; cursor: pointer; font-size: 13px; font-weight: 500; color: var(--text-muted); transition: background 0.1s, color 0.1s, border-color 0.1s; }
.tooth-btn.region { width: auto; padding: 0 12px; }
.tooth-btn:hover { border-color: var(--primary); color: var(--primary); }
.tooth-btn.selected { background: var(--primary); color: #fff; border-color: var(--primary); }
.selected-teeth { font-size: 12px; color: var(--primary); margin-top: 6px; }

/* Template picker */
.template-select-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 14px; margin-bottom: 20px; }
.tpl-card { border: 2px solid var(--border); border-radius: 8px; padding: 12px; cursor: pointer; background: var(--surface); transition: border-color 0.12s; }
.tpl-card:hover { border-color: var(--primary); }
.tpl-card.selected { border-color: var(--primary); background: var(--primary-light); }
.tpl-card-info { margin-top: 8px; display: flex; flex-direction: column; gap: 2px; }
.tpl-card-info strong { font-size: 12px; }
.tpl-card-info span { font-size: 11px; color: var(--text-muted); }

/* Photo picker */
.pick-count { font-size: 12px; color: var(--primary); font-weight: 600; margin-bottom: 12px; }
.date-group.small h4 { font-size: 13px; }
.pick-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 8px; margin-bottom: 16px; }
.pick-item { border: 2px solid var(--border); border-radius: var(--radius); overflow: hidden; cursor: pointer; background: var(--surface); display: block; transition: border-color 0.12s; }
.pick-item:hover { border-color: var(--primary); }
.pick-item.selected { border-color: var(--primary); background: var(--primary-light); }
.pick-item img { width: 100%; aspect-ratio: 1; object-fit: cover; display: block; }
.pick-name { display: block; padding: 3px 4px; font-size: 9px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Settings ──────────────────────────────────────────────────────────────── */
.settings-view { max-width: 560px; }
.settings-section { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 20px 24px; margin-bottom: 16px; }
.settings-section h3 { font-size: 14px; margin-bottom: 14px; padding-bottom: 10px; border-bottom: 1px solid var(--border); }
.toggle-label { display: flex; align-items: center; gap: 8px; cursor: pointer; font-size: 13.5px; }
.toggle-label input { width: 16px; height: 16px; }
.privacy-note { margin-top: 8px; }
.treatments-list { list-style: none; display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; max-height: 200px; overflow-y: auto; }
.treatment-item { display: flex; align-items: center; justify-content: space-between; padding: 6px 10px; background: var(--bg); border-radius: 4px; border: 1px solid var(--border); }
.treatment-item-text { font-size: 13px; }
.add-treatment-row { display: flex; gap: 8px; }
.add-treatment-row input { flex: 1; }
.settings-actions { margin-top: 20px; display: flex; align-items: center; gap: 12px; }
.save-msg { font-size: 13px; color: var(--success); font-weight: 500; }

/* ── Importer ──────────────────────────────────────────────────────────────── */
.importer-view { max-width: 600px; }

/* ── Editor ─────────────────────────────────────────────────────────────────── */

/* 編輯器覆蓋整個 main-content，取消 main-content 的 padding */
.editor-root {
  display: flex;
  flex-direction: column;
  height: 100%;
  /* 負 margin 抵消 main-content 的 28px 32px padding */
  margin: -28px -32px;
}

/* 頂部標題列 */
.editor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 12px;
  min-height: 52px;
}
.editor-header-left { display: flex; align-items: center; gap: 12px; flex: 1; min-width: 0; }
.editor-header-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.editor-case-title { font-size: 13.5px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.editor-case-title strong { font-weight: 600; margin-right: 4px; }
.title-chartno { color: var(--text-muted); }
.title-sep { color: var(--border); margin: 0 4px; }
.editor-dirty-indicator { font-size: 12px; color: #e67e22; font-weight: 500; }

/* 三欄主體 */
.editor-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* 左：照片匣 */
.editor-tray {
  width: 160px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.tray-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.tray-count { background: var(--primary-light); color: var(--primary); border-radius: 10px; padding: 1px 7px; font-size: 11px; }
.tray-photos { flex: 1; overflow-y: auto; padding: 8px; display: flex; flex-direction: column; gap: 6px; }
.tray-item {
  border: 2px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: grab;
  background: var(--surface);
  transition: border-color 0.12s;
  user-select: none;
}
.tray-item:hover { border-color: var(--primary); }
.tray-item.dragging { opacity: 0.5; }
.tray-item.pending { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.tray-item-img-wrap { position: relative; overflow: hidden; }
.tray-item img { width: 100%; aspect-ratio: 4/3; object-fit: cover; display: block; pointer-events: none; }
.tray-item-name { display: block; padding: 3px 5px; font-size: 9px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tray-empty-hint { font-size: 11px; color: var(--text-muted); text-align: center; padding: 16px 8px; line-height: 1.6; }

/* 照片匣：方位調整鈕組（↔ ↕ ⟳） */
.tray-orient-btns {
  position: absolute;
  top: 3px;
  right: 3px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  z-index: 5;
}
.tray-mirror-btn {
  background: rgba(0,0,0,0.5);
  color: #fff;
  border: none;
  border-radius: 3px;
  font-size: 11px;
  padding: 1px 4px;
  cursor: pointer;
  line-height: 1.4;
  transition: background 0.12s;
  display: block;
}
.tray-mirror-btn:hover { background: rgba(0,0,0,0.75); }
.tray-mirror-btn.active { background: var(--primary); }
.tray-mirror-badge {
  position: absolute;
  bottom: 3px;
  left: 3px;
  background: rgba(43,128,43,0.88);
  color: #fff;
  font-size: 8px;
  padding: 1px 4px;
  border-radius: 2px;
  pointer-events: none;
}

/* 空格位口鏡提示小字 */
.slot-mirror-tray-hint {
  font-size: 7px;
  color: #e67e22;
  margin-top: 2px;
  text-align: center;
  line-height: 1.4;
  white-space: normal;
  padding: 0 4px;
}

/* 中：畫布區域 */
.editor-canvas-wrap {
  flex: 1;
  background: #dce3e9;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  padding: 16px;
}

/* 案例頁（A4 比例） */
.canvas-page {
  background: var(--surface);
  box-shadow: 0 2px 16px rgba(0,0,0,0.18);
  border-radius: 2px;
  display: flex;
  flex-direction: column;
  /* A4 橫式：297x210mm → 約 1.414:1，直式反之 */
  /* 用 vw/vh 自適應，上限設定在畫布可用區域 */
  width: min(calc(100% - 32px), 860px);
  aspect-ratio: 1.414 / 1;   /* 預設橫式，直式由 JS 覆蓋 */
}
.canvas-page.portrait { aspect-ratio: 1 / 1.414; }

/* 頁首 */
.canvas-page-header {
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.page-header-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: baseline;
  font-size: 11px;
}
.page-patient { font-weight: 700; font-size: 12px; }
.page-chartno { color: var(--text-muted); }
.page-date, .page-treatment { color: var(--text-muted); }
.page-teeth { color: var(--text-muted); }
.page-stage { background: var(--primary-light); color: var(--primary); border-radius: 3px; padding: 0 5px; font-size: 10px; }

/* CSS Grid 主體 */
.canvas-grid {
  flex: 1;
  min-height: 0;
  padding: 6px;
}

/* 頁尾 */
.canvas-page-footer {
  padding: 4px 10px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.page-footer-note { font-size: 10px; color: var(--text-muted); }

/* 格位 */
.grid-empty { background: #f0f2f4; border-radius: 2px; }

.grid-cell {
  background: #edf1f4;
  border: 2px dashed var(--border);
  border-radius: 3px;
  cursor: pointer;
  transition: border-color 0.12s;
}
.grid-cell:hover { border-color: var(--primary); }
.grid-cell.selected { border-color: var(--primary) !important; border-style: solid !important; box-shadow: 0 0 0 2px var(--primary-light); }
.grid-cell.drag-over { border-color: var(--accent); background: #e8f5f5; }
.grid-cell.has-photo { border-style: solid; border-color: transparent; }
.grid-cell.has-photo:hover { border-color: var(--primary); }
.grid-cell.has-photo.selected { border-color: var(--primary); }

/* 空格位佔位符 */
.slot-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4px;
  pointer-events: none;
}
.slot-display-name { font-size: 9px; color: var(--text-muted); text-align: center; line-height: 1.4; }
.slot-mirror-hint { font-size: 8px; color: #e67e22; margin-top: 2px; }

/* 格位中的照片 */
.slot-photo-img { /* 由 inline style 控制 */ }
.slot-badge-mirror {
  /* 純狀態指示，不可互動 */
  position: absolute;
  top: 3px;
  left: 3px;
  background: rgba(230,126,34,0.85);
  color: #fff;
  font-size: 9px;
  padding: 1px 5px;
  border-radius: 3px;
  pointer-events: none;
  z-index: 10;
}
.slot-swap-handle {
  /* 換格拖曳把手：拖到另一格＝互換、拖到待放區＝移回 */
  position: absolute;
  top: 3px;
  right: 3px;
  background: rgba(0,0,0,0.6);
  color: #fff;
  font-size: 12px;
  line-height: 1;
  padding: 4px 7px;
  border-radius: 4px;
  cursor: grab;
  z-index: 11;
  user-select: none;
}
.slot-swap-handle:hover { background: var(--primary); }
.slot-swap-handle:active { cursor: grabbing; }
.slot-photo-name {
  position: absolute;
  bottom: 2px;
  left: 3px;
  font-size: 8px;
  color: rgba(255,255,255,0.75);
  background: rgba(0,0,0,0.35);
  padding: 1px 4px;
  border-radius: 2px;
  pointer-events: none;
  white-space: nowrap;
  overflow: hidden;
  max-width: calc(100% - 6px);
  text-overflow: ellipsis;
}
.slot-badge-ai-pending {
  /* 中上，避開左上「已鏡射」與右上換格把手 */
  position: absolute;
  top: 3px;
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
  background: rgba(220,160,0,0.9);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 3px;
  pointer-events: all;
  cursor: pointer;
  z-index: 10;
  letter-spacing: 0.5px;
}
.slot-badge-ai-pending:hover {
  background: rgba(200,130,0,0.95);
}

/* 右：調整面板 */
.editor-panel {
  width: 220px;
  flex-shrink: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}
.panel-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 12px;
  line-height: 1.7;
}
#panel-content { display: flex; flex-direction: column; gap: 0; }

.panel-section {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.panel-section:last-child { border-bottom: none; }
.panel-section-title { font-size: 11px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 8px; }
.panel-slot-info { background: var(--primary-light); }
.panel-slot-label { font-size: 12px; font-weight: 600; color: var(--primary); }
.panel-mirror-default-hint { font-size: 10px; color: #e67e22; margin-top: 3px; }

.panel-row { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.panel-row:last-child { margin-bottom: 0; }
.panel-label { font-size: 11px; color: var(--text-muted); width: 56px; flex-shrink: 0; }
.panel-val { font-size: 11px; color: var(--text); width: 36px; text-align: right; flex-shrink: 0; }
.panel-slider { flex: 1; accent-color: var(--primary); }
.panel-adj-hint { font-size: 9px; color: var(--text-muted); flex: 1; text-align: center; }
.panel-adj-row { flex-wrap: wrap; }
.panel-adj-row .panel-label { width: 52px; }

.panel-rotate-btns { display: flex; gap: 6px; margin-bottom: 8px; }
.panel-rotate-btns .btn { flex: 1; font-size: 11px; padding: 4px 6px; }
.panel-rotate-fine { margin-top: 4px; }

.panel-mirror-btns { display: flex; gap: 6px; }
.panel-mirror-btns .btn { flex: 1; font-size: 11px; padding: 4px 6px; }
.panel-mirror-btns .btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.panel-wb-btns { display: flex; flex-direction: column; gap: 6px; }
.panel-wb-btns .btn { font-size: 11px; padding: 5px 8px; }

.panel-batch { display: flex; flex-direction: column; gap: 6px; }
.panel-batch .btn { font-size: 11px; padding: 5px 8px; }

.panel-empty-slot { padding: 16px 14px; }

/* 直式畫布修正 */
.canvas-page.portrait .canvas-grid { aspect-ratio: auto; }

/* ── Template Editor ────────────────────────────────────────────────────────── */

.tpl-editor-root {
  display: flex;
  flex-direction: column;
  height: 100%;
  margin: -28px -32px;
}

.tpl-editor-header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.tpl-editor-title {
  flex: 1;
  font-size: 16px;
  font-weight: 600;
  margin: 0;
}

.tpl-editor-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.tpl-editor-preview-wrap {
  flex: 1;
  background: #d8e3ea;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow: auto;
}

.te-preview-grid {
  max-width: 640px;
  max-height: 540px;
}

.tpl-editor-panel {
  width: 260px;
  flex-shrink: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
}

.tpl-editor-panel-inner {
  padding: 4px 0;
}

.te-section {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.te-section:last-child { border-bottom: none; }

.te-section-title {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 10px;
}

.te-input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 9px;
  font-size: 13px;
  color: var(--text);
  background: var(--surface);
  font-family: inherit;
}
.te-input:focus { outline: none; border-color: var(--primary); }
.te-input-sm { font-size: 12px; padding: 4px 8px; }

.te-select {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 5px 8px;
  font-size: 13px;
  color: var(--text);
  background: var(--surface);
  font-family: inherit;
  width: 100%;
}
.te-select:focus { outline: none; border-color: var(--primary); }
.te-select-sm { font-size: 12px; padding: 4px 6px; }

.te-field-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.te-field-row:last-child { margin-bottom: 0; }

.te-label {
  font-size: 12px;
  color: var(--text-muted);
  flex-shrink: 0;
  width: 72px;
}

.te-range-hint {
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.te-spinner-wrap {
  display: flex;
  align-items: center;
  gap: 4px;
}

.te-spinner-val {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  min-width: 20px;
  text-align: center;
}

.te-hint {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
}

.te-slot-info {
  font-size: 12px;
  font-weight: 600;
  color: var(--primary);
  background: var(--primary-light);
  padding: 4px 8px;
  border-radius: 4px;
  margin-bottom: 10px;
}

.te-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text);
  cursor: pointer;
}

.te-slot-delete {
  margin-top: 10px;
  width: 100%;
  color: var(--error);
  border-color: var(--error);
}
.te-slot-delete:hover { background: #fde8e8; }

/* 拖曳高亮：可放=綠框、不可放=紅框 */
.te-preview-empty.te-drop-ok,
.te-preview-cell.te-drop-ok {
  outline: 2px solid #27ae60;
  outline-offset: -2px;
}
.te-preview-empty.te-drop-reject,
.te-preview-cell.te-drop-reject {
  outline: 2px solid var(--error);
  outline-offset: -2px;
}

/* Template group label in new-case step 2 */
.tpl-group-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 0 0 10px;
  padding-left: 2px;
}
.template-select-grid + .tpl-group-label { margin-top: 18px; }

/* Settings — template management */
.s-tpl-group-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 8px;
}

.s-tpl-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.s-tpl-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--bg);
  border-radius: 4px;
  border: 1px solid var(--border);
}

.s-tpl-name {
  font-size: 13px;
  flex: 1;
}

.s-tpl-count {
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.s-tpl-badge {
  font-size: 10px;
  background: var(--primary-light);
  color: var(--primary);
  border-radius: 3px;
  padding: 1px 5px;
  flex-shrink: 0;
}

/* ── 換模板 Dialog ──────────────────────────────────────────────────────────── */

/* 頂部模板名稱標籤 */
.editor-tpl-name {
  font-size: 12px;
  color: var(--primary);
  background: var(--primary-light);
  border-radius: 4px;
  padding: 2px 8px;
  white-space: nowrap;
  flex-shrink: 0;
}

/* dialog 本體 */
.change-tpl-dialog {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0;
  width: min(780px, 92vw);
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0,0,0,0.22);
}
.change-tpl-dialog::backdrop {
  background: rgba(0,0,0,0.45);
}

.ctd-header {
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.ctd-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  margin: 0 0 4px;
}
.ctd-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin: 0;
}

.ctd-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px 20px 16px;
}

.ctd-group-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 12px 0 8px;
}
.ctd-group-label:first-child { margin-top: 0; }

.ctd-tpl-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 4px;
}

.ctd-tpl-card {
  border: 2px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  cursor: pointer;
  background: var(--surface);
  transition: border-color 0.12s, background 0.12s;
}
.ctd-tpl-card:hover { border-color: var(--primary); }
.ctd-tpl-card.selected { border-color: var(--primary); background: var(--primary-light); }
.ctd-tpl-card.current { border-color: var(--border); background: var(--bg); cursor: default; opacity: 0.7; }

.ctd-tpl-preview {
  width: 100%;
  margin-bottom: 6px;
}

.ctd-tpl-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.ctd-tpl-info strong { font-size: 11px; color: var(--text); }
.ctd-tpl-info span { font-size: 10px; color: var(--text-muted); }
.ctd-current-badge {
  font-size: 9px;
  background: var(--primary);
  color: #fff;
  border-radius: 3px;
  padding: 1px 5px;
  width: fit-content;
}

.ctd-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--surface);
}
.ctd-current-label { font-size: 12px; color: var(--text-muted); }
.ctd-current-label strong { color: var(--text); }
.ctd-footer-btns { display: flex; gap: 8px; }

/* ── 面板：移回待放區按鈕 ─────────────────────────────────────────────────── */
.btn-return-to-tray {
  width: 100%;
  font-weight: 600;
  background: #f5f0e8;
  border-color: #c8a96e;
  color: #7a5c1e;
}
.btn-return-to-tray:hover {
  background: #ede3d0;
}
