/* ── CSS tokens scoped to plugin elements ─────────────────── */
#realesrgan-panel,
#realesrgan-trigger {
  --v0:     #000000;
  --v1:     #0a0a0a;
  --v2:     #111111;
  --v3:     #1a1a1a;
  --v4:     #222222;
  --ln:     rgba(255,255,255,0.08);
  --ln-hi:  rgba(255,255,255,0.16);
  --t1:     #ffffff;
  --t2:     rgba(255,255,255,0.45);
  --t3:     rgba(255,255,255,0.18);
  --warn:   #e8a030;
  --danger: #e85050;
  --r:      10px;
  --rsm:    7px;
  --spring: cubic-bezier(0.34,1.56,0.64,1);
  --out:    cubic-bezier(0.22,1,0.36,1);
}

/* ── TRIGGER ──────────────────────────────────────────────── */
#realesrgan-trigger {
  position: fixed;
  z-index: 9999;
  width: 56px;
  height: 56px;
  border-radius: 14px;
  background: var(--v0);
  border: 1px solid var(--ln);
  cursor: pointer;
  display: grid;
  place-items: center;
  overflow: hidden;
  transition: transform 0.35s var(--spring), border-color 0.3s, box-shadow 0.3s;
  box-shadow: 0 4px 20px rgba(0,0,0,0.8);
}
#realesrgan-trigger::after {
  content: '';
  position: absolute;
  top: 0; left: -120%;
  width: 60%; height: 100%;
  background: linear-gradient(105deg, transparent 20%, rgba(255,255,255,0.07) 50%, transparent 80%);
  pointer-events: none;
}
#realesrgan-trigger:hover::after { left: 160%; transition: left 0.5s ease; }
#realesrgan-trigger:hover {
  transform: scale(1.08);
  border-color: var(--ln-hi);
  box-shadow: 0 6px 28px rgba(0,0,0,0.9);
}
#realesrgan-trigger:active  { transform: scale(0.93); }
#realesrgan-trigger.rg-open { border-color: rgba(255,255,255,0.2); }

.rg-trigger-star {
  font-size: 20px;
  color: #fff;
  position: relative;
  z-index: 2;
  animation: rge-breathe 2.8s ease-in-out infinite;
  transition: transform 0.35s var(--spring);
}
#realesrgan-trigger.rg-open .rg-trigger-star {
  animation: rge-star-open 0.4s var(--out) forwards;
}

/* ── PANEL ────────────────────────────────────────────────── */
.rg-panel {
  position: fixed;
  z-index: 9998;
  width: 300px;
  opacity: 0;
  pointer-events: none;
  transition: transform 0.38s var(--spring), opacity 0.24s ease;
  filter: drop-shadow(0 20px 40px rgba(0,0,0,0.9));
}
.rg-panel--bottom-right {
  bottom: 90px; right: 24px;
  transform: scale(0.9) translateY(12px);
  transform-origin: bottom right;
}
.rg-panel--bottom-left {
  bottom: 90px; left: 24px;
  transform: scale(0.9) translateY(12px);
  transform-origin: bottom left;
}
.rg-panel--top-right {
  top: 90px; right: 24px;
  transform: scale(0.9) translateY(-12px);
  transform-origin: top right;
}
.rg-panel--top-left {
  top: 90px; left: 24px;
  transform: scale(0.9) translateY(-12px);
  transform-origin: top left;
}
.rg-panel.rg-open {
  transform: scale(1) translateY(0);
  opacity: 1;
  pointer-events: all;
}

/* ── CARD ─────────────────────────────────────────────────── */
.rg-card {
  background: var(--v1);
  border: 1px solid var(--ln);
  border-radius: var(--r);
  overflow: hidden;
}

/* ── HEADER ───────────────────────────────────────────────── */
.rg-header {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 13px 14px 12px;
  border-bottom: 1px solid var(--ln);
}
.rg-header-star {
  font-size: 14px;
  color: #fff;
  animation: rge-breathe 2.8s ease-in-out infinite;
  flex-shrink: 0;
  opacity: 0.9;
}
.rg-header-name {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--t1);
  flex: 1;
}

.rg-x {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: transparent;
  border: 1px solid var(--ln);
  color: var(--t3);
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 10px;
  flex-shrink: 0;
  font-family: inherit;
  transition: border-color 0.2s, color 0.2s;
}
.rg-x:hover { border-color: var(--ln-hi); color: var(--t2); }

