/* ===================================================================
   D-PAD PASSPORT — Modern CSS Layer
   Cutting-edge CSS features layered on top of existing styles
   =================================================================== */

/* --- Layer Order --- */
@layer tokens, base, layout, components, pages, utilities, overrides;

/* ===================================================================
   TOKENS — Design system primitives
   =================================================================== */
@layer tokens {
  :root {
    /* Enable smooth height: 0 → auto transitions */
    interpolate-size: allow-keywords;

    /* --- oklch Color System --- */
    --paper: oklch(0.96 0.01 85);
    --ink: oklch(0.15 0 0);
    --gold-base: oklch(0.82 0.15 85);
    --gold-light: oklch(0.92 0.10 90);
    --gold-dark: oklch(0.65 0.15 75);
    --surface-dark: oklch(0.91 0.02 80);
    --surface-darker: oklch(0.87 0.02 80);
    --accent-green: oklch(0.68 0.17 155);
    --accent-blue: oklch(0.62 0.15 250);
    --accent-red: oklch(0.58 0.20 25);
    --accent-orange: oklch(0.68 0.17 55);

    /* Dynamic tints via color-mix */
    --surface-hover: color-mix(in oklch, var(--paper), var(--gold-base) 8%);
    --text-muted: color-mix(in oklch, var(--ink), transparent 40%);
    --gold-glow: color-mix(in oklch, var(--gold-base), transparent 70%);
    --gold-border: color-mix(in oklch, var(--gold-base), transparent 80%);
    --surface-glass: color-mix(in oklch, var(--surface-dark), transparent 8%);
    --surface-glass-border: color-mix(in oklch, var(--ink), transparent 92%);

    /* --- Font Stack --- */
    --font-display: 'Archivo Black', system-ui, sans-serif;
    --font-ui: 'Figtree', system-ui, sans-serif;

    /* --- Fluid Spacing via round() --- */
    --space-xs: round(nearest, 0.25rem, 1px);
    --space-sm: round(nearest, 0.5rem, 1px);
    --space-md: round(nearest, 1rem, 1px);
    --space-lg: round(nearest, 1.5rem, 1px);
    --space-xl: round(nearest, 2rem, 1px);
    --space-2xl: round(nearest, 3rem, 1px);

    /* --- Spring Easing via linear() --- */
    --ease-spring: linear(
      0, 0.006, 0.025 2.8%, 0.101 6.1%, 0.539 18.9%,
      0.721 25.3%, 0.849 31.5%, 0.937 38.1%, 0.968 41.8%,
      0.991 45.7%, 1.006 50%, 1.015 55%, 1.011 63%,
      1.004 73%, 1
    );
    --ease-out: cubic-bezier(0.2, 0, 0, 1);
    --ease-in-out: cubic-bezier(0.4, 0, 0.2, 1);

    /* --- Durations --- */
    --duration-fast: 150ms;
    --duration-normal: 250ms;
    --duration-slow: 400ms;
    --duration-expand: 300ms;

    /* --- Radii --- */
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 14px;
    --radius-xl: 18px;
    --radius-pill: 100px;

    /* --- Shadows --- */
    --shadow-sm: 0 1px 3px oklch(0 0 0 / 0.12), 0 1px 2px oklch(0 0 0 / 0.08);
    --shadow-md: 0 4px 16px oklch(0 0 0 / 0.15);
    --shadow-lg: 0 8px 32px oklch(0 0 0 / 0.2);
    --shadow-glow-gold: 0 0 24px var(--gold-glow);

    /* --- Layout --- */
    --nav-height: calc(56px + env(safe-area-inset-bottom, 0px));
    --header-height: 72px;
  }
}

/* ===================================================================
   BASE — Typography, resets, global styles
   =================================================================== */
