/* Center native overlays in the play area as soon as a result is shown. */
#overlay:has(> #result-panel:not([hidden])) {
  display: grid;
  grid-template: minmax(0, 1fr) / minmax(0, 1fr);
  place-items: center;
  padding: clamp(8px, 2vmin, 20px);
  background: rgb(10 13 18 / 42%);
}
#overlay > #result-panel:not([hidden]) {
  place-self: center;
  margin: 0;
  max-height: 100%;
}
#result.modal:not([hidden]) { place-items: center; }
#result .modal-box:has(.end-game-links) { max-height: calc(100dvh - 32px); overflow-y: auto; }
#res:not(.hide):has(> .card) {
  inset: 54px 0 0;
  width: auto;
  display: grid;
  grid-template: minmax(0, 1fr) / minmax(0, 1fr);
  place-items: center;
  padding: 10px;
}
#res:not(.hide) > .card {
  width: min(420px, 100%);
  margin: 0;
  max-height: 100%;
  overflow-y: auto;
  padding: 16px;
  border: 1px solid #aaa8;
  border-radius: 18px;
  background: var(--bg, #fffaf0);
}

.end-game-strip[aria-busy="true"] { visibility: hidden; }
.end-game-home {
  box-sizing: border-box;
  display: flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: fit-content;
  max-width: calc(100% - 20px);
  min-height: 44px;
  margin: 8px auto 0;
  padding: 6px 15px;
  border: 1px solid #dcb576;
  border-radius: 10px;
  background: #1a2131;
  color: #ffda9a;
  font: 750 clamp(14px, 1.6vw, 17px)/1.25 system-ui, sans-serif;
  text-align: center;
  text-decoration: underline;
  text-underline-offset: 3px;
}
.end-game-home:hover { background: #303950; color: #ffe7bd; }
.end-game-home:focus-visible { outline: 2px solid #ffda9a; outline-offset: 2px; }
nav.end-game-links {
  box-sizing: border-box;
  flex: none;
  min-width: 0;
  max-width: 100%;
  margin: 9px 0 0;
  padding: 7px 0 0;
  border: 0;
  border-top: 1px solid color-mix(in srgb, currentColor 20%, transparent);
  color: inherit;
  font: 600 13px/1.25 system-ui, sans-serif;
  text-align: start;
}
.end-game-strip {
  display: grid;
  grid-auto-flow: column;
  grid-template-rows: 44px;
  grid-auto-columns: clamp(128px, 45%, 186px);
  gap: 6px;
  padding: 2px;
  overflow-x: scroll;
  overscroll-behavior-x: contain;
  scroll-snap-type: x proximity;
  scrollbar-width: thin;
}
.end-game-rail { display: contents; }
.end-game-links:not([data-placement]) .end-game-card bdi {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
.end-game-links a.end-game-card {
  box-sizing: border-box;
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) auto;
  align-items: center;
  gap: 6px;
  min-width: 0;
  min-height: 44px;
  margin: 0;
  padding: 3px 5px 3px 3px;
  border: 1px solid color-mix(in srgb, currentColor 28%, transparent);
  border-radius: 8px;
  background: color-mix(in srgb, currentColor 6%, transparent);
  color: inherit;
  font: 650 12px/1.3 system-ui, sans-serif;
  text-align: start;
  text-decoration: none;
  scroll-snap-align: start;
}
.end-game-links .end-game-card img {
  display: block;
  width: 100%;
  height: 36px;
  object-fit: cover;
  border-radius: 5px;
}
.end-game-links .end-game-card bdi { min-width: 0; overflow-wrap: anywhere; }
.end-game-links .end-game-card span { opacity: .7; font: 600 13px/1 system-ui, sans-serif; }
.end-game-links .end-game-card:hover { background: color-mix(in srgb, currentColor 13%, transparent); }
.end-game-links .end-game-card:focus-visible { outline: 2px solid currentColor; outline-offset: -2px; }
.result-footer ~ nav.end-game-links { margin: 0; padding: 5px 12px 8px; }
#game > nav.end-game-links:not([data-placement]) { margin: 0; padding: 3px 8px; }

@media (min-height: 500px) {
  .end-game-links:not([data-placement]) .end-game-strip { grid-auto-columns: clamp(150px, 46%, 200px); grid-template-rows: 50px; }
  .end-game-links:not([data-placement]) .end-game-card {
    grid-template-columns: 56px minmax(0, 1fr) auto;
    min-height: 50px;
    font-size: 13px;
  }
  .end-game-links:not([data-placement]) .end-game-card img { height: 42px; }
}

@media (min-width: 800px) {
  /* These games end in a page section instead of an overlay. */
  .page:has(#summary-panel:not([hidden])) > .game-section,
  .page:has(#summary-panel:not([hidden])) > .game-sidebar {
    grid-column: 1 / -1;
    width: 100%;
    justify-self: center;
  }
  .page:has(#summary-panel:not([hidden])) > .game-sidebar {
    max-width: 480px;
    padding-inline-start: 0;
    border-inline-start: 0;
  }
  #game-workspace:has(> #summary-panel:not([hidden])) {
    max-width: 456px;
    margin-inline: auto;
    overflow: visible;
  }
  #game[data-end-surface] {
    width: min(510px, 100%);
    align-self: center;
  }
  nav.end-game-links[data-placement=sides]:popover-open,
  #summary-panel > nav.end-game-links[data-placement=sides] {
    position: fixed;
    inset: var(--end-top) var(--end-right) auto var(--end-left);
    width: auto;
    height: var(--end-height);
    max-width: none;
    max-height: none;
    margin: 0;
    padding: 0;
    border: 0;
    overflow: visible;
    background: transparent;
    color: #f5f3ef;
    pointer-events: none;
  }
  nav.end-game-links[data-placement=sides]:popover-open > .end-game-home {
    position: absolute;
    inset: auto auto calc(100% + 8px) 50%;
    min-height: 36px;
    margin: 0;
    transform: translateX(-50%);
    box-shadow: 0 2px 12px #0005;
    pointer-events: auto;
  }
  #summary-panel:has(> nav[data-placement=sides]) { position: relative; }
  #summary-panel > nav.end-game-links[data-placement=sides] {
    position: absolute;
    inset: 0 calc(-1 * (var(--end-width) + var(--end-gap)));
    height: auto;
  }
  .end-game-links[data-placement] .end-game-strip {
    display: flex;
    justify-content: space-between;
    height: 100%;
    padding: 0;
    overflow: visible;
    direction: ltr;
  }
  .end-game-links[data-placement] .end-game-rail {
    box-sizing: border-box;
    display: grid;
    grid-template-rows: repeat(3, minmax(0, 1fr));
    gap: clamp(6px, 1.2vh, 12px);
    width: var(--end-width);
    min-height: 0;
  }
  html[dir=rtl] .end-game-links[data-placement] .end-game-rail { direction: rtl; }
  .end-game-links[data-placement] a.end-game-card {
    display: grid;
    grid-template: minmax(0, 1fr) auto / minmax(0, 1fr);
    align-items: stretch;
    gap: 0;
    min-height: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid #ffffff40;
    border-radius: 12px;
    background: #181d26;
    color: #f8f7f3;
    box-shadow: 0 2px 10px #0003;
    pointer-events: auto;
  }
  .end-game-links[data-placement] .end-game-card img {
    min-height: 0;
    height: 100%;
    border-radius: 0;
  }
  .end-game-links[data-placement] .end-game-card bdi {
    padding: 5px 10px;
    font-size: clamp(13px, 1.45vw, 16px);
    line-height: 1.25;
  }
  .end-game-links[data-placement] a.end-game-card:hover {
    background: #2b3340;
    border-color: #ffffffa6;
  }
  .end-game-links[data-placement] .end-game-card span { display: none; }
}

/* Leave space for the two cover columns while giving results more room on desktop. */
@media (min-width: 800px) and (min-height: 500px) {
  html:not([data-ipad]) [data-end-surface],
  html:not([data-ipad]) #overlay > #result-panel[data-end-surface] {
    box-sizing: border-box;
    min-width: clamp(400px, 44vw, 550px);
  }
  html:not([data-ipad]) #game-workspace:has(> #summary-panel:not([hidden])) {
    max-width: clamp(456px, 45vw, 560px);
  }
  html:not([data-ipad]) dialog.result-dialog[data-end-surface] {
    min-width: clamp(430px, 54vw, 600px);
  }
  html:not([data-ipad]) [data-end-surface]:not(#summary-panel):not(#game) {
    min-height: min(62dvh, 450px);
  }
  html:not([data-ipad]) #overlay > #result-panel[data-end-surface] {
    min-height: min(100%, 62dvh, 450px);
  }
  html:not([data-ipad]) #overlay > #result-panel[data-end-surface]:has(> .result-body) {
    min-height: min(100%, 74dvh, 520px);
  }
  html:not([data-ipad]) #result-panel[data-end-surface] > .result-body {
    flex: 1 1 auto;
    padding: 22px 24px;
  }
  html:not([data-ipad]) #result-panel[data-end-surface] .result-heading {
    padding-bottom: 16px;
  }
  html:not([data-ipad]) #result-panel[data-end-surface] .result-heading h2 {
    font-size: clamp(25px, 2.8vw, 34px);
  }
  html:not([data-ipad]) #result-panel[data-end-surface] .result-heading .result-score strong {
    font-size: clamp(48px, 5.5vw, 64px);
  }
  html:not([data-ipad]) #result-panel[data-end-surface] .each { margin-top: 18px; }
  html:not([data-ipad]) #result-panel[data-end-surface] .each li { min-width: 48px; padding: 6px 10px; }
  html:not([data-ipad]) #result-panel[data-end-surface] .each li b { font-size: 21px; }
  html:not([data-ipad]) #result-panel[data-end-surface] .each li span { font-size: 16px; }
  html:not([data-ipad]) #result-panel[data-end-surface] .fine-print { margin-top: 9px; font-size: 14px; }
  html:not([data-ipad]) #result-panel[data-end-surface] > .result-footer {
    padding: 12px 20px 8px;
  }
}

.end-game-share {
  box-sizing: border-box;
  display: flex;
  flex: none;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 4px 8px;
  width: min(100%, 440px);
  margin: 8px auto 0;
  padding-inline: 8px;
  color: inherit;
  font: 600 13px/1.3 system-ui, sans-serif;
  text-align: center;
}
.end-game-share > button {
  box-sizing: border-box;
  min-width: 0;
  min-height: 44px;
  max-width: 100%;
  padding: 6px 12px;
  border: 1px solid currentColor;
  border-radius: 8px;
  background: color-mix(in srgb, currentColor 8%, transparent);
  color: inherit;
  font: inherit;
  cursor: pointer;
}
.end-game-share > button:hover { background: color-mix(in srgb, currentColor 16%, transparent); }
.end-game-share > :focus-visible { outline: 2px solid currentColor; outline-offset: 2px; }
.end-game-share > input {
  box-sizing: border-box;
  flex: 1 1 100%;
  min-width: 0;
  width: 100%;
  min-height: 36px;
  padding: 4px 8px;
  border: 1px solid currentColor;
  border-radius: 6px;
  background: #fff;
  color: #19202a;
  font: 500 16px/1.3 system-ui, sans-serif;
  user-select: text;
  -webkit-user-select: text;
}
.end-game-share > input[hidden] { display: none; }
#chroma-result-share {
  position: absolute;
  inset: auto 0 6px;
  z-index: 1;
  pointer-events: none;
}
#chroma-result-share .end-game-share > * { pointer-events: auto; }
#chroma-result-share .end-game-share > button {
  background: #153f30;
  color: #f7fce9;
}
#chroma-result-share .end-game-share > span:not(:empty) {
  padding: 5px 8px;
  border-radius: 6px;
  background: #153f30;
  color: #f7fce9;
}
