/* ---------------------------------------------------------------------------
   Design tokens. Light palette on :root; only the values that change are
   redefined for dark, so nothing can be left undefined in one theme.
   --------------------------------------------------------------------------- */

:root {
  --bg: #f4f6fb;
  --bg-glow: radial-gradient(1100px 520px at 50% -12%, #e0e7ff 0%, transparent 70%);
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e3e8f0;
  --border-strong: #cbd5e1;

  --text: #0f172a;
  --text-muted: #64748b;
  --text-faint: #94a3b8;

  --accent: #6366f1;
  --accent-hover: #5457e5;
  --accent-soft: #eef2ff;
  --accent-contrast: #ffffff;

  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --danger-border: #fecaca;
  --success: #059669;

  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.05), 0 8px 24px -12px rgba(15, 23, 42, 0.18);
  --shadow-lift: 0 2px 4px rgba(15, 23, 42, 0.06), 0 18px 40px -16px rgba(15, 23, 42, 0.28);

  --font: ui-sans-serif, system-ui, -apple-system, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #080b16;
    --bg-glow: radial-gradient(1100px 520px at 50% -12%, #1e1b4b 0%, transparent 70%);
    --surface: #101527;
    --surface-2: #161c31;
    --border: #232a41;
    --border-strong: #333c56;

    --text: #eef2f8;
    --text-muted: #98a3ba;
    --text-faint: #6b7899;

    --accent: #818cf8;
    --accent-hover: #949dfa;
    --accent-soft: #1e2242;
    --accent-contrast: #0b0f1d;

    --danger: #f87171;
    --danger-soft: #2a1416;
    --danger-border: #4c2225;
    --success: #34d399;

    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px -12px rgba(0, 0, 0, 0.6);
    --shadow-lift: 0 2px 4px rgba(0, 0, 0, 0.5), 0 18px 40px -16px rgba(0, 0, 0, 0.7);
  }
}

* {
  box-sizing: border-box;
}

/* Any explicit `display` below (flex/grid) would otherwise beat the `hidden`
   attribute, leaving empty error boxes and progress rows on screen. */
[hidden] {
  display: none !important;
}

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

body {
  margin: 0;
  min-height: 100vh;
  background-color: var(--bg);
  background-image: var(--bg-glow);
  background-repeat: no-repeat;
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.shell {
  width: min(760px, 100% - 2.5rem);
  margin-inline: auto;
  padding: clamp(2rem, 6vw, 4rem) 0 3rem;
}

/* --------------------------------- header -------------------------------- */

.masthead {
  text-align: center;
  margin-bottom: clamp(1.75rem, 4vw, 2.5rem);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 11px;
  background: linear-gradient(140deg, var(--accent), #a855f7);
  color: #fff;
  box-shadow: 0 6px 18px -6px color-mix(in srgb, var(--accent) 70%, transparent);
}

.brand-mark svg {
  width: 20px;
  height: 20px;
}

.brand-text {
  font-size: clamp(1.35rem, 3.5vw, 1.65rem);
  font-weight: 700;
  letter-spacing: -0.02em;
}

.tagline {
  margin: 0.55rem 0 0;
  color: var(--text-muted);
  font-size: 0.95rem;
}

/* --------------------------------- gate ---------------------------------- */

.gate {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background-color: var(--bg);
  background-image: var(--bg-glow);
  background-repeat: no-repeat;
}

.gate-card {
  width: min(380px, 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
  padding: 2rem 1.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lift);
  text-align: center;
}

.gate-card h1 {
  margin: 0.4rem 0 0;
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.gate-card > p {
  margin: 0 0 0.4rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}

#gate-code {
  width: 100%;
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  outline: none;
}

#gate-code:focus {
  border-color: color-mix(in srgb, var(--accent) 55%, var(--border));
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 15%, transparent);
}

#gate-submit {
  width: 100%;
}

.gate-card .field-error {
  margin: 0;
  align-self: flex-start;
}

/* ------------------------------ licence notice --------------------------- */

.cc-notice {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  margin-bottom: 1rem;
  padding: 0.85rem 1rem;
  border: 1px solid color-mix(in srgb, var(--accent) 30%, var(--border));
  background: var(--accent-soft);
  border-radius: var(--radius-sm);
}

.cc-badge {
  flex: none;
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.02em;
}

.cc-notice p {
  margin: 0;
  font-size: 0.86rem;
  line-height: 1.5;
  color: var(--text);
}

