:root {
  --primary: #d4838a;
  --primary-dark: #b86670;
  --bg: #faf6f5;
  --sidebar-bg: #2d2426;
  --sidebar-text: #f4e9e8;
  --card-bg: #ffffff;
  --text: #3d2f31;
  --muted: #888;
  --border: #ead8d6;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* Login */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5d0d4 0%, #faf6f5 100%);
}
.login-box {
  background: white;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.08);
  width: 360px;
}
.login-box h1 {
  margin: 0 0 8px;
  font-size: 22px;
  color: var(--primary-dark);
}
.login-box .subtitle {
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 24px;
}

/* Layout with sidebar */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 220px;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  padding: 24px 0;
  flex-shrink: 0;
}
.sidebar .logo {
  padding: 0 24px 24px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  font-size: 16px;
  font-weight: 600;
}
.sidebar .nav-item {
  display: block;
  padding: 12px 24px;
  color: var(--sidebar-text);
  text-decoration: none;
  font-size: 14px;
  transition: background 0.2s;
}
.sidebar .nav-item:hover { background: rgba(255,255,255,0.05); }
.sidebar .nav-item.active {
  background: var(--primary);
  color: white;
}

/* === 群組分頁（出現在合併頁面 main-header 下方） === */
.page-tabs {
  display: flex;
  gap: 4px;
  background: #fff;
  border-radius: 12px;
  padding: 6px;
  margin-bottom: 18px;
  border: 1px solid var(--border);
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  flex-wrap: wrap;
}
.page-tab {
  padding: 9px 18px;
  border-radius: 8px;
  text-decoration: none;
  color: #666;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.15s;
  white-space: nowrap;
}
.page-tab:hover {
  background: #faf3f2;
  color: var(--primary-dark);
}
.page-tab.active {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
  color: #fff;
  box-shadow: 0 2px 6px rgba(212,131,138,0.3);
}
@media (max-width: 768px) {
  .page-tab { padding: 7px 12px; font-size: 13px; }
}

.main {
  flex: 1;
  padding: 32px;
  overflow-x: auto;
}
.main-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}
.main-header h1 { margin: 0; font-size: 24px; }

/* Cards */
.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 16px;
}
.card-title {
  font-size: 14px;
  color: var(--muted);
  margin: 0 0 8px;
}
.card-value {
  font-size: 28px;
  font-weight: 600;
  color: var(--primary-dark);
}

/* Stat grid */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

/* Form */
.field { margin-bottom: 16px; }
.field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
  letter-spacing: 0.02em;
}
.field input, .field textarea, .field select,
.modal input:not([type=checkbox]):not([type=radio]):not([type=color]):not([type=file]):not([type=range]):not([type=button]):not([type=submit]):not([type=reset]):not([type=image]),
.modal textarea,
.modal select,
input[type="text"], input[type="email"], input[type="tel"],
input[type="password"], input[type="number"], input[type="date"],
input[type="time"], input[type="datetime-local"], input[type="url"],
input[type="search"], textarea {
  width: 100%;
  padding: 10px 14px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  background: #fff;
  transition: border-color .15s, box-shadow .15s;
  outline: none;
}
.field textarea, textarea { min-height: 84px; resize: vertical; line-height: 1.5; }
.field input:focus, .field textarea:focus, .field select:focus,
input:focus:not([type="checkbox"]):not([type="radio"]):not([type="color"]):not([type="file"]),
textarea:focus, select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(212, 131, 138, 0.15);
}
.field input::placeholder, textarea::placeholder { color: rgba(0,0,0,.32); }

/* 自訂 select 下拉箭頭 */
.field select, select {
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path fill='none' stroke='%23999' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round' d='M1 1l5 5 5-5'/></svg>");
  background-repeat: no-repeat;
  background-position: right 14px center;
  padding-right: 38px;
  cursor: pointer;
}

/* 美化 checkbox */
input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border: 1.8px solid var(--border);
  border-radius: 5px;
  cursor: pointer;
  position: relative;
  vertical-align: middle;
  background: #fff;
  transition: all .15s;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  flex: none;
}
input[type="checkbox"]:hover { border-color: var(--primary); }
input[type="checkbox"]:checked {
  background: var(--primary);
  border-color: var(--primary);
}
input[type="checkbox"]:checked::after {
  content: "";
  position: absolute;
  left: 5px;
  top: 1px;
  width: 5px;
  height: 9px;
  border: solid white;
  border-width: 0 2.2px 2.2px 0;
  transform: rotate(45deg);
}
input[type="checkbox"]:focus-visible {
  box-shadow: 0 0 0 3px rgba(212, 131, 138, 0.3);
}

