:root {
    --sidebar-w: 340px;
    --accent: #2563eb;
    --border: #e2e8f0;
    --a4-w: 210mm;
    --a4-h: 297mm;
    --page-pad: 12mm;
    --name-lines: 2;
    --bio-lines: 3;
}

/* --- VISIBILITY TOGGLES --- */
body.hide-bio .card-bio {
    display: none !important;
}

body.hide-net .card-net {
    display: none !important;
}

body.hide-email .card-field-email {
    display: none !important;
}

body.hide-phone .card-field-phone {
    display: none !important;
}

body.hide-addr .card-field-addr {
    display: none !important;
}

.empty-state {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    color: #cbd5e1;
    font-weight: 800;
    font-size: 24px;
    text-align: center;
    padding: 20px;
    min-height: 500px;
    /* Ensure visibility */
}

body.show-pagenum .page-footer {
    display: block !important;
}

* {
    box-sizing: border-box;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
}

body {
    font-family: 'Inter', system-ui, sans-serif;
    margin: 0;
    display: flex;
    height: 100vh;
    background: #f1f5f9;
    color: #1e293b;
    overflow: hidden;
}

/* --- SIDEBAR --- */
aside {
    width: var(--sidebar-w);
    background: white;
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 100;
    flex-shrink: 0;
}

.sidebar-header {
    padding: 20px;
    border-bottom: 1px solid var(--border);
    position: relative;
}

