:root {
  --bg-primary: #f5f5f5;
  --bg-secondary: #ffffff;
  --bg-sidebar: #e8e8e8;
  --bg-input: #ffffff;
  --bg-message-received: #e3e8ef;
  --bg-message-sent: #d4edda;
  --bg-hover: #dcdcdc;
  --bg-active: #c8c8c8;
  --bg-card: #ffffff;
  --text-primary: #1a1a1a;
  --text-secondary: #666666;
  --text-muted: #999999;
  --border-color: #d0d0d0;
  --accent: #5865f2;
  --accent-hover: #4752c4;
  --accent-light: rgba(88, 101, 242, 0.08);
  --danger: #e74c3c;
  --success: #27ae60;
  --embed-bg: #f0f1f4;
  --embed-border: #5865f2;
}

[data-theme="dark"] {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-sidebar: #0f172a;
  --bg-input: #1e2a4a;
  --bg-message-received: #243050;
  --bg-message-sent: #1a3a2a;
  --bg-hover: #253050;
  --bg-active: #2a3a60;
  --bg-card: #1e2a4a;
  --text-primary: #e0e0e0;
  --text-secondary: #a0a0a0;
  --text-muted: #707070;
  --border-color: #2a3a5a;
  --accent: #7289da;
  --accent-hover: #5b6ecc;
  --accent-light: rgba(114, 137, 218, 0.12);
  --embed-bg: #1a2540;
  --embed-border: #7289da;
}

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: var(--text-primary);
  background: var(--bg-primary);
  overflow: hidden;
}

body { display: flex; }

/* ── sidebar ── */

#sidebar {
  width: 230px;
  min-width: 180px;
  display: flex;
  flex-direction: column;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
}

#sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  border-bottom: 1px solid var(--border-color);
}

#sidebar-header h2 { font-size: 13px; font-weight: 600; }

#refresh-channels, #toggle-theme {
  width: 26px; height: 26px;
  border: none; border-radius: 6px;
  background: var(--bg-hover);
  color: var(--text-primary);
  cursor: pointer; font-size: 14px;
  display: flex; align-items: center; justify-content: center;
}

#refresh-channels:hover, #toggle-theme:hover { background: var(--bg-active); }

#guild-tree { flex: 1; overflow-y: auto; padding: 4px; }

#sidebar-footer { padding: 6px 12px; border-top: 1px solid var(--border-color); display: flex; justify-content: center; }

.guild-folder { margin-bottom: 2px; }

.guild-toggle {
  display: flex; align-items: center; gap: 4px;
  padding: 6px 8px; border-radius: 6px; cursor: pointer;
  font-size: 12px; font-weight: 600; color: var(--text-secondary);
  user-select: none;
}

.guild-toggle:hover { background: var(--bg-hover); color: var(--text-primary); }

.guild-toggle .arrow { font-size: 10px; width: 12px; text-align: center; transition: transform 0.15s; }
.guild-toggle .arrow.open { transform: rotate(90deg); }

.guild-channels { margin-left: 12px; display: none; }
.guild-channels.open { display: block; }

.channel-item {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 8px; border-radius: 6px; cursor: pointer;
  font-size: 13px; color: var(--text-secondary);
  margin-bottom: 1px;
}

.channel-item::before { content: "#"; font-weight: 700; color: var(--text-muted); font-size: 14px; }
.channel-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.channel-item.active { background: var(--accent-light); color: var(--accent); font-weight: 500; }
.channel-item.active::before { color: var(--accent); }

.guild-empty {
  padding: 20px 12px; text-align: center;
  color: var(--text-muted); font-size: 12px; line-height: 1.6;
}

/* ── main ── */

#main-panel {
  flex: 1; display: flex; flex-direction: column;
  min-width: 0; background: var(--bg-secondary);
}

/* tabs */

#tab-bar {
  display: flex; align-items: center; gap: 2px;
  padding: 6px 12px; border-bottom: 1px solid var(--border-color);
  flex-shrink: 0; background: var(--bg-primary);
}

.tab-btn {
  padding: 6px 16px; border: none; border-radius: 6px;
  background: transparent; color: var(--text-secondary);
  cursor: pointer; font-size: 13px; font-weight: 500;
}

.tab-btn:hover { background: var(--bg-hover); }
.tab-btn.active { background: var(--accent-light); color: var(--accent); }

.dot {
  font-size: 9px; margin-left: auto;
}