/* Attribution line on the resolved video card. */
.attribution {
  margin: 0.4rem 0 0;
  font-size: 0.78rem;
  line-height: 1.45;
  color: var(--text-muted);
}

.attribution .lic {
  display: inline-block;
  padding: 1px 6px;
  margin-right: 0.35rem;
  border-radius: 5px;
  border: 1px solid color-mix(in srgb, var(--success) 45%, transparent);
  background: color-mix(in srgb, var(--success) 12%, transparent);
  color: var(--success);
  font-size: 0.72rem;
  font-weight: 700;
}

/* ------------------------------- paste card ------------------------------ */

.paste-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.55rem;
}

.paste-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.paste-icon {
  display: grid;
  place-items: center;
  padding-left: 0.6rem;
  color: var(--text-faint);
  flex: none;
}

.paste-icon svg {
  width: 18px;
  height: 18px;
}

#url {
  flex: 1 1 auto;
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  padding: 0.7rem 0.2rem;
  outline: none;
}

#url::placeholder {
  color: var(--text-faint);
}

.primary-btn,
.ghost-btn,
.download-btn,
.cancel-btn {
  font: inherit;
  font-weight: 600;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease,
    transform 0.08s ease, box-shadow 0.15s ease;
}

.primary-btn {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-width: 118px;
  padding: 0.72rem 1.1rem;
  border: 1px solid transparent;
  background: var(--accent);
  color: var(--accent-contrast);
}

.primary-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

.primary-btn:active:not(:disabled) {
  transform: translateY(1px);
}

.primary-btn:disabled {
  opacity: 0.62;
  cursor: default;
}

.ghost-btn {
  flex: none;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 0.87rem;
}

.ghost-btn:hover {
  color: var(--text);
  border-color: var(--border-strong);
}

/* Spinner shown while resolving */
.spinner {
  display: none;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 2px solid color-mix(in srgb, var(--accent-contrast) 40%, transparent);
  border-top-color: var(--accent-contrast);
  animation: spin 0.7s linear infinite;
}

.is-loading .spinner {
  display: block;
}

.is-loading .btn-label {
  display: none;
}

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

.field-error {
  margin: 0.5rem 0.6rem 0.25rem;
  color: var(--danger);
  font-size: 0.87rem;
}

/* --------------------------------- result -------------------------------- */

.result {
  margin-top: 1.25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  animation: rise 0.28s ease both;
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}

.video-card {
  display: flex;
  gap: 1rem;
  padding: 1rem;
  border-bottom: 1px solid var(--border);
}

.thumb-wrap {
  position: relative;
  flex: none;
  width: 168px;
  aspect-ratio: 16 / 9;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface-2);
}

#thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.duration-chip {
  position: absolute;
  right: 6px;
  bottom: 6px;
  padding: 1px 6px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.82);
  color: #fff;
  font-size: 0.74rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.video-meta {
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.video-meta h2 {
  margin: 0;
  font-size: 1.03rem;
  font-weight: 650;
  line-height: 1.35;
  letter-spacing: -0.01em;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.channel,
.views {
  margin: 0.3rem 0 0;
  color: var(--text-muted);
  font-size: 0.87rem;
}

.views {
  margin-top: 0.1rem;
  color: var(--text-faint);
  font-size: 0.8rem;
}

/* ---------------------------------- tabs --------------------------------- */

.tabs {
  display: flex;
  gap: 0.25rem;
  padding: 0.7rem 1rem 0;
}

.tab {
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  padding: 0.5rem 0.9rem;
  border: 0;
  border-radius: 9px 9px 0 0;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  position: relative;
}

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

.tab.active {
  color: var(--accent);
  background: var(--accent-soft);
}

.panel {
  padding: 0.75rem 1rem 1rem;
}

/* ------------------------------ quality rows ----------------------------- */

.quality-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.quality-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 0.85rem;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}

.quality-row:hover {
  border-color: var(--border-strong);
}

.q-main {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  min-width: 0;
}

.q-label {
  font-weight: 650;
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
}

.q-badges {
  display: flex;
  gap: 0.3rem;
  flex-wrap: wrap;
}

.badge {
  padding: 1px 6px;
  border-radius: 5px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.badge.hi {
  border-color: color-mix(in srgb, var(--accent) 45%, transparent);
  background: var(--accent-soft);
  color: var(--accent);
}

.q-size {
  color: var(--text-muted);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.download-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.38rem;
  padding: 0.45rem 0.8rem;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 0.86rem;
}

.download-btn:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

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

.download-btn.done {
  border-color: color-mix(in srgb, var(--success) 50%, transparent);
  color: var(--success);
  background: color-mix(in srgb, var(--success) 10%, transparent);
}

.dl-icon {
  width: 15px;
  height: 15px;
  flex: none;
}

/* ------------------------------- progress -------------------------------- */

.quality-row.busy {
  grid-template-columns: 1fr;
  gap: 0.5rem;
}

.quality-row.busy .q-size,
.quality-row.busy .download-btn {
  display: none;
}

.q-progress {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "bar cancel" "meta cancel";
  align-items: center;
  column-gap: 0.75rem;
  row-gap: 0.35rem;
}

.bar {
  grid-area: bar;
  height: 7px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent), #a855f7);
  transition: width 0.25s ease;
}

/* Merge/convert has no byte progress — show motion instead of a stuck bar. */
.q-progress.indeterminate .bar-fill {
  width: 100% !important;
  background: linear-gradient(
    90deg,
    transparent 0%,
    var(--accent) 40%,
    #a855f7 60%,
    transparent 100%
  );
  background-size: 220% 100%;
  animation: sweep 1.1s linear infinite;
}

@keyframes sweep {
  from {
    background-position: 160% 0;
  }
  to {
    background-position: -60% 0;
  }
}

.bar-meta {
  grid-area: meta;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
  min-width: 0;
  font-size: 0.79rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* In a narrow row these two would each wrap onto their own second line and
   push the bar around; keep them on one line and clip the phase instead. */
.bar-phase {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bar-stats {
  flex: none;
  white-space: nowrap;
  color: var(--text-faint);
}

.cancel-btn {
  grid-area: cancel;
  padding: 0.35rem 0.7rem;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.8rem;
}

.cancel-btn:hover {
  color: var(--danger);
  border-color: var(--danger-border);
}

.row-error {
  grid-column: 1 / -1;
  margin: 0.1rem 0 0;
  color: var(--danger);
  font-size: 0.82rem;
}

/* ------------------------------ page states ------------------------------ */

.page-error {
  display: flex;
  align-items: flex-start;
  gap: 0.7rem;
  margin-top: 1.25rem;
  padding: 0.9rem 1rem;
  border: 1px solid var(--danger-border);
  background: var(--danger-soft);
  border-radius: var(--radius-sm);
}

.page-error-icon {
  flex: none;
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  font-size: 0.78rem;
  font-weight: 700;
}

.page-error p {
  margin: 0;
  color: var(--danger);
  font-size: 0.9rem;
}

.empty-hint {
  margin-top: 1.5rem;
  text-align: center;
  color: var(--text-faint);
  font-size: 0.85rem;
}

.empty-hint code {
  font-family: var(--mono);
  font-size: 0.82em;
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.foot {
  margin-top: 2.5rem;
  text-align: center;
  color: var(--text-faint);
  font-size: 0.79rem;
  line-height: 1.6;
}

.foot p {
  margin: 0 auto;
  max-width: 46ch;
}

.engine {
  margin-top: 0.5rem !important;
  font-family: var(--mono);
  font-size: 0.72rem;
  opacity: 0.7;
}

/* ------------------------------- a11y/focus ------------------------------ */

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 6px;
}

.paste-card:focus-within {
  border-color: color-mix(in srgb, var(--accent) 55%, var(--border));
  box-shadow: var(--shadow), 0 0 0 4px color-mix(in srgb, var(--accent) 15%, transparent);
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

/* ------------------------------- responsive ------------------------------ */

@media (max-width: 560px) {
  .paste-row {
    flex-wrap: wrap;
  }

  #url {
    order: -1;
    width: 100%;
    flex-basis: 100%;
    padding-inline: 0.6rem;
  }

  .paste-icon {
    display: none;
  }

  .primary-btn {
    flex: 1 1 auto;
  }

  .video-card {
    flex-direction: column;
  }

  .thumb-wrap {
    width: 100%;
  }

  .quality-row {
    grid-template-columns: 1fr auto;
    row-gap: 0.5rem;
  }

  .download-btn {
    grid-column: 2;
    grid-row: 1;
  }

  .q-size {
    grid-column: 1;
    grid-row: 2;
  }
}
