/* ==========================================================
   PH Portal — Front page
   สีทั้งหมดมาจากตัวแปร --ph-* ที่ระบบสร้างจาก "ธีมสี" ในหน้าผู้ดูแล
   (ค่าด้านล่างเป็นค่าสำรองเมื่อไม่มีการตั้งค่า)
   ========================================================== */

:root {
    --ph-primary: #f0857a; --ph-primary-rgb: 240, 133, 122; --ph-primary-dark: #bb4135; --ph-primary-soft: #fde7e4;
    --ph-accent: #f7b38f; --ph-accent-rgb: 247, 179, 143; --ph-accent-ink: #a15933; --ph-accent-soft: #fdefe8; --ph-accent-2: #f9d2c3;
    --ph-bg: #fefaf7; --ph-surface: #fff; --ph-surface-2: #fffcfa; --ph-border: #fce9e3;
    --ph-text: #34201d; --ph-muted: #74625f; --ph-on-primary: #fff;
    --ph-btn-from: #ea5849; --ph-btn-to: #e95b11; --ph-btn-text: #fff;
    --ph-header-bg: rgba(254, 250, 247, .78); --ph-blob-opacity: .5;

    --ph-shadow: 0 1px 2px rgba(var(--ph-primary-rgb), .06), 0 4px 14px rgba(var(--ph-primary-rgb), .07);
    --ph-shadow-hover: 0 20px 44px -12px rgba(var(--ph-primary-rgb), .38);
    --ease-out: cubic-bezier(.22, 1, .36, 1);
    --ease-spring: cubic-bezier(.34, 1.56, .64, 1);
    --radius: 20px;
}
[data-bs-theme="dark"] {
    --ph-shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 18px rgba(0, 0, 0, .28);
    --ph-shadow-hover: 0 22px 44px -12px rgba(0, 0, 0, .7), 0 0 0 1px rgba(var(--ph-primary-rgb), .25);
}
:root, [data-bs-theme="light"], [data-bs-theme="dark"] {
    --bs-body-bg: var(--ph-bg);
    --bs-body-color: var(--ph-text);
    --bs-body-font-family: "Sarabun", system-ui, -apple-system, "Segoe UI", sans-serif;
    --bs-secondary-color: var(--ph-muted);
}

* { -webkit-tap-highlight-color: transparent; }
html { scroll-behavior: smooth; }
body {
    background: var(--ph-bg); color: var(--ph-text);
    min-height: 100vh; display: flex; flex-direction: column; overflow-x: hidden;
    transition: background-color .4s, color .4s;
}
main { flex: 1; }
h1, h2, h3, .brand-title, .footer-name { font-family: "Prompt", var(--bs-body-font-family); }
::selection { background: var(--ph-primary); color: var(--ph-on-primary); }
:focus-visible { outline: 3px solid rgba(var(--ph-primary-rgb), .45); outline-offset: 2px; }

.skip-link {
    position: absolute; top: .5rem; left: .5rem; z-index: 2000;
    background: var(--ph-btn-from); color: var(--ph-btn-text); padding: .5rem 1rem; border-radius: .5rem;
}

/* ---------- Keyframes ---------- */
@keyframes fadeUp { from { opacity: 0; transform: translateY(24px); filter: blur(4px); } to { opacity: 1; transform: none; filter: none; } }
@keyframes float1 { 0%, 100% { transform: translate(0, 0) scale(1); } 33% { transform: translate(40px, -30px) scale(1.08); } 66% { transform: translate(-25px, 20px) scale(.95); } }
@keyframes float2 { 0%, 100% { transform: translate(0, 0) scale(1); } 50% { transform: translate(-50px, 30px) scale(1.12); } }
@keyframes gradientShift { 0%, 100% { background-position: 0% 50%; } 50% { background-position: 100% 50%; } }
@keyframes pulseRing { 0% { box-shadow: 0 0 0 0 rgba(var(--ph-primary-rgb), .55); } 70% { box-shadow: 0 0 0 8px rgba(var(--ph-primary-rgb), 0); } 100% { box-shadow: 0 0 0 0 rgba(var(--ph-primary-rgb), 0); } }
@keyframes wiggle { 0%, 100% { transform: rotate(0) scale(1.1); } 25% { transform: rotate(-10deg) scale(1.15); } 50% { transform: rotate(8deg) scale(1.15); } 75% { transform: rotate(-4deg) scale(1.12); } }
@keyframes shine { from { transform: translateX(-120%) skewX(-20deg); } to { transform: translateX(260%) skewX(-20deg); } }
@keyframes spinIn { from { transform: rotate(-180deg) scale(.4); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-6px); } }

