<!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>
<?php
  $themePalette = isset($themePalette) && is_array($themePalette) ? $themePalette : [];
  $themeVars = [
    '--person-accent' => (string) ($themePalette['accent'] ?? ($favoriteColor ?? '#4a90e2')),
    '--fh-primary' => (string) ($themePalette['primary'] ?? '#4a90e2'),
    '--fh-primary-hover' => (string) ($themePalette['primary_hover'] ?? '#3e79be'),
    '--fh-primary-active' => (string) ($themePalette['primary_active'] ?? '#3567a2'),
    '--fh-primary-subtle' => (string) ($themePalette['primary_subtle'] ?? '#edf4fc'),
    '--fh-primary-border' => (string) ($themePalette['primary_border'] ?? '#c6dcf6'),
    '--fh-primary-text' => (string) ($themePalette['primary_text'] ?? '#ffffff'),
    '--fh-primary-subtle-text' => (string) ($themePalette['primary_subtle_text'] ?? '#1f2a37'),
    '--fh-secondary' => (string) ($themePalette['secondary'] ?? '#dce9f8'),
    '--fh-secondary-hover' => (string) ($themePalette['secondary_hover'] ?? '#cfdff2'),
    '--fh-secondary-active' => (string) ($themePalette['secondary_active'] ?? '#c0d3ea'),
    '--fh-secondary-border' => (string) ($themePalette['secondary_border'] ?? '#b4c9e3'),
    '--fh-secondary-text' => (string) ($themePalette['secondary_text'] ?? '#1f2a37'),
    '--fh-tertiary' => (string) ($themePalette['tertiary'] ?? '#f4f8fd'),
    '--fh-tertiary-hover' => (string) ($themePalette['tertiary_hover'] ?? '#e5eef9'),
    '--fh-tertiary-active' => (string) ($themePalette['tertiary_active'] ?? '#d6e5f5'),
    '--fh-tertiary-border' => (string) ($themePalette['tertiary_border'] ?? '#c6d8ee'),
    '--fh-tertiary-text' => (string) ($themePalette['tertiary_text'] ?? '#1f2a37'),
    '--fh-focus-ring' => (string) ($themePalette['focus_ring'] ?? '#89b6eb'),
    '--fh-surface-tint' => (string) ($themePalette['surface_tint'] ?? '#f7fafe'),
    '--fh-header-gradient-end' => (string) ($themePalette['header_gradient_end'] ?? '#2c5282'),
    '--fh-success' => (string) ($themePalette['success'] ?? '#198754'),
    '--fh-success-hover' => (string) ($themePalette['success_hover'] ?? '#157347'),
    '--fh-success-active' => (string) ($themePalette['success_active'] ?? '#146c43'),
    '--fh-success-subtle' => (string) ($themePalette['success_subtle'] ?? '#d1e7dd'),
    '--fh-success-border' => (string) ($themePalette['success_border'] ?? '#a3cfbb'),
    '--fh-success-text' => (string) ($themePalette['success_text'] ?? '#ffffff'),
    '--fh-success-subtle-text' => (string) ($themePalette['success_subtle_text'] ?? '#0f5132'),
    '--fh-warning' => (string) ($themePalette['warning'] ?? '#f59e0b'),
    '--fh-warning-hover' => (string) ($themePalette['warning_hover'] ?? '#d97706'),
    '--fh-warning-active' => (string) ($themePalette['warning_active'] ?? '#b45309'),
    '--fh-warning-subtle' => (string) ($themePalette['warning_subtle'] ?? '#fff3cd'),
    '--fh-warning-border' => (string) ($themePalette['warning_border'] ?? '#ffe69c'),
    '--fh-warning-text' => (string) ($themePalette['warning_text'] ?? '#111827'),
    '--fh-warning-subtle-text' => (string) ($themePalette['warning_subtle_text'] ?? '#664d03'),
    '--fh-danger' => (string) ($themePalette['danger'] ?? '#dc3545'),
    '--fh-danger-hover' => (string) ($themePalette['danger_hover'] ?? '#bb2d3b'),
    '--fh-danger-active' => (string) ($themePalette['danger_active'] ?? '#b02a37'),
    '--fh-danger-subtle' => (string) ($themePalette['danger_subtle'] ?? '#f8d7da'),
    '--fh-danger-border' => (string) ($themePalette['danger_border'] ?? '#f1aeb5'),
    '--fh-danger-text' => (string) ($themePalette['danger_text'] ?? '#ffffff'),
    '--fh-danger-subtle-text' => (string) ($themePalette['danger_subtle_text'] ?? '#842029'),
    '--fh-info' => (string) ($themePalette['info'] ?? '#0ea5e9'),
    '--fh-info-hover' => (string) ($themePalette['info_hover'] ?? '#0b84bd'),
    '--fh-info-active' => (string) ($themePalette['info_active'] ?? '#0a6f9f'),
    '--fh-info-subtle' => (string) ($themePalette['info_subtle'] ?? '#cff4fc'),
    '--fh-info-border' => (string) ($themePalette['info_border'] ?? '#9eeaf9'),
    '--fh-info-text' => (string) ($themePalette['info_text'] ?? '#ffffff'),
    '--fh-info-subtle-text' => (string) ($themePalette['info_subtle_text'] ?? '#055160'),
  ];
  $bodyStyleParts = [];
  foreach ($themeVars as $varName => $varValue) {
    $bodyStyleParts[] = $varName . ': ' . $varValue;
  }
  $bodyStyle = implode('; ', $bodyStyleParts) . ';';
?>
<body class="family-hub-body <?= htmlspecialchars($headerThemeClass ?? 'header-tone-dark', ENT_QUOTES, 'UTF-8') ?><?= !empty($isCalendarTvView) ? ' calendar-tv-dark' : '' ?>" style="<?= htmlspecialchars($bodyStyle, ENT_QUOTES, 'UTF-8') ?>">
  <?php include __DIR__ . '/calendar_tv_collage_bg.php'; ?>
  <?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>
  <?php
    $fhMainClasses = !empty($isCalendarTvView)
      ? 'container-fluid px-0 py-0'
      : 'container py-4';
  ?>
  <main class="<?= htmlspecialchars($fhMainClasses, ENT_QUOTES, 'UTF-8') ?>">
