:root {
    color-scheme: light;
    --bg: #f8fafc;
    --surface: #ffffff;
    --surface-soft: #f1f5f9;
    --text: #0f172a;
    --muted: #64748b;
    --line: #e2e8f0;
    --primary: #3b82f6;
    --primary-dark: #2563eb;
    --danger: #ef4444;
    --danger-soft: #fef2f2;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);
    --radius: 12px;
    --radius-sm: 8px;
    
    /* User colors - more vibrant modern palette */
    --user-color-1: #ef4444;
    --user-color-2: #3b82f6;
    --user-color-3: #10b981;
    --user-color-4: #8b5cf6;
    --user-color-5: #f59e0b;
}

* {
    box-sizing: border-box;
}

html {
    -webkit-text-size-adjust: 100%;
}

body {
    min-height: 100vh;
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
}

button,
input {
    font: inherit;
}

button {
    min-height: 44px;
    border: 0;
    cursor: pointer;
    touch-action: manipulation;
}

input:not([type="checkbox"]) {
    width: 100%;
    min-height: 48px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 11px 12px;
    background: #fff;
    color: var(--text);
    font-size: 16px;
}

input[type="checkbox"] {
    width: 20px;
    height: 20px;
    margin: 0;
    accent-color: var(--primary);
}

input:disabled {
    background: #f4f6fa;
    color: var(--muted);
    opacity: 1;
}

input:focus,
button:focus-visible,
a:focus-visible {
    outline: 3px solid rgba(23, 105, 170, 0.25);
    outline-offset: 2px;
}

a {
    color: inherit;
}

.site-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: calc(16px + env(safe-area-inset-top)) 20px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    color: var(--text);
}

