/* Shared Components — Buttons, Cards, Forms, Badges, Toasts, Toggle */

@layer components {

  /* ---- Buttons ---- */
  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-4);
    font-size: var(--text-sm);
    font-weight: var(--weight-semibold);
    border-radius: var(--radius-sm);
    transition: background var(--transition-fast), box-shadow var(--transition-fast), opacity var(--transition-fast);
    white-space: nowrap;
    line-height: 1.4;
  }
  .btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    pointer-events: none;
  }

  .btn-primary {
    background: var(--color-primary);
    color: #fff;
  }
  .btn-primary:hover { background: var(--color-primary-hover); }

  .btn-success {
    background: var(--color-success);
    color: #fff;
  }
  .btn-success:hover { background: color-mix(in srgb, var(--color-success), white 12%); }

  .btn-danger {
    background: var(--color-danger);
    color: #fff;
  }
  .btn-danger:hover { background: color-mix(in srgb, var(--color-danger), white 12%); }

  .btn-warning {
    background: var(--color-warning);
    color: #111;
  }
  .btn-warning:hover { background: color-mix(in srgb, var(--color-warning), white 12%); }

  .btn-ghost {
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border-subtle);
  }
  .btn-ghost:hover {
    background: var(--surface-3);
    color: var(--text-primary);
    border-color: var(--border-default);
  }

  .btn-lg {
    padding: var(--space-3) var(--space-6);
    font-size: var(--text-base);
    border-radius: var(--radius-md);
  }

  .btn-sm {
    padding: var(--space-1) var(--space-3);
    font-size: var(--text-xs);
  }

  .btn-icon {
    padding: var(--space-2);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    background: transparent;
  }
  .btn-icon:hover {
    background: var(--surface-3);
    color: var(--text-primary);
  }

  /* ---- Cards ---- */
  .card {
    background: var(--surface-2);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    padding: var(--space-5);
    transition: box-shadow var(--transition-base);
  }
  .card:hover {
    box-shadow: var(--shadow-sm);
  }

  .card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--space-4);
  }

  .card-title {
    font-size: var(--text-lg);
    font-weight: var(--weight-semibold);
  }

  /* ---- Forms ---- */
  .form-group {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
  }

  .form-label {
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
  }

  .form-input,
  .form-select,
  .form-textarea {
    width: 100%;
    padding: var(--space-2) var(--space-3);
    background: var(--surface-0);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: var(--text-sm);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
  }
  .form-input:focus,
  .form-select:focus,
  .form-textarea:focus {
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px var(--color-primary-subtle);
    outline: none;
  }
  .form-input::placeholder,
  .form-textarea::placeholder {
    color: var(--text-muted);
  }

  .form-select {
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%239898a8' stroke-width='2'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right var(--space-2) center;
    padding-right: var(--space-8);
  }

  .form-textarea {
    resize: vertical;
    min-height: 72px;
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    line-height: 1.6;
  }

  .form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-4);
  }
  @media (max-width: 600px) {
    .form-row { grid-template-columns: 1fr; }
  }

  .form-check {
    display: flex;
    align-items: center;
    gap: var(--space-2);
  }
  .form-check input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--color-primary);
  }
  .form-check label {
    font-size: var(--text-sm);
    color: var(--text-secondary);
    cursor: pointer;
  }

  /* ---- Badges ---- */
  .badge {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    padding: 2px var(--space-2);
    font-size: 11px;
    font-weight: var(--weight-semibold);
    border-radius: var(--radius-full);
    line-height: 1.4;
  }
  .badge-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    flex-shrink: 0;
  }

  .badge-success {
    background: var(--color-success-subtle);
    color: var(--color-success);
  }
  .badge-success .badge-dot { background: var(--color-success); }

  .badge-danger {
    background: var(--color-danger-subtle);
    color: var(--color-danger);
  }
  .badge-danger .badge-dot { background: var(--color-danger); }

  .badge-warning {
    background: var(--color-warning-subtle);
    color: var(--color-warning);
  }
  .badge-warning .badge-dot { background: var(--color-warning); }

  .badge-muted {
    background: var(--surface-3);
    color: var(--text-secondary);
  }
  .badge-muted .badge-dot { background: var(--text-muted); }

  /* ---- Toast ---- */
  .toast-container {
    position: fixed;
    top: var(--space-4);
    right: var(--space-4);
    z-index: var(--z-toast);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    pointer-events: none;
  }
  .toast {
    pointer-events: auto;
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    color: #fff;
    box-shadow: var(--shadow-lg);
    opacity: 0;
    transform: translateX(20px);
    transition: opacity var(--transition-base), transform var(--transition-base);
  }
  .toast.show {
    opacity: 1;
    transform: translateX(0);
  }
  .toast-success { background: var(--color-success); }
  .toast-error { background: var(--color-danger); }
  .toast-warn { background: var(--color-warning); color: #111; }

  /* ---- Status badge (connected/disconnected) ---- */
  .status-badge {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-1) var(--space-3);
    border-radius: var(--radius-full);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
  }
  .status-connected {
    background: var(--color-success-subtle);
    color: var(--color-success);
  }
  .status-connecting {
    background: var(--color-warning-subtle);
    color: var(--color-warning);
  }
  .status-disconnected {
    background: var(--surface-3);
    color: var(--text-muted);
  }

  /* ---- Transcript ---- */
  .transcript {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
  }
  .transcript-entry {
    font-size: var(--text-sm);
    line-height: 1.5;
  }
  .transcript-user {
    color: #60a5fa;
  }
  .transcript-assistant {
    color: var(--color-success);
  }
  /* In-flight interim transcript — set by setInterimUserTranscript while
     STT is still streaming. Italic + dimmed reads as "still being said."
     finalizeUserTranscript removes the class to lock the bubble in. */
  .transcript-user.transcript-interim {
    opacity: 0.55;
    font-style: italic;
  }
  .transcript-placeholder {
    color: var(--text-muted);
    text-align: center;
    font-size: var(--text-sm);
    padding: var(--space-4);
  }

  /* ---- Spinner ---- */
  .spinner {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 2.5px solid var(--border-subtle);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
  }
  @keyframes spin { to { transform: rotate(360deg); } }

  .spinner-sm {
    width: 14px;
    height: 14px;
    border-width: 2px;
  }

  /* ---- Range Slider ----
     Filled-progress track: left of the thumb is the accent color, right
     of the thumb is a visible but quiet gray. The --fill CSS variable is
     set per-instance by JS on input events (see profile-editor.js).
     Defaults to 0% so sliders render correctly before JS runs. */
  .form-range {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 8px;
    background: linear-gradient(
      to right,
      var(--color-primary) 0%,
      var(--color-primary) var(--fill, 0%),
      color-mix(in srgb, var(--surface-3), #fff 6%) var(--fill, 0%),
      color-mix(in srgb, var(--surface-3), #fff 6%) 100%
    );
    border: 1px solid var(--border-default);
    border-radius: 4px;
    outline: none;
    cursor: pointer;
    margin-top: var(--space-2);
  }
  .form-range::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--color-primary);
    border: 2px solid var(--surface-1);
    box-shadow: 0 0 0 2px var(--color-primary-subtle);
    cursor: pointer;
    transition: box-shadow var(--transition-fast);
  }
  .form-range::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--color-primary);
    border: 2px solid var(--surface-1);
    box-shadow: 0 0 0 2px var(--color-primary-subtle);
    cursor: pointer;
  }
  .form-range:focus-visible::-webkit-slider-thumb {
    box-shadow: 0 0 0 3px var(--color-primary-subtle);
  }

  .slider-value {
    float: right;
    font-weight: var(--weight-semibold);
    color: var(--text-primary);
    font-variant-numeric: tabular-nums;
    cursor: text;
    padding: 0 var(--space-1);
    border-radius: var(--radius-sm);
    transition: background var(--transition-fast), color var(--transition-fast);
  }
  .slider-value:hover {
    background: var(--surface-3);
    color: var(--color-primary);
  }

  .slider-value-edit {
    width: 5ch;
    padding: 0 2px;
    margin: 0;
    border: none;
    border-bottom: 1.5px solid var(--color-primary);
    background: transparent;
    color: var(--text-primary);
    font: inherit;
    font-weight: var(--weight-semibold);
    font-variant-numeric: tabular-nums;
    text-align: right;
    outline: none;
    -moz-appearance: textfield;
  }
  .slider-value-edit::-webkit-inner-spin-button,
  .slider-value-edit::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
  }

  /* ---- Info Tooltip ---- */
  .info-tip {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    margin-left: 6px;
    border-radius: 50%;
    background: transparent;
    border: 1.5px solid var(--text-muted);
    color: var(--text-muted);
    font-size: 10px;
    font-weight: var(--weight-bold);
    font-style: normal;
    cursor: help;
    vertical-align: middle;
    flex-shrink: 0;
    line-height: 1;
    transition: border-color var(--transition-fast), color var(--transition-fast), background var(--transition-fast);
  }
  .info-tip:hover {
    background: var(--color-primary-subtle);
    border-color: var(--color-primary);
    color: var(--color-primary);
  }
  .info-tip::after {
    content: attr(aria-label);
    position: absolute;
    bottom: calc(100% + 8px);
    left: 50%;
    transform: translateX(-50%);
    padding: var(--space-2) var(--space-3);
    background: var(--surface-0);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: var(--text-xs);
    font-weight: var(--weight-normal);
    text-transform: none;
    letter-spacing: 0;
    white-space: normal;
    width: 220px;
    line-height: 1.5;
    box-shadow: var(--shadow-md);
    pointer-events: none;
    opacity: 0;
    transition: opacity var(--transition-fast);
    z-index: var(--z-tooltip);
  }
  .info-tip:hover::after {
    opacity: 1;
  }

  /* ---- Visual Prompt Editor (Overlay) ---- */
  .visual-prompt-editor {
    position: relative;
    background: var(--surface-0);
    border-radius: var(--radius-sm);
  }
  .visual-prompt-highlight {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    padding: var(--space-2) var(--space-3);
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    line-height: 1.6;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow: hidden;
    pointer-events: none;
    color: var(--text-primary);
    z-index: 0;
  }
  .visual-prompt-editor .form-textarea {
    position: relative;
    z-index: 1;
    background: transparent;
    color: transparent;
    caret-color: var(--text-primary);
  }
  /* Overlay pills must not affect text flow — no padding/border/font-weight */
  .visual-prompt-highlight .var-pill {
    padding: 0;
    border: none;
    font-weight: inherit;
    border-radius: 3px;
  }
  /* Use box-shadow instead of border for ref pills (no layout impact) */
  .visual-prompt-highlight .var-pill.var-ref.var-fragment {
    box-shadow: inset 0 0 0 1.5px rgba(46, 184, 138, 0.6);
  }
  .visual-prompt-highlight .var-pill.var-ref.var-recent {
    box-shadow: inset 0 0 0 1.5px rgba(91, 91, 214, 0.6);
  }
  .visual-prompt-highlight .var-pill.var-ref.var-conversation {
    box-shadow: inset 0 0 0 1.5px rgba(212, 167, 44, 0.6);
  }

  /* Variable pills */
  .var-pill {
    display: inline;
    padding: 1px 5px;
    border-radius: 4px;
    font-weight: var(--weight-semibold);
    font-size: inherit;
    font-family: var(--font-mono);
    white-space: nowrap;
  }
  /* Reference pills (outlined/dimmer) — used in system prompt */
  .var-pill.var-ref {
    border: 1.5px solid;
    background: transparent;
  }
  .var-pill.var-ref.var-fragment {
    border-color: rgba(46, 184, 138, 0.6);
    color: rgba(46, 184, 138, 0.85);
  }
  .var-pill.var-ref.var-recent {
    border-color: rgba(91, 91, 214, 0.6);
    color: rgba(91, 91, 214, 0.85);
  }
  .var-pill.var-ref.var-conversation {
    border-color: rgba(212, 167, 44, 0.6);
    color: rgba(212, 167, 44, 0.85);
  }
  /* Data pills (filled/solid) — used in user message */
  .var-pill.var-data {
    border: none;
  }
  .var-pill.var-data.var-fragment {
    background: rgba(46, 184, 138, 0.2);
    color: #2eb88a;
  }
  .var-pill.var-data.var-recent {
    background: rgba(91, 91, 214, 0.2);
    color: #5b5bd6;
  }
  .var-pill.var-data.var-conversation {
    background: rgba(212, 167, 44, 0.2);
    color: #d4a72c;
  }
  /* Generic ref/data pills (for the note footer) */
  .var-pill.var-ref:not(.var-fragment):not(.var-recent):not(.var-conversation) {
    border-color: var(--border-default);
    color: var(--text-muted);
  }
  .var-pill.var-data:not(.var-fragment):not(.var-recent):not(.var-conversation) {
    background: var(--surface-3);
    color: var(--text-secondary);
  }

  /* ---- Variable Reference Card ---- */
  .visual-var-ref {
    margin-top: var(--space-2);
    background: var(--surface-1);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    overflow: hidden;
  }
  .visual-var-ref-toggle {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--space-2) var(--space-3);
    background: none;
    border: none;
    cursor: pointer;
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    transition: color var(--transition-fast);
  }
  .visual-var-ref-toggle:hover { color: var(--text-secondary); }
  .visual-var-ref-toggle .chevron {
    transition: transform var(--transition-fast);
  }
  .visual-var-ref:not(.open) .visual-var-ref-body { display: none; }
  .visual-var-ref.open .visual-var-ref-toggle .chevron { transform: rotate(180deg); }
  .visual-var-ref-body {
    padding: 0 var(--space-3) var(--space-3);
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
  }
  .visual-var-entry {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
  }
  .visual-var-pills {
    display: flex;
    align-items: center;
    gap: var(--space-2);
  }
  .visual-var-arrow {
    color: var(--text-muted);
    font-size: var(--text-xs);
  }
  .visual-var-desc {
    font-size: var(--text-xs);
    color: var(--text-secondary);
    line-height: 1.5;
    padding-left: var(--space-1);
  }
  .visual-var-slider {
    margin-top: var(--space-2);
  }
  .visual-var-slider .form-label {
    font-size: var(--text-xs);
    margin-bottom: var(--space-1);
  }
  .visual-var-note {
    font-size: 11px;
    color: var(--text-muted);
    padding: var(--space-2) var(--space-3);
    background: var(--surface-0);
    border-radius: var(--radius-sm);
    line-height: 1.8;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 2px;
  }

  /* ---- API Key Input Row ---- */
  .key-input-row {
    display: flex;
    gap: var(--space-2);
    align-items: center;
  }
  .key-input-row .form-input {
    flex: 1;
  }

  /* ---- API Key Status Indicator ---- */
  .key-status {
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
    margin-top: var(--space-1);
  }
  .key-set {
    color: var(--color-success);
  }
  .key-unset {
    color: var(--text-muted);
  }

  /* ---- Mic Selector ---- */
  .mic-selector {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
  }

  .mic-device-select {
    width: 100%;
    font-size: var(--text-sm);
    color: var(--text-secondary);
    background: var(--surface-1);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    padding: var(--space-1) var(--space-2);
    cursor: pointer;
  }

  .mic-device-select:focus {
    outline: 2px solid var(--color-primary);
    outline-offset: 1px;
  }

  .btn-reset-visuals {
    color: var(--color-warning, #f59e0b);
    border: 1px solid color-mix(in srgb, var(--color-warning, #f59e0b) 35%, transparent);
    background: transparent;
    display: flex;
    align-items: center;
    gap: var(--space-2);
  }

  .btn-reset-visuals:hover:not(:disabled) {
    background: color-mix(in srgb, var(--color-warning, #f59e0b) 10%, transparent);
  }

  .btn-reset-visuals:disabled {
    opacity: 0.35;
    cursor: not-allowed;
  }

  /* Visitor "Skip intro" — ghost button shown only during the opening greeting */
  .btn-skip-intro:disabled {
    opacity: 0.35;
    cursor: default;
  }

  .btn-shortcut {
    font-size: var(--text-xs);
    opacity: 0.7;
    font-family: monospace;
    letter-spacing: 0.02em;
  }

  .mic-error-banner {
    font-size: var(--text-sm);
    color: var(--color-error, #ef4444);
    background: color-mix(in srgb, var(--color-error, #ef4444) 10%, transparent);
    border: 1px solid color-mix(in srgb, var(--color-error, #ef4444) 30%, transparent);
    border-radius: var(--radius-sm);
    padding: var(--space-2) var(--space-3);
    line-height: 1.4;
  }
}

/* ---- Operator stage strip (visitor ?op=1 + admin Live session) ---- */
.op-strip {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border: 1px dashed var(--border-color, var(--surface-2));
  border-radius: var(--radius-md);
  background: var(--surface-1);
  font-size: var(--text-xs);
}
.op-strip-held { border-color: var(--color-warning); }
.op-strip-chips { display: flex; flex-wrap: wrap; gap: var(--space-1); }
.op-chip {
  padding: 2px 8px;
  border-radius: var(--radius-full);
  background: var(--surface-2);
  color: var(--text-secondary);
  white-space: nowrap;
}
.op-chip.done { opacity: 0.55; text-decoration: line-through; }
.op-chip.active { background: var(--color-primary); color: #fff; }
.op-strip-line { color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.op-strip-btns { display: flex; gap: var(--space-2); flex-wrap: wrap; }
.op-btn[disabled] { opacity: 0.4; }

