/* ===================================================================
   Mökin Varauskalenteri — modern mobile-first stylesheet
   ------------------------------------------------------------------
   Design tokens use CSS custom properties; dark mode kicks in via
   `prefers-color-scheme`. All status classes the JS toggles
   (.pending, .approved-private, .approved-shared, .selected, etc.)
   are preserved — visual treatment changed, semantics unchanged.
   =================================================================== */

/* ---- Design tokens (light mode) ---- */
:root {
    /* Surface + text */
    --bg:            #f7f8fa;
    --surface:       #ffffff;
    --surface-2:     #f1f3f5;
    --border:        #e5e7eb;
    --border-strong: #d1d5db;
    --text:          #111827;
    --text-muted:    #6b7280;

    /* Accent */
    --accent:        #4f46e5;
    --accent-hover:  #4338ca;
    --accent-contrast: #ffffff;
    --accent-soft:   rgba(79, 70, 229, 0.10);

    /* Semantic status colors (dots/indicators) */
    --available:     #10b981;   /* emerald */
    --private:       #ef4444;   /* red */
    --shared:        #eab308;   /* amber */
    --pending:       #f59e0b;   /* orange */
    --restricted:    #94a3b8;   /* slate */
    --disabled-fg:   #9ca3af;

    /* Shape + motion */
    --radius-sm:  6px;
    --radius:     10px;
    --radius-lg:  16px;
    --radius-pill: 999px;
    --shadow-sm:  0 1px 2px rgba(15, 23, 42, 0.04);
    --shadow:     0 2px 10px rgba(15, 23, 42, 0.06);
    --shadow-lg:  0 12px 32px rgba(15, 23, 42, 0.12);
    --transition: 160ms cubic-bezier(0.2, 0, 0, 1);

    /* Spacing scale */
    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 0.75rem;
    --space-4: 1rem;
    --space-5: 1.5rem;
    --space-6: 2rem;
    --space-7: 3rem;

    /* Tap target minimums */
    --tap: 44px;
}

/* ---- Dark mode ---- */
@media (prefers-color-scheme: dark) {
    :root {
        --bg:            #0b1220;
        --surface:       #111827;
        --surface-2:     #1f2937;
        --border:        #1f2937;
        --border-strong: #374151;
        --text:          #f3f4f6;
        --text-muted:    #9ca3af;

        --accent:        #818cf8;
        --accent-hover:  #a5b4fc;
        --accent-contrast: #0b1220;
        --accent-soft:   rgba(129, 140, 248, 0.16);

        --available:     #34d399;
        --private:       #f87171;
        --shared:        #facc15;
        --pending:       #fbbf24;
        --restricted:    #64748b;
        --disabled-fg:   #6b7280;

        --shadow-sm:  0 1px 2px rgba(0, 0, 0, 0.4);
        --shadow:     0 2px 10px rgba(0, 0, 0, 0.4);
        --shadow-lg:  0 12px 32px rgba(0, 0, 0, 0.5);
    }
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;
    font-size: clamp(15px, 0.92rem + 0.2vw, 17px);
    line-height: 1.5;
    background-color: var(--bg);
    color: var(--text);
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3 {
    margin: 0;
    font-weight: 600;
    letter-spacing: -0.01em;
}

h1 { font-size: clamp(1.15rem, 1rem + 0.6vw, 1.4rem); }
h2 { font-size: clamp(1.1rem, 1rem + 0.4vw, 1.35rem); }
h3 { font-size: 1rem; }

p { margin: 0; }

/* ---- Header ---- */
.site-header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    color: var(--text);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-5);
    position: sticky;
    top: 0;
    z-index: 50;
    backdrop-filter: saturate(180%) blur(8px);
    -webkit-backdrop-filter: saturate(180%) blur(8px);
}

.site-header h1 {
    font-size: clamp(1.05rem, 0.95rem + 0.5vw, 1.25rem);
}

/* Centered title pattern: back-arrow on the left, spacer on the right */
.site-header .header-title {
    flex: 1;
    text-align: center;
}
.site-header .header-spacer {
    width: var(--tap);
    flex-shrink: 0;
}

