:root {
    --sidebar-width: 270px;
}

html, body {
    height: 100%;
    width: 100%;
    overflow: hidden;
}

/* Base App Layout */
.calendar-app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    width: 100vw;
}

/* Center Navigator & View Selector */
.top-bar-center {
    display: flex;
    align-items: center;
    gap: 1.25rem;
}

.date-navigator {
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

.current-period-title {
    font-family: var(--font-heading);
    font-size: 1.15rem;
    font-weight: 700;
    min-width: 170px;
    margin-left: 0.5rem;
    color: var(--text-main);
}

.view-mode-group {
    display: flex;
    align-items: center;
    background: var(--bg-surface-elevated);
    padding: 3px;
    border-radius: 8px;
    border: 1px solid var(--border-subtle);
    gap: 2px;
}

.view-btn {
    background: none;
    border: 1px solid transparent;
    color: var(--text-muted);
    font-size: 0.8rem;
    font-weight: 500;
    padding: 0.3rem 0.75rem;
    border-radius: 6px;
    cursor: pointer;
    transition: var(--transition);
}

.view-btn:hover {
    color: var(--text-main);
}

.view-btn.active {
    background: rgba(56, 189, 248, 0.12);
    color: var(--primary-light);
    border-color: rgba(56, 189, 248, 0.28);
    font-weight: 600;
    box-shadow: none;
}

/* Top Bar Right */
.top-bar-right {
    display: flex;
    align-items: center;
    gap: 0.65rem;
}


/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.45rem;
    font-family: var(--font-body);
    font-size: 0.82rem;
    font-weight: 600;
    padding: 0.42rem 0.85rem;
    border-radius: 8px;
    cursor: pointer;
    border: 1px solid transparent;
    transition: var(--transition);
}

.btn-sm {
    padding: 0.3rem 0.65rem;
    font-size: 0.78rem;
    border-radius: 7px;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
    box-shadow: 0 2px 8px var(--primary-glow);
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-secondary {
    background: var(--bg-surface-elevated);
    border-color: var(--border-subtle);
    color: var(--text-main);
}

.btn-secondary:hover {
    background: var(--bg-card-hover);
    border-color: var(--border-hover);
}

.btn-danger {
    background: var(--danger);
    color: #fff;
}

.btn-danger:hover {
    background: var(--danger-hover);
}

.icon-btn {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    border: 1px solid var(--border-subtle);
    background: var(--bg-surface-elevated);
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition);
}

.icon-btn:hover {
    color: var(--text-main);
    border-color: var(--border-hover);
}

.icon-btn-sm {
    width: 26px;
    height: 26px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-subtle);
    background: transparent;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition);
}

.icon-btn-sm:hover {
    color: var(--text-main);
    background: var(--bg-surface-elevated);
    border-color: var(--border-hover);
}

.mobile-toggle-btn {
    display: none;
}

/* Theme toggle icons */
[data-theme="light"] .moon-icon { display: block; }
[data-theme="light"] .sun-icon { display: none; }
:root:not([data-theme="light"]) .moon-icon { display: none; }
:root:not([data-theme="light"]) .sun-icon { display: block; }

/* Calendar Main Layout */
.calendar-layout {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Sidebar */
.calendar-sidebar {
    width: var(--sidebar-width);
    background: var(--bg-surface);
    border-right: 1px solid var(--border-subtle);
    display: flex;
    flex-direction: column;
    padding: 1.25rem 1rem;
    gap: 1.25rem;
    overflow-y: auto;
    flex-shrink: 0;
}

/* Mini Calendar Widget */
.mini-calendar-widget {
    background: var(--bg-surface-elevated);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: 0.75rem;
}

.mini-cal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.5rem;
}

.mini-cal-title {
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--text-main);
}

.mini-cal-nav {
    display: flex;
    gap: 0.2rem;
}

.mini-nav-btn {
    width: 22px;
    height: 22px;
    background: none;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-size: 0.7rem;
    cursor: pointer;
}

.mini-nav-btn:hover {
    color: var(--text-main);
    background: var(--bg-card-hover);
}

.mini-cal-weekdays {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    text-align: center;
    font-size: 0.68rem;
    font-weight: 600;
    color: var(--text-faint);
    margin-bottom: 0.35rem;
}

.mini-cal-days {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 2px;
}

.mini-day-cell {
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.72rem;
    border-radius: 4px;
    cursor: pointer;
    color: var(--text-muted);
    transition: var(--transition);
}

.mini-day-cell:hover {
    background: var(--border-subtle);
    color: var(--text-main);
}

