import { css } from '@genesislcap/web-core';
import { settingsModalStyles } from '../components/settings-modal/settings-modal.styles';
import {
  AI_COLOUR_AMBER,
  AI_COLOUR_CYAN,
  AI_COLOUR_PINK,
  AI_COLOUR_VIOLET,
} from '../styles/ai-colours';

const baseStyles = css`
  :host {
    /* Shared max width for the conversation column and the composer, so they stay
       aligned. Override on the host to change both at once. NOTE: the @container
       threshold that floats the composer can't read this (container queries can't
       use custom properties), so if you override this below the 1500px default the
       float still triggers at 1500px. */
    --ai-conversation-max-width: 1500px;

    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    font-family: var(--body-font);
    color: var(--neutral-foreground-rest);
  }

  .chat-wrapper {
    container-type: inline-size;
    position: relative;
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
    background-color: var(--neutral-layer-1);
    border: 1px solid var(--neutral-stroke-rest);
    border-radius: var(--control-corner-radius) * 1px;
  }

  .chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: calc(var(--design-unit) * 2px) calc(var(--design-unit) * 3px);
    border-bottom: 1px solid var(--neutral-stroke-rest);
    background-color: var(--neutral-layer-2);
  }

  .chat-header.draggable {
    cursor: move;
    user-select: none;
  }

  .chat-title {
    font-weight: bold;
    font-size: 1.2em;
  }

  .header-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    margin-left: auto;
  }

  .settings-button,
  .popout-button {
    font-size: 1.2em;
  }

  ::slotted([slot='header-actions-extra']) {
    font-size: 1.2em;
  }

  .splash-wrapper {
    position: absolute;
    inset: 0;
    z-index: 10;
    overflow: hidden;
    pointer-events: none;
  }

  ::slotted([slot='splash']) {
    display: block;
    width: 100%;
    height: 100%;
  }

  .messages {
    flex: 1;

    /* Allow the scroll area to shrink below its content height so a grown
       composer (drag-to-resize) takes space from here instead of overflowing
       the column and clipping the input row's buttons. */
    min-height: 0;
    overflow-y: auto;
    padding: 10px;
  }

  /* Caps and centres the conversation column inside the (full-width) scroll
     container, so the scrollbar stays at the panel edge while the messages sit
     in a readable-width column. Exposed as part="messages-content" so a host
     can widen it (::part(messages-content) { max-width: none }) or neutralise
     the wrapper entirely (::part(messages-content) { display: contents }). */
  .messages-content {
    display: flex;
    flex-direction: column;
    gap: calc(var(--design-unit) * 2.5px);
    width: 100%;
    max-width: var(--ai-conversation-max-width);
    margin-inline: auto;
  }

  @keyframes slide-in-right {
    from {
      opacity: 0%;
      transform: translateX(16px);
    }

    to {
      opacity: 100%;
      transform: translateX(0);
    }
  }

  @keyframes slide-in-left {
    from {
      opacity: 0%;
      transform: translateX(-16px);
    }

    to {
      opacity: 100%;
      transform: translateX(0);
    }
  }

  @keyframes thinking-ring {
    0% {
      transform: scale(1);
      opacity: 60%;
    }

    100% {
      transform: scale(1.8);
      opacity: 0%;
    }
  }

  .agent-switch-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 8px 0;
    color: var(--neutral-foreground-hint, var(--neutral-foreground-rest));
    opacity: 60%;
    font-size: 0.75em;
  }

  .agent-switch-indicator::before,
  .agent-switch-indicator::after {
    content: '';
    flex: 1;
    height: 1px;
    background: currentColor;
  }

  .agent-switch-label {
    white-space: nowrap;
  }

  .message-row {
    display: flex;
    align-items: flex-start;
    gap: 8px;
  }

  .message-row.user {
    flex-direction: row-reverse;
    animation: slide-in-right 0.25s ease-out;
  }

  .message-row.ai,
  .message-row.ai-thinking,
  .message-row.ai-function {
    animation: slide-in-left 0.25s ease-out;
  }

  .message-row.interaction {
    flex-direction: column;
    animation: slide-in-left 0.25s ease-out;
  }

  /* A 'bubble' interaction reads like a normal assistant message: avatar to the
     left, bubble to the right — not the full-width column the other interaction
     presentations use. */
  .message-row.interaction.present-bubble {
    flex-direction: row;
  }

  .avatar {
    position: relative;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background-color: var(--neutral-layer-3);
    border: 1px solid var(--neutral-stroke-rest);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 1em;
    overflow: hidden;
  }

  .avatar.ai-thinking {
    border-color: var(--ai-thinking-color, #7c3aed);
    overflow: visible;
  }

  .avatar.ai-thinking::before,
  .avatar.ai-thinking::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: 50%;
    border: 2px solid var(--ai-thinking-color, #7c3aed);
    animation: thinking-ring 1.5s ease-out infinite;
  }

  .avatar.ai-thinking::after {
    animation-delay: 0.75s;
  }

  .avatar-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
  }

  .avatar-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
  }

  .avatar-icon svg {
    /* !important so a consumer-supplied SVG's own inline width/height/style
       cannot break out of the avatar bounds. */
    width: 84% !important;
    height: 84% !important;
  }

  .message {
    padding: calc(var(--design-unit) * 2.5px) calc(var(--design-unit) * 3.5px);
    max-width: 80%;
    min-width: 70px;
    font-family: var(--body-font);
    font-size: 1.1em;

    /* Long unbroken strings (URLs, file paths, tokens, base64) wrap instead of
       overflowing the bubble. overflow-wrap: anywhere also lets the bubble shrink
       below such a string's width in the flex row, preventing horizontal overflow. */
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  .message.user {
    background: linear-gradient(
      135deg,
      var(--accent-fill-rest) 0%,
      color-mix(in srgb, var(--accent-fill-rest) 70%, black) 100%
    );
    color: var(--accent-foreground-on-accent-rest);

    /* Squared "tail" at top-right — the corner nearest the user avatar. */
    border-radius: 18px 4px 18px 18px;
    box-shadow: 0 2px 8px color-mix(in srgb, var(--accent-fill-rest) 35%, transparent);
  }

  .message.ai {
    background: linear-gradient(135deg, var(--neutral-layer-3) 0%, var(--neutral-layer-4) 100%);
    color: var(--neutral-foreground-rest);

    /* Squared "tail" at top-left — the corner nearest the assistant avatar. */
    border-radius: 4px 18px 18px;
    border: calc(var(--stroke-width) * 1px) solid var(--neutral-stroke-rest);
    box-shadow: var(--ai-message-shadow, 0 2px 8px rgb(0 0 0 / 15%));
  }

  .message.ai-thinking {
    background: linear-gradient(
      135deg,
      color-mix(in srgb, var(--ai-thinking-color, #7c3aed) 12%, transparent) 0%,
      color-mix(in srgb, var(--ai-thinking-color, #7c3aed) 18%, transparent) 100%
    );
    color: var(--neutral-foreground-rest);
    border-radius: 4px 18px 18px;
    border-left: 2px solid var(--ai-thinking-color, #7c3aed);
    font-style: italic;
    font-size: 0.95em;
  }

  /* Narration shares the Thinking bubble but is the assistant's commentary (labelled "Working"),
     not its inner monologue — keep it upright so it reads as action, distinct from italic reasoning. */
  .message.ai-thinking.narration {
    font-style: normal;
  }

  .message.ai-function {
    background: linear-gradient(135deg, rgb(15 32 39 / 90%) 0%, rgb(30 58 58 / 80%) 100%);
    color: var(--ai-function-color, #86efac);
    border-radius: 4px 18px 18px;
    font-size: 1em;
    border: 1px solid color-mix(in srgb, var(--ai-function-color, #86efac) 20%, transparent);
  }

  /* 'label' (default) and 'bare' interactions render the widget chrome-free and
     full-width — the widget owns its body. */
  .message-row.interaction.present-label .message,
  .message-row.interaction.present-bare .message {
    background: none;
    border: none;
    padding: 0;
    max-width: 100%;
    width: 100%;
  }

  /* 'bubble' interactions reuse the standard assistant-message skin so the
     widget sits inside a normal chat bubble. Padding, max-width and min-width
     come from the base .message rule. */
  .message-row.interaction.present-bubble .message {
    background: linear-gradient(135deg, var(--neutral-layer-3) 0%, var(--neutral-layer-4) 100%);
    color: var(--neutral-foreground-rest);
    border-radius: 4px 18px 18px;
    border: calc(var(--stroke-width) * 1px) solid var(--neutral-stroke-rest);
    box-shadow: var(--ai-message-shadow, 0 2px 8px rgb(0 0 0 / 15%));
  }

  .sender {
    font-weight: bold;
    font-size: 0.9em;
    margin-bottom: 4px;
  }

  .payload {
    font-family: monospace;
    white-space: pre-wrap;
    margin: 0;
    font-size: 0.95em;
  }

  .payload.unknown-tool {
    color: var(--error-color, #f87171);
    border-left: 2px solid color-mix(in srgb, var(--error-color, #f87171) 40%, transparent);
    padding-left: 8px;
  }

  .live-sub-agent-trace {
    animation: slide-in-left 0.25s ease-out;
    border-left: 2px solid var(--neutral-stroke-rest);
    padding: 4px 8px;
    margin: 4px 0;
    opacity: 80%;
  }

  .live-sub-agent-name {
    font-family: monospace;
    font-size: 0.8em;
    opacity: 70%;
    font-style: italic;
    display: block;
    margin-bottom: 2px;
  }

  .sub-agent-trace {
    margin-top: 6px;
    border-left: 2px solid var(--neutral-stroke-rest);
    padding-left: 8px;
  }

  .sub-agent-trace-summary {
    font-family: monospace;
    font-size: 0.85em;
    opacity: 70%;
    cursor: pointer;
    user-select: none;
    padding: 2px 0;
  }

  .sub-agent-trace-summary:hover {
    opacity: 100%;
  }

  .sub-agent-message {
    font-family: monospace;
    font-size: 0.85em;
    margin-top: 4px;
  }

  .sub-agent-assistant {
    opacity: 85%;
    white-space: pre-wrap;
  }

  .sub-agent-tool-call {
    opacity: 60%;
  }

  .sub-agent-tool-name::before {
    content: '⚙ ';
  }

  .sub-agent-tool-result {
    opacity: 50%;
    border-left: 1px solid var(--neutral-stroke-rest);
    padding-left: 6px;
    white-space: pre-wrap;
  }

  .input-row {
    display: flex;
    gap: calc(var(--design-unit) * 2px);
    align-items: flex-end;
    padding: calc(var(--design-unit) * 2px) calc(var(--design-unit) * 3px);
    border-top: 1px solid var(--neutral-stroke-rest);
    background-color: var(--neutral-layer-2);
  }

  /* Mirror the messages column: cap the composer (input row + its buttons, resize
     handle, attachment chips, agent picker panel, suggestions) at the same max width
     and centre it, so it does not span full width while the messages sit in a
     narrower column. These are flex items in the column layout, so margin-inline:auto
     would disable their default stretch — width:100% keeps them filling up to the
     cap, and border-box keeps padding/border from overflowing when full-width.
     Overridable via ::part(input-row) etc. */
  .input-row,
  .composer-resize-handle,
  .attachment-chips,
  .agent-picker-panel,
  .session-menu-panel,
  chat-suggestions {
    box-sizing: border-box;
    width: 100%;
    max-width: var(--ai-conversation-max-width);
    margin-inline: auto;
  }

  /* When the assistant is wider than that cap the composer is genuinely constrained,
     so lift the input row — and the panels that sit above it — into matching
     floating, rounded, outlined cards with a gap to the bottom. Below the cap they
     stay full-width and flush (the default border-top). The query resolves against
     the assistant's own inline-size container (.chat-wrapper). The threshold is a
     literal because a container query can't read a custom property — keep it equal
     to the --ai-conversation-max-width default. */
  @container (width > 1500px) {
    .input-row,
    .agent-picker-panel,
    .session-menu-panel,
    chat-suggestions {
      border: 1px solid var(--neutral-stroke-rest);
      border-radius: 12px;
    }

    /* chat-suggestions paints its background on an inner element, so clip it to the
       rounded corners — otherwise that square inner background bleeds past the radius
       and the corners read as square with the border hidden behind them. */
    chat-suggestions {
      overflow: hidden;
    }

    .input-row {
      margin-bottom: 12px;
    }

    /* Suggestions and the picker stack above the input row — a smaller gap keeps
       the cards distinct without a large void. */
    .agent-picker-panel,
    .session-menu-panel,
    chat-suggestions {
      margin-bottom: 8px;
    }
  }

  /* Right-hand control column, mirroring .input-left-controls — holds the
     Stop button (shown while a turn runs) stacked above the Send button. The
     Stop button is an icon (stop → spinner), so the column width is driven by
     Send and stays stable whether a turn is running or idle. */
  .input-right-controls {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: calc(var(--design-unit) * 1px);
  }

  /* The "stopping" state shows a spinner — rotate it (the icon set's glyph is
     static). Transform-only, so it never affects layout/width. */
  @keyframes ai-stop-spin {
    to {
      transform: rotate(360deg);
    }
  }

  .stop-icon-spinning {
    /* inline-block so the icon host is transformable — an inline element
       ignores the transform, which is why the spinner appeared frozen. */
    display: inline-block;
    animation: ai-stop-spin 0.8s linear infinite;
  }

  .input-left-controls {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: calc(var(--design-unit) * 1px);
  }

  /* Lock the width so the column doesn't reflow when the button content
     switches between the "Auto" label, the pin icon, and the chevron. */
  .agent-toggle-button {
    width: 56px;
    min-width: 56px;
    max-width: 56px;
    box-sizing: border-box;
  }

  .agent-toggle-label {
    font-size: 0.75em;
    font-weight: 600;
    letter-spacing: 0.02em;
  }

  @keyframes agent-picker-slide-in {
    from {
      opacity: 0%;
      transform: translateY(8px);
    }

    to {
      opacity: 100%;
      transform: translateY(0);
    }
  }

  @keyframes agent-picker-slide-out {
    from {
      opacity: 100%;
      transform: translateY(0);
    }

    to {
      opacity: 0%;
      transform: translateY(8px);
    }
  }

  .agent-picker-panel {
    animation: agent-picker-slide-in 0.2s ease-out;
    overflow: hidden;
  }

  .agent-picker-panel.closing {
    animation: agent-picker-slide-out 0.2s ease-in forwards;
  }

  /* Session lifecycle menu (GENC-1351 §5.5) — a horizontal toolbar above the
     input row. The ⋮ button takes no width override, so it stretches to the
     Send/Stop column width like the Stop button it shares the slot with. */
  .session-menu-icon {
    font-size: 1.2em;
    line-height: 1;
  }

  .session-menu-panel {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: calc(var(--design-unit) * 2px);
    padding: calc(var(--design-unit) * 1.5px) calc(var(--design-unit) * 3px);
    overflow: hidden;
    border-top: 1px solid var(--neutral-stroke-rest);
    background-color: var(--neutral-layer-2);
    animation: agent-picker-slide-in 0.2s ease-out;
  }

  .session-menu-panel.closing {
    animation: agent-picker-slide-out 0.2s ease-in forwards;
  }

  /* Both buttons share a fixed min width so the pair never resizes when the
     labels swap to Confirm / Back. */
  .session-menu-item::part(control) {
    min-width: calc(var(--design-unit) * 20px);
    justify-content: center;
  }

  /* Destructive confirm (Clear) — tint the accent button to the error colour. */
  .session-menu-item.danger::part(control) {
    background: var(--error-color, #d13438);
    border-color: var(--error-color, #d13438);
  }

  /* Shown in place of the transcript while a manual compaction runs (GENC-1351). */
  .compacting-indicator {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: calc(var(--design-unit) * 2px);
    padding: calc(var(--design-unit) * 6px) 0;
    color: var(--neutral-foreground-hint, var(--neutral-foreground-rest));
  }

  .compacting-caption {
    font-size: 0.9em;
  }

  .chat-input {
    flex: 1;
    resize: none;

    /* Match the height of the left-controls column when it has stacked buttons,
       so the textarea fills the row instead of sitting flush to the send button. */
    align-self: stretch;

    /* Drag-to-resize. --composer-height (set inline by the handle) pins an
       explicit height once dragged; unset, height is auto and capped at 150px
       (the original auto-grow behaviour). The host is sized — not the inner
       control — because the design system already stretches its control to the
       host height, and that rule out-specifies anything we could set via
       ::part(control). Sizing the host (not just its max-height) is what stops
       a downward drag from squishing the textarea below its natural height. */
    height: var(--composer-height);
    max-height: var(--composer-height, 150px);
  }

  /* Full-width grab bar above the input row; drag up/down to resize the composer.
     Sits above the row so it never overlaps the send/stop controls. */
  .composer-resize-handle {
    height: 7px;
    flex-shrink: 0;
    cursor: ns-resize;
    touch-action: none;
    user-select: none;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .composer-resize-handle::after {
    content: '';
    width: 32px;
    height: 3px;
    border-radius: 999px;
    background-color: var(--neutral-stroke-rest);
    opacity: 0%;
    transition: opacity 0.15s ease;
  }

  .composer-resize-handle:hover::after {
    opacity: 70%;
  }

  .file-input {
    display: none;
  }

  .thinking-dots {
    display: flex;
    align-items: center;
    align-self: center;
    gap: 5px;
    padding: calc(var(--design-unit) * 2px) calc(var(--design-unit) * 3px);
  }

  .dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background-image: linear-gradient(145deg, rgb(255 255 255 / 40%) 0%, rgb(0 0 0 / 0%) 100%);
    animation: dot-bounce 1.5s linear infinite;
  }

  .dot-1 {
    background-color: ${AI_COLOUR_AMBER};
    animation-delay: 0s;
  }

  .dot-2 {
    background-color: ${AI_COLOUR_PINK};
    animation-delay: 0.1s;
  }

  .dot-3 {
    background-color: ${AI_COLOUR_CYAN};
    animation-delay: 0.2s;
  }

  .dot-4 {
    background-color: ${AI_COLOUR_VIOLET};
    animation-delay: 0.3s;
  }

  @keyframes dot-bounce {
    0%,
    50%,
    100% {
      transform: scale(1);
      filter: blur(0);
    }

    25% {
      transform: scale(0.6);
      filter: blur(2px);
    }

    75% {
      transform: scale(1.4);
      filter: blur(2px);
    }
  }

  .thinking-waves,
  .thinking-flowing-waves,
  .thinking-plasma {
    display: flex;
    flex-direction: column;
    align-items: center;
    align-self: center;
    gap: 6px;
    padding: calc(var(--design-unit) * 2px) calc(var(--design-unit) * 3px);
  }

  /* The flowing-waves host is the avatar's height (32px), so drop the top padding
     the shared rule adds — that keeps the wave line level with the avatar instead
     of dropping it toward the row's vertical centre. */
  .thinking-flowing-waves {
    padding-top: 0;
  }

  .thinking-caption {
    font-size: var(--type-ramp-minus-1-font-size, 12px);
    line-height: var(--type-ramp-minus-1-line-height, 16px);
    color: var(--neutral-foreground-rest);
    opacity: 70%;
  }

  .attachment-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 0 calc(var(--design-unit) * 3px) calc(var(--design-unit) * 2px);
  }

  .attachment-chip {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 8px;
    background-color: var(--neutral-layer-4);
    border-radius: calc(var(--control-corner-radius) * 1px);
    font-size: 0.9em;
    border: 1px solid var(--neutral-stroke-rest);
  }

  .attachment-chip-error {
    background-color: var(--error-fill-rest);
    border-color: var(--error-stroke-rest);
  }

  .chip-remove {
    background: none;
    border: none;
    color: var(--neutral-foreground-rest);
    cursor: pointer;
    font-size: 1.2em;
    padding: 0 0 0 4px;
    opacity: 70%;
  }

  .chip-remove:hover {
    opacity: 100%;
  }
`;

export const styles = css`
  ${baseStyles}
  ${settingsModalStyles}
`;
