* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #0f0f1a;
  --bg-card: #1a1a2e;
  --bg-card2: #16213e;
  --accent: #e94560;
  --accent2: #0f3460;
  --text: #eee;
  --text-dim: #8892a4;
  --text-muted: #556;
  --green: #27ae60;
  --blue: #3498db;
  --orange: #f39c12;
  --purple: #9b59b6;
  --pink: #e94560;
  --radius: 12px;
  --sidebar-w: 220px;
}

body {
  font-family: 'Segoe UI', 'Microsoft JhengHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

.app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-card);
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  border-right: 1px solid #2a2a4a;
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  z-index: 10;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 32px;
  padding: 0 8px;
}

.logo-icon { font-size: 28px; }
.logo-text { font-size: 18px; font-weight: 700; color: var(--accent); }

.nav-links {
  list-style: none;
  flex: 1;
}

.nav-links li {
  padding: 12px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-dim);
  transition: all 0.2s;
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.nav-links li:hover { background: var(--bg-card2); color: var(--text); }
.nav-links li.active { background: var(--accent); color: #fff; font-weight: 600; }
.nav-icon { font-size: 16px; }

.sidebar-footer {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 16px;
  border-top: 1px solid #2a2a4a;
}

/* Main */
.content {
  flex: 1;
  margin-left: var(--sidebar-w);
  padding: 32px;
  max-width: 1200px;
  height: 100vh;
  overflow-y: auto;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 28px;
}

.page-header h1 {
  font-size: 24px;
  font-weight: 700;
}

/* Tabs */
.tab-content { display: none; }
.tab-content.active { display: block; }

/* KPI Cards */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 16px;
  margin-bottom: 28px;
}

.kpi-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 20px;
  border: 1px solid #2a2a4a;
  transition: transform 0.2s;
}

.kpi-card:hover { transform: translateY(-2px); }
.kpi-card.accent { border-left: 4px solid var(--accent); }
.kpi-card.highlight { border-left: 4px solid var(--green); }

.kpi-label {
  font-size: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

.kpi-value {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 4px;
}

.kpi-sub {
  font-size: 11px;
  color: var(--text-muted);
}

/* Charts */
.chart-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
  margin-bottom: 20px;
}

.chart-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 24px;
  border: 1px solid #2a2a4a;
}

.chart-card h3 {
  font-size: 15px;
  margin-bottom: 16px;
  color: var(--text-dim);
  font-weight: 600;
}

.chart-card.wide {
  grid-column: 1 / -1;
}

/* Top Posts */
.top-posts-list { display: flex; flex-direction: column; gap: 12px; }

.top-post-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 16px;
  background: var(--bg-card2);
  border-radius: 8px;
  transition: background 0.2s;
}

.top-post-item:hover { background: #1e2d4a; }

.top-post-rank {
  font-size: 20px;
  font-weight: 800;
  color: var(--accent);
  min-width: 30px;
}

.top-post-info { flex: 1; }

.top-post-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 500px;
}

.top-post-meta {
  font-size: 11px;
  color: var(--text-dim);
  display: flex;
  gap: 12px;
}

.top-post-stats {
  display: flex;
  gap: 16px;
  font-size: 13px;
  color: var(--text-dim);
}

.top-post-stats span { white-space: nowrap; }

/* Forms */
.input-form {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 28px;
  border: 1px solid #2a2a4a;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group.full-width {
  margin-bottom: 16px;
}

.form-group label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
}

.form-group small {
  font-size: 11px;
  color: var(--text-muted);
}

input, select, textarea {
  background: var(--bg);
  border: 1px solid #2a2a4a;
  border-radius: 8px;
  padding: 10px 14px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  transition: border-color 0.2s;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.form-actions {
  display: flex;
  gap: 12px;
  margin-top: 20px;
}

/* Buttons */
.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 10px 24px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}

.btn-primary:hover { opacity: 0.85; }

