.application {
  position: absolute;
  z-index: var(--z-index-window);
  transform-origin: top left;

  display: flex;
  flex-flow: column nowrap;
  min-height: calc(var(--header-height) + 1rem);
  min-width: 200px;
  max-height: calc(100vh - 1.5 * var(--hotbar-height));
  max-width: 100%;
  overflow: hidden;

  body.detached & {
    max-height: none;
    max-width: none;
    &.minimizing, &.maximizing {
      max-height: 100vh;
      max-width: 100vw;
    }
  }

  background: var(--background);
  font-size: var(--font-size-14);
  color: var(--color-text-primary);
  color-scheme: var(--color-scheme);

  border: 1px solid var(--color-border);
  border-radius: 6px;
  box-shadow: 0 0 10px #000;

  // Closing or minimizing the Application
  &.minimizing, &.minimized, &.maximizing {
    min-width: unset;
    min-height: unset;
  }
  &.minimizing {
    transition: max-height 0.25s ease-out, max-width 0.25s ease-out;
  }
  &.maximizing {
    transition: max-height 0.25s ease-in, max-width 0.25s ease-in;
  }
  &.minimized .window-content,
  &.minimizing .window-content,
  &.maximizing .window-content {
    display: none;
  }

  // All Applications have window content
  .window-content {
    flex: 1;
    display: flex;
    flex-flow: column nowrap;
    padding: var(--spacer-16);
    overflow: hidden;
  }

  // Framed applications have a window header
  .window-header {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    flex: 0 0 var(--header-height);
    position: relative;
    overflow: hidden;
    padding: 0 0.5rem;
    gap: 0.25rem;
    z-index: 1;
    cursor: var(--cursor-grab);

    background: var(--color-header-background);
    font-size: var(--font-size-13);
    color: var(--color-light-1);
    border-bottom: 1px solid var(--color-border);

    &:active {
      cursor: var(--cursor-grab-down);
    }

    .window-icon {
      flex: none;
    }

    .window-title {
      flex: 1;
      font-size: inherit;
      font-family: inherit;
      font-weight: inherit;
      margin: 0;
      line-height: var(--header-height);
      border: none;
      overflow: hidden;
      color: var(--color-light-1);
      text-align: left;
      white-space: nowrap;
      text-overflow: ellipsis;
      color: var(--color-light-1);
    }

    button.header-control {
      --button-text-color: var(--color-light-1);
      --button-background-color: none;
      --button-size: 1.5rem;
      border: none;
      line-height: normal;
      margin: 0;
    }
  }

  // Only show the close button when the application is minimized
  &.minimized .header-control {
    &:not([data-action=close]) { display: none; }
  }

  .window-resize-handle {
    position: absolute;
    inset: auto 1px 1px auto;
    width: 11px;
    height: 11px;
    cursor: nwse-resize;
    background: transparent url("../ui/resize-handle.webp") no-repeat center / contain;
  }
  &.minimized .window-resize-handle {
    display: none;
  }
}

/* ---------------------------------------- */
/* Category Browser                         */
/* ---------------------------------------- */

.category-browser {
  .window-content {
    flex-direction: row;
    gap: 0;
  }
  .loading {
    margin: 45% 1rem;
    text-align: center;
  }
  aside {
    flex: 0 0 220px;
    border-right: var(--sidebar-separator);
    margin-right: 16px;
    padding-right: 16px;
    input[type=search]::placeholder {
      font-family: var(--font-awesome);
      font-weight: 900;
      position: relative;
      top: 1px;
    }
    nav.tabs {
      padding: 1rem var(--scroll-margin) 1rem 0.5rem;
      > button {
        border-radius: 0;
        flex-wrap: nowrap;
        gap: 1rem;
        height: initial;
        justify-content: space-between;
        position: relative;
        transition: none;
        > i.status {
          font-size: var(--font-size-10);
          position: absolute;
          inset-inline-start: -1rem;
          top: 10px;
          text-align: center;
        }
        &.filter-in > i.status { color: var(--color-text-emphatic); }
        &.filter-out, &.no-matches {
          color: var(--color-text-subtle);
          .count {
            color: inherit;
          }
        }
        .count {
          color: var(--color-text-secondary);
        }
      }
    }
    button.reset-defaults {
      margin-top: auto;
    }
  }
  .main {
    flex: 0 1 100%;
    gap: 1rem;
    .categories {
      flex: 1;
      height: 0;
      section.tab {
        flex: 1;
        height: 0;
        padding-top: 0.5rem;
      }
    }
    &.interstitial .categories {
      overflow: hidden
    }
  }
}

