.ai-shell {
    --ai-toggle-size: 42px;
    --ai-shell-bottom-offset: calc(var(--itinerary-handle-size) + 72px);
    --ai-panel-open-width: min(400px, calc(100vw - var(--planner-handle-size) - var(--overlay-gap) - var(--ai-toggle-size) - 34px));
    position: absolute;
    right: calc(var(--overlay-gap) + 18px);
    bottom: var(--ai-shell-bottom-offset);
    z-index: 23;
    pointer-events: none;
    display: flex;
    flex-direction: row;
    align-items: flex-end;
    gap: 10px;
}

.ai-shell[hidden] {
    display: none !important;
}

.ai-toggle-button {
    width: var(--ai-toggle-size);
    min-width: var(--ai-toggle-size);
    height: var(--ai-toggle-size);
    padding: 0;
    pointer-events: auto;
    border: 1px solid rgba(255, 255, 255, 0.78);
    border-radius: 999px;
    background: rgba(255, 250, 242, 0.94);
    box-shadow: 0 10px 28px rgba(23, 49, 59, 0.14);
    color: var(--accent-strong);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: transform 120ms ease, background-color 120ms ease, box-shadow 120ms ease, color 120ms ease;
}

.ai-toggle-button:hover {
    background: rgba(255, 250, 242, 1);
    transform: translateY(-1px);
}

.ai-toggle-icon {
    width: 22px;
    height: 22px;
    overflow: visible;
}

