<script>
  let hijriDate = $state(null);
  let gregorian = $state('');
  let loading = $state(true);

  async function loadHijri() {
    try {
      const today = new Date();
      const d = String(today.getDate()).padStart(2, '0');
      const m = String(today.getMonth() + 1).padStart(2, '0');
      const y = today.getFullYear();
      gregorian = `${d}/${m}/${y}`;
      const url = `https://api.aladhan.com/v1/gToH/${d}-${m}-${y}`;
      const res = await fetch(url);
      const data = await res.json();
      if (data.code === 200) {
        hijriDate = data.data.hijri;
      }
      loading = false;
    } catch (e) {
      loading = false;
    }
  }

  $effect(() => { loadHijri(); });
</script>

<div class="card">
  <h2>📅 Hijri Calendar</h2>

  {#if loading}
    <p class="loading">⏳ Loading…</p>
  {:else if hijriDate}
    <div class="date-display">
      <div class="hijri-big">
        <span class="hijri-day">{hijriDate.day}</span>
        <span class="hijri-month">{hijriDate.month.en}</span>
        <span class="hijri-year">{hijriDate.year} AH</span>
      </div>
      <div class="gregorian-row">
        <span>Gregorian: {gregorian}</span>
      </div>
      <div class="hijri-info">
        <div class="info-row"><span>Weekday</span><span>{hijriDate.weekday.en}</span></div>
        <div class="info-row"><span>Month</span><span>{hijriDate.month.number}</span></div>
        <div class="info-row"><span>Days in Month</span><span>{hijriDate.month.days}</span></div>
        <div class="info-row"><span>Designation</span><span>{hijriDate.designation.expanded}</span></div>
      </div>
    </div>
  {/if}
</div>

<style>
  .loading { color: var(--text-dim); text-align: center; padding: 20px; }

  .date-display { display: flex; flex-direction: column; align-items: center; gap: 16px; }
  
  .hijri-big {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: linear-gradient(135deg, var(--bg3), var(--surface));
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 28px 40px;
    width: 100%;
    text-align: center;
    position: relative;
    overflow: hidden;
  }
  
  .hijri-big::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 3px;
    background: linear-gradient(90deg, var(--gold), var(--green), var(--gold));
  }
  
  .hijri-day { 
    font-family: 'Cinzel', serif;
    font-size: 3.5rem;
    font-weight: 900;
    color: var(--gold);
    line-height: 1;
    text-shadow: 0 0 30px rgba(201,168,76,0.3);
  }
  
  .hijri-month { 
    font-family: 'Cinzel', serif;
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--text);
    margin-top: 8px;
  }
  
  .hijri-year { 
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.9rem;
    letter-spacing: 0.15em;
    color: var(--text-dim);
    margin-top: 4px;
  }
  
  .gregorian-row { 
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.75rem;
    letter-spacing: 0.1em;
    color: var(--text-dim);
    padding: 8px 16px;
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: 8px;
  }
  
  .hijri-info { width: 100%; display: flex; flex-direction: column; gap: 6px; }
  
  .info-row {
    display: flex;
    justify-content: space-between;
    padding: 12px 16px;
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 0.9rem;
    transition: all 0.2s;
  }
  
  .info-row:hover { border-color: var(--gold); background: var(--gold-dim); }
  
  .info-row span:first-child { 
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.7rem;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--text-dim);
  }
  
  .info-row span:last-child { 
    font-family: 'DM Sans', sans-serif;
    font-weight: 500;
    color: var(--text);
  }
</style>