:root {
  --brand: #06c755;
  --brand-hover: #05b04b;
  --brand-subtle: #e6f9ee;
  --sidebar-bg: #0f172a;
  --sidebar-fg: #cbd5e1;
  --sidebar-muted: #64748b;
  --sidebar-active: rgba(6, 199, 85, 0.14);
  --surface: #ffffff;
  --page-bg: #f4f6fa;
  --border: #e5e7eb;
  --text-muted: #6b7280;
  --sidebar-width: 248px;
  --bs-font-sans-serif: "Noto Sans TC", system-ui, -apple-system, "Segoe UI", sans-serif;
}

body {
  background: var(--page-bg);
  color: #111827;
  font-family: var(--bs-font-sans-serif);
}

/* ---------- 按鈕 ---------- */
.btn-brand {
  --bs-btn-color: #fff;
  --bs-btn-bg: var(--brand);
  --bs-btn-border-color: var(--brand);
  --bs-btn-hover-color: #fff;
  --bs-btn-hover-bg: var(--brand-hover);
  --bs-btn-hover-border-color: var(--brand-hover);
  --bs-btn-active-color: #fff;
  --bs-btn-active-bg: var(--brand-hover);
  --bs-btn-active-border-color: var(--brand-hover);
  --bs-btn-focus-shadow-rgb: 6, 199, 85;
  --bs-btn-disabled-bg: var(--brand);
  --bs-btn-disabled-border-color: var(--brand);
  font-weight: 500;
}
.btn-icon { font-size: 1.4rem; line-height: 1; padding: .25rem .5rem; }
.bg-brand-subtle { background: var(--brand-subtle); color: #048a3b; }
.form-control:focus, .form-select:focus, .form-check-input:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 .2rem rgba(6, 199, 85, .2);
}
.form-check-input:checked { background-color: var(--brand); border-color: var(--brand); }

/* ---------- 外框 ---------- */
.app-shell { display: flex; min-height: 100vh; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.content { padding: 1.5rem; flex: 1; width: 100%; min-width: 0; }

.sidebar {
  background: var(--sidebar-bg);
  color: var(--sidebar-fg);
  display: flex;
  flex-direction: column;
}
@media (min-width: 992px) {
  .sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    position: sticky;
    top: 0;
    height: 100vh;
  }
  /* Bootstrap 在 lg 以上會把 offcanvas-lg 背景設為 transparent !important */
  .sidebar.offcanvas-lg { background-color: var(--sidebar-bg) !important; }
}
.sidebar.offcanvas-lg { --bs-offcanvas-width: var(--sidebar-width); --bs-offcanvas-bg: var(--sidebar-bg); }

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: 1.25rem 1.25rem 1rem;
  border-bottom: 1px solid rgba(255, 255, 255, .06);
}
.brand-mark {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; border-radius: 10px;
  background: var(--brand); color: #fff; font-size: 1.15rem; flex-shrink: 0;
}
.brand-mark-lg { width: 56px; height: 56px; border-radius: 14px; font-size: 1.6rem; }
.brand-title { display: block; font-weight: 700; color: #fff; line-height: 1.2; }
.brand-sub { display: block; font-size: .72rem; color: var(--sidebar-muted); letter-spacing: .04em; }

.sidebar-nav { padding: 1rem .75rem; display: flex; flex-direction: column; gap: .15rem; flex: 1; }
.nav-section {
  font-size: .7rem; text-transform: uppercase; letter-spacing: .08em;
  color: var(--sidebar-muted); padding: .9rem .75rem .35rem;
}
.nav-item-link {
  display: flex; align-items: center; gap: .7rem;
  padding: .6rem .75rem; border-radius: 8px;
  color: var(--sidebar-fg); text-decoration: none; font-weight: 500;
  transition: background-color .15s, color .15s;
}
.nav-item-link i { font-size: 1.05rem; width: 1.2rem; text-align: center; }
.nav-item-link:hover { background: rgba(255, 255, 255, .06); color: #fff; }
.nav-item-link.active { background: var(--sidebar-active); color: var(--brand); }
.nav-item-link:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }

.sidebar-footer { padding: 1rem 1.25rem 1.25rem; border-top: 1px solid rgba(255, 255, 255, .06); }
.user-chip { display: flex; align-items: center; gap: .6rem; margin-bottom: .75rem; color: #fff; font-weight: 500; }
.user-avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: #334155; color: #fff; display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; text-transform: uppercase; flex-shrink: 0;
}

.topbar {
  display: flex; align-items: center; gap: .75rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: .85rem 1.5rem;
  position: sticky; top: 0; z-index: 1020;
}
.page-title { font-size: 1.2rem; font-weight: 700; margin: 0; }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: .5rem; }

