<?php
// OZORWebFactory — Admin Panel v4
session_start();
$config = json_decode(file_get_contents(__DIR__ . '/config.json'), true);
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['password'])) {
    if (hash('sha256', $_POST['password']) === $config['adminHash']) {
        $_SESSION['auth'] = true; header('Location: admin.php'); exit;
    }
    $error = 'Parolă incorectă.';
}
if (isset($_POST['logout'])) { session_destroy(); header('Location: admin.php'); exit; }

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['auth'])) {
    $allowedMimes = ['image/jpeg','image/png','image/webp'];
    if (!is_dir(__DIR__ . '/uploads')) mkdir(__DIR__ . '/uploads', 0755, true);
    if (!empty($_FILES['hero_upload']['tmp_name']) && $_FILES['hero_upload']['error'] === UPLOAD_ERR_OK) {
        $mime = mime_content_type($_FILES['hero_upload']['tmp_name']);
        if (in_array($mime, $allowedMimes) && $_FILES['hero_upload']['size'] < 5*1024*1024) {
            $ext = explode('/', $mime)[1] === 'jpeg' ? 'jpg' : explode('/', $mime)[1];
            foreach (glob(__DIR__ . '/uploads/hero.*') as $old) unlink($old);
            move_uploaded_file($_FILES['hero_upload']['tmp_name'], __DIR__ . '/uploads/hero.' . $ext);
            $config['heroPhoto'] = 'uploads/hero.' . $ext . '?v=' . time();
        }
    }
    for ($i = 0; $i < 4; $i++) {
        $key = "gallery_upload_$i";
        if (!empty($_FILES[$key]['tmp_name']) && $_FILES[$key]['error'] === UPLOAD_ERR_OK) {
            $mime = mime_content_type($_FILES[$key]['tmp_name']);
            if (in_array($mime, $allowedMimes) && $_FILES[$key]['size'] < 5*1024*1024) {
                $ext = explode('/', $mime)[1] === 'jpeg' ? 'jpg' : explode('/', $mime)[1];
                foreach (glob(__DIR__ . '/uploads/gallery-' . $i . '.*') as $old) unlink($old);
                move_uploaded_file($_FILES[$key]['tmp_name'], __DIR__ . '/uploads/gallery-' . $i . '.' . $ext);
                $config['galleryPhotos'][$i] = 'uploads/gallery-' . $i . '.' . $ext . '?v=' . time();
            }
        }
    }
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['auth']) && !empty($_FILES['logo']['tmp_name']) && $_FILES['logo']['error'] === UPLOAD_ERR_OK) {
    $allowed = ['image/png','image/jpeg','image/svg+xml','image/webp'];
    $mime = mime_content_type($_FILES['logo']['tmp_name']);
    if (in_array($mime, $allowed) && $_FILES['logo']['size'] < 2*1024*1024) {
        $ext = $mime === 'image/svg+xml' ? 'svg' : (explode('/',$mime)[1] === 'jpeg' ? 'jpg' : explode('/',$mime)[1]);
        foreach (glob(__DIR__ . '/logo.*') as $old) unlink($old);
        move_uploaded_file($_FILES['logo']['tmp_name'], __DIR__ . '/logo.' . $ext);
        $config['hasLogo'] = true;
        file_put_contents(__DIR__ . '/config.json', json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
    }
}
if (isset($_POST['deleteLogo']) && isset($_SESSION['auth'])) {
    foreach (glob(__DIR__ . '/logo.*') as $old) unlink($old);
    $config['hasLogo'] = false;
    file_put_contents(__DIR__ . '/config.json', json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['auth']) && isset($_POST['save'])) {
    $config['businessName'] = trim($_POST['businessName'] ?? $config['businessName']);
    $config['tagline']      = trim($_POST['tagline'] ?? $config['tagline']);
    $config['phone']        = trim($_POST['phone'] ?? $config['phone']);
    $config['address']      = trim($_POST['address'] ?? $config['address']);
    $config['hours']        = trim($_POST['hours'] ?? $config['hours']);
    $config['instagram']    = trim($_POST['instagram'] ?? '');
    $config['facebook']     = trim($_POST['facebook'] ?? '');
    $config['tiktok']       = trim($_POST['tiktok'] ?? '');
    $config['youtube']      = trim($_POST['youtube'] ?? '');
    $config['heroTitle']    = trim($_POST['heroTitle'] ?? $config['heroTitle']);
    $config['heroDesc']     = trim($_POST['heroDesc'] ?? $config['heroDesc']);
    $config['aboutText1']   = trim($_POST['aboutText1'] ?? $config['aboutText1']);
    $config['aboutText2']   = trim($_POST['aboutText2'] ?? $config['aboutText2']);
    $config['accentColor']  = trim($_POST['accentColor'] ?? '#C8862A');
    $config['heroPhoto']    = trim($_POST['heroPhoto'] ?? $config['heroPhoto']);
    $days = ['mon','tue','wed','thu','fri','sat','sun'];
    foreach ($days as $d) $config['schedule'][$d] = trim($_POST["schedule_$d"] ?? '10:00–20:00');
    $config['offers'] = [];
    for ($i = 0; $i < 10; $i++) {
        if (!empty($_POST["offer_title_$i"])) $config['offers'][] = ['title'=>trim($_POST["offer_title_$i"]),'desc'=>trim($_POST["offer_desc_$i"]??''),'valid'=>trim($_POST["offer_valid_$i"]??'')];
    }
    for ($i = 0; $i < count($config['services']); $i++) {
        if (isset($_POST["svc_name_$i"])) { $config['services'][$i]['name'] = trim($_POST["svc_name_$i"]); $config['services'][$i]['desc'] = trim($_POST["svc_desc_$i"]); }
    }
    for ($i = 0; $i < count($config['galleryPhotos']); $i++) {
        if (isset($_POST["gallery_$i"])) $config['galleryPhotos'][$i] = trim($_POST["gallery_$i"]);
    }
    for ($i = 0; $i < count($config['reviews']); $i++) {
        if (isset($_POST["rev_name_$i"])) { $config['reviews'][$i]['name'] = trim($_POST["rev_name_$i"]); $config['reviews'][$i]['text'] = trim($_POST["rev_text_$i"]); $config['reviews'][$i]['ago'] = trim($_POST["rev_ago_$i"]??''); }
    }
    if (!empty($_POST['newPassword']) && strlen($_POST['newPassword']) >= 6) $config['adminHash'] = hash('sha256', $_POST['newPassword']);
    file_put_contents(__DIR__ . '/config.json', json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
    $saved = true;
}
$auth = isset($_SESSION['auth']);
?>
<!DOCTYPE html>
<html lang="ro">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin — <?= htmlspecialchars($config['businessName']) ?></title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
<style>
:root {
  --bg: #0C0E18; --surface: #141724; --card: #1A1E30;
  --border: #252B42; --text: #E8EAF0; --muted: #7A829E; --dim: #4A5268;
  --accent: <?php echo $config['accentColor'] ?? '#C8862A'; ?>;
  --accent-bg: <?php
    $c = ltrim($config['accentColor'] ?? '#C8862A', '#');
    $r = hexdec(substr($c,0,2)); $g = hexdec(substr($c,2,2)); $b = hexdec(substr($c,4,2));
    echo "rgba($r,$g,$b,0.15)";
  ?>;
  --success: #3DBA6F; --error: #E74C3C; --radius: 14px;
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; }
body { font-family: 'Inter', sans-serif; background: var(--bg); color: var(--text); line-height: 1.6; min-height: 100vh; }
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 2rem; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 22px; padding: 3rem 2.5rem; width: 100%; max-width: 400px; text-align: center; }
.login-icon { width: 52px; height: 52px; background: var(--accent-bg); border-radius: 14px; display: grid; place-items: center; margin: 0 auto 1.5rem; }
.login-icon svg { width: 26px; height: 26px; stroke: var(--accent); fill: none; stroke-width: 1.8; }
.login-card h1 { font-size: 1.35rem; font-weight: 600; margin-bottom: .35rem; }
.login-card p { color: var(--muted); font-size: .85rem; margin-bottom: 2rem; }
.login-card input[type=password] { width: 100%; padding: 1rem 1.2rem; background: var(--bg); border: 1.5px solid var(--border); border-radius: 12px; color: var(--text); font-size: .95rem; outline: none; margin-bottom: 1rem; transition: border-color .2s; font-family: inherit; }
.login-card input[type=password]:focus { border-color: var(--accent); }
.btn-login { width: 100%; padding: 1rem; background: var(--accent); color: #fff; border: none; border-radius: 12px; font-size: .92rem; font-weight: 600; cursor: pointer; transition: opacity .2s; font-family: inherit; }
.btn-login:hover { opacity: .88; }
.login-error { background: rgba(231,76,60,.1); border: 1px solid rgba(231,76,60,.3); color: var(--error); padding: .85rem 1rem; border-radius: 10px; font-size: .82rem; margin-bottom: 1rem; text-align: left; }
.toast { position: fixed; top: 1.5rem; right: 1.5rem; z-index: 9999; background: var(--success); color: #fff; padding: .9rem 1.4rem; border-radius: 12px; font-size: .85rem; font-weight: 500; box-shadow: 0 8px 30px rgba(0,0,0,.3); display: flex; align-items: center; gap: .7rem; animation: toastIn .3s ease forwards; }
.toast svg { width: 18px; height: 18px; stroke: #fff; fill: none; stroke-width: 2; }
@keyframes toastIn { from { transform: translateX(110%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
.admin-layout { display: block; min-height: 100vh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); padding: 1.5rem 1.25rem; display: flex; flex-direction: column; position: fixed; top: 0; left: 0; width: 252px; height: 100vh; overflow-y: auto; }
.sb-brand { display: flex; align-items: center; gap: .65rem; margin-bottom: .4rem; }
.sb-icon { width: 32px; height: 32px; background: var(--accent-bg); border-radius: 9px; display: grid; place-items: center; flex-shrink: 0; }
.sb-icon svg { width: 16px; height: 16px; stroke: var(--accent); fill: none; stroke-width: 2; }
.sb-name { font-size: .88rem; font-weight: 600; }
.sb-site { font-size: .72rem; color: var(--muted); margin-bottom: 1.75rem; padding-bottom: 1.25rem; border-bottom: 1px solid var(--border); }
.nav-label { font-size: .6rem; text-transform: uppercase; letter-spacing: .12em; color: var(--dim); margin: 1.25rem .2rem .5rem; }
.nav-item { display: flex; align-items: center; gap: .7rem; padding: .62rem .9rem; border-radius: 10px; font-size: .82rem; color: var(--muted); text-decoration: none; margin-bottom: .08rem; transition: all .15s; }
.nav-item:hover { background: var(--card); color: var(--text); }
.nav-item svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.sb-footer { margin-top: auto; padding-top: 1.25rem; border-top: 1px solid var(--border); }
.btn-preview-sb { display: flex; align-items: center; gap: .6rem; padding: .65rem .9rem; border-radius: 10px; font-size: .8rem; color: var(--muted); text-decoration: none; margin-bottom: .5rem; transition: all .15s; }
.btn-preview-sb:hover { background: var(--card); color: var(--text); }
.btn-preview-sb svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 1.8; }
.btn-logout { width: 100%; padding: .7rem; background: transparent; border: 1px solid var(--border); color: var(--muted); border-radius: 10px; font-size: .78rem; cursor: pointer; transition: all .2s; font-family: inherit; }
.btn-logout:hover { border-color: var(--error); color: var(--error); }
.main-content { margin-left: 252px; padding: 2rem; max-width: 880px; }
.page-header { margin-bottom: 2rem; }
.page-header h1 { font-size: 1.3rem; font-weight: 600; margin-bottom: .25rem; }
.page-header p { font-size: .82rem; color: var(--muted); }
.section { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.75rem; margin-bottom: 1.25rem; }
.section-head { margin-bottom: 1.5rem; }
.section-title { font-size: .95rem; font-weight: 600; margin-bottom: .2rem; }
.section-desc { font-size: .78rem; color: var(--muted); }
.section-divider { border: none; border-top: 1px solid var(--border); margin: 1.5rem 0; }
.field { margin-bottom: 1.1rem; }
.field label { display: block; font-size: .72rem; font-weight: 500; color: var(--muted); margin-bottom: .45rem; text-transform: uppercase; letter-spacing: .04em; }
.field input[type=text], .field textarea { width: 100%; padding: .85rem 1rem; background: var(--bg); border: 1.5px solid var(--border); border-radius: 10px; color: var(--text); font-size: .88rem; outline: none; transition: border-color .2s, box-shadow .2s; font-family: inherit; resize: vertical; }
.field input:focus, .field textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-bg); }
.field textarea { min-height: 90px; }
input::placeholder, textarea::placeholder { color: var(--dim); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.current-logo { background: var(--bg); border: 1.5px solid var(--border); border-radius: 10px; padding: 1.25rem 1.5rem; display: inline-flex; align-items: center; gap: 1rem; margin-bottom: 1rem; }
.current-logo img { max-height: 48px; max-width: 180px; display: block; }
.btn-delete { background: transparent; border: 1px solid rgba(231,76,60,.35); color: var(--error); padding: .5rem 1rem; border-radius: 8px; font-size: .75rem; cursor: pointer; transition: all .2s; font-family: inherit; }
.btn-delete:hover { background: rgba(231,76,60,.1); }
.file-upload input[type=file] { width: 100%; padding: .8rem 1rem; background: var(--bg); border: 1.5px dashed var(--border); border-radius: 10px; color: var(--muted); font-size: .82rem; cursor: pointer; transition: border-color .2s; font-family: inherit; }
.file-upload input[type=file]:hover { border-color: var(--accent); }
.file-hint { font-size: .7rem; color: var(--dim); margin-top: .4rem; }
.size-badge { display: inline-flex; align-items: center; background: rgba(91,108,240,.12); border: 1px solid rgba(91,108,240,.2); color: #7A9BF0; padding: .3rem .7rem; border-radius: 6px; font-size: .67rem; font-weight: 500; margin-bottom: .6rem; }
.hero-upload-box { background: var(--bg); border: 1.5px solid var(--border); border-radius: 12px; display: grid; grid-template-columns: 200px 1fr; gap: 1.5rem; padding: 1.25rem; align-items: start; }
.hero-thumb { width: 100%; height: 120px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); display: block; }
.gallery-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-top: 1rem; }
.gallery-cell { background: var(--bg); border: 1.5px solid var(--border); border-radius: 10px; overflow: hidden; }
.gallery-cell img { width: 100%; height: 90px; object-fit: cover; display: block; }
.gallery-cell-body { padding: .75rem; }
.gallery-cell-label { font-size: .65rem; color: var(--muted); margin-bottom: .4rem; display: block; }
.color-picker { display: flex; flex-wrap: wrap; gap: .65rem; }
.color-swatch { width: 48px; height: 48px; border-radius: 12px; cursor: pointer; border: 2.5px solid transparent; transition: transform .15s, box-shadow .15s; position: relative; }
.color-swatch:hover { transform: scale(1.06); }
.color-swatch input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.color-swatch.selected { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4.5px var(--accent); transform: scale(1.06); }
.color-swatch .check { display: none; position: absolute; inset: 0; place-items: center; pointer-events: none; }
.color-swatch.selected .check { display: grid; }
.color-swatch .check svg { width: 20px; height: 20px; stroke: #fff; fill: none; stroke-width: 3; }
.schedule-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: .6rem; }
.day-cell { background: var(--bg); border: 1.5px solid var(--border); border-radius: 10px; padding: .75rem .5rem; text-align: center; }
.day-name { font-size: .62rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: .45rem; }
.day-cell input { width: 100%; padding: .5rem .4rem; background: transparent; border: none; color: var(--text); font-size: .78rem; text-align: center; outline: none; font-family: inherit; }
.day-cell input:focus { background: var(--surface); border-radius: 6px; }
.item-card { background: var(--bg); border: 1.5px solid var(--border); border-radius: 11px; padding: 1.1rem; margin-bottom: .7rem; }
.item-number { display: inline-flex; align-items: center; justify-content: center; width: 26px; height: 26px; background: var(--accent-bg); color: var(--accent); border-radius: 7px; font-size: .72rem; font-weight: 600; margin-bottom: .85rem; }
.btn-add-item { display: flex; align-items: center; gap: .5rem; padding: .65rem 1.1rem; background: transparent; border: 1.5px dashed var(--border); color: var(--muted); border-radius: 10px; font-size: .8rem; cursor: pointer; transition: all .2s; font-family: inherit; margin-top: .5rem; }
.btn-add-item:hover { border-color: var(--accent); color: var(--accent); }
.sticky-save { position: sticky; bottom: 0; background: rgba(20,23,36,0.97); backdrop-filter: blur(16px); border-top: 1px solid var(--border); padding: 1rem 2rem; margin: 2rem -2rem -2rem; display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.btn-save { background: var(--accent); color: #fff; padding: .9rem 2.25rem; border: none; border-radius: 11px; font-size: .9rem; font-weight: 600; cursor: pointer; transition: opacity .2s; display: flex; align-items: center; gap: .55rem; font-family: inherit; }
.btn-save:hover { opacity: .88; }
.btn-save svg { width: 16px; height: 16px; stroke: #fff; fill: none; stroke-width: 2.5; }
.save-hint { font-size: .78rem; color: var(--muted); }
@media (max-width: 800px) { .sidebar { display: none; } .main-content { margin-left: 0; } .grid-2, .hero-upload-box { grid-template-columns: 1fr; } .gallery-grid { grid-template-columns: repeat(2, 1fr); } .schedule-grid { grid-template-columns: repeat(4, 1fr); } }
@media (max-width: 480px) { .schedule-grid { grid-template-columns: repeat(2, 1fr); } .main-content { padding: 1rem; } }
</style>
</head>
<body>
<?php if (!$auth): ?>
<!-- ─── LOGIN PAGE ─── -->
<div class="login-page">
  <div class="login-card">
    <div class="login-icon">
      <svg viewBox="0 0 24 24"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></svg>
    </div>
    <h1>Admin Panel</h1>
    <p><?= htmlspecialchars($config['businessName']) ?></p>
    <?php if ($error): ?><div class="login-error"><?= $error ?></div><?php endif; ?>
    <form method="POST">
      <input type="password" name="password" placeholder="Introdu parola" autofocus>
      <button type="submit" class="btn-login">Intră în cont</button>
    </form>
  </div>
</div>

<?php else: ?>

<?php if (isset($saved)): ?>
<div class="toast" id="toast">
  <svg viewBox="0 0 24 24"><polyline points="20 6 9 17 4 12"/></svg>
  Modificările au fost salvate!
</div>
<script>setTimeout(() => { const t = document.getElementById('toast'); if(t) t.style.opacity='0'; t.style.transition='opacity .4s'; setTimeout(() => t.remove(), 400); }, 3000);</script>
<?php endif; ?>

<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="save" value="1">

<div class="admin-layout">

  <!-- ─── SIDEBAR ─── -->
  <aside class="sidebar">
    <div class="sb-brand">
      <div class="sb-icon">
        <svg viewBox="0 0 24 24"><path d="M12 2L2 7l10 5 10-5-10-5z"/><path d="M2 17l10 5 10-5"/><path d="M2 12l10 5 10-5"/></svg>
      </div>
      <div class="sb-name">Admin Panel</div>
    </div>
    <div class="sb-site"><?= htmlspecialchars($config['businessName']) ?></div>

    <div class="nav-label">Identitate</div>
    <a href="#sec-info" class="nav-item">
      <svg viewBox="0 0 24 24"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>
      Informații
    </a>
    <a href="#sec-logo" class="nav-item">
      <svg viewBox="0 0 24 24"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg>
      Logo
    </a>
    <a href="#sec-style" class="nav-item">
      <svg viewBox="0 0 24 24"><circle cx="12" cy="12" r="10"/><circle cx="12" cy="12" r="3"/></svg>
      Culoare & Stil
    </a>
    <a href="#sec-social" class="nav-item">
      <svg viewBox="0 0 24 24"><path d="M18 2h-3a5 5 0 0 0-5 5v3H7v4h3v8h4v-8h3l1-4h-4V7a1 1 0 0 1 1-1h3z"/></svg>
      Social Media
    </a>
    <a href="#sec-schedule" class="nav-item">
      <svg viewBox="0 0 24 24"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>
      Program
    </a>

    <div class="nav-label">Conținut</div>
    <a href="#sec-hero" class="nav-item">
      <svg viewBox="0 0 24 24"><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/></svg>
      Hero & Texte
    </a>
    <a href="#sec-services" class="nav-item">
      <svg viewBox="0 0 24 24"><line x1="8" y1="6" x2="21" y2="6"/><line x1="8" y1="12" x2="21" y2="12"/><line x1="8" y1="18" x2="21" y2="18"/><line x1="3" y1="6" x2="3.01" y2="6"/><line x1="3" y1="12" x2="3.01" y2="12"/><line x1="3" y1="18" x2="3.01" y2="18"/></svg>
      Servicii
    </a>
    <a href="#sec-offers" class="nav-item">
      <svg viewBox="0 0 24 24"><polyline points="20 12 20 22 4 22 4 12"/><rect x="2" y="7" width="20" height="5"/><line x1="12" y1="22" x2="12" y2="7"/><path d="M12 7H7.5a2.5 2.5 0 0 1 0-5H12V7z"/><path d="M12 7h4.5a2.5 2.5 0 0 0 0-5H12V7z"/></svg>
      Oferte
    </a>
    <a href="#sec-photos" class="nav-item">
      <svg viewBox="0 0 24 24"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg>
      Poze
    </a>
    <a href="#sec-reviews" class="nav-item">
      <svg viewBox="0 0 24 24"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg>
      Recenzii
    </a>
    <a href="#sec-security" class="nav-item">
      <svg viewBox="0 0 24 24"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>
      Securitate
    </a>

    <script>
    function pickColor(radio) {
      document.querySelectorAll(".color-swatch").forEach(function(s) { s.classList.remove("selected"); });
      radio.closest(".color-swatch").classList.add("selected");
      var hex = radio.value;
      document.documentElement.style.setProperty("--accent", hex);
      var r = parseInt(hex.slice(1,3),16), g = parseInt(hex.slice(3,5),16), b = parseInt(hex.slice(5,7),16);
      document.documentElement.style.setProperty("--accent-bg", "rgba("+r+","+g+","+b+",0.15)");
    }
    </script>
    <div class="sb-footer">
      <a href="index.php" target="_blank" class="btn-preview-sb">
        <svg viewBox="0 0 24 24"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/></svg>
        Previzualizează site-ul
      </a>
      <form method="POST">
        <input type="hidden" name="logout" value="1">
        <button type="submit" class="btn-logout">Ieșire din cont</button>
      </form>
    </div>
  </aside>

  <!-- ─── MAIN ─── -->
  <main class="main-content">
    <div class="page-header">
      <h1>Editează site-ul</h1>
      <p>Modificările se salvează imediat după ce apeși butonul de mai jos.</p>
    </div>

    <!-- INFO -->
    <div class="section" id="sec-info">
      <div class="section-head">
        <div class="section-title">Informații de bază</div>
        <div class="section-desc">Datele principale ale afacerii tale</div>
      </div>
      <div class="grid-2">
        <div class="field"><label>Nume afacere</label><input type="text" name="businessName" value="<?= htmlspecialchars($config['businessName']) ?>" placeholder="Ex: Zakon Barbershop"></div>
        <div class="field"><label>Tagline</label><input type="text" name="tagline" value="<?= htmlspecialchars($config['tagline'] ?? '') ?>" placeholder="Ex: Cel mai bun barbershop din oraș"></div>
      </div>
      <div class="grid-2">
        <div class="field"><label>Telefon</label><input type="text" name="phone" value="<?= htmlspecialchars($config['phone']) ?>" placeholder="+373 600 00 000"></div>
        <div class="field"><label>Adresă completă</label><input type="text" name="address" value="<?= htmlspecialchars($config['address']) ?>" placeholder="Strada, oraș"></div>
      </div>
      <div class="field"><label>Program (câmp legacy — folosit dacă programul pe zile nu e afișat)</label><input type="text" name="hours" value="<?= htmlspecialchars($config['hours'] ?? '') ?>" placeholder="Ex: Luni–Sâmbătă 10:00–20:00"></div>
    </div>

    <!-- LOGO -->
    <div class="section" id="sec-logo">
      <div class="section-head">
        <div class="section-title">Logo</div>
        <div class="section-desc">Înlocuiește numele text din navigație cu logo-ul tău</div>
      </div>
      <?php $logoFiles = glob(__DIR__ . '/logo.*'); $hasLogo = !empty($logoFiles); ?>
      <?php if ($hasLogo): ?>
        <div style="margin-bottom:1.25rem">
          <div style="font-size:.72rem;color:var(--muted);margin-bottom:.6rem;text-transform:uppercase;letter-spacing:.04em">Logo actual</div>
          <div class="current-logo">
            <img src="<?= basename($logoFiles[0]) ?>?v=<?= time() ?>" alt="Logo">
            <form method="POST">
              <button type="submit" name="deleteLogo" value="1" class="btn-delete">Șterge logo</button>
            </form>
          </div>
        </div>
      <?php else: ?>
        <div style="background:var(--bg);border:1.5px dashed var(--border);border-radius:10px;padding:1.25rem 1.5rem;margin-bottom:1.25rem;color:var(--dim);font-size:.82rem">
          Niciun logo încărcat — în navigație apare numele afacerii ca text.
        </div>
      <?php endif; ?>
      <div class="file-upload">
        <div class="size-badge">PNG / JPG / SVG / WebP · max 2MB · recomandat: 200×60px fundal transparent</div>
        <input type="file" name="logo" accept="image/png,image/jpeg,image/svg+xml,image/webp" onchange="this.form.submit()">
      </div>
    </div>

    <!-- STYLE -->
    <div class="section" id="sec-style">
      <div class="section-head">
        <div class="section-title">Culoare accent</div>
        <div class="section-desc">Culoarea principală — butoane, icoane, elemente decorative</div>
      </div>
      <div class="color-picker">
        <?php
        $palette = [
          '#C8862A' => 'Amber',
          '#B07040' => 'Terracotta',
          '#4A5680' => 'Slate',
          '#8B5E3C' => 'Brown',
          '#2C7A7B' => 'Teal',
          '#9B2C2C' => 'Red',
          '#276749' => 'Green',
          '#5B6CF0' => 'Indigo',
        ];
        $current = $config['accentColor'] ?? '#C8862A';
        foreach ($palette as $hex => $name):
        ?>
        <label class="color-swatch <?= $current === $hex ? 'selected' : '' ?>" style="background:<?= $hex ?>" title="<?= $name ?>">
          <input type="radio" name="accentColor" value="<?= $hex ?>" <?= $current === $hex ? 'checked' : '' ?> onchange="pickColor(this)">
          <div class="check"><svg viewBox="0 0 24 24"><polyline points="20 6 9 17 4 12"/></svg></div>
        </label>
        <?php endforeach; ?>
      </div>
    </div>

    <!-- SOCIAL -->
    <div class="section" id="sec-social">
      <div class="section-head">
        <div class="section-title">Social Media</div>
        <div class="section-desc">Link-uri afișate în footer și în navigație</div>
      </div>
      <div class="grid-2">
        <div class="field"><label>Instagram (fără @)</label><input type="text" name="instagram" value="<?= htmlspecialchars($config['instagram'] ?? '') ?>" placeholder="numeprofil"></div>
        <div class="field"><label>Facebook</label><input type="text" name="facebook" value="<?= htmlspecialchars($config['facebook'] ?? '') ?>" placeholder="facebook.com/pagina-ta"></div>
      </div>
      <div class="grid-2">
        <div class="field"><label>TikTok</label><input type="text" name="tiktok" value="<?= htmlspecialchars($config['tiktok'] ?? '') ?>" placeholder="@numeprofil"></div>
        <div class="field"><label>YouTube</label><input type="text" name="youtube" value="<?= htmlspecialchars($config['youtube'] ?? '') ?>" placeholder="youtube.com/@canal"></div>
      </div>
    </div>

    <!-- SCHEDULE -->
    <div class="section" id="sec-schedule">
      <div class="section-head">
        <div class="section-title">Program de lucru</div>
        <div class="section-desc">Completează orele pentru fiecare zi. Scrie "Închis" pentru ziua liberă.</div>
      </div>
      <div class="schedule-grid">
        <?php
        $dayNames = ['Lun','Mar','Mie','Joi','Vin','Sâm','Dum'];
        $dayCodes = ['mon','tue','wed','thu','fri','sat','sun'];
        foreach ($dayNames as $i => $dn):
        ?>
        <div class="day-cell">
          <div class="day-name"><?= $dn ?></div>
          <input type="text" name="schedule_<?= $dayCodes[$i] ?>" value="<?= htmlspecialchars($config['schedule'][$dayCodes[$i]] ?? '10:00–20:00') ?>">
        </div>
        <?php endforeach; ?>
      </div>
    </div>

    <!-- HERO & TEXTS -->
    <div class="section" id="sec-hero">
      <div class="section-head">
        <div class="section-title">Hero & Texte principale</div>
        <div class="section-desc">Prima secțiune vizibilă pe site — titlu, descriere, textele Despre noi</div>
      </div>
      <div class="field"><label>Titlu principal (Hero)</label><input type="text" name="heroTitle" value="<?= htmlspecialchars($config['heroTitle'] ?? '') ?>" placeholder="Ex: Frizerie premium în Chișinău"></div>
      <div class="field"><label>Descriere scurtă (sub titlu)</label><textarea name="heroDesc" placeholder="Ex: Tuns, bărbierit, tratamente pentru păr..."><?= htmlspecialchars($config['heroDesc'] ?? '') ?></textarea></div>
      <hr class="section-divider">
      <div class="grid-2">
        <div class="field"><label>Despre noi — paragraf 1</label><textarea name="aboutText1" placeholder="Povestea afacerii tale..."><?= htmlspecialchars($config['aboutText1'] ?? '') ?></textarea></div>
        <div class="field"><label>Despre noi — paragraf 2</label><textarea name="aboutText2" placeholder="Valorile, echipa, experiența..."><?= htmlspecialchars($config['aboutText2'] ?? '') ?></textarea></div>
      </div>
    </div>

    <!-- SERVICES -->
    <div class="section" id="sec-services">
      <div class="section-head">
        <div class="section-title">Servicii</div>
        <div class="section-desc">Lista serviciilor afișate în secțiunea dedicată</div>
      </div>
      <?php foreach ($config['services'] as $i => $svc): ?>
      <div class="item-card">
        <div class="item-number"><?= $i + 1 ?></div>
        <div class="grid-2">
          <div class="field"><label>Nume serviciu</label><input type="text" name="svc_name_<?= $i ?>" value="<?= htmlspecialchars($svc['name']) ?>" placeholder="Ex: Tuns clasic"></div>
          <div class="field"><label>Descriere</label><input type="text" name="svc_desc_<?= $i ?>" value="<?= htmlspecialchars($svc['desc']) ?>" placeholder="Ex: Tuns + spălat + coafat"></div>
        </div>
      </div>
      <?php endforeach; ?>
    </div>

    <!-- OFFERS -->
    <div class="section" id="sec-offers">
      <div class="section-head">
        <div class="section-title">Oferte & Promoții</div>
        <div class="section-desc">Ofertele active apar pe site ca un banner special</div>
      </div>
      <div id="offers-list">
        <?php
        $offers = $config['offers'] ?? [];
        foreach ($offers as $i => $offer):
        ?>
        <div class="item-card" id="offer-<?= $i ?>">
          <div class="item-number"><?= $i + 1 ?></div>
          <div class="grid-2">
            <div class="field"><label>Titlu ofertă</label><input type="text" name="offer_title_<?= $i ?>" value="<?= htmlspecialchars($offer['title'] ?? '') ?>" placeholder="Ex: -20% în weekend"></div>
            <div class="field"><label>Valabil până la</label><input type="text" name="offer_valid_<?= $i ?>" value="<?= htmlspecialchars($offer['valid'] ?? '') ?>" placeholder="Ex: 31.03.2026"></div>
          </div>
          <div class="field"><label>Detalii ofertă</label><input type="text" name="offer_desc_<?= $i ?>" value="<?= htmlspecialchars($offer['desc'] ?? '') ?>" placeholder="Ex: Reducere pentru toate tunsorile de vineri–duminică"></div>
        </div>
        <?php endforeach; ?>
      </div>
      <button type="button" class="btn-add-item" onclick="addOffer()">
        <svg viewBox="0 0 24 24" style="width:15px;height:15px;stroke:currentColor;fill:none;stroke-width:2.5"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
        Adaugă ofertă
      </button>
      <script>
        var offerIdx = <?= count($offers) ?>;
        function addOffer() {
          var list = document.getElementById('offers-list');
          var d = document.createElement('div');
          d.className = 'item-card';
          d.innerHTML = '<div class="item-number">' + (offerIdx + 1) + '</div>'
            + '<div class="grid-2">'
            + '<div class="field"><label>Titlu ofertă</label><input type="text" name="offer_title_' + offerIdx + '" placeholder="Ex: -20% în weekend" style="width:100%;padding:.85rem 1rem;background:var(--bg);border:1.5px solid var(--border);border-radius:10px;color:var(--text);font-size:.88rem;outline:none;font-family:inherit"></div>'
            + '<div class="field"><label>Valabil până la</label><input type="text" name="offer_valid_' + offerIdx + '" placeholder="Ex: 31.03.2026" style="width:100%;padding:.85rem 1rem;background:var(--bg);border:1.5px solid var(--border);border-radius:10px;color:var(--text);font-size:.88rem;outline:none;font-family:inherit"></div>'
            + '</div>'
            + '<div class="field"><label>Detalii ofertă</label><input type="text" name="offer_desc_' + offerIdx + '" placeholder="Detalii..." style="width:100%;padding:.85rem 1rem;background:var(--bg);border:1.5px solid var(--border);border-radius:10px;color:var(--text);font-size:.88rem;outline:none;font-family:inherit"></div>';
          list.appendChild(d);
          offerIdx++;
        }
      </script>
    </div>

    <!-- PHOTOS -->
    <div class="section" id="sec-photos">
      <div class="section-head">
        <div class="section-title">Poze</div>
        <div class="section-desc">Hero banner și galeria de imagini</div>
      </div>

      <div style="font-size:.75rem;font-weight:600;color:var(--muted);text-transform:uppercase;letter-spacing:.04em;margin-bottom:.85rem">Poza Hero (banner principal)</div>
      <div class="hero-upload-box" style="margin-bottom:1.75rem">
        <img class="hero-thumb" src="<?= htmlspecialchars($config['heroPhoto']) ?>" onerror="this.style.opacity='.15'" alt="Hero">
        <div>
          <div class="size-badge">Recomandat: 1400 × 800 px · landscape · max 5MB</div>
          <div class="file-upload">
            <input type="file" name="hero_upload" accept="image/jpeg,image/png,image/webp">
            <div class="file-hint">Poza apare pe tot ecranul, în spatele titlului. Alege o imagine orizontală de calitate.</div>
          </div>
        </div>
      </div>

      <div style="font-size:.75rem;font-weight:600;color:var(--muted);text-transform:uppercase;letter-spacing:.04em;margin-bottom:.85rem">Galerie (4 poze)</div>
      <div class="gallery-grid">
        <?php
        $gSizes = ['800 × 1200 px portret','800 × 600 px landscape','800 × 600 px landscape','800 × 600 px landscape'];
        $gLabels = ['Poza mare — stânga','Sus dreapta','Mijloc dreapta','Jos dreapta'];
        foreach ($config['galleryPhotos'] as $i => $url):
        ?>
        <div class="gallery-cell">
          <img src="<?= htmlspecialchars($url) ?>" onerror="this.style.opacity='.1'" alt="">
          <div class="gallery-cell-body">
            <div class="size-badge" style="margin-bottom:.5rem"><?= $gSizes[$i] ?></div>
            <span class="gallery-cell-label"><?= $gLabels[$i] ?></span>
            <div class="file-upload">
              <input type="file" name="gallery_upload_<?= $i ?>" accept="image/jpeg,image/png,image/webp">
            </div>
          </div>
        </div>
        <?php endforeach; ?>
      </div>
    </div>

    <!-- REVIEWS -->
    <div class="section" id="sec-reviews">
      <div class="section-head">
        <div class="section-title">Recenzii clienți</div>
        <div class="section-desc">Recenzii Google afișate pe site</div>
      </div>
      <?php foreach ($config['reviews'] as $i => $rev): ?>
      <div class="item-card">
        <div class="item-number"><?= $i + 1 ?></div>
        <div class="grid-2">
          <div class="field"><label>Numele clientului</label><input type="text" name="rev_name_<?= $i ?>" value="<?= htmlspecialchars($rev['name']) ?>"></div>
          <div class="field"><label>De când (ex: 2 luni în urmă)</label><input type="text" name="rev_ago_<?= $i ?>" value="<?= htmlspecialchars($rev['ago'] ?? '') ?>"></div>
        </div>
        <div class="field"><label>Textul recenziei</label><textarea name="rev_text_<?= $i ?>"><?= htmlspecialchars($rev['text']) ?></textarea></div>
      </div>
      <?php endforeach; ?>
    </div>

    <!-- SECURITY -->
    <div class="section" id="sec-security">
      <div class="section-head">
        <div class="section-title">Securitate</div>
        <div class="section-desc">Schimbă parola de acces la admin</div>
      </div>
      <div style="max-width:380px">
        <div class="field">
          <label>Parolă nouă (minim 6 caractere)</label>
          <input type="text" name="newPassword" placeholder="Lasă gol pentru a păstra parola actuală">
        </div>
        <div style="font-size:.72rem;color:var(--dim)">Parola implicită = ultimele 6 cifre din telefon. Schimb-o după prima accesare.</div>
      </div>
    </div>

    <!-- STICKY SAVE -->
    <div class="sticky-save">
      <div class="save-hint">Toate modificările se aplică imediat pe site.</div>
      <button type="submit" class="btn-save">
        <svg viewBox="0 0 24 24"><path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/><polyline points="17 21 17 13 7 13 7 21"/><polyline points="7 3 7 8 15 8"/></svg>
        Salvează modificările
      </button>
    </div>

  </main>
</div>
</form>
<?php endif; ?>
</body>
</html>