.app-title {
    margin: 0 0 15px 0;
    font-size: 18px;
    font-weight: 800;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.version-tag {
    font-weight: 400;
    font-size: 11px;
    background: #f1f5f9;
    padding: 2px 6px;
    border-radius: 4px;
    color: #64748b;
}

.search-box {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    margin: 10px 0;
    font-size: 14px;
    outline: none;
}

.search-box:focus {
    border-color: var(--accent);
}

.sort-row {
    display: flex;
    gap: 6px;
    margin-bottom: 8px;
}

.btn-sm {
    flex: 1;
    padding: 7px;
    font-size: 10px;
    cursor: pointer;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-weight: 700;
    text-transform: uppercase;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
}

.btn-sm:hover {
    background: #f8fafc;
    border-color: var(--accent);
    color: var(--accent);
}

/* Network Sections */
.network-section {
    border-bottom: 1px solid #f8fafc;
}

.network-header {
    padding: 10px 15px;
    background: #f8fafc;
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-size: 11px;
    font-weight: 800;
    color: #64748b;
}

.network-content {
    display: none;
}

.network-section.expanded .network-content {
    display: block;
}

/* DISTINCT STYLE FOR "ALL PEOPLE" GROUP */
.network-section.sys-group {
    border-bottom: 4px solid #f1f5f9;
}

.network-section.sys-group .network-header {
    background: #e2e8f0;
    color: #334155;
    padding: 12px 15px;
    font-size: 12px;
}

.net-actions {
    margin-left: auto;
    padding: 4px;
    opacity: 0.5;
    font-size: 14px;
    transition: 0.2s;
}

.net-actions:hover {
    opacity: 1;
    color: var(--accent);
    transform: scale(1.1);
}

.group-count {
    font-weight: 500;
    opacity: 0.7;
    font-size: 10px;
    margin-left: 4px;
}

/* Group Export Menu */
.net-actions-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.export-group-menu {
    position: absolute;
    top: 100%;
    right: 0;
    background: white;
    border: 1px solid var(--border);
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 1000;
    min-width: 150px;
    padding: 5px;
    display: none;
    flex-direction: column;
}

.export-group-menu.active {
    display: flex;
}

.export-group-menu button {
    background: none;
    border: none;
    padding: 8px 12px;
    text-align: left;
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    border-radius: 4px;
    color: #475569;
    display: flex;
    align-items: center;
    gap: 8px;
    white-space: nowrap;
}

.export-group-menu button:hover {
    background: #f1f5f9;
    color: var(--accent);
}

.export-group-menu button span {
    opacity: 0.5;
    font-size: 12px;
}

.export-group-header {
    padding: 8px 12px;
    font-size: 10px;
    font-weight: 800;
    text-transform: uppercase;
    color: #94a3b8;
    border-bottom: 1px solid #f1f5f9;
    margin-bottom: 5px;
    letter-spacing: 0.5px;
}

.person-item {
    display: flex;
    align-items: center;
    padding: 8px 15px 8px 35px;
    cursor: grab;
    border-bottom: 1px solid #f8fafc;
    gap: 10px;
}

.person-item:hover {
    background: #f1f5f9;
}

.thumb {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: #ccc;
    overflow: hidden;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-weight: bold;
    font-size: 10px;
}

.thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.edit-trigger {
    margin-left: auto;
    opacity: 0.4;
    font-size: 14px;
    cursor: pointer;
}

.edit-trigger:hover {
    opacity: 1;
    color: var(--accent);
}

/* Sidebar Footer */
.sidebar-footer {
    padding: 15px;
    border-top: 1px solid var(--border);
    margin-top: auto;
    font-size: 10px;
    color: #94a3b8;
    text-align: center;
}

.sidebar-footer a {
    color: inherit;
    text-decoration: underline;
    cursor: pointer;
}

/* --- MAIN WORKSPACE --- */
main {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px 0;
}

.toolbar {
    width: 95%;
    max-width: 1200px;
    background: white;
    padding: 12px 20px;
    margin-bottom: 25px;
    border-radius: 12px;
    border: 1px solid var(--border);
    display: flex;
    gap: 15px;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 50;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
}

/* --- A4 ENGINE --- */
.page {
    width: var(--a4-w);
    height: var(--a4-h);
    background: white;
    padding: var(--page-pad);
    margin-bottom: 40px;
    display: flex;
    flex-direction: column;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    position: relative;
    overflow: hidden;
    flex-shrink: 0;
}

.page-header {
    margin-bottom: 5mm;
    border-bottom: 2px solid #000;
    padding-bottom: 5px;
    flex-shrink: 0;
}

.page-header h1 {
    margin: 0;
    font-size: 24px;
    text-transform: uppercase;
    line-height: 1;
    color: var(--accent);
}

/* --- DOUBLE PAGE ENGINE --- */
.sheet-2up {
    width: 297mm;
    height: 210mm;
    background: white;
    margin-bottom: 40px;
    display: flex;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    position: relative;
    flex-shrink: 0;
}

.logical-page-wrapper {
    width: 148.5mm;
    height: 210mm;
    position: relative;
    overflow: hidden;
    /* Clip at wrapper boundary to prevent cutoff */
}

.logical-page {
    width: 210mm;
    height: 297mm;
    padding: var(--page-pad);
    transform: scale(0.70707);
    /* A4 to A5 scaling factor */
    transform-origin: top left;
    display: flex;
    flex-direction: column;
}

@media print {
    @page {
        margin: 0;
    }

    body.mode-2up {
        width: 297mm;
    }

    body.mode-2up main {
        width: 297mm;
    }

    .sheet-2up {
        margin: 0;
        box-shadow: none;
        page-break-after: always;
        width: 297mm;
        height: 210mm;
    }

    .logical-page-wrapper {
        width: 148.5mm;
        height: 210mm;
        overflow: hidden;
    }

    .logical-page {
        width: 210mm;
        height: 297mm;
        transform: scale(0.70707);
        transform-origin: top left;
    }
}

.grid-container {
    display: grid;
    gap: 4mm;
    flex: 1;
    width: 100%;
    overflow: hidden;
    align-content: stretch;
}

.cols-1 {
    grid-template-columns: 1fr;
}

.cols-2 {
    grid-template-columns: 1fr 1fr;
}

.cols-3 {
    grid-template-columns: repeat(3, 1fr);
}

.cols-4 {
    grid-template-columns: repeat(4, 1fr);
}

.cols-5 {
    grid-template-columns: repeat(5, 1fr);
    font-size: 0.85em;
}

.cols-6 {
    grid-template-columns: repeat(6, 1fr);
    font-size: 0.75em;
}

/* --- SMART CARDS --- */
.person-card {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px;
    display: flex;
    flex-direction: column;
    background: #fff;
    position: relative;
    overflow: hidden;
    height: 100%;
    cursor: grab;
    transition: transform 0.15s;
}

.person-card:active {
    cursor: grabbing;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
    transform: scale(1.02);
    z-index: 10;
}

/* CARD HOVER EDIT SYSTEM */
.card-overlay {
    position: absolute;
    inset: 0;
    background: rgba(255, 255, 255, 0.8);
    opacity: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.2s;
    pointer-events: none;
    /* Let drag pass through unless button clicked */
}

.person-card:hover .card-overlay {
    opacity: 1;
    pointer-events: auto;
}

.edit-btn-card {
    background: var(--accent);
    color: white;
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 700;
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    transform: translateY(10px);
    transition: 0.2s;
}

.person-card:hover .edit-btn-card {
    transform: translateY(0);
}

.card-photo {
    /* Dynamic Scaling Logic */
    width: 100%;
    aspect-ratio: 1 / 1;
    align-self: center;

    /* Flexibility: Grow to fill space, but allow shrinking if text is dense */
    flex: 1 1 auto;
    min-height: 80px;

    border-radius: 8px;
    background: #f1f5f9;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    margin: 0 auto 10px auto;
}

.card-photo img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.card-net {
    font-size: 0.72em;
    font-weight: 800;
    color: var(--accent);
    text-transform: uppercase;
    margin-bottom: 2px;
}

.card-prefix {
    color: #94a3b8;
    font-weight: 500;
    font-size: 0.85em;
    margin-right: 4px;
}

.person-card h3 {
    margin: 0 0 6px 0;
    font-size: 1.15em;
    line-height: 1.25;
    font-weight: 800;
    color: #1e293b;
    display: -webkit-box;
    -webkit-line-clamp: var(--name-lines);
    line-clamp: var(--name-lines);
    -webkit-box-orient: vertical;
    overflow: hidden;
    flex-shrink: 0;
}

.card-bio {
    font-size: 0.85em;
    line-height: 1.5;
    color: #64748b;
    display: -webkit-box;
    -webkit-line-clamp: var(--bio-lines);
    line-clamp: var(--bio-lines);
    -webkit-box-orient: vertical;
    overflow: hidden;
    flex-shrink: 0;
    margin-bottom: 6px;
}

.card-bio:empty,
.card-net:empty {
    display: none !important;
}

.card-contacts {
    border-top: 1px solid #f1f5f9;
    padding-top: 6px;
    margin-top: auto;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
    justify-content: flex-end;
}

.card-contacts:empty {
    display: none !important;
}

.card-field {
    font-size: 0.75em;
    font-weight: 600;
    color: #334155;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 8px;
    line-height: 1.2;
}

.card-field svg {
    width: 12px;
    height: 12px;
    fill: #94a3b8;
    flex-shrink: 0;
}

.page-footer {
    position: absolute;
    bottom: 8mm;
    right: var(--page-pad);
    font-size: 10px;
    color: #94a3b8;
    display: none;
}

/* --- MODAL SYSTEM --- */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.85);
    display: none !important;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    padding: 20px;
}

