@use '../mixins/_media-queries';

:root {
  //--mat-fab-small-container-elevation-shadow: var(--whiteframe-shadow-4dp);
  --mat-dialog-container-small-max-width: 94vw;
  --mat-dialog-container-max-width: 85vw;
  --mat-menu-item-label-text-size: 14px;

  // Tab contrast improvements - use darker shade for light theme
  --mat-tab-header-active-label-text-color: var(--palette-primary-700);
  --mat-tab-header-active-ripple-color: var(--palette-primary-700);
  --mat-tab-header-active-focus-label-text-color: var(--palette-primary-700);
  --mat-tab-header-active-hover-label-text-color: var(--palette-primary-700);
  --mdc-tab-indicator-active-indicator-color: var(--palette-primary-700);
  --mat-tab-header-inactive-label-text-color: var(--text-color-muted);
  --calendar-hover-bg: color-mix(in srgb, var(--c-primary) 35%, transparent);
}

// Dark theme tab overrides for better contrast - use lighter shade
body.isDarkTheme {
  --mat-tab-header-active-label-text-color: var(--palette-primary-300);
  --mat-tab-header-active-ripple-color: var(--palette-primary-300);
  --mat-tab-header-active-focus-label-text-color: var(--palette-primary-300);
  --mat-tab-header-active-hover-label-text-color: var(--palette-primary-300);
  --mdc-tab-indicator-active-indicator-color: var(--palette-primary-300);
  --calendar-hover-bg: color-mix(in srgb, var(--c-primary) 20%, transparent);
}

// Apply tab colors explicitly to tab components
.mat-mdc-tab-group,
.mat-mdc-tab-nav-bar {
  // Active tab label color
  .mdc-tab--active .mdc-tab__text-label {
    color: var(--mat-tab-header-active-label-text-color);
  }

  // Active tab indicator (underline)
  .mdc-tab-indicator--active .mdc-tab-indicator__content--underline {
    border-color: var(--mdc-tab-indicator-active-indicator-color);
  }

  // Inactive tab labels
  .mdc-tab:not(.mdc-tab--active) .mdc-tab__text-label {
    color: var(--mat-tab-header-inactive-label-text-color);
  }
}

* {
  --mat-chip-container-shape-radius: 4px !important;
  --mat-chip-elevated-container-color: transparent !important;
  //--mat-chip-outline-color: var(--mat-chip-outline-color, var(--mat-sys-outline)) !important;
  --mat-chip-outline-color: var(--chip-outline-color) !important;
  --mat-chip-outline-width: 1px !important;
}

.mat-drawer.mat-drawer-side {
  // should be below notes panel
  z-index: 1 !important;
}

// on android this is not properly injected for unknown reasons
.cdk-overlay-container {
  z-index: 1000;
}

// Native mobile: dialogs need explicit safe-area offsets because they use
// position:fixed inside a container that spans the full viewport.
// Connected overlays (menus, selects) are handled via the CDK viewport margin
// patch in GlobalThemeService._patchCdkViewportForSafeArea().
body.isNativeMobile .cdk-global-overlay-wrapper {
  padding: var(--safe-area-top) var(--safe-area-right) var(--safe-area-bottom)
    var(--safe-area-left);
}

body.isIOS.isKeyboardVisible .cdk-global-overlay-wrapper {
  // Dialogs render outside the app shell; when iOS does not shrink the WebView,
  // reserve keyboard space in the overlay layer itself.
  padding-bottom: calc(var(--safe-area-bottom) + var(--keyboard-overlay-offset));
}

// Constrain menu panels to safe area so content scrolls instead of
// extending behind the status bar when CDK pushes the overlay up.
body.isNativeMobile .mat-mdc-menu-panel {
  max-height: calc(
    100vh - var(--safe-area-top) - var(--safe-area-bottom) - 16px
  ) !important;
}

// DIALOGS
// ----------
.mat-mdc-dialog-title.mat-mdc-dialog-title,
.mdc-dialog__title.mdc-dialog__title {
  margin-bottom: 0;
  margin-top: 8px;
  display: flex;
  align-items: center;
}

.cdk-overlay-pane.mat-mdc-dialog-panel {
  @include media-queries.mq(xs, max) {
    max-width: 96vw !important;
    max-height: calc(
      100vh - env(safe-area-inset-top) - env(safe-area-inset-bottom)
    ) !important;
  }
}

body.isIOS.isKeyboardVisible .cdk-overlay-pane.mat-mdc-dialog-panel {
  max-height: calc(
    var(--visual-viewport-height) - var(--safe-area-top) - var(--safe-area-bottom)
  ) !important;
}

