/* 1. Default Light Mode */
:root {
    --mac-toolbar-bg: linear-gradient(0deg, #E7E6E7 0%, #D1D0D1 100%);
    --mac-seg-bg: #FFFFFF;
    --mac-seg-divider: #E5E5E6;
    --mac-seg-active-bg: linear-gradient(180deg, #6AB3FA 0%, #087FFF 100%);
    --mac-seg-active-outline: #4DA3F8;
    --mac-text: #000000;
    --mac-text-active: #FFFFFF;
    --mac-shadow: 0px 0.5px 1px rgba(0, 0, 0, 0.27);
    
    /* Search Bar Light */
    --mac-search-bg: linear-gradient(180deg, #FEFEFE 0%, #F1F1F1 100%);
    --mac-search-icon: #505051;
    --mac-search-text: #737372;
}

/* 2. Dark Mode (System Preference) */
@media (prefers-color-scheme: dark) {
    :root {
        --mac-toolbar-bg: linear-gradient(0deg, #303131 0%, #3A3B3C 100%);
        --mac-seg-bg: #606162;
        --mac-seg-divider: #525353;
        --mac-seg-active-bg: linear-gradient(180deg, #326AE3 0%, #2B5DC6 100%);
        --mac-seg-active-outline: #3675E1;
        --mac-text: #EEEEEE;
        --mac-search-bg: linear-gradient(180deg, #454647 0%, #3D3E3F 100%);
    }
}

[data-theme="light"] {
    --mac-toolbar-bg: linear-gradient(0deg, #E7E6E7 0%, #D1D0D1 100%);
    --mac-seg-bg: #FFFFFF;
    --mac-seg-divider: #E5E5E6;
    --mac-seg-active-bg: linear-gradient(180deg, #6AB3FA 0%, #087FFF 100%);
    --mac-seg-active-outline: #4DA3F8;
    --mac-text: #000000;
    --mac-text-active: #FFFFFF;
    --mac-shadow: 0px 0.5px 1px rgba(0, 0, 0, 0.27);
    
    /* Search Bar Light */
    --mac-search-bg: linear-gradient(180deg, #FEFEFE 0%, #F1F1F1 100%);
    --mac-search-icon: #505051;
    --mac-search-text: #737372;
}

[data-theme="dark"] {
    --mac-toolbar-bg: linear-gradient(0deg, #303131 0%, #3A3B3C 100%);
    --mac-seg-bg: #606162;
    --mac-seg-divider: #525353;
    --mac-seg-active-bg: linear-gradient(180deg, #326AE3 0%, #2B5DC6 100%);
    --mac-seg-active-outline: #3675E1;
    --mac-text: #EEEEEE;
    --mac-search-bg: linear-gradient(180deg, #454647 0%, #3D3E3F 100%);
}

/* Toolbar Main Container */
.mac-toolbar-container {
    background: var(--mac-toolbar-bg);
    padding: 10px 16px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.2);
    display: flex;
    align-items: center;
    gap: 16px;
}

/* Search Wrapper */
.mac-search-wrapper {
    background: var(--mac-search-bg);
    border: 0.5px solid var(--mac-search-border);
    border-radius: 5px;
    display: flex;
    align-items: center;
    padding: 3px 8px;
    box-shadow: inset 0 0.5px 1px rgba(0,0,0,0.05);
    width: 220px;
}

.mac-search-icon {
    color: var(--mac-search-icon);
    width: 14px;
    height: 14px;
    display: flex;
    align-items: center;
}

.mac-search-wrapper input {
    background: transparent;
    border: none;
    outline: none;
    font-size: 13px;
    color: var(--mac-search-text);
    padding-left: 6px;
    width: 100%;
}

.mac-search-wrapper input::placeholder {
    color: var(--mac-search-text);
    opacity: 0.7;
}

/* Segmented Control */
.mac-segmented-control {
    display: flex;
    background: var(--mac-seg-bg);
    border-radius: 4px;
    padding: 1px;
    box-shadow: var(--mac-shadow);
}

.seg-item {
    padding: 2px 14px;
    font-size: 13px;
    color: var(--mac-text);
    cursor: pointer;
    position: relative;
    border-radius: 3px;
}

/* Divider visibility logic */
.seg-item:not(:last-child):not(.active):not(:has(+ .active))::after {
    content: "";
    position: absolute;
    right: 0;
    top: 20%;
    height: 60%;
    width: 1px;
    background: var(--mac-seg-divider);
}

.seg-item.active {
    background: var(--mac-seg-active-bg);
    color: var(--mac-text-active);
    outline: 0.5px solid var(--mac-seg-active-outline);
    box-shadow: var(--mac-shadow);
    z-index: 1;
}

/* --- 1. Layout Fix: Single Line Desktop --- */
.mac-toolbar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between; /* Spreads items out */
    gap: 12px;
    width: 100%;
}

/* --- 2. Radio Detection Fix --- */
/* Hide the radio but use its state to style the label */
.mac-segmented-control input[type="radio"] {
    display: none;
}

/* Use the + selector to find the label immediately after the checked radio */
.mac-segmented-control input[type="radio"]:checked + .seg-item {
    background: var(--mac-seg-active-bg);
    color: var(--mac-text-active);
    outline: 0.5px solid var(--mac-seg-active-outline);
    box-shadow: var(--mac-shadow);
    z-index: 1;
    border-radius: 0;
}

.mac-segmented-control input[type="radio"]:checked + .seg-item:first-of-type {
    border-top-left-radius: 4px;
    border-bottom-left-radius: 4px;
}

/* 3. If the active item is the LAST label: Round Right Only */
.mac-segmented-control input[type="radio"]:checked + .seg-item:last-of-type {
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
}
/* --- 3. Divider Fix: Longer height --- */
/* Hide divider if current radio is checked OR the next radio is checked */
.mac-segmented-control input[type="radio"]:not(:checked) + .seg-item:not(:last-child):not(:has(+ input:checked))::after {
    content: "";
    position: absolute;
    right: 0;
    top: 10%;      /* Centered */
    height: 80%;   /* Increased from 60% to 80% */
    width: 1px;
    background: var(--mac-seg-divider);
}

/* --- 4. Select Dropdown Styling --- */
.mac-select {
    appearance: none; /* Kill OS default */
    -webkit-appearance: none;
    background: var(--mac-seg-bg);
    color: var(--mac-text);
    border: 0.5px solid var(--mac-seg-divider);
    border-radius: 5px;
    padding: 2px 24px 2px 10px;
    font-size: 13px;
    box-shadow: var(--mac-shadow);
    cursor: pointer;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 24 24' fill='none' stroke='%23737372' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M7 15l5 5 5-5M7 9l5-5 5 5'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 8px center;
}

.mac-select:focus {
    outline: 0.5px solid var(--mac-seg-active-outline);
}

/* --- 5. Mobile Responsiveness & Layout Fixes --- */

/* Default Desktop: Items stay in a single row */
.mac-toolbar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    width: 100%;
    flex-wrap: nowrap;
}

/* Mobile: Two-line layout */
@media (max-width: 768px) {
    .mac-toolbar-inner {
        flex-direction: column; /* Stack vertically */
        gap: 10px;
    }

    /* Row 1: Search and Sort */
    /* We create a sub-row for Search and Sort to sit side-by-side on top */
    .mac-search-wrapper {
        order: 1; 
        flex: 1;
        width: 100%; /* Take full width or adjust as needed */
    }

    .mac-sort-wrapper {
        order: 2;
    }

    /* Row 2: Segmented Control */
    .mac-segmented-control {
        order: 3;
        width: 100%;
        justify-content: space-between; /* Spread items on mobile */
        overflow-x: auto; /* Allow scrolling if too many items */
    }
    
    .seg-item {
        flex: 1;
        text-align: center;
        padding: 4px 8px;
    }

    /* Custom container for the top row on mobile */
    .mobile-top-row {
        display: flex;
        width: 100%;
        gap: 8px;
        align-items: center;
    }
}
