@use '../../../styles/_globals.scss' as *;

:host {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  display: flex;
  flex-direction: column;

  :host-context([dir='rtl']) {
    direction: rtl;
  }
}

:host-context(.isMac.isElectron) {
  padding-top: var(--mac-title-bar-padding);
}

// position:fixed bypasses body's safe-area padding, so apply it here so the
// formatting toolbar and controls stay clear of the status bar / home indicator.
:host-context(body.isNativeMobile) {
  padding-top: var(--safe-area-top);
  padding-bottom: var(--safe-area-bottom);
  padding-left: var(--safe-area-left);
  padding-right: var(--safe-area-right);
}

// Keyboard open: shrink the dialog above the IME, or its textarea + Close/Save
// get squashed/hidden behind the keyboard. Subtract only the keyboard amount —
// `:host` is border-box (global `* { box-sizing: border-box }`) so the
// padding-top/bottom safe areas above are ALREADY inside `height`; subtracting
// `--safe-area-top` again would double-remove the top inset and leave a
// safe-area-top-sized gap between the controls and the keyboard (#8508 API 28
// follow-up — invisible until --safe-area-top became non-zero on API < 30).
// `--keyboard-height` is the resize-detecting value (0 once the system/native
// shrank the window, the obscured amount otherwise), same signal as the
// add-task bar. Full rationale + device matrix in
// docs/android-edge-to-edge-keyboard.md (#8508).
// `:not(.isIOS)` makes this mutually exclusive with the iOS rule below — iOS
// carries BOTH isNativeMobile and isIOS and sets --keyboard-height non-zero, so
// without it the two equal-specificity rules would both match and iOS would
// depend on source order. Excluding iOS here removes that fragility.
:host-context(body.isNativeMobile:not(.isIOS).isKeyboardVisible) {
  height: calc(100% - var(--keyboard-height, 0px));
}

// iOS keeps its own override (different keyboard runtime: the WebView does not
// resize, so it uses `--keyboard-overlay-offset`). Left as-is — the
// `- --safe-area-top` term here is unverified on an iOS device and out of scope
// for the API 28 fix; check it separately if an iOS bottom gap shows up.
:host-context(body.isIOS.isKeyboardVisible) {
  height: calc(100% - var(--keyboard-overlay-offset, 0px) - var(--safe-area-top));
}

.formatting-toolbar {
  display: flex;
  flex-wrap: nowrap;
  overflow-x: auto;
  gap: var(--s-quarter);
  padding: var(--s) var(--s2);
  background-color: var(--bg-lightest);
  @include extraBorder('-bottom');
  position: relative;
  z-index: 1;
  color: var(--text-color);

  .toolbar-divider {
    width: 1px;
    flex-shrink: 0;
    background: var(--divider-color);
    margin: var(--s-half) var(--s-half);
  }

  .heading-label {
    font-weight: bold;
    font-size: 11px;
    line-height: 1;
  }

  .mat-mdc-icon-button {
    --mdc-icon-button-icon-size: 18px;
    min-width: 28px;
    width: 28px;
    height: 28px;
    padding: 0;
    line-height: 28px;
    flex-shrink: 0;

    mat-icon,
    .mat-icon {
      font-size: 18px;
      width: 18px;
      height: 18px;
      line-height: 18px;
    }
  }
}

.content {
  flex-grow: 1;
  position: relative;
  color: var(--text-color-more-intense);
  background-color: var(--bg-lightest);
  display: flex;
  flex-direction: column;
  // Allow proper flex shrinking
  min-height: 0;
  overflow: hidden;

  .editor-section {
    display: flex;
    flex-direction: column;
    flex-grow: 1;
    // Allow proper flex shrinking
    min-height: 0;
    overflow: hidden;

    textarea {
      flex-grow: 1;
      padding: var(--s) var(--s2);
      box-sizing: border-box;
      overflow: auto;
      background: inherit;
      color: inherit;
      border: none;
      overflow-wrap: break-word;
      display: block;
      resize: none;
      font-size: 14px;
      // Allow proper flex shrinking
      min-height: 0;

      @include scrollY;

      &:focus {
        outline: none;
      }
    }
  }

  .markdown-preview {
    flex-grow: 1;
    padding: var(--s) var(--s2);
    box-sizing: border-box;
    overflow: auto;

    @include scrollY;
  }

  &.split-view {
    flex-direction: row;

    .editor-section,
    .markdown-preview {
      width: 50%;
    }

    .markdown-preview {
      border-left: 2px solid;
      border-color: var(--extra-border-color);
    }
  }
}

.controls {
  flex-direction: row;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  box-shadow: var(--whiteframe-shadow-2dp);
  padding: 0 var(--s2);

  > button {
    margin: var(--s);
  }
}

.left-buttons {
  align-items: flex-start;
  flex-grow: 0;
  flex-shrink: 0;
  margin: var(--s);
  margin-right: auto;
}