@layer base {
  /* Fluid Typography */
  h1, h2, h3,
  .page-header,
  .page-title,
  .section-header {
    font-family: var(--font-display);
    text-transform: uppercase;
    letter-spacing: -0.02em;
    margin: 0;
    text-wrap: balance;

    /* NEW: Filled text, not outlined */
    color: var(--ink);
    -webkit-text-stroke: 0;
    text-stroke: 0;
  }

  h1 {
    font-size: clamp(1.8rem, 5vw, 3rem);
    line-height: 0.95;
  }

  h2 {
    font-size: clamp(1.2rem, 3vw, 2rem);
    line-height: 1.1;
  }

  h3 {
    font-size: clamp(0.9rem, 2vw, 1.2rem);
  }

  p, .body-text {
    text-wrap: pretty;
  }

  /* Gold gradient text for headings on map page */
  #tab-map h1, #tab-map h2, #tab-map h3,
  #p6 h1, #p6 h2, #p6 h3 {
    background: linear-gradient(135deg, var(--gold-dark), var(--gold-light));
    background-clip: text;
    -webkit-background-clip: text;
    color: transparent;
    -webkit-text-stroke: 0;
    text-stroke: 0;
  }

  /* All pages — solid dark fill headings */
  #p1 h1, #p1 h2,
  #tab-departures h1, #tab-departures h2,
  #tab-shop h1, #tab-shop h2 {
    color: var(--ink);
    -webkit-text-stroke: 0;
    text-stroke: 0;
  }
}

/* ===================================================================
   COMPONENTS — Disclosure cards, glassmorphism, micro-interactions
   =================================================================== */
