        * { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

        :root {
            --safe-top: env(safe-area-inset-top, 0px);
            --safe-bottom: env(safe-area-inset-bottom, 0px);
            --safe-left: env(safe-area-inset-left, 0px);
            --safe-right: env(safe-area-inset-right, 0px);
            --header-height: 64px;
            --radius-sm: 8px;
            --radius-md: 12px;
            --radius-lg: 14px;
            --blue: #2563eb;
            --blue-dark: #1e40af;
            --green: #10b981;
            --red: #b91c1c;
            --red-light: #fee2e2;
            --gray-50: #f9fafb;
            --gray-100: #f3f4f6;
            --gray-200: #e5e7eb;
            --gray-300: #d1d5db;
            --gray-400: #9ca3af;
            --gray-500: #6b7280;
            --gray-700: #374151;
            --gray-900: #1f2937;
        }

        html {
            height: -webkit-fill-available;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, system-ui, sans-serif;
            background: var(--gray-100);
            color: var(--gray-900);
            min-height: 100vh;
            min-height: -webkit-fill-available;
            padding: 0 0 calc(20px + var(--safe-bottom)) 0;
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
            overscroll-behavior-y: contain;
        }

        .app-container {
            max-width: 700px;
            margin: 0 auto;
            padding: calc(var(--safe-top)) 0 calc(20px + var(--safe-bottom)) 0;
        }

        /* Header / Sticky */
        .app-header {
            position: sticky;
            top: 0;
            z-index: 100;
            background: linear-gradient(135deg, var(--blue), var(--blue-dark));
            color: white;
            padding: calc(12px + var(--safe-top)) 20px 12px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-radius: 0 0 var(--radius-md) var(--radius-md);
            margin: 0 0 20px 0;
            box-shadow: 0 2px 12px rgba(37, 99, 235, 0.25);
        }

        .app-header h1 {
            font-size: 1.2rem;
            font-weight: 600;
            letter-spacing: -0.3px;
        }

        .menu-btn {
            background: rgba(255, 255, 255, 0.2);
            border: none;
            color: white;
            font-size: 1.6rem;
            cursor: pointer;
            width: 44px;
            height: 44px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: background 0.15s;
            touch-action: manipulation;
            flex-shrink: 0;
        }

        .menu-btn:active {
            background: rgba(255, 255, 255, 0.35);
            transform: scale(0.92);
        }

        /* Dropdown Menu (overlay) */
        .dropdown-menu {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.45);
            z-index: 1000;
            display: none;
            align-items: flex-start;
            justify-content: flex-end;
            overscroll-behavior: contain;
            backdrop-filter: blur(2px);
            -webkit-backdrop-filter: blur(2px);
        }

        .dropdown-menu.active {
            display: flex;
        }

        .dropdown-content {
            background: white;
            width: 280px;
            max-width: 85%;
            height: 100%;
            padding: calc(20px + var(--safe-top)) 0 calc(20px + var(--safe-bottom)) 0;
            box-shadow: -4px 0 24px rgba(0, 0, 0, 0.12);
            animation: slideIn 0.25s cubic-bezier(0.32, 0.72, 0, 1);
            overflow-y: auto;
        }

        @keyframes slideIn {
            from { transform: translateX(100%); }
            to { transform: translateX(0); }
        }

        .menu-header {
            padding: 8px 20px;
            font-size: 0.7rem;
            color: var(--gray-500);
            letter-spacing: 1.2px;
            font-weight: 600;
        }

        .menu-item {
            display: flex;
            align-items: center;
            gap: 14px;
            padding: 16px 20px;
            border: none;
            background: none;
            width: 100%;
            text-align: left;
            font-size: 1rem;
            font-weight: 500;
            color: var(--gray-900);
            cursor: pointer;
            transition: background 0.1s;
            touch-action: manipulation;
        }

        .menu-item:active {
            background: var(--gray-100);
        }

        .divider {
            height: 1px;
            background: var(--gray-200);
            margin: 12px 0;
        }

        /* Tabs */
        .tab-content {
            display: none;
            padding: 0 16px;
        }

        .tab-content.active {
            display: block;
        }

        /* Form Card */
        .form-card {
            background: white;
            border-radius: var(--radius-lg);
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.04);
        }

        .form-card h2 {
            font-size: 1.2rem;
            margin-bottom: 16px;
            color: var(--blue);
            font-weight: 600;
        }

        form label {
            display: block;
            margin: 14px 0 4px 0;
            font-weight: 500;
            font-size: 0.8rem;
            color: var(--gray-500);
        }

        form input,
        form select {
            width: 100%;
            padding: 14px 16px;
            border: 1.5px solid var(--gray-200);
            border-radius: var(--radius-sm);
            font-size: 1rem;
            background: var(--gray-50);
            transition: border-color 0.15s, box-shadow 0.15s;
            -webkit-appearance: none;
            appearance: none;
            color: var(--gray-900);
        }

        form input:focus,
        form select:focus {
            outline: none;
            border-color: var(--blue);
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
            background: white;
        }

        form select {
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%236b7280' stroke-width='2' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 14px center;
            padding-right: 40px;
        }

        .row-fields {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        .row-fields .field-group {
            flex: 1;
            min-width: 100px;
        }

        .form-buttons {
            display: flex;
            gap: 10px;
            margin-top: 20px;
        }

        .btn-primary {
            background: var(--blue);
            color: white;
            border: none;
            padding: 14px 20px;
            border-radius: 40px;
            font-weight: 600;
            font-size: 1rem;
            flex: 1;
            cursor: pointer;
            touch-action: manipulation;
            transition: transform 0.1s, box-shadow 0.15s;
        }

        .btn-primary:active {
            transform: scale(0.97);
            box-shadow: 0 2px 8px rgba(37, 99, 235, 0.3);
        }

        .btn-secondary {
            background: var(--gray-200);
            color: var(--gray-700);
            border: none;
            padding: 14px 20px;
            border-radius: 40px;
            font-weight: 600;
            font-size: 1rem;
            flex: 1;
            cursor: pointer;
            touch-action: manipulation;
            transition: transform 0.1s;
        }

        .btn-secondary:active {
            transform: scale(0.97);
            background: var(--gray-300);
        }

        /* Device List */
        .list-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 10px;
            margin: 24px 0 12px;
        }

        .list-header h2 {
            font-size: 1.1rem;
            font-weight: 600;
            color: var(--gray-900);
        }

        .search-input {
            flex: 1;
            min-width: 160px;
            padding: 12px 16px;
            border-radius: 40px;
            border: 1.5px solid var(--gray-200);
            background: white;
            font-size: 0.95rem;
            -webkit-appearance: none;
            appearance: none;
        }

        .search-input:focus {
            outline: none;
            border-color: var(--blue);
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
        }

        .search-result {
            font-size: 0.85rem;
            color: var(--gray-500);
            margin-top: 4px;
        }

        .devices-list {
            display: flex;
            flex-direction: column;
            gap: 12px;
        }

        .device-card {
            background: white;
            border-radius: var(--radius-md);
            padding: 16px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04), 0 2px 8px rgba(0, 0, 0, 0.04);
            border-left: 5px solid var(--blue);
            transition: transform 0.15s;
        }

        .device-card:active {
            transform: scale(0.99);
        }

        .device-card h3 {
            font-size: 1rem;
            font-weight: 600;
            margin-bottom: 8px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 8px;
            line-height: 1.4;
        }

        .device-badge {
            font-size: 0.65rem;
            background: #eef2ff;
            padding: 4px 10px;
            border-radius: 30px;
            color: var(--blue-dark);
            font-weight: 500;
            white-space: nowrap;
        }

        .device-details {
            font-size: 0.8rem;
            color: var(--gray-500);
            display: flex;
            flex-wrap: wrap;
            gap: 6px;
            margin-top: 8px;
            line-height: 1.5;
        }

        .device-details span {
            background: var(--gray-100);
            padding: 4px 10px;
            border-radius: 6px;
            white-space: nowrap;
        }

        .card-buttons {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 14px;
        }

        .card-btn-left {
            display: flex;
            gap: 10px;
        }

        .edit-btn,
        .clone-btn,
        .delete-btn {
            border: none;
            padding: 10px 18px;
            border-radius: 6px;
            font-size: 0.85rem;
            cursor: pointer;
            touch-action: manipulation;
            font-weight: 500;
            transition: transform 0.1s;
        }

        .edit-btn {
            background: #e0e7ff;
            color: var(--blue-dark);
        }

        .edit-btn:active {
            transform: scale(0.95);
            background: #c7d2fe;
        }

        .clone-suffix { color: var(--gray-400); font-weight: 400; font-size: 0.85rem; }

