/* Galaxy reskin (personal fork) — remap EngramQuest's indigo/violet accent → sky/cyan
 * tông Knowledge Galaxy, giữ nguyên vàng/xanh-lá/xanh-dương/đỏ (ngữ nghĩa chỉ số).
 * Idempotent: LUÔN transform từ index.js.orig-backup (bản gốc pristine).
 * Chạy: node scripts/galaxy-reskin.mjs  → ghi đè src/styles/index.js */
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { createRequire } from 'module';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);
const stylesDir = path.join(__dirname, '..', 'src', 'styles');
const orig = path.join(stylesDir, 'index.js.orig-backup');
const out = path.join(stylesDir, 'index.js');

let css = require(orig); // pristine CSS string

// --- 1. Hex remap: indigo/violet family -> sky/cyan (case-insensitive) ---
const HEX = {
  '#6366f1': '#38bdf8', // indigo-500 (accent chính)  -> sky-400
  '#4f46e5': '#0ea5e9', // indigo-600 (accent đậm)     -> sky-500
  '#4338ca': '#0284c7', // indigo-700                  -> sky-600
  '#818cf8': '#7dd3fc', // indigo-400 (hover)          -> sky-300
  '#a5b4fc': '#7dd3fc', // indigo-300 (logo/stat dark) -> sky-300
  '#a78bfa': '#38bdf8', // violet-400                  -> sky-400
  '#8b5cf6': '#0ea5e9', // violet-500                  -> sky-500
  '#7c3aed': '#0284c7', // violet-600                  -> sky-600
  '#4c1d95': '#0c4a6e', // violet-900                  -> sky-900
  '#c4b5fd': '#7dd3fc', // violet-300                  -> sky-300
  '#d8b4fe': '#7dd3fc', // purple-300                  -> sky-300
  '#c7d2fe': '#bae6fd', // indigo-200                  -> sky-200
  '#c7d8ff': '#bae6fd', //                             -> sky-200
  '#ddd6fe': '#bae6fd', // violet-200                  -> sky-200
  '#e8e0ff': '#e0f2fe', //                             -> sky-100
  '#ede9fe': '#e0f2fe', // violet-100                  -> sky-100
  '#eef2ff': '#e0f2fe', // indigo-50                   -> sky-100
  '#f0f4ff': '#eff6ff', //                             -> sky-50-ish
  '#faf5ff': '#f0f9ff', // purple-50                   -> sky-50
  '#f8faff': '#f0f9ff', //                             -> sky-50
  '#667eea': '#38bdf8', // gradient indigo             -> sky-400
  '#764ba2': '#0284c7', // gradient purple             -> sky-600
};
for (const [from, to] of Object.entries(HEX)) {
  css = css.replace(new RegExp(from.replace('#', '#'), 'gi'), to);
}

// --- 2. rgba() remap (glow/shadow dùng cùng họ tím) ---
const RGBA = {
  'rgba(99,102,241':  'rgba(56,189,248',  // 6366f1 -> 38bdf8
  'rgba(129,140,248': 'rgba(125,211,252', // 818cf8 -> 7dd3fc
  'rgba(79,70,229':   'rgba(14,165,233',  // 4f46e5 -> 0ea5e9
  'rgba(139,92,246':  'rgba(56,189,248',  // 8b5cf6 -> 38bdf8
  'rgba(167,139,250': 'rgba(125,211,252', // a78bfa -> 7dd3fc
};
for (const [from, to] of Object.entries(RGBA)) {
  css = css.split(from).join(to);
}