/* checkbox + label inline */
label > input[type="checkbox"] { margin-right: 6px; }

/* 美化 color picker */
input[type="color"] {
  width: 56px;
  height: 36px;
  padding: 4px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  background: #fff;
  vertical-align: middle;
  transition: border-color .15s;
}
input[type="color"]:hover { border-color: var(--primary); }
input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
input[type="color"]::-webkit-color-swatch { border: none; border-radius: 4px; }
input[type="color"]::-moz-color-swatch { border: none; border-radius: 4px; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 18px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  font-family: inherit;
  transition: all .15s;
  letter-spacing: 0.01em;
  line-height: 1.2;
  white-space: nowrap;
}
.btn-primary {
  background: var(--primary);
  color: white;
  box-shadow: 0 2px 6px rgba(212, 131, 138, 0.25);
}
.btn-primary:hover {
  background: var(--primary-dark);
  box-shadow: 0 4px 10px rgba(212, 131, 138, 0.35);
  transform: translateY(-1px);
}
.btn-primary:active { transform: translateY(0); }
.btn-secondary {
  background: white;
  color: var(--text);
  border: 1.5px solid var(--border);
}
.btn-secondary:hover {
  background: #faf6f5;
  border-color: var(--primary);
  color: var(--primary-dark);
}
.btn-danger {
  background: #d9534f;
  color: white;
  box-shadow: 0 2px 6px rgba(217, 83, 79, 0.25);
}
.btn-danger:hover {
  background: #b94440;
  box-shadow: 0 4px 10px rgba(217, 83, 79, 0.35);
  transform: translateY(-1px);
}
.btn-sm, .btn-tiny { padding: 5px 12px; font-size: 12px; border-radius: 6px; gap: 4px; }
.btn:disabled, .btn[disabled] {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none !important;
  box-shadow: none !important;
}

