:root {
  --bg: #0e0e14;
  --panel: #1a1a26;
  --panel-2: #23233440;
  --text: #f2f2f7;
  --muted: #9a9ab0;
  --accent: #e63973;
  --accent-2: #ff5c8a;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 20px;
  background: rgba(14, 14, 20, 0.85);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid #ffffff10;
}

header h1 { font-size: 20px; margin: 0; white-space: nowrap; cursor: pointer; user-select: none; }
header h1:hover { color: var(--accent-2); }

#searchForm { display: flex; flex: 1; gap: 8px; max-width: 560px; }

#searchInput {
  flex: 1;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid #ffffff1a;
  background: var(--panel);
  color: var(--text);
  font-size: 15px;
}
#searchInput:focus { outline: none; border-color: var(--accent); }

button {
  cursor: pointer;
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  background: var(--accent);
  color: white;
  transition: background 0.15s;
}
button:hover { background: var(--accent-2); }

#platform, #homeLang {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid #ffffff1a;
  background: var(--panel);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
#platform:focus, #homeLang:focus { outline: none; border-color: var(--accent); }

.icon-btn { background: var(--panel); font-size: 18px; padding: 9px 12px; }
.icon-btn:hover { background: #2a2a3c; }

main { padding: 20px; max-width: 1100px; margin: 0 auto; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
}

.card {
  background: var(--panel);
  border-radius: 14px;
  overflow: hidden;
  cursor: pointer;
  border: 1px solid #ffffff0d;
  transition: transform 0.15s, border-color 0.15s;
}
.card:hover { transform: translateY(-4px); border-color: var(--accent); }

.card .thumb {
  width: 100%;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  background: var(--panel-2);
  display: block;
}
.card .meta { padding: 10px 12px; }
.card .meta .title { font-size: 14px; font-weight: 600; line-height: 1.3; }
.card .meta .sub { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* Episode chips */
#episodeList.grid { grid-template-columns: repeat(auto-fill, minmax(80px, 1fr)); }
.ep {
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
  background: var(--panel);
  border: 1px solid #ffffff12;
}
.ep:hover { border-color: var(--accent); transform: translateY(-3px); }

video {
  width: 100%;
  border-radius: 14px;
  background: black;
  aspect-ratio: 16 / 9;
}

h2 { font-size: 22px; }
.back {
  background: var(--panel);
  margin-bottom: 14px;
}
.back:hover { background: #2a2a3c; }

.hidden { display: none; }
.hint, .note, .status { color: var(--muted); font-size: 14px; }
.status { margin-top: 20px; }
.note { margin: 6px 0 14px; }

/* Settings dialog */
dialog {
  border: 1px solid #ffffff1a;
  border-radius: 16px;
  background: var(--panel);
  color: var(--text);
  padding: 24px;
  width: min(420px, 90vw);
}
dialog::backdrop { background: #000000aa; }
dialog h3 { margin-top: 0; }
#keyInput {
  width: 100%;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid #ffffff1a;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}
dialog menu { display: flex; justify-content: flex-end; gap: 10px; padding: 0; margin: 18px 0 0; }
dialog menu button[value="cancel"] { background: var(--panel-2); }

/* Drama poster: download overlay + explicit Watch/Download button */
.drama-card { position: relative; }
.thumb-wrap { position: relative; }
.prov-badge {
  position: absolute; top: 6px; left: 6px; z-index: 2;
  padding: 2px 7px; border-radius: 999px;
  font-size: 11px; font-weight: 600; line-height: 1.4;
  color: #fff; background: rgba(0,0,0,.72);
  border: 1px solid rgba(255,255,255,.18);
  pointer-events: none;
}
.drama-card.downloaded { border-color: #2e6b47; box-shadow: inset 0 0 0 2px #2e6b47; }
/* Always-visible button on the poster itself, not just a status badge —
   tells you at a glance whether clicking the picture downloads it or
   plays it straight from Telegram. */
.poster-action {
  position: absolute;
  left: 8px; right: 8px; bottom: 8px; z-index: 3;
  text-align: center;
  padding: 7px 0;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.2px;
  pointer-events: none;
}
.poster-action.watch {
  background: rgba(30, 58, 42, 0.9);
  color: #6ee7a0;
  border: 1px solid rgba(110, 231, 160, 0.35);
}
.poster-action.dl {
  background: rgba(0, 0, 0, 0.75);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.18);
}
.card-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(0, 0, 0, 0.78);
  border-radius: 14px;
  z-index: 4;
}
.card-overlay.hidden { display: none; }
.card-overlay .ring { width: 72px; height: 72px; }
.card-overlay .ring span { font-size: 15px; }
.card-overlay .ovlabel { font-size: 12px; color: var(--muted); font-weight: 600; text-align: center; padding: 0 8px; }

/* Video + download overlay */
#videoWrap { position: relative; }
#dlOverlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: rgba(0, 0, 0, 0.72);
  border-radius: 14px;
  backdrop-filter: blur(2px);
}
#dlOverlay.hidden { display: none; }
.ring {
  width: 96px;
  height: 96px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: conic-gradient(var(--accent-2) var(--pct, 0%), #ffffff22 0);
  position: relative;
}
.ring::before {
  content: "";
  position: absolute;
  inset: 9px;
  border-radius: 50%;
  background: var(--bg);
}
.ring span { position: relative; font-weight: 800; font-size: 20px; }
#dlLabel { color: var(--muted); font-weight: 600; }

/* Player controls */
#playerControls {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 14px;
  flex-wrap: wrap;
}
#prevBtn, #nextBtn { background: var(--panel); }
#prevBtn:hover:not(:disabled), #nextBtn:hover:not(:disabled) { background: var(--accent); }
#prevBtn:disabled, #nextBtn:disabled { opacity: 0.4; cursor: default; }
.autoplay {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.autoplay input { width: 16px; height: 16px; accent-color: var(--accent); }
.tag { font-size: 13px; font-weight: 600; padding: 6px 11px; border-radius: 8px; }
.tag-local { background: #1e3a2a; color: #6ee7a0; }
.tag-live { background: #3a2a1e; color: #ffcf8a; }
#epCounter { background: var(--panel); color: var(--text); }

/* Subtitle language picker */
.lang-picker { position: relative; }
#langBtn { background: var(--panel); font-weight: 600; font-size: 14px; }
#langBtn:hover { background: var(--accent); }
#langBtn.busy { opacity: 0.6; cursor: wait; }
.lang-menu {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 0;
  z-index: 10;
  background: var(--panel);
  border: 1px solid #ffffff1a;
  border-radius: 10px;
  padding: 6px;
  width: 220px;
  max-height: 280px;
  overflow-y: auto;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}
.lang-menu.hidden { display: none; }
.lang-menu button {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border-radius: 7px;
  padding: 8px 10px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
}
.lang-menu button:hover { background: var(--panel-2); }
.lang-menu button.active { background: #1e3a2a; color: #6ee7a0; font-weight: 700; }
.lang-menu .note { padding: 6px 10px; font-size: 12px; }

/* Per-drama actions */
#dramaActions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 4px 0 16px;
  flex-wrap: wrap;
}
#downloadAllBtn { background: var(--panel); border: 1px solid var(--accent); }
#downloadAllBtn:hover:not(:disabled) { background: var(--accent); }
#downloadAllBtn:disabled { opacity: 0.6; cursor: default; }

/* Episode chips */
.ep { position: relative; overflow: hidden; }
.ep .num { position: relative; z-index: 1; }
.ep .dot {
  position: absolute;
  top: 4px;
  right: 6px;
  font-size: 12px;
  font-weight: 800;
  color: #6ee7a0;
  z-index: 2;
}
.ep .dot.err { color: #ff7a7a; }
.ep .cpct {
  position: absolute;
  bottom: 5px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 11px;
  font-weight: 700;
  color: var(--accent-2);
  z-index: 2;
}
.ep .cbar {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 4px;
  width: var(--pct, 0%);
  background: var(--accent-2);
  z-index: 1;
  transition: width 0.3s;
}
/* downloaded = green; downloading = pink ring; */
.ep.saved { border-color: #2e6b47; box-shadow: inset 0 0 0 1px #2e6b47; }
.ep.downloading { border-color: var(--accent-2); }
.ep.error { border-color: #ff7a7a; }

/* Library view */
#popularBlock { margin-bottom: 30px; padding-bottom: 20px; border-bottom: 1px solid var(--panel-2); }
#popularBlock.hidden { display: none; }
.lib-drama { margin: 22px 0; }
.lib-head { display: flex; gap: 14px; align-items: center; margin-bottom: 12px; }
.lib-head img, .lib-head .thumb {
  width: 54px; height: 80px; object-fit: cover; border-radius: 8px; background: var(--panel-2);
}
.lib-head .title { font-size: 17px; font-weight: 700; }
.open-drama {
  margin-top: 6px;
  padding: 5px 10px;
  font-size: 12px;
  background: var(--panel);
  border: 1px solid #ffffff1a;
}
.open-drama:hover { background: var(--accent); }
.eps { grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); }