.brand,
.login-brand {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.brand-mark {
    display: inline-grid;
    place-items: center;
    flex: 0 0 40px;
    width: 40px;
    height: 40px;
    border-radius: var(--radius-sm);
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    font-weight: 700;
    font-size: 18px;
}

.brand h1,
.login-brand h1 {
    margin: 0;
    font-size: 20px;
    line-height: 1.2;
    letter-spacing: -0.02em;
    font-weight: 700;
}

.brand p {
    margin: 2px 0 0;
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
}

.user-menu {
    display: flex;
    align-items: center;
    gap: 12px;
    white-space: nowrap;
    font-size: 14px;
}

.group-selector {
    display: flex;
    align-items: center;
}

.group-select {
    min-height: 36px;
    border-radius: var(--radius-sm);
    padding: 6px 12px;
    background: var(--surface-soft);
    color: var(--text);
    font-weight: 600;
    font-size: 13px;
    border: 1px solid var(--line);
    cursor: pointer;
    transition: all 0.2s;
}

.group-select:hover {
    background: var(--line);
}

.group-select:focus {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

.user-menu a {
    min-height: 36px;
    display: inline-flex;
    align-items: center;
    border-radius: var(--radius-sm);
    padding: 8px 12px;
    text-decoration: none;
    background: var(--surface-soft);
    color: var(--text);
    font-weight: 600;
    font-size: 13px;
    transition: background 0.2s;
}

.user-menu a:hover {
    background: var(--line);
}

.container {
    width: min(1120px, 100%);
    margin: 0 auto;
    padding: 24px 20px calc(32px + env(safe-area-inset-bottom));
}

.calendar-card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
}

.calendar-toolbar {
    display: grid;
    grid-template-columns: 48px minmax(0, 1fr) 48px;
    align-items: center;
    gap: 12px;
    padding: 16px 20px;
    border-bottom: 1px solid var(--line);
    background: var(--surface);
}

.month-heading {
    min-width: 0;
    text-align: center;
}

.month-heading h2 {
    margin: 0;
    font-size: clamp(20px, 4vw, 26px);
    line-height: 1.2;
    text-transform: capitalize;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.icon-button,
.close-button {
    display: inline-grid;
    place-items: center;
    border-radius: var(--radius-sm);
    background: var(--surface-soft);
    color: var(--text);
    font-size: 24px;
    line-height: 1;
    transition: all 0.2s;
}

.icon-button:hover,
.close-button:hover {
    background: var(--line);
}

.icon-button:active,
.primary-button:active,
.secondary-button:active,
.danger-button:active,
.link-button:active {
    transform: translateY(1px);
}

.link-button {
    min-height: 32px;
    margin-top: 4px;
    background: transparent;
    color: var(--primary);
    font-weight: 600;
    padding: 0 8px;
    border-radius: var(--radius-sm);
    transition: background 0.2s;
}

.link-button:hover {
    background: var(--surface-soft);
}

.calendar-grid {
    display: grid;
    grid-template-columns: minmax(34px, 0.42fr) repeat(7, minmax(0, 1fr));
    gap: 1px;
    background: var(--line);
}

.weekday,
.day,
.empty-day,
.week-number {
    background: var(--surface);
}

.weekday {
    min-height: 36px;
    display: grid;
    place-items: center;
    color: var(--muted);
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.week-heading,
.week-number {
    background: var(--surface-soft);
    color: var(--muted);
}

.week-number {
    min-height: 80px;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding-top: 10px;
    font-size: 11px;
    font-weight: 600;
}

.day {
    position: relative;
    min-height: 90px;
    padding: 6px 4px;
    text-align: left;
    -webkit-tap-highlight-color: transparent;
    transition: background 0.15s;
}

.day:hover {
    background: var(--surface-soft);
}

.day-open {
    min-height: 28px;
    display: inline-grid;
    place-items: center;
    border-radius: 50%;
    padding: 0;
    background: transparent;
    color: inherit;
}

.day.is-today {
    background: #eff6ff;
}

.day-number {
    width: 28px;
    height: 28px;
    display: inline-grid;
    place-items: center;
    border-radius: 50%;
    font-weight: 600;
    font-size: 14px;
}

.day.is-today .day-number {
    background: var(--primary);
    color: white;
}

.event {
    width: 100%;
    margin-top: 4px;
    border-radius: var(--radius-sm);
    padding: 5px 8px;
    color: white;
    text-align: left;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
    transition: transform 0.15s, box-shadow 0.15s;
}

.event:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

.event.is-allday-compact {
    min-height: 0;
    margin-top: 2px;
    border-radius: 6px;
    padding: 3px 6px;
    box-shadow: none;
    display: flex;
    align-items: center;
    gap: 4px;
}

.event.is-allday-compact.is-own {
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.45) inset;
}

.event.is-allday-compact .event-user {
    font-size: 9px;
    font-weight: 700;
    line-height: 1.1;
}

.user-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    font-size: 9px;
    font-weight: 700;
    color: white;
    flex-shrink: 0;
}

.user-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    padding: 16px 20px;
    background: var(--surface-soft);
    border-bottom: 1px solid var(--line);
}

.user-legend-item {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    font-weight: 600;
}

@media (max-width: 760px) {
    .user-legend {
        padding: 8px 12px;
        gap: 6px;
    }

    .user-legend-item {
        font-size: 11px;
    }
}

.user-legend-badge {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 9px;
    font-weight: 700;
    color: white;
}

.event.is-readonly {
    cursor: default;
}

.event.is-own {
    box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.55) inset, 0 2px 7px rgba(24, 38, 63, 0.15);
}

