import { css } from 'lit';
import componentStyles from '../../styles/component.styles.mjs';

const styles = css`
  ${componentStyles}

  :host {
    --width: 31rem;
    --header-spacing: var(--flow-ui-spacing-large);
    --body-spacing: var(--flow-ui-spacing-large);
    --footer-spacing: var(--flow-ui-spacing-large);

    display: contents;
  }

  :host([tabbed]) {
    --header-spacing: var(--flow-ui-spacing-medium) var(--flow-ui-spacing-x-large);
    --body-spacing: 0 var(--flow-ui-spacing-x-large);
  }

  /* Remove margins from first and last items slotted into dialog body */
  ::slotted(:nth-child(1 of :not([slot]))) {
    margin-top: 0;
  }

  ::slotted(:nth-last-child(1 of :not([slot]))) {
    margin-bottom: 0;
  }

  .dialog {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: var(--flow-ui-z-index-dialog);
  }

  .dialog__panel {
    display: flex;
    flex-direction: column;
    z-index: 2;
    width: var(--width);
    max-width: calc(100% - var(--flow-ui-spacing-2x-large));
    max-height: calc(100% - var(--flow-ui-spacing-2x-large));
    background-color: var(--flow-ui-dialog-background-color);
    border-radius: var(--flow-ui-border-radius-medium);
    box-shadow: var(--flow-ui-shadow-x-large);
  }

  .dialog__panel:focus {
    outline: none;
  }

  /* Ensure there's enough vertical padding for phones that don't update vh when chrome appears (e.g. iPhone) */
  @media screen and (max-width: 420px) {
    .dialog__panel {
      max-height: 80vh;
    }
  }

  .dialog--open .dialog__panel {
    display: flex;
    opacity: 1;
  }

  .dialog__header {
    flex: 0 0 auto;
    display: flex;
  }

  .dialog__title {
    flex: 1 1 auto;
    font: inherit;
    font-size: var(--flow-ui-font-size-large);
    line-height: var(--flow-ui-line-height-dense);
    padding: var(--header-spacing);
    margin: 0;
  }

  .dialog__header-actions {
    flex-shrink: 0;
    display: flex;
    flex-wrap: wrap;
    justify-content: end;
    gap: var(--flow-ui-spacing-2x-small);
    padding: var(--header-spacing);
  }

  .dialog__header-actions flow-ui-icon-button,
  .dialog__header-actions ::slotted(flow-ui-icon-button) {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    font-size: var(--flow-ui-font-size-large);
  }

  .dialog__body {
    flex: 1 1 auto;
    display: block;
    padding-inline: var(--body-spacing);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }

  .dialog__footer {
    flex: 0 0 auto;
    text-align: right;
    padding: var(--footer-spacing);
  }

  .dialog__footer ::slotted(flow-ui-button:not(:first-of-type)) {
    margin-inline-start: var(--flow-ui-spacing-small);
  }

  .dialog:not(.dialog--has-footer) .dialog__footer {
    display: none;
  }

  .dialog__overlay {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: var(--flow-ui-overlay-background-color);
  }

  /* Tabbed dialogs */
  .dialog__tab-group {
    flex: 1;
    min-height: 0;
  }

  .dialog__tab-group ::slotted(flow-ui-tab-panel) {
    --padding: var(--body-spacing);
  }

  @media (forced-colors: active) {
    .dialog__panel {
      border: solid 1px var(--flow-ui-color-neutral-0);
    }
  }
`;

export { styles as default };
