/* ============================================================
   Assistente Legale AI - Chat UI
   Studio Legale Franchetto
   Vanilla CSS, no dependencies
   ============================================================ */

:root {
    /* Color tokens (4-shade gray palette) */
    --color-bg: #1A1A1A;           /* Background - darkest */
    --color-surface: #252525;       /* Elevated surfaces */
    --color-border: #3A3A3A;        /* Borders, dividers */
    --color-border-subtle: #2D2D2D; /* Subtle separators */

    /* Text colors (2 levels) */
    --color-text-primary: #FFFFFF;  /* Primary text */
    --color-text-muted: #9CA3AF;    /* Secondary text */

    /* Brand accent (orange) */
    --color-accent: #F47920;
    --color-accent-hover: #FF8B3D;
    --color-accent-muted: rgba(244, 121, 32, 0.15);

    /* Semantic colors */
    --color-success: #22C55E;
    --color-error: #EF4444;
    --color-warning: #F59E0B;

    /* Focus states (2px solid orange) */
    --focus-ring-width: 2px;
    --focus-ring-color: var(--color-accent);
    --focus-ring-offset: 2px;

    /* Interactive states */
    --hover-lighten: rgba(255, 255, 255, 0.05);
    --disabled-opacity: 0.5;
    --active-darken: rgba(0, 0, 0, 0.1);

    /* Typography (system fonts) */
    --font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    --font-mono: 'SF Mono', 'Fira Code', 'Fira Mono', Menlo, Consolas, monospace;
    --font-size-small: 12px;
    --font-size-base: 16px;
    --font-size-heading: 20px;
    --font-size-title: 24px;
    --font-weight-regular: 400;
    --font-weight-bold: 700;
    --line-height-normal: 1.5;
    --line-height-relaxed: 1.7;

    /* Spacing */
    --space-xs: 4px;
    --space-sm: 8px;
    --space-md: 16px;
    --space-lg: 24px;
    --space-xl: 32px;

    /* Transitions */
    --transition-fast: 150ms ease;
    --transition-normal: 250ms ease;

    /* Layout */
    --sidebar-width: 280px;
    --content-max-width: 800px;
    --header-height: 56px;

    /* Radius */
    --radius-sm: 6px;
    --radius-md: 12px;
    --radius-lg: 20px;
    --radius-full: 9999px;
}

/* Reset */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* Firefox scrollbar */
* {
    scrollbar-width: thin;
    scrollbar-color: var(--color-border) var(--color-bg);
}

html {
    height: 100%;
    color-scheme: dark; /* Tells browser this is a dark theme */
}

body {
    font-family: var(--font-family);
    font-size: var(--font-size-base);
    line-height: var(--line-height-normal);
    color: var(--color-text-primary);
    background-color: var(--color-bg);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    height: 100%;
}

/* Scrollbar - Webkit */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: var(--color-bg);
}

::-webkit-scrollbar-thumb {
    background: var(--color-border);
    border-radius: var(--radius-full);
}

::-webkit-scrollbar-thumb:hover {
    background: var(--color-text-muted);
}

/* ============================================================
   Focus States - Keyboard Navigation (WCAG 2.4.7)
   ============================================================ */

/* Remove default focus outline for mouse users */
*:focus {
    outline: none;
}

/* Show focus ring ONLY for keyboard navigation */
*:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
}

/* Buttons */
button:focus-visible,
.btn:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
}

/* Form inputs - use box-shadow instead of outline for better aesthetics */
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
    outline: none;
    border-color: var(--color-accent);
    box-shadow: 0 0 0 3px var(--color-accent-muted);
}

/* Links - slightly larger offset for inline text readability */
a:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: 3px;
    border-radius: 2px;
}

/* Sidebar items and interactive surfaces */
.sidebar-nav a:focus-visible,
.interactive-surface:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: -2px; /* Inset for contained elements */
}

/* Skip link for accessibility */
.skip-link {
    position: absolute;
    top: -100%;
    left: var(--space-md);
    padding: var(--space-sm) var(--space-md);
    background: var(--color-accent);
    color: var(--color-bg);
    font-weight: var(--font-weight-bold);
    border-radius: var(--radius-sm);
    z-index: 100;
    transition: top var(--transition-fast);
}

.skip-link:focus {
    top: var(--space-md);
}

/* ============================================================
   Interactive States
   ============================================================ */

/* Hover: subtle background lighten (LOCKED: per CONTEXT.md) */
.interactive-surface:hover {
    background: var(--hover-lighten);
}

/* Base button interactive states */
.btn,
button {
    transition: background-color var(--transition-fast),
                transform var(--transition-fast),
                opacity var(--transition-fast);
}

