@import '../../utils/shared-styles.scss';

/* ===== SAFARI/iOS NUCLEAR TOUCH FIX ===== */
/* Safari hat einen Bug mit backdrop-filter + Touch Events */
/* Diese Fixes sind AGGRESSIV aber notwendig */

.site-header {
  /* ALLE klickbaren Elemente brauchen diese Fixes */
  a, button, [role="button"], .brand-btn, .nav-toggle, .dropdown-item {
    -webkit-tap-highlight-color: rgba(0,0,0,0) !important;
    -webkit-touch-callout: none !important;
    touch-action: manipulation !important;
    cursor: pointer !important;
    /* Eigener Compositing-Layer für jedes Element */
    -webkit-transform: translate3d(0,0,0);
    transform: translate3d(0,0,0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    /* Verhindert dass Safari Events "optimiert" */
    will-change: transform;
  }
}

.site-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background: rgba(255, 255, 255, 0.95);
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  box-shadow:
    0 4px 16px rgba(0, 0, 0, 0.04),
    0 0 0 1px rgba(255, 255, 255, 0.5) inset;
  transition: all 0.3s ease;
  padding-top: env(safe-area-inset-top);
  /* Stacking Context isolieren */
  isolation: isolate;
  
  /* backdrop-filter NUR auf Desktop - auf Mobile ist es der Touch-Bug-Verursacher! */
  @media (min-width: 900px) {
    -webkit-backdrop-filter: blur(20px) saturate(160%);
    backdrop-filter: blur(20px) saturate(160%);
    background: rgba(255, 255, 255, 0.85);
  }

  .header__inner {
    display: flex;
    align-items: center;
    gap: 1rem;
    min-height: 56px;
    padding: 0.5rem 1rem;

    @media (min-width: 900px) {
      gap: 2rem;
      min-height: 72px;
      padding: 0 1.5rem;
    }
  }
}

/* ===== LOGO ===== */

.brand-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin: 0;
  background: transparent;
  border: none;
  cursor: pointer;
  flex-shrink: 0;
  /* Safari Touch Fix - KRITISCH */
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  -webkit-appearance: none;
  appearance: none;
  -webkit-user-select: none;
  user-select: none;
  position: relative;
  z-index: 1;

  &:focus-visible {
    outline: 2px solid $color-brand-primary;
    outline-offset: 4px;
    border-radius: $radius-sm;
  }
}

.brand {
  height: 32px;
  width: auto;
  object-fit: contain;
  pointer-events: none; /* Klicks gehen zum Parent-Button */
  transition: transform 0.3s ease;
  flex-shrink: 0;

  .brand-btn:hover & {
    transform: scale(1.05);
  }

  @media (min-width: 900px) {
    height: 44px;
  }
}

/* ===== NAVIGATION ===== */

.nav {
  display: none;
  flex: 1;

  @media (min-width: 900px) {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  /* Mobile Menu Open */
  &.open {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: calc(env(safe-area-inset-top) + 56px);
    left: 0;
    right: 0;
    /* KEIN backdrop-filter auf Mobile! Verursacht Touch-Bugs in Safari */
    background: rgba(255, 255, 255, 0.98);
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
    padding: 0.75rem;
    gap: 0.5rem;
    box-shadow: $shadow-glass;
    animation: slideDown 0.3s ease;
    isolation: isolate;

    .nav__main,
    .nav__auth {
      flex-direction: column;
      width: 100%;
      gap: 0.375rem;
    }

    .nav__main {
      padding-bottom: 0.75rem;
      border-bottom: 1px solid $color-gray-200;
    }

    .nav__auth {
      padding-top: 0.5rem;
    }

    a, button {
      width: 100%;
      justify-content: flex-start;
      padding: 0.875rem 1rem;
    }

    .nav-link--icon {
      width: 100%;
      height: auto;
      padding: 0.875rem 1rem;
      border-radius: $radius-md;

      &::after {
        content: attr(title);
        margin-left: 0.5rem;
        font-weight: 500;
        font-family: inherit;
        font-size: 0.9375rem;
      }
    }

    /* Hide notification in mobile nav (shown in header__mobile-actions) */
    app-admin-notification-center {
      display: none;
    }
  }
}

.nav__main {
  display: flex;
  align-items: center;
  gap: 0.25rem;

  a {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.875rem;
    min-height: 44px; /* iOS minimum touch target */
    border-radius: $radius-md;
    font-size: 0.9375rem;
    font-weight: 500;
    color: $color-text-secondary;
    text-decoration: none;
    transition: all 0.2s ease;
    white-space: nowrap;
    /* Safari Touch Fix */
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    position: relative;
    z-index: 1; /* Über dem backdrop-filter Layer */

    &:hover {
      color: $color-brand-primary;
      background: rgba(37, 99, 235, 0.06);
    }

    &.active {
      color: $color-brand-primary;
      font-weight: 600;
      background: $gradient-subtle;
    }

    &:focus-visible {
      outline: none;
      box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
    }
  }
}

.nav__auth {
  display: flex;
  align-items: center;
  gap: 0.5rem;

  app-admin-notification-center {
    display: flex;
    align-items: center;
  }
}

/* ===== ICON BUTTONS (Logged In) ===== */

.nav-link--icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px; /* iOS minimum touch target */
  height: 44px; /* iOS minimum touch target */
  padding: 0;
  border-radius: $radius-full;
  color: $color-brand-primary; // Always colored
  text-decoration: none;
  border: 2px solid transparent;
  background: transparent;
  cursor: pointer;
  transition: all 0.2s ease;
  /* Safari Touch Fix */
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  position: relative;
  z-index: 1;

  &:hover {
    color: $color-brand-primary;
    background: rgba(37, 99, 235, 0.08);
    border-color: rgba(37, 99, 235, 0.15);
    transform: translateY(-1px);
  }

  &.active {
    color: $color-brand-primary;
    background: $gradient-subtle;
    border-color: rgba(37, 99, 235, 0.2);
  }

  &:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
  }
}