/**
 * Reduced Transparency Preferences
 */
@media (prefers-reduced-transparency) {
  .application .window-content {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
  .app {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
  #tooltip,
  .locked-tooltip {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
}

/* -------------------------------------------- */
/*  Document Ownership                          */
/* -------------------------------------------- */

.document-ownership {
  p.instructions {
    margin: 0;
  }
  &.no-gm .gm {
    display: none;
  }
  .author {
    color: var(--color-text-secondary);
  }
  .role-icon {
    color: var(--color-text-subtle);
    font-size: var(--font-size-12);
  }
}

/* ---------------------------------------- */
/* Roll Fulfillment                         */
/* ---------------------------------------- */

.application.roll-resolver {
  --flash-color: #ffff004d;

  section[data-application-part] { gap: .5rem; }
  .input-grid { --grid-cols: 3; }

  .formula {
    padding: .5rem .5rem 0;
    font-size: var(--font-size-18);
    text-align: center;
  }

  label.die-input > input {
    text-align: center;
    font-size: var(--font-size-16);
  }

  .form-fields {
    display: flex;
    gap: .125rem;

    > label { flex: 1; }
    > button {
      flex: none;
      width: unset;
      > i { margin: 0; }
    }
  }

  .new-addition { animation: background-flash 400ms ease-in-out; }

  @keyframes background-flash {
    0% { background-color: transparent; }
    50% { background-color: var(--flash-color); }
    100% { background-color: transparent; }
  }
}

/* ----------------------------------------- */
/*  Region Legend                            */
/* ----------------------------------------- */

#region-legend {
  max-height: calc(100% - 200px);

  .window-content {
    padding: 0.5rem;
  }

  #region-legend-list {
    height: 0; // To ensure flex
    flex: 1;
    gap: 0.5rem;
  }

  .region-filters {
    gap: 0.5rem;
    input[name="elevationBottom"], input[name="elevationTop"] {
      flex: 0 0 50px;
      &::placeholder {
        font-size: var(--font-size-12);
      }
    }
  }

  .region-list {
    --scroll-margin: 0.5rem;
    flex: 1;
    margin: 0;
    padding-left: 0;
  }

  .region {
    gap: 0.25rem;
    padding: 0.25rem 0;
    border-top: 2px groove transparent;
    border-bottom: 2px groove transparent;
    &.hovered {
      border-color: var(--color-border-dark-secondary);
    }
    &.controlled {
      border-color: var(--color-border-highlight);
    }
  }
  .region-color {
    flex: none;
    height: 16px;
    width: 16px;
    border: 2px solid #000;
    border-radius: 3px;
  }
  .region-name {
    margin: 0;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  .region-meta {
    flex: none;
    color: var(--color-form-hint);
    font-size: var(--font-size-12);
  }
  button.icon {
    font-size: var(--font-size-12);
  }
}

/* ----------------------------------------- */
/*  Image Popout                             */
/* ----------------------------------------- */

.application.image-popout {
  background: var(--color-cool-5-75);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  figure {
    margin: 0;
    padding: 1rem;
    flex-grow: 1;
    img, video {
      border: none;
      box-shadow: none;
      flex: 1;
      object-fit: contain;
      object-position: 50% 50%;
    }
    figcaption {
      font-size: var(--font-size-16);
      margin-top: 1rem;
    }
  }
  .window-resize-handle { filter: invert(1); }
}

// Allow full height on small screens
@media (max-height: 850px) {
  .application {
    max-height: 100vh;
  }
}

/* -------------------------------------------- */
/*  Formula Editor                              */
/* -------------------------------------------- */

.dialog.formula-editor {
  header { --header-height: 24px; }

  [contenteditable] {
    flex: 1;
    outline: none;
    cursor: auto;
    font-family: monospace;
    font-size: 13px;
  }

  data {
    display: inline-block;
    padding: 1.5px 5px;
    border-radius: 3px;
    border: 1px solid var(--color-data-border);
    background: var(--color-data-background);
  }
}