.mini-day-cell.other-month {
    opacity: 0.3;
}

.mini-day-cell.today {
    color: var(--primary-light);
    font-weight: 700;
}

.mini-day-cell.selected {
    background: var(--primary);
    color: #fff;
    font-weight: 700;
}

.mini-day-cell.has-events::after {
    content: '';
    position: absolute;
    bottom: 2px;
    width: 3px;
    height: 3px;
    border-radius: 50%;
    background: var(--primary-light);
}

/* Sidebar Section */
.sidebar-section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.65rem;
}

.sidebar-section-header h3 {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-faint);
}

/* Calendars List */
.calendars-list {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.calendar-item {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.4rem 0.5rem;
    border-radius: var(--radius-sm);
    transition: var(--transition);
    user-select: none;
}

.calendar-item:hover {
    background: var(--bg-surface-elevated);
}

.cal-checkbox {
    appearance: none;
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 4px;
    cursor: pointer;
    display: grid;
    place-content: center;
    transition: var(--transition);
}

.cal-checkbox::before {
    content: '';
    width: 8px;
    height: 8px;
    clip-path: polygon(14% 44%, 0 65%, 50% 100%, 100% 16%, 80% 0%, 43% 62%);
    transform: scale(0);
    transition: 0.12s transform ease-in-out;
    background-color: white;
}

.cal-checkbox:checked::before {
    transform: scale(1);
}

.cal-name {
    flex: 1;
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--text-main);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
}

.cal-count {
    font-size: 0.7rem;
    color: var(--text-faint);
    background: var(--bg-surface-elevated);
    padding: 0.1rem 0.4rem;
    border-radius: var(--radius-full);
}

.cal-actions {
    display: none;
    gap: 0.2rem;
}

.calendar-item:hover .cal-actions {
    display: flex;
}

/* Upcoming Section */
.upcoming-section {
    flex: 1;
}

.upcoming-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.upcoming-card {
    padding: 0.5rem 0.65rem;
    border-radius: var(--radius-sm);
    background: var(--bg-surface-elevated);
    border-left: 3px solid var(--primary);
    cursor: pointer;
    transition: var(--transition);
}

.upcoming-card:hover {
    background: var(--bg-card-hover);
    transform: translateX(2px);
}

.upcoming-card-title {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-main);
    margin-bottom: 0.15rem;
}

.upcoming-card-time {
    font-size: 0.7rem;
    color: var(--text-muted);
}

.empty-hint {
    font-size: 0.78rem;
    color: var(--text-faint);
    font-style: italic;
    padding: 0.5rem 0;
}

/* Sidebar Footer */
.sidebar-footer {
    display: flex;
    gap: 0.5rem;
    padding-top: 0.75rem;
    border-top: 1px solid var(--border-subtle);
}

.btn-sidebar-action {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    background: var(--bg-surface-elevated);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    padding: 0.4rem;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-muted);
    cursor: pointer;
    transition: var(--transition);
}

.btn-sidebar-action:hover {
    color: var(--text-main);
    border-color: var(--border-hover);
}

/* Main Canvas Area */
.calendar-canvas {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-base);
    overflow: hidden;
}

.canvas-subbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.6rem 1.25rem;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-subtle);
    gap: 1rem;
}

.search-wrap {
    display: flex;
    align-items: center;
    background: var(--bg-surface-elevated);
    border: 1px solid var(--border-subtle);
    border-radius: 8px;
    padding: 0.28rem 0.75rem;
    gap: 0.4rem;
    width: 280px;
    transition: var(--transition);
}

.search-wrap:focus-within {
    border-color: var(--primary-light);
    box-shadow: 0 0 0 2px var(--primary-glow);
}

.search-icon {
    color: var(--text-faint);
}

.search-wrap input {
    background: none;
    border: none;
    outline: none;
    color: var(--text-main);
    font-size: 0.8rem;
    width: 100%;
}

.clear-search-btn {
    background: none;
    border: none;
    color: var(--text-faint);
    cursor: pointer;
    font-size: 1rem;
}

.quick-stats-pills {
    display: flex;
    gap: 0.5rem;
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* View Container */
.view-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

/* ========================================== */
/* Month View Grid                            */
/* ========================================== */
.calendar-month-view {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
}

.month-weekdays-header {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-subtle);
    text-align: center;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    padding: 0.5rem 0;
}

.month-grid {
    flex: 1;
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    grid-template-rows: repeat(6, 1fr);
    gap: 1px;
    background: var(--border-subtle);
    overflow-y: auto;
}