/* Disabled: 50% opacity (LOCKED: per CONTEXT.md) */
.btn:disabled,
button:disabled,
input:disabled,
textarea:disabled,
select:disabled {
    opacity: var(--disabled-opacity);
    cursor: not-allowed;
    pointer-events: none;
}

/* Active/pressed: button darkens slightly (LOCKED: per CONTEXT.md) */
.btn:active:not(:disabled),
button:active:not(:disabled) {
    transform: scale(0.98);
}

/* Primary button (accent color) */
.btn-primary {
    background: var(--color-accent);
    color: var(--color-text-primary);
    border: none;
    padding: var(--space-sm) var(--space-md);
    border-radius: var(--radius-md);
    cursor: pointer;
}

.btn-primary:hover:not(:disabled) {
    background: var(--color-accent-hover);
}

.btn-primary:active:not(:disabled) {
    background: color-mix(in srgb, var(--color-accent) 85%, black);
}

/* Secondary button (ghost style) */
.btn-secondary {
    background: transparent;
    color: var(--color-text-primary);
    border: 1px solid var(--color-border);
    padding: var(--space-sm) var(--space-md);
    border-radius: var(--radius-md);
    cursor: pointer;
}

.btn-secondary:hover:not(:disabled) {
    background: var(--hover-lighten);
    border-color: var(--color-text-muted);
}

/* Icon button (square, no text) */
.btn-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--color-text-primary);
    cursor: pointer;
}

.btn-icon:hover:not(:disabled) {
    background: var(--hover-lighten);
}

/* ============================================================
   App Layout - Grid Structure
   ============================================================ */

.app-layout {
    display: grid;
    grid-template-rows: var(--header-height) 1fr auto;
    grid-template-columns: 1fr;
    grid-template-areas:
        "header"
        "main"
        "footer";
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height for mobile */
    background: var(--color-bg);
}

/* Sidebar - Hidden by default on mobile, overlay drawer pattern */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-width);
    height: 100vh;
    height: 100dvh;
    background: var(--color-surface);
    border-right: 1px solid var(--color-border);
    transform: translateX(-100%);
    transition: transform var(--transition-normal);
    z-index: 50;
    overflow-y: auto;
}

.sidebar.is-open {
    transform: translateX(0);
}

/* Overlay backdrop when sidebar open (mobile) */
.sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--transition-normal), visibility var(--transition-normal);
    z-index: 40;
}

.sidebar-overlay.is-visible {
    opacity: 1;
    visibility: visible;
}

/* Sidebar header */
.sidebar-header {
    padding: var(--space-lg);
    border-bottom: 1px solid var(--color-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-sm);
}

.sidebar-title {
    font-size: var(--font-size-heading);
    font-weight: var(--font-weight-bold);
    color: var(--color-text-primary);
}

.sidebar-user-info {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.sidebar-user-name {
    font-size: var(--font-size-body);
    font-weight: var(--font-weight-bold);
    color: var(--color-text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-admin-link {
    font-size: var(--font-size-small);
    color: var(--color-accent);
    text-decoration: none;
    opacity: 0.8;
    transition: opacity 0.15s ease;
}

.sidebar-admin-link:hover {
    opacity: 1;
    text-decoration: underline;
}

.sidebar-logout-btn {
    flex-shrink: 0;
}

/* ============================================================
   Phase 4: Sidebar Conversation History
   ============================================================ */

/* Sidebar nav layout */
.sidebar-nav {
    display: flex;
    flex-direction: column;
    height: calc(100% - 73px); /* Subtract header height */
    overflow: hidden;
}

/* Search wrapper and input */
.sidebar-search-wrapper {
    padding: var(--space-md);
    border-bottom: 1px solid var(--color-border);
    flex-shrink: 0;
}

.sidebar-search {
    width: 100%;
    padding: var(--space-sm) var(--space-md);
    background: var(--color-bg);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    color: var(--color-text-primary);
    font-family: var(--font-family);
    font-size: var(--font-size-base);
    transition: border-color var(--transition-fast);
}

.sidebar-search::placeholder {
    color: var(--color-text-muted);
}

.sidebar-search:focus {
    outline: none;
    border-color: var(--color-accent);
    box-shadow: 0 0 0 3px var(--color-accent-muted);
}

/* Conversation list container */
.conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-sm) 0;
}

.conversation-list__empty {
    display: block;
    padding: var(--space-lg) var(--space-md);
    color: var(--color-text-muted);
    font-size: var(--font-size-small);
    text-align: center;
}

/* Date group headers */
.conversation-group-header {
    padding: var(--space-xs) var(--space-md);
    font-size: var(--font-size-small);
    font-weight: var(--font-weight-bold);
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-top: var(--space-sm);
}

.conversation-group-header:first-child {
    margin-top: 0;
}

/* Conversation items */
.conversation-item {
    display: flex;
    flex-direction: column;
    gap: 2px;
    width: 100%;
    padding: var(--space-sm) var(--space-md);
    background: transparent;
    border: none;
    text-align: left;
    color: var(--color-text-primary);
    cursor: pointer;
    transition: background-color var(--transition-fast);
    font-family: var(--font-family);
}

.conversation-item:hover {
    background: var(--hover-lighten);
}

.conversation-item:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: -2px;
}