.modal.active {
    display: flex !important;
}

.modal-content {
    background: white;
    padding: 30px;
    border-radius: 16px;
    width: 100%;
    max-width: 620px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
    position: relative;
    /* For absolute positioning of close button */
}

.btn-close-x {
    position: absolute;
    top: 15px;
    right: 15px;
    width: 32px;
    height: 32px;
    background: #f1f5f9;
    border: none;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    cursor: pointer;
    color: #64748b;
    transition: 0.2s;
    z-index: 100;
    line-height: 1;
}

.btn-close-x:hover {
    background: #e2e8f0;
    color: #ef4444;
}

.modal-sm {
    max-width: 400px;
}

.form-section-title {
    font-size: 11px;
    font-weight: 800;
    color: #94a3b8;
    text-transform: uppercase;
    border-bottom: 1px solid #f1f5f9;
    padding-bottom: 5px;
    margin: 20px 0 12px 0;
}

.form-group {
    margin-bottom: 12px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.form-group label {
    font-size: 11px;
    font-weight: 700;
    color: #64748b;
}

.form-group input,
.form-group textarea,
.form-group select {
    padding: 10px;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    font-size: 14px;
    background: white;
    outline-color: var(--accent);
}

.big-btn {
    width: 100%;
    padding: 15px;
    border: 1px solid var(--border);
    background: white;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    text-align: left;
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 10px;
    transition: 0.2s;
}

.big-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: #f8fafc;
}