@layer components {

  /* ---------------------------------------------------------------
     Progressive Disclosure System
     <details class="disclosure-card"> + <summary>
     --------------------------------------------------------------- */
  .disclosure-card {
    border: 1px solid color-mix(in oklch, var(--ink), transparent 88%);
    border-radius: var(--radius-md);
    background: color-mix(in oklch, white, transparent 30%);
    transition:
      border-color var(--duration-normal) var(--ease-out),
      box-shadow var(--duration-normal) var(--ease-out);
    overflow: hidden;
    container-type: inline-size;
    container-name: disclosure;
  }

  /* Map page disclosure cards */
  #tab-map .disclosure-card,
  #p6 .disclosure-card {
    background: color-mix(in oklch, white, transparent 94%);
    border-color: color-mix(in oklch, white, transparent 88%);
  }

  .disclosure-card summary {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: var(--space-md);
    cursor: pointer;
    list-style: none;
    user-select: none;
    -webkit-user-select: none;
    transition: background var(--duration-fast) var(--ease-out);
  }

  .disclosure-card summary::-webkit-details-marker {
    display: none;
  }

  .disclosure-card summary::marker {
    content: '';
  }

  .disclosure-card summary:hover {
    background: var(--surface-hover);
  }

  #tab-map .disclosure-card summary:hover,
  #p6 .disclosure-card summary:hover {
    background: color-mix(in oklch, white, transparent 92%);
  }

  .disclosure-card summary:active {
    scale: 0.99;
  }

  /* Summary layout pieces */
  .disclosure-icon {
    font-size: 1.4rem;
    flex-shrink: 0;
    width: 2rem;
    text-align: center;
  }

  .disclosure-title {
    font-family: var(--font-ui);
    font-size: 0.85rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    flex: 1;
    color: var(--ink);
  }

  #tab-map .disclosure-title,
  #p6 .disclosure-title {
    color: var(--ink);
  }

  .disclosure-value {
    font-family: var(--font-ui);
    font-size: 1rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--accent-orange);
  }

  .disclosure-chevron {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
    position: relative;
    transition: rotate var(--duration-normal) var(--ease-spring);
  }

  .disclosure-chevron::before {
    content: '';
    position: absolute;
    inset: 4px;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    rotate: 45deg;
    translate: 0 -2px;
    opacity: 0.5;
  }

  /* Smooth expand/collapse via ::details-content */
  .disclosure-card::details-content {
    transition:
      content-visibility var(--duration-expand) allow-discrete,
      opacity var(--duration-expand) var(--ease-out),
      block-size var(--duration-expand) var(--ease-out);
    block-size: 0;
    opacity: 0;
    overflow: clip;
  }

  .disclosure-card[open]::details-content {
    block-size: auto;
    opacity: 1;
  }

  @starting-style {
    .disclosure-card[open]::details-content {
      block-size: 0;
      opacity: 0;
    }
  }

  /* Open state glow */
  .disclosure-card[open] {
    border-color: var(--gold-border);
    box-shadow: 0 8px 32px var(--gold-glow);
  }

  /* Chevron rotates when open */
  .disclosure-card[open] .disclosure-chevron {
    rotate: 180deg;
  }

  /* Disclosure inner content */
  .disclosure-content {
    padding: 0 var(--space-md) var(--space-md);
    border-top: 1px solid color-mix(in oklch, var(--ink), transparent 92%);
  }

  #tab-map .disclosure-content,
  #p6 .disclosure-content {
    border-top-color: color-mix(in oklch, white, transparent 90%);
  }

  /* ---------------------------------------------------------------
     Glassmorphism Panels
     --------------------------------------------------------------- */
  .glass-panel {
    background: color-mix(in oklch, white, transparent 25%);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border: 1px solid var(--surface-glass-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
  }

  .glass-panel--dark {
    background: color-mix(in oklch, var(--surface-dark), transparent 15%);
    border-color: color-mix(in oklch, var(--ink), transparent 90%);
  }

  /* ---------------------------------------------------------------
     Modern Card — clean, subtle
     --------------------------------------------------------------- */
  .modern-card {
    background: white;
    border: 1px solid color-mix(in oklch, var(--ink), transparent 88%);
    border-radius: var(--radius-md);
    overflow: hidden;
    transition:
      transform var(--duration-normal) var(--ease-out),
      box-shadow var(--duration-normal) var(--ease-out);
  }

  .modern-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
  }

  /* Touch micro-interaction */
  .modern-card:active {
    transform: translateY(0) scale(0.98);
    transition-duration: var(--duration-fast);
  }

  /* ---------------------------------------------------------------
     @property for animated gradient borders
     --------------------------------------------------------------- */
  @property --border-angle {
    syntax: '<angle>';
    initial-value: 0deg;
    inherits: false;
  }

  @keyframes rotate-border {
    to { --border-angle: 360deg; }
  }

  .gold-shimmer-border {
    position: relative;
    border: 2px solid transparent;
    background-origin: border-box;
    background-clip: padding-box, border-box;
    background-image:
      linear-gradient(var(--paper), var(--paper)),
      conic-gradient(
        from var(--border-angle),
        var(--gold-dark),
        var(--gold-light),
        var(--gold-dark)
      );
    animation: rotate-border 4s linear infinite;
    border-radius: var(--radius-lg);
  }

  /* ---------------------------------------------------------------
     Micro-interactions — subtle scale/opacity on touch
     --------------------------------------------------------------- */
  .tap-target {
    transition: scale var(--duration-fast) var(--ease-spring);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  .tap-target:active {
    scale: 0.96;
  }

  /* ---------------------------------------------------------------
     Consistent card elevation
     --------------------------------------------------------------- */
  .shop-item, .bp-card, .token-inventory-card, .mission-card {
    box-shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
  }

  /* ---------------------------------------------------------------
     Universal button press feedback
     --------------------------------------------------------------- */
  button:active, [role="button"]:active {
    transform: scale(0.97);
  }

  /* ---------------------------------------------------------------
     Stat row inside disclosure
     --------------------------------------------------------------- */
  .stat-row-modern {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--space-sm) 0;
    font-family: var(--font-ui);
    font-size: 0.8rem;

    &:not(:last-child) {
      border-bottom: 1px dashed color-mix(in oklch, var(--ink), transparent 88%);
    }

    & .stat-label {
      color: var(--text-muted);
    }

    & .stat-value {
      font-weight: 700;
      font-variant-numeric: tabular-nums;
      color: var(--ink);
    }
  }

  #tab-map .stat-row-modern,
  #tab-shop .stat-row-modern {
    &:not(:last-child) {
      border-bottom-color: rgba(0, 0, 0, 0.08);
    }

    & .stat-label {
      color: var(--text-muted);
    }

    & .stat-value {
      color: var(--ink);
    }
  }
}