.conversation-item--active {
    background: var(--color-accent-muted);
    border-left: 3px solid var(--color-accent);
}

.conversation-item__title {
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-regular);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conversation-item__time {
    font-size: var(--font-size-small);
    color: var(--color-text-muted);
}

/* New chat button */
.new-chat-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    margin: var(--space-md);
    margin-left: auto;
    background: var(--color-accent);
    border: none;
    border-radius: var(--radius-full);
    color: var(--color-text-primary);
    cursor: pointer;
    flex-shrink: 0;
    transition: background-color var(--transition-fast), transform var(--transition-fast);
}

.new-chat-btn:hover {
    background: var(--color-accent-hover);
}

.new-chat-btn:active {
    transform: scale(0.95);
}

.new-chat-btn:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
}

.new-chat-btn svg {
    width: 20px;
    height: 20px;
}

/* Loading spinner for conversation switch */
.conversation-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}

/* Header in grid */
.app-header {
    grid-area: header;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 var(--space-lg);
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
    height: var(--header-height);
}

/* Header brand */
.header-brand {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.brand-logo {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--color-accent);
}

.brand-name {
    font-size: var(--font-size-heading);
    font-weight: var(--font-weight-bold);
    color: var(--color-text-primary);
}

.app-header .header-left {
    display: flex;
    align-items: center;
    gap: var(--space-md);
}

.header-right {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

/* Privacy link in header */
.privacy-link {
    font-size: var(--font-size-small);
    color: var(--color-text-muted);
    text-decoration: none;
    padding: var(--space-xs) var(--space-sm);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
}

.privacy-link:hover,
.privacy-link:focus-visible {
    color: var(--color-text-primary);
    border-color: var(--color-accent);
    background: var(--hover-lighten);
}

/* Main content area */
.app-main {
    grid-area: main;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Messages container - centered with max-width */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-lg);
}

.messages-wrapper {
    max-width: var(--content-max-width);
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
}

/* Input area - fixed at bottom */
.app-footer {
    grid-area: footer;
    background: var(--color-bg);
    border-top: 1px solid var(--color-border);
    padding: var(--space-md) var(--space-lg);
}

.input-wrapper {
    max-width: var(--content-max-width);
    margin: 0 auto;
}

/* Hamburger button (mobile only) */
.hamburger-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    background: transparent;
    border: none;
    color: var(--color-text-primary);
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: background-color var(--transition-fast);
}

.hamburger-btn:hover,
.hamburger-btn:focus-visible {
    background: var(--hover-lighten);
}

/* Hamburger icon (3 lines) */
.hamburger-icon {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.hamburger-icon span {
    display: block;
    width: 20px;
    height: 2px;
    background: currentColor;
    border-radius: 1px;
}

/* ============================================================
   Responsive - Tablet/Desktop (768px+)
   ============================================================ */

@media (min-width: 768px) {
    .app-layout {
        grid-template-columns: var(--sidebar-width) 1fr;
        grid-template-areas:
            "sidebar header"
            "sidebar main"
            "sidebar footer";
    }

    .sidebar {
        position: relative;
        transform: none;
        grid-area: sidebar;
    }

    .sidebar-overlay {
        display: none;
    }

    /* Sidebar collapsed on desktop */
    .app-layout.sidebar-collapsed {
        grid-template-columns: 1fr;
        grid-template-areas:
            "header"
            "main"
            "footer";
    }

    .app-layout.sidebar-collapsed .sidebar {
        position: fixed;
        transform: translateX(-100%);
    }
}

@media (min-width: 1024px) {
    /* No changes from tablet - sidebar visible, just more space */
    .messages-container {
        padding: var(--space-xl);
    }
}

/* Privacy Modal */
.privacy-modal[hidden] {
    display: none;
}

.privacy-modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.privacy-modal-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
}

.privacy-modal-content {
    position: relative;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    padding: var(--space-lg);
    max-width: 560px;
    width: 100%;
    max-height: 80vh;
    overflow-y: auto;
}

.privacy-modal-content h2 {
    font-size: var(--font-size-heading);
    color: var(--color-text-primary);
    margin-bottom: var(--space-md);
}