/* ===== SPECIAL NAV LINKS ===== */

.nav-link--primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.5rem 1.25rem;
  min-height: 44px; /* iOS minimum touch target */
  border-radius: $radius-md;
  font-size: 0.9375rem;
  font-weight: 600;
  text-decoration: none;
  background: $gradient-primary;
  color: white;
  box-shadow: $shadow-brand;
  transition: all 0.2s ease;
  /* Safari Touch Fix */
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  position: relative;
  z-index: 1;

  &:hover {
    box-shadow: $shadow-brand-hover;
    transform: translateY(-2px);
    background: $gradient-primary-hover;
  }

  &:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.3), $shadow-brand;
  }
}

.nav-link--admin {
  color: #ea580c !important; // Always orange

  &:hover {
    color: #c2410c !important;
    background: rgba(234, 88, 12, 0.1) !important;
    border-color: rgba(234, 88, 12, 0.25) !important;
  }

  &.active {
    color: #c2410c !important;
    background: rgba(234, 88, 12, 0.12) !important;
    border-color: rgba(234, 88, 12, 0.3) !important;
  }
}

/* Admin Link in Main Navigation */
.nav-link--admin-main {
  color: #ea580c !important;

  .material-symbols-outlined {
    color: #ea580c !important;
  }

  &:hover {
    color: #c2410c !important;
    background: rgba(234, 88, 12, 0.08) !important;

    .material-symbols-outlined {
      color: #c2410c !important;
    }
  }

  &.active {
    color: #c2410c !important;
    background: rgba(234, 88, 12, 0.12) !important;
    font-weight: 600;

    .material-symbols-outlined {
      color: #c2410c !important;
    }
  }
}

.nav-link--logout {
  color: #dc2626 !important; // Always red

  &:hover {
    color: #b91c1c !important;
    background: rgba(239, 68, 68, 0.1) !important;
    border-color: rgba(239, 68, 68, 0.25) !important;
  }
}

/* ===== MOBILE ACTIONS ===== */

.header__mobile-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-left: auto;

  @media (min-width: 900px) {
    display: none;
  }

  app-admin-notification-center {
    display: flex;
    align-items: center;
    /* Bell vertical centering tweak */
    position: relative;
    top: 4px;

    .notification-bell {
      vertical-align: middle;
    }
  }
}

.nav-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px; /* iOS minimum touch target */
  height: 44px; /* iOS minimum touch target */
  padding: 0;
  background: transparent;
  border: 2px solid $color-gray-200;
  border-radius: $radius-md;
  color: $color-text-primary;
  cursor: pointer;
  transition: all 0.2s ease;
  /* Safari Touch Fix - KRITISCH für Hamburger Menu */
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  position: relative;
  z-index: 10; /* Muss über allem sein */
  -webkit-appearance: none;
  appearance: none;

  @media (min-width: 900px) {
    display: none;
  }

  &:hover {
    border-color: $color-brand-primary;
    background: rgba(37, 99, 235, 0.06);
    color: $color-brand-primary;
  }

  &:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
  }

  .material-symbols-outlined {
    font-size: 22px;
  }
}