.anim-up { animation: fadeUp .9s var(--ease-out) both; animation-delay: var(--d, 0s); }

/* ---------- Header ---------- */
.site-header {
    position: sticky; top: 0; z-index: 1030; padding: .7rem 0;
    background: var(--ph-header-bg);
    backdrop-filter: saturate(180%) blur(14px); -webkit-backdrop-filter: saturate(180%) blur(14px);
    border-bottom: 1px solid transparent;
    transition: padding .3s var(--ease-out), border-color .3s, box-shadow .3s;
}
.site-header.scrolled { padding: .45rem 0; border-bottom-color: var(--ph-border); box-shadow: 0 8px 24px -16px rgba(var(--ph-primary-rgb), .45); }
.brand { color: var(--ph-text); min-width: 0; }
.brand-text { min-width: 0; }
.brand-logo {
    width: 46px; height: 46px; border-radius: 14px; flex-shrink: 0;
    display: grid; place-items: center; overflow: hidden;
    background: linear-gradient(135deg, var(--ph-primary), var(--ph-accent));
    color: var(--ph-on-primary); font-size: 1.4rem;
    box-shadow: 0 6px 16px -6px rgba(var(--ph-primary-rgb), .8);
    transition: transform .4s var(--ease-spring);
}
.brand-logo.has-img { background: #fff; box-shadow: var(--ph-shadow); }
.brand-logo img { width: 100%; height: 100%; object-fit: contain; padding: 3px; }
.brand:hover .brand-logo { transform: rotate(-8deg) scale(1.06); }
.brand-title, .brand-sub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-title { font-weight: 600; font-size: 1.05rem; color: var(--ph-text); }
.brand-sub { font-size: .8rem; color: var(--ph-muted); }
.header-date { align-items: center; gap: .5rem; font-size: .88rem; color: var(--ph-muted); white-space: nowrap; }

.btn-icon {
    width: 42px; height: 42px; border-radius: 50%; flex-shrink: 0;
    display: grid; place-items: center; padding: 0;
    background: var(--ph-surface); color: var(--ph-primary-dark);
    border: 1px solid var(--ph-border); font-size: 1.05rem;
    transition: transform .3s var(--ease-spring), background-color .2s;
}
.btn-icon:hover { transform: scale(1.1); background: var(--ph-primary-soft); }
.btn-icon i.spin { animation: spinIn .5s var(--ease-spring); }

/* ---------- Hero ---------- */
.hero { position: relative; padding: clamp(2.6rem, 7vw, 4.75rem) 0 clamp(1.8rem, 4vw, 3rem); overflow: hidden; isolation: isolate; }
.hero-bg {
    position: absolute; inset: 0; z-index: -1; pointer-events: none;
    mask-image: linear-gradient(to bottom, #000 55%, transparent 100%);
    -webkit-mask-image: linear-gradient(to bottom, #000 55%, transparent 100%);
}
.blob { position: absolute; border-radius: 50%; filter: blur(64px); opacity: var(--ph-blob-opacity); transition: opacity .4s, background-color .4s; }
.b1 { width: 440px; height: 440px; top: -150px; left: -90px; background: var(--ph-primary); animation: float1 18s ease-in-out infinite; }
.b2 { width: 380px; height: 380px; top: -70px; right: -110px; background: var(--ph-accent); animation: float2 22s ease-in-out infinite; }
.b3 { width: 300px; height: 300px; bottom: -150px; left: 40%; background: var(--ph-accent-2); animation: float1 26s ease-in-out infinite reverse; }
.grid-dots {
    position: absolute; inset: 0;
    background-image: radial-gradient(rgba(var(--ph-primary-rgb), .22) 1.2px, transparent 1.2px);
    background-size: 22px 22px;
    mask-image: radial-gradient(ellipse 60% 70% at 50% 40%, #000 20%, transparent 75%);
    -webkit-mask-image: radial-gradient(ellipse 60% 70% at 50% 40%, #000 20%, transparent 75%);
}

.welcome-pill {
    display: inline-flex; align-items: center; gap: .55rem; max-width: 100%;
    background: var(--ph-surface); color: var(--ph-primary-dark);
    border: 1px solid var(--ph-border);
    padding: .4rem 1.1rem; border-radius: 999px; font-size: .92rem; font-weight: 500;
    box-shadow: var(--ph-shadow);
}
.pulse-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; background: var(--ph-primary); animation: pulseRing 2s infinite; }
.hero-title {
    font-weight: 700; font-size: clamp(1.9rem, 5.5vw, 3.6rem); line-height: 1.3;
    margin: 1.1rem 0 .9rem; overflow-wrap: anywhere;
    background: linear-gradient(100deg, var(--ph-primary-dark), var(--ph-primary), var(--ph-accent), var(--ph-primary-dark));
    background-size: 300% 100%;
    -webkit-background-clip: text; background-clip: text; color: transparent;
    animation: fadeUp .9s var(--ease-out) both, gradientShift 8s ease-in-out infinite;
    animation-delay: var(--d, 0s), 0s;
}
.hero-desc { max-width: 660px; margin: 0 auto 2rem; color: var(--ph-muted); font-size: clamp(.98rem, 1.6vw, 1.08rem); line-height: 1.75; }

/* ---------- Search ---------- */
.search-wrap { max-width: 580px; margin: 0 auto; }
.search-box { position: relative; transition: transform .35s var(--ease-out); }
.search-box:focus-within { transform: translateY(-2px) scale(1.015); }
.search-box > .bi-search {
    position: absolute; left: 1.3rem; top: 50%; transform: translateY(-50%);
    color: var(--ph-primary-dark); font-size: 1.15rem; pointer-events: none; z-index: 2;
}
.search-box .form-control {
    height: 58px; border-radius: 999px; padding: 0 3.2rem 0 3.3rem;
    background: var(--ph-surface); color: var(--ph-text);
    border: 1.5px solid var(--ph-border);
    box-shadow: 0 12px 32px -14px rgba(var(--ph-primary-rgb), .4);
    font-size: 1.02rem; transition: border-color .25s, box-shadow .25s;
}
.search-box .form-control::placeholder { color: var(--ph-muted); opacity: .9; }
.search-box .form-control:focus {
    border-color: var(--ph-primary);
    box-shadow: 0 0 0 5px rgba(var(--ph-primary-rgb), .16), 0 14px 36px -12px rgba(var(--ph-primary-rgb), .45);
}
.search-box .form-control::-webkit-search-cancel-button { display: none; }
.search-kbd {
    position: absolute; right: 1.2rem; top: 50%; transform: translateY(-50%);
    background: var(--ph-surface-2); color: var(--ph-muted);
    border: 1px solid var(--ph-border); border-radius: 6px;
    font-size: .75rem; padding: .1rem .45rem; pointer-events: none; transition: opacity .2s;
}
.search-box:focus-within .search-kbd { opacity: 0; }
.search-clear {
    position: absolute; right: .7rem; top: 50%; transform: translateY(-50%);
    width: 38px; height: 38px; border: 0; border-radius: 50%; z-index: 2;
    background: var(--ph-primary-soft); color: var(--ph-primary-dark);
    animation: spinIn .35s var(--ease-spring);
}

.hero-stats {
    display: flex; flex-wrap: wrap; justify-content: center; gap: .5rem 1.5rem;
    margin-top: 1.3rem; font-size: .9rem; color: var(--ph-muted);
}
.hero-stats i { color: var(--ph-primary-dark); margin-right: .25rem; }
.hero-stats b { color: var(--ph-text); font-weight: 600; }

/* ---------- Section ---------- */
.systems-section { padding-bottom: clamp(2.5rem, 6vw, 4rem); }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: .75rem 1rem; flex-wrap: wrap; margin-bottom: 1.4rem; }
.section-title { display: flex; align-items: center; gap: .75rem; font-size: clamp(1.15rem, 2.4vw, 1.35rem); font-weight: 600; margin: 0; }
.section-icon {
    width: 44px; height: 44px; border-radius: 13px; flex-shrink: 0;
    display: grid; place-items: center; font-size: 1.2rem;
    background: linear-gradient(135deg, var(--ph-btn-from), var(--ph-btn-to)); color: var(--ph-btn-text);
    box-shadow: 0 8px 18px -8px rgba(var(--ph-primary-rgb), .85);
}
.section-count {
    font-size: .85rem; color: var(--ph-muted); white-space: nowrap;
    background: var(--ph-surface); border: 1px solid var(--ph-border);
    padding: .3rem .9rem; border-radius: 999px;
}

/* ---------- Scroll reveal ---------- */
.js .reveal {
    opacity: 0; transform: translateY(28px) scale(.98);
    transition: opacity .7s var(--ease-out), transform .7s var(--ease-out);
    transition-delay: calc(var(--i, 0) * 60ms);
}
.js .reveal.is-visible { opacity: 1; transform: none; }
.system-item[hidden] { display: none !important; }
.system-item.pop { animation: fadeUp .45s var(--ease-out) both; }

/* ---------- System card ---------- */
.system-card {
    --mx: 50%; --my: 50%;
    --tone: var(--ph-primary); --tone-rgb: var(--ph-primary-rgb);
    --tone-soft: var(--ph-primary-soft); --tone-ink: var(--ph-primary-dark);
    position: relative; isolation: isolate; overflow: hidden; height: 100%;
    display: flex; align-items: flex-start; gap: 1rem;
    padding: 1.3rem; border-radius: var(--radius);
    background: var(--ph-surface); border: 1px solid var(--ph-border);
    box-shadow: var(--ph-shadow);
    color: var(--ph-text); text-decoration: none;
    transition: transform .45s var(--ease-spring), box-shadow .35s var(--ease-out), border-color .3s;
}
/* สลับโทนสีหลัก/สีรอง ให้เห็นทั้ง 2 สีบนหน้าเว็บ */
.system-item:nth-child(even) .system-card {
    --tone: var(--ph-accent); --tone-rgb: var(--ph-accent-rgb);
    --tone-soft: var(--ph-accent-soft); --tone-ink: var(--ph-accent-ink);
}
/* เส้นไล่สีด้านบนการ์ด */
.system-card::before {
    content: ""; position: absolute; left: 0; right: 0; top: 0; height: 3px;
    background: linear-gradient(90deg, var(--ph-primary), var(--ph-accent));
    transform: scaleX(0); transform-origin: left; transition: transform .5s var(--ease-out);
}
.card-glow {
    position: absolute; inset: 0; z-index: -1; pointer-events: none;
    background: radial-gradient(260px circle at var(--mx) var(--my), rgba(var(--tone-rgb), .14), transparent 70%);
    opacity: 0; transition: opacity .35s;
}
.system-card::after {
    content: ""; position: absolute; top: 0; bottom: 0; left: 0; width: 45%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .5), transparent);
    transform: translateX(-120%) skewX(-20deg); pointer-events: none;
}
[data-bs-theme="dark"] .system-card::after { background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .06), transparent); }