.event-user {
    display: block;
    overflow: hidden;
    font-size: 12px;
    font-weight: 800;
    line-height: 1.25;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.event-text {
    display: block;
    overflow: hidden;
    margin-top: 1px;
    font-size: 11px;
    line-height: 1.25;
    opacity: 0.92;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.event-range {
    display: block;
    overflow: hidden;
    margin-top: 2px;
    font-size: 10px;
    font-weight: 800;
    line-height: 1.2;
    opacity: 0.88;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.event-time {
    display: block;
    font-size: 10px;
    font-weight: 900;
    line-height: 1.2;
    opacity: 0.95;
}

.event-more {
    width: 100%;
    min-height: 18px;
    margin-top: 2px;
    border-radius: 4px;
    background: #eef3fa;
    color: var(--primary);
    font-size: 9px;
    font-weight: 800;
    padding: 2px 4px;
}

.fab {
    display: none;
    position: fixed;
    right: calc(20px + env(safe-area-inset-right));
    bottom: calc(20px + env(safe-area-inset-bottom));
    z-index: 5;
    width: 56px;
    height: 56px;
    border-radius: 16px;
    background: var(--primary);
    color: white;
    font-size: 28px;
    line-height: 1;
    box-shadow: var(--shadow-lg);
    transition: transform 0.2s, box-shadow 0.2s;
}

.fab:hover {
    transform: scale(1.05);
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.15);
}

.sheet {
    position: fixed;
    inset: 0;
    z-index: 11;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    background: rgba(15, 23, 42, 0.48);
    opacity: 0;
    transition: opacity 0.22s ease;
}

.sheet.is-open {
    opacity: 1;
}

.sheet-box {
    width: 100%;
    max-height: calc(100svh - env(safe-area-inset-top) - 12px);
    display: grid;
    gap: 12px;
    padding: 12px 20px calc(24px + env(safe-area-inset-bottom));
    border-radius: 20px 20px 0 0;
    background: var(--surface);
    box-shadow: var(--shadow-lg);
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    overflow: auto;
}

.sheet.is-open .sheet-box {
    transform: translateY(0);
}

.sheet-handle {
    width: 40px;
    height: 4px;
    margin: 0 auto 4px;
    border-radius: 999px;
    background: var(--line);
}

.sheet-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.sheet-header h3 {
    margin: 0;
    font-size: 18px;
    line-height: 1.2;
    text-transform: capitalize;
}

.sheet-events {
    display: grid;
    gap: 8px;
}

.sheet-event {
    min-height: 52px;
    padding: 10px 12px;
}

.sheet-empty {
    margin: 0;
    color: var(--muted);
    font-size: 14px;
}

.sheet-add {
    width: 100%;
}

#popup {
    position: fixed;
    inset: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 18px;
    background: rgba(15, 23, 42, 0.48);
    opacity: 0;
    transition: opacity 0.22s ease;
}

#popup.is-open {
    opacity: 1;
}

.hidden {
    display: none !important;
}

.popup-box {
    width: min(420px, 100%);
    display: grid;
    gap: 16px;
    padding: 24px;
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow-lg);
    transform: scale(0.96);
    transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

#popup.is-open .popup-box {
    transform: scale(1);
}

.date-summary {
    margin: -4px 0 0;
    padding: 10px 12px;
    border-radius: var(--radius);
    background: var(--surface-soft);
    color: var(--primary);
    font-size: 14px;
    font-weight: 700;
    line-height: 1.35;
    text-transform: capitalize;
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
}

.time-fields {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.popup-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.popup-header h3 {
    margin: 0;
    font-size: 20px;
}

.close-button {
    width: 44px;
    background: #eef3fa;
    color: var(--text);
    font-size: 26px;
}

label {
    display: grid;
    gap: 6px;
    color: #344054;
    font-size: 13px;
    font-weight: 700;
}

.form-error,
.error {
    margin: 0;
    border-radius: var(--radius);
    background: var(--danger-soft);
    color: #9a3412;
    padding: 10px 12px;
    font-size: 14px;
    font-weight: 700;
}

.popup-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    padding-top: 4px;
}

.popup-actions .danger-button {
    grid-column: 1 / -1;
}

.primary-button,
.secondary-button,
.danger-button {
    min-height: 48px;
    border-radius: var(--radius);
    padding: 11px 14px;
    font-weight: 800;
}

.primary-button {
    background: var(--primary);
    color: white;
}

.primary-button:hover,
.icon-button:hover {
    background: var(--primary-dark);
}

.secondary-button {
    background: var(--surface-soft);
    color: var(--text);
    font-weight: 600;
}

.secondary-button:hover {
    background: var(--line);
}

.danger-button {
    background: var(--danger-soft);
    color: var(--danger);
}

