:root {
  color-scheme: light dark;
  --paper: light-dark(#fbf8f7, #13171a);
  --ink: light-dark(#1b252c, #f1f3f5);
  --muted: light-dark(#59636b, #9aa0a6);
  --dock-bg: light-dark(rgba(251, 248, 247, 0.65), rgba(19, 23, 26, 0.72));
  --dock-bg-hover: light-dark(rgba(251, 248, 247, 0.88), rgba(25, 31, 35, 0.88));
  --dock-border: light-dark(rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0.12));
  --dock-shadow: light-dark(0 4px 20px rgba(27, 37, 44, 0.06), 0 4px 24px rgba(0, 0, 0, 0.35));
  --card-bg: light-dark(rgba(251, 248, 247, 0.78), rgba(25, 31, 36, 0.85));
  --card-border: light-dark(rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0.12));
  --card-hover-bg: light-dark(rgba(255, 255, 255, 0.95), rgba(35, 43, 50, 0.95));
  --card-hover-border: light-dark(rgba(27, 37, 44, 0.16), rgba(255, 255, 255, 0.22));
  --tooltip-bg: light-dark(rgba(251, 248, 247, 0.94), rgba(25, 31, 36, 0.94));
  --tooltip-border: light-dark(rgba(27, 37, 44, 0.08), rgba(255, 255, 255, 0.14));
  --page-inset: clamp(20px, 7.18vw, 152px);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --coast-position: center;
  --coast-veil: linear-gradient(97deg,
    rgb(251 248 247 / 32%) 0%,
    rgb(251 248 247 / 12%) 28%,
    rgb(251 248 247 / 0%) 56%);
  --content-left: max(var(--page-inset), calc(env(safe-area-inset-left, 0px) + 12px));
  --content-right: max(var(--page-inset), calc(env(safe-area-inset-right, 0px) + 12px));
}

/* Fallback for browsers without light-dark() support */
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #13171a;
    --ink: #f1f3f5;
    --muted: #9aa0a6;
    --dock-bg: rgba(19, 23, 26, 0.72);
    --dock-bg-hover: rgba(25, 31, 35, 0.88);
    --dock-border: rgba(255, 255, 255, 0.12);
    --dock-shadow: 0 4px 24px rgba(0, 0, 0, 0.35);
    --card-bg: rgba(25, 31, 36, 0.85);
    --card-border: rgba(255, 255, 255, 0.12);
    --card-hover-bg: rgba(35, 43, 50, 0.95);
    --card-hover-border: rgba(255, 255, 255, 0.22);
    --tooltip-bg: rgba(25, 31, 36, 0.94);
    --tooltip-border: rgba(255, 255, 255, 0.14);
    --coast-veil: linear-gradient(97deg,
      rgb(19 23 26 / 82%) 0%,
      rgb(19 23 26 / 48%) 30%,
      rgb(19 23 26 / 0%) 65%);
  }
}