.btn-secondary {
  background: var(--bg-card2);
  color: var(--text-dim);
  border: 1px solid #2a2a4a;
  border-radius: 8px;
  padding: 10px 20px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-secondary:hover { background: #1e2d4a; color: var(--text); }

.btn-danger {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 10px 24px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-danger:hover { background: var(--accent); color: #fff; }

.btn-sm {
  padding: 4px 10px;
  font-size: 12px;
  border-radius: 6px;
  cursor: pointer;
  border: none;
}

.btn-edit { background: var(--blue); color: #fff; }
.btn-delete { background: #c0392b; color: #fff; }

/* Posts Table */
.posts-table-wrapper { overflow-x: auto; }

.posts-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg-card);
  border-radius: var(--radius);
  overflow: hidden;
}

.posts-table thead { background: var(--bg-card2); }

.posts-table th {
  padding: 12px 14px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  text-align: left;
  white-space: nowrap;
}

.posts-table td {
  padding: 12px 14px;
  font-size: 13px;
  border-bottom: 1px solid #1a1a2e;
  vertical-align: middle;
}

.posts-table tr:hover { background: #1e2d4a; }

.type-badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.type-長文觀點 { background: #1a3a5c; color: #5dade2; }
.type-案例故事 { background: #1a4a2e; color: #58d68d; }
.type-觀點短文 { background: #4a3a1a; color: #f7dc6f; }
.type-導流CTA { background: #3a1a4a; color: #bb8fce; }
.type-互動問答 { background: #4a1a2e; color: #f1948a; }
.type-串文 { background: #2a2a3e; color: #aab7c4; }
.type-純文字 { background: #2a2a3e; color: #aab7c4; }

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-dim);
}

.empty-state p:first-child { font-size: 18px; margin-bottom: 8px; }

/* Insights */
.insights-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
  gap: 20px;
}

.insight-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 24px;
  border: 1px solid #2a2a4a;
  border-left: 4px solid var(--accent);
}

.insight-card.positive { border-left-color: var(--green); }
.insight-card.warning { border-left-color: var(--orange); }
.insight-card.info { border-left-color: var(--blue); }

.insight-card h4 {
  font-size: 15px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.insight-card p {
  font-size: 13px;
  line-height: 1.7;
  color: var(--text-dim);
}

.insight-card .highlight-text {
  color: var(--accent);
  font-weight: 700;
}

/* Settings */
.settings-form {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 28px;
  border: 1px solid #2a2a4a;
  max-width: 500px;
}

.settings-form hr {
  border: none;
  border-top: 1px solid #2a2a4a;
  margin: 24px 0;
}

.settings-form h3 {
  color: var(--accent);
  font-size: 14px;
  margin-bottom: 12px;
}

/* Filter row */
.filter-row {
  display: flex;
  gap: 10px;
}

.filter-row select {
  min-width: 160px;
}

/* Date filter */
.date-filter select {
  min-width: 120px;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 30px;
  right: 30px;
  background: var(--green);
  color: #fff;
  padding: 14px 24px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  z-index: 1000;
  animation: slideIn 0.3s ease, fadeOut 0.3s ease 2.5s forwards;
}

@keyframes slideIn { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
@keyframes fadeOut { to { opacity: 0; transform: translateY(20px); } }

/* Responsive */
@media (max-width: 900px) {
  .sidebar { width: 60px; padding: 16px 8px; }
  .logo-text, .nav-links li span:not(.nav-icon), .sidebar-footer { display: none; }
  .nav-links li { justify-content: center; padding: 12px; }
  .content { margin-left: 60px; padding: 20px; }
  .chart-grid { grid-template-columns: 1fr; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
}

/* Warning Banners */
.warning-banner {
  background: var(--orange);
  color: #fff;
  padding: 12px 20px;
  border-radius: 8px;
  margin-bottom: 16px;
  font-size: 13px;
  line-height: 1.5;
}

.warning-banner.warning-red {
  background: var(--accent);
}

/* Viral Analysis Grid */
.viral-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
}

.viral-card {
  background: var(--bg-card2);
  border-radius: 8px;
  padding: 20px;
  border: 1px solid #2a2a4a;
}

.viral-card h4 {
  font-size: 14px;
  margin-bottom: 12px;
  color: var(--text);
}

.viral-card p {
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.6;
}

/* KPI Comparison */
.kpi-up { color: var(--green); font-weight: 700; }
.kpi-down { color: var(--accent); font-weight: 700; }

/* Heatmap */
.heatmap-table { width: 100%; border-collapse: collapse; font-size: 11px; }
.heatmap-table th { padding: 6px 4px; color: var(--text-muted); font-weight: 400; font-size: 10px; }
.heatmap-table td { padding: 8px 4px; text-align: center; }
.heatmap-day { color: var(--text-dim); font-weight: 600; white-space: nowrap; padding-right: 8px !important; text-align: right !important; }
.heatmap-cell { border-radius: 4px; color: #fff; font-weight: 600; min-width: 36px; cursor: default; transition: transform 0.15s; }
.heatmap-cell:hover { transform: scale(1.3); z-index: 2; position: relative; }

/* Health Check */
.health-score-wrapper {
  display: flex;
  align-items: center;
  gap: 32px;
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 32px;
  margin-bottom: 24px;
  border: 1px solid #2a2a4a;
}

.health-score {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  flex-shrink: 0;
}

.health-score .score-number {
  font-size: 42px;
  font-weight: 800;
  line-height: 1;
}

.health-score .score-label {
  font-size: 12px;
  margin-top: 4px;
  opacity: 0.8;
}

.health-breakdown {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
}

.health-metric {
  display: flex;
  align-items: center;
  gap: 10px;
}

.health-bar {
  flex: 1;
  height: 8px;
  background: #1a1a2e;
  border-radius: 4px;
  overflow: hidden;
}

.health-bar-fill {
  height: 100%;
  border-radius: 4px;
  transition: width 0.5s ease;
}

.health-metric-label {
  font-size: 12px;
  color: var(--text-dim);
  min-width: 90px;
}

.health-metric-value {
  font-size: 12px;
  font-weight: 700;
  min-width: 40px;
  text-align: right;
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: #2a2a4a; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #3a3a5a; }

/* ===== Newsletter Tool ===== */
.nl-tab {
  font-size: 13px;
  padding: 8px 16px;
  cursor: pointer;
  color: var(--text-muted);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  background: none;
  border-top: none;
  border-left: none;
  border-right: none;
  transition: color .15s;
}
.nl-tab:hover { color: var(--text-bright); }
.nl-tab-active { color: var(--accent) !important; border-bottom-color: var(--accent) !important; font-weight: 600; }

.nl-rule-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid #1a1a2e;
}

.nl-rule-icon {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  flex-shrink: 0;
  margin-top: 2px;
}
.nl-icon-on  { background: #1a3a2a; color: #27ae60; }
.nl-icon-off { background: #2a2a4a; color: #556; }

.nl-toggle {
  width: 36px;
  height: 20px;
  border-radius: 10px;
  border: 1px solid #2a2a4a;
  background: #2a2a4a;
  cursor: pointer;
  position: relative;
  transition: background .2s, border-color .2s;
  flex-shrink: 0;
  margin-top: 2px;
}
.nl-toggle-on { background: var(--accent) !important; border-color: var(--accent) !important; }
.nl-toggle::after {
  content: '';
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  top: 2px;
  left: 2px;
  transition: left .2s;
}
.nl-toggle-on::after { left: 18px; }

/* Newsletter Step 2 selected post border */
.nl-post-selected { border-color: var(--accent) !important; }

/* Step 4 preview iframe wrapper */
#nlPreview iframe {
  width: 100%;
  border: none;
  border-radius: 6px;
  min-height: 500px;
}

/* Newsletter Mode Buttons */
.nl-mode-btn {
  font-size: 12px;
  padding: 7px 16px;
  border-radius: 20px;
  border: 1px solid #2a2a4a;
  background: var(--bg-card2);
  color: var(--text-dim);
  cursor: pointer;
  transition: all .2s;
  white-space: nowrap;
}
.nl-mode-btn:hover { border-color: var(--accent); color: var(--text); }
.nl-mode-active {
  background: var(--accent) !important;
  border-color: var(--accent) !important;
  color: #fff !important;
  font-weight: 600;
}

/* Full-text Post Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.65);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-box {
  background: var(--bg-card);
  border: 1px solid #2a2a4a;
  border-radius: var(--radius);
  width: 100%;
  max-width: 680px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #2a2a4a;
  flex-shrink: 0;
}

.modal-type-badge {
  font-size: 11px;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 12px;
  background: var(--accent2);
  color: var(--blue);
}

.modal-text {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  font-size: 14px;
  line-height: 1.9;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
}

.modal-stats {
  padding: 12px 20px;
  border-top: 1px solid #2a2a4a;
  display: flex;
  gap: 18px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.modal-link {
  padding: 10px 20px;
  border-top: 1px solid #2a2a4a;
  flex-shrink: 0;
}

.modal-link a {
  font-size: 12px;
  color: var(--blue);
  text-decoration: none;
}
.modal-link a:hover { text-decoration: underline; }

/* KPI compare badges */
.kpi-delta { font-size: 12px; font-weight: 700; margin-left: 6px; }
.kpi-up   { color: #27ae60; }
.kpi-down { color: var(--accent); }

/* ===== Thread Splitter ===== */
.ts-post-card {
  background: var(--bg);
  border: 1px solid #2a2a4a;
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 12px;
  transition: border-color .2s;
}
.ts-post-card:focus-within { border-color: var(--blue); }

.ts-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.ts-seq-badge {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent2);
  color: var(--blue);
  font-size: 12px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.ts-post-textarea {
  background: var(--bg-card2);
  border: 1px solid #2a2a4a;
  border-radius: 6px;
  color: var(--text);
  font-family: 'Segoe UI', 'Microsoft JhengHei', sans-serif;
  font-size: 13px;
  line-height: 1.8;
  padding: 10px 12px;
  min-height: 100px;
  transition: border-color .2s;
}
.ts-post-textarea:focus {
  outline: none;
  border-color: var(--blue);
}

.ts-btn-sm {
  background: var(--bg-card2);
  border: 1px solid #2a2a4a;
  color: var(--text-dim);
  border-radius: 5px;
  padding: 3px 8px;
  font-size: 12px;
  cursor: pointer;
  transition: all .15s;
}
.ts-btn-sm:hover { background: #2a2a4a; color: var(--text); }
.ts-btn-del:hover { background: rgba(233,69,96,.15); border-color: var(--accent); color: var(--accent); }

.ts-ai-btns {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  flex-wrap: wrap;
}

.ts-btn-ai {
  background: none;
  border: 1px solid #2a2a4a;
  color: var(--text-muted);
  border-radius: 12px;
  padding: 3px 10px;
  font-size: 11px;
  cursor: pointer;
  transition: all .15s;
}
.ts-btn-ai:hover { border-color: var(--blue); color: var(--blue); background: rgba(52,152,219,.08); }

/* Phone Preview */
.ts-phone-wrap {
  max-width: 420px;
  background: #111;
  border-radius: 16px;
  padding: 16px;
  border: 1px solid #333;
}

.ts-phone-post {
  display: flex;
  gap: 10px;
  margin-bottom: 4px;
}

.ts-phone-avatar {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex-shrink: 0;
  width: 36px;
}

.ts-phone-avatar-circle {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #2a2a4a;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
}

.ts-phone-thread-line {
  width: 2px;
  flex: 1;
  min-height: 16px;
  background: #333;
  margin: 4px 0;
}

.ts-phone-body {
  flex: 1;
  padding-bottom: 12px;
}

.ts-phone-name {
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  margin-bottom: 4px;
}

.ts-phone-text {
  font-size: 13px;
  color: #ddd;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-word;
}

.ts-phone-actions {
  font-size: 16px;
  margin-top: 10px;
  color: #888;
  letter-spacing: 4px;
}

.ts-phone-reply .ts-phone-avatar-circle {
  width: 28px;
  height: 28px;
  font-size: 12px;
  background: #1a1a2e;
  color: #556;
}
