<script lang="ts">
  // The "update required" notice (the hard version tier). Shown when the gateway has refused a
  // foreground call as too old, driving the net-state machine into offlineVersionLocked. Unlike the
  // old terminal cover it is dismissable: "Update" takes the user to the fix (the store on native,
  // a reload on the web), while "Play offline" dismisses the notice and leaves the app in the offline
  // lobby (the version lock is an offline state, sticky until an actual update on the next launch).
  // Mirrors MaintenanceOverlay.svelte's look.
  import { netState } from '../lib/netstate.svelte';
  import { openUpdate } from '../lib/update.svelte';
  import { t } from '../lib/i18n/index.svelte';

  // Dismissed for the session once the user chooses "Play offline". The lock is sticky until a fresh
  // launch, so the notice does not reappear this session; the app stays in the offline lobby.
  let dismissed = $state(false);
</script>

{#if netState.versionLocked && !dismissed}
  <div class="scrim" role="alertdialog" aria-modal="true" aria-labelledby="update-title" aria-describedby="update-body">
    <div class="card">
      <div class="tile" aria-hidden="true">Э</div>
      <h1 id="update-title">{t('update.title')}</h1>
      <p id="update-body">{t('update.body')}</p>
      <div class="acts">
        <button type="button" class="primary" onclick={openUpdate}>{t('update.action')}</button>
        <button type="button" onclick={() => (dismissed = true)}>{t('update.playOffline')}</button>
      </div>
    </div>
  </div>
{/if}

<style>
  .scrim {
    position: fixed;
    inset: 0;
    /* Above the game (z 60) and toasts (z 50); below the dev DebugPanel (z 10000). */
    z-index: 100;
    background: rgba(0, 0, 0, 0.55);
    display: grid;
    place-items: center;
    padding: 24px;
    box-sizing: border-box;
  }
  .card {
    max-width: 22rem;
    text-align: center;
    background: var(--surface);
    color: var(--text);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 2rem 1.5rem;
  }
  /* Mirrors a placed board tile (as Splash.svelte / MaintenanceOverlay.svelte do). */
  .tile {
    display: inline-grid;
    place-items: center;
    width: 3.5rem;
    height: 3.5rem;
    font-size: 2rem;
    font-weight: 700;
    border-radius: 4px;
    background: var(--tile-bg);
    color: var(--tile-text);
    box-shadow:
      inset 0 -2px 0 var(--tile-edge),
      2px 0 3px -1px rgba(0, 0, 0, 0.4);
    margin-bottom: 1.25rem;
  }
  h1 {
    font-size: 1.25rem;
    margin: 0 0 0.5rem;
  }
  p {
    margin: 0 0 1.5rem;
    color: var(--text-muted);
    line-height: 1.5;
  }
  .acts {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
  }
  button {
    font: inherit;
    padding: 0.55rem 1.4rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: transparent;
    color: var(--text);
    cursor: pointer;
  }
  button:hover {
    border-color: var(--accent);
    color: var(--accent);
  }
  .primary {
    background: var(--accent);
    color: var(--accent-text);
    border-color: var(--accent);
  }
  .primary:hover {
    color: var(--accent-text);
    opacity: 0.9;
  }
</style>