/* ── BODY ─────────────────────────────────────────────────── */
.rg-body { padding: 10px 0; }

.rg-group {
  padding: 0 12px;
  margin-bottom: 2px;
}
.rg-group-label {
  font-size: 8px;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--t3);
  padding: 4px 2px 6px;
  display: block;
}

/* ── ROWS ─────────────────────────────────────────────────── */
.rg-row {
  display: grid;
  grid-template-columns: 82px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 4px 2px;
  border-radius: var(--rsm);
}
.rg-row:hover { background: rgba(255,255,255,0.025); }
.rg-lbl {
  font-size: 10.5px;
  color: var(--t2);
  letter-spacing: 0.02em;
  white-space: nowrap;
}

/* ── SELECTS ──────────────────────────────────────────────── */
.rg-sel-wrap {
  position: relative;
  display: flex;
  align-items: center;
}
.rg-sel-wrap select {
  width: 100%;
  appearance: none;
  background: var(--v2);
  border: 1px solid var(--ln);
  border-radius: var(--rsm);
  color: var(--t1);
  font-family: inherit;
  font-size: 10.5px;
  padding: 5px 24px 5px 9px;
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s;
}
.rg-sel-wrap select:hover,
.rg-sel-wrap select:focus { border-color: var(--ln-hi); }
.rg-sel-wrap select option { background: #111; }
.rg-chev {
  position: absolute;
  right: 8px;
  font-size: 11px;
  color: var(--t3);
  pointer-events: none;
  transform: rotate(90deg);
  line-height: 1;
}

/* ── HELP BUTTON ──────────────────────────────────────────── */
.rg-help {
  width: 20px;
  height: 20px;
  border-radius: 5px;
  background: var(--v2);
  border: 1px solid var(--ln);
  color: var(--t3);
  font-size: 9px;
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  font-family: inherit;
  transition: border-color 0.2s, color 0.2s;
}
.rg-help:hover { border-color: var(--ln-hi); color: var(--t2); }

/* ── TOGGLE ROW ───────────────────────────────────────────── */
.rg-toggle-row {
  display: grid;
  grid-template-columns: 82px 1fr;
  align-items: center;
  gap: 8px;
  padding: 4px 2px;
  border-radius: var(--rsm);
}
.rg-toggle-row:hover { background: rgba(255,255,255,0.025); }
.rg-switch {
  position: relative;
  display: inline-block;
  width: 32px;
  height: 17px;
  flex-shrink: 0;
}
.rg-switch input { opacity: 0; width: 0; height: 0; }
.rg-slider {
  position: absolute;
  inset: 0;
  background: var(--v3);
  border: 1px solid var(--ln);
  border-radius: 99px;
  cursor: pointer;
  transition: background 0.25s, border-color 0.25s;
}
.rg-slider::before {
  content: '';
  position: absolute;
  width: 11px;
  height: 11px;
  left: 2px;
  top: 50%;
  transform: translateY(-50%);
  background: var(--t3);
  border-radius: 50%;
  transition: transform 0.28s var(--spring), background 0.25s;
}
.rg-switch input:checked + .rg-slider {
  background: var(--v3);
  border-color: var(--ln-hi);
}
.rg-switch input:checked + .rg-slider::before {
  transform: translate(15px, -50%);
  background: var(--t1);
}
.rg-toggle-inner {
  display: flex;
  align-items: center;
  gap: 8px;
}
.rg-limit-label {
  font-size: 10.5px;
  color: var(--t2);
}

/* ── DIVIDER ──────────────────────────────────────────────── */
.rg-div {
  height: 1px;
  background: var(--ln);
  margin: 8px 0;
}

/* ── WARNING ──────────────────────────────────────────────── */
.rg-warn {
  display: flex;
  gap: 7px;
  align-items: flex-start;
  background: rgba(232,160,48,0.05);
  border: 1px solid rgba(232,160,48,0.12);
  border-radius: var(--rsm);
  padding: 7px 9px;
  margin: 4px 12px;
}
.rg-warn.hidden { display: none; }
.rg-warn-icon {
  font-size: 10px;
  color: var(--warn);
  flex-shrink: 0;
  margin-top: 1px;
}
.rg-warn-text {
  font-size: 9.5px;
  color: rgba(232,160,48,0.65);
  line-height: 1.55;
}

/* ── ADVANCED ROWS (animated collapse) ───────────────────── */
.rg-adv-rows {
  overflow: hidden;
  max-height: 0;
  opacity: 0;
  transition: max-height 0.38s var(--out), opacity 0.28s ease;
}
.rg-adv-rows.rg-open {
  max-height: 400px;
  opacity: 1;
}

/* ── PROGRESS ─────────────────────────────────────────────── */
.rg-prog {
  padding: 10px 12px;
  border-top: 1px solid var(--ln);
  background: rgba(0,0,0,0.3);
}
.rg-prog-track {
  height: 2px;
  background: var(--v3);
  border-radius: 99px;
  overflow: hidden;
  margin-bottom: 7px;
}
.rg-prog-fill {
  height: 100%;
  background: rgba(255,255,255,0.5);
  border-radius: 99px;
  width: 0;
  transition: width 0.35s ease;
  position: relative;
}
.rg-prog-fill::after {
  content: '';
  position: absolute;
  right: 0; top: 0; bottom: 0;
  width: 24px;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.5));
  border-radius: 99px;
  animation: rge-pulse 1.4s ease-in-out infinite;
}
.rg-prog-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
#rg-status-el {
  font-size: 9px;
  color: var(--t2);
  letter-spacing: 0.05em;
}
#rg-status-el.rg-status--cancelled  { color: var(--danger); }
#rg-status-el.rg-status--cancelling { color: var(--warn); }
.rg-pct {
  font-size: 9px;
  color: rgba(255,255,255,0.5);
}

