:root {
  --bg: #0e0e10;
  --surface: #18181b;
  --surface-2: #1f1f23;
  --surface-3: #26262c;
  --border: #2f2f35;
  --text: #efeff1;
  --muted: #adadb8;
  --purple: #9147ff;
  --purple-hover: #772ce8;
  --red: #eb0400;
  --radius: 10px;
  --topbar: 56px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom);
}

button, input, select {
  font: inherit;
  color: inherit;
}

button { cursor: pointer; }

svg { width: 100%; height: 100%; fill: currentColor; }

/* ------------------------------------------------------------- topbar */

.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 10px;
  height: calc(var(--topbar) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 12px 0;
  background: rgba(14, 14, 16, 0.92);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 9px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  flex: 0 0 auto;
}

.icon-btn:active { background: var(--surface-3); }
.icon-btn svg { width: 22px; height: 22px; }

.brand {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text);
  text-decoration: none;
  font-weight: 800;
  font-size: 17px;
  letter-spacing: -0.02em;
  flex: 0 0 auto;
}

.brand svg { width: 24px; height: 24px; color: var(--purple); }

.search {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1 1 auto;
  min-width: 0;
  height: 38px;
  padding: 0 12px;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: 20px;
}

.search:focus-within { border-color: var(--purple); }

.search svg { width: 17px; height: 17px; color: var(--muted); flex: 0 0 auto; }

.search input {
  flex: 1 1 auto;
  min-width: 0;
  border: 0;
  outline: 0;
  background: transparent;
  font-size: 15px;
}

.search input::placeholder { color: var(--muted); }

.suggestions {
  position: absolute;
  z-index: 60;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #1b1b1f;
  box-shadow: 0 12px 30px #0009;
}
.suggestions[hidden] { display: none; }
.suggestion { display: grid; grid-template-columns: 36px 1fr; grid-template-rows: 1fr 1fr; align-items: center; gap: 0 10px; width: 100%; padding: 8px 12px; border: 0; background: transparent; text-align: left; }
.suggestion:focus, .suggestion:hover { outline: 0; background: var(--surface-3); }
.suggestion img { grid-row: 1 / 3; width: 36px; height: 36px; border-radius: 50%; }
.suggestion-name { font-size: 13px; font-weight: 700; }
.suggestion small { color: var(--muted); font-size: 11px; }

/* ------------------------------------------------------------- layout */

.layout {
  max-width: 1280px;
  margin: 0 auto;
}

.view { padding: 14px 12px 40px; }

/* ------------------------------------------------------------- home */

.home-head { padding: 6px 2px 14px; }
.home-head h1 { margin: 0 0 4px; font-size: 24px; letter-spacing: -0.02em; }
.home-head p { margin: 0; color: var(--muted); font-size: 14px; }

.section-title {
  margin: 18px 2px 10px;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
}

.chips { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-size: 14px;
  font-weight: 600;
}

.chip:active { background: var(--surface-3); }

.chip .chip-live {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--red);
}

.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px 10px;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 0;
  border: 0;
  background: transparent;
  text-align: left;
  min-width: 0;
}

.card .thumb {
  position: relative;
  aspect-ratio: 16 / 9;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface-2);
}

.card .thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.card .viewers {
  position: absolute;
  left: 6px;
  bottom: 6px;
  padding: 2px 6px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.75);
  font-size: 11px;
  font-weight: 700;
}

.card .viewers::before {
  content: "";
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 5px;
  border-radius: 50%;
  background: var(--red);
  vertical-align: 1px;
}

.card .card-title {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.3;
}

.card .card-sub {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.skeleton {
  aspect-ratio: 16 / 9;
  border-radius: var(--radius);
  background: linear-gradient(100deg, var(--surface) 30%, var(--surface-2) 50%, var(--surface) 70%);
  background-size: 220% 100%;
  animation: shimmer 1.3s infinite linear;
}

@keyframes shimmer {
  to { background-position: -220% 0; }
}

/* ------------------------------------------------------------- watch */

.watch-grid { width: 100%; }

.watch-left { display: flex; flex-direction: column; gap: 0; }

.player {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
  overflow: hidden;
  border-radius: 0;
}

@media (min-width: 700px) {
  .player { border-radius: var(--radius); }
}

.player video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  background: #000;
  object-fit: contain;
}