/* ===================================================================
   PAGES — Per-page overrides and refinements
   =================================================================== */
@layer pages {

  /* --- Page 1: ID / Passport --- */
  #p1 {
    .photo-frame {
      border-radius: var(--radius-sm);
      border: 2px solid color-mix(in oklch, var(--ink), transparent 60%);
      box-shadow: var(--shadow-sm);
      filter: grayscale(80%) contrast(1.1);
    }

    .level-badge {
      border-radius: var(--radius-pill);
      padding: 6px 14px;
      font-size: 11px;
      font-weight: 700;
      letter-spacing: 1px;
      background: var(--ink);
      color: var(--paper);
      border: none;
    }
  }

  /* --- Intel & Earnings (departures tab) --- */
  #tab-departures {
    .dashboard-card {
      border-radius: var(--radius-md);
      border: 1px solid color-mix(in oklch, var(--ink), transparent 88%);
      box-shadow: var(--shadow-sm);
    }

    .summary-stat {
      border-radius: var(--radius-md);
      border: 1px solid color-mix(in oklch, var(--ink), transparent 88%);
      background: color-mix(in oklch, white, transparent 20%);
      box-shadow: var(--shadow-sm);
    }
  }

  /* --- Passport tab: Visas / Stamps --- */
  #tab-passport {
    .stamp-canvas {
      border: 1px solid color-mix(in oklch, var(--ink), transparent 85%);
      border-radius: var(--radius-md);
      background: var(--paper);
    }
  }

  /* --- Shop tab --- */
  #tab-shop {
    .shop-item-title {
      /* Override old outlined style */
      color: var(--gold-light);
      -webkit-text-stroke: 0;
      text-stroke: 0;
      font-size: 12px;
      font-weight: 700;
    }

    .shop-item-price {
      font-variant-numeric: tabular-nums;
    }
  }

  /* --- Passport Status Bar --- */
  .passport-status-bar {
    position: absolute;
    top: calc(50px + var(--safe-top, 0px));
    width: 100%;
    padding: 15px 20px;
    display: flex;
    justify-content: space-between;
    z-index: 10;
    pointer-events: none;
  }

  .status-online {
    color: var(--accent-green);
  }

  /* --- Shop page new classes --- */
  .shop-balance-display {
    text-align: center;
    margin: 15px 0;
    font-family: var(--font-ui);
    font-size: 14px;
    color: var(--accent-blue);
  }

  .shop-container-grid {
    padding: 0 15px;
  }

  .shop-loading-placeholder {
    padding: 20px;
    text-align: center;
    color: color-mix(in oklch, var(--ink), transparent 60%);
  }

  /* --- Mission card title override — filled, not stroked --- */
  .mission-card__title {
    color: var(--gold-light);
    -webkit-text-stroke: 0;
    text-stroke: 0;
    font-size: 14px;
  }

  .mission-card--expanded .mission-card__title {
    font-size: 16px;
  }

  .mission-card--compact .mission-card__title {
    font-size: 12px;
  }

  .mission-card__empty-title {
    color: color-mix(in oklch, var(--gold-base), transparent 40%);
    -webkit-text-stroke: 0;
    text-stroke: 0;
  }

  .missions-section__title {
    background: linear-gradient(135deg, var(--gold-dark), var(--gold-light));
    background-clip: text;
    -webkit-background-clip: text;
    color: transparent;
    -webkit-text-stroke: 0;
    text-stroke: 0;
  }

  /* --- Currency header symbol override --- */
  .currency-symbol {
    color: currentColor;
    -webkit-text-stroke: 0;
    font-family: var(--font-display);
    font-size: 15px;
  }

  .currency-chip.pounds .currency-symbol { color: var(--accent-orange); }
  .currency-chip.miles .currency-symbol { color: var(--accent-blue); }
}

/* ===================================================================
   UTILITIES
   =================================================================== */