.site-header nav a {
    color: var(--accent);
    text-decoration: none;
    font-weight: 500;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius);
    transition: background-color var(--transition);
}

.site-header nav a:hover {
    background-color: var(--accent-soft);
}

.user-info {
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

#username-display {
    font-size: 0.92rem;
    color: var(--text-muted);
    /* Hide on narrow screens — username still visible via account button hover/label */
    display: none;
}

@media (min-width: 540px) {
    #username-display { display: inline; }
}

/* ---- Main layout ---- */
main {
    width: 100%;
    max-width: 760px;
    margin: 0 auto;
    padding: var(--space-5) var(--space-4) var(--space-7);
    flex: 1;
}

@media (min-width: 720px) {
    main { padding: var(--space-6); }
}

main > section {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--space-5);
    box-shadow: var(--shadow-sm);
}

@media (min-width: 720px) {
    main > section { padding: var(--space-6); }
}

main > section + section { margin-top: var(--space-5); }

main > section > h2,
main > section > h3 {
    margin-bottom: var(--space-4);
}

/* ---- Buttons ---- */
button,
.button {
    appearance: none;
    -webkit-appearance: none;
    background: var(--accent);
    color: var(--accent-contrast);
    border: none;
    border-radius: var(--radius);
    font: inherit;
    font-weight: 500;
    padding: 0 var(--space-5);
    min-height: var(--tap);
    cursor: pointer;
    transition: background-color var(--transition), transform var(--transition), box-shadow var(--transition);
}

button:hover,
.button:hover { background: var(--accent-hover); }
button:active,
.button:active { transform: translateY(1px); }

button:focus-visible,
.button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

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

/* Semantic button variants (used by modal actions) */
.button-secondary {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border-strong);
}
.button-secondary:hover { background: var(--border); }

.button-danger {
    background: var(--private);
    color: #fff;
}
.button-danger:hover { background: color-mix(in oklab, var(--private), black 10%); filter: brightness(0.95); }

.button-approve {
    background: var(--available);
    color: #fff;
}
.button-approve:hover { filter: brightness(0.95); }

/* Icon buttons */
.icon-button {
    background: transparent;
    color: var(--text);
    border: none;
    border-radius: var(--radius-pill);
    width: var(--tap);
    height: var(--tap);
    min-height: 0;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color var(--transition);
}
.icon-button:hover { background: var(--surface-2); }
.icon-button svg { width: 22px; height: 22px; fill: currentColor; }

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

input[type="text"],
input[type="password"],
input[type="email"],
select,
textarea {
    width: 100%;
    appearance: none;
    -webkit-appearance: none;
    font: inherit;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    padding: 0 var(--space-4);
    min-height: var(--tap);
    transition: border-color var(--transition), box-shadow var(--transition);
}

/* Selectille tarvitaan natiivi pudotusnuoli; appearance:none piilottaisi sen. */
select {
    appearance: auto;
    -webkit-appearance: auto;
    padding-right: var(--space-3);
}

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

input:focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

/* Feedback messages */
.error-message,
.success-message,
.feedback-message {
    margin-top: var(--space-2);
    font-size: 0.92rem;
    min-height: 1.25em;
}
.error-message,
.feedback-message { color: var(--private); }
.success-message { color: var(--available); }

/* ---- Login view ---- */
#login-view {
    max-width: 420px;
    margin-left: auto;
    margin-right: auto;
}

/* ---- Calendar ---- */
.calendar-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--space-3);
    margin-bottom: var(--space-5);
}

.calendar-controls h2 {
    flex: 1;
    text-align: center;
    text-transform: capitalize;
}

.calendar-controls button {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border-strong);
    width: var(--tap);
    height: var(--tap);
    padding: 0;
    font-size: 1.1rem;
    line-height: 1;
}
.calendar-controls button:hover { background: var(--border); }

/* Weekday header */
.calendar-grid-header {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: var(--space-2);
    text-align: center;
    font-weight: 500;
    color: var(--text-muted);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-bottom: var(--space-2);
}