.system-icon {
    width: 58px; height: 58px; flex-shrink: 0; border-radius: 17px;
    display: grid; place-items: center; font-size: 1.7rem;
    background: var(--tone-soft); color: var(--tone-ink);
    transition: background .35s, color .35s, box-shadow .35s;
}
.system-body { flex: 1; min-width: 0; padding-right: 1.6rem; }
.system-name { font-size: 1.04rem; font-weight: 600; margin: .2rem 0 .3rem; line-height: 1.4; overflow-wrap: anywhere; }
.system-desc { font-size: .88rem; color: var(--ph-muted); margin: 0; line-height: 1.55; }
.system-arrow {
    position: absolute; top: 1.1rem; right: 1.1rem;
    width: 32px; height: 32px; border-radius: 50%;
    display: grid; place-items: center; font-size: .9rem;
    color: var(--tone-ink); background: var(--ph-surface-2); border: 1px solid var(--ph-border);
    transition: transform .4s var(--ease-spring), background-color .3s, color .3s, border-color .3s;
}

/* เอฟเฟกต์เมื่อชี้ (เฉพาะอุปกรณ์ที่มีเมาส์) */
@media (hover: hover) {
    a.system-card:hover { transform: translateY(-8px); box-shadow: var(--ph-shadow-hover); border-color: rgba(var(--tone-rgb), .55); color: var(--ph-text); }
    a.system-card:hover::before { transform: scaleX(1); }
    a.system-card:hover .card-glow { opacity: 1; }
    a.system-card:hover::after { animation: shine .9s var(--ease-out); }
    a.system-card:hover .system-icon {
        background: linear-gradient(135deg, var(--ph-primary), var(--ph-accent)); color: var(--ph-on-primary);
        box-shadow: 0 10px 20px -8px rgba(var(--tone-rgb), .9);
    }
    a.system-card:hover .system-icon i { display: inline-block; animation: wiggle .6s var(--ease-out); }
    a.system-card:hover .system-arrow { background: var(--tone); border-color: var(--tone); color: var(--ph-on-primary); transform: translate(3px, -3px) rotate(45deg); }
    a.system-card:hover .system-arrow i { display: inline-block; transform: rotate(-45deg); }
}
a.system-card:focus-visible { outline: none; border-color: var(--tone); box-shadow: 0 0 0 4px rgba(var(--tone-rgb), .25), var(--ph-shadow-hover); }
a.system-card:focus-visible::before { transform: scaleX(1); }
a.system-card:active { transform: scale(.98); transition-duration: .1s; }