@layer utilities {
  .text-balance { text-wrap: balance; }
  .text-pretty { text-wrap: pretty; }

  .tabular-nums { font-variant-numeric: tabular-nums; }

  .visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }
}

/* ===================================================================
   OVERRIDES — Cleanup of dated visual noise
   =================================================================== */
@layer overrides {
  /* Remove CRT scanline overlay */
  .crt-overlay {
    display: none !important;
  }

  /* Remove security guilloche pattern */
  .security-pattern {
    display: none !important;
  }

  /* Remove paper grain texture from passport-device */
  .passport-device {
    background-image: none;
    background: var(--paper);
  }

  /* Remove paper grain from stamp canvas */
  .stamp-canvas {
    background-image: none;
  }

  /* Remove old outlined heading stroke declarations */
  .page h1.ink-print {
    color: var(--ink);
    -webkit-text-stroke: 0;
    text-stroke: 0;
  }

  /* Tech text modernize */
  .tech-text {
    font-family: var(--font-ui);
    font-size: 10px;
    color: var(--text-muted);
    letter-spacing: 1.5px;
    text-transform: uppercase;
  }

  /* Login overlay heading fix */
  #login-overlay h1 {
    color: var(--ink) !important;
    -webkit-text-stroke: 0;
    text-stroke: 0;
    font-size: clamp(1.5rem, 4vw, 2.5rem);
  }

  /* Location HUD glassmorphism */
  #location-hud {
    background: color-mix(in oklch, var(--surface-dark), transparent 15%);
    backdrop-filter: blur(16px) saturate(180%);
    -webkit-backdrop-filter: blur(16px) saturate(180%);
    border: 1px solid color-mix(in oklch, var(--gold-base), transparent 70%);
    border-radius: var(--radius-md);
    box-shadow: 0 0 20px color-mix(in oklch, var(--gold-base), transparent 80%);
  }

  /* Orbit button glassmorphism */
  .orbit-btn {
    background: color-mix(in oklch, var(--surface-dark), transparent 15%);
    backdrop-filter: blur(16px) saturate(180%);
    -webkit-backdrop-filter: blur(16px) saturate(180%);
    border-radius: var(--radius-md);
    border: 1px solid color-mix(in oklch, var(--gold-base), transparent 60%);
    transition:
      transform var(--duration-normal) var(--ease-spring),
      box-shadow var(--duration-normal) var(--ease-out);
  }

  .orbit-btn:hover {
    transform: scale(1.04);
    box-shadow: 0 0 30px color-mix(in oklch, var(--gold-base), transparent 60%);
  }

  .orbit-btn:active {
    transform: scale(0.97);
  }

  /* Challenge card section header — filled text */
  .challenge-section-header {
    background: linear-gradient(135deg, var(--gold-dark), var(--gold-light));
    background-clip: text;
    -webkit-background-clip: text;
    color: transparent;
    text-shadow: none;
  }

  /* Store section title — filled text */
  .store-section-title {
    background: linear-gradient(135deg, var(--gold-dark), var(--gold-light));
    background-clip: text;
    -webkit-background-clip: text;
    color: transparent;
  }

  /* --- @container responsive cards --- */
  @container page (max-width: 360px) {
    .disclosure-card summary {
      padding: var(--space-sm);
      gap: var(--space-xs);
    }

    .disclosure-icon {
      font-size: 1.1rem;
      width: 1.5rem;
    }

    .disclosure-title {
      font-size: 0.75rem;
    }

    .disclosure-value {
      font-size: 0.85rem;
    }
  }

  /* --- @starting-style for dynamically loaded content --- */
  @starting-style {
    .stamp-item {
      opacity: 0;
      scale: 0.8;
    }

    .shop-item {
      opacity: 0;
      translate: 0 10px;
    }

    .challenge-card {
      opacity: 0;
      scale: 0.95;
    }
  }

  .stamp-item,
  .shop-item,
  .challenge-card {
    transition:
      opacity var(--duration-slow) var(--ease-out),
      scale var(--duration-slow) var(--ease-spring),
      translate var(--duration-slow) var(--ease-out);
  }
}