.fullscreen-chat { display: none; }
.player.overlay-fullscreen, .player:fullscreen, .player:-webkit-full-screen { position: fixed; inset: 0; z-index: 100; width: 100vw; height: 100dvh; max-width: none; aspect-ratio: auto; border-radius: 0; }
.player:fullscreen .fullscreen-chat, .player:-webkit-full-screen .fullscreen-chat, .player.overlay-fullscreen .fullscreen-chat {
  position: absolute; display: flex; flex-direction: column; justify-content: flex-end;
  left: 0; bottom: 58px; z-index: 4; width: min(45%, 520px); max-height: 65%; overflow: hidden;
  padding: 12px 18px; pointer-events: none; mask-image: linear-gradient(transparent, #000 14%);
}
.fullscreen-chat .msg { padding: 2px 6px; border-radius: 4px; background: #08080bb8; text-shadow: 0 1px 3px #000; }

.player-shade { display: none; }

/* controls */

.controls {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 22px 6px 6px;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.85), rgba(0, 0, 0, 0));
  opacity: 1;
  transition: opacity 0.2s;
}

.controls .icon-btn { width: 38px; height: 38px; padding: 8px; color: #fff; }
.controls .icon-btn:active { background: rgba(255, 255, 255, 0.14); }
.controls svg { width: 21px; height: 21px; }

.grow { flex: 1 1 auto; }

.live-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-left: 4px;
  padding: 3px 7px;
  border-radius: 4px;
  background: var(--red);
  color: #fff;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.04em;
}

.ctrl-viewers {
  margin-left: 7px;
  font-size: 12px;
  font-weight: 600;
  color: #dcdce4;
}

#qualitySelect {
  max-width: 120px;
  height: 30px;
  padding: 0 4px;
  border: 0;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
}

#qualitySelect option { background: var(--surface); color: var(--text); }

/* overlays */

.ad-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 6;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 9px;
  border-radius: 5px;
  background: rgba(235, 4, 0, 0.92);
  color: #fff;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.06em;
}

.ad-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #fff;
  animation: pulse 1s infinite;
}

@keyframes pulse {
  50% { opacity: 0.25; }
}

.big-play {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 6;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 64px;
  height: 64px;
  padding: 15px;
  transform: translate(-50%, -50%);
  border: 0;
  border-radius: 50%;
  background: rgba(145, 71, 255, 0.95);
  color: #fff;
  box-shadow: 0 6px 26px rgba(0, 0, 0, 0.5);
}

.big-play svg { width: 34px; height: 34px; margin-left: 3px; }
.big-play[hidden] { display: none; }