.system-badges { display: flex; flex-wrap: wrap; gap: .35rem; margin-top: .65rem; }
.badge-soft { font-size: .72rem; font-weight: 500; padding: .15rem .6rem; border-radius: 999px; background: var(--ph-primary-soft); color: var(--ph-primary-dark); }
.badge-soft.muted { background: transparent; border: 1px dashed var(--ph-border); color: var(--ph-muted); }
.system-card.is-disabled { cursor: not-allowed; background: var(--ph-surface-2); }
.system-card.is-disabled .system-icon { filter: saturate(.55); opacity: .8; }
.system-card.is-disabled .system-arrow { color: var(--ph-muted); }

mark.hl { background: linear-gradient(transparent 55%, rgba(var(--ph-accent-rgb), .45) 55%); color: inherit; padding: 0; }

/* ---------- Empty ---------- */
.empty-state { text-align: center; padding: 3.5rem 1rem; animation: fadeUp .5s var(--ease-out); }
.empty-icon {
    width: 76px; height: 76px; margin: 0 auto 1rem; border-radius: 50%;
    display: grid; place-items: center; font-size: 2rem;
    background: var(--ph-primary-soft); color: var(--ph-primary-dark);
    animation: bob 2.4s ease-in-out infinite;
}

/* ---------- Footer ---------- */
.site-footer { position: relative; padding: 2.4rem 0 calc(2rem + env(safe-area-inset-bottom)); background: var(--ph-surface); font-size: .9rem; }
.site-footer::before {
    content: ""; position: absolute; top: 0; left: 0; right: 0; height: 3px;
    background: linear-gradient(90deg, var(--ph-primary), var(--ph-accent), var(--ph-accent-2), var(--ph-primary));
    background-size: 300% 100%; animation: gradientShift 10s linear infinite;
}
.footer-name { font-weight: 600; font-size: 1.02rem; }
.footer-en { color: var(--ph-muted); margin: .2rem 0 .8rem; }
.footer-link {
    display: inline-flex; align-items: center; gap: .4rem;
    color: var(--ph-primary-dark); text-decoration: none; font-weight: 500;
    padding: .35rem 1rem; border-radius: 999px; background: var(--ph-primary-soft);
    transition: transform .3s var(--ease-spring);
}
.footer-link:hover { transform: translateY(-2px); color: var(--ph-primary-dark); }
.footer-copy { color: var(--ph-muted); font-size: .82rem; margin-top: 1rem; display: flex; flex-wrap: wrap; justify-content: center; gap: .25rem .6rem; }
.footer-admin { color: var(--ph-muted); text-decoration: none; }
.footer-admin:hover { color: var(--ph-primary-dark); }