.dot.connected { color: var(--success); }
.dot.disconnected { color: var(--danger); }
.dot.connecting { color: #f0ad4e; animation: pulse 1s infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* tab panels */

.tab-panel { display: none; flex: 1; flex-direction: column; overflow: hidden; }
.tab-panel.active { display: flex; }

/* ── chat tab ── */

#chat-header {
  padding: 10px 16px; border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

#chat-title { font-size: 14px; font-weight: 600; }

#message-container { flex: 1; overflow-y: auto; padding: 8px 16px; }

#message-list { display: flex; flex-direction: column; gap: 6px; min-height: 100%; }

.empty-state {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  flex: 1; min-height: 200px;
  text-align: center; color: var(--text-muted); font-size: 13px; line-height: 1.8;
}

.empty-state .icon { font-size: 36px; margin-bottom: 10px; }
.empty-state .error-text { color: var(--danger); }

/* messages */

.message-item {
  display: flex; flex-direction: column;
  max-width: 88%; padding: 8px 12px;
  border-radius: 8px; font-size: 13px; line-height: 1.45;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.message-item.received {
  align-self: flex-start;
  background: var(--bg-message-received);
  border-bottom-left-radius: 2px;
}

.message-item.sent {
  align-self: flex-end;
  background: var(--bg-message-sent);
  border-bottom-right-radius: 2px;
}

.message-meta {
  display: flex; align-items: center; gap: 8px;
  margin-bottom: 2px; flex-wrap: wrap;
}

.message-sender { font-weight: 600; font-size: 11px; color: var(--accent); }
.message-time { font-size: 10px; color: var(--text-muted); }

.badge {
  font-size: 9px; padding: 0 4px; border-radius: 2px; font-weight: 500;
}

.badge.sent { background: rgba(39, 174, 96, 0.15); color: var(--success); }
.badge.received { background: var(--accent-light); color: var(--accent); }

.message-content { word-break: break-word; white-space: pre-wrap; }

/* attachments */

.attachments { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }

.attachments img {
  max-width: 240px; max-height: 200px; border-radius: 6px;
  cursor: pointer; object-fit: cover; border: 1px solid var(--border-color);
}

.attachments a {
  display: block; font-size: 12px; color: var(--accent);
  padding: 4px 8px; background: var(--bg-primary);
  border-radius: 4px; text-decoration: none; word-break: break-all;
}

/* embeds */

.embed-card {
  margin-top: 6px; padding: 10px 12px;
  background: var(--embed-bg);
  border-left: 4px solid var(--embed-border);
  border-radius: 4px; font-size: 12px;
}

.embed-title { font-weight: 600; margin-bottom: 4px; color: var(--text-primary); }

.embed-title a { color: var(--accent); text-decoration: none; }

.embed-desc { color: var(--text-secondary); white-space: pre-wrap; margin-bottom: 4px; }

.embed-fields { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }

.embed-field { min-width: 100px; }

.embed-field .field-name { font-weight: 600; font-size: 11px; }
.embed-field .field-value { font-size: 11px; color: var(--text-secondary); }

.embed-image img, .embed-thumb img {
  max-width: 280px; max-height: 160px; border-radius: 4px; margin-top: 4px;
}

.embed-footer { font-size: 10px; color: var(--text-muted); margin-top: 6px; }

.embed-author { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }

.embed-author img { width: 20px; height: 20px; border-radius: 50%; }

.embed-author span { font-size: 12px; font-weight: 600; }

/* image lightbox */

#lightbox {
  display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.85); z-index: 9999; cursor: pointer;
  align-items: center; justify-content: center;
}

#lightbox.show { display: flex; }

#lightbox img { max-width: 90vw; max-height: 90vh; border-radius: 6px; }

/* ── input ── */

#input-area {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 16px; border-top: 1px solid var(--border-color);
  background: var(--bg-primary); flex-shrink: 0;
}

#message-input {
  flex: 1; resize: none; padding: 10px 14px;
  border: 1px solid var(--border-color); border-radius: 8px;
  font-size: 13px; font-family: inherit; color: var(--text-primary);
  background: var(--bg-input); outline: none;
  transition: border-color 0.15s; max-height: 120px; line-height: 1.45;
}

#message-input:focus {
  border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-light);
}

#message-input:disabled { opacity: 0.5; cursor: not-allowed; }

#send-button {
  width: 36px; height: 36px; border: none; border-radius: 8px;
  background: var(--accent); color: #fff; font-size: 16px;
  cursor: pointer; display: flex; align-items: center;
  justify-content: center; flex-shrink: 0;
}

#send-button:hover:not(:disabled) { background: var(--accent-hover); }
#send-button:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── stats tab ── */

#tab-stats.active { display: flex; }

#stats-content {
  flex: 1; overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 16px;
}

.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }

.stat-card {
  padding: 14px; background: var(--bg-card); border-radius: 8px;
  border: 1px solid var(--border-color); text-align: center;
}

.stat-card .stat-value { font-size: 24px; font-weight: 700; color: var(--accent); }
.stat-card .stat-label { font-size: 11px; color: var(--text-muted); margin-top: 2px; }

.stats-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

@media (max-width: 600px) { .stats-row { grid-template-columns: 1fr; } }

.stats-section {
  background: var(--bg-card); border: 1px solid var(--border-color);
  border-radius: 8px; padding: 12px;
}

.stats-section h3 { font-size: 13px; font-weight: 600; margin-bottom: 10px; color: var(--text-secondary); }

.stat-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 5px 0; border-bottom: 1px solid var(--border-color);
  font-size: 12px;
}

.stat-row:last-child { border-bottom: none; }

.stat-row .name { color: var(--text-primary); flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.stat-row .count { font-weight: 600; color: var(--accent); margin-left: 12px; }

.stat-bar-wrap {
  width: 60px; height: 6px; background: var(--bg-hover);
  border-radius: 3px; margin-left: 8px; overflow: hidden;
}

.stat-bar-inner {
  height: 100%; background: var(--accent); border-radius: 3px; transition: width 0.3s;
}

/* trend chart */

.trend-bars {
  display: flex; align-items: flex-end; gap: 3px; height: 80px;
}

.trend-bar {
  flex: 1; min-width: 4px; background: var(--accent);
  border-radius: 2px 2px 0 0; transition: height 0.3s;
  position: relative; cursor: default;
}

.trend-bar:hover { opacity: 0.8; }

.trend-bar .tip {
  display: none; position: absolute; bottom: 100%;
  left: 50%; transform: translateX(-50%);
  background: var(--text-primary); color: var(--bg-primary);
  padding: 2px 6px; border-radius: 3px; font-size: 10px;
  white-space: nowrap; margin-bottom: 4px;
}

.trend-bar:hover .tip { display: block; }