body .mat-mdc-dialog-content {
  overflow: auto;
  // Let content fill available space within the constrained panel
  // Panel already has max-height, so content doesn't need its own
  flex: 1 1 auto;
  min-height: 0; // Important for flex scrolling to work

  mat-form-field {
    display: block;
  }
}

body .mat-mdc-dialog-surface {
  overflow: visible;
  // Enable flex layout for proper content scrolling
  display: flex;
  flex-direction: column;
}

.mat-mdc-dialog-panel {
  @include media-queries.mq(xs) {
    //--mat-dialog-container-min-width: 350px;
  }
}

.mat-mdc-dialog-title {
  vertical-align: middle;

  > * {
    vertical-align: middle;
  }

  > mat-icon:first-child {
    margin-right: 8px;
  }
}

.big {
  height: auto;
  width: 100% !important;
  max-width: none !important;
  max-height: calc(
    95vh - env(safe-area-inset-top) - env(safe-area-inset-bottom)
  ) !important;

  // Desktop gets some breathing room
  @include media-queries.mq(xs) {
    margin: var(--s2);
  }
}

.project-complete-fullscreen-dialog {
  width: 100vw !important;
  height: 100vh !important;
  max-width: 100vw !important;
  max-height: 100vh !important;
  margin: 0 !important;

  @supports (height: 100dvh) {
    height: 100dvh !important;
    max-height: 100dvh !important;
  }

  .mat-mdc-dialog-surface {
    width: 100%;
    height: 100%;
    max-height: none;
    overflow: hidden;
    background: transparent;
    border-radius: 0;
    box-shadow: none;
  }
}

body.isNativeMobile .project-complete-fullscreen-dialog {
  width: calc(100vw - var(--safe-area-left) - var(--safe-area-right)) !important;
  height: calc(100dvh - var(--safe-area-top) - var(--safe-area-bottom)) !important;
}

// FORMS
// --------
.mat-mdc-form-field {
  margin-bottom: var(--s2);

  // Feed semantic input tokens to official Material CSS variables
  --mat-form-field-filled-container-color: var(--input-bg);
  --mat-form-field-filled-container-shape: var(--input-border-radius);

  // Hide the active indicators (underline ripple) by setting their color to transparent,
  // but leave the error indicators intact so the red invalid line shows up.
  --mat-form-field-filled-active-indicator-color: transparent;
  --mat-form-field-filled-hover-active-indicator-color: transparent;
  --mat-form-field-filled-focus-active-indicator-color: transparent;

  // Targets default filled text boxes to transform them into flat, token-driven containers
  .mdc-text-field--filled:not(.mdc-text-field--disabled) {
    border: 1px solid var(--input-border-color) !important;
    // Material hardcodes the filled field's bottom corner radii to 0 (the
    // container-shape token above only rounds the top two corners), so a full
    // border would render rounded-top/square-bottom. Round all four corners to
    // read as a card, matching the task-detail panel this style is copied from.
    border-radius: var(--input-border-radius) !important;
    transition:
      background var(--transition-duration-s) ease-out,
      border-color var(--transition-duration-s) ease-out,
      box-shadow var(--transition-duration-s) ease-out !important;

    &:hover {
      background: var(--input-bg-hover) !important;
    }
  }

  // Active / Focus state
  &.mat-focused .mdc-text-field--filled:not(.mdc-text-field--disabled) {
    background: var(--input-bg-hover) !important;
    border-color: var(--input-border-color-focus) !important;
    box-shadow: 0 0 0 1px var(--input-border-color-focus) !important;
  }

  // Specialized focus treatment for toggle fields:
  // Remove the border change and enhance the focus ring (outline) instead
  &:has(mat-slide-toggle) {
    &.mat-focused .mdc-text-field--filled:not(.mdc-text-field--disabled) {
      border-color: var(--input-border-color) !important;
      box-shadow: none !important;
    }
    &:has(:focus-visible) .mdc-text-field--filled:not(.mdc-text-field--disabled) {
      outline: var(--focus-ring-width) solid var(--focus-ring-color);
      outline-offset: var(--focus-ring-offset);
    }
  }

  // Error / Invalid state
  &.mat-form-field-invalid .mdc-text-field--filled:not(.mdc-text-field--disabled) {
    border-color: var(--input-border-color-invalid) !important;
  }
}

// needs to be global cause of cdk overlay
// is used for select-task
mat-option.isDone {
  .mdc-list-item__primary-text {
    text-decoration: line-through !important;
    opacity: 0.4 !important;
  }
}