/* ---------- Back to top ---------- */
.back-to-top {
    position: fixed; right: calc(1.25rem + env(safe-area-inset-right)); bottom: calc(1.25rem + env(safe-area-inset-bottom)); z-index: 1030;
    width: 50px; height: 50px; border-radius: 50%; border: 0;
    display: grid; place-items: center;
    background: var(--ph-surface); color: var(--ph-primary-dark); font-size: 1.15rem;
    box-shadow: var(--ph-shadow-hover);
    opacity: 0; visibility: hidden; transform: translateY(16px) scale(.8);
    transition: opacity .35s, transform .45s var(--ease-spring), visibility .35s;
}
.back-to-top.show { opacity: 1; visibility: visible; transform: none; }
.back-to-top:hover { transform: translateY(-4px); }
.back-to-top:hover i { animation: bob .8s ease-in-out infinite; }
.progress-ring { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); }
.progress-ring circle {
    fill: none; stroke: var(--ph-primary); stroke-width: 3; stroke-linecap: round;
    stroke-dasharray: 132; stroke-dashoffset: calc(132 - 132 * var(--p, 0));
}

/* ==========================================================
   Responsive
   ========================================================== */

/* Tablet */
@media (max-width: 991.98px) {
    .b1 { width: 340px; height: 340px; }
    .b2 { width: 300px; height: 300px; }
}

