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

:root {
    --color-bg:             #ffffff;
    --color-bg-2:           #f8f9fa;
    --color-bg-3:           #f1f3f5;
    --color-border:         #e2e8f0;
    --color-border-subtle:  #f0f2f5;
    --color-text:           #1a202c;
    --color-text-2:         #64748b;
    --color-text-3:         #94a3b8;
    --color-primary:        #3b82f6;
    --color-primary-hover:  #2563eb;
    --color-primary-muted:  #eff6ff;
    --color-today:          #ef4444;
    --shadow-sm:  0 1px 3px rgba(0,0,0,0.07), 0 1px 2px rgba(0,0,0,0.04);
    --shadow-md:  0 4px 12px rgba(0,0,0,0.09), 0 2px 4px rgba(0,0,0,0.05);
    --shadow-lg:  0 12px 32px rgba(0,0,0,0.12), 0 4px 8px rgba(0,0,0,0.06);
    --radius-sm:  4px;
    --radius-md:  8px;
    --radius-lg:  14px;
    --header-h:   60px;
    --font:       -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        --color-bg:             #0f1117;
        --color-bg-2:           #181c27;
        --color-bg-3:           #1e2336;
        --color-border:         #2a3047;
        --color-border-subtle:  #1e2336;
        --color-text:           #e2e8f0;
        --color-text-2:         #8899b8;
        --color-text-3:         #556070;
        --color-primary:        #60a5fa;
        --color-primary-hover:  #93c5fd;
        --color-primary-muted:  #0f1f36;
        --color-today:          #f87171;
        --shadow-sm:  0 1px 3px rgba(0,0,0,0.3);
        --shadow-md:  0 4px 12px rgba(0,0,0,0.4);
        --shadow-lg:  0 12px 32px rgba(0,0,0,0.55);
    }
}

html, body {
    height: 100%;
    font-family: var(--font);
    background: var(--color-bg);
    color: var(--color-text);
    -webkit-font-smoothing: antialiased;
    overflow: hidden;
}

body {
    display: flex;
    flex-direction: column;
}

/* ─── HEADER ─────────────────────────────────────────── */

.app-header {
    position: sticky;
    top: 0;
    z-index: 100;
    min-height: var(--header-h);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    row-gap: 8px;
    padding: 10px 20px;
    background: var(--color-bg);
    border-bottom: 1px solid var(--color-border);
    box-shadow: var(--shadow-sm);
    gap: 12px;
}

.app-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.brand-icon {
    width: 22px;
    height: 22px;
    color: var(--color-primary);
}

.app-title {
    font-size: 17px;
    font-weight: 600;
    letter-spacing: -0.3px;
    white-space: nowrap;
}

.last-refreshed {
    font-size: 11px;
    color: var(--color-text-3);
    white-space: nowrap;
    padding: 3px 8px;
    border-radius: 100px;
    background: var(--color-bg-2);
    border: 1px solid var(--color-border);
}
.last-refreshed:empty { display: none; }

.refresh-controls {
    display: flex;
    align-items: center;
    gap: 8px;
}

.refresh-disclaimer {
    font-size: 11px;
    color: var(--color-text-3);
    white-space: nowrap;
}

#refreshNowBtn svg {
    transition: transform 0.4s ease;
}
#refreshNowBtn:disabled {
    opacity: 0.6;
    cursor: default;
}
#refreshNowBtn.is-refreshing svg {
    animation: refresh-spin 0.9s linear infinite;
}
@keyframes refresh-spin {
    to { transform: rotate(360deg); }
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* ─── RESPONSIVE / MOBILE ─────────────────────────────── */

@media (max-width: 720px) {
    .app-header {
        padding: 8px 12px;
    }

    .app-title {
        font-size: 15px;
    }

    .brand-icon {
        width: 19px;
        height: 19px;
    }

    /* Rather than letting controls overflow the viewport unreachably, turn
       the action bar into its own horizontally scrollable strip — the same
       pattern already used for the calendar-source and day-pill rows. */
    .header-actions {
        width: 100%;
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 2px;
    }
    .header-actions::-webkit-scrollbar { height: 3px; }

    .zoom-controls,
    .color-type-legend,
    #previousDayBtn,
    #todayBtn,
    #nextDayBtn {
        flex-shrink: 0;
    }

    .zoom-label {
        min-width: 48px;
        font-size: 10px;
    }

    .btn {
        padding: 6px 10px;
        font-size: 12px;
    }

    .type-pill {
        padding: 4px 8px;
        font-size: 11px;
    }

    .type-group-label {
        padding: 4px 6px 4px 8px;
    }
}

/* ─── BUTTONS ─────────────────────────────────────────── */

.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 13px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-weight: 500;
    font-family: var(--font);
    cursor: pointer;
    white-space: nowrap;
    user-select: none;
    transition: background-color 0.13s, color 0.13s;
}
.btn svg { width: 15px; height: 15px; flex-shrink: 0; }

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

.btn-secondary {
    background: var(--color-bg-3);
    color: var(--color-text);
    border: 1px solid var(--color-border);
}
.btn-secondary:hover { background: var(--color-bg-2); }

.btn-icon {
    padding: 7px;
    background: var(--color-bg-3);
    color: var(--color-text-2);
    border: 1px solid var(--color-border);
}
.btn-icon:hover { background: var(--color-bg-2); color: var(--color-text); }
.btn-icon svg { width: 16px; height: 16px; }

.zoom-controls {
    display: flex;
    align-items: center;
    gap: 4px;
}
.zoom-label {
    font-size: 11px;
    color: var(--color-text-3);
    min-width: 62px;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

/* ─── MAIN ────────────────────────────────────────────── */

.app-main {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* ─── DROP ZONE ───────────────────────────────────────── */

.drop-zone {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 24px;
    border: 2px dashed var(--color-border);
    border-radius: var(--radius-lg);
    cursor: pointer;
    outline: none;
    transition: border-color 0.15s, background-color 0.15s;
}
.drop-zone:hover,
.drop-zone:focus-visible {
    border-color: var(--color-primary);
    background: var(--color-primary-muted);
}
.drop-zone.drag-active {
    border-color: var(--color-primary);
    background: var(--color-primary-muted);
    transform: scale(1.005);
}

.drop-zone-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 40px;
    text-align: center;
    pointer-events: none;
    user-select: none;
}

.drop-icon {
    width: 72px;
    height: 72px;
    color: var(--color-text-3);
    margin-bottom: 6px;
}

.drop-title {
    font-size: 20px;
    font-weight: 600;
    color: var(--color-text);
}
.drop-subtitle {
    font-size: 14px;
    color: var(--color-text-2);
}
.drop-hint {
    font-size: 12px;
    color: var(--color-text-3);
    max-width: 380px;
    line-height: 1.6;
    margin-top: 4px;
}

/* ─── TIMELINE VIEW ───────────────────────────────────── */

.timeline-view {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
}

/* ─── LEGEND ──────────────────────────────────────────── */

.source-legend {
    display: flex;
    align-items: center;
    flex-wrap: nowrap;
    overflow-x: auto;
    gap: 6px;
    padding: 8px 16px;
    border-bottom: 1px solid var(--color-border-subtle);
    flex-shrink: 0;
    background: var(--color-bg);
}

.legend-item {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    padding: 3px 10px;
    background: var(--color-bg-2);
    border: 1px solid var(--color-border);
    border-radius: 100px;
    font-size: 12px;
    font-family: var(--font);
    color: var(--color-text-2);
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
    transition: background-color 0.13s, border-color 0.13s, color 0.13s;
}

.legend-item:hover {
    background: var(--color-bg-3);
    color: var(--color-text);
}

.legend-item--active {
    background: var(--color-primary-muted);
    border-color: var(--color-primary);
    color: var(--color-primary);
}

.legend-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

.legend-label {
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.legend-count {
    font-size: 11px;
    color: var(--color-text-3);
}

/* ─── TIMELINE CONTAINER ──────────────────────────────── */

.timeline-container {
    flex: 1;
    overflow: auto;
    background: var(--color-bg);
    min-height: 0;
    scrollbar-width: thin;
    scrollbar-color: var(--color-border) transparent;
}
.timeline-container::-webkit-scrollbar { height: 7px; width: 7px; }
.timeline-container::-webkit-scrollbar-track { background: transparent; }
.timeline-container::-webkit-scrollbar-thumb {
    background: var(--color-border);
    border-radius: 4px;
}
.timeline-container::-webkit-scrollbar-thumb:hover { background: var(--color-text-3); }

.timeline-inner {
    position: relative;
}

/* ─── RULER ───────────────────────────────────────────── */

.ruler {
    position: sticky;
    top: 0;
    z-index: 20;
    height: 52px;
    background: var(--color-bg);
    border-bottom: 1px solid var(--color-border);
    flex-shrink: 0;
    user-select: none;
}

.ruler-month-line {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 1px;
    background: var(--color-border);
    pointer-events: none;
}

.ruler-month-label {
    position: absolute;
    top: 5px;
    font-size: 13px;
    font-weight: 600;
    color: var(--color-text-2);
    letter-spacing: 0.3px;
    white-space: nowrap;
    pointer-events: none;
    padding-left: 5px;
}

.ruler-day-label {
    position: absolute;
    bottom: 5px;
    font-size: 12px;
    font-weight: 500;
    color: var(--color-text-3);
    pointer-events: none;
    transform: translateX(-50%);
    white-space: nowrap;
}

.ruler-day-label--weekend {
    color: var(--color-text-2);
}

.ruler-day-tick {
    position: absolute;
    bottom: 0;
    width: 1px;
    height: 8px;
    background: var(--color-border-subtle);
    pointer-events: none;
}

/* ─── DAILY SHIFT TALLY ───────────────────────────────── */

.day-tally {
    position: sticky;
    top: 52px; /* directly below the ruler */
    z-index: 19; /* below the ruler (20) and today-marker (25) */
    height: 26px;
    background: var(--color-bg);
    border-bottom: 1px solid var(--color-border-subtle);
    flex-shrink: 0;
    user-select: none;
}

.day-tally-cell {
    position: absolute;
    top: 0;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
}

.day-tally-badge {
    font-size: 10px;
    font-weight: 700;
    padding: 2px 5px;
    border-radius: 4px;
    letter-spacing: 0.2px;
    white-space: nowrap;
    border: 1px solid transparent;
}

.day-tally-badge--good { background: rgba(34, 197, 94, 0.18);  color: #86efac; border-color: rgba(34, 197, 94, 0.35); }
.day-tally-badge--warn { background: rgba(234, 179, 8, 0.20);  color: #fde68a; border-color: rgba(234, 179, 8, 0.4); }
.day-tally-badge--bad  { background: rgba(239, 68, 68, 0.20);  color: #fca5a5; border-color: rgba(239, 68, 68, 0.4); }

/* ─── EVENTS AREA ─────────────────────────────────────── */

.events-area {
    position: relative;
}

/* ─── DAY GRID LINES ──────────────────────────────────── */

.day-lines {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 0;
}

.day-line {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 1px;
    background: var(--color-border-subtle);
}

.day-weekend-band {
    position: absolute;
    top: 0;
    bottom: 0;
    /* A flat, pre-existing surface tone rather than a translucent overlay —
       keeps the band clearly distinct from --color-border-subtle so the
       day-line dividers remain visible inside weekend columns. */
    background: var(--color-bg-2);
    pointer-events: none;
}

/* ─── LEAVE BANDS ─────────────────────────────────────── */

.event-chip--leave {
    background-color: rgba(220, 220, 220, 0.30) !important;
    border: 1px solid rgba(200, 200, 200, 0.5);
}

/* ─── CHIP TIME LABELS ────────────────────────────────── */

.chip-time {
    position: absolute;
    font-size: 9.5px;
    color: var(--color-text-3);
    white-space: nowrap;
    pointer-events: none;
    line-height: 26px;
    height: 26px;
    z-index: 2;
}

.chip-time-start {
    transform: translateX(-100%);
    padding-right: 3px;
    text-align: right;
}

.chip-time-end {
    padding-left: 3px;
}

/* ─── EVENT CHIP ──────────────────────────────────────── */

.event-chip {
    position: absolute;
    height: 26px;
    border-radius: 5px;
    display: flex;
    align-items: center;
    padding: 0 7px;
    cursor: pointer;
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0,0,0,0.18);
    transition: filter 0.1s, transform 0.1s, box-shadow 0.1s;
    outline: none;
    min-width: 8px;
}
.event-chip:hover,
.event-chip:focus-visible {
    filter: brightness(1.12);
    transform: translateY(-1px);
    box-shadow: 0 4px 10px rgba(0,0,0,0.22);
    z-index: 10;
}

.event-chip-label {
    position: relative;
    z-index: 1;
    font-size: 11.5px;
    font-weight: 500;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
    text-shadow: 0 1px 2px rgba(0,0,0,0.25);
    flex-shrink: 0; /* keep the initials fully legible; the qual badge shrinks first */
}

.event-chip-qual-badge {
    position: relative;
    z-index: 1;
    margin-left: auto;
    padding-left: 6px;
    min-width: 0; /* allow text-overflow ellipsis to kick in within a flex item */
    font-size: 10px;
    font-style: italic;
    font-weight: 500;
    color: rgba(255, 255, 255, 0.55);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
}

.event-chip-activity-segment {
    position: absolute;
    top: 0;
    bottom: 0;
    min-width: 2px;
    pointer-events: none;
}

.event-chip-dollar-segment {
    position: absolute;
    top: 0;
    bottom: 0;
    min-width: 2px;
    border-top: 2px solid #22c55e;
    border-bottom: 2px solid #22c55e;
    border-radius: 0;
    box-sizing: border-box;
    pointer-events: none;
    z-index: 2;
}

.event-chip-dollar-segment--start { border-left: 2px solid #22c55e; }
.event-chip-dollar-segment--end { border-right: 2px solid #22c55e; }

/* ─── TODAY MARKER ────────────────────────────────────── */

.today-marker {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 2px;
    background: var(--color-today);
    z-index: 25; /* above the sticky ruler (z-index 20) so the label isn't hidden */
    pointer-events: none;
}
.today-marker-label {
    position: absolute;
    top: 6px;
    left: 5px;
    padding: 1px 5px;
    background: var(--color-today);
    border-radius: 3px;
    font-size: 9.5px;
    font-weight: 700;
    color: #fff;
    letter-spacing: 0.3px;
    white-space: nowrap;
}

/* ─── ADD MORE HINT ───────────────────────────────────── */

.add-more-hint {
    padding: 5px 16px;
    font-size: 11px;
    color: var(--color-text-3);
    text-align: center;
    border-top: 1px solid var(--color-border-subtle);
    flex-shrink: 0;
    background: var(--color-bg);
}

/* ─── EVENT POPUP ─────────────────────────────────────── */

.event-popup {
    position: fixed;
    z-index: 1000;
    width: 300px;
    background: var(--color-bg);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    animation: popup-in 0.14s ease;
}
@keyframes popup-in {
    from { opacity: 0; transform: scale(0.95) translateY(-4px); }
    to   { opacity: 1; transform: scale(1) translateY(0); }
}

.popup-color-stripe { height: 4px; }

.popup-body {
    padding: 14px 16px 16px;
    position: relative;
}

.popup-close {
    position: absolute;
    top: 10px;
    right: 10px;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--color-text-3);
    padding: 4px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    transition: color 0.13s, background 0.13s;
}
.popup-close:hover {
    color: var(--color-text);
    background: var(--color-bg-3);
}
.popup-close svg { width: 16px; height: 16px; }

.popup-title {
    font-size: 14.5px;
    font-weight: 600;
    color: var(--color-text);
    padding-right: 26px;
    line-height: 1.4;
    margin-bottom: 10px;
    word-break: break-word;
}

.popup-meta {
    display: flex;
    flex-direction: column;
    gap: 7px;
}

.popup-row {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    font-size: 12.5px;
    color: var(--color-text-2);
    line-height: 1.5;
}

.popup-row-icon {
    flex-shrink: 0;
    width: 16px;
    height: 16px;
    margin-top: 1px;
    color: var(--color-text-3);
}

.popup-description {
    white-space: pre-line;
    word-break: break-word;
    max-height: 100px;
    overflow-y: auto;
    font-size: 12px;
    color: var(--color-text-2);
}

.popup-source {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: 11px;
    color: var(--color-text-3);
    margin-top: 4px;
    padding-top: 8px;
    border-top: 1px solid var(--color-border-subtle);
}
.popup-source-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    flex-shrink: 0;
}

/* ─── COLOR TYPE LEGEND ───────────────────────────────── */

.color-type-legend {
    display: flex;
    align-items: center;
    gap: 4px;
}

.type-pill {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 4px 9px;
    border: 2px solid transparent;
    border-radius: 100px;
    font-size: 12px;
    font-weight: 600;
    font-family: var(--font);
    color: #fff;
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
    opacity: 0.9;
    transition: opacity 0.13s, border-color 0.13s, transform 0.13s;
}
.type-pill:hover {
    opacity: 1;
    transform: translateY(-1px);
}
.type-pill--active {
    opacity: 1;
    border-color: var(--color-text);
}
.color-type-legend.has-active .type-pill:not(.type-pill--active) {
    opacity: 0.35;
}

.type-pill-group {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    padding: 2px;
    border: 1px solid var(--color-border);
    border-radius: 100px;
    background: var(--color-bg-2);
}

.type-pill--grouped {
    border-color: transparent;
}

.type-group-label {
    padding: 4px 8px 4px 10px;
    border: none;
    background: none;
    font-size: 11px;
    font-weight: 600;
    color: var(--color-text-3);
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
    transition: color 0.13s;
}
.type-group-label:hover {
    color: var(--color-text-2);
}
.type-group-label--active {
    color: var(--color-text);
}

/* ─── URL IMPORT MODAL ────────────────────────────────── */

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 300;
}

.modal-box {
    background: var(--color-bg);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    width: 420px;
    max-width: calc(100vw - 32px);
    position: relative;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 16px 0 18px;
}

.modal-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--color-text);
}

.modal-body {
    padding: 14px 18px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.modal-label {
    font-size: 12.5px;
    font-weight: 500;
    color: var(--color-text-2);
}

.modal-input {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-bg-2);
    color: var(--color-text);
    font-size: 13px;
    font-family: var(--font);
    outline: none;
    transition: border-color 0.15s;
}
.modal-input:focus { border-color: var(--color-primary); }

.modal-hint {
    font-size: 12px;
    color: var(--color-text-3);
    line-height: 1.5;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 4px 18px 16px;
}

/* ─── UTILITY ─────────────────────────────────────────── */

.hidden { display: none !important; }