* { box-sizing: border-box; }
html { background: var(--paper); }
html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}
body {
  margin: 0;
  color: var(--ink);
  font-family: Inter, "Helvetica Neue", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
a:focus-visible { outline: 2px solid var(--ink); outline-offset: 7px; border-radius: 2px; }

.page {
  position: relative;
  z-index: 1;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  align-items: center;
  padding-block: max(clamp(12px, 3dvh, 32px), var(--safe-top)) max(clamp(12px, 3dvh, 32px), var(--safe-bottom));
}

/* The photograph extends behind Safari's floating UI instead of being clipped
   to the smaller, changing content viewport. Keep it outside .page. */
.backdrop {
  position: fixed;
  z-index: 0;
  inset: 0 0 auto;
  height: 100vh;
  height: 100lvh;
  min-height: 100%;
  overflow: hidden;
  pointer-events: none;
  background-color: var(--paper);
}

/* Cover both axes at every aspect ratio. The veil belongs to the viewport,
   not the photograph, so cropping never exposes a hard image edge. */
.coast,
.backdrop::after {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.backdrop::after { content: ""; background: var(--coast-veil); }
.coast {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: var(--coast-position);
  opacity: 0;
  transition: opacity 280ms ease-out;
}

.coast.is-ready { opacity: 1; }
.coast.is-cached { transition: none; }

.intro {
  margin-inline-start: var(--content-left);
  width: max-content;
  max-width: calc(100% - var(--content-left) - var(--content-right));
}

#home {
  align-items: flex-end;

  .intro {
    margin-bottom: max(clamp(20px, 4.8dvh, 48px), var(--safe-bottom));
    transform: none;
  }
}

.home-intro {
  position: relative;
}

h1 {
  margin: 0;
  font-size: min(clamp(58px, 5.5vw, 112px), clamp(32px, 10dvh, 112px));
  font-weight: 400;
  line-height: 1.1;
  letter-spacing: -0.025em;

  span { display: block; }
}

.welcome {
  margin: clamp(12px, 2.5dvh, 24px) 0 0;
  font-size: min(clamp(20px, 1.7vw, 30px), clamp(16px, 4dvh, 30px));
  line-height: 1.45;
  letter-spacing: -0.02em;
  color: var(--muted);
}
.rule {
  width: clamp(52px, 3.6vw, 60px);
  height: 1px;
  margin-top: clamp(12px, 3.4dvh, 32px);
  background: light-dark(#c8cbcc, #3f474d);
}
.details {
  margin: clamp(12px, 3dvh, 28px) 0 0;
  font-size: min(clamp(18px, 1.4vw, 20px), clamp(14px, 3.3dvh, 20px));
  line-height: 1.6;
  letter-spacing: -0.01em;
  color: var(--muted);
}

/* ── Dock Capsule & Social Actions ── */
.socials {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: var(--dock-bg);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border: 1px solid var(--dock-border);
  border-radius: 9999px;
  box-shadow: var(--dock-shadow), 0 1px 3px rgba(0, 0, 0, 0.04);
  color: var(--ink);
  margin: 0;
  transition: transform 200ms ease, box-shadow 200ms ease, background-color 200ms ease;

  &:hover {
    background: var(--dock-bg-hover);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  }

  a, button {
    display: grid;
    place-items: center;
    flex: 0 0 38px;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    color: var(--ink);
    transition: color 160ms ease, background-color 160ms ease, transform 160ms ease;
    -webkit-tap-highlight-color: transparent;
    position: relative;
    box-sizing: border-box;

    &:focus-visible { outline-offset: 3px; }
    &:active { background-color: rgb(127 127 127 / 15%); }
    &:hover { background-color: rgb(127 127 127 / 10%); }
  }

  button.social-btn {
    border: none;
    background: transparent;
    font: inherit;
    cursor: pointer;
    padding: 0;
    margin: 0;
  }

  .separator {
    width: 1px;
    height: 14px;
    background: light-dark(rgba(27, 37, 44, 0.14), rgba(255, 255, 255, 0.14));
    flex-shrink: 0;
  }

  svg {
    display: block;
    width: 20px;
    height: 20px;
  }
}

@media (max-width: 700px) {
  :root {
    --coast-veil: linear-gradient(180deg,
      transparent 0%,
      rgb(251 248 247 / 15%) 60%,
      rgb(251 248 247 / 35%) 100%);
  }
}

/* iOS Safari paints the root canvas into browser-adjacent areas. A fixed
   backdrop instead lets its solid color be sampled for toolbar extension. */
@supports (-webkit-touch-callout: none) {
  html {
    --coast-image: none;
    --browser-bleed: calc(var(--safe-top) + var(--safe-bottom) + 100lvh - 100svh);
    --canvas-height: calc(100lvh + var(--browser-bleed));
    height: var(--canvas-height);
    background-image: linear-gradient(var(--paper), transparent 12lvh), var(--coast-veil), var(--coast-image);
    background-repeat: no-repeat;
    background-size: 100% 100%, 100% 100%, cover;
    background-position: center, center, var(--coast-position);
  }

  .backdrop { position: absolute; visibility: hidden; background: transparent; }
}

/* ── Native Cross-Document View Transitions ── */
@view-transition {
  navigation: auto;
}
.backdrop {
  view-transition-name: page-backdrop;
}

/* ── Game Toggle Control ── */
.game-toggle {
  grid-template-areas: "center";
  anchor-name: --game-toggle;
}

.icon-game,
.icon-close {
  grid-area: center;
  width: 20px;
  height: 20px;
  transition: transform 240ms cubic-bezier(0.34, 1.56, 0.64, 1),
              opacity 180ms ease;
}
.icon-close {
  opacity: 0;
  transform: scale(0.5) rotate(-90deg);
  pointer-events: none;
}
.game-toggle.is-active {
  background-color: light-dark(rgba(27, 37, 44, 0.1), rgba(255, 255, 255, 0.12)) !important;

  .icon-game {
    opacity: 0;
    transform: scale(0.5) rotate(90deg);
    pointer-events: none;
  }
  .icon-close {
    opacity: 1;
    transform: scale(1) rotate(0deg);
    pointer-events: auto;
  }
  .btn-tooltip {
    display: none;
  }
}

.github-icon,
.email-icon,
.gamepad-icon,
.close-icon {
  transition: transform 220ms cubic-bezier(0.34, 1.56, 0.64, 1);
  transform-origin: center;
}

/* ── Accessible Tooltips (Hover & Focus-Visible) ── */
.btn-tooltip {
  position: absolute;
  bottom: calc(100% + 10px);
  left: 50%;
  transform: translateX(-50%) translateY(4px);
  padding: 3px 9px;
  font-size: 11.5px;
  font-weight: 500;
  letter-spacing: 0.02em;
  color: var(--ink);
  background: var(--tooltip-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--tooltip-border);
  border-radius: 9999px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  pointer-events: none;
  opacity: 0;
  transition: opacity 160ms ease, transform 160ms cubic-bezier(0.16, 1, 0.3, 1);
  white-space: nowrap;
}

/* Show tooltips on both hover AND keyboard focus-visible for accessibility */
.socials a:is(:hover, :focus-visible) .btn-tooltip,
.socials button:is(:hover, :focus-visible) .btn-tooltip {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (hover: hover) {
  .socials a:is(:hover, :focus-visible) .github-icon {
    transform: rotate(8deg) scale(1.1);
  }
  .socials a:is(:hover, :focus-visible) .email-icon {
    transform: translateY(-2.5px) scale(1.1);
  }
  .game-toggle:not(.is-active):is(:hover, :focus-visible) .gamepad-icon {
    transform: rotate(-10deg) scale(1.1);
  }
  .game-toggle.is-active:is(:hover, :focus-visible) .icon-close {
    transform: rotate(90deg) scale(1.15);
  }
}

/* ── Games Drawer (Native Popover & Top Layer Animation) ── */
.games-drawer {
  /* Default un-anchored fallback inside .home-intro */
  position: absolute;
  bottom: calc(100% + 16px);
  left: 0;
  width: min(260px, calc(100vw - var(--content-left) - var(--content-right)));
  margin: 0;
  padding: 0;
  border: none;
  background: transparent;
  overflow: visible;
  color: inherit;

  /* Closed / exit state */
  opacity: 0;
  visibility: hidden;
  transform: translateY(12px) scale(0.97);
  filter: blur(4px);
  pointer-events: none;

  /* Discrete display/overlay transitions for top layer entry and exit */
  transition-property: opacity, transform, filter, visibility, display, overlay;
  transition-duration: 260ms;
  transition-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
  transition-behavior: allow-discrete;
  z-index: 20;

  &::backdrop {
    background: transparent;
  }
}

/* Open state: matches native :popover-open or fallback .is-open class */
.games-drawer:is(:popover-open, .is-open) {
  opacity: 1;
  visibility: visible;
  transform: translateY(0) scale(1);
  filter: blur(0);
  pointer-events: auto;

  @starting-style {
    opacity: 0;
    transform: translateY(12px) scale(0.97);
    filter: blur(4px);
  }
}

/* Modern CSS Anchor Positioning when supported */
@supports (position-area: block-start) {
  .games-drawer {
    position-anchor: --game-toggle;
    position-area: block-start span-inline-end;
    position-try-fallbacks: flip-block, flip-inline;
    inset: auto;
    margin: 0;
  }
}

/* Fixed viewport fallback for browsers supporting Popover Top Layer without Anchor Positioning */
@supports not (position-area: block-start) {
  .games-drawer:popover-open {
    position: fixed;
    inset: auto auto calc(max(clamp(20px, 4.8dvh, 48px), var(--safe-bottom)) + 58px) var(--content-left);
    margin: 0;
  }
}

/* ── Games List & Game Cards ── */
.games-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
  width: 100%;

  li {
    list-style: none;
    margin: 0;
    padding: 0;
  }
}

.game-card {
  display: flex;
  align-items: center;
  gap: 13px;
  padding: 10px 14px;
  border-radius: 16px;
  text-decoration: none;
  color: var(--ink);
  background: var(--card-bg);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border: 1px solid var(--card-border);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.05), 0 1px 2px rgba(0, 0, 0, 0.03);
  transition: transform 200ms cubic-bezier(0.16, 1, 0.3, 1),
              background-color 200ms ease,
              border-color 200ms ease,
              box-shadow 200ms ease;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;

  &:active {
    transform: translateY(0);
    background: var(--card-hover-bg);
  }

  @media (hover: hover) {
    &:hover {
      background: var(--card-hover-bg);
      border-color: var(--card-hover-border);
      transform: translateY(-2px);
      box-shadow: 0 10px 24px rgba(0, 0, 0, 0.09);

      .card-icon {
        transform: scale(1.06);
      }
      .card-arrow {
        opacity: 1;
        color: var(--ink);
        transform: translateX(3px);
      }
    }
  }

  &:focus-visible {
    outline: 2px solid var(--ink);
    outline-offset: 3px;
  }
}

.card-icon {
  width: 40px;
  height: 40px;
  border-radius: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: transform 200ms ease;

  svg {
    width: 23px;
    height: 23px;
  }
}

.tetris-theme {
  background: light-dark(#ebf0f5, #222d38);
  color: light-dark(#2c3e50, #90b4d6);
}
.num2048-theme {
  background: light-dark(#fdf5e7, #382c1e);
  color: light-dark(#d48b1b, #f0b24d);
}
.sudoku-theme {
  background: light-dark(#e6f4ea, #1b3329);
  color: light-dark(#0f766e, #5eead4);
}

.card-body {
  flex: 1;
  min-width: 0;
}

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-title {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
  line-height: 1.3;
  color: var(--ink);
  letter-spacing: -0.01em;
}

.card-arrow {
  margin-left: auto;
  color: var(--muted);
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  opacity: 0.45;
  transition: opacity 180ms ease, color 180ms ease, transform 180ms ease;

  svg {
    display: block;
    width: 100%;
    height: 100%;
  }
}

@media (max-width: 520px) {
  .game-card {
    padding: 9px 12px;
    gap: 12px;
  }
  .card-icon {
    width: 36px;
    height: 36px;
    border-radius: 10px;

    svg {
      width: 20px;
      height: 20px;
    }
  }
  .card-title {
    font-size: 15px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .github-icon,
  .email-icon,
  .gamepad-icon,
  .close-icon,
  .icon-game,
  .icon-close,
  .card-icon,
  .card-arrow {
    transition: none !important;
    transform: none !important;
  }
  .games-drawer {
    transition-duration: 0.1s !important;
    transform: none !important;
    filter: none !important;
  }
}
