<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Family Hub</title>

  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Figtree:ital,wght@0,400;0,500;0,600;0,700;1,400&display=swap" rel="stylesheet">

  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
  <link rel="stylesheet" href="assets/css/style.css">
</head>
<body class="family-hub-body <?= htmlspecialchars($headerThemeClass ?? 'header-tone-dark', ENT_QUOTES, 'UTF-8') ?>" style="--person-accent: <?= htmlspecialchars($favoriteColor ?? '#4a90e2', ENT_QUOTES, 'UTF-8') ?>;">
  <?php $hideTopHeader = !empty($isCalendarTvView); ?>
  <?php if (!$hideTopHeader): ?>
  <header class="app-header app-header-with-tabs text-white pt-3 px-3 pb-2 mb-0">
    <div class="container">
      <?php
        $hdrSym = trim((string) ($familySettings['currency_symbol'] ?? '★'));
        $hdrName = trim((string) ($familySettings['currency_name'] ?? ''));
        $hdrBal = 0.0;
        if ($activePerson !== null) {
            $hdrBal = is_numeric($activePerson['currency_balance'] ?? null)
                ? (float) $activePerson['currency_balance']
                : 0.0;
        }
      ?>
      <div class="app-header-top">
        <h1 class="h3 mb-0 app-header-brand">
          <a href="index.php" class="app-header-logo-link" aria-label="Family Hub home">
            <img
              src="assets/logo/trans_480x480.svg"
              alt="Family Hub"
              class="app-header-logo"
              width="240"
              height="80"
            >
          </a>
        </h1>
        <div class="app-header-actions d-flex align-items-center gap-2 flex-wrap justify-content-end">
          <?php if (count($people) === 0): ?>
            <span class="small opacity-75 text-end">Add people in Family settings.</span>
          <?php else: ?>
            <?php if ($activePerson !== null): ?>
              <div class="user-balance app-header-balance badge rounded-pill bg-light text-dark px-3 py-2 text-wrap text-start" title="Current balance for the selected profile">
                <span class="text-muted small">Balance</span><br>
                <strong class="fs-6"><?= htmlspecialchars(number_format($hdrBal, 2, '.', ''), ENT_QUOTES, 'UTF-8') ?></strong>
                <span class="ms-1"><?= htmlspecialchars($hdrSym, ENT_QUOTES, 'UTF-8') ?><?php if ($hdrName !== ''): ?><span class="text-muted small"> <?= htmlspecialchars($hdrName, ENT_QUOTES, 'UTF-8') ?></span><?php endif; ?></span>
              </div>
            <?php endif; ?>
            <div class="persona-switcher d-none d-md-flex flex-wrap gap-2 align-items-center justify-content-end" role="toolbar" aria-label="Who is using the hub">
          <?php foreach ($people as $p): ?>
            <?php
              $pid = $p['id'] ?? '';
              $pname = $p['name'] ?? 'Unknown';
              $isActive = $activePerson && ($activePerson['id'] ?? '') === $pid;
              $needsPin = personRequiresPinToActivate($p) ? '1' : '0';
              $roleLabel = $p['role'] ?? '';
            ?>
            <button
              type="button"
              class="btn btn-sm persona-chip <?= $isActive ? 'btn-light active' : 'btn-outline-light' ?>"
              data-person-id="<?= htmlspecialchars($pid, ENT_QUOTES, 'UTF-8') ?>"
              data-needs-pin="<?= $needsPin ?>"
              data-person-name="<?= htmlspecialchars($pname, ENT_QUOTES, 'UTF-8') ?>"
            >
              <?= sanitizeInput($pname) ?>
              <?php if ($roleLabel === ROLE_HEAD): ?>
                <span class="visually-hidden">(Head of household)</span>
                <i class="fa fa-house-chimney-user ms-1" aria-hidden="true"></i>
              <?php elseif ($roleLabel === ROLE_ADULT): ?>
                <i class="fa fa-person ms-1" aria-hidden="true"></i>
              <?php elseif ($roleLabel === ROLE_CHILD): ?>
                <i class="fa fa-child ms-1" aria-hidden="true"></i>
              <?php endif; ?>
            </button>
          <?php endforeach; ?>
            </div>
          <?php endif; ?>
        </div>

        <?php if (count($people) > 0): ?>
        <div class="dropdown d-md-none app-header-persona-mobile">
          <button class="btn btn-light btn-sm dropdown-toggle w-100 text-start" type="button" data-bs-toggle="dropdown" aria-expanded="false">
            <i class="fa fa-user-group me-1" aria-hidden="true"></i>
            <?= $activePerson !== null ? sanitizeInput((string) ($activePerson['name'] ?? 'Select family member')) : 'Select family member' ?>
          </button>
          <ul class="dropdown-menu w-100">
            <?php foreach ($people as $p): ?>
              <?php
                $pid = $p['id'] ?? '';
                $pname = $p['name'] ?? 'Unknown';
                $isActive = $activePerson && ($activePerson['id'] ?? '') === $pid;
                $needsPin = personRequiresPinToActivate($p) ? '1' : '0';
                $roleLabel = $p['role'] ?? '';
              ?>
              <li>
                <button
                  type="button"
                  class="dropdown-item persona-chip persona-chip-mobile <?= $isActive ? 'active' : '' ?>"
                  data-person-id="<?= htmlspecialchars($pid, ENT_QUOTES, 'UTF-8') ?>"
                  data-needs-pin="<?= $needsPin ?>"
                  data-person-name="<?= htmlspecialchars($pname, ENT_QUOTES, 'UTF-8') ?>"
                >
                  <?= sanitizeInput($pname) ?>
                  <?php if ($roleLabel === ROLE_HEAD): ?>
                    <i class="fa fa-house-chimney-user ms-1" aria-hidden="true"></i>
                  <?php elseif ($roleLabel === ROLE_ADULT): ?>
                    <i class="fa fa-person ms-1" aria-hidden="true"></i>
                  <?php elseif ($roleLabel === ROLE_CHILD): ?>
                    <i class="fa fa-child ms-1" aria-hidden="true"></i>
                  <?php endif; ?>
                </button>
              </li>
            <?php endforeach; ?>
          </ul>
        </div>
        <?php endif; ?>
      </div>
      <?php
        $_nav = isset($navTabs) && is_array($navTabs) && $navTabs !== [] ? $navTabs : ($TABS ?? []);
      ?>
      <?php if ($_nav !== []): ?>
      <nav class="tabs" aria-label="Sections">
        <?php foreach ($_nav as $tabId => $tab): ?>
        <a href="?tab=<?= htmlspecialchars($tabId, ENT_QUOTES, 'UTF-8') ?>" class="tab <?= ($activeTab ?? '') === $tabId ? 'active' : '' ?>">
          <i class="fa fa-<?= htmlspecialchars($tab['icon'], ENT_QUOTES, 'UTF-8') ?>" aria-hidden="true"></i>
          <span><?= htmlspecialchars($tab['title'], ENT_QUOTES, 'UTF-8') ?></span>
        </a>
        <?php endforeach; ?>
      </nav>
      <?php endif; ?>
    </div>
  </header>
  <?php endif; ?>

  <div class="modal fade" id="hohPinModal" tabindex="-1" aria-labelledby="hohPinModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h2 class="modal-title h5" id="hohPinModalLabel">Head of household PIN</h2>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <p class="small text-muted mb-2" id="hohPinModalPrompt">Enter PIN to switch to this profile.</p>
          <label for="hohPinInput" class="form-label">PIN</label>
          <input type="password" class="form-control form-control-lg" id="hohPinInput" autocomplete="current-password" minlength="4">
          <div class="invalid-feedback d-block d-none" id="hohPinError"></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
          <button type="button" class="btn btn-primary" id="hohPinSubmit">Continue</button>
        </div>
      </div>
    </div>
  </div>

  <script>
    window.familyHubApiBase = <?= json_encode($familyHubApiBase, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP) ?>;
  </script>
  <main class="container py-4">
