:root {
    --primary-color: #007AFF; /* iOS Blue */
    --bg-color: #F2F2F7; /* iOS System Gray 6 */
    --sidebar-bg: rgba(255, 255, 255, 0.8); /* Translucent */
    --card-bg: #FFFFFF;
    --text-color: #000000;
    --secondary-text: #8E8E93; /* iOS Gray */
    --border-color: #C6C6C8; /* iOS Separator */
    --success-color: #34C759; /* iOS Green */
    --danger-color: #FF3B30; /* iOS Red */
    --warning-color: #FF9500; /* iOS Orange */
}

* {
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Helvetica, Arial, sans-serif;
    margin: 0;
    padding: 0;
    display: flex;
    background-color: var(--bg-color);
    color: var(--text-color);
    height: 100vh;
    overflow: hidden;
}

.sidebar {
    width: 260px;
    background-color: var(--sidebar-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    padding: 20px;
    display: flex;
    flex-direction: column;
    border-right: 1px solid rgba(0,0,0,0.1);
}

.logo {
    font-size: 24px;
    font-weight: 700;
    margin-bottom: 30px;
    color: var(--text-color);
    display: flex;
    align-items: center;
    gap: 10px;
    padding-left: 10px;
}

.nav-item {
    padding: 12px 16px;
    margin-bottom: 5px;
    cursor: pointer;
    border-radius: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: var(--text-color);
    transition: background-color 0.2s, transform 0.1s;
    font-weight: 500;
}

.nav-item:active {
    transform: scale(0.98);
}

.nav-item:hover {
    background-color: rgba(0,0,0,0.05);
}

.nav-item.active {
    background-color: rgba(0, 122, 255, 0.15); /* Light Blue */
    color: var(--primary-color);
}

.add-cat-btn {
    background: transparent;
    color: var(--primary-color);
    border: none;
    padding: 12px;
    text-align: left;
    cursor: pointer;
    font-size: 16px;
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 500;
}

.add-cat-btn:hover {
    opacity: 0.7;
}

.main {
    flex: 1;
    padding: 30px 40px;
    overflow-y: auto;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
}

.search-bar {
    padding: 10px 16px;
    border: none;
    border-radius: 10px;
    width: 240px;
    background-color: rgba(118, 118, 128, 0.12); /* iOS Search Bar Fill */
    font-size: 15px;
    color: var(--text-color);
    transition: width 0.3s;
}

.search-bar:focus {
    outline: none;
    background-color: rgba(118, 118, 128, 0.18);
    width: 280px;
}

.add-bookmark-btn {
    background-color: var(--bg-color); /* White/Transparent feel */
    color: var(--primary-color);
    border: none;
    padding: 8px 16px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 16px;
    display: flex;
    align-items: center;
    gap: 5px;
    font-weight: 500;
    transition: opacity 0.2s;
}

.add-bookmark-btn:hover {
    background-color: rgba(0, 122, 255, 0.1);
}

/* Override for primary action buttons to look like iOS filled buttons */
.add-bookmark-btn.primary, 
.modal-actions .add-bookmark-btn {
    background-color: var(--primary-color);
    color: white;
}
.add-bookmark-btn.primary:hover, 
.modal-actions .add-bookmark-btn:hover {
    background-color: #0062cc; /* Darker blue */
}

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 24px;
}

.card {
    background: var(--card-bg);
    border-radius: 16px;
    padding: 16px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.04);
    transition: transform 0.2s, box-shadow 0.2s;
    display: flex;
    flex-direction: column;
    border: 1px solid rgba(0,0,0,0.05);
}

.card:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(0,0,0,0.08);
}

.card.pinned {
    border: 1px solid rgba(0, 122, 255, 0.3);
    background-color: rgba(0, 122, 255, 0.02);
}

.card-header {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
}

.card-header > a {
    min-width: 0; /* Allow flex item to shrink for text-overflow */
}

.card-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 12px;
    object-fit: cover;
    background-color: #f9f9f9;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.card-icon-text {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 12px;
    background-color: var(--primary-color);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    font-weight: bold;
    text-transform: uppercase;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.card-title {
    font-weight: 600;
    font-size: 16px;
    color: var(--text-color);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}