.month-cell {
    background: var(--bg-surface);
    padding: 0.4rem;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    min-height: 90px;
    cursor: pointer;
    transition: var(--transition);
}

.month-cell:hover {
    background: var(--bg-card-hover);
}

.month-cell.other-month {
    background: var(--bg-base);
    opacity: 0.5;
}

.month-cell-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.2rem;
}

.month-day-num {
    font-size: 0.78rem;
    font-weight: 600;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    color: var(--text-muted);
}

.month-cell.today .month-day-num {
    background: var(--primary);
    color: #fff;
    font-weight: 700;
}

/* Event Badges in Month Cells */
.event-pill {
    font-size: 0.72rem;
    font-weight: 500;
    padding: 0.15rem 0.4rem;
    border-radius: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    align-items: center;
    gap: 0.3rem;
    color: #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.event-pill:hover {
    filter: brightness(1.15);
    transform: scale(1.02);
}

.event-pill.completed {
    text-decoration: line-through;
    opacity: 0.6;
}

.event-overflow-badge {
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--text-muted);
    padding: 0.1rem 0.3rem;
    cursor: pointer;
}

.event-overflow-badge:hover {
    color: var(--primary-light);
}

/* ========================================== */
/* Week & Day View Grids                      */
/* ========================================== */
.calendar-week-view, .calendar-day-view {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.week-header-row {
    display: grid;
    grid-template-columns: 60px repeat(7, 1fr);
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-subtle);
    padding: 0.5rem 0;
}

.time-col-header {
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--text-faint);
    text-align: center;
}

.week-day-header-cell {
    text-align: center;
}

.week-day-name {
    font-size: 0.72rem;
    color: var(--text-muted);
}

.week-day-number {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text-main);
}

.week-day-header-cell.today .week-day-number {
    color: var(--primary-light);
}

.week-body-scroll, .day-body-scroll {
    flex: 1;
    overflow-y: auto;
    position: relative;
}

.week-time-grid {
    display: grid;
    grid-template-columns: 60px repeat(7, 1fr);
    position: relative;
    min-height: 1200px;
}

.day-time-grid {
    display: grid;
    grid-template-columns: 60px 1fr;
    position: relative;
    min-height: 1200px;
}

.time-hour-label {
    height: 50px;
    font-size: 0.68rem;
    color: var(--text-faint);
    text-align: right;
    padding-right: 0.5rem;
    border-bottom: 1px solid var(--border-subtle);
}

.time-grid-slot {
    height: 50px;
    border-bottom: 1px solid var(--border-subtle);
    border-left: 1px solid var(--border-subtle);
    cursor: pointer;
    transition: var(--transition);
}

.time-grid-slot:hover {
    background: rgba(56, 189, 248, 0.04);
}

/* Absolute positioned events in Week / Day */
.timed-event-card {
    position: absolute;
    border-radius: var(--radius-sm);
    padding: 0.35rem 0.5rem;
    font-size: 0.75rem;
    color: #fff;
    cursor: pointer;
    overflow: hidden;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
    z-index: 10;
    transition: var(--transition);
}

.timed-event-card:hover {
    filter: brightness(1.15);
    z-index: 20;
}

.timed-event-title {
    font-weight: 600;
    margin-bottom: 0.15rem;
}

.timed-event-time {
    font-size: 0.68rem;
    opacity: 0.9;
}

/* Current Time Indicator Line */
.current-time-line {
    position: absolute;
    left: 60px;
    right: 0;
    height: 2px;
    background: #ef4444;
    z-index: 15;
    pointer-events: none;
}

.current-time-line::before {
    content: '';
    position: absolute;
    left: -5px;
    top: -4px;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #ef4444;
}

/* ========================================== */
/* Agenda View                                */
/* ========================================== */
.calendar-agenda-view {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
}

.agenda-list {
    max-width: 800px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.agenda-date-group {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}

.agenda-date-header {
    font-family: var(--font-heading);
    font-size: 1rem;
    font-weight: 700;
    color: var(--primary-light);
    border-bottom: 1px solid var(--border-subtle);
    padding-bottom: 0.4rem;
}

.agenda-item-card {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.85rem 1rem;
    border-radius: var(--radius-md);
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    transition: var(--transition);
    cursor: pointer;
}

.agenda-item-card:hover {
    background: var(--bg-card-hover);
    border-color: var(--border-hover);
    transform: translateY(-1px);
}

.agenda-item-color-bar {
    width: 4px;
    height: 38px;
    border-radius: 2px;
    flex-shrink: 0;
}

.agenda-item-main {
    flex: 1;
}

.agenda-item-title {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-main);
    margin-bottom: 0.2rem;
}

.agenda-item-desc {
    font-size: 0.78rem;
    color: var(--text-muted);
}

.agenda-item-time {
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-muted);
    white-space: nowrap;
}

/* ========================================== */
/* Modals & Dialogs                           */
/* ========================================== */
.modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(6px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.modal-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 520px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.7);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: modalPop 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.modal-card-sm {
    max-width: 420px;
}

.modal-card-detail {
    max-width: 480px;
}

@keyframes modalPop {
    from { transform: scale(0.95); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border-subtle);
}

.modal-header h3 {
    font-family: var(--font-heading);
    font-size: 1.1rem;
    font-weight: 700;
}

.modal-close-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--text-muted);
    cursor: pointer;
    line-height: 1;
}

.modal-close-btn:hover {
    color: var(--text-main);
}

form {
    padding: 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.form-row {
    display: flex;
    gap: 0.75rem;
}

.flex-1 {
    flex: 1;
}

label {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-muted);
}

input[type="text"],
input[type="date"],
input[type="time"],
select,
textarea {
    width: 100%;
    background: var(--bg-surface-elevated);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: 0.55rem 0.75rem;
    color: var(--text-main);
    font-family: var(--font-body);
    font-size: 0.85rem;
    outline: none;
    transition: var(--transition);
}

input:focus,
select:focus,
textarea:focus {
    border-color: var(--primary-light);
    box-shadow: 0 0 0 2px var(--primary-glow);
}

.select-with-badge {
    position: relative;
    display: flex;
    align-items: center;
}

.color-dot-preview {
    position: absolute;
    right: 32px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    pointer-events: none;
}

.form-checkbox-row {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    padding: 0.2rem 0;
}

.custom-checkbox-label {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    cursor: pointer;
    font-size: 0.82rem;
    color: var(--text-main);
}

.color-swatches {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
}

.color-swatch {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid transparent;
    transition: var(--transition);
}

.color-swatch:hover {
    transform: scale(1.15);
}

.color-swatch.active {
    border-color: #fff;
    box-shadow: 0 0 0 2px var(--primary-light);
}

.custom-color-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8rem;
}

.custom-color-row input[type="color"] {
    width: 32px;
    height: 32px;
    border-radius: 6px;
    border: 1px solid var(--border-subtle);
    background: none;
    cursor: pointer;
}

.modal-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.65rem;
    padding-top: 0.75rem;
    border-top: 1px solid var(--border-subtle);
}

/* Detail Popover */
.detail-body {
    padding: 1.25rem;
}

.detail-header-tags {
    display: flex;
    gap: 0.5rem;
}

.cal-pill-badge {
    font-size: 0.72rem;
    font-weight: 600;
    padding: 0.2rem 0.55rem;
    border-radius: var(--radius-full);
    color: #fff;
}

.completed-pill-badge {
    font-size: 0.72rem;
    font-weight: 600;
    padding: 0.2rem 0.55rem;
    border-radius: var(--radius-full);
    background: var(--success);
    color: #fff;
}

.detail-title {
    font-family: var(--font-heading);
    font-size: 1.35rem;
    font-weight: 700;
    margin-bottom: 0.4rem;
}

.detail-time-row {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 1rem;
}

.detail-desc {
    font-size: 0.88rem;
    line-height: 1.5;
    color: var(--text-main);
    background: var(--bg-surface-elevated);
    padding: 0.75rem;
    border-radius: var(--radius-md);
    white-space: pre-wrap;
}

/* Responsive adjustments */
/* App selection bar shrinks to icon-only at 1500px */
@media (max-width: 1500px) {
    .universal-nav-tabs .nav-tab span {
        display: none !important;
    }

    .universal-nav-tabs .nav-tab {
        padding: 5px 9px !important;
        gap: 0 !important;
        justify-content: center !important;
    }

    .universal-nav-tabs .nav-tab-icon {
        width: 15px !important;
        height: 15px !important;
    }
}

@media (max-width: 820px) {
    .calendar-sidebar {
        position: fixed;
        left: -270px;
        top: var(--header-height);
        bottom: 0;
        z-index: 60;
        transition: transform 0.2s ease;
    }

    .calendar-sidebar.open {
        transform: translateX(270px);
    }

    .mobile-toggle-btn {
        display: flex;
    }

    .universal-nav-tabs {
        display: none !important;
    }
}