// BUTTONS
// -----------
// Ensure proper contrast for text and stroked buttons on standard backgrounds
// Text buttons use theme colors directly on background, so we need good contrast
.mat-mdc-button,
.mat-mdc-outlined-button {
  &[color='primary'] {
    // Use primary color - validated palettes ensure WCAG AA compliance
    --mdc-text-button-label-text-color: var(--palette-primary-500);
    --mdc-outlined-button-label-text-color: var(--palette-primary-500);
    --mdc-outlined-button-outline-color: var(--palette-primary-500);
  }

  &[color='accent'] {
    // Use accent color - validated palettes ensure WCAG AA compliance
    --mdc-text-button-label-text-color: var(--palette-accent-500);
    --mdc-outlined-button-label-text-color: var(--palette-accent-500);
    --mdc-outlined-button-outline-color: var(--palette-accent-500);
  }

  &[color='warn'] {
    // Use warn color - validated palettes ensure WCAG AA compliance
    --mdc-text-button-label-text-color: var(--palette-warn-500);
    --mdc-outlined-button-label-text-color: var(--palette-warn-500);
    --mdc-outlined-button-outline-color: var(--palette-warn-500);
  }
}

.mat-mdc-menu-item-text {
  overflow: hidden !important;
  text-overflow: ellipsis !important;
  max-width: 100%;
  white-space: nowrap;

  display: flex;
  justify-content: space-between;
  align-items: center;

  plugin-icon {
    margin-right: 14px;
  }

  span {
    margin-right: auto;
  }
}

.mat-mdc-icon-button.mat-mdc-button-base {
  --mat-icon-button-state-layer-size: 40px;
}

.mat-mdc-mini-fab.mat-mdc-mini-fab.mat-mdc-mini-fab {
  --mat-fab-small-state-layer-size: 48px;
}

.mat-mdc-icon-button.mat-mdc-icon-button.mat-mdc-icon-button {
  width: 40px;
  height: 40px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;

  .mat-mdc-button-touch-target {
    width: 40px;
    height: 40px;
  }
}

// ICONS
// -----

mat-icon,
.mat-icon {
  vertical-align: middle;
  visibility: hidden;
}

body.isMaterialSymbolsLoaded .mat-icon,
body.isMaterialSymbolsLoaded mat-icon {
  visibility: visible;
}

.mat-icon.mat-icon {
  // to make utf emojis look better
  overflow: visible;
}

.mat-icon {
  // for some reason material puts that to transform and it is bad for performance
  will-change: auto !important;
}

mat-icon.mat-icon[svgicon] {
  display: flex;
  justify-content: center;
}

// Ensure icon flex behaviour stays consistent even before Angular Material
// injects the MatButton styles (which set min-height: fit-content lazily).
body .mat-mdc-button-base .mat-icon {
  //flex-shrink: 1 !important;
  min-height: auto !important;
}

// AUTOCOMPLETE
// -----------------
.mdc-list-item__primary-text {
  white-space: nowrap !important;
  overflow: hidden !important;
  text-overflow: ellipsis !important;
}

// CDK DRAG
/* Animate items as they're being sorted. */
.cdk-drop-list-dragging .cdk-drag {
  transition: transform 100ms cubic-bezier(0, 0, 0.2, 1);
}

/* Animate an item that has been dropped. */
.cdk-drag-animating {
  transition: transform 100ms cubic-bezier(0, 0, 0.2, 1);
}

.cdk-drag-preview {
  box-shadow: var(--whiteframe-shadow-8dp) !important;
  cursor: grabbing !important;
  overflow: hidden;
  pointer-events: none !important;

  &:hover,
  &:active {
    cursor: grabbing !important;
  }

  background: var(--bg-lightest) !important;
}