#crop-container {
    width: 100%;
    height: 300px;
    background: #000;
    position: relative;
    overflow: hidden;
    border-radius: 12px;
    cursor: move;
}

#crop-canvas {
    position: absolute;
    top: 0;
    left: 0;
}

.crop-guide {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 220px;
    height: 220px;
    transform: translate(-50%, -50%);
    border: 2px solid white;
    box-shadow: 0 0 0 1000px rgba(0, 0, 0, 0.6);
    pointer-events: none;
}

@media print {

    aside,
    .toolbar,
    .no-print,
    .card-overlay,
    .edit-btn-card {
        display: none !important;
    }

    body {
        background: white;
        overflow: visible;
    }

    main {
        padding: 0;
        overflow: visible;
        display: block;
    }

    .page {
        margin: 0;
        box-shadow: none;
        border: none;
        height: var(--a4-h);
        page-break-after: always;
        overflow: hidden;
    }
}

/* --- RESPONSIVE MOBILE STYLES --- */
@media screen and (max-width: 768px) {

    /* Layout Reset */
    body {
        flex-direction: column;
    }

    /* Sidebar Drawer Transformation */
    aside {
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
        width: 85%;
        max-width: 320px;
        transform: translateX(-100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
        border-right: none;
    }

    aside.active {
        transform: translateX(0);
    }

    .export-group-menu {
        right: 0;
        left: auto;
        min-width: 220px;
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
        border: 1px solid #cbd5e1;
    }

    .export-group-menu button {
        padding: 12px 16px;
        font-size: 13px;
    }

    /* Mobile Overlay */
    .sidebar-overlay {
        position: fixed;
        inset: 0;
        background: rgba(15, 23, 42, 0.6);
        backdrop-filter: blur(2px);
        z-index: 90;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.3s;
    }

    .sidebar-overlay.active {
        opacity: 1;
        pointer-events: auto;
    }

    /* Main Content Adjustments */
    main {
        width: 100%;
        display: block;
        /* Override flex */
        overflow-x: auto;
        /* Enable horizontal scroll for A4 */
        padding: 10px;
        /* Reduced padding */
    }

    #page-container {
        width: fit-content;
        margin: 0 auto;
    }

    .toolbar {
        width: 100%;
        max-width: none;
        position: sticky;
        top: 10px;
        margin-bottom: 20px;
        padding: 8px 12px;
        /* Ensure toolbar stays on top of scrolled content but below sidebar */
        z-index: 80;
    }

    /* Button Visibility */
    .mobile-only {
        display: block !important;
    }

    .desktop-text {
        display: none;
    }

    /* Modal Mobile Optimization */
    .modal {
        padding: 10px;
        align-items: center;
    }

    .modal-content {
        max-height: 85vh;
        padding: 20px;
        border-radius: 12px;
        width: 100%;
    }

    /* Fine-tune Page for Mobile View */
    .page {
        margin-bottom: 20px;
        /* Keep A4 specific dimensions */
        transform-origin: top left;
        margin-left: auto;
        margin-right: auto;
    }

    /* Tutorial Driver Fixes for Mobile */
    .driver-popover.driverjs-theme {
        max-width: 90vw !important;
        left: 5vw !important;
        right: 5vw !important;
        position: fixed !important;
    }
}

/* UPDATE NOTIFICATION */
#update-toast {
    position: fixed;
    bottom: 20px;
    right: 20px;
    background: #1e293b;
    color: white;
    padding: 16px 20px;
    border-radius: 12px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
    display: none;
    align-items: center;
    gap: 15px;
    z-index: 9999;
    transform: translateY(100px);
    transition: 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    border-left: 4px solid var(--accent);
}

#update-toast.active {
    display: flex;
    transform: translateY(0);
}

#update-toast button {
    background: var(--accent);
    color: white;
    border: none;
    padding: 6px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 700;
    font-size: 11px;
    text-transform: uppercase;
}