/*
Theme Name: Bazar Digitale
Description: Tema WordPress per Bazar Digitale.
Version: 1.0.0
*/

/* =========================================================
   BAZAR DIGITALE - STILE GENERALE
   ========================================================= */

:root{
    --bg:#090711;
    --panel:#130d1d;
    --p:#8b5cf6;
    --p2:#a855f7;
    --txt:#ffffff;
    --muted:#c8bfd2;
    --line:rgba(255,255,255,.10);
}

*{box-sizing:border-box}
html{scroll-behavior:smooth}

body{
    margin:0;
    background:var(--bg);
    color:var(--txt);
    font-family:Arial,Helvetica,sans-serif;
}

a{
    color:inherit;
    text-decoration:none;
}

.wrap{
    max-width:1180px;
    margin:auto;
    padding:0 24px;
}

header{
    position:sticky;
    top:0;
    z-index:20;
    background:rgba(9,7,17,.93);
    backdrop-filter:blur(14px);
    border-bottom:1px solid var(--line);
}

.nav{
    height:76px;
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:20px;
}

.logo{
    font-weight:900;
    font-size:21px;
}

.logo span{color:var(--p)}

nav{
    display:flex;
    gap:25px;
    font-size:14px;
    font-weight:700;
    color:#ddd5e7;
}

.bazzle,
.btn{
    display:inline-block;
    background:linear-gradient(135deg,var(--p),var(--p2));
    padding:12px 17px;
    border-radius:11px;
    font-weight:800;
}

.hero{
    min-height:620px;
    display:flex;
    align-items:center;
    position:relative;
    overflow:hidden;
}