/* Table */
.table {
  width: 100%;
  background: white;
  border-collapse: collapse;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.table th, .table td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.table th {
  background: #f8f0ef;
  font-weight: 600;
  color: var(--text);
}
.table tr:last-child td { border-bottom: none; }
.table tr:hover { background: #fcf5f4; }

/* Badge */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  background: #e0e0e0;
  color: #555;
}
.badge.active { background: #d4eed4; color: #2d6d2d; }
.badge.inactive { background: #f0d4d4; color: #8a2929; }

/* ============================================================
   Modal — 統一精緻設計（所有頁面共用）
   ============================================================ */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(45, 36, 38, 0.55);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
  overflow-y: auto;
  animation: modalBackdropFade 0.2s ease;
}
@keyframes modalBackdropFade {
  from { opacity: 0; backdrop-filter: blur(0px); }
  to   { opacity: 1; backdrop-filter: blur(4px); }
}
.modal-backdrop.show { display: flex; }

.modal {
  background: white;
  padding: 0;
  border-radius: 14px;
  width: 100%;
  max-width: 560px;
  max-height: calc(100vh - 32px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow:
    0 20px 60px rgba(45, 36, 38, 0.25),
    0 4px 12px rgba(0, 0, 0, 0.08);
  animation: modalSlideIn 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
}
@keyframes modalSlideIn {
  from { opacity: 0; transform: translateY(-12px) scale(0.97); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* 頂部品牌色細條（從上方延伸）*/
.modal::before {
  content: "";
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 4px;
  background: linear-gradient(90deg, var(--primary) 0%, var(--primary-dark) 100%);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 22px 26px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-header h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: 0.01em;
}

.modal-close {
  background: none;
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background .15s, color .15s;
  padding: 0;
  flex-shrink: 0;
}
.modal-close:hover {
  background: rgba(45, 36, 38, 0.06);
  color: var(--text);
}

/* modal 內容區 — 自動 scroll，padding 一致 */
.modal > *:not(.modal-header):not(.modal-footer) {
  padding: 18px 26px;
  overflow-y: auto;
  flex: 1;
}
/* 第一個 padding 已含於 header；body 開頭如果緊接 header 就不重複 */
.modal-header + div,
.modal-header + form { padding-top: 18px; }

.modal-footer {
  margin-top: 0;
  padding: 16px 26px;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  border-top: 1px solid var(--border);
  background: #fafafa;
  border-radius: 0 0 14px 14px;
  flex-shrink: 0;
}

/* modal 內表單欄位 — 緊湊版本（各 modal 都受惠）*/
.modal .field { margin-bottom: 14px; }
.modal .field:last-child { margin-bottom: 0; }
.modal label:not(.toggle-switch) {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
  letter-spacing: 0.02em;
}
/* label 內含 checkbox / radio 時改回 inline-flex（讓 checkbox 跟文字同行）*/
.modal label:has(> input[type="checkbox"]),
.modal label:has(> input[type="radio"]) {
  display: inline-flex !important;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-weight: 500;
  font-size: 14px;
  margin-bottom: 0 !important;
}

/* modal 多列欄位 grid helper（HTML 直接用 inline grid 也 OK，這個是 utility）*/
.modal .row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.modal .row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
@media (max-width: 480px) {
  .modal .row2, .modal .row3 { grid-template-columns: 1fr; }
}

/* 必填星號 */
.required-star { color: #d9534f; margin-left: 2px; }

/* 既有 modal HTML 沒用 .field class（直接 <div><label>...</label></div>）— 自動間距 */
.modal > *:not(.modal-header):not(.modal-footer) > div + div { margin-top: 14px; }

/* color picker 樣式：保持 inline-block 大小固定 */
.modal input[type="color"] { display: inline-block; vertical-align: middle; }

/* Toast */
.toast {
  position: fixed;
  top: 20px;
  right: 20px;
  background: #2d2426;
  color: white;
  padding: 12px 20px;
  border-radius: 6px;
  z-index: 200;
  display: none;
}
.toast.show { display: block; animation: slideIn 0.3s; }
.toast.error { background: #d9534f; }
@keyframes slideIn {
  from { transform: translateX(100%); }
  to { transform: translateX(0); }
}

/* Quick reply chips */
.qr-list { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.qr-chip {
  background: #f0e0e0;
  padding: 6px 12px;
  border-radius: 16px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.qr-chip button {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--muted);
  font-size: 14px;
  line-height: 1;
}

/* Empty state */
.empty {
  text-align: center;
  padding: 40px;
  color: var(--muted);
}

/* Image preview */
.img-preview {
  max-width: 200px;
  max-height: 200px;
  border-radius: 6px;
  border: 1px solid var(--border);
  margin-top: 8px;
}

/* ===== 漢堡選單按鈕 + 遮罩（手機版）===== */
.hamburger {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 50;
  width: 40px;
  height: 40px;
  border: none;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  border-radius: 6px;
  cursor: pointer;
  font-size: 20px;
  align-items: center;
  justify-content: center;
}
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 40;
}
.sidebar-backdrop.show { display: block; }

/* 表格水平滾動容器 */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: 8px;
}

/* ===== Mobile breakpoint ===== */
@media (max-width: 768px) {
  /* Sidebar slide from left */
  .layout { display: block; }
  .hamburger { display: flex; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    z-index: 45;
    width: 240px;
    overflow-y: auto;
  }
  .sidebar.open { transform: translateX(0); }

  /* Main 騰出空間 + 縮小 padding */
  .main {
    padding: 56px 16px 16px;  /* 上方留 hamburger 空間 */
  }

  /* Header 縮排 + 換行 */
  .main-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  .main-header h1 {
    font-size: 18px;
  }

  /* 卡片 */
  .card { padding: 14px; }
  .card-value { font-size: 22px; }
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
  }

  /* 表格：強制水平捲動 */
  .table { font-size: 13px; min-width: 600px; }
  .table th, .table td { padding: 8px 10px; }

  /* Modal 全寬 */
  .modal {
    width: 100%;
    max-height: 95vh;
    border-radius: 8px;
    margin: 8px;
  }
  .modal-wide { width: 100%; max-width: none; }

  /* edit-grid 改單欄 */
  .edit-grid { grid-template-columns: 1fr !important; gap: 16px; }

  /* 三欄表單變單欄 */
  .field input, .field textarea, .field select { font-size: 16px; } /* 避免 iOS 自動 zoom */

  /* Login box 縮 */
  .login-box {
    width: 100%;
    margin: 16px;
    padding: 28px 20px;
  }

  /* Buttons */
  .btn { padding: 10px 16px; font-size: 14px; }
  .btn-sm { padding: 6px 10px; font-size: 12px; }

  /* Toast 全寬置中 */
  .toast {
    top: 16px;
    left: 16px;
    right: 16px;
    text-align: center;
  }

  /* RichMenu 預覽圖縮放 */
  .rm-grid { grid-template-columns: 1fr !important; }
}

/* 平板 — 中間尺寸略調整 */
@media (max-width: 1024px) and (min-width: 769px) {
  .stats-grid { grid-template-columns: repeat(3, 1fr); }
  .main { padding: 24px; }
}

/* ===== Wide modal with two-column layout ===== */
.modal-wide {
  max-width: 1100px;
  width: 95%;
}

.edit-grid {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 24px;
}

@media (max-width: 900px) {
  .edit-grid { grid-template-columns: 1fr; }
}

/* ===== Quick Reply editor ===== */
.qr-editor {
  background: #fafafa;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
}

.qr-cards {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.qr-card {
  background: white;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.qr-card-main {
  flex: 1;
  min-width: 0;
}

.qr-card-label {
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
}

.qr-card-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
  font-size: 11px;
}

.qr-card-value {
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 280px;
}

.qr-type-pill {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 10px;
  font-size: 10px;
  font-weight: 600;
}
.qr-type-message  { background: #d4eed4; color: #2d6d2d; }
.qr-type-postback { background: #d4dbf0; color: #2d3f80; }
.qr-type-uri      { background: #f0e0d4; color: #804020; }

.qr-card-actions {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}

.btn-icon {
  width: 28px;
  height: 28px;
  border: 1px solid var(--border);
  background: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn-icon:hover:not(:disabled) { background: #f5f5f5; }
.btn-icon:disabled { opacity: 0.3; cursor: not-allowed; }
.btn-icon-danger { color: #b94440; }
.btn-icon-danger:hover:not(:disabled) { background: #fee; }

/* Inline QR form */
.qr-form-card {
  background: white;
  border: 1px solid var(--primary);
  border-radius: 6px;
  padding: 12px;
  margin-top: 8px;
}
.qr-form-label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 4px;
}
.qr-form-card input, .qr-form-card select {
  width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 13px;
  font-family: inherit;
}

/* ===== LINE phone preview ===== */
.preview-label {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 8px;
  text-align: center;
}

.phone-mockup {
  background: #7a90b0;
  border-radius: 24px;
  padding: 12px 8px 18px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
}

.phone-header {
  background: white;
  border-radius: 6px;
  padding: 8px 14px;
  font-weight: 600;
  font-size: 13px;
  color: #333;
  margin-bottom: 8px;
}

.phone-body {
  background: #8da4c1;
  min-height: 360px;
  border-radius: 4px;
  padding: 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.bubble {
  padding: 10px 12px;
  border-radius: 12px;
  max-width: 78%;
  font-size: 13px;
  line-height: 1.5;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.bot-bubble {
  background: white;
  align-self: flex-start;
  color: #333;
}

.bubble-image {
  max-width: 100%;
  border-radius: 6px;
  display: block;
}

.qr-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
  padding: 0 4px;
}

.qr-chip-preview {
  background: white;
  color: #2c5282;
  border: 1px solid #cbd5e0;
  border-radius: 16px;
  padding: 5px 12px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ===== Phone RWD hardening (≤600px, appended last) — desktop ≥601 untouched ===== */
@media (max-width: 600px) {
  html, body { overflow-x: hidden; max-width: 100%; }
  .main { overflow-x: hidden; }
  /* 統計卡片:2 欄緊湊磚塊 */
  .stats-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 10px; }
  .stats-grid .card { padding: 12px 14px; }
  .stats-grid .card-value { font-size: 20px; }
  /* 卡片內任何元素都不得超出卡片;長字 / 網址換行 */
  .card { min-width: 0; overflow-wrap: anywhere; word-break: break-word; }
  .card * { max-width: 100% !important; }
  .card [style*="min-width"] { min-width: 0 !important; }
  /* 卡片內的橫向 flex 列(標題+狀態、數字+進度條)在窄螢幕換行 */
  .card > div[style*="flex"] { flex-wrap: wrap !important; }
  img, canvas, video { max-width: 100%; height: auto; }
  /* 升級教學表格改成可獨立左右捲,不撐破卡片 */
  #quota-card table { display: block; overflow-x: auto; white-space: nowrap; max-width: 100%; }
  #quota-card summary { white-space: normal; }
}