.spinner {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.spinner[hidden] { display: none; }

.spinner span {
  width: 38px;
  height: 38px;
  border: 3px solid rgba(255, 255, 255, 0.22);
  border-top-color: var(--purple);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.player-error {
  position: absolute;
  inset: 0;
  z-index: 8;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 20px;
  background: rgba(0, 0, 0, 0.82);
  text-align: center;
}

.player-error[hidden] { display: none; }
.player-error p { margin: 0; font-size: 14px; color: #dcdce4; }

.btn {
  padding: 10px 18px;
  border: 0;
  border-radius: 8px;
  background: var(--purple);
  color: #fff;
  font-weight: 700;
}

.btn:active { background: var(--purple-hover); }

/* ------------------------------------------------------------- meta */

.meta {
  display: flex;
  align-items: flex-start;
  gap: 11px;
  padding: 12px 12px 10px;
}

.avatar {
  flex: 0 0 auto;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: var(--surface-3) center/cover no-repeat;
  border: 1px solid var(--border);
}

.meta-main { flex: 1 1 auto; min-width: 0; }

.meta-top {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
}

.meta-top h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--red);
  flex: 0 0 auto;
}

.twitch-link {
  margin-left: auto;
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-decoration: none;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
}

.ch-title {
  margin: 4px 0 5px;
  font-size: 13.5px;
  line-height: 1.35;
  color: #dcdce4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.meta-sub {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  font-size: 12.5px;
  color: var(--muted);
}

.meta-sub span:not(:empty)::before { content: ""; }

#chGame { color: var(--purple); font-weight: 700; }

/* ------------------------------------------------------------- chat */

.chat {
  display: flex;
  flex-direction: column;
  margin-top: 4px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  font-size: 13px;
  font-weight: 800;
  border-bottom: 1px solid var(--border);
}

.chat-status {
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
}

.chat-status.on { color: #00c46a; }
.chat-status.off { color: #ff6b6b; }

.chat-list {
  height: 46vh;
  min-height: 220px;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 8px 12px;
  font-size: 14px;
  line-height: 1.45;
  -webkit-overflow-scrolling: touch;
}

.chat-foot {
  padding: 9px 12px calc(9px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 11.5px;
  text-align: center;
}

.msg {
  padding: 2px 0;
  overflow-wrap: anywhere;
  animation: msgin 0.18s ease-out;
}

@keyframes msgin {
  from { opacity: 0; transform: translateY(3px); }
}

.msg .user { font-weight: 800; }
.msg-time { margin-right: 5px; color: #85858f; font-size: 10px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.chat-badge { width: 18px; height: 18px; margin: 0 3px -4px 0; object-fit: contain; }
.msg.deleted { color: #8d8d96; opacity: .7; font-style: italic; }
.msg .sep { color: var(--muted); }
.msg.system { color: var(--muted); font-style: italic; font-size: 13px; }
.msg .emote {
  height: 28px;
  width: auto;
  vertical-align: middle;
  margin: -4px 0;
}

.msg .emote-wrap {
  position: relative;
  display: inline-block;
  vertical-align: middle;
  margin: -4px 0;
  line-height: 0;
}

.msg .emote-wrap .emote {
  display: block;
  margin: 0;
}

.msg .emote-wrap .emote-overlay {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: auto;
  margin: 0;
  pointer-events: none;
}

/* ------------------------------------------------------------- desktop */

@media (min-width: 1000px) {
  .view { padding: 20px 20px 60px; }

  .watch-left {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 340px;
    grid-template-areas:
      "player chat"
      "meta   chat";
    grid-template-rows: auto 1fr;
    align-items: start;
    column-gap: 16px;
  }

  .player { grid-area: player; }
  .meta { grid-area: meta; padding: 12px 4px 10px; }
  .chat {
    grid-area: chat;
    margin-top: 0;
    height: calc(100% - 2px);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
  }

  .chat-list { height: calc(100vh - 300px); min-height: 320px; }

  .grid { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px 14px; }
  .home-head h1 { font-size: 28px; }
}

@media (min-width: 1400px) {
  .grid { grid-template-columns: repeat(5, minmax(0, 1fr)); }
}

/* Landscape cinema mode: player and chat share the full viewport. */
@media (max-height: 500px) and (orientation: landscape) {
  .topbar { display: none; }
  .view, .viewWatch.cinema { padding: 0; }
  .viewWatch.cinema .watch-left { display: grid; grid-template-columns: minmax(0, 1fr) minmax(250px, 34vw); grid-template-rows: minmax(0, 1fr); grid-template-areas: "player chat"; gap: 0 8px; height: 100dvh; }
  .viewWatch.cinema .player { grid-area: player; border-radius: 0; aspect-ratio: auto; height: 100%; min-height: 0; }
  .viewWatch.cinema .meta { display: none; }
  .viewWatch.cinema .chat { grid-area: chat; margin: 0; height: 100dvh; min-height: 0; border-top: 0; border-left: 1px solid var(--border); }
  .viewWatch.cinema .chat-list { flex: 1; height: auto; min-height: 0; font-size: 12px; padding: 6px 8px; }
  .viewWatch.cinema .chat-foot { display: none; }
}

@media (min-width: 700px) and (max-width: 999px) {
  .view { padding-left: 18px; padding-right: 18px; }
  .chat-list { height: 38vh; }
}