.privacy-modal-content h3 {
    font-size: 14px;
    font-weight: var(--font-weight-bold);
    color: var(--color-text-primary);
    margin-top: var(--space-md);
    margin-bottom: var(--space-sm);
}

.privacy-body p {
    font-size: 13px;
    color: var(--color-text-muted);
    margin-bottom: var(--space-sm);
}

.privacy-close {
    display: block;
    margin: var(--space-lg) auto 0;
    padding: var(--space-sm) var(--space-lg);
    border: none;
    border-radius: var(--radius-sm);
    background: var(--color-accent);
    color: var(--color-text-primary);
    font-size: 14px;
    cursor: pointer;
    transition: background var(--transition-fast);
}

.privacy-close:hover,
.privacy-close:focus-visible {
    background: var(--color-accent-hover);
}

/* ============================================================
   Reduced Motion - Accessibility (WCAG 2.3.3)
   ============================================================ */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }

    .tw-word { opacity: 1 !important; }
    .tw-hidden { opacity: 1 !important; transition: none; }
}

/* Safe animations - only run when user hasn't requested reduced motion */
@media (prefers-reduced-motion: no-preference) {
    /* Message fade-in animation (snappy 200ms) */
    .message {
        animation: fadeIn 200ms ease-out;
    }

    @keyframes fadeIn {
        from {
            opacity: 0;
            transform: translateY(6px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }
}

/* ============================================================
   New Chat Message System (Phase 3)
   ============================================================ */

/* Message base */
.message--user,
.message--ai {
    display: flex;
    gap: var(--space-sm);
    max-width: 85%;
}

/* User messages: right-aligned, no avatar */
.message--user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

/* AI messages: left-aligned, with avatar */
.message--ai {
    align-self: flex-start;
}

/* AI Avatar */
.message__avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--color-accent);
    color: var(--color-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: var(--font-weight-bold);
    flex-shrink: 0;
}

/* Message content bubble */
.message__content {
    padding: var(--space-sm) var(--space-md);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    font-size: 14px;
    line-height: var(--line-height-relaxed);
    word-break: break-word;
}

/* Typing indicator */
.message--typing .message__content {
    padding: var(--space-sm) var(--space-md);
}

.typing-dots {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: var(--space-xs) 0;
}

.typing-dots span {
    width: 8px;
    height: 8px;
    background: var(--color-accent);
    border-radius: 50%;
    opacity: 0.7;
}

@media (prefers-reduced-motion: no-preference) {
    .typing-dots span {
        animation: typingBounce 1.4s infinite ease-in-out;
    }

    @keyframes typingBounce {
        0%, 80%, 100% {
            transform: translateY(0);
            opacity: 0.4;
        }
        40% {
            transform: translateY(-6px);
            opacity: 1;
        }
    }

    .typing-dots span:nth-child(1) { animation-delay: 0s; }
    .typing-dots span:nth-child(2) { animation-delay: 0.15s; }
    .typing-dots span:nth-child(3) { animation-delay: 0.3s; }
}

/* Code blocks */
.code-block {
    margin: var(--space-sm) 0;
    border-radius: var(--radius-sm);
    overflow: hidden;
    background: var(--color-bg);
    border: 1px solid var(--color-border);
}

.code-block__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-xs) var(--space-sm);
    background: rgba(0, 0, 0, 0.2);
    border-bottom: 1px solid var(--color-border);
}

.code-block__language {
    font-size: var(--font-size-small);
    color: var(--color-text-muted);
    text-transform: lowercase;
}

.code-block__copy {
    font-size: var(--font-size-small);
    padding: var(--space-xs) var(--space-sm);
    background: transparent;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-text-muted);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.code-block__copy:hover {
    background: var(--hover-lighten);
    color: var(--color-text-primary);
    border-color: var(--color-text-muted);
}

.code-block__copy.copied {
    color: var(--color-success);
    border-color: var(--color-success);
}

.code-block pre {
    margin: 0;
    padding: var(--space-md);
    overflow-x: auto;
    background: transparent;
}

.code-block code {
    font-family: var(--font-mono);
    font-size: 13px;
    line-height: 1.5;
}

/* Inline code in messages */
.message__content code:not(.hljs) {
    font-family: var(--font-mono);
    font-size: 13px;
    background: var(--color-bg);
    padding: 2px 6px;
    border-radius: 3px;
    border: 1px solid var(--color-border-subtle);
}

/* Markdown content styling */
.message__content p {
    margin-bottom: var(--space-sm);
}

.message__content p:last-child {
    margin-bottom: 0;
}

.message__content ul,
.message__content ol {
    margin: var(--space-sm) 0;
    padding-left: var(--space-lg);
}

.message__content li {
    margin-bottom: var(--space-xs);
}