/* ── FOOTER ───────────────────────────────────────────────── */
.rg-footer {
  padding: 10px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  border-top: 1px solid var(--ln);
}
.rg-footer-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.rg-count {
  font-size: 9.5px;
  color: var(--t3);
  letter-spacing: 0.04em;
}
.rg-footer-msg {
  font-size: 9.5px;
  color: var(--t2);
  min-height: 0;
}
.rg-saved { color: #63cb74; }

/* ── ADVANCED TOGGLE BUTTON ───────────────────────────────── */
.rg-adv-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  background: transparent;
  border: 1px solid var(--ln);
  border-radius: 6px;
  padding: 4px 8px;
  font-family: inherit;
  font-size: 9.5px;
  color: var(--t3);
  cursor: pointer;
  letter-spacing: 0.06em;
  transition: border-color 0.2s, color 0.2s;
}
.rg-adv-btn:hover {
  border-color: var(--ln-hi);
  color: var(--t2);
}
.rg-adv-ch {
  font-size: 11px;
  display: inline-block;
  transform: rotate(90deg);
  transition: transform 0.25s ease;
  line-height: 1;
}
.rg-adv-btn.rg-open .rg-adv-ch { transform: rotate(-90deg); }

/* ── ACTIONS ROW ──────────────────────────────────────────── */
.rg-actions {
  display: flex;
  gap: 7px;
}

/* ── CANCEL BUTTON ────────────────────────────────────────── */
.rg-cancel {
  width: 40px;
  height: 40px;
  border-radius: var(--rsm);
  background: var(--v2);
  border: 1px solid var(--ln);
  color: var(--t3);
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 12px;
  flex-shrink: 0;
  font-family: inherit;
  transition: border-color 0.2s, color 0.2s, transform 0.2s;
}
.rg-cancel:hover {
  border-color: rgba(232,80,80,0.3);
  color: rgba(232,80,80,0.7);
  transform: scale(1.06);
}
.rg-cancel:active  { transform: scale(0.93); }
.rg-cancel.rg-hidden { display: none; }
.rg-cancel.rg-busy { opacity: 0.5; pointer-events: none; }

/* ── RUN BUTTON ───────────────────────────────────────────── */
.rg-run {
  flex: 1;
  height: 40px;
  border-radius: var(--rsm);
  background: var(--v0);
  border: 1px solid var(--ln-hi);
  color: var(--t1);
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  overflow: hidden;
  position: relative;
  transition: transform 0.3s var(--spring), border-color 0.3s, box-shadow 0.3s;
  box-shadow: 0 2px 12px rgba(0,0,0,0.6);
}
.rg-run::after {
  content: '';
  position: absolute;
  top: 0; left: -120%;
  width: 60%; height: 100%;
  background: linear-gradient(105deg, transparent 20%, rgba(255,255,255,0.06) 50%, transparent 80%);
  pointer-events: none;
}
.rg-run:hover::after { left: 160%; transition: left 0.5s ease; }
.rg-run:hover {
  transform: scale(1.03);
  border-color: rgba(255,255,255,0.25);
  box-shadow: 0 4px 20px rgba(0,0,0,0.8);
}
.rg-run:active  { transform: scale(0.97); }
.rg-run:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none !important;
}
.rg-run-star {
  font-size: 14px;
  animation: rge-breathe 2.8s ease-in-out infinite;
}