.calendar-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: var(--space-2);
}

/* Each day cell — three vertical zones for booker (top, absolute),
   day-number (middle, flex-centered), and status dot (bottom, absolute).
   Each is independently positioned so they cannot crowd each other on
   small mobile cells. */
.calendar-day {
    position: relative;
    aspect-ratio: 1 / 1;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius);
    background: var(--surface-2);
    color: var(--text);
    font-weight: 500;
    font-size: 0.95rem;
    cursor: pointer;
    user-select: none;
    transition: background-color var(--transition), color var(--transition), transform var(--transition);
    border: 1px solid transparent;
    /* Reserve top + bottom strips so flex content (day-number) cannot
       extend into the booker/dot zones, even on very small cells. */
    padding: 12px 2px 12px;
}

/* Day number — flex-centered in the remaining vertical space. */
.calendar-day .day-number {
    line-height: 1;
}

/* Booker's first name — pinned to the top strip of the cell. */
.calendar-day .booker {
    position: absolute;
    top: 3px;
    left: 50%;
    transform: translateX(-50%);
    max-width: calc(100% - 4px);
    font-size: 0.62rem;
    font-weight: 600;
    line-height: 1;
    color: var(--text-muted);
    letter-spacing: 0.01em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.calendar-day.selected .booker {
    color: var(--accent-contrast);
    opacity: 0.92;
}

.calendar-day:hover { background: var(--border); }
.calendar-day:active { transform: scale(0.97); }

.calendar-day.empty {
    background: transparent;
    cursor: default;
    pointer-events: none;
}

.calendar-day.disabled {
    background: transparent;
    color: var(--disabled-fg);
    cursor: not-allowed;
    text-decoration: line-through;
    pointer-events: none;
}

/* Status indicator dot at the bottom of each booked day */
.calendar-day.pending::after,
.calendar-day.approved-private::after,
.calendar-day.approved-shared::after,
.calendar-day.restricted::after {
    content: "";
    position: absolute;
    bottom: 6px;
    left: 50%;
    transform: translateX(-50%);
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-muted);
}

.calendar-day.pending {
    cursor: pointer;
    background: color-mix(in oklab, var(--pending), transparent 88%);
}
.calendar-day.pending::after { background: var(--pending); }

.calendar-day.approved-private {
    cursor: pointer;
    background: color-mix(in oklab, var(--private), transparent 88%);
}
.calendar-day.approved-private::after { background: var(--private); }

.calendar-day.approved-shared {
    cursor: pointer;
    background: color-mix(in oklab, var(--shared), transparent 86%);
}
.calendar-day.approved-shared::after { background: var(--shared); }

.calendar-day.restricted {
    cursor: not-allowed;
    pointer-events: none;
    background: var(--surface-2);
    color: var(--restricted);
}
.calendar-day.restricted::after { background: var(--restricted); }

/* Selected (overrides everything) */
.calendar-day.selected {
    background: var(--accent);
    color: var(--accent-contrast);
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}
.calendar-day.selected::after { background: var(--accent-contrast); }

/* Slightly larger cells on tablet+ */
@media (min-width: 600px) {
    .calendar-day { font-size: 1rem; }
}

/* On mobile, let the calendar section extend edge-to-edge so cells get more
   width. The negative margin "escapes" main's horizontal padding; the
   side border + radius + box-shadow are dropped so it visually fills the
   viewport. Other sections keep their card appearance. */
@media (max-width: 600px) {
    #calendar-view {
        margin-left: calc(-1 * var(--space-4));
        margin-right: calc(-1 * var(--space-4));
        border-left: 0;
        border-right: 0;
        border-radius: 0;
        box-shadow: none;
        padding-left: var(--space-2);
        padding-right: var(--space-2);
    }

    /* Tighter gap between cells maximises usable cell area. */
    .calendar-grid,
    .calendar-grid-header {
        gap: var(--space-1);
    }
}

/* ---- Status legend (optional, render via JS if desired; styles ready) ---- */
.legend {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3) var(--space-4);
    margin-top: var(--space-4);
    color: var(--text-muted);
    font-size: 0.85rem;
}
.legend-item { display: inline-flex; align-items: center; gap: var(--space-2); }
.legend-dot { width: 8px; height: 8px; border-radius: 50%; }