.message__content strong {
    font-weight: var(--font-weight-bold);
}

.message__content em {
    font-style: italic;
}

/* Error message */
.message--error .message__content {
    background: rgba(239, 68, 68, 0.1);
    border-color: var(--color-error);
    color: var(--color-error);
}

/* Chat input area */
.chat-input-area {
    display: flex;
    align-items: flex-end;
    gap: var(--space-sm);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: var(--space-sm);
}

.chat-textarea {
    flex: 1;
    min-height: 24px;
    max-height: none; /* Unlimited growth per CONTEXT.md */
    padding: var(--space-xs) var(--space-sm);
    background: transparent;
    border: none;
    color: var(--color-text-primary);
    font-family: var(--font-family);
    font-size: var(--font-size-base);
    line-height: var(--line-height-normal);
    resize: none;
    overflow-y: hidden;
}

.chat-textarea::placeholder {
    color: var(--color-text-muted);
}

.chat-textarea:focus {
    outline: none;
}

/* Focus state on container instead */
.chat-input-area:focus-within {
    border-color: var(--color-accent);
    box-shadow: 0 0 0 3px var(--color-accent-muted);
}

/* Send button - icon only */
.chat-send-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    padding: 0;
    background: var(--color-accent);
    border: none;
    border-radius: var(--radius-full);
    color: var(--color-text-primary);
    cursor: pointer;
    flex-shrink: 0;
    transition: background-color var(--transition-fast), transform var(--transition-fast);
}

.chat-send-btn:hover:not(:disabled) {
    background: var(--color-accent-hover);
}

.chat-send-btn:active:not(:disabled) {
    transform: scale(0.95);
}

.chat-send-btn:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
}

.chat-send-btn:disabled {
    opacity: var(--disabled-opacity);
    cursor: not-allowed;
}

.chat-send-btn svg {
    width: 18px;
    height: 18px;
}

/* ============================================================
   Login Page Styles
   ============================================================ */

/* Login Page Layout */
.login-page {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    min-height: 100dvh;
    background: var(--color-bg);
    padding: var(--space-lg);
    transition: opacity 400ms ease;
}

.login-page.fade-out {
    opacity: 0;
}

.login-page[hidden] {
    display: none;
}

/* Login Logo */
.login-logo {
    margin-bottom: var(--space-xl);
    font-size: var(--font-size-title);
    font-weight: var(--font-weight-bold);
    opacity: 0; /* Initial hidden state for animation */
}

.login-logo__ictlc {
    color: var(--color-accent);
}

.login-logo__factory {
    color: var(--color-text-primary);
}

/* Login Card */
.login-card {
    width: 100%;
    max-width: 400px;
    padding: var(--space-xl);
    background: linear-gradient(145deg, rgba(37,37,37,1) 0%, rgba(30,30,30,0.95) 100%);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    opacity: 0; /* Initial hidden state for animation */
}

/* Login Typography */
.login-title {
    font-size: var(--font-size-title);
    font-weight: var(--font-weight-bold);
    color: var(--color-text-primary);
    text-align: center;
    margin-bottom: var(--space-xs);
}

.login-tagline {
    font-size: var(--font-size-base);
    color: var(--color-text-muted);
    text-align: center;
    margin-bottom: var(--space-lg);
}

/* Login Error Banner */
.login-error {
    background: rgba(239, 68, 68, 0.1);
    border: 1px solid var(--color-error);
    border-radius: var(--radius-sm);
    padding: var(--space-sm) var(--space-md);
    margin-bottom: var(--space-md);
}

.login-error[hidden] {
    display: none;
}

.login-error__text {
    color: var(--color-error);
    font-size: var(--font-size-base);
    margin: 0;
}

/* Login Form */
.login-form {
    display: flex;
    flex-direction: column;
    gap: var(--space-md);
}

.form-field {
    display: flex;
    flex-direction: column;
    gap: var(--space-xs);
}

.form-label {
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-bold);
    color: var(--color-text-primary);
}

.form-input {
    padding: var(--space-sm) var(--space-md);
    background: var(--color-bg);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    color: var(--color-text-primary);
    font-family: var(--font-family);
    font-size: var(--font-size-base);
    transition: border-color var(--transition-fast);
}

.form-input::placeholder {
    color: var(--color-text-muted);
}

.form-input:focus {
    outline: none;
    border-color: var(--color-accent);
    box-shadow: 0 0 0 3px var(--color-accent-muted);
}

/* Login Button */
.login-btn {
    position: relative;
    width: 100%;
    padding: var(--space-sm) var(--space-md);
    margin-top: var(--space-sm);
    min-height: 44px;
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-bold);
    cursor: pointer;
}

.login-btn__text {
    transition: opacity var(--transition-fast);
}

