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

:root {
  --bg: #0d0d0f;
  --surface: #16161a;
  --border: #2a2a32;
  --text: #efeff1;
  --text-muted: #adadb8;
  --twitch: #9147ff;
  --youtube: #ff0000;
  --live: #00f593;
  --offline: #53535f;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html {
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  overflow: hidden;
}

/* ─── Header ─────────────────────────────────────────────────────────────── */
.header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px 10px;
  flex-shrink: 0;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.header-logo {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  flex-shrink: 0;
  object-fit: cover;
}

.header-title {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  flex: 1;
}

.stats-toggle {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-muted);
  padding: 6px 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s, border-color 0.2s;
  -webkit-tap-highlight-color: transparent;
  flex-shrink: 0;
}

.stats-toggle:hover,
.stats-toggle[aria-expanded="true"] {
  color: var(--text);
  border-color: var(--text-muted);
}

/* ─── Status pills ───────────────────────────────────────────────────────── */
.status-row {
  display: flex;
  gap: 8px;
}

.status-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 0.75rem;
  flex: 1;
  min-width: 0;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--offline);
  flex-shrink: 0;
  transition: background 0.3s;
}

.status-pill.live .status-dot {
  background: var(--live);
  box-shadow: 0 0 6px var(--live);
}

.status-label {
  font-weight: 600;
  flex-shrink: 0;
}

.status-pill[data-platform="twitch"] .status-label { color: var(--twitch); }
.status-pill[data-platform="youtube"] .status-label { color: var(--youtube); }