.hero:before{
    content:"";
    position:absolute;
    right:-180px;
    top:-180px;
    width:700px;
    height:700px;
    background:radial-gradient(circle,#8b5cf64d,transparent 68%);
}

.hero-grid{
    display:grid;
    grid-template-columns:1.05fr .95fr;
    gap:50px;
    align-items:center;
}

.kicker{
    color:#c2a7ff;
    text-transform:uppercase;
    letter-spacing:2px;
    font-size:12px;
    font-weight:800;
}

h1{
    font-size:clamp(45px,6vw,76px);
    line-height:.98;
    letter-spacing:-3px;
    margin:16px 0 24px;
}

h1 span{color:var(--p)}

.lead{
    max-width:600px;
    color:var(--muted);
    font-size:18px;
    line-height:1.65;
}

.actions{
    display:flex;
    gap:12px;
    flex-wrap:wrap;
    margin-top:28px;
}

.secondary{
    background:#ffffff08;
    border:1px solid var(--line);
}

.hero-art{
    height:420px;
    border:1px solid var(--line);
    border-radius:28px;
    background:linear-gradient(145deg,#1a1025,#0d0a13);
    display:flex;
    align-items:center;
    justify-content:center;
}

.device{
    width:70%;
    aspect-ratio:1.2;
    border-radius:26px;
    background:linear-gradient(145deg,#292031,#0c0910);
    border:1px solid #ffffff22;
    transform:rotate(-7deg);
    display:flex;
    align-items:center;
    justify-content:center;
    text-align:center;
    font-weight:900;
    font-size:28px;
    box-shadow:0 35px 70px #0009;
}

.device span{color:var(--p2)}

.section{padding:75px 0}

.steps,
.grid4{
    display:grid;
    grid-template-columns:repeat(4,1fr);
    gap:17px;
}

.step,
.card{
    background:var(--panel);
    border:1px solid var(--line);
    border-radius:16px;
    overflow:hidden;
}

.step{padding:22px}

.num{
    width:32px;
    height:32px;
    border-radius:50%;
    display:grid;
    place-items:center;
    background:var(--p);
    font-weight:900;
    margin-bottom:12px;
}

.step p,
.muted{
    color:var(--muted);
    line-height:1.6;
}

.cta{
    padding:55px 30px;
    text-align:center;
    border-radius:25px;
    background:linear-gradient(135deg,#221038,#110a18);
    border:1px solid #8b5cf640;
}

.cta h2{
    font-size:39px;
    margin:0 0 10px;
}

.login-shell{
    min-height:calc(100vh - 77px);
    display:grid;
    place-items:center;
    padding:50px 20px;
}

.login-card{
    width:min(440px,100%);
    background:var(--panel);
    border:1px solid var(--line);
    border-radius:22px;
    padding:34px;
}

.field{margin:16px 0}

.field input{
    width:100%;
    padding:13px;
    border-radius:10px;
    border:1px solid #ffffff20;
    background:#0b0910;
    color:#fff;
}

.notice{
    padding:12px;
    border-radius:10px;
    background:#3a1420;
    color:#ffb7c6;
    margin-bottom:15px;
}

.private-banner{
    padding:22px;
    border:1px solid #8b5cf633;
    background:#8b5cf60d;
    border-radius:16px;
    margin-bottom:28px;
}

.cover{
    height:180px;
    background:linear-gradient(145deg,#271637,#100b16);
    display:flex;
    align-items:center;
    justify-content:center;
    color:#bfa5ff;
    font-weight:900;
}

.card-body{padding:16px}
.tag{color:#bfa5ff;font-size:12px}
.price{font-size:23px;font-weight:900}
.coin{color:#cbb4ff;font-size:13px;margin-top:8px}

.empty{
    padding:30px;
    border:1px dashed #ffffff25;
    border-radius:16px;
    color:var(--muted);
}

footer{
    border-top:1px solid var(--line);
    padding:40px 0;
    color:var(--muted);
}

/* =========================================================
   BAZZLE DASHBOARD - VERSIONE PULITA
   ========================================================= */

.bazzle-dashboard{
    max-width:1050px;
    margin:55px auto;
    padding:0 24px;
    color:#ffffff !important;
}

/* Header */

.bazzle-dashboard-header{
    display:flex;
    align-items:flex-end;
    justify-content:space-between;
    gap:30px;
    margin-bottom:28px;
}

.bazzle-dashboard-header > div:first-child{
    flex:1;
}

.bazzle-dashboard-header .kicker{
    color:#cbb7ff !important;
    margin-bottom:10px;
}

.bazzle-dashboard-header h1{
    margin:0 0 8px;
    color:#ffffff !important;
    font-size:clamp(36px,5vw,52px);
    line-height:1.05;
    letter-spacing:-2px;
}

.bazzle-dashboard-header p{
    margin:0;
    color:#c8bfd2 !important;
    opacity:1 !important;
}

/* Saldo */

.bazzle-dashboard-header .bazzle-balance{
    min-width:235px;
    margin:0;
    padding:20px 22px;
    display:flex;
    flex-direction:column;
    align-items:flex-start;
    gap:4px;
    border-radius:18px;
    background:linear-gradient(135deg,#211333,#120c19) !important;
    border:1px solid rgba(139,92,246,.28);
    color:#ffffff !important;
    box-shadow:0 18px 45px rgba(0,0,0,.25);
}

.bazzle-dashboard-header .balance-label{
    color:#c8bfd2 !important;
    font-size:10px;
    font-weight:800;
    letter-spacing:1.8px;
    margin:0;
}

.bazzle-dashboard-header .bazzle-balance strong{
    color:#ffffff !important;
    font-size:32px;
    line-height:1.1;
}

.bazzle-dashboard-header .balance-small{
    color:#aaa0b5 !important;
    font-size:11px;
    margin:0;
}

/* Card */

.bazzle-dashboard .bazzle-card{
    background:linear-gradient(145deg,#15101f,#0f0b16) !important;
    border:1px solid rgba(255,255,255,.09) !important;
    border-radius:22px;
    padding:30px;
    margin-bottom:24px;
    color:#ffffff !important;
    box-shadow:0 20px 50px rgba(0,0,0,.22);
}

.bazzle-card-title{
    display:flex;
    align-items:flex-start;
    gap:13px;
    margin-bottom:24px;
}

.bazzle-card-title .card-icon{
    width:42px;
    height:42px;
    flex:0 0 42px;
    display:flex;
    align-items:center;
    justify-content:center;
    border-radius:12px;
    background:rgba(139,92,246,.16);
    color:#ffffff !important;
    font-size:20px;
}

.bazzle-card-title h2{
    margin:0 0 4px;
    color:#ffffff !important;
    font-size:23px;
    line-height:1.2;
    letter-spacing:-.4px;
}

.bazzle-card-title p{
    margin:0;
    color:#c2b8cc !important;
    font-size:14px;
    opacity:1 !important;
}

/* Profilo */

.bazzle-profile-grid{
    display:grid;
    grid-template-columns:repeat(2,minmax(0,1fr));
    gap:12px;
}

.bazzle-profile-grid .bazzle-info{
    display:flex;
    flex-direction:column;
    align-items:flex-start;
    justify-content:center;
    gap:5px;
    padding:15px 17px;
    min-height:68px;
    border:1px solid rgba(255,255,255,.08) !important;
    border-radius:14px;
    background:rgba(255,255,255,.035) !important;
    color:#ffffff !important;
}

.bazzle-profile-grid .bazzle-info span{
    color:#b7adbf !important;
    font-size:10px;
    font-weight:800;
    letter-spacing:1.1px;
    text-transform:uppercase;
    opacity:1 !important;
}

.bazzle-profile-grid .bazzle-info strong{
    color:#ffffff !important;
    font-size:15px;
    font-weight:700;
    text-align:left;
    word-break:break-word;
}

/* Statistiche */

.bazzle-stats-grid{
    display:grid;
    grid-template-columns:repeat(4,minmax(0,1fr));
    gap:13px;
}

.bazzle-stat{
    padding:20px 17px;
    border-radius:16px;
    background:rgba(255,255,255,.045) !important;
    border:1px solid rgba(255,255,255,.08) !important;
    color:#ffffff !important;
    transition:transform .2s ease,border-color .2s ease,background .2s ease;
}

.bazzle-stat:hover{
    transform:translateY(-2px);
    background:rgba(139,92,246,.08) !important;
    border-color:rgba(139,92,246,.32) !important;
}

.bazzle-stat span{
    display:block;
    color:#c0b6c9 !important;
    font-size:10px;
    font-weight:800;
    line-height:1.3;
    letter-spacing:1px;
    text-transform:uppercase;
    margin-bottom:10px;
    opacity:1 !important;
}

.bazzle-stat strong{
    display:block;
    color:#ffffff !important;
    font-size:31px;
    line-height:1;
    font-weight:900;
    margin-bottom:7px;
}

.bazzle-stat small{
    display:block;
    color:#aaa0b5 !important;
    font-size:11px;
}

/* Movimenti */

.bazzle-movements{
    display:flex;
    flex-direction:column;
    gap:11px;
}

.bazzle-movement{
    display:flex;
    align-items:center;
    gap:15px;
    padding:16px 17px;
    border-radius:15px;
    background:rgba(255,255,255,.045) !important;
    border:1px solid rgba(255,255,255,.08) !important;
    color:#ffffff !important;
    transition:transform .2s ease,background .2s ease,border-color .2s ease;
}

.bazzle-movement:hover{
    transform:translateX(2px);
    background:rgba(139,92,246,.065) !important;
    border-color:rgba(139,92,246,.30) !important;
}

.movement-icon{
    width:44px;
    height:44px;
    flex:0 0 44px;
    display:flex;
    align-items:center;
    justify-content:center;
    border-radius:12px;
    background:rgba(139,92,246,.15) !important;
    color:#ffffff !important;
    font-size:18px;
}

.movement-main{
    flex:1;
    min-width:0;
}

.movement-main strong{
    display:block;
    color:#ffffff !important;
    font-size:16px;
    font-weight:800;
    line-height:1.3;
    margin-bottom:4px;
}

.movement-main span{
    display:block;
    color:#c0b6c9 !important;
    font-size:13px;
    line-height:1.4;
    opacity:1 !important;
}

.movement-main small{
    display:block;
    margin-top:5px;
    color:#9f95a8 !important;
    font-size:11px;
}

/* Valori */

.movement-value{
    flex:0 0 auto;
    min-width:125px;
    text-align:right;
    color:#ffffff !important;
}

.movement-value strong{
    display:block;
    color:#c4a5ff !important;
    font-size:19px;
    font-weight:900;
    line-height:1.2;
}

.movement-value span{
    display:block;
    color:#aaa0b5 !important;
    font-size:10px;
    font-weight:700;
    margin-top:3px;
}

.movement-value small{
    display:block;
    color:#9a8fa4 !important;
    font-size:10px;
    margin-top:4px;
}

.bazzle-card:nth-of-type(3) .movement-value strong{
    color:#ffffff !important;
}

/* Vuoti */

.bazzle-empty{
    padding:35px 20px;
    text-align:center;
    border:1px dashed rgba(255,255,255,.15);
    border-radius:16px;
    background:rgba(255,255,255,.025);
}

.bazzle-empty .empty-icon{
    font-size:28px;
    margin-bottom:8px;
}

.bazzle-empty h3{
    margin:0 0 6px;
    color:#ffffff !important;
    font-size:17px;
}

.bazzle-empty p{
    margin:0;
    color:#b7adbf !important;
    font-size:13px;
}

/* Errori */

.bazzle-error{
    max-width:700px;
    margin:60px auto;
    padding:30px;
    border:1px solid rgba(255,255,255,.10);
    border-radius:20px;
    background:#130d1d;
    color:#ffffff !important;
}

/* Footer dashboard */

.bazzle-dashboard-footer{
    display:flex;
    justify-content:center;
    margin-top:8px;
    padding-bottom:25px;
    color:#c8bfd2 !important;
}

.bazzle-dashboard-footer .btn.secondary{
    color:#ffffff !important;
    border:1px solid rgba(255,255,255,.12);
    background:rgba(255,255,255,.045);
}

.bazzle-dashboard-footer .btn.secondary:hover{
    background:rgba(139,92,246,.12);
    border-color:rgba(139,92,246,.35);
}

/* =========================================================
   REGOLE DI SICUREZZA CONTRO CONFLITTI DI TEMA
   ========================================================= */

.bazzle-dashboard p,
.bazzle-dashboard span,
.bazzle-dashboard small,
.bazzle-dashboard label{
    opacity:1 !important;
}

.bazzle-dashboard .muted{
    color:#c8bfd2 !important;
}

.bazzle-dashboard h1,
.bazzle-dashboard h2,
.bazzle-dashboard h3,
.bazzle-dashboard h4,
.bazzle-dashboard strong{
    color:#ffffff !important;
}

/* Responsive */

@media(max-width:900px){
    nav{display:none}
    .hero-grid{grid-template-columns:1fr}
    .steps,.grid4{grid-template-columns:repeat(2,1fr)}
}

@media(max-width:850px){
    .bazzle-dashboard-header{
        align-items:stretch;
        flex-direction:column;
    }

    .bazzle-dashboard-header .bazzle-balance{
        width:100%;
    }

    .bazzle-stats-grid{
        grid-template-columns:repeat(2,minmax(0,1fr));
    }
}

@media(max-width:600px){
    .wrap{padding:0 17px}
    .hero{padding:65px 0}
    .hero-art{height:270px}
    .steps,.grid4{grid-template-columns:1fr}
    .section{padding:60px 0}

    .bazzle-dashboard{
        margin:35px auto;
        padding:0 15px;
    }

    .bazzle-card{
        padding:21px;
        border-radius:18px;
    }

    .bazzle-dashboard-header h1{
        font-size:34px;
        letter-spacing:-1.2px;
    }

    .bazzle-card-title h2{
        font-size:20px;
    }

    .bazzle-profile-grid{
        grid-template-columns:1fr;
    }

    .bazzle-stats-grid{
        gap:9px;
    }

    .bazzle-stat{
        padding:16px 13px;
    }

    .bazzle-stat strong{
        font-size:25px;
    }

    .bazzle-stat span{
        font-size:9px;
        letter-spacing:.7px;
    }

    .bazzle-movement{
        align-items:flex-start;
        padding:14px;
    }

    .movement-icon{
        width:38px;
        height:38px;
        flex-basis:38px;
        font-size:16px;
    }

    .movement-main strong{
        font-size:14px;
    }

    .movement-main span{
        font-size:12px;
    }

    .movement-value{
        min-width:88px;
    }

    .movement-value strong{
        font-size:15px;
    }

    .movement-value span{
        font-size:9px;
    }
}

/* =========================================================
   PROTEZIONE ADMIN
   ========================================================= */


function bazzle_is_admin_gestionale() {

    if ( ! is_user_logged_in() ) {
        return false;
    }

    $user = wp_get_current_user();

    if ( ! $user || ! $user->exists() ) {
        return false;
    }

    if ( ! current_user_can( 'manage_options' ) ) {
        return false;
    }

    if ( strtolower( $user->user_email ) !== 'admin@bazar-digitale.it' ) {
        return false;
    }

    return true;
}


/* =========================================================
   REGISTRA MENU
   ========================================================= */

function bazzle_registra_menu_admin() {

    if ( ! bazzle_is_admin_gestionale() ) {
        return;
    }

    add_menu_page(
        'Bazar Digitale',
        'Bazar Digitale',
        'manage_options',
        'bazzle-admin',
        'bazzle_pagina_admin',
        'dashicons-games',
        25
    );
}

add_action( 'admin_menu', 'bazzle_registra_menu_admin' );



/* =========================================================
   FUNZIONE PRINCIPALE BASEROW ADMIN
   ========================================================= */



function bazzle_baserow_request( $method, $table_id, $row_id = null, $data = array() ) {

    if (
        ! defined( 'BAZZLE_BASEROW_URL' ) ||
        ! defined( 'BAZZLE_BASEROW_TOKEN' )
    ) {
        return new WP_Error(
            'baserow_config',
            'Configurazione Baserow mancante.'
        );
    }

    $url = rtrim( BAZZLE_BASEROW_URL, '/' )
        . '/api/database/rows/table/'
        . intval( $table_id );

    if ( $row_id ) {
        $url .= '/' . intval( $row_id );
    }

    $url .= '/?user_field_names=true';

    $args = array(
        'method'  => strtoupper( $method ),
        'timeout' => 20,
        'headers' => array(
            'Authorization' => 'Token ' . BAZZLE_BASEROW_TOKEN,
            'Content-Type'  => 'application/json',
        ),
    );

    if ( ! empty( $data ) ) {
        $args['body'] = wp_json_encode( $data );
    }

    $response = wp_remote_request( $url, $args );

    if ( is_wp_error( $response ) ) {
        return $response;
    }

    $status = wp_remote_retrieve_response_code( $response );
    $body   = wp_remote_retrieve_body( $response );

    $decoded = json_decode( $body, true );

    if ( $status < 200 || $status >= 300 ) {

        return new WP_Error(
            'baserow_error',
            'Baserow ha restituito HTTP ' . $status,
            array(
                'response' => $decoded,
            )
        );
    }

    return $decoded;
}



/* =========================================================
   PAGINA AMMINISTRATORE
   ========================================================= */

function bazzle_pagina_admin() {

    if ( ! bazzle_is_admin_gestionale() ) {

        wp_die(
            'Non sei autorizzato ad accedere a questa pagina.'
        );
    }

    ?>

    <div class="wrap">

        <h1>🎮 Bazar Digitale</h1>

        <p>
            Gestionale interno Bazar Digitale.
        </p>

        <hr>

        <div style="
            display:grid;
            grid-template-columns:repeat(3,1fr);
            gap:20px;
            max-width:1000px;
        ">

            <a href="<?php echo esc_url(
                admin_url( 'admin.php?page=bazzle-clienti' )
            ); ?>"
            style="
                display:block;
                padding:30px;
                background:#fff;
                border:1px solid #ddd;
                border-radius:12px;
                text-decoration:none;
            ">

                <h2>👤 Clienti</h2>

                <p>
                    Gestisci anagrafiche, crediti e account.
                </p>

            </a>


            <a href="<?php echo esc_url(
                admin_url( 'admin.php?page=bazzle-giochi' )
            ); ?>"
            style="
                display:block;
                padding:30px;
                background:#fff;
                border:1px solid #ddd;
                border-radius:12px;
                text-decoration:none;
            ">

                <h2>🎮 Giochi</h2>

                <p>
                    Gestisci catalogo, prezzi, stock e permute.
                </p>

            </a>


            <a href="<?php echo esc_url(
                admin_url( 'admin.php?page=bazzle-movimenti' )
            ); ?>"
            style="
                display:block;
                padding:30px;
                background:#fff;
                border:1px solid #ddd;
                border-radius:12px;
                text-decoration:none;
            ">

                <h2>💰 Movimenti</h2>

                <p>
                    Registra acquisti, permute, bonus e correzioni.
                </p>

            </a>

        </div>

    </div>

    <?php
}


/* =========================================================
   GESTIONE SEZIONI BASEROW
   ========================================================= */



function bazzle_registra_sottomenu_admin() {

    if ( ! bazzle_is_admin_gestionale() ) {
        return;
    }

    add_submenu_page(
        'bazzle-admin',
        'Clienti',
        '👤 Clienti',
        'manage_options',
        'bazzle-clienti',
        'bazzle_admin_clienti'
    );

    add_submenu_page(
        'bazzle-admin',
        'Giochi',
        '🎮 Giochi',
        'manage_options',
        'bazzle-giochi',
        'bazzle_admin_giochi'
    );

    add_submenu_page(
        'bazzle-admin',
        'Movimenti',
        '💰 Movimenti',
        'manage_options',
        'bazzle-movimenti',
        'bazzle_admin_movimenti'
    );
}

add_action(
    'admin_menu',
    'bazzle_registra_sottomenu_admin'
);


/* =========================================================
   RICERCA CLIENTI
   ========================================================= */

.bazzle-clienti-search {
    margin: 22px 0 10px;
}

.bazzle-clienti-search-main {
    display: grid;
    grid-template-columns: minmax(250px, 1fr) 170px auto auto;
    gap: 10px;
    align-items: center;
}

.bazzle-clienti-search input,
.bazzle-clienti-search select {
    width: 100%;
    min-height: 46px;
    padding: 0 14px;

    border-radius: 11px;

    border: 1px solid rgba(255,255,255,.10);

    background: rgba(255,255,255,.045);

    color: #fff;

    font-family: inherit;
    font-size: 14px;

    outline: none;
}

.bazzle-clienti-search input::placeholder {
    color: #8f8699;
}

.bazzle-clienti-search input:focus,
.bazzle-clienti-search select:focus {
    border-color: rgba(139,92,246,.55);

    background: rgba(139,92,246,.06);

    box-shadow:
        0 0 0 3px rgba(139,92,246,.10);
}

.bazzle-clienti-search select {
    cursor: pointer;
}

.bazzle-clienti-search select option {
    background: #15101f;
    color: #fff;
}

.bazzle-clienti-search .bazzle-admin-btn {
    min-height: 46px;
    white-space: nowrap;
}

.bazzle-clienti-results {
    margin: 12px 0 18px;

    color: #aaa0b4;

    font-size: 12px;
    font-weight: 700;
    letter-spacing: .4px;
}

.bazzle-clienti-no-results {
    padding: 30px 20px;

    text-align: center;

    border: 1px dashed rgba(255,255,255,.12);

    border-radius: 16px;

    background: rgba(255,255,255,.02);
}

.bazzle-clienti-no-results strong {
    display: block;

    margin-bottom: 7px;

    color: #fff;

    font-size: 17px;
}

.bazzle-clienti-no-results p {
    margin: 0 0 18px;

    color: #91879d;

    font-size: 13px;
}


/* MOBILE */

@media (max-width: 750px) {

    .bazzle-clienti-search-main {
        grid-template-columns: 1fr 1fr;
    }

    .bazzle-clienti-search input {
        grid-column: 1 / -1;
    }

}


@media (max-width: 480px) {

    .bazzle-clienti-search-main {
        grid-template-columns: 1fr;
    }

    .bazzle-clienti-search input {
        grid-column: auto;
    }

    .bazzle-clienti-search .bazzle-admin-btn {
        width: 100%;
    }

}