html,
body {
  height: 100%;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

html {
  background: #f4efe4;
}

body {
  margin: 0;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 100dvh;
  width: 100%;
  display: flex;
  flex-direction: column;
  padding-top: var(--memory-nav-height, 60px);
  box-sizing: border-box;
  background: #f4efe4 url("assets/fond.webp") center / cover no-repeat;
  color: #3f2e1f;
  font-family: var(--font-sans, system-ui, sans-serif);
}

.game-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  height: auto;
  min-height: calc(
    60px + max(env(safe-area-inset-top, 0px), var(--memory-obstructed-top, 0px))
  );
  box-sizing: border-box;
  padding-top: max(
    env(safe-area-inset-top, 0px),
    var(--memory-obstructed-top, 0px)
  );
  background: transparent;
  border-bottom: 0;
  box-shadow: none;
}

.nav-left {
  gap: 0.5rem;
}

.game-nav .back-button[hidden] {
  display: none;
}

.memory-app {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  box-sizing: border-box;
  padding-bottom: max(
    40px,
    calc(env(safe-area-inset-bottom, 0px) + 24px),
    var(--memory-obstructed, 0px) + 8px
  );
  padding-left: max(24px, env(safe-area-inset-left, 0px));
  padding-right: max(24px, env(safe-area-inset-right, 0px));
}

/* Phone navigation is often outside both the safe area and the visual
   viewport, so a touch screen always keeps a 48px empty strip at the bottom. */
@media (pointer: coarse) {
  .memory-app {
    padding-bottom: max(
      48px,
      calc(env(safe-area-inset-bottom, 0px) + 24px),
      var(--memory-obstructed, 0px) + 8px
    );
  }
}

.memory-setup,
.memory-play {
  flex: 1 1 auto;
  height: auto;
  min-height: 0;
}

.memory-setup {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0.75rem;
  box-sizing: border-box;
}

/* The panel shrinks to the space left (no percentage max-height: Safari does
   not resolve it here); overflow-y is only the last resort. */
.memory-panel {
  flex: 0 1 auto;
  min-height: 0;
  width: min(100%, 26.25rem);
  overflow-y: auto;
  overscroll-behavior: contain;
  background: #fff;
  border-radius: 1.25rem;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
  box-sizing: border-box;
}

.memory-resume {
  flex: 0 0 auto;
  width: min(100%, 26.25rem);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 0.625rem;
}

.memory-label {
  margin: 0;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #64748b;
}

.memory-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.memory-chip,
.memory-start,
.memory-text {
  font: inherit;
  cursor: pointer;
  border-radius: 999px;
}

.memory-chip {
  border: 1px solid #cbd5e1;
  background: #fff;
  padding: 0.5rem 0.75rem;
  font-weight: 700;
}

.memory-chip.is-selected {
  background: #1d4ed8;
  border-color: #1d4ed8;
  color: #fff;
}

.memory-start,
.memory-text {
  border: 0;
  background: #1d4ed8;
  color: #fff;
  font-weight: 800;
  padding: 0.75rem 1rem;
}

.memory-start[aria-disabled="true"] {
  opacity: 0.7;
  cursor: wait;
}

.memory-choice {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
  min-width: 0;
}

.memory-setup-column {
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
  min-width: 0;
}

/* Theme tiles are the only part that gives up height when the screen is
   short. Their list must be a direct child of the column that is height
   constrained, or its wrappers shrink past its minimum and the tiles overlap. */
.memory-setup-panel {
  container-type: inline-size;
}

.memory-setup-looks,
.memory-theme-choice {
  display: contents;
}

.memory-theme-list {
  --theme-rows: 3;
  flex: 0 1 auto;
  height: calc(
    (100cqw - 1rem) / 4.5 * var(--theme-rows) + (var(--theme-rows) - 1) * 0.5rem
  );
  min-height: calc(
    var(--theme-rows) * 1.5rem + (var(--theme-rows) - 1) * 0.5rem
  );
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-auto-rows: minmax(0, 1fr);
  gap: 0.5rem;
}

.memory-back-list {
  display: flex;
  gap: 0.5rem;
  overflow-x: auto;
  padding: 0.1875rem;
  scroll-snap-type: x mandatory;
  scrollbar-width: thin;
}

.memory-theme,
.memory-back {
  position: relative;
  padding: 0;
  border: 2px solid transparent;
  border-radius: 0.75rem;
  overflow: hidden;
  background: #e2e8f0;
  font: inherit;
  cursor: pointer;
}

.memory-theme.is-selected,
.memory-back.is-selected {
  border-color: #1d4ed8;
  box-shadow: 0 0 0 2px #1d4ed8;
}

.memory-theme img,
.memory-back img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

.memory-theme {
  min-height: 0;
}

.memory-theme img {
  position: absolute;
  inset: 0;
}

.memory-theme-name {
  position: absolute;
  inset: auto 0 0;
  padding: 0.625rem 0.25rem 0.1875rem;
  background: linear-gradient(transparent, rgba(15, 23, 42, 0.85));
  color: #fff;
  font-size: 0.75rem;
  font-weight: 800;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.memory-back {
  flex: 0 0 3.5rem;
  height: 3.5rem;
  scroll-snap-align: start;
}

.memory-text-ghost {
  background: #e2e8f0;
  color: #0f172a;
}

.memory-play[hidden],
.memory-setup[hidden],
.memory-resume[hidden],
.memory-end[hidden],
.memory-choice[hidden] {
  display: none;
}

.memory-lang {
  display: flex;
  gap: 0.5rem;
}

@media (orientation: landscape) and (min-width: 640px) {
  .memory-setup-panel {
    flex-direction: row;
    align-items: stretch;
    gap: 1rem;
    width: min(100%, 51.25rem);
  }

  .memory-resume {
    flex-direction: row;
    width: min(100%, 51.25rem);
  }

  .memory-resume .memory-start {
    flex: 1 1 0;
  }

  .memory-setup-column {
    flex: 1 1 0;
  }

  .memory-setup-looks {
    display: flex;
    container-type: inline-size;
  }

  .memory-setup-rules {
    justify-content: center;
  }
}

@media (max-height: 640px) {
  .memory-setup {
    padding: 0.375rem 0.75rem;
  }

  .memory-panel,
  .memory-setup-column {
    gap: 0.375rem;
  }

  .memory-resume {
    gap: 0.375rem;
    margin-bottom: 0.375rem;
  }

  .memory-resume .memory-start {
    padding: 0.4375rem 1rem;
  }

  .memory-panel {
    padding: 0.625rem 0.875rem;
  }

  .memory-back {
    flex-basis: 2.75rem;
    height: 2.75rem;
  }

  .memory-chip {
    padding: 0.375rem 0.625rem;
  }

  .memory-start {
    padding: 0.5625rem 1rem;
  }
}