/* ===== SERVICES DROPDOWN ===== */

.nav-dropdown-container {
  position: relative;
  /* Wichtig: Container soll NICHT größer sein als sein Inhalt */
  display: inline-flex;
  flex-direction: column;
  align-items: flex-start;

  // Desktop: Hover funktioniert auch
  @media (min-width: 900px) {
    &:hover .nav-dropdown {
      opacity: 1;
      visibility: visible;
      transform: translateY(0);
    }
  }
}

.nav-dropdown-trigger {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.875rem;
  min-height: 44px; /* iOS minimum touch target */
  border-radius: $radius-md;
  font-size: 0.9375rem;
  font-weight: 500;
  color: $color-text-secondary;
  background: transparent;
  border: none;
  cursor: pointer;
  transition: all 0.2s ease;
  white-space: nowrap;
  /* Safari Touch Fix */
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  z-index: 1;

  &:hover {
    color: $color-brand-primary;
    background: rgba(37, 99, 235, 0.06);
  }

  &.active {
    color: $color-brand-primary;
    font-weight: 600;
    background: $gradient-subtle;
  }

  .dropdown-arrow {
    transition: transform 0.2s ease;
  }
}

.dropdown-open .nav-dropdown-trigger {
  color: $color-brand-primary;
  background: rgba(37, 99, 235, 0.06);

  .dropdown-arrow {
    transform: rotate(180deg);
  }
}

.nav-dropdown {
  // Desktop Styles
  @media (min-width: 900px) {
    position: absolute;
    top: calc(100% + 0.5rem);
    left: 0;
    min-width: 220px;
    background: white;
    border-radius: $radius-lg;
    box-shadow: 
      0 10px 40px rgba(0, 0, 0, 0.12),
      0 0 0 1px rgba(0, 0, 0, 0.05);
    padding: 0.5rem;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-8px);
    transition: all 0.2s ease;
    z-index: 100;

    // Show on click
    .dropdown-open & {
      opacity: 1;
      visibility: visible;
      transform: translateY(0);
    }
  }

  // Mobile Styles: Inline expanded
  @media (max-width: 899px) {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    padding: 0.5rem 0 0.5rem 1rem;
    margin-top: 0.25rem;
    border-left: 2px solid rgba($color-brand-primary, 0.2);
    margin-left: 1rem;
  }
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.625rem 0.875rem;
  min-height: 44px; /* iOS minimum touch target */
  border-radius: $radius-md;
  font-size: 0.875rem;
  font-weight: 500;
  color: $color-text-secondary;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.15s ease;
  background: transparent;
  border: none;
  width: 100%;
  text-align: left;
  /* Safari Touch Fix */
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  z-index: 1;

  .material-symbols-outlined {
    font-size: 18px;
    color: $color-text-tertiary;
    transition: color 0.15s;
  }

  &:hover {
    background: rgba($color-brand-primary, 0.06);
    color: $color-brand-primary;

    .material-symbols-outlined {
      color: $color-brand-primary;
    }
  }

  &--all {
    font-weight: 600;
    color: $color-brand-primary;

    .material-symbols-outlined {
      color: $color-brand-primary;
    }
  }
}

.dropdown-divider {
  height: 1px;
  background: $color-gray-200;
  margin: 0.375rem 0.5rem;
}

// Mobile Menu anpassen
.nav.open {
  .nav-dropdown-container {
    width: 100%;
    /* WICHTIG: Nicht die anderen Elemente überdecken! */
    position: relative;
    z-index: auto;
  }

  .nav-dropdown-trigger {
    width: 100%;
    justify-content: flex-start;
    padding: 0.875rem 1rem;
  }

  .nav-dropdown {
    animation: slideDown 0.2s ease;
    /* Dropdown innerhalb des Flows, nicht overlay */
    position: relative;
    z-index: auto;
  }
}

/* ===== BACKDROP ===== */

.backdrop {
  position: fixed;
  inset: 0;
  /* KEIN backdrop-filter! Verursacht Touch-Bugs in Safari */
  background: rgba(15, 23, 42, 0.6);
  z-index: -1;
  animation: fadeIn 0.3s ease;
  height: 2000px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ===== ANIMATIONS ===== */

@keyframes slideDown {
  from {
    transform: translateY(-10px);
    opacity: 0;
  }

  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

/* ===== MOTION PREFERENCES ===== */

@media (prefers-reduced-motion: reduce) {

  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}