/* CSS Reset ja yleiset tyylit */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* Ensure full-height layout for iframe embedding and prevent double scrolling */
html, body {
    height: 100%;
}

:root {
    /* Värit */
    --primary-color: #821e14;
    --accent-color: #FFC107;
    --danger-color: #F44336;
    --bg-color: #fafafa;
    --text-color: #333;
    --text-light: #666;
    --border-color: #e0e0e0;
    --shadow-light: 0 2px 8px rgba(0,0,0,0.1);
    --shadow-medium: 0 4px 16px rgba(0,0,0,0.15);
    
    /* Spacing */
    --spacing-xs: 4px;
    --spacing-sm: 8px;
    --spacing-md: 12px;
    --spacing-lg: 16px;
    --spacing-xl: 20px;
    --spacing-xxl: 24px;
    
    /* Border radius */
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    
    /* Fonts */
    --font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --font-size-sm: 12px;
    --font-size-md: 14px;
    --font-size-lg: 16px;
    --font-size-xl: 18px;
}

body {
    font-family: var(--font-family);
    font-size: var(--font-size-md);
    color: var(--text-color);
    background-color: var(--bg-color);
    line-height: 1.5;
}

/* Yleisiä apuluokkia */
.hidden {
    display: none !important;
}

.visible {
    display: block !important;
}

/* NOTE: Avoid using a global .active rule to prevent unintended styling.
   Active styles are scoped per component below (e.g., .tab-button.active,
   .chat-tab.active). Removing the generic .active prevents text from turning
   white on white backgrounds inside active tabs. */

/* Päätä sivu (ei iframe) */
.main-content {
    max-width: 800px;
    margin: 0 auto;
    padding: var(--spacing-xxl);
}

.header {
    text-align: center;
    margin-bottom: var(--spacing-xxl);
}

.header h1 {
    font-size: 2rem;
    margin-bottom: var(--spacing-md);
    color: var(--primary-color);
}

.demo-section, .info-section {
    background: white;
    padding: var(--spacing-xl);
    margin-bottom: var(--spacing-lg);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-light);
}

.info-section ul {
    list-style: none;
    margin-top: var(--spacing-md);
}

.info-section li {
    margin-bottom: var(--spacing-sm);
    padding-left: var(--spacing-lg);
}

/* Chat Widget */
.chat-widget {
    position: fixed;
    bottom: var(--spacing-xl);
    right: var(--spacing-xl);
    z-index: 1000;
}

.chat-widget.iframe-mode {
    position: static;
    bottom: auto;
    right: auto;
    width: 100%;
    height: 100vh;
}

/* Kelluva painike */
.chat-toggle {
    width: 56px;
    height: 56px;
    background: var(--primary-color);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: var(--shadow-medium);
    transition: all 0.2s ease;
}

.chat-toggle:hover {
    transform: scale(1.05);
    box-shadow: 0 6px 20px rgba(0,0,0,0.2);
}

.chat-icon {
    font-size: 24px;
    color: white;
}

/* Chat paneeli */
.chat-panel {
    position: absolute;
    bottom: 70px;
    right: 0;
    width: 380px;
    max-width: 90vw;
    height: 600px;
    background: white;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-medium);
    display: none;
    flex-direction: column;
    overflow: hidden;
    animation: slideUp 0.2s ease-out;
}

.chat-widget.iframe-mode .chat-panel {
    position: static;
    width: 100%;
    height: 100%;
    max-width: none;
    border-radius: 0;
    box-shadow: none;
    display: flex;
}

.chat-panel.visible {
    display: flex;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Chat yläpalkki */
.chat-header {
    background: var(--primary-color);
    color: white;
    padding: var(--spacing-md) var(--spacing-lg);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.chat-title {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    font-weight: 600;
}

.bot-emoji {
    font-size: var(--font-size-lg);
}

.chat-tabs {
    display: flex;
    gap: var(--spacing-xs);
}

.tab-button {
    background: rgba(255,255,255,0.2);
    border: none;
    color: white;
    padding: var(--spacing-xs) var(--spacing-md);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: var(--font-size-sm);
    transition: background 0.2s;
}

.tab-button:hover {
    background: rgba(255,255,255,0.3);
}

.tab-button.active {
    background: rgba(255,255,255,0.4);
    font-weight: 600;
}

/* Scoped enhancement: active tab button in the chat header
   uses brand color for text without affecting other pages */
.chat-header .tab-button.active {
    background: #ffffff;
    color: var(--primary-color);
    border: 1px solid rgba(255,255,255,0.6);
}

.chat-close {
    background: none;
    border: none;
    color: white;
    font-size: var(--font-size-lg);
    cursor: pointer;
    padding: var(--spacing-xs);
    border-radius: var(--radius-sm);
    transition: background 0.2s;
}

.chat-close:hover {
    background: rgba(255,255,255,0.2);
}

/* Chat sisältö */
.chat-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0; /* allow children to shrink properly inside flex */
}

.chat-tab {
    display: none;
    flex: 1;
    min-height: 0; /* allow .chat-messages to scroll */
    flex-direction: column;
}

.chat-tab.tab-visible {
    display: flex;
}

/* Chat viestit - CRITICAL: This is the ONLY scrollable element in the widget */
.chat-messages {
    flex: 1 1 auto;
    min-height: 0; /* critical for flex children to allow scrolling */
    overflow-y: auto;
    overflow-x: hidden;
    padding: var(--spacing-lg);
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
    -webkit-overflow-scrolling: touch; /* iOS inertial scroll */
    position: relative; /* positioning context for the scroll-to-latest button */
    scroll-behavior: smooth;
}

/* Floating button to quickly return to latest messages */
.scroll-to-latest {
    position: sticky; /* stick near bottom inside scroll area */
    bottom: 8px;
    align-self: center;
    transform: translateY(8px);
    padding: 6px 12px;
    border-radius: 16px;
    border: 1px solid var(--border-color);
    background: rgba(255,255,255,0.95);
    color: var(--text-color);
    box-shadow: var(--shadow-light);
    cursor: pointer;
    font-size: var(--font-size-sm);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease, transform 0.15s ease;
    z-index: 5;
}

.scroll-to-latest.visible {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
}

.message {
    display: flex;
    gap: var(--spacing-sm);
    align-items: flex-start;
}

.message.user {
    flex-direction: row-reverse;
}

.avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--border-color);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--font-size-sm);
    flex-shrink: 0;
}

.message-content {
    max-width: 80%;
}

.message-bubble {
    background: var(--bg-color);
    padding: var(--spacing-md);
    border-radius: var(--radius-md);
    margin-bottom: var(--spacing-xs);
    word-wrap: break-word;
    color: var(--text-color);
    min-height: 1.2em; /* Ensure bubble has minimum height even when empty */
}

/* Hide empty bubbles completely */
.message-bubble:empty {
    display: none;
}

.message.user .message-bubble {
    background: var(--primary-color);
    color: white;
}

.message.bot .message-bubble {
    background: #e8f5e8;
    color: var(--text-color);
}

.message-time {
    font-size: var(--font-size-sm);
    color: var(--text-light);
    margin-left: var(--spacing-sm);
}

.message.user .message-time {
    margin-left: 0;
    margin-right: var(--spacing-sm);
}

/* Clickable links inside message bubbles */
.message-link {
    color: var(--primary-color);
    text-decoration: underline;
    word-break: break-all;
}

/* Kysymyspallerot */
.question-balls {
    display: flex;
    flex-wrap: wrap;
    gap: var(--spacing-sm);
    margin-top: var(--spacing-sm);
}

.question-ball {
    background: white;
    border: 2px solid var(--border-color);
    border-radius: 20px;
    padding: var(--spacing-sm) var(--spacing-md);
    cursor: pointer;
    font-size: var(--font-size-sm);
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: var(--spacing-xs);
    max-width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.question-ball:hover {
    border-color: var(--primary-color);
    background: var(--primary-color);
    color: white;
    transform: scale(1.02);
}

.question-ball.disabled {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
    background: #f3f3f3;
    border-color: var(--border-color);
}

/* Chat syöte - FIXED: Always stays at the bottom */
.chat-input-area {
    padding: var(--spacing-lg);
    border-top: 1px solid var(--border-color);
    background: white;
    flex: 0 0 auto; /* Do not grow or shrink */
}

.input-container {
    display: flex;
    gap: var(--spacing-sm);
    align-items: center;
}

#chatInput {
    flex: 1;
    padding: var(--spacing-md);
    border: 2px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: var(--font-size-md);
    outline: none;
    transition: border-color 0.2s;
}

#chatInput:focus {
    border-color: var(--primary-color);
}

.send-button {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: var(--spacing-md);
    border-radius: var(--radius-md);
    cursor: pointer;
    font-size: var(--font-size-lg);
    min-width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.send-button:hover {
    background: #45a049;
}

.send-button:disabled {
    background: var(--text-light);
    cursor: not-allowed;
}

/* Kalenteri */
.calendar-container {
    padding: var(--spacing-lg);
}

.calendar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--spacing-lg);
}

.month-nav {
    background: none;
    border: 2px solid var(--border-color);
    border-radius: var(--radius-sm);
    width: 36px;
    height: 36px;
    cursor: pointer;
    font-size: var(--font-size-lg);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.month-nav:hover {
    border-color: var(--primary-color);
    background: var(--primary-color);
    color: white;
}

#currentMonth {
    font-size: var(--font-size-lg);
    font-weight: 600;
}

.calendar-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 2px;
    margin-bottom: var(--spacing-lg);
}

.calendar-day {
    aspect-ratio: 1;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: var(--font-size-sm);
    transition: all 0.2s;
    background: white;
}

.calendar-day:hover {
    transform: scale(1.05);
    z-index: 1;
    box-shadow: var(--shadow-light);
}

.calendar-day.other-month {
    color: var(--text-light);
    background: #f5f5f5;
}

.calendar-day.today {
    font-weight: 600;
    border-color: var(--primary-color);
}

.calendar-day.free {
    background: #4ade80;
    border-color: #22c55e;
    color: white;
    font-weight: 600;
}

.calendar-day.partial {
    background: #fff3cd;
    border-color: var(--accent-color);
}

.calendar-day.busy {
    background: #f8d7da;
    border-color: var(--danger-color);
}

.calendar-legend {
    display: flex;
    gap: var(--spacing-lg);
    justify-content: center;
    flex-wrap: wrap;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: var(--spacing-xs);
    font-size: var(--font-size-sm);
}

.legend-color {
    width: 16px;
    height: 16px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
}

.legend-color.free {
    background: #4ade80;
    border-color: #22c55e;
}

.legend-color.partial {
    background: #fff3cd;
    border-color: var(--accent-color);
}

.legend-color.busy {
    background: #f8d7da;
    border-color: var(--danger-color);
}

/* Päiväkortti */
.day-card {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: white;
    z-index: 10;
    display: flex;
    flex-direction: column;
    animation: slideIn 0.2s ease-out;
}

@keyframes slideIn {
    from {
        transform: translateX(100%);
    }
    to {
        transform: translateX(0);
    }
}

.day-card-header {
    padding: var(--spacing-lg);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.close-day-card {
    background: none;
    border: none;
    font-size: var(--font-size-lg);
    cursor: pointer;
    padding: var(--spacing-xs);
    border-radius: var(--radius-sm);
    color: var(--text-light);
}

.close-day-card:hover {
    background: var(--border-color);
}

.day-card-content {
    flex: 1;
    padding: var(--spacing-lg);
    overflow-y: auto;
}

.day-card-actions {
    padding: var(--spacing-lg);
    border-top: 1px solid var(--border-color);
}

.action-button {
    width: 100%;
    padding: var(--spacing-md) var(--spacing-lg);
    border: none;
    border-radius: var(--radius-md);
    font-size: var(--font-size-md);
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.action-button.primary {
    background: var(--primary-color);
    color: white;
}

.action-button.primary:hover {
    background: #45a049;
}

/* Yhteystietolomake */
.contact-form-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--spacing-lg);
}