.ai-toggle-icon path {
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.ai-toggle-icon text {
    fill: currentColor;
    font-family: var(--sans);
    font-size: 7px;
    font-weight: 800;
    letter-spacing: 0.02em;
}

body.using-keyboard-focus .ai-toggle-button:focus,
body.using-keyboard-focus .ai-close-button:focus,
body.using-keyboard-focus .ai-history-button:focus,
body.using-keyboard-focus .ai-follow-up-button:focus,
body.using-keyboard-focus .ai-route-details-toggle:focus,
body.using-keyboard-focus .ai-panel-submit:focus,
body.using-keyboard-focus .ai-clear-session:focus,
body.using-keyboard-focus .ai-prompt-input:focus {
    outline: 3px solid rgba(59, 141, 173, 0.35);
    outline-offset: 2px;
}

.ai-shell.is-open .ai-toggle-button {
    background: rgba(31, 111, 98, 0.16);
}

.ai-panel {
    width: 0;
    min-width: 0;
    height: 0;
    min-height: 0;
    max-width: var(--ai-panel-open-width);
    max-height: calc(100dvh - env(safe-area-inset-top, 0px) - var(--ai-shell-bottom-offset) - 12px);
    padding: 0;
    border: 0;
    border-radius: 20px;
    opacity: 0;
    overflow: hidden;
    pointer-events: none;
    transform: translateX(8px);
    transition: width 200ms ease, height 200ms ease, opacity 200ms ease, transform 200ms ease, padding 200ms ease, border-width 200ms ease;
    display: flex;
    flex-direction: column;
    gap: 12px;
    align-items: stretch;
}

.ai-shell.is-open .ai-panel {
    width: var(--ai-panel-open-width);
    height: min(720px, calc(100dvh - env(safe-area-inset-top, 0px) - var(--ai-shell-bottom-offset) - 12px));
    padding: 14px;
    border: 1px solid rgba(255, 255, 255, 0.82);
    opacity: 1;
    pointer-events: auto;
    transform: translateX(0);
    box-shadow: 0 18px 36px rgba(23, 49, 59, 0.2);
}

.ai-panel-header {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
}

.ai-close-button {
    flex: 0 0 auto;
}

.ai-transcript {
    flex: 1 1 auto;
    min-height: 0;
    overflow: auto;
    display: grid;
    align-content: start;
    gap: 10px;
    padding-right: 2px;
}

.ai-transcript:empty {
    display: none;
}

.ai-empty-state {
    margin: 0;
    padding: 2px 2px 0;
    color: var(--muted);
    font-size: 0.8rem;
    line-height: 1.45;
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
}

.ai-message {
    display: grid;
    gap: 8px;
    padding: 12px;
    border-radius: 16px;
    border: 1px solid rgba(23, 49, 59, 0.1);
    background: rgba(255, 255, 255, 0.8);
}

.ai-message[data-ai-role="user"] {
    background: rgba(59, 141, 173, 0.08);
    border-color: rgba(59, 141, 173, 0.18);
}

.ai-message[data-ai-error="true"] {
    background: rgba(212, 92, 44, 0.08);
    border-color: rgba(212, 92, 44, 0.16);
}

.ai-message-header {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 10px;
}

.ai-message-title {
    margin: 0;
    font-size: 0.84rem;
    font-weight: 800;
    line-height: 1.2;
}

.ai-message-body {
    margin: 0;
    font-size: 0.82rem;
    line-height: 1.45;
    color: var(--ink);
}

.ai-message-list {
    margin: 0;
    padding-left: 18px;
    display: grid;
    gap: 4px;
    font-size: 0.78rem;
    line-height: 1.4;
    color: var(--muted);
}

.ai-follow-up-list {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.ai-follow-up-button {
    min-height: 32px;
    padding: 7px 12px;
    border: 1px solid rgba(23, 49, 59, 0.08);
    border-radius: 999px;
    background: rgba(255, 250, 242, 0.92);
    color: var(--accent-strong);
    font-size: 0.76rem;
    font-weight: 700;
    line-height: 1.2;
}

.ai-route-summary {
    display: grid;
    gap: 0;
    padding: 12px;
    border-radius: 16px;
    background: rgba(31, 111, 98, 0.08);
}

.ai-route-summary[hidden] {
    display: none !important;
}

.ai-route-summary-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 10px;
}

.ai-route-summary-heading {
    min-width: 0;
    display: flex;
    align-items: baseline;
    gap: 10px;
    flex-wrap: wrap;
}

.ai-route-id {
    margin: 0;
    font-size: 0.8rem;
    font-weight: 800;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

.ai-route-details-toggle {
    padding: 0;
    font-size: 0.72rem;
    font-weight: 700;
    line-height: 1.35;
    white-space: nowrap;
}

.ai-route-badge {
    min-height: 24px;
    padding: 4px 8px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.85);
    color: var(--accent-strong);
    font-size: 0.72rem;
    font-weight: 800;
    white-space: nowrap;
}

.ai-route-badge[hidden] {
    display: none !important;
}

.ai-route-summary-body {
    display: grid;
    gap: 8px;
    overflow: hidden;
    max-height: 180px;
    margin-top: 8px;
    opacity: 1;
    transition: max-height 180ms ease, margin-top 180ms ease, opacity 180ms ease;
}

.ai-route-summary:not(.is-expanded) .ai-route-summary-body {
    max-height: 0;
    margin-top: 0;
    opacity: 0;
}

.ai-route-label,
.ai-route-based-on,
.ai-route-summary-text {
    margin: 0;
}

.ai-route-label {
    font-size: 0.88rem;
    font-weight: 700;
    line-height: 1.3;
}

.ai-route-based-on {
    color: var(--muted);
    font-size: 0.74rem;
    font-weight: 700;
    line-height: 1.35;
}

.ai-route-summary-text {
    font-size: 0.8rem;
    line-height: 1.45;
}

.ai-history-controls {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 10px;
    align-items: center;
}

.ai-history-button {
    min-width: 38px;
    height: 38px;
    padding: 0;
    justify-content: center;
}

.ai-history-button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.ai-history-status {
    min-width: 0;
    display: grid;
    gap: 2px;
    text-align: center;
}

.ai-history-label,
.ai-history-meta {
    margin: 0;
}

.ai-history-label {
    font-size: 0.84rem;
    font-weight: 800;
    line-height: 1.2;
}

.ai-history-meta {
    color: var(--muted);
    font-size: 0.72rem;
    line-height: 1.35;
}

.ai-panel-form {
    display: grid;
    gap: 8px;
    min-width: 0;
}

.ai-prompt-input {
    min-height: 58px;
    max-height: 116px;
    resize: vertical;
}

.ai-panel-hint {
    margin: 0;
    color: var(--muted);
    font-size: 0.72rem;
    line-height: 1.35;
}

.ai-prompt-meta {
    display: grid;
    grid-template-columns: minmax(0, 1fr) max-content;
    align-items: start;
    gap: 8px;
    min-width: 0;
}

.ai-prompt-counter,
.ai-prompt-limit-message {
    margin: 0;
    color: var(--muted);
    font-size: 0.72rem;
    line-height: 1.35;
}

.ai-prompt-counter {
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
}

.ai-prompt-counter.is-near-limit {
    color: #7a5a00;
}

.ai-prompt-counter.is-over-limit,
.ai-prompt-limit-message {
    color: #9b1c1c;
}

.ai-panel-actions {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: max-content;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    min-width: 0;
}

.ai-panel-submit,
.ai-clear-session {
    min-width: 0;
    width: auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    white-space: nowrap;
}

.ai-panel-submit.is-loading::before {
    content: "";
    width: 0.9rem;
    height: 0.9rem;
    border-radius: 999px;
    border: 2px solid rgba(31, 111, 98, 0.22);
    border-top-color: currentColor;
    animation: ai-submit-spinner 0.7s linear infinite;
}

.ai-panel-submit:disabled,
.ai-clear-session:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

@keyframes ai-submit-spinner {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}

@media (max-width: 900px) {
    .ai-shell {
        --ai-shell-bottom-offset: calc(var(--itinerary-handle-size) + 66px);
        right: calc(var(--overlay-gap) + 12px);
    }

    .ai-shell.is-open .ai-panel {
        width: min(360px, calc(100vw - var(--planner-handle-size) - var(--overlay-gap) - var(--ai-toggle-size) - 28px));
    }
}

.mobile-ui .ai-shell {
    --ai-toggle-size: 34px;
    --ai-shell-bottom-offset: calc(var(--itinerary-handle-size) + var(--mobile-bottom-safe-space) + 52px);
    --ai-panel-open-width: calc(100vw - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px) - var(--ai-toggle-size) - 26px);
    right: calc(var(--overlay-gap) + env(safe-area-inset-right, 0px) + 8px);
}