.card-surface {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

/* ---------- 工具列與統計卡 ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: .75rem; padding: .9rem 1rem; margin-bottom: 1rem; }
.toolbar-field { min-width: 240px; flex: 0 1 360px; }

.stat-card {
  display: flex; align-items: center; gap: .9rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 1rem 1.1rem; height: 100%;
}
.stat-icon {
  width: 44px; height: 44px; border-radius: 10px; display: inline-flex;
  align-items: center; justify-content: center; font-size: 1.25rem; flex-shrink: 0;
}
.stat-label { font-size: .8rem; color: var(--text-muted); }
.stat-value { font-size: 1.45rem; font-weight: 700; line-height: 1.25; font-variant-numeric: tabular-nums; }
.stat-value-sm { font-size: .95rem; font-weight: 500; }

.view-toggle .btn[aria-pressed="true"] { background: var(--brand); border-color: var(--brand); color: #fff; }

/* ---------- 對話檢視 ---------- */
.chat-panel { overflow: hidden; }
.chat-scroll {
  height: calc(100vh - 330px);
  min-height: 420px;
  overflow-y: auto;
  padding: .5rem 1.25rem 1.25rem;
  background: #eef2f7;
}
.chat-list { display: flex; flex-direction: column; gap: .85rem; }
.chat-date {
  align-self: center; font-size: .75rem; color: #475569;
  background: rgba(15, 23, 42, .08); border-radius: 999px; padding: .2rem .8rem; margin: .5rem 0;
}
.chat-system {
  align-self: center; font-size: .78rem; color: #64748b;
  background: rgba(255, 255, 255, .7); border-radius: 999px; padding: .2rem .8rem;
}
.chat-msg { display: flex; gap: .6rem; align-items: flex-start; width: 100%; }
.chat-msg > div { flex: 1; min-width: 0; }
.chat-avatar {
  width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0; object-fit: cover;
  display: inline-flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 700; font-size: .9rem;
}
.chat-meta { font-size: .78rem; color: #475569; margin-bottom: .2rem; }
.chat-meta .chat-name { font-weight: 600; color: #1e293b; margin-right: .4rem; }
.chat-bubble {
  background: #fff; border-radius: 4px 14px 14px 14px; padding: .5rem .8rem;
  white-space: pre-wrap; word-break: break-word; box-shadow: 0 1px 1px rgba(15, 23, 42, .06);
}
.chat-bubble.is-media { color: #64748b; font-style: italic; }
.chat-bubble.is-unsent { opacity: .65; }
.chat-badge { font-size: .68rem; vertical-align: middle; }

.empty-state { text-align: center; padding: 4rem 1rem; color: #475569; }
.empty-state > i { font-size: 2.5rem; color: #94a3b8; display: block; margin-bottom: .75rem; }

/* ---------- 資料表 ---------- */
#messages-table td.col-content { white-space: pre-wrap; word-break: break-word; }
#messages-table td.col-time { white-space: nowrap; font-variant-numeric: tabular-nums; color: #475569; }
.type-badge { font-weight: 500; }

/* ---------- 設定 ---------- */
.section-head { display: flex; align-items: center; gap: .8rem; margin-bottom: 1.5rem; }
.section-icon {
  width: 42px; height: 42px; border-radius: 10px; background: #f1f5f9; color: #334155;
  display: inline-flex; align-items: center; justify-content: center; font-size: 1.2rem;
}
.section-icon-line { background: var(--brand-subtle); color: var(--brand); }
.setup-steps { padding-left: 1.2rem; margin: 0; color: #374151; }
.setup-steps li { margin-bottom: .45rem; }

/* ---------- 登入 ---------- */
.login-page { background: var(--page-bg); }
.login-wrap { display: flex; min-height: 100vh; }
.login-hero {
  flex: 1; align-items: center; justify-content: center; padding: 3rem;
  background: radial-gradient(circle at 20% 20%, #13314f 0%, var(--sidebar-bg) 60%);
  color: #e2e8f0;
}
.login-hero h2 { font-weight: 700; margin: 1.5rem 0 .75rem; color: #fff; }
.login-hero p { color: #94a3b8; }
.hero-points { list-style: none; padding: 0; margin: 2rem 0 0; }
.hero-points li { margin-bottom: .7rem; }
.hero-points i { color: var(--brand); margin-right: .5rem; }
.login-panel { flex: 1 1 0; min-width: 0; display: flex; align-items: center; justify-content: center; padding: 2rem 1rem; }
.login-card {
  width: 100%; background: var(--surface);
  border: 1px solid var(--border); border-radius: 16px; padding: 2rem;
  box-shadow: 0 10px 30px rgba(15, 23, 42, .06);
}

@media (max-width: 575.98px) {
  .content { padding: 1rem; }
  .topbar { padding: .75rem 1rem; }
  .toolbar-field { min-width: 0; flex: 1 1 100%; }
  .chat-scroll { padding: .5rem .75rem 1rem; height: calc(100vh - 420px); }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; scroll-behavior: auto !important; }
}