.status-text {
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.viewer-count {
  margin-left: auto;
  font-weight: 700;
  font-size: 0.7rem;
  color: var(--live);
  flex-shrink: 0;
}

/* ─── Stats panel ────────────────────────────────────────────────────────── */
.stats-panel {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.stats-panel[hidden] { display: none; }

.stats-grid {
  display: flex;
  gap: 10px;
}

.stat-card {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}

.stat-value {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.stat-chatters-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stat-chatters {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chatter-chip {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.chatter-chip strong {
  color: var(--text);
  margin-left: 4px;
}

/* ─── Tabs ───────────────────────────────────────────────────────────────── */
.tabs {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.tab {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.9rem;
  font-weight: 600;
  padding: 12px 8px;
  cursor: pointer;
  position: relative;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.2s;
}

.tab.active {
  color: var(--text);
}

.tab.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 16px;
  right: 16px;
  height: 2px;
  background: var(--twitch);
  border-radius: 2px 2px 0 0;
}

.tab[data-tab="youtube"].active::after { background: var(--youtube); }
.tab[data-tab="combined"].active::after {
  background: linear-gradient(90deg, var(--twitch), var(--youtube));
}

.tab-badge {
  display: none;
  background: var(--twitch);
  color: white;
  font-size: 0.65rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 1px 5px;
  margin-left: 4px;
  vertical-align: middle;
}

.tab[data-tab="youtube"] .tab-badge { background: var(--youtube); }

.tab.has-unread .tab-badge {
  display: inline;
}

/* ─── Chat layout ────────────────────────────────────────────────────────── */
.chat-container {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.chat-panel {
  display: none;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

.chat-panel.active {
  display: flex;
}

/* ─── Stream bar ─────────────────────────────────────────────────────────── */
.stream-bar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 7px 12px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.stream-bar[hidden] { display: none; }

.stream-bar-left {
  flex: 1;
  min-width: 0;
}

.stream-bar-title {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 2px;
}

.stream-bar-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
}

.meta-sep {
  opacity: 0.4;
}

.meta-sep[hidden] { display: none; }

/* ─── Clip button ────────────────────────────────────────────────────────── */
.clip-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  background: var(--twitch);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 0.15s, transform 0.1s;
}

.clip-btn:active {
  transform: scale(0.95);
}

.clip-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

/* ─── Clip toast ─────────────────────────────────────────────────────────── */
.clip-toast {
  position: fixed;
  bottom: calc(20px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--twitch);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 10px 20px;
  border-radius: 999px;
  z-index: 200;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
  white-space: nowrap;
  animation: toastIn 0.2s ease;
}

.clip-toast[hidden] { display: none; }

.clip-toast.clip-toast-error {
  background: #c0392b;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ─── Offline banner ─────────────────────────────────────────────────────── */
.offline-banner {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 32px 24px;
  color: var(--text-muted);
  gap: 6px;
}

.offline-banner.hidden { display: none; }

.offline-banner p {
  font-size: 1rem;
  font-weight: 500;
}

.offline-sub {
  font-size: 0.8rem !important;
  opacity: 0.7;
}

/* ─── Chat list ──────────────────────────────────────────────────────────── */
.chat-list {
  list-style: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  flex: 1;
  padding: 8px 12px 16px;
  scroll-behavior: smooth;
}

.chat-list:empty { display: none; }

/* ─── Chat items ─────────────────────────────────────────────────────────── */
.chat-item {
  padding: 5px 0;
  line-height: 1.5;
  font-size: 1.05rem;
  word-break: break-word;
  animation: fadeIn 0.15s ease;
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 3px;
}

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

.chat-item.superchat {
  background: rgba(21, 101, 192, 0.15);
  border-left: 3px solid #1565c0;
  padding-left: 8px;
  border-radius: 4px;
  margin: 2px 0;
}

/* ─── Platform icons ─────────────────────────────────────────────────────── */
.platform-icon {
  vertical-align: middle;
  flex-shrink: 0;
  margin-right: 1px;
}

.twitch-icon { fill: var(--twitch); }
.youtube-icon { fill: var(--youtube); }

/* ─── Badges ─────────────────────────────────────────────────────────────── */
.badge {
  font-size: 0.6rem;
  font-weight: 700;
  padding: 1px 4px;
  border-radius: 3px;
  vertical-align: middle;
  flex-shrink: 0;
  letter-spacing: 0.02em;
}

.badge-mod {
  background: rgba(0, 245, 147, 0.15);
  color: var(--live);
}

.badge-owner {
  background: rgba(255, 0, 0, 0.15);
  color: #ff6666;
}

.badge-vip {
  background: rgba(255, 0, 230, 0.15);
  color: #ff66f5;
}

.badge-sub {
  background: rgba(145, 71, 255, 0.15);
  color: var(--twitch);
}

.badge-superchat {
  background: rgba(21, 101, 192, 0.3);
  color: #64b5f6;
}

/* ─── Username + message ─────────────────────────────────────────────────── */
.chat-user {
  font-weight: 700;
  flex-shrink: 0;
}

.chat-message {
  color: var(--text);
  flex: 1;
  min-width: 0;
}

/* ─── Timestamp ──────────────────────────────────────────────────────────── */
.chat-time {
  font-size: 0.7rem;
  color: var(--text-muted);
  opacity: 0.75;
  flex-shrink: 0;
  align-self: flex-end;
  margin-left: auto;
  padding-left: 6px;
}

/* ─── Emotes ─────────────────────────────────────────────────────────────── */
.chat-emote {
  height: 1.4em;
  width: auto;
  vertical-align: middle;
  display: inline-block;
  margin: 0 1px;
}

/* ─── Mention highlight ──────────────────────────────────────────────────── */
.chat-mention {
  background: rgba(255, 193, 7, 0.2);
  color: #ffd54f;
  border-radius: 3px;
  padding: 0 2px;
  font-style: normal;
}

/* ─── Jump to bottom button ──────────────────────────────────────────────── */
.jump-btn {
  position: fixed;
  bottom: calc(72px + var(--safe-bottom));
  right: 16px;
  z-index: 150;
  background: var(--twitch);
  color: #fff;
  border: none;
  border-radius: 999px;
  padding: 10px 18px;
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.55);
  -webkit-tap-highlight-color: transparent;
  animation: toastIn 0.2s ease;
}

.jump-btn[hidden] { display: none; }

.jump-btn:active { transform: scale(0.95); }

/* ─── Event banner (raids / subs / gifts) ────────────────────────────────── */
.event-banner {
  position: fixed;
  top: 130px;
  left: 12px;
  right: 12px;
  z-index: 160;
  border-radius: 14px;
  padding: 14px 18px;
  text-align: center;
  font-size: 1rem;
  font-weight: 700;
  color: #fff;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
  animation: bannerIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  background: linear-gradient(135deg, #6441a5, #9147ff);
}

.event-banner[hidden] { display: none; }

.event-banner[data-type="raid"] {
  background: linear-gradient(135deg, #b34000, #ff6600);
}

.event-banner[data-type="gift"] {
  background: linear-gradient(135deg, #b38600, #f5c518);
  color: #1a1a00;
}

@keyframes bannerIn {
  from { opacity: 0; transform: translateY(-20px) scale(0.95); }
  to   { opacity: 1; transform: translateY(0)     scale(1); }
}

/* ─── Reconnect bar ──────────────────────────────────────────────────────── */
.connection-bar {
  position: fixed;
  bottom: calc(12px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 0.8rem;
  color: var(--text-muted);
  z-index: 100;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
}

/* ─── Desktop layout ─────────────────────────────────────────────────────── */
@media (min-width: 768px) {
  .tabs { display: none; }

  .chat-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0;
  }

  .chat-panel {
    display: flex !important;
    border-right: 1px solid var(--border);
  }

  .chat-panel:last-child { border-right: none; }

  .chat-panel::before {
    content: attr(data-platform);
    display: block;
    text-transform: capitalize;
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
  }

  #panel-twitch::before { content: 'Twitch'; color: var(--twitch); }
  #panel-youtube::before { content: 'YouTube'; color: var(--youtube); }
  #panel-combined { display: none !important; }
}

@media (prefers-reduced-motion: reduce) {
  .chat-item { animation: none; }
  .chat-list { scroll-behavior: auto; }
}
