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

// TODO split up into more manageable bits

@mixin btn-transition {
  transition: var(--transition-standard);
  opacity: 0;
  position: relative;
  z-index: 2;
  margin: var(--s) 0 0 0;
}

:host {
  display: block;
  position: relative;
  user-select: none;
  --header-nav-button-gap: var(--s-half);
  --header-button-size: 40px;
}

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

:host-context(.isNoMac.isObsidianStyleHeader.isElectron) {
  // Dynamic window controls width using Window Controls Overlay CSS env vars
  // env(titlebar-area-width) = available width (viewport - controls)
  // So: controls width = 100vw - titlebar-area-width
  // Fallback calc ensures 140px when env vars unavailable
  // Only on non-Mac: macOS window controls are on the left, no right padding needed
  padding-right: calc(100vw - env(titlebar-area-width, calc(100vw - 140px)));

  // Prevent header content from overflowing into the window controls area
  // when the window is resized to a narrow width (fixes #6365).
  // Uses clip instead of hidden to avoid affecting vertical overflow (dropdowns).
  .wrapper {
    overflow-x: clip;
  }
}

:host-context(.isMac.isElectron),
:host-context(.isObsidianStyleHeader.isElectron) {
  -webkit-app-region: drag;
  cursor: grab;

  .wrapper {
    -webkit-app-region: drag;
  }

  &:active {
    cursor: grabbing;
  }

  .current-work-context-title,
  ::ng-deep .current-task-title,
  button,
  a,
  ::ng-deep button,
  ::ng-deep a {
    -webkit-app-region: no-drag;
  }
}

.wrapper {
  background: transparent;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  padding: var(--s-half) var(--s);
  min-height: var(--bar-height-large);

  @include mq(xs) {
    min-height: var(--bar-height);
    padding: var(--s-half) var(--s2);
  }

  &:has(page-title:hover) ::ng-deep .current-task-title,
  &:has(page-title [aria-expanded='true']) ::ng-deep .current-task-title {
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-duration-m) ease-out;
  }
}

.project-settings-btn {
  display: none;
  @include mq(xs) {
    display: block;
    transition: var(--transition-standard);
    opacity: 0;
    margin-right: var(--s2);
    margin-left: calc(var(--s) * -1);
    position: relative;
    z-index: 1;
  }

  &:hover,
  .current-work-context-title:hover + & {
    opacity: 1;
  }
}

a,
button {
  cursor: pointer;
}

button.isActive2 {
  background: var(--c-accent);
  transition: background var(--transition-standard);
}

.counters-action-group {
  display: flex;
  align-items: center;
  gap: var(--header-nav-button-gap);
  flex-shrink: 0;
  margin-right: calc(var(--s) * 1.5);

  ::ng-deep button {
    width: 36px;
    height: 36px;

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

.action-nav-right {
  margin-right: 0;
  margin-left: auto;
  display: flex;
  align-items: center;
  // Keep the action group at its intrinsic width so a long title shrinks
  // (it ellipsizes) instead of pushing these buttons off screen (#7477).
  flex: 0 0 auto;
  gap: var(--header-nav-button-gap);
  min-width: 0;

  :host-context([dir='rtl']) & {
    margin-left: 0;
    margin-right: auto;
  }

  // EXPERIMENT: vertical action strip styles live in src/styles.scss so they
  // bypass Angular view encapsulation (and any containing-block surprises
  // from this file). See `nav.action-nav-right` rules there.

  // Set 20px icon sizing for buttons outside counters-action-group
  // (add button, panel buttons, etc.)
  > button:not(.sync-btn) .mat-icon:not(.sync-state-ico),
  ::ng-deep
    > *:not(.counters-action-group):not(.sync-btn)
    .mat-icon:not(.sync-state-ico) {
    font-size: 20px;
    width: 20px;
    height: 20px;
    line-height: 20px;
  }

  > button.sync-btn .mat-icon:not(.sync-state-ico) {
    font-size: 24px;
    width: 24px;
    height: 24px;
    line-height: 24px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transform-origin: center center;
    backface-visibility: hidden;
  }
}

page-title {
  position: relative;
}

.mobile-dropdown-wrapper {
  position: relative;
}

.mobile-dropdown {
  position: absolute;
  display: flex;
  transition: var(--transition-standard);
  top: 100%;
  flex-direction: column;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  pointer-events: none;
  // Scrolling support for many counters (fixes #5999)
  max-height: calc(100dvh - 120px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding-bottom: var(--s);

  &.isVisible {
    pointer-events: all;
    border-radius: var(--card-border-radius);
    background: var(--card-bg);
    box-shadow: var(--whiteframe-shadow-2dp);
  }

  simple-counter-button {
    @include btn-transition;
    transform: translateY(-100%);
    opacity: 0;
    position: relative;
    z-index: 2;
    margin-top: var(--s);
    margin-left: 0;
    flex-shrink: 0;

    @for $i from 2 through 7 {
      &:nth-child(#{$i}) {
        transform: translateY(#{$i * -100%});
      }
    }
  }

  &.isVisible simple-counter-button {
    transform: translateY(0);
    opacity: 1;
  }
}

.panel-btn {
  position: relative;
  transition: var(--transition-standard);
  overflow: visible !important;

  &.isActive {
    background: transparent;

    .mat-icon {
      transition: var(--transition-standard);
      transform: rotate(45deg);
    }

    &::after {
      border-radius: var(--card-border-radius);
      box-shadow: 0 -2px 3px var(--separator-alpha);
      background: var(--sidenav-bg);
      content: '';
      width: 100%;
      position: absolute;
      top: 0;
      right: auto;
      bottom: -5px;
      left: 1px;
      z-index: -1;
    }

    &.isCustomized::after {
      background: var(--c-accent);
    }
  }

  &:hover:not(.isActive):not(:disabled) {
    background: var(--hover-color, rgba(0, 0, 0, 0.04));
  }

  &:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background: transparent !important;

    &::after {
      background: transparent !important;
    }
  }
}

.spin {
  animation: spin 1.5s linear infinite;
  will-change: transform;
  transform-origin: center center;
}

@keyframes spin {
  from {
    transform: rotate(0deg) translateZ(0);
  }

  to {
    transform: rotate(-360deg) translateZ(0);
  }
}

.current-task-title {
  position: absolute;
  right: 100%;
  width: auto;
  border: 2px solid var(--c-accent);
  border-radius: 12px;
}

.sync-btn {
  position: relative;
}

.sync-state-ico {
  // Positioning - center within sync button
  position: absolute !important;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  // Icon sizing
  font-size: 15px;
  width: 15px;
  height: 15px;
  line-height: 15px;

  // Solid circle background for clean outline
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--c-bg);
  border-radius: 50%;
  padding: 3px;

  // Ensure it sits above sync icon
  z-index: 1;

  // Fallback for Windows High Contrast Mode
  @media (forced-colors: active) {
    outline: 2px solid CanvasText;
    outline-offset: -2px;
  }
}