.contact-form {
    background: white;
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 500px;
    max-height: 90vh;
    overflow-y: auto;
    animation: fadeIn 0.2s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: scale(0.9);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.form-header {
    padding: var(--spacing-xl);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.form-header h3 {
    margin: 0;
    font-size: var(--font-size-xl);
}

.close-form {
    background: none;
    border: none;
    font-size: var(--font-size-lg);
    cursor: pointer;
    padding: var(--spacing-xs);
    border-radius: var(--radius-sm);
    color: var(--text-light);
}

.close-form:hover {
    background: var(--border-color);
}

.contact-form form {
    padding: var(--spacing-xl);
}

.form-group {
    margin-bottom: var(--spacing-lg);
}

.form-group label {
    display: block;
    margin-bottom: var(--spacing-xs);
    font-weight: 500;
    color: var(--text-color);
}

.form-group input,
.form-group textarea {
    width: 100%;
    padding: var(--spacing-md);
    border: 2px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: var(--font-size-md);
    font-family: inherit;
    outline: none;
    transition: border-color 0.2s;
}

.form-group input:focus,
.form-group textarea:focus {
    border-color: var(--primary-color);
}

.form-group textarea {
    resize: vertical;
    min-height: 80px;
}

.form-actions {
    display: flex;
    gap: var(--spacing-md);
    justify-content: flex-end;
    margin-top: var(--spacing-xl);
}

.button {
    padding: var(--spacing-md) var(--spacing-xl);
    border: none;
    border-radius: var(--radius-md);
    font-size: var(--font-size-md);
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    min-width: 100px;
}

.button.primary {
    background: var(--primary-color);
    color: white;
}

.button.primary:hover {
    background: #45a049;
}

.button.secondary {
    background: white;
    color: var(--text-color);
    border: 2px solid var(--border-color);
}

.button.secondary:hover {
    border-color: var(--text-color);
    background: #f8f8f8;
}

/* Chat alatunniste */
.chat-footer {
    padding: var(--spacing-sm) var(--spacing-lg);
    border-top: 1px solid var(--border-color);
    text-align: center;
    background: #f8f8f8;
    flex-shrink: 0;
}

.powered-by {
    font-size: var(--font-size-sm);
    color: var(--text-light);
}

/* Responsive */
@media (max-width: 768px) {
    .chat-widget {
        bottom: var(--spacing-md);
        right: var(--spacing-md);
        left: var(--spacing-md);
    }
    
    .chat-panel {
        width: 100%;
        height: 80vh;
        bottom: 70px;
        right: 0;
        left: 0;
        border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    }
    
    .calendar-grid {
        gap: 1px;
    }
    
    .calendar-day {
        font-size: 12px;
    }
    
    .question-balls {
        justify-content: center;
    }
    
    .question-ball {
        white-space: normal;
        text-align: center;
        min-width: 0;
    }
    
    .chat-tabs {
        order: -1;
        margin-right: auto;
    }
    
    .chat-title {
        order: 1;
    }
}

@media (max-width: 480px) {
    .main-content {
        padding: var(--spacing-lg);
    }
    
    .chat-panel {
        height: 100vh;
        border-radius: 0;
    }
    
    .form-actions {
        flex-direction: column;
    }
    
    .button {
        width: 100%;
    }
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Focus styles */
button:focus-visible,
input:focus-visible,
textarea:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

/* Loading indikaattori */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: var(--spacing-xs);
    color: var(--text-light);
    font-style: italic;
}

.typing-dots {
    display: flex;
    gap: 2px;
}

.typing-dot {
    width: 4px;
    height: 4px;
    background: var(--text-light);
    border-radius: 50%;
    animation: typingPulse 1.5s infinite;
}

.typing-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typingPulse {
    0%, 60%, 100% {
        opacity: 0.3;
    }
    30% {
        opacity: 1;
    }
}