: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 {
  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: 14px; }
.field label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 4px;
}
.field input, .field textarea, .field select {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
}
.field textarea { min-height: 80px; resize: vertical; }
.field input:focus, .field textarea:focus, .field select:focus {
  outline: none;
  border-color: var(--primary);
}

/* Buttons */
.btn {
  display: inline-block;
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  text-decoration: none;
  font-family: inherit;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: #eee; color: var(--text); }
.btn-secondary:hover { background: #ddd; }
.btn-danger { background: #d9534f; color: white; }
.btn-danger:hover { background: #b94440; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* 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(0,0,0,0.5);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-backdrop.show { display: flex; }
.modal {
  background: white;
  padding: 24px;
  border-radius: 8px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}
.modal-header h2 { margin: 0; font-size: 18px; }
.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: var(--muted);
}
.modal-footer {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* 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;
}