/* Mobile */
@media (max-width: 575.98px) {
    .container { --bs-gutter-x: 2rem; }
    .site-header { padding: .55rem 0; }
    .brand { gap: .65rem !important; }
    .brand-logo { width: 40px; height: 40px; border-radius: 12px; font-size: 1.2rem; }
    .brand-title { font-size: .95rem; }
    .brand-sub { font-size: .74rem; }
    .btn-icon { width: 38px; height: 38px; }

    .welcome-pill { font-size: .82rem; padding: .35rem .9rem; }
    .hero-desc { margin-bottom: 1.5rem; }
    .search-box .form-control { height: 52px; padding-left: 3rem; font-size: 1rem; }
    .search-box > .bi-search { left: 1.1rem; }
    .hero-stats { font-size: .84rem; gap: .4rem 1rem; }

    .section-icon { width: 38px; height: 38px; border-radius: 11px; font-size: 1.05rem; }
    .section-count { font-size: .78rem; padding: .25rem .75rem; }

    .system-card { padding: 1rem; gap: .85rem; border-radius: 18px; }
    .system-icon { width: 48px; height: 48px; font-size: 1.4rem; border-radius: 14px; }
    .system-name { font-size: .98rem; margin-top: .1rem; }
    .system-desc { font-size: .84rem; }
    .system-arrow { top: .9rem; right: .9rem; width: 28px; height: 28px; font-size: .8rem; }

    .blob { filter: blur(46px); }
    .b1 { width: 240px; height: 240px; top: -90px; }
    .b2 { width: 210px; height: 210px; }
    .b3 { width: 180px; height: 180px; }

    .back-to-top { width: 44px; height: 44px; right: calc(.9rem + env(safe-area-inset-right)); bottom: calc(.9rem + env(safe-area-inset-bottom)); }
}

/* จอเล็กมาก */
@media (max-width: 359.98px) {
    .container { --bs-gutter-x: 1.5rem; }
    .brand-sub { display: none !important; }
    .system-icon { width: 42px; height: 42px; font-size: 1.25rem; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; }
    .js .reveal { opacity: 1; transform: none; }
}
