// Copyright (C) 2025 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

@import "common";
@import "./graph/graph.scss";
@import "./graph/text_label.scss";
@import "./help.scss";
@import "./graph/node_box.scss";
@import "./node_styling_widgets.scss";
@import "./node_panel.scss";
@import "./operations/operation_component.scss";
@import "./nodes/sources/slices_source.scss";
@import "./nodes/sources/table_source.scss";
@import "./nodes/sources/timerange_source.scss";
@import "./nodes/add_columns_node.scss";
@import "./nodes/aggregation_node.scss";
@import "./nodes/modify_columns_node.scss";
@import "./table_list.scss";
@import "./function_list.scss";
@import "./widgets.scss";
@import "./join_widgets.scss";

.pf-query-builder-layout {
  // CSS variable for side panel width, used in both SCSS and TypeScript
  --pf-qb-side-panel-width: 60px;

  height: 100%;

  // DrawerPanel's main section needs to have the grid layout
  .pf-drawer-panel__main {
    display: grid;
    grid-template-columns: 1fr auto auto auto;
    overflow: hidden;
  }

  .pf-qb-node-graph {
    grid-column: 1;
    overflow: auto;
    position: relative;
  }

  // Position the resize handle in the grid, but let the widget handle its own styling
  .pf-resize-handle {
    grid-column: 2;
  }

  .pf-qb-explorer {
    grid-column: 3;
    overflow: hidden;
    height: 100%;
    // Width is controlled by inline style for smooth resizing
  }

  .pf-qb-side-panel {
    grid-column: 4;
    width: var(--pf-qb-side-panel-width);
    height: 100%;
    display: flex;
    flex-direction: column;
    gap: 0;
    border-left: 1px solid var(--pf-color-border);
    padding: 0;
    align-items: stretch;
    justify-content: flex-start;

    .pf-button {
      width: 100%;
      height: 60px;
      font-size: var(--pf-font-size-xxl);
      box-shadow: none;
      border: none;
      border-radius: 0;
      border-bottom: 1px solid var(--pf-color-border);

      &.pf-active {
        background-color: var(--pf-color-primary);
        color: var(--pf-color-text-on-primary);
      }
    }
  }

  .pf-qb-floating-controls {
    position: absolute;
    top: 1rem;
    right: 1rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;

    .pf-button {
      width: 60px;
      height: 60px;
      font-size: 28px;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);

      &:hover {
        box-shadow: 0 6px 16px rgba(0, 0, 0, 0.4);
        transform: scale(1.05);
      }
    }
  }

  .pf-qb-floating-controls-bottom {
    position: absolute;
    bottom: 1rem;
    right: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }

  .pf-qb-floating-warning {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background-color: var(--pf-color-danger);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    transition: all 0.2s ease;
    cursor: pointer;

    &:hover {
      box-shadow: 0 6px 16px rgba(0, 0, 0, 0.4);
      transform: scale(1.05);
    }

    .pf-qb-warning-icon {
      color: white;
      font-size: 28px;
    }
  }

  &.explorer-collapsed {
    .pf-resize-handle {
      display: none;
    }

    .pf-qb-explorer {
      border-left: none;
      overflow: hidden;
    }
  }
}

// Navigation side panel (Data Explorer landing)
.pf-unselected-explorer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  height: 100%;
  padding: 32px 24px;
  overflow-y: auto;

  // Center content when small, scroll when large
  &::before {
    content: "";
    flex-grow: 1;
  }

  &::after {
    content: "";
    flex-grow: 1;
  }
}

// Section wrapper: groups header + content with tight internal gap
.pf-nav-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  max-width: 460px;
}

// Section header with line divider
.pf-nav-section-header {
  display: flex;
  align-items: center;
  gap: 12px;

  span {
    font-size: var(--pf-font-size-xs);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--pf-color-text-muted);
    white-space: nowrap;
  }

  &::after {
    content: "";
    flex: 1;
    height: 1px;
    background: var(--pf-color-border-secondary);
  }
}

// Primary action cards (New graph / Smart graph)
.pf-nav-actions {
  display: flex;
  gap: 12px;
  width: 100%;
  max-width: 460px;
}

.pf-nav-action-card {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 18px;

  .pf-nav-action-card__icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border-radius: 10px;
    background: color-mix(in srgb, var(--pf-color-primary), transparent 88%);
    flex-shrink: 0;

    .pf-icon {
      font-size: var(--pf-font-size-xxl);
      color: var(--pf-color-primary);
    }
  }

  .pf-nav-action-card__text {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
  }

  .pf-nav-action-card__title {
    font-size: var(--pf-font-size-l);
    font-weight: 600;
  }

  .pf-nav-action-card__desc {
    font-size: var(--pf-font-size-s);
    color: var(--pf-color-text-muted);
    line-height: 1.3;
  }

  &:hover,
  &:focus-within {
    .pf-nav-action-card__icon {
      background: color-mix(in srgb, var(--pf-color-primary), transparent 80%);
    }
  }
}