.card-link {
    font-size: 13px;
    color: var(--secondary-text);
    text-decoration: none;
    margin-bottom: 12px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
}

.card-link:hover {
    color: var(--primary-color);
}

.card-actions {
    margin-top: auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    color: var(--secondary-text);
}

.action-btn {
    background: transparent;
    border: none;
    color: var(--primary-color);
    cursor: pointer;
    font-size: 14px;
    margin-left: 12px;
    padding: 4px;
    border-radius: 50%;
    width: 28px;
    height: 28px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s;
}

.action-btn:hover {
    background-color: rgba(0, 122, 255, 0.1);
}

.action-btn i.active-pin {
    color: var(--primary-color);
    transform: rotate(-45deg);
}

.action-btn.delete {
    color: var(--danger-color);
}
.action-btn.delete:hover {
    background-color: rgba(255, 59, 48, 0.1);
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-left: auto;
}
.status-online { background-color: var(--success-color); box-shadow: 0 0 5px var(--success-color); }
.status-offline { background-color: var(--danger-color); }
.status-pending { background-color: var(--warning-color); }

.delete-cat {
    color: var(--secondary-text);
    opacity: 0;
    transition: opacity 0.2s;
}

.nav-item:hover .delete-cat {
    opacity: 1;
}

.delete-cat:hover {
    color: var(--danger-color);
}

.tag-badge {
    display: inline-block;
    background: rgba(142, 142, 147, 0.12);
    color: var(--secondary-text);
    padding: 4px 10px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 500;
    margin-right: 6px;
    margin-bottom: 4px;
    cursor: pointer;
}

.tag-badge:hover {
    background: var(--primary-color);
    color: white;
}

.latency-badge {
    font-size: 11px;
    padding: 2px 6px;
    border-radius: 4px;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-weight: 500;
}