.login-btn--loading .login-btn__text {
    opacity: 0;
}

.login-btn--loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin-top: -10px;
    margin-left: -10px;
    border: 2px solid transparent;
    border-top-color: var(--color-text-primary);
    border-radius: 50%;
}

/* ============================================================
   Login Page Animations (respect reduced motion)
   ============================================================ */

@media (prefers-reduced-motion: no-preference) {
    /* Slide-down fade animation */
    @keyframes slideDownFade {
        from {
            opacity: 0;
            transform: translateY(-20px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    .login-logo.animate-in {
        animation: slideDownFade 300ms ease forwards;
        opacity: 1; /* Maintain opacity after animation completes */
    }

    .login-card.animate-in {
        animation: slideDownFade 300ms ease forwards;
        opacity: 1; /* Maintain opacity even when shake animation runs */
    }

    /* Shake animation for invalid credentials */
    @keyframes shake {
        0%, 100% {
            transform: translateX(0);
        }
        10%, 30%, 50%, 70%, 90% {
            transform: translateX(-6px);
        }
        20%, 40%, 60%, 80% {
            transform: translateX(6px);
        }
    }

    .login-card.shake {
        animation: shake 400ms ease;
    }

    /* Spinner animation */
    @keyframes spin {
        to {
            transform: rotate(360deg);
        }
    }

    .login-btn--loading::after {
        animation: spin 800ms linear infinite;
    }
}

/* Reduced motion fallback */
@media (prefers-reduced-motion: reduce) {
    .login-logo.animate-in,
    .login-card.animate-in {
        opacity: 1;
    }

    .login-page {
        transition: none;
    }
}

/* ============================================================
   Phase 5: Toast Notifications
   ============================================================ */

.toast-container {
    position: fixed;
    top: var(--space-lg);
    right: var(--space-lg);
    z-index: 100;
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    pointer-events: none;
}

.toast {
    padding: var(--space-sm) var(--space-md);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    color: var(--color-text-primary);
    font-size: var(--font-size-base);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    pointer-events: auto;
    max-width: 360px;

    /* Initial state - hidden */
    opacity: 0;
    transform: translateX(100%);
    transition: opacity 300ms ease-out, transform 300ms ease-out;
}

.toast--visible {
    opacity: 1;
    transform: translateX(0);
}

/* Error type */
.toast--error {
    border-color: var(--color-error);
    background: rgba(239, 68, 68, 0.1);
}

/* Success type (for future use) */
.toast--success {
    border-color: var(--color-success);
    background: rgba(34, 197, 94, 0.1);
}

/* Reduced motion for toast */
@media (prefers-reduced-motion: reduce) {
    .toast {
        transition: none;
        transform: none;
    }
}

/* Toast mobile adjustments */
@media (max-width: 600px) {
    .toast-container {
        top: var(--space-md);
        right: var(--space-md);
        left: var(--space-md);
    }

    .toast {
        max-width: none;
    }
}

/* ============================================================
   Phase 5: Message Feedback Buttons
   ============================================================ */

.message-feedback {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    margin-top: var(--space-sm);
}

.message-feedback__btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--color-text-muted);
    cursor: pointer;
    transition: color var(--transition-fast), background-color var(--transition-fast);
}

.message-feedback__btn svg {
    width: 16px;
    height: 16px;
}

.message-feedback__btn:hover {
    color: var(--color-text-primary);
    background: var(--hover-lighten);
}

.message-feedback__btn:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: 2px;
}

.message-feedback__btn.selected {
    color: var(--color-accent);
}

/* Animation on click */
@media (prefers-reduced-motion: no-preference) {
    .message-feedback__btn.animate {
        animation: feedbackPop 200ms ease-out;
    }

    @keyframes feedbackPop {
        0% { transform: scale(1); }
        50% { transform: scale(1.15); }
        100% { transform: scale(1); }
    }
}

/* ============================================================
   Phase 8: Confirmation Modal
   ============================================================ */

.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.6);
    padding: var(--space-lg);
}

@media (prefers-reduced-motion: no-preference) {
    .modal-overlay {
        animation: modalFadeIn 150ms ease-out;
    }

    @keyframes modalFadeIn {
        from { opacity: 0; }
        to { opacity: 1; }
    }
}

.modal-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    padding: var(--space-lg);
    max-width: 400px;
    width: 100%;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

.modal-title {
    font-size: var(--font-size-heading);
    font-weight: var(--font-weight-bold);
    color: var(--color-text-primary);
    margin-bottom: var(--space-sm);
}

.modal-message {
    font-size: var(--font-size-base);
    color: var(--color-text-muted);
    line-height: var(--line-height-normal);
    margin-bottom: var(--space-lg);
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-sm);
}