.cdk-drag-preview.tree-dnd-drag-preview {
  box-sizing: border-box;
  max-width: calc(100vw - var(--s2));
  border-radius: var(--card-border-radius);
  color: var(--sidenav-text);

  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  .nav-child-item,
  nav-item,
  .nav-link {
    min-width: 0;
  }

  .nav-link,
  .nav-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

.drag-preview-time-badge {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 16px;
  font-weight: bold;
  border: 2px solid var(--c-primary);
  border-radius: var(--card-border-radius);
  background: var(--bg-lighter);
  background: inherit;
  z-index: 100;
  pointer-events: none;
  padding: 2px 6px;
  box-shadow: var(--whiteframe-shadow-3dp);
  white-space: nowrap;
}

.cdk-drag-placeholder {
  cursor: grabbing;
  opacity: 0.1;
}

.cdk-drop-list-dragging {
  outline: 1px dashed var(--c-primary);
  border-radius: 6px;
  cursor: grabbing;
}

.cdk-drop-list-disabled {
  border: 4px solid red;
}

.cdk-drop-list-receiving {
  outline: 1px dashed var(--c-primary);
  border-color: var(--c-primary);
  border-radius: 6px;
  cursor: grabbing;
  //border: 2px solid green;
}

// MAT MENU
.mat-mdc-menu-panel {
  overflow-x: hidden !important;
}

.mat-mdc-menu-content {
  padding: 0 !important;
}

.mat-mdc-menu-item.quick-access {
  border: 1px solid red;
}

task.cdk-drag-preview .box {
  background: var(--bg-lightest) !important;
}

// MAT TOOLTIP
mat-tooltip-component {
  pointer-events: none;
}

// The tooltip's overlay PANE wrapper (not just the inner component) must
// not swallow pointer events. A tooltip shown 'below' a button in the
// vertical action strip lands directly over the next button — without
// this it intercepts clicks for both real users and Playwright. Scoped
// to the experimental flag so other tooltips in the app keep their
// pane-level pointer events (interactive hover content, etc.).
body.isVerticalActionBar .cdk-overlay-pane.mat-mdc-tooltip-panel {
  pointer-events: none;
}

// SNACK
// Move snack bar above mobile bottom nav for all touch devices
@include media-queries.mq(xs, max) {
  // Apply to all touch devices, not just when mobile bottom nav is shown
  body.hasMobileBottomNav .mat-mdc-snack-bar-handset {
    pointer-events: none;
  }
  body.hasMobileBottomNav mat-snack-bar-container {
    margin-bottom: calc(
      var(--bottom-nav-height) + var(--bottom-nav-safe-area) + 24px
    ) !important;
    pointer-events: auto;
    transition: 150ms margin;
  }
  body.hasMobileBottomNav.isAddTaskBarOpen mat-snack-bar-container {
    margin-bottom: calc(
      var(--bottom-nav-height) + var(--bottom-nav-safe-area) + 48px
    ) !important;
    pointer-events: auto;
  }
}

.mat-expansion-panel-header {
  --mat-expansion-header-expanded-state-height: 48px;
}

.mat-mdc-menu-panel:has(select-option-row) .mat-mdc-menu-item,
.mat-mdc-autocomplete-panel:has(select-option-row) .mat-mdc-option {
  height: auto !important;
  min-height: 48px;
  padding-top: 8px !important;
  padding-bottom: 8px !important;
  display: flex !important;
  align-items: center !important;
}

.mention-menu:has(select-option-row) .mention-item {
  height: auto !important;
  padding-top: 6px !important;
  padding-bottom: 6px !important;
  display: flex !important;
  align-items: center !important;
}

// Shared calendar hover and focus overrides
.mat-calendar:not(.no-hover-effects) {
  .mat-calendar-body-cell:not(.mat-calendar-body-disabled):focus
    > .mat-calendar-body-cell-content {
    outline: 2px solid color-mix(in srgb, var(--c-accent) 80%, transparent) !important;
  }

  .mat-calendar-body-cell:not(.mat-calendar-body-disabled):hover
    > .mat-calendar-body-cell-content {
    outline: 2px dashed color-mix(in srgb, var(--c-accent) 80%, transparent) !important;
  }

  .mat-calendar-body-cell:not(.mat-calendar-body-disabled):hover
    > .mat-calendar-body-cell-content:not(.mat-calendar-body-selected),
  .mat-calendar-body-cell:not(.mat-calendar-body-disabled):focus
    > .mat-calendar-body-cell-content:not(.mat-calendar-body-selected) {
    background-color: var(--calendar-hover-bg) !important;
  }
}

// SLIDE TOGGLE
// ------------
.mat-mdc-slide-toggle {
  // Inherit all unchecked state ripple configurations (both colors and opacities)
  // for the checked state to ensure they match exactly in color and strength.
  --mat-slide-toggle-selected-hover-state-layer-color: var(
    --mat-slide-toggle-unselected-hover-state-layer-color
  ) !important;
  --mat-slide-toggle-selected-focus-state-layer-color: var(
    --mat-slide-toggle-unselected-focus-state-layer-color
  ) !important;
  --mat-slide-toggle-selected-pressed-state-layer-color: var(
    --mat-slide-toggle-unselected-pressed-state-layer-color
  ) !important;

  --mat-slide-toggle-selected-hover-state-layer-opacity: var(
    --mat-slide-toggle-unselected-hover-state-layer-opacity
  ) !important;
  --mat-slide-toggle-selected-focus-state-layer-opacity: var(
    --mat-slide-toggle-unselected-focus-state-layer-opacity
  ) !important;
  --mat-slide-toggle-selected-pressed-state-layer-opacity: var(
    --mat-slide-toggle-unselected-pressed-state-layer-opacity
  ) !important;
}
