* {
  box-sizing: border-box;
}

:root {
  /* Using color-scheme and light-dark allows us to easily set up light and dark color schemes for simple websites */
  color-scheme: light dark;

  &:has(#color-scheme-light:checked) {
    color-scheme: light;
  }
  &:has(#color-scheme-dark:checked) {
    color-scheme: dark;
  }
  
  /* using oklch allows for more intuitive color changes, and using from allows for one color to affect the whole color scheme*/
  --bg-dark: light-dark(
    oklch(from var(--bg) 0.80 c h), 
    oklch(from var(--bg) 0.15 c h));
  --bg: light-dark(
    oklch(0.90 0.04 148),
    oklch(0.25 0.03 148));
  --bg-light: light-dark(
    oklch(from var(--bg) 0.95 c h),
    oklch(from var(--bg) 0.30 c h));
  /* Using a small amount of background colors limits design, and helps to pull focus to elements while discouraging too much extra nesting. */
  
  --text: light-dark(
    oklch(from var(--bg) 0.1 0.01 h),
    oklch(from var(--bg) 0.95 0.03 h));
  --text-muted: light-dark(
    oklch(from var(--text) 0.3 c h),
    oklch(from var(--text) 0.7 c h));
  
  --shadow: light-dark(
    oklch(from var(--bg) 0.6 0.05 h),
    oklch(from var(--bg) 0.1 0.01 h));
  
  --border: light-dark(
    oklch(from var(--bg) 1 0.03 h),
    oklch(from var(--bg) 0.45 0.03 h));
  
  --pad: 1rem;
}

html {
  scroll-behavior: smooth;
  scrollbar-color: var(--bg) var(--bg-dark);
  
  /* prevent the document from scrolling while a popover is open */
  &:has(:popover-open) {
    overflow-y: hidden;
    scrollbar-gutter: stable;
  }
}

body {
  background-color: var(--bg-dark);
  color: var(--text);
  padding: 0;
  margin: 0;
  font-family: sans-serif;
}

.main-header {
  display: flex;
  align-items: center;
  background-color: var(--bg-light);
  line-height: 4rem;
  
  h1 {
    margin: 0;
    font-size: 1.5rem;
    margin-inline-end: auto;
    position: relative;
  }
  
  @media (width <= 800px) {
    line-height: 3rem;
    h1 {
      font-size: 1.2rem;
    }
  }
  
  button {
    border-width: 0;
    background-color: transparent;
    color: inherit;
    font-size: 2rem;
    padding-inline: 1rem;
    cursor: pointer;
    line-height: inherit;
    &:hover {
      background-color: var(--bg);
    }
  }
  
  ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    display: flex;
    
    gap: 0.5rem;
    align-items: center;
  }
  a {
    color: inherit;
    font-weight: bold;
    text-decoration: none;
    display: inline-block;
    padding-inline: 1rem;
    &:hover {
      background-color: var(--bg);
    }
  }
}
.main-nav {
  display: flex;
  @media (width <= 800px) {
    display: none;
  }
}

#main-nav-popover {
  inset: unset;
  padding: 0;
  right: 0;
  top: 0;
  border-width: 0;
  height: 100%;
  overflow-y: auto;
  width: min(100vi - 2rem, 30ch);
  background-color: var(--bg-light);
  box-shadow: 0 0 30px var(--shadow);
  transition: display 0.3s allow-discrete,
    overlay 0.3s allow-discrete;
  
  &::backdrop {
    background-color: rgb(0 0 0 / 0.4);
    backdrop-filter: blur(3px);
  }
  
  ul {
    flex-direction: column;
    align-items: stretch;
    line-height: 2rem;
    gap: 1rem;
    padding-block-start: 2rem;
    a {
      display: block;
      &:hover {
        background-color: var(--bg);
      }
    }
    button {
      width: 100%;
      text-align: right;
    }
  }
 }




/* Because of display:none, we need to use animations for the transitions of a popover */
#main-nav-popover {
  animation: slideOut 0.3s forwards;
  &:popover-open {
    animation: slideIn 0.3s forwards;
    &::backdrop {
      animation: backdropFadeIn 0.3s forwards;
    }
  }
  &::backdrop {
    animation: backdropFadeOut 0.3s forwards;
  }
}
@keyframes slideIn {
  from { translate: 100%; }
  to { translate: 0; }
}

@keyframes slideOut {
  from { translate: 0; }
  to { translate: 100%; }
}
@keyframes backdropFadeIn {
  from {
    background: rgb(0 0 0 / 0%);
    backdrop-filter: blur(0);
  }
  to {
    background: rgb(0 0 0 / 40%);
    backdrop-filter: blur(4px);
  }
}
@keyframes backdropFadeOut {
  from {
    background: rgb(0 0 0 / 40%);
    backdrop-filter: blur(4px);
  }
  to {
    background: rgb(0 0 0 / 0%);
    backdrop-filter: blur(0);
  }
}



/* This is my current solution for a container element that allows a breakout element */
/* The padder can applied to any top level element like a container and doesn't do anything but add padding */
.padder {
  --padding-min: 0.5rem;
  --width-max: 800px;
  --padding: max(var(--padding-min), (100vw - var(--width-max)) / 2);
  padding-inline: var(--padding);
  container-type: inline-size;
  /* The breakout applies padding margin and width, so let it place its children, and style those */
  .breakout {
    width: calc(100cqi + (2 * var(--padding)));
    margin-inline-start: calc(-1 * var(--padding));
    padding-inline: var(--padding);
  }
}















/* here be some design */
main {
  /* Push down the bottom of the page */
  margin-block-end: min(20ch,50vh);
  
  button {
    background-color: var(--bg-light);
    border-width: 0;
    padding: 0.4em 0.5em;
    border-radius: 0.3em;
    cursor: pointer;
  }
  [popover] {
    background-color: var(--bg-light);
    padding: 1rem;
    border-width: 0;
    border-radius: 0.5rem;
    &::backdrop {
      background-color: rgb(0 0 0 / 0.8);
    }
  }
  [animated] {
    transition: display 0.3s allow-discrete,
      overlay 0.3s allow-discrete;
    animation: pop-slideOut 0.3s forwards;
    &:popover-open {
      animation: pop-slideIn 0.3s forwards;
      &::backdrop {
        animation: pop-fadeIn 0.3s forwards;
      }
    }
    &::backdrop {
      animation: pop-fadeOut 0.3s forwards;
    }
  }
  
  :where(ul, ol, p) {
    line-height: 1.4;
    margin-block: 1.4rem;
  }
  :where(ul,ol) :where(ul,ol) {
    margin-block: 0;
  }
}
@keyframes pop-slideIn {
  from { translate: 0 -1em; opacity: 0; }
  to { translate: 0 0; opacity: 1; }
}
@keyframes pop-slideOut {
  from { translate: 0 0; opacity: 1; }
  to { translate: 0 1em; opacity: 0; }
}
@keyframes pop-fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes pop-fadeOut {
  from { opacity: 1; }
  to { opacity: 0; }
}

section {
  

  /* The h2 will sticky to the top */
  > .heading {
    position: sticky;
    top: 0;
    background-color: var(--bg);
    color: var(--text-muted);
    line-height: 3rem;
    box-shadow: 0 0.5em 0.5em rgb(from var(--shadow) r g b / 0.5);
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    h2 {
      margin-block: 0;
      font-size: 1.2rem;

      @media (width <= 800px) {
        font-size: 1rem;
        white-space: nowrap;
        overflow-x: auto;
        line-height: 2.5rem;
      }
    }
  }
}

.card {
  display: flow-root;
  background-color: var(--bg);
  margin-block: 1rem;
  padding: 1rem;
  border-radius: 0.5rem;
  overflow-x: auto;
  
  > :first-child { margin-block-start: 0; }
  > :last-child { margin-block-end: 0; }
}
aside {
  width: 80%;
  margin: 1rem auto;
  padding: 1rem;
  border-radius: 0.5rem;
  background-image: linear-gradient(to bottom, var(--bg-light), var(--bg) 70%);
  border: 2px solid var(--bg-light);
  p {
    margin: 0;
  }
  @media (width <= 800px) {
    width: calc(100cqi - 2rem);
  }
}

code {
  display: inline;
  background-color: var(--bg);
  padding: 0.2rem;
  border-radius: 3px;
  white-space: nowrap;
  
  &[block] {
    display: block;
    margin-block: 1rem;
    white-space: pre;
    padding: 0.5rem 0.8rem;
    border-radius: 5px;
    border: 2px solid var(--border);
    box-shadow: 0 0 20px rgb(from var(--shadow) r g b / 0.7);
    overflow-x: auto;
  }
  .card & {
    background-color: var(--bg-light);
  }
}


.sticky-example {
  width: min(100cqi - 4rem, 600px);
  margin-inline: auto;
  background-color: var(--bg-light);
  overflow: auto;
  border: 2px solid var(--border);
  
  [sticky] {
    position: sticky;
    top: 0;
    & ~ div > [sticky] {
      top: 3rem;
      z-index: 1;
    }
    &:has( ~ div > [sticky]) {
      z-index: 2;
    }
  }
  h2 {
    font-size: 1rem;
    padding: 0.5em;
    background-color: var(--bg); 
    margin: 0;
  }
  p {
    padding: 0.5rem;
    margin: 0;
  }
  h1 {
    background-color: var(--bg-dark);
    margin: 0;
    line-height: 3rem;
    font-size: 1.2rem;
    padding-inline: 0.5rem;
  }
}





/* body:has([only]) {
  section:not([only]) {
    display: none;
  }
} */

.radio-group {
  display: inline-flex;
  background-color: var(--bg-light);
  border-radius: 1rem;
  overflow: hidden;
  line-height: 2rem;
  border: 2px solid var(--border);
  
  input {
    display: none;
  }
  
  label {
    flex: 0;
    padding-inline: 0.5rem;
    cursor: pointer;
    
    &:hover {
      background-color: oklch(from var(--bg-light) calc(l + 0.1) c h);
    }
    
    &:first-of-type {
      padding-inline-start: 1rem;
    }
    &:last-of-type {
      padding-inline-end: 1rem;
    }
    
    #color-scheme-auto:checked ~ &:is([for="color-scheme-auto"]) {
      background-color: var(--bg);
    }
    #color-scheme-light:checked ~ &:is([for="color-scheme-light"]) {
      background-color: var(--bg);
    }
    #color-scheme-dark:checked ~ &:is([for="color-scheme-dark"]) {
      background-color: var(--bg);
    }
  }
}