/* Danger button (destructive actions) */
.btn-danger {
    background: var(--color-error);
    color: var(--color-text-primary);
    border: none;
    padding: var(--space-sm) var(--space-md);
    border-radius: var(--radius-md);
    cursor: pointer;
    font-family: var(--font-family);
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-bold);
    transition: background-color var(--transition-fast);
}

.btn-danger:hover:not(:disabled) {
    background: #DC2626;
}

.btn-danger:focus-visible {
    outline: var(--focus-ring-width) solid var(--color-error);
    outline-offset: var(--focus-ring-offset);
}

/* ============================================================
   Phase 8: Conversation Item - Delete & Rename
   ============================================================ */

/* Row wrapper for title + delete side by side */
.conversation-item__row {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    width: 100%;
}

.conversation-item__row .conversation-item__title {
    flex: 1;
    min-width: 0;
}

/* Delete button - hidden by default, shown on hover */
.conversation-item__delete {
    display: none;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 22px;
    height: 22px;
    border-radius: var(--radius-sm);
    color: var(--color-text-muted);
    cursor: pointer;
    transition: color var(--transition-fast), background-color var(--transition-fast);
}

.conversation-item:hover .conversation-item__delete {
    display: flex;
}

.conversation-item__delete:hover {
    color: var(--color-error);
    background: rgba(239, 68, 68, 0.15);
}

/* Inline rename input */
.conversation-item__edit {
    flex: 1;
    min-width: 0;
    padding: 2px var(--space-xs);
    background: var(--color-bg);
    border: 1px solid var(--color-accent);
    border-radius: var(--radius-sm);
    color: var(--color-text-primary);
    font-family: var(--font-family);
    font-size: var(--font-size-base);
    outline: none;
    box-shadow: 0 0 0 3px var(--color-accent-muted);
}

/* ============================================================
   Phase 8: Migration Banner
   ============================================================ */

.migration-banner {
    padding: var(--space-md);
    margin: var(--space-sm) var(--space-md);
    background: var(--color-accent-muted);
    border: 1px solid var(--color-accent);
    border-radius: var(--radius-md);
}

.migration-banner__text {
    font-size: var(--font-size-small);
    color: var(--color-text-primary);
    line-height: var(--line-height-normal);
    margin-bottom: var(--space-sm);
}

.migration-banner__actions {
    display: flex;
    gap: var(--space-sm);
}

.migration-banner__actions .btn-secondary,
.migration-banner__actions .btn-primary {
    font-size: var(--font-size-small);
    padding: var(--space-xs) var(--space-sm);
}

/* ============================================================
   Phase 9: RAG Quality - Confidence, Citations, Source Panel
   ============================================================ */

/* Confidence Indicator - appears at top of AI message */
.confidence-indicator {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    border-radius: 12px;
    font-size: var(--font-size-small);
    font-weight: 500;
    margin-bottom: 8px;
    background: rgba(255, 255, 255, 0.05);
}
.confidence-indicator svg {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
}
.confidence-indicator--alta {
    color: var(--color-success);
}
.confidence-indicator--media {
    color: var(--color-warning);
}
.confidence-indicator--bassa {
    color: var(--color-error);
}
.confidence-indicator--nd {
    color: var(--color-text-muted);
}

/* Citation Badges - inline clickable pills */
.citation-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    height: 20px;
    padding: 0 5px;
    margin: 0 1px;
    border-radius: 10px;
    background: var(--color-accent-muted);
    color: var(--color-accent);
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    border: 1px solid transparent;
    transition: background 0.15s, border-color 0.15s;
    vertical-align: super;
    line-height: 1;
    font-family: var(--font-family);
}
.citation-badge:hover {
    background: rgba(244, 121, 32, 0.25);
    border-color: var(--color-accent);
}
.citation-badge:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
}

/* Source Panel - collapsible section below AI message */
.source-panel {
    margin-top: 12px;
    border: 1px solid var(--color-border-subtle);
    border-radius: 8px;
    overflow: hidden;
}
.source-panel__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;
    background: rgba(255, 255, 255, 0.03);
    cursor: pointer;
    font-size: var(--font-size-small);
    color: var(--color-text-muted);
    user-select: none;
    border: none;
    width: 100%;
    text-align: left;
    font-family: var(--font-family);
}
.source-panel__header:hover {
    background: var(--hover-lighten);
}
.source-panel__header-text {
    font-weight: 500;
}
.source-panel__chevron {
    transition: transform 0.2s ease;
    width: 16px;
    height: 16px;
}
.source-panel.expanded .source-panel__chevron {
    transform: rotate(180deg);
}
.source-panel__list {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease;
}
.source-panel.expanded .source-panel__list {
    max-height: 1000px;
}
.source-panel__empty {
    padding: 12px;
    font-size: var(--font-size-small);
    color: var(--color-text-muted);
    font-style: italic;
}
.source-panel__show-more {
    display: block;
    width: 100%;
    padding: 8px;
    background: none;
    border: none;
    border-top: 1px solid var(--color-border-subtle);
    color: var(--color-accent);
    font-size: var(--font-size-small);
    cursor: pointer;
    font-family: var(--font-family);
}
.source-panel__show-more:hover {
    background: var(--hover-lighten);
}