/* ---- Booking controls view ---- */
.booking-type-selection {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    margin: var(--space-4) 0;
}

.booking-type-selection label {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    cursor: pointer;
    transition: border-color var(--transition), background-color var(--transition);
}

.booking-type-selection label:has(input:checked) {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.booking-type-selection input[type="radio"] {
    width: 18px;
    height: 18px;
    accent-color: var(--accent);
}

.booking-actions {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
}

@media (min-width: 540px) {
    .booking-actions { flex-direction: row; }
    .booking-actions > * { flex: 1; }
}

#selected-days-list {
    color: var(--accent);
}

/* ---- Modal ---- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.55);
    display: flex;
    justify-content: center;
    align-items: flex-end;       /* slide-up sheet on mobile */
    z-index: 1000;
    padding: 0;
    animation: fade-in 200ms ease-out;
}

@media (min-width: 540px) {
    .modal-overlay {
        align-items: center;
        padding: var(--space-5);
    }
}

.modal-content {
    background: var(--surface);
    color: var(--text);
    padding: var(--space-5);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 480px;
    animation: slide-up 240ms cubic-bezier(0.2, 0, 0, 1);
}

@media (min-width: 540px) {
    .modal-content {
        border-radius: var(--radius-lg);
        animation: slide-down 240ms cubic-bezier(0.2, 0, 0, 1);
    }
}

.modal-title { margin-bottom: var(--space-3); }

.modal-body {
    color: var(--text-muted);
    margin-bottom: var(--space-5);
}
.modal-body p + p { margin-top: var(--space-2); }
.modal-body strong { color: var(--text); }

.modal-actions {
    display: flex;
    flex-direction: column-reverse;
    gap: var(--space-2);
}
@media (min-width: 540px) {
    .modal-actions {
        flex-direction: row;
        justify-content: flex-end;
    }
}

@keyframes slide-up {
    from { transform: translateY(40px); opacity: 0; }
    to   { transform: translateY(0);    opacity: 1; }
}
@keyframes slide-down {
    from { transform: translateY(-16px); opacity: 0; }
    to   { transform: translateY(0);     opacity: 1; }
}
@keyframes fade-in {
    from { opacity: 0; }
    to   { opacity: 1; }
}

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

/* ---- Footer ---- */
footer {
    text-align: center;
    padding: var(--space-5);
    color: var(--text-muted);
    font-size: 0.85rem;
    margin-top: auto;
}

/* ---- Chat (oma osio, ilmestyy booking-controls-näkymän alle) ---- */
#chat-view h2 {
    margin-bottom: var(--space-4);
}

.chat-list {
    max-height: 320px;
    overflow-y: auto;
    overscroll-behavior: contain;
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    margin-bottom: var(--space-4);
    padding: var(--space-3);
    background: var(--surface-2);
    border-radius: var(--radius);
}

.chat-empty {
    color: var(--text-muted);
    text-align: center;
    padding: var(--space-4);
    margin: 0;
}

.chat-message {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: var(--space-3);
    max-width: 92%;
    align-self: flex-start;
}

.chat-message--own {
    align-self: flex-end;
    background: var(--accent-soft);
    border-color: var(--accent);
}

.chat-meta {
    display: flex;
    gap: var(--space-2);
    align-items: baseline;
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: var(--space-1);
}

.chat-meta strong {
    color: var(--text);
    font-weight: 600;
}

.chat-content {
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

.chat-form {
    display: flex;
    gap: var(--space-2);
    align-items: flex-start;
}

.chat-form textarea {
    flex: 1;
    min-height: var(--tap);
    resize: vertical;
    padding: var(--space-3) var(--space-4);
    font: inherit;
    line-height: 1.4;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    transition: border-color var(--transition), box-shadow var(--transition);
}

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

.chat-form button {
    flex-shrink: 0;
    align-self: stretch;
}

#chat-feedback:empty {
    display: none;
}