.latency-fast { color: var(--success-color); background: #e6fffa; }
.latency-medium { color: var(--warning-color); background: #fffaf0; }
.latency-slow { color: var(--danger-color); background: #fff5f5; }

/* Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.4);
    z-index: 1000;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    transition: opacity 0.3s;
}

#confirm-modal {
    z-index: 1100; /* Ensure confirmation is always on top of other modals */
}

.modal.active {
    display: flex;
    animation: fadeIn 0.2s ease-out;
}

.modal-content {
    background: white;
    padding: 24px;
    border-radius: 20px;
    width: 90%;
    max-width: 500px;
    box-shadow: 0 20px 40px rgba(0,0,0,0.15);
    transform: scale(0.95);
    opacity: 0;
    animation: scaleIn 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes scaleIn {
    from { transform: scale(0.95); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-title {
    font-size: 17px;
    font-weight: 600;
    margin-bottom: 24px;
    color: var(--text-color);
    text-align: center;
    padding-bottom: 0;
    border-bottom: none;
}

.modal-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

.form-group {
    margin-bottom: 16px;
}

.form-group.full-width {
    grid-column: span 2;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
    font-size: 13px;
    color: #888;
    margin-left: 4px;
}

.form-group input, 
.form-group select, 
.form-group textarea {
    width: 100%;
    padding: 12px 16px;
    background-color: #F2F2F7;
    border: none;
    border-radius: 12px;
    font-size: 16px; /* Prevent iOS zoom */
    color: #000;
    transition: background-color 0.2s;
    box-sizing: border-box;
    -webkit-appearance: none; /* 移除 iOS 默认样式 */
    appearance: none; /* 标准属性，确保兼容性 */
}

.form-group select {
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%238E8E93' viewBox='0 0 16 16'%3E%3Cpath d='M7.247 11.14 2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 40px;
}

.form-group input:focus, 
.form-group select:focus, 
.form-group textarea:focus {
    background-color: #E5E5EA;
    outline: none;
}

.form-group input::placeholder,
.form-group textarea::placeholder {
    color: #C7C7CC;
}

.modal-actions {
    display: flex;
    justify-content: space-between; /* Spread buttons */
    gap: 16px;
    margin-top: 32px;
    padding-top: 0;
    border-top: none;
}

.btn-secondary {
    background: #F2F2F7;
    color: var(--primary-color);
    border: none;
    padding: 14px 20px;
    border-radius: 12px;
    cursor: pointer;
    font-size: 16px;
    font-weight: 500;
    flex: 1; /* Equal width buttons */
    transition: background-color 0.2s;
}

.btn-secondary:active {
    background-color: #E5E5EA;
    opacity: 0.7;
}

/* Primary button style in modal */
.add-bookmark-btn.primary, 
.modal-actions .add-bookmark-btn {
    background-color: var(--primary-color);
    color: white;
    padding: 14px 20px;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 600;
    flex: 1; /* Equal width buttons */
    justify-content: center;
    transition: opacity 0.2s;
}

.modal-actions .add-bookmark-btn:active {
    opacity: 0.8;
}

/* Toast Notifications */
#toast-container {
    position: fixed;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 3000;
    display: flex;
    flex-direction: column;
    gap: 10px;
    pointer-events: none; /* Allow clicking through container */
}

.toast {
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 10px 20px;
    border-radius: 25px;
    font-size: 14px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
    display: flex;
    align-items: center;
    gap: 10px;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    animation: slideUpFade 0.3s ease-out forwards;
    pointer-events: auto; /* Enable clicks on toasts */
    max-width: 90vw;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.toast.error {
    background: rgba(255, 59, 48, 0.9); /* iOS Red */
}

.toast.success {
    background: rgba(52, 199, 89, 0.9); /* iOS Green */
}

.toast i {
    font-size: 16px;
}

@keyframes slideUpFade {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes fadeOut {
    from {
        opacity: 1;
        transform: translateY(0);
    }
    to {
        opacity: 0;
        transform: translateY(-10px);
    }
}

/* Mobile FAB Button */
.fab-btn {
    display: none;
}

/* Mobile Styles */
.menu-btn {
    display: none;
    background: transparent;
    border: none;
    font-size: 20px;
    color: var(--primary-color);
    cursor: pointer;
    margin-right: 15px;
    padding: 5px;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.3);
    z-index: 900;
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
}

@media (max-width: 768px) {
    body {
        /* Prevent elastic scrolling on body */
        overscroll-behavior-y: none;
    }

    .menu-btn {
        display: block;
        padding: 10px; /* Larger touch target */
    }

    .sidebar {
        position: fixed;
        left: -280px;
        top: 0;
        bottom: 0;
        z-index: 1000;
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        transform: translateX(0);
        width: 260px;
        box-shadow: 2px 0 10px rgba(0,0,0,0.1);
        padding-top: calc(20px + env(safe-area-inset-top));
        padding-bottom: calc(20px + env(safe-area-inset-bottom));
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .sidebar.active {
        transform: translateX(280px);
        left: -280px; /* Keep base position */
    }

    .header {
        flex-wrap: wrap;
        gap: 10px;
        margin-bottom: 20px;
        position: fixed; /* Stick to top */
        top: 0;
        left: 0;
        right: 0;
        background: rgba(242, 242, 247, 0.95); /* Semi-transparent bg */
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        z-index: 90;
        padding-bottom: 10px;
        margin-left: 0;
        margin-right: 0;
        padding-left: 15px;
        padding-right: 15px;
        padding-top: calc(10px + env(safe-area-inset-top)); /* Adjust for status bar */
        border-bottom: 1px solid rgba(0,0,0,0.05);
    }

    .main {
        padding: 15px;
        /* Increase top padding to account for fixed header (approx 110px + safe area) */
        padding-top: calc(120px + env(safe-area-inset-top)); 
        padding-bottom: 80px; /* Space for FAB */
        width: 100%;
        -webkit-overflow-scrolling: touch;
    }

    .header h2 {
        font-size: 18px;
        margin: 0;
        flex: 1;
        white-space: nowrap;
        text-align: center; /* Center title like iOS */
        padding-right: 44px; /* Balance menu btn space */
    }

    .header > div {
        width: 100%;
        display: flex;
        gap: 10px;
    }

    .search-bar {
        width: 100%;
        flex: 1;
        height: 36px; /* Standard iOS search height */
        font-size: 16px; /* Prevent zoom */
        background-color: #E3E3E8;
        border-radius: 10px;
        text-align: center; /* Center text initially */
    }
    
    .search-bar:focus {
        width: 100%;
        background-color: #fff;
        text-align: left;
        padding-left: 30px; /* Simulate search icon space if we had one */
    }

    /* Hide desktop add button in header */
    .header .add-bookmark-btn {
        display: none;
    }

    /* Show Mobile FAB */
    .fab-btn {
        display: flex;
        position: fixed;
        bottom: 30px;
        right: 30px;
        width: 56px;
        height: 56px;
        border-radius: 50%;
        background-color: var(--primary-color);
        color: white;
        box-shadow: 0 4px 15px rgba(0, 122, 255, 0.4);
        z-index: 2000;
        justify-content: center;
        align-items: center;
        padding: 0;
        font-size: 24px;
        border: none;
        cursor: pointer;
        margin: 0;
    }

    .fab-btn:active {
        transform: scale(0.95);
    }

    .grid {
        grid-template-columns: 1fr; /* Single column */
        gap: 15px;
    }

    .card {
        border-radius: 12px; /* Slightly smaller radius */
        box-shadow: 0 1px 3px rgba(0,0,0,0.05); /* Lighter shadow */
        max-width: 100%; /* Ensure card doesn't exceed grid cell */
        overflow: hidden; /* Prevent inner content overflow */
    }

    .modal-content {
        width: 90%;
        padding: 20px;
        max-height: 80vh;
        overflow-y: auto;
        padding-bottom: calc(20px + env(safe-area-inset-bottom));
        border-radius: 20px; /* iOS rounded bottom sheet style */
    }

    .modal-grid {
        grid-template-columns: 1fr; /* Stack form fields */
    }
    
    .form-group.full-width {
        grid-column: span 1;
    }
    
    .form-group input, 
    .form-group select, 
    .form-group textarea {
        font-size: 16px; /* Prevent zoom on focus */
        padding: 12px; /* Larger tap area */
        background-color: #F2F2F7;
        border: none;
    }

    .card-actions {
        flex-direction: column; /* Stack date/latency and buttons */
        align-items: stretch; /* Full width */
        gap: 15px;
        margin-top: 15px;
        padding-top: 15px;
        border-top: 1px solid rgba(0,0,0,0.05);
    }
    
    .card-actions > div:first-child {
        justify-content: space-between; /* Spread date and latency */
        width: 100%;
    }

    .card-actions > div:last-child {
        display: flex;
        justify-content: space-between; /* Distribute buttons evenly */
        width: 100%;
        gap: 0; /* Remove gap, use space-between */
        margin: 0;
    }
    
    .action-btn {
        padding: 8px;
        width: 44px; /* Minimum touch target */
        height: 44px;
        background: rgba(0,0,0,0.03); /* Slight background for better target visibility */
        flex: 1; /* Make buttons grow to fill space if desired, or just space-between */
        max-width: 60px; /* Limit max width so they aren't huge */
        border-radius: 12px; /* Softer rect for touch areas */
        margin: 0 5px; /* Small spacing */
    }
    
    .action-btn:active {
        background: rgba(0,0,0,0.1);
        transform: scale(0.96);
    }
    
    /* Ensure long URLs don't break layout */
    .card-link {
        width: 100%;
        max-width: 100%;
    }

    .card-header > a {
        max-width: calc(100% - 20px); /* Ensure title truncates before hitting status dot */
    }

    .action-btn:first-child { margin-left: 0; }
    .action-btn:last-child { margin-right: 0; }
}

/* iOS Toggle Switch */
.switch {
  position: relative;
  display: inline-block;
  width: 51px;
  height: 31px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #e9e9eb; /* iOS off state color */
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 27px;
  width: 27px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
  box-shadow: 0 2px 4px rgba(0,0,0,0.15);
}

input:checked + .slider {
  background-color: #34C759; /* iOS Green */
}

input:focus + .slider {
  box-shadow: 0 0 1px #34C759;
}

input:checked + .slider:before {
  -webkit-transform: translateX(20px);
  -ms-transform: translateX(20px);
  transform: translateX(20px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 31px;
}

.slider.round:before {
  border-radius: 50%;
}