/* Source Card - individual source in the panel */
.source-card {
    border-top: 1px solid var(--color-border-subtle);
    padding: 10px 12px;
}
.source-card:first-child {
    border-top: none;
}
.source-card__header {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    cursor: pointer;
}
.source-card__number {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    background: var(--color-accent-muted);
    color: var(--color-accent);
    font-size: 11px;
    font-weight: 600;
    flex-shrink: 0;
}
.source-card__title {
    font-size: 13px;
    color: var(--color-text-primary);
    font-weight: 500;
    line-height: 1.4;
}
.source-card__section {
    font-size: var(--font-size-small);
    color: var(--color-text-muted);
    margin-top: 2px;
}
.source-card__excerpt {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.2s ease, padding 0.2s ease;
    font-size: var(--font-size-small);
    color: var(--color-text-muted);
    line-height: var(--line-height-relaxed);
    padding: 0 0 0 28px;
}
.source-card.expanded .source-card__excerpt {
    max-height: 200px;
    padding: 6px 0 0 28px;
}
.source-card__show-text {
    display: block;
    margin-top: 4px;
    margin-left: 28px;
    background: none;
    border: none;
    color: var(--color-accent);
    font-size: 11px;
    cursor: pointer;
    padding: 0;
    font-family: var(--font-family);
}
.source-card__show-text:hover {
    text-decoration: underline;
}

/* Reduced motion preference for Phase 9 elements */
@media (prefers-reduced-motion: reduce) {
    .source-panel__list,
    .source-card__excerpt,
    .source-panel__chevron,
    .citation-badge {
        transition: none;
    }
}

/* ============================================================
   Phase 10: Response UX - Search Mode, Typewriter, Mode Labels
   ============================================================ */

/* Mode Dropdown (Gemini-style) */
.mode-dropdown {
    position: relative;
    flex-shrink: 0;
}
.mode-dropdown__trigger {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 6px 10px;
    background: var(--color-bg);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-full);
    color: var(--color-text-muted);
    font-family: var(--font-family);
    font-size: var(--font-size-small);
    cursor: pointer;
    transition: border-color var(--transition-fast), color var(--transition-fast);
    white-space: nowrap;
}
.mode-dropdown__trigger:hover {
    border-color: var(--color-text-muted);
    color: var(--color-text-primary);
}
.mode-dropdown__trigger svg {
    transition: transform var(--transition-fast);
}
.mode-dropdown.open .mode-dropdown__trigger svg {
    transform: rotate(180deg);
}
.mode-dropdown__menu {
    position: absolute;
    bottom: calc(100% + 8px);
    right: 0;
    min-width: 220px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    padding: var(--space-xs) 0;
    box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.3);
    z-index: 10;
}
.mode-dropdown__menu[hidden] {
    display: none;
}
.mode-dropdown__option {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 10px 14px;
    background: none;
    border: none;
    color: var(--color-text-primary);
    cursor: pointer;
    font-family: var(--font-family);
    text-align: left;
    transition: background var(--transition-fast);
}
.mode-dropdown__option:hover {
    background: var(--hover-lighten);
}
.mode-dropdown__option-info {
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.mode-dropdown__option-name {
    font-size: 14px;
    font-weight: 500;
}
.mode-dropdown__option-desc {
    font-size: var(--font-size-small);
    color: var(--color-text-muted);
}
.mode-dropdown__check {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
    color: var(--color-accent);
}

/* Typewriter Animation */
.tw-word {
    opacity: 0;
    transition: none;
}
.tw-word.tw-visible {
    opacity: 1;
}
.tw-hidden {
    opacity: 0;
    transition: opacity 0.3s ease;
}
.tw-hidden.tw-revealed {
    opacity: 1;
}

/* Mode Label */
.mode-label {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 10px;
    margin-bottom: 6px;
    background: rgba(255, 255, 255, 0.05);
    color: var(--color-text-muted);
    font-weight: 500;
}
.mode-label--veloce {
    color: var(--color-warning);
}
.mode-label--approfondita {
    color: var(--color-accent);
}
.mode-label svg {
    width: 12px;
    height: 12px;
    flex-shrink: 0;
}
