// ============================================================
// Terminal Component
// .cyber-terminal — console window: header bar, scrolling body, input row.
// A11y: a live console that prints new lines should be `role="log"` (it is
// polite by default). Give `.cyber-terminal__input-field` an `aria-label`.
// A scrolling body needs `tabindex="0"` so the keyboard can reach it.
// ============================================================

@use "kit";

// Reduced-motion overrides, shared by the media query and data-motion="reduce"
@mixin -reduced-motion {
  .cyber-terminal__cursor {
    animation: none;
  }

  .cyber-terminal-typing {
    animation: none;
  }
}

@layer components {
  .cyber-terminal {
    --terminal-color: var(--cyber-green-500);
    --terminal-text: var(--cyber-green-400);
    --terminal-border: var(--cyber-green-700);

    position: relative;
    border: 1px solid var(--terminal-border);
    overflow: hidden;
    font-family: var(--font-mono);
    font-size: var(--text-sm);
    background: var(--color-bg-secondary);

    // Header bar: dots on the leading side, title on the trailing side
    &__header {
      display: flex;
      gap: var(--space-2xs);
      align-items: center;
      min-height: 2rem;
      padding: var(--space-3xs) var(--space-sm);
      border-bottom: 1px solid var(--terminal-border);
      background: color-mix(in srgb, var(--terminal-color) 14%, var(--color-bg-secondary));
    }

    &__dot {
      flex: none;
      width: 0.625rem;
      height: 0.625rem;
      border-radius: 50%;
      background: var(--cyber-chrome-600);

      &--red {
        background: var(--cyber-magenta-500);
      }

      &--yellow {
        background: var(--cyber-yellow-500);
      }

      &--green {
        background: var(--cyber-green-500);
      }
    }

    &__title {
      overflow: hidden;
      font-size: kit.$text-xs;
      letter-spacing: 0.1em;
      text-transform: uppercase;
      white-space: nowrap;
      color: var(--terminal-color);
      margin-inline-start: auto;
      text-overflow: ellipsis;
    }

    &__body {
      @include kit.scrollbar;

      padding: var(--space-sm) var(--space-md);
      overflow: auto;
      line-height: 1.7;
      color: var(--terminal-text);
      overflow-wrap: anywhere;
      tab-size: 2;

      &:focus-visible {
        @include kit.focus-ring(var(--terminal-color), -2px);
      }
    }

    &__prompt {
      flex: none;
      color: var(--cyber-cyan-500);
      user-select: none;
    }

    // Block cursor. Steady in reduced motion.
    &__cursor {
      display: inline-block;
      width: 0.55em;
      height: 1.1em;
      background: var(--terminal-color);
      box-shadow: 0 0 0.5rem color-mix(in srgb, var(--terminal-color) 70%, transparent);
      animation: cyber-terminal-blink 1s step-end infinite;
      margin-inline-start: 2px;
      vertical-align: text-bottom;
    }

    &__line {
      display: flex;
      gap: var(--space-2xs);
      white-space: pre-wrap;

      &--input {
        color: var(--color-text-primary);
      }

      &--output {
        color: var(--color-text-secondary);
      }

      &--error {
        color: var(--cyber-magenta-500);
      }

      &--success {
        color: var(--cyber-green-500);
      }

      &--warning {
        color: var(--cyber-yellow-500);
      }

      &--info {
        color: var(--cyber-cyan-500);
      }
    }

    &__command {
      color: var(--color-text-primary);
    }

    // Input row for interactive terminals
    &__input {
      display: flex;
      gap: var(--space-2xs);
      align-items: center;
      min-height: 2.75rem;
      padding: var(--space-3xs) var(--space-md);
      border-top: 1px solid var(--terminal-border);
      background: var(--color-bg-tertiary);

      // The row shows focus, since the field itself has no box
      &:focus-within {
        box-shadow: inset 0 0 0 2px var(--cyber-cyan-500);
      }
    }

    &__input-field {
      flex: 1;
      min-width: 0;
      border: 0;
      font: inherit;
      color: var(--color-text-primary);
      background: transparent;
      caret-color: var(--cyber-cyan-500);
      outline: 2px solid transparent;

      &::placeholder {
        color: var(--cyber-chrome-400);
        opacity: 100%;
      }
    }

    // --------------------------------------------------------
    // Syntax tokens
    // --------------------------------------------------------

    .syntax-keyword {
      color: var(--cyber-magenta-500);
    }

    .syntax-string {
      color: var(--cyber-green-500);
    }

    .syntax-number {
      color: var(--cyber-yellow-500);
    }

    .syntax-comment {
      font-style: italic;
      color: var(--cyber-chrome-400);
    }

    .syntax-function {
      color: var(--cyber-cyan-500);
    }

    .syntax-variable {
      color: var(--cyber-chrome-200);
    }

    .syntax-operator {
      color: var(--cyber-chrome-300);
    }

    // --------------------------------------------------------
    // Sizes
    // --------------------------------------------------------

    &--sm {
      font-size: kit.$text-xs;

      .cyber-terminal__body {
        min-height: 6.25rem;
        max-height: 15.5rem;
      }
    }

    &--lg {
      font-size: var(--text-base);

      .cyber-terminal__body {
        min-height: 18.75rem;
        max-height: 43.75rem;
      }
    }

    &--fullheight .cyber-terminal__body {
      max-height: none;
    }

    // --------------------------------------------------------
    // Styles
    // --------------------------------------------------------

    &--no-header .cyber-terminal__header {
      display: none;
    }

    &--rounded {
      border-radius: var(--radius-lg);
    }

    &--glow {
      box-shadow:
        0 0 1.25rem color-mix(in srgb, var(--terminal-color) 22%, transparent),
        inset 0 0 3.5rem color-mix(in srgb, var(--terminal-color) 5%, transparent);
    }

    // CRT scanlines over the whole window
    &--scanlines::after {
      content: "";
      position: absolute;
      z-index: 1;
      background: repeating-linear-gradient(
        0deg,
        transparent 0 2px,
        color-mix(in srgb, var(--cyber-void-900) 28%, transparent) 2px 4px
      );
      pointer-events: none;
      inset: 0;
    }

    &--cyan {
      --terminal-color: var(--cyber-cyan-500);
      --terminal-text: var(--cyber-cyan-300);
      --terminal-border: var(--cyber-cyan-700);
    }

    &--magenta {
      --terminal-color: var(--cyber-magenta-500);
      --terminal-text: var(--cyber-chrome-200);
      --terminal-border: var(--cyber-magenta-700);
    }
  }

  // Typewriter reveal for one line of text. --typing-chars sets the step count.
  .cyber-terminal-typing {
    --typing-chars: 30;

    max-width: 100%;
    overflow: hidden;
    white-space: nowrap;
    border-inline-end: 2px solid var(--cyber-cyan-500);

    @include kit.motion-safe {
      animation:
        cyber-terminal-typing 2s steps(var(--typing-chars), end) both,
        cyber-terminal-caret 1s step-end infinite;
    }
  }

  .cyber-terminal-ascii {
    overflow-x: auto;
    line-height: 1.2;
    white-space: pre;
    color: var(--cyber-cyan-500);
    text-shadow: 0 0 0.6rem color-mix(in srgb, var(--cyber-cyan-500) 70%, transparent);
  }

  @keyframes cyber-terminal-blink {
    50% {
      opacity: 0%;
    }
  }

  @keyframes cyber-terminal-typing {
    from {
      width: 0;
    }

    to {
      width: 100%;
    }
  }

  @keyframes cyber-terminal-caret {
    50% {
      border-color: transparent;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    @include -reduced-motion;
  }

  [data-motion="reduce"] {
    @include -reduced-motion;
  }

  @media (prefers-contrast: more) {
    .cyber-terminal {
      --terminal-border: var(--terminal-color);

      &--scanlines::after {
        display: none;
      }
    }
  }

  @media (forced-colors: active) {
    .cyber-terminal__cursor {
      background: CanvasText;
      forced-color-adjust: none;
    }
  }
}