/* ── INLINE SINGLE-IMAGE UPSCALE STATE ───────────────────── */
.image-container.rg-inline-upscaling {
  position: relative;
}

.image-container .wp-manga-chapter-img.rg-inline-upscaling-target {
  opacity: 0 !important;
  transition: opacity 0.2s ease;
}

.rg-inline-upscale {
  position: absolute;
  z-index: 6;
  pointer-events: none;
  overflow: hidden;
  border-radius: 12px;
  box-shadow: 0 16px 32px rgba(0,0,0,0.18);
}

.rg-inline-skeleton {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(110deg, rgba(255,255,255,0.05) 8%, rgba(255,255,255,0.18) 18%, rgba(255,255,255,0.05) 33%),
    linear-gradient(180deg, rgba(24,24,24,0.92), rgba(40,40,40,0.92));
  background-size: 220% 100%, 100% 100%;
  animation: rg-inline-shimmer 1.25s linear infinite;
}

.rg-inline-badge {
  position: absolute;
  bottom: 12px;
  left: 12px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(10,10,10,0.84);
  border: 1px solid rgba(255,255,255,0.12);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.03em;
  backdrop-filter: blur(8px);
}

.rg-inline-spinner {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.24);
  border-top-color: rgba(255,255,255,0.95);
  animation: rg-inline-spin 0.8s linear infinite;
  flex-shrink: 0;
}

.rg-inline-text {
  white-space: nowrap;
}

/* ── TOOLTIP ──────────────────────────────────────────────── */
.rg-tip {
  display: none;
  position: absolute;
  top: 0;
  left: 0;
  background: var(--v1);
  border: 1px solid var(--ln-hi);
  border-radius: var(--r);
  padding: 10px 12px;
  font-size: 10px;
  color: var(--t2);
  line-height: 1.6;
  max-width: 210px;
  box-shadow: 0 8px 28px rgba(0,0,0,0.7);
  z-index: 10;
}
.rg-tip.rg-show    { display: block; }
.rg-tip-title {
  display: block;
  font-size: 11px;
  font-weight: 600;
  color: var(--t1);
  margin-bottom: 4px;
}
.rg-tip-body {
  font-size: 10px;
  color: var(--t2);
  line-height: 1.6;
}

/* ── KEYFRAMES ────────────────────────────────────────────── */
@keyframes rge-breathe {
  0%, 100% { transform: scale(1);    opacity: 1;    }
  50%       { transform: scale(1.28); opacity: 0.65; }
}
@keyframes rge-star-open {
  0%   { transform: rotate(0deg)   scale(1);    }
  60%  { transform: rotate(200deg) scale(0.82); }
  100% { transform: rotate(180deg) scale(1);    }
}
@keyframes rge-pulse {
  0%, 100% { opacity: 0.4; }
  50%       { opacity: 1;   }
}
@keyframes rg-inline-spin {
  to { transform: rotate(360deg); }
}
@keyframes rg-inline-shimmer {
  0% { background-position: 100% 0, 0 0; }
  100% { background-position: -100% 0, 0 0; }
}

/* ── TRIGGER TOAST ────────────────────────────────────────── */
#rg-trigger-toast {
  position: fixed;
  z-index: 10000;
  background: #0d0d0d;
  border: 1px solid rgba(255,255,255,0.13);
  border-radius: 11px;
  color: #fff;
  font-family: inherit;
  padding: 9px 13px 9px 11px;
  pointer-events: none;
  box-shadow: 0 6px 24px rgba(0,0,0,0.8);
  display: flex;
  align-items: center;
  gap: 9px;
  opacity: 1;
  transform: translateY(0) scale(1);
  transition: opacity 0.28s ease, transform 0.28s ease;
}
.rg-toast-icon {
  font-size: 15px;
  opacity: 0.75;
  flex-shrink: 0;
  animation: rge-breathe 2.8s ease-in-out infinite;
}
.rg-toast-body {
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.rg-toast-title {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: #fff;
  white-space: nowrap;
}
.rg-toast-sub {
  font-size: 9.5px;
  color: rgba(255,255,255,0.42);
  white-space: nowrap;
}
#rg-trigger-toast.rg-toast-hide {
  opacity: 0;
  transform: translateY(5px) scale(0.95);
}