// Compact list items for tutorials/solutions
.pf-nav-list {
  width: 100%;
}

.pf-nav-list-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 16px;

  .pf-nav-list-item__icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    background: color-mix(in srgb, var(--pf-color-primary), transparent 90%);
    flex-shrink: 0;

    .pf-icon {
      font-size: var(--pf-font-size-xl);
      color: var(--pf-color-primary);
    }
  }

  .pf-nav-list-item__text {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 1px;
  }

  .pf-nav-list-item__title {
    font-size: var(--pf-font-size-m);
    font-weight: 500;
  }

  .pf-nav-list-item__desc {
    font-size: var(--pf-font-size-s);
    color: var(--pf-color-text-muted);
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  &:hover,
  &:focus-within {
    .pf-nav-list-item__icon {
      background: color-mix(in srgb, var(--pf-color-primary), transparent 82%);
    }
  }
}

// Recent graphs
.pf-recent-graphs-section {
  width: 100%;
}

.pf-recent-graphs-scroll-container {
  max-height: 280px;
  overflow-y: auto;
}

.pf-recent-graphs-empty {
  color: var(--pf-color-text-muted);
  font-size: var(--pf-font-size-m);
  font-style: italic;
  text-align: center;
  padding: 12px 0;
}

.pf-recent-graph-card {
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;

  > div:not(.pf-recent-graph-card__actions) {
    flex: 1;
    min-width: 0;
  }

  h3 {
    margin: 0;
    font-size: var(--pf-font-size-m);
    font-weight: 500;
  }

  p {
    margin: 0;
    font-size: var(--pf-font-size-s);
    color: var(--pf-color-text-muted);
  }

  .pf-recent-graph-card__actions {
    display: flex;
    visibility: hidden;
    flex-shrink: 0;
  }

  &:hover .pf-recent-graph-card__actions,
  &:focus-within .pf-recent-graph-card__actions {
    visibility: visible;
  }
}

.pf-source-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px;

  .pf-source-card-clickable {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    margin-bottom: 2px;
    transition: transform 0.1s ease;

    .pf-icon {
      font-size: var(--pf-font-size-xxl);
      color: var(--pf-color-primary);
      transition: color 0.1s ease;
    }

    h3 {
      margin: 0;
      font-size: var(--pf-font-size-m);
      font-weight: 500;
      transition: color 0.1s ease;
      text-align: center;
    }
  }

  &:hover .pf-source-card-clickable,
  &:focus-within .pf-source-card-clickable {
    transform: translateY(-2px);

    .pf-icon {
      color: var(--pf-color-accent);
    }

    h3 {
      color: var(--pf-color-accent);
    }
  }

  p {
    margin: 0;
    font-size: var(--pf-font-size-s);
    line-height: 1.3;
    color: var(--pf-color-text-muted);
    text-align: center;
  }

  .pf-source-card-hotkey {
    align-self: center;
    margin-top: 2px;
  }
}

.pf-examples-modal {
  padding: 16px;
}

.pf-examples-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
  max-width: 800px;
}

.pf-example-card {
  display: flex;
  flex-direction: column;
  gap: 8px;

  .pf-example-card-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
    transition: transform 0.1s ease;

    .pf-icon {
      font-size: 32px;
      color: var(--pf-color-primary);
      transition: color 0.1s ease;
    }

    h3 {
      margin: 0;
      font-size: var(--pf-font-size-l);
      font-weight: 500;
      transition: color 0.1s ease;
    }
  }

  &:hover .pf-example-card-content,
  &:focus-within .pf-example-card-content {
    transform: translateY(-2px);

    .pf-icon {
      color: var(--pf-color-accent);
    }

    h3 {
      color: var(--pf-color-accent);
    }
  }

  p {
    margin: 0;
    font-size: var(--pf-font-size-s);
    line-height: 1.4;
    color: var(--pf-color-text-muted);
    text-align: center;
  }
}

.pf-error-details {
  min-width: 300px;
  max-width: 500px;
  max-height: 400px;
  overflow-y: auto;
  padding: 12px;
  font-size: var(--pf-font-size-s);
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--pf-font-family-monospace, "Roboto Mono", monospace);
}