.clone-btn {
            background: #dbeafe;
            color: var(--blue-dark);
        }

        .clone-btn:active {
            transform: scale(0.95);
            background: #bfdbfe;
        }

        .delete-btn {
            background: var(--red-light);
            color: var(--red);
        }

        .delete-btn:active {
            transform: scale(0.95);
            background: #fecaca;
        }

        .empty-state {
            text-align: center;
            color: var(--gray-500);
            padding: 48px 20px;
            font-size: 0.95rem;
        }

        /* Directory Cards */
        .directory-card {
            background: white;
            border-radius: var(--radius-md);
            padding: 20px;
            margin-bottom: 16px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
        }

        .directory-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 16px;
            flex-wrap: wrap;
            gap: 10px;
        }

        .directory-header h3 {
            color: var(--blue);
            font-size: 1.1rem;
            font-weight: 600;
        }

        .add-btn {
            background: var(--blue);
            color: white;
            border: none;
            padding: 10px 18px;
            border-radius: 6px;
            font-size: 0.85rem;
            font-weight: 500;
            cursor: pointer;
            touch-action: manipulation;
            transition: transform 0.1s;
        }

        .add-btn:active {
            transform: scale(0.95);
        }

        .items-list {
            max-height: 320px;
            overflow-y: auto;
        }

        .list-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 14px 12px;
            border-bottom: 1px solid var(--gray-200);
        }

        .list-item:last-child {
            border-bottom: none;
        }

        .item-name {
            font-size: 0.95rem;
            color: var(--gray-900);
        }

        .item-actions {
            display: flex;
            gap: 8px;
            flex-shrink: 0;
        }

        .edit-item,
        .delete-item,
        .merge-item {
            padding: 8px 14px;
            border: none;
            border-radius: 20px;
            cursor: pointer;
            font-size: 0.8rem;
            touch-action: manipulation;
            transition: transform 0.1s;
        }

        .edit-item:active,
        .delete-item:active,
        .merge-item:active {
            transform: scale(0.9);
        }

        .edit-item {
            background: #e0e7ff;
            color: var(--blue-dark);
        }

        .delete-item {
            background: var(--red-light);
            color: var(--red);
        }

        .merge-item {
            background: #e0f2fe;
            color: #0369a1;
        }

        .add-form {
            display: flex;
            gap: 8px;
            margin-top: 16px;
            flex-wrap: wrap;
        }

        .add-form input {
            flex: 1;
            min-width: 120px;
            padding: 12px 16px;
            border: 1.5px solid var(--gray-300);
            border-radius: 40px;
            font-size: 0.9rem;
            -webkit-appearance: none;
        }

        .add-form input:focus {
            outline: none;
            border-color: var(--blue);
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
        }

        .add-form button {
            padding: 12px 20px;
            background: var(--green);
            color: white;
            border: none;
            border-radius: 40px;
            cursor: pointer;
            font-weight: 500;
            touch-action: manipulation;
        }

        .add-form button:active {
            transform: scale(0.95);
        }

        /* Pairs */
        .pair-card {
            background: linear-gradient(135deg, #fef3c7, #fde68a);
            border-left: 5px solid #f59e0b;
            margin-bottom: 16px;
        }

        .pair-card h4 {
            color: #92400e;
            margin-bottom: 8px;
            font-weight: 600;
        }

        /* Modal / Bottom Sheet */
        .modal-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.45);
            display: flex;
            align-items: flex-end;
            justify-content: center;
            z-index: 1100;
            animation: fadeIn 0.2s ease;
            backdrop-filter: blur(2px);
            -webkit-backdrop-filter: blur(2px);
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        .modal-sheet {
            background: white;
            border-radius: var(--radius-lg) var(--radius-lg) 0 0;
            padding: 24px 24px calc(24px + var(--safe-bottom)) 24px;
            max-width: 100%;
            width: 500px;
            animation: slideUp 0.3s cubic-bezier(0.32, 0.72, 0, 1);
            max-height: 85vh;
            overflow-y: auto;
        }

        @keyframes slideUp {
            from { transform: translateY(100%); }
            to { transform: translateY(0); }
        }

        .modal-handle {
            width: 40px;
            height: 4px;
            background: var(--gray-300);
            border-radius: 4px;
            margin: -8px auto 16px auto;
        }

        .modal-sheet h3 {
            margin-bottom: 16px;
            color: var(--blue);
            font-size: 1.2rem;
        }

        .email-input {
            width: 100%;
            padding: 14px 16px;
            border: 1.5px solid var(--gray-200);
            border-radius: var(--radius-sm);
            margin: 12px 0;
            font-size: 1rem;
            -webkit-appearance: none;
        }

        .email-input:focus {
            outline: none;
            border-color: var(--blue);
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
        }

        /* Model directory photo thumb */