.mobile-ui .map-stage.itinerary-open .ai-shell {
    --ai-shell-bottom-offset: calc(var(--itinerary-height) + var(--mobile-bottom-safe-space) + 54px);
}

.mobile-ui .ai-toggle-button {
    font-size: 0.98rem;
}

.mobile-ui .ai-toggle-icon {
    width: 18px;
    height: 18px;
}

.mobile-ui .ai-toggle-icon text {
    font-size: 5.8px;
}

.mobile-ui .ai-panel {
    max-width: var(--ai-panel-open-width);
    max-height: calc(100dvh - env(safe-area-inset-top, 0px) - var(--ai-shell-bottom-offset) - 8px);
}

.mobile-ui .ai-shell.is-open .ai-panel {
    width: var(--ai-panel-open-width);
    height: calc(100dvh - env(safe-area-inset-top, 0px) - var(--ai-shell-bottom-offset) - 8px);
    padding: 12px;
}

.mobile-ui .ai-route-summary {
    padding: 10px;
}

.mobile-ui .ai-route-summary-heading {
    gap: 8px;
}

.mobile-ui .ai-route-id {
    font-size: 0.74rem;
}

.mobile-ui .ai-route-details-toggle,
.mobile-ui .ai-route-badge,
.mobile-ui .ai-history-meta,
.mobile-ui .ai-panel-hint {
    font-size: 0.68rem;
}

.mobile-ui .ai-history-label,
.mobile-ui .ai-route-label {
    font-size: 0.8rem;
}

.mobile-ui .ai-route-based-on,
.mobile-ui .ai-route-summary-text,
.mobile-ui .ai-message-body,
.mobile-ui .ai-message-list {
    font-size: 0.72rem;
    line-height: 1.38;
}

.mobile-ui .ai-message {
    padding: 10px;
}

.mobile-ui .ai-transcript {
    gap: 8px;
}

.mobile-ui .ai-prompt-input {
    min-height: 52px;
    max-height: 96px;
}

.mobile-ui .ai-panel-submit,
.mobile-ui .ai-clear-session {
    min-width: 0;
}
