/* ==========================================================================
   THE LEVELED [[ PICKER + THE TOKEN FIELD (wave 3 - CONVERSATION-BUILDER.md section 20)

   The token field: a contenteditable that draws text and CHIPS (friendly path + type
   icon; red when the token can never hold a value, dashed when it may be blank). The
   picker: a breadcrumb level, a search, rows with the raw token in muted mono, the
   highlighted row's doc line, the collection filter's form. Popups float under the
   caret; the Tokens panel embeds the same picker in browse mode.

   Only --theme-* / --conv-* custom properties (Tests/ConversationBuilderPortalTests).
   ========================================================================== */

/* ── the field ─────────────────────────────────────────────────────────── */

.conv-tok-field {
    position: relative;
    /* the room the text leaves for the [[ ]] / {} buttons over the top-right corner (74px of buttons, a 6px
       inset, a little air) - one token, so the input and the raw editor never drift apart again */
    --conv-tok-tools-room: 88px;
}

/* A field of words offers the emoji picker as a third tool (Components/Emoji - section 17 "The emoji picker"): 28px more
   room on every line, so text never runs under the smiley. */
.conv-tok-field.has-emoji {
    --conv-tok-tools-room: 116px;
}

.conv-tok-input {
    display: block;
    width: 100%;
    min-height: 38px;
    padding: 8px var(--conv-tok-tools-room) 8px 10px;
    border: 1px solid var(--theme-border);
    border-radius: var(--theme-radius-button);
    background: var(--theme-background);
    color: var(--theme-text);
    font: inherit;
    font-size: var(--theme-font-size-sm);
    line-height: 1.6;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    cursor: text;
    transition: border-color var(--conv-motion), box-shadow var(--conv-motion);
}

/* A ONE-LINE FIELD WRAPS (Kirk, 2026-10-04: a single-line pill with a sideways scrollbar, the chips cut off, the [[ ]] /
   { } buttons over the text). The value stays one line - Enter never adds a line break - but its words and chips flow
   onto more lines as the field needs them, so everything in it is in view, and nothing ever scrolls under the buttons:
   the room kept for them on the right is outside the text on every line. Tests/TokenFieldWrapTests. */
.conv-tok-field.is-single .conv-tok-input {
    overflow: visible;
}

.conv-tok-field.is-mono .conv-tok-input {
    font-family: var(--theme-font-mono);
    font-size: var(--theme-font-size-xs);
}

.conv-tok-input:focus {
    border-color: var(--conv-fam, var(--conv-ask));
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--conv-fam, var(--conv-ask)) 16%, transparent);
    outline: none;
}

.conv-tok-input:empty::before {
    content: attr(data-placeholder);
    color: var(--theme-text-placeholder, var(--theme-text-muted));
    pointer-events: none;
}

.conv-tok-field.is-disabled .conv-tok-input {
    opacity: .65;
    cursor: default;
}

.conv-tok-field.has-bad .conv-tok-input {
    border-color: color-mix(in srgb, var(--conv-issue-error) 55%, var(--theme-border));
}

.conv-tok-tools {
    position: absolute;
    top: 5px;
    right: 6px;
    display: flex;
    gap: 2px;
}

.conv-tok-tool {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 26px;
    height: 24px;
    padding: 0 5px;
    border: 1px solid transparent;
    border-radius: var(--theme-radius-button);
    background: transparent;
    color: var(--theme-text-muted);
    font-size: var(--theme-font-size-xs);
    cursor: pointer;
    transition: background var(--conv-motion), color var(--conv-motion);
}

.conv-tok-tool:hover:not(:disabled),
.conv-tok-tool:focus-visible {
    background: var(--theme-background-hover);
    color: var(--theme-text);
    outline: none;
}

.conv-tok-tool.is-on {
    background: var(--conv-ask-soft);
    color: var(--conv-ask);
}

.conv-tok-tool:disabled {
    opacity: .45;
    cursor: default;
}

.conv-tok-brackets {
    font-family: var(--theme-font-mono);
    font-weight: 600;
    letter-spacing: -.02em;
}

.conv-tok-raw {
    padding-right: var(--conv-tok-tools-room);
}

.conv-tok-warn {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 4px;
    color: var(--conv-issue-error);
    font-size: var(--theme-font-size-xs);
}

/* ── chips ─────────────────────────────────────────────────────────────── */

.conv-tokchip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    max-width: 100%;
    margin: 0 1px;
    padding: 0 7px;
    border: 1px solid color-mix(in srgb, var(--conv-ask) 28%, transparent);
    border-radius: 999px;
    background: var(--conv-ask-soft);
    color: var(--theme-text-emphasis);
    font-family: var(--theme-font);
    font-size: var(--theme-font-size-xs);
    line-height: 1.55;
    vertical-align: baseline;
    white-space: nowrap;
    cursor: pointer;
    user-select: none;
    transition: box-shadow var(--conv-motion), background var(--conv-motion);
}

.conv-tokchip > .bi {
    color: var(--conv-ask);
    font-size: .95em;
}

.conv-tokchip-label {
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 260px;
}

.conv-tokchip:hover {
    box-shadow: 0 0 0 2px color-mix(in srgb, var(--conv-ask) 22%, transparent);
}

.conv-tokchip--blank {
    border-style: dashed;
    border-color: color-mix(in srgb, var(--conv-issue-warn) 55%, transparent);
    background: color-mix(in srgb, var(--conv-issue-warn) 10%, var(--theme-background-raised));
}

.conv-tokchip--blank > .bi {
    color: var(--conv-issue-warn);
}

.conv-tokchip--bad {
    border-color: color-mix(in srgb, var(--conv-issue-error) 60%, transparent);
    background: color-mix(in srgb, var(--conv-issue-error) 12%, var(--theme-background-raised));
    color: var(--conv-issue-error);
}

.conv-tokchip--bad > .bi {
    color: var(--conv-issue-error);
}

/* ── the picker ────────────────────────────────────────────────────────── */

.conv-tok-picker {
    display: flex;
    flex-direction: column;
    background: var(--theme-background-raised);
    color: var(--theme-text);
    font-family: var(--theme-font);
    font-size: var(--theme-font-size-sm);
}

.conv-tok-picker--inline,
.conv-tok-picker--popup {
    position: fixed;
    z-index: 1080;
    /* TokenPopupPlacement: never wider than a phone's screen; its inline max-height is the room where it opens */
    width: min(380px, calc(100vw - 16px));
    max-height: 360px;
    border: 1px solid var(--theme-border);
    border-radius: var(--theme-radius);
    box-shadow: var(--theme-shadow-lg);
    overflow: hidden;
    animation: conv-tok-rise 120ms ease-out;
}

.conv-tok-picker--browse {
    border: 1px solid var(--theme-border-subtle);
    border-radius: var(--theme-radius);
    min-height: 0;
}

@keyframes conv-tok-rise {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: none; }
}

.conv-tok-head {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 8px 10px 6px;
    border-bottom: 1px solid var(--theme-border-subtle);
}

.conv-tok-back {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border: none;
    border-radius: var(--theme-radius-button);
    background: transparent;
    color: var(--theme-text-secondary);
    cursor: pointer;
}

.conv-tok-back:hover,
.conv-tok-back:focus-visible {
    background: var(--theme-background-hover);
    outline: none;
}

.conv-tok-crumbs {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 2px;
    min-width: 0;
    font-size: var(--theme-font-size-xs);
}

.conv-tok-crumb {
    padding: 1px 4px;
    border: none;
    border-radius: var(--theme-radius-button);
    background: transparent;
    color: var(--theme-text-secondary);
    font: inherit;
    cursor: pointer;
}

.conv-tok-crumb:hover,
.conv-tok-crumb:focus-visible {
    background: var(--theme-background-hover);
    color: var(--theme-text);
    outline: none;
}

.conv-tok-crumb.is-current {
    color: var(--theme-text-emphasis);
    font-weight: 600;
    cursor: default;
}

.conv-tok-crumb-sep {
    color: var(--theme-text-muted);
    font-size: .75em;
}

/* Opened from a chip: Remove takes the chip out of its field (picking another token changes it). Quiet until pointed at;
   the danger ink only on hover / focus. */
.conv-tok-remove {
    display: inline-flex;
    flex: none;
    align-items: center;
    gap: 4px;
    margin-left: auto;
    padding: 2px 8px;
    border: 1px solid var(--theme-border-subtle);
    border-radius: var(--theme-radius-button);
    background: transparent;
    color: var(--theme-text-secondary);
    font: inherit;
    font-size: var(--theme-font-size-xs);
    cursor: pointer;
    transition: background var(--conv-motion), color var(--conv-motion), border-color var(--conv-motion);
}

.conv-tok-remove:hover,
.conv-tok-remove:focus-visible {
    border-color: color-mix(in srgb, var(--conv-issue-error) 45%, var(--theme-border-subtle));
    background: color-mix(in srgb, var(--conv-issue-error) 10%, var(--theme-background-raised));
    color: var(--conv-issue-error);
    outline: none;
}

.conv-tok-search {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    border-bottom: 1px solid var(--theme-border-subtle);
    color: var(--theme-text-muted);
}

.conv-tok-search input {
    flex: 1;
    min-width: 0;
    border: none;
    background: transparent;
    color: var(--theme-text);
    font: inherit;
    outline: none;
}

.conv-tok-typed {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    color: var(--theme-text-muted);
    font-size: var(--theme-font-size-xs);
}

.conv-tok-list {
    flex: 1;
    min-height: 0;
    margin: 0;
    padding: 4px;
    overflow-y: auto;
    list-style: none;
}

.conv-tok-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    border-radius: var(--theme-radius-button);
    cursor: pointer;
    transition: background var(--conv-motion);
}

.conv-tok-row:hover {
    background: var(--theme-background-hover);
}

.conv-tok-row.is-active {
    background: var(--conv-ask-soft);
    box-shadow: inset 2px 0 0 var(--conv-ask);
}

.conv-tok-row.is-later {
    opacity: .55;
}

.conv-tok-icon {
    flex: none;
    width: 18px;
    color: var(--conv-ask);
    text-align: center;
}

.conv-tok-icon--group,
.conv-tok-icon--family {
    color: var(--conv-flow);
}

.conv-tok-icon--function {
    color: var(--conv-act);
}

.conv-tok-icon--ambient,
.conv-tok-icon--param {
    color: var(--conv-say);
}

.conv-tok-main {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
}

.conv-tok-label {
    overflow: hidden;
    color: var(--theme-text-emphasis);
    text-overflow: ellipsis;
    white-space: nowrap;
}

.conv-tok-sub {
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
}

.conv-tok-key {
    overflow: hidden;
    color: var(--theme-text-muted);
    font-family: var(--theme-font-mono);
    font-size: var(--theme-font-size-xs);
    text-overflow: ellipsis;
    white-space: nowrap;
}

.conv-tok-from {
    flex: none;
    color: var(--theme-text-secondary);
    font-size: var(--theme-font-size-xs);
}

.conv-tok-badge {
    flex: none;
    padding: 0 6px;
    border: 1px dashed color-mix(in srgb, var(--conv-issue-warn) 60%, transparent);
    border-radius: 999px;
    color: var(--theme-text-secondary);
    font-size: var(--theme-font-size-xs);
}

.conv-tok-badge.is-later {
    border-color: var(--theme-border);
    border-style: solid;
}

.conv-tok-sample {
    flex: none;
    max-width: 120px;
    overflow: hidden;
    color: var(--theme-success, var(--theme-text-secondary));
    font-family: var(--theme-font-mono);
    font-size: var(--theme-font-size-xs);
    text-overflow: ellipsis;
    white-space: nowrap;
}

.conv-tok-more {
    flex: none;
    color: var(--theme-text-muted);
}

.conv-tok-act {
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border: none;
    border-radius: var(--theme-radius-button);
    background: transparent;
    color: var(--theme-text-muted);
    cursor: pointer;
}

.conv-tok-act:hover,
.conv-tok-act:focus-visible {
    background: var(--theme-surface-hover, var(--theme-background-hover));
    color: var(--conv-ask);
    outline: none;
}

.conv-tok-empty,
.conv-tok-note {
    display: flex;
    align-items: flex-start;
    gap: 6px;
    padding: 8px 12px;
    color: var(--theme-text-muted);
    font-size: var(--theme-font-size-xs);
}

.conv-tok-note.is-error {
    color: var(--conv-issue-error);
}

.conv-tok-doc {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 8px 12px;
    border-top: 1px solid var(--theme-border-subtle);
    background: var(--theme-background-subtle);
    font-size: var(--theme-font-size-xs);
}

.conv-tok-doc strong {
    color: var(--theme-text-emphasis);
    font-weight: 600;
}

.conv-tok-doc-why {
    color: var(--theme-text-secondary);
    font-style: italic;
}

.conv-tok-foot {
    padding: 6px 12px;
    border-top: 1px solid var(--theme-border-subtle);
    color: var(--theme-text-muted);
    font-size: var(--theme-font-size-xs);
}

/* ── the collection filter's form ─────────────────────────────────────── */

.conv-tok-filter {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 10px 12px;
    overflow-y: auto;
}

.conv-tok-filter-title {
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--theme-text-emphasis);
    font-weight: 600;
}

.conv-tok-filter-row {
    display: grid;
    grid-template-columns: 96px 1fr;
    align-items: center;
    gap: 8px;
    margin: 0;
    color: var(--theme-text-secondary);
    font-size: var(--theme-font-size-xs);
}

.conv-tok-filter-hint {
    color: var(--theme-text-muted);
    font-size: var(--theme-font-size-xs);
}

.conv-tok-filter-preview {
    padding: 6px 8px;
    border-radius: var(--theme-radius-button);
    background: var(--theme-background-subtle);
    color: var(--theme-text);
    font-family: var(--theme-font-mono);
    font-size: var(--theme-font-size-xs);
    overflow-wrap: anywhere;
}

.conv-tok-filter-actions {
    display: flex;
    justify-content: flex-end;
    gap: 6px;
}

.conv-tok-picker .conv-btn-sm {
    padding: 4px 10px;
    font-size: var(--theme-font-size-xs);
}

/* ── the Tokens panel ──────────────────────────────────────────────────── */

.conv-tok-panel-tools {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
}

.conv-tok-samples {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
}

.conv-tok-samples-label {
    color: var(--theme-text-secondary);
    font-size: var(--theme-font-size-xs);
}

.conv-tok-samples-kind {
    width: auto;
    flex: 1 1 120px;
}

.conv-tok-samples-key {
    flex: 1 1 140px;
    width: auto;
}

@media (prefers-reduced-motion: reduce) {
    .conv-tok-picker--inline,
    .conv-tok-picker--popup {
        animation: none;
    }

    .conv-tok-input,
    .conv-tok-tool,
    .conv-tok-remove,
    .conv-tokchip,
    .conv-tok-row {
        transition: none;
    }
}