.login-page {
    min-height: 100svh;
    display: grid;
    place-items: center;
    padding: calc(18px + env(safe-area-inset-top)) 18px calc(18px + env(safe-area-inset-bottom));
    background:
        radial-gradient(circle at top left, rgba(245, 184, 75, 0.32), transparent 280px),
        linear-gradient(145deg, #123b5d, #1769aa 62%, #e8f3ff);
}

.login-shell {
    width: min(390px, 100%);
}

.login-box {
    display: grid;
    gap: 18px;
    width: 100%;
    padding: 24px;
    border-radius: var(--radius);
    background: rgba(255, 255, 255, 0.96);
    box-shadow: var(--shadow);
}

.login-box p {
    margin: -6px 0 0;
    color: var(--muted);
}

.login-box form {
    display: grid;
    gap: 14px;
}

.login-box .primary-button {
    width: 100%;
    margin-top: 2px;
}

@media (max-width: 760px) {
    .fab {
        display: inline-grid;
        place-items: center;
    }

    .site-header {
        align-items: flex-start;
        padding-left: 16px;
        padding-right: 16px;
        padding-top: calc(12px + env(safe-area-inset-top));
    }

    .brand p {
        display: none;
    }

    .brand h1 {
        font-size: 18px;
    }

    .brand-mark {
        flex-basis: 36px;
        width: 36px;
        height: 36px;
        font-size: 16px;
    }

    .user-menu {
        flex-direction: column;
        align-items: flex-end;
        gap: 4px;
        font-size: 12px;
    }

    .container {
        padding: 16px 12px calc(24px + env(safe-area-inset-bottom));
    }

    .calendar-toolbar {
        grid-template-columns: 44px minmax(0, 1fr) 44px;
        padding: 12px 16px;
    }

    .calendar-card {
        border-left: 0;
        border-right: 0;
        border-radius: 0;
    }

    .weekday {
        min-height: 32px;
        font-size: 10px;
    }

    .calendar-grid {
        grid-template-columns: repeat(7, minmax(0, 1fr));
    }

    .week-heading,
    .week-number {
        display: none;
    }

    .day {
        min-height: 70px;
        padding: 4px 3px;
    }

    .day-number {
        width: 24px;
        height: 24px;
        font-size: 13px;
    }

    .event:not(.is-allday-compact) {
        min-height: 36px;
        margin-top: 2px;
        padding: 4px 6px;
    }

    .event.is-allday-compact {
        margin-top: 1px;
        padding: 2px 5px;
        min-height: 20px;
    }

    .event.is-allday-compact .event-user {
        font-size: 9px;
        line-height: 1.15;
    }

    .user-badge {
        width: 16px;
        height: 16px;
        font-size: 8px;
    }

    .event:not(.is-allday-compact) .event-user {
        font-size: 10px;
        line-height: 1.15;
    }

    .event-text {
        font-size: 9px;
        line-height: 1.15;
    }

    .event-range {
        font-size: 9px;
    }

    .event:not(.is-allday-compact) .event-user,
    .event-text,
    .event-range {
        white-space: normal;
    }

    #popup {
        align-items: flex-end;
        padding: 0;
    }

    .popup-box {
        width: 100%;
        max-height: calc(100svh - env(safe-area-inset-top) - 12px);
        overflow: auto;
        border-radius: 20px 20px 0 0;
        padding: 20px 16px calc(24px + env(safe-area-inset-bottom));
        transform: translateY(100%);
    }

    #popup.is-open .popup-box {
        transform: translateY(0);
    }

    .time-fields {
        grid-template-columns: 1fr;
    }

    .user-legend {
        padding: 12px 16px;
        gap: 8px;
    }

    .user-legend-item {
        font-size: 12px;
    }
}

@media (max-width: 390px) {
    .site-header {
        gap: 8px;
    }

    .brand {
        gap: 8px;
    }

    .brand h1 {
        font-size: 15px;
    }

    .user-menu a {
        padding-left: 8px;
        padding-right: 8px;
    }

    .month-heading h2 {
        font-size: 19px;
    }

    .day {
        min-height: 68px;
    }
}