.model-photo-thumb { width:40px; height:40px; border-radius:8px; object-fit:cover; vertical-align:middle; margin-right:8px; background:var(--gray-100); }

/* Photo */
        .photo-section { margin: 16px 0 8px; }
        .photo-preview { position: relative; display: inline-block; max-width: 100%; border-radius: var(--radius-sm); overflow: hidden; margin-top: 8px; }
        .photo-preview img { display: block; max-width: 100%; max-height: 200px; border-radius: var(--radius-sm); object-fit: contain; background: var(--gray-100); }
        .photo-preview .remove-photo { position: absolute; top: 4px; right: 4px; background: rgba(0,0,0,0.5); color: white; border: none; width: 32px; height: 32px; border-radius: 50%; font-size: 1.1rem; cursor: pointer; display: flex; align-items: center; justify-content: center; touch-action: manipulation; }
        .photo-btn { background: var(--gray-100); border: 1.5px dashed var(--gray-300); color: var(--gray-500); padding: 10px 18px; border-radius: var(--radius-sm); cursor: pointer; font-size: 0.9rem; display: inline-flex; align-items: center; gap: 8px; touch-action: manipulation; transition: border-color 0.15s; }
        .photo-btn:active { border-color: var(--blue); background: #eef2ff; }
        .card-photo { margin: 8px 0 0; cursor: pointer; border-radius: var(--radius-sm); overflow: hidden; }
        .card-photo img { display: block; max-height: 96px; object-fit: cover; border-radius: var(--radius-sm); background: var(--gray-100); }
        .photo-modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.85); z-index: 1200; display: flex; align-items: center; justify-content: center; cursor: pointer; animation: fadeIn 0.2s ease; }
        .photo-modal img { max-width: 95%; max-height: 90%; border-radius: 8px; object-fit: contain; }

        /* Conflict resolution */
        .conflict-modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 1300; display: flex; align-items: center; justify-content: center; padding: 16px; }
        .conflict-content { background: white; border-radius: var(--radius-lg); padding: 24px; max-width: 600px; width: 100%; max-height: 90vh; overflow-y: auto; animation: fadeIn 0.2s ease; }
        .conflict-content h3 { color: #92400e; margin-bottom: 12px; }
        .conflict-table { width: 100%; border-collapse: collapse; margin: 12px 0; font-size: 0.85rem; }
        .conflict-table th { text-align: left; padding: 6px 8px; background: var(--gray-100); font-weight: 600; color: var(--gray-700); }
        .conflict-table td { padding: 6px 8px; border-bottom: 1px solid var(--gray-200); vertical-align: top; }
        .conflict-table .diff { background: #fef3c7; font-weight: 600; }
        .conflict-table .field-name { color: var(--gray-500); white-space: nowrap; }
        .conflict-actions { display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; }
        .conflict-actions .btn-primary { flex: 0 1 auto; padding: 12px 20px; }
        .conflict-actions .btn-secondary { flex: 0 1 auto; padding: 12px 20px; }
        .conflict-counter { font-size: 0.8rem; color: var(--gray-500); text-align: center; margin-bottom: 8px; }
        .conflict-badge { display: inline-block; background: #fef3c7; color: #92400e; padding: 2px 8px; border-radius: 12px; font-size: 0.7rem; font-weight: 600; }

        /* Utility */
        .hidden {
            display: none !important;
        }

        /* Scrollbar */
        ::-webkit-scrollbar {
            width: 4px;
        }

        ::-webkit-scrollbar-track {
            background: transparent;
        }

        ::-webkit-scrollbar-thumb {
            background: var(--gray-300);
            border-radius: 4px;
        }

        /* Reduced motion */
        @media (prefers-reduced-motion: reduce) {
            *,
            *::before,
            *::after {
                animation-duration: 0.01ms !important;
                transition-duration: 0.01ms !important;
            }
        }

        /* Toast notification */
        .toast {
            position: fixed;
            bottom: calc(24px + var(--safe-bottom));
            left: 50%;
            transform: translateX(-50%) translateY(100px);
            background: #065f46;
            color: white;
            padding: 14px 28px;
            border-radius: var(--radius-md);
            font-size: 0.95rem;
            font-weight: 500;
            box-shadow: 0 4px 20px rgba(0,0,0,0.15);
            z-index: 9999;
            opacity: 0;
            transition: all 0.3s cubic-bezier(0.32, 0.72, 0, 1);
            pointer-events: none;
            white-space: nowrap;
        }
        .toast.show {
            opacity: 1;
            transform: translateX(-50%) translateY(0);
        }

        /* ─── Desktop / Large Screens ────────────────────────────── */
        @media (min-width: 1024px) {
            .app-container {
                max-width: 1440px;
                padding-left: 280px;
            }

            .menu-btn { display: none; }

            .dropdown-menu {
                display: flex;
                position: fixed;
                top: var(--header-height);
                left: 0;
                width: 280px;
                height: calc(100% - var(--header-height));
                background: white;
                z-index: 99;
                align-items: stretch;
                justify-content: stretch;
                backdrop-filter: none;
                -webkit-backdrop-filter: none;
                box-shadow: 2px 0 12px rgba(0,0,0,0.06);
            }

            .dropdown-content {
                width: 100%;
                max-width: 100%;
                height: 100%;
                padding: 12px 0;
                box-shadow: none;
                animation: none;
                border-radius: 0;
            }

            #mainTab.active {
                display: grid;
                grid-template-columns: 380px 1fr;
                grid-template-rows: auto 1fr;
                gap: 24px;
                align-items: start;
            }

            #mainTab.active .form-card {
                grid-column: 1;
                grid-row: 1 / -1;
                position: sticky;
                top: calc(var(--header-height) + 20px);
                margin-bottom: 0;
            }

            #mainTab.active .list-header {
                grid-column: 2;
                grid-row: 1;
            }

            #mainTab.active #devicesList {
                grid-column: 2;
                grid-row: 2;
                display: flex;
                flex-direction: column;
                gap: 12px;
            }

            #directoriesTab.active {
                display: flex;
                flex-direction: column;
                gap: 16px;
            }

            .directory-card {
                width: 100%;
            }

            .btn-primary,
            .btn-secondary {
                border-radius: 10px;
            }
        }