// --- 3. Lớp phủ Galaxy (scoped .lh-dark, đặt CUỐI file -> thắng tie specificity) ---
const GALAXY = `
/* ===================== GALAXY RESKIN (personal fork) ===================== */
.lh-dark .lh-nav { background:linear-gradient(180deg,#0b1224,#070b16) !important; border-bottom:1px solid rgba(56,189,248,.16) !important; }
.lh-dark .lh-tab.active, .lh-dark .lh-subtab.active { box-shadow:0 0 0 1px rgba(56,189,248,.45), 0 0 20px rgba(56,189,248,.18) !important; }
.lh-dark .lh-review-logo, .lh-dark .lh-logo { text-shadow:0 0 14px rgba(56,189,248,.35); }
/* 3 chỉ số phát sáng theo màu ngữ nghĩa */
.lh-dark .lh-stat-num.ready { color:#7dd3fc !important; text-shadow:0 0 26px rgba(125,211,252,.55),0 2px 12px rgba(0,0,0,.6) !important; }
.lh-dark .lh-stat-num.due   { color:#7dd3fc !important; text-shadow:0 0 26px rgba(125,211,252,.55),0 2px 12px rgba(0,0,0,.6) !important; }
.lh-dark .lh-stat-num.new   { color:#93c5fd !important; text-shadow:0 0 26px rgba(147,197,253,.5),0 2px 12px rgba(0,0,0,.6) !important; }
.lh-dark .lh-stat-num.total { color:#ffd166 !important; text-shadow:0 0 26px rgba(255,209,102,.5),0 2px 12px rgba(0,0,0,.6) !important; }
/* nút recall/CTA: gradient cyan + glow */
.lh-dark .lh-pill-recall { background:linear-gradient(135deg,#0ea5e9,#7dd3fc) !important; box-shadow:0 4px 18px rgba(56,189,248,.4) !important; }
.lh-dark .lh-pill-btn:hover { border-color:rgba(56,189,248,.5) !important; }
/* thẻ deck trong danh sách: viền cyan mờ khi hover */
.lh-dark .lh-tab-list-item:hover { border-color:rgba(56,189,248,.35) !important; box-shadow:0 0 16px rgba(56,189,248,.12) !important; }
/* galaxy line-icon inline (thay emoji) */
.gqi{display:inline-flex;align-items:center;justify-content:center;vertical-align:-0.15em;width:1.15em;height:1.15em;line-height:0}
.gqi svg{width:100%;height:100%;display:block}
.gqi.lg{width:1.5em;height:1.5em}
.gqi.xl{width:2.2em;height:2.2em}
/* quest-map style: galaxy — hành tinh + deep-space thay vương miện/lâu đài */
.qm-galaxy{background:radial-gradient(130% 90% at 50% 0%,#0d1b3e 0%,#080f22 55%,#04070f 100%)!important;position:relative;}
.qm-galaxy::before{content:"";position:absolute;inset:0;pointer-events:none;opacity:.85;background-image:radial-gradient(1px 1px at 18% 28%,rgba(255,255,255,.75),transparent),radial-gradient(1px 1px at 68% 62%,rgba(255,255,255,.5),transparent),radial-gradient(1px 1px at 42% 82%,rgba(180,220,255,.6),transparent),radial-gradient(1.6px 1.6px at 85% 22%,rgba(255,255,255,.65),transparent),radial-gradient(1px 1px at 55% 12%,rgba(200,225,255,.5),transparent);}
.qm-galaxy .qm-platform,.qm-galaxy .qm-node-number-wrap{display:none!important;}
.qm-gx{position:relative;z-index:4;display:flex;align-items:center;justify-content:center;filter:drop-shadow(0 10px 18px rgba(0,0,0,.4));}
.qm-gx svg{width:104px;height:auto;overflow:visible;display:block;}
.qm-gx-boss svg{width:142px;}
.qm-gx-boss{animation:qmSuperPulse 2.4s ease-in-out infinite;}
@keyframes qmSuperPulse{0%,100%{transform:scale(1);filter:drop-shadow(0 0 22px rgba(255,170,40,.45));}50%{transform:scale(1.06);filter:drop-shadow(0 0 42px rgba(255,190,70,.72));}}
.qm-galaxy .qm-active .qm-gx{animation:qmPlanetPulse 2s ease-in-out infinite;}
@keyframes qmPlanetPulse{0%,100%{filter:drop-shadow(0 0 10px rgba(56,189,248,.5));}50%{filter:drop-shadow(0 0 28px rgba(56,189,248,.9));}}
.qm-galaxy .qm-current-badge{background:rgba(56,189,248,.92)!important;color:#04121f!important;border-color:rgba(125,211,252,.6)!important;}
.qm-galaxy .qm-badge-crown,.qm-galaxy .qm-crown-img{display:none!important;}
.qm-galaxy .qm-completed{outline-color:rgba(74,222,128,.75)!important;}
.qm-galaxy .qm-completed .qm-label-wrap{border-color:rgba(74,222,128,.55)!important;box-shadow:0 0 14px rgba(74,222,128,.28)!important;}`;

css = css.replace(/\s*$/, '') + '\n' + GALAXY + '\n';

fs.writeFileSync(out, '"use strict";\n\nmodule.exports = ' + JSON.stringify(css) + ';\n');
console.log('galaxy-reskin: wrote', path.relative(process.cwd(), out), '(' + css.length + ' bytes)');