/* ── HEADER BACKEND BADGE ─────────────────────────────────── */
.rg-backend-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 8.5px;
  font-weight: 700;
  letter-spacing: 0.08em;
  padding: 2px 6px;
  border-radius: 5px;
  border: 1px solid transparent;
  flex-shrink: 0;
  transition: background 0.3s, color 0.3s, border-color 0.3s;
  cursor: default;
  user-select: none;
}
.rg-backend-badge:empty { display: none; }
/* WebGL — neutral */
.rg-backend-badge--webgl {
  background: rgba(255,255,255,0.06);
  border-color: rgba(255,255,255,0.14);
  color: rgba(255,255,255,0.55);
}
/* WebGPU — blue accent */
.rg-backend-badge--webgpu {
  background: rgba(56,139,253,0.12);
  border-color: rgba(56,139,253,0.35);
  color: #5ba4f5;
}
/* Blue checkmark dot before label */
.rg-backend-badge--webgl::before,
.rg-backend-badge--webgpu::before {
  content: '\2713';
  font-size: 8px;
  font-weight: 900;
}
.rg-backend-badge--webgl::before  { color: rgba(255,255,255,0.35); }
.rg-backend-badge--webgpu::before { color: #5ba4f5; }

/* ── IN-PANEL DEVICE BANNER ───────────────────────────────── */
/* Banner lives inside #rg-footer-msg — it inherits the footer width
   naturally and never conflicts with .rg-card overflow:hidden.       */
.rg-banner {
  display: flex;
  align-items: flex-start;
  gap: 5px;
  padding: 6px 8px;
  font-size: 9px;
  line-height: 1.5;
  border-radius: 6px;
  border: 1px solid transparent;
  word-break: break-word;
  overflow-wrap: anywhere;
  animation: rge-slidein 0.2s ease;
  width: 100%;
  box-sizing: border-box;
}
.rg-banner--warn {
  background: rgba(232,160,48,0.08);
  border-color: rgba(232,160,48,0.22);
  color: rgba(232,160,48,0.9);
}
.rg-banner--error {
  background: rgba(232,80,80,0.08);
  border-color: rgba(232,80,80,0.22);
  color: rgba(232,80,80,0.9);
}
.rg-banner--info {
  background: rgba(255,255,255,0.03);
  border-color: rgba(255,255,255,0.1);
  color: rgba(255,255,255,0.5);
}
.rg-banner-icon { flex-shrink: 0; line-height: 1.5; }
.rg-banner-text { flex: 1; min-width: 0; }
.rg-banner-close {
  flex-shrink: 0;
  align-self: flex-start;
  background: none;
  border: none;
  cursor: pointer;
  color: inherit;
  opacity: 0.5;
  font-size: 11px;
  padding: 0 0 0 4px;
  line-height: 1;
}
.rg-banner-close:hover { opacity: 1; }
@keyframes rge-slidein {
  from { opacity: 0; transform: translateY(-3px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── MOBILE ───────────────────────────────────────────────── */
@media (max-width: 480px) {
  /* Trigger: shrink slightly, keep position driven by JS inline style */
  #realesrgan-trigger {
    width: 50px;
    height: 50px;
    border-radius: 13px;
  }

  /* Panel: stretch edge-to-edge with safe margins.
     Override all four positional classes so none of them
     fight each other on small screens. */
  .rg-panel,
  .rg-panel--bottom-right,
  .rg-panel--bottom-left,
  .rg-panel--top-right,
  .rg-panel--top-left {
    left: 10px !important;
    right: 10px !important;
    bottom: 76px !important;
    top: auto !important;
    width: auto !important;
    /* Animate from slightly below rather than scaled, avoids layout thrash */
    transform: translateY(14px) !important;
    transform-origin: bottom center !important;
  }
  .rg-panel.rg-open {
    transform: translateY(0) !important;
  }

  /* Slightly tighten the row grid so labels don't overflow */
  .rg-row { grid-template-columns: 70px 1fr auto; }

  /* Ensure card never exceeds viewport height with scroll fallback */
  .rg-card {
    max-height: calc(100dvh - 100px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .rg-inline-badge {
    bottom: 10px;
    left: 10px;
    font-size: 10px;
    padding: 7px 10px;
  }
}
