:root {
    --bg-main: #d4bbfc;                  
    --bg-container: #ffffffcc; 
    --bg-dock: #ffffffcc;
    --color-title: #0071e3;                    
    --color-channel-name: #20b2aa;             
    --color-input-bg: rgba(255, 255, 255, 0.8);
    --color-modal-bg: rgba(255, 255, 255, 0.85);
    --shadow-box: 0 2px 8px rgba(0, 0, 0, 0.12); 
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; user-select: none; }
body {
    font-family: -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg-main);
    position: fixed; width: 100vw; height: 100dvh; overflow: hidden; overscroll-behavior: none;
    display: flex; justify-content: center; align-items: flex-start; padding-top: calc(env(safe-area-inset-top, 50px) + 10px);
}

.app-container {
    background: var(--bg-container); backdrop-filter: blur(40px); -webkit-backdrop-filter: blur(40px);
    border-radius: 36px; border: 1px solid rgba(255, 255, 255, 0.6);
    box-shadow: 0 10px 30px rgba(0,0,0,0.05); 
    width: min(90vw, 360px); padding: 20px 14px; position: relative; transition: box-shadow 0.3s ease;
    overscroll-behavior: none;
}
.app-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 18px 8px; justify-items: center; width: 100%; }
.app-item { display: flex; flex-direction: column; align-items: center; width: 100%; position: relative; cursor: pointer; transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), filter 0.3s; min-width: 0; }
.app-item:active { transform: scale(0.92); filter: drop-shadow(0 0 10px rgba(255,255,255,0.8)); } 

.icon-wrapper {
    width: 64px; height: 64px; position: relative; border-radius: 16px;
    background-color: rgba(255, 255, 255, 0.15); overflow: hidden !important;
    margin-bottom: 8px; isolation: isolate; display: flex; align-items: center; justify-content: center;
    box-shadow: var(--shadow-box); transition: transform 0.2s ease, box-shadow 0.2s ease;
    border: none; flex-shrink: 0;
}
.icon-img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; z-index: 2; border-radius: inherit; background: transparent; }

.fallback-text { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 24px; font-weight: 700; z-index: 1; color: #ffffff; text-shadow: 0 1px 3px rgba(0, 0, 0, 0.4); text-transform: uppercase; user-select: none; display: none; }

.mark-overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 3; pointer-events: none; mix-blend-mode: overlay; }

.app-info { display: flex; align-items: center; justify-content: center; width: 100%; min-width: 0; pointer-events: none; }
.app-name { display: block; font-size: 11px; color: #1e1e2f; opacity: 0.9; text-align: center; width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; padding: 0 2px;}
body.hide-labels .app-info { display: none !important; }
.vision-stroke { border: 1px solid rgba(0, 0, 0, 0.15) !important; box-sizing: border-box; }

#settingsBtn {
    position: fixed; bottom: calc(30px + env(safe-area-inset-bottom, 20px)); right: 20px; z-index: 999;
    width: 50px !important; height: 50px !important; min-width: 50px !important; min-height: 50px !important; border-radius: 50% !important;
    background: transparent; border: none; 
    display: flex; align-items: center; justify-content: center; cursor: pointer; padding: 0; overflow: hidden;
    transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1); isolation: isolate;
}
#settingsBtn:active { transform: scale(0.92); filter: drop-shadow(0 0 10px rgba(255,255,255,0.8)); }
#settingsBtn img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; z-index: 1; }
body.dock-active #settingsBtn, body.dock-active.delete-mode #settingsBtn { display: none !important; }

/* DOCK */
.dock-container { position: fixed; bottom: calc(20px + env(safe-area-inset-bottom, 20px)); left: 50%; transform: translateX(-50%); width: 90%; max-width: 360px; display: flex; justify-content: space-around; align-items: center; padding: 10px; border-radius: 28px; z-index: 997; transition: box-shadow 0.3s ease; }
.dock-style-glass { background: var(--bg-dock); backdrop-filter: blur(20px); border: 1px solid rgba(255, 255, 255, 0.5); }
.dock-style-wood { background: linear-gradient(to bottom, #a16d3a, #7c4b21, #a16d3a); border: 1px solid #5a3517; }
.dock-style-blur { background: var(--bg-dock); backdrop-filter: blur(15px); border: 1px solid rgba(255,255,255,0.2); }

#dockSetInner { width: 64px !important; height: 64px !important; }
.dock-container .app-item .icon-wrapper { margin-bottom: 0; }

/* DRAWER */
#drawerWrapper {
    position: fixed; z-index: 998; display: flex; align-items: center; justify-content: center;
    box-shadow: 0 10px 30px rgba(0,0,0,0.15); opacity: 0; pointer-events: none;
    transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.3s ease;
}
#drawerWrapper.open { opacity: 1; pointer-events: auto; }
#drawerWrapper.drawer-horizontal.open { transform: scale(1) !important; }
#drawerWrapper.drawer-vertical.open { transform: translateX(-50%) scale(1) !important; }

#drawerContent { display: flex; align-items: center; gap: 8px; overflow: hidden; width: 100%; height: 100%; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.drawer-horizontal { background: rgba(255,255,255,0.4); backdrop-filter: blur(40px); border-radius: 50px; padding: 6px; border: 1px solid rgba(255,255,255,0.6); flex-direction: row; }
.drawer-vertical { background: rgba(255,255,255,0.7) !important; backdrop-filter: blur(30px) !important; border-radius: 32px !important; padding: 6px !important; border: 1px solid rgba(255,255,255,0.6); flex-direction: column; }
.drawer-vertical #drawerClose { display: none !important; }

.drawer-btn {
    width: 44px !important; height: 44px !important; border-radius: 50% !important; border: none;
    display: flex; align-items: center; justify-content: center; cursor: pointer; flex-shrink: 0;
    background: rgba(255,255,255,0.7); box-shadow: var(--shadow-box); overflow: hidden; transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s, box-shadow 0.2s;
}
.drawer-btn:active { transform: scale(0.92); opacity: 0.8; box-shadow: 0 0 15px rgba(255,255,255,0.8); background: rgba(255,255,255,0.9); }
.drawer-btn img { width: 100%; height: 100%; border-radius: 50% !important; object-fit: cover; pointer-events: none; }

#drawerClose { 
    width: 44px !important; height: 44px !important; border-radius: 50%; background: rgba(0, 122, 255, 0.15); 
    color: #007AFF; border: none; font-size: 20px; font-weight: bold; cursor: pointer; display: flex; 
    align-items: center; justify-content: center; flex-shrink: 0; transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s;
}
#drawerClose:active { transform: scale(0.92); opacity: 0.8; box-shadow: 0 0 15px rgba(0,122,255,0.4); }

/* DELETE MODE */
#deleteTrash { display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%) scale(0); z-index: 1001; pointer-events: auto; transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.3s; opacity: 0; }
body.delete-mode #deleteTrash { display: block !important; transform: translate(-50%, -50%) scale(1) !important; opacity: 1 !important; }
body.delete-mode .icon-wrapper { animation: wiggle 1.5s infinite ease-in-out !important; }
body.delete-mode .app-item { transition: transform 0.3s ease; }
body.delete-mode #settingsBtn, body.delete-mode #dockSetInner { background: rgba(255, 59, 48, 0.9) !important; border-radius: 50% !important; box-shadow: 0 4px 12px rgba(255, 59, 48, 0.4) !important; display: flex !important; align-items: center !important; justify-content: center !important; }
body.delete-mode #settingsBtn img, body.delete-mode #dockSetInner img, body.delete-mode #settingsBtn .mark-overlay, body.delete-mode #dockSetInner .mark-overlay { display: none !important; }
body.delete-mode #settingsBtn::after, body.delete-mode #dockSetInner::after { content: '✕'; font-size: 28px; font-weight: bold; color: white; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 10; }
body.delete-mode #dockSetInner::after { font-size: 24px; }

/* Hiệu ứng thùng rác phóng to khi kéo */
body.delete-mode #deleteTrash.active {
    transform: translate(-50%, -50%) scale(1.15) !important;
    filter: drop-shadow(0 0 20px rgba(255,59,48,0.8));
}

/* MODAL CHUNG */
.modal-overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.4); backdrop-filter: blur(0px); opacity: 0; pointer-events: none; transition: opacity 0.4s ease, backdrop-filter 0.4s ease; z-index: 1000; display: flex; align-items: center; justify-content: center; }
.modal-overlay.active { opacity: 1; pointer-events: auto; backdrop-filter: blur(10px); }
.modal-content { background: var(--color-modal-bg); backdrop-filter: blur(30px); width: 90%; max-width: 360px; max-height: 78vh; border-radius: 28px; padding: 24px 16px; box-shadow: 0 12px 40px rgba(0,0,0,0.2); overflow-y: auto; transform: scale(0.85) translateY(30px); opacity: 0; transition: all 0.5s cubic-bezier(0.34, 1.56, 0.64, 1); }
.modal-overlay.active .modal-content { transform: scale(1) translateY(0); opacity: 1; }
.modal-content h3 { font-size: 18px; text-align: center; margin-bottom: 12px; color: #1c1c1e; }
.modal-content label { color: var(--color-channel-name); font-weight: 600; display: block; margin-bottom: 6px; font-size: 14px; }
.input-num, select, .search-box { width: 100%; padding: 12px 14px; border-radius: 14px; border: none; background: var(--color-input-bg); box-shadow: var(--shadow-box); font-size: 15px; outline: none; transition: transform 0.3s; color: #1e1e2f; }
.search-box { color: #1e1e2f; }
.close-modal { display: block; width: 100%; margin-top: 14px; padding: 12px; background: rgba(255,255,255,0.85); border: none; border-radius: 16px; font-weight: 600; font-size: 15px; cursor: pointer; text-align: center; transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s, background 0.2s;}

#onboardingModal { z-index: 9999; }

.color-picker-input { -webkit-appearance: none; appearance: none; width: 32px; height: 32px; border: none; border-radius: 50%; cursor: pointer; padding: 0; background: transparent; overflow: hidden; }
.color-picker-input::-webkit-color-swatch-wrapper { padding: 0; }
.color-picker-input::-webkit-color-swatch { border: 1px solid rgba(255,255,255,0.3); border-radius: 50%; }

#storeToggleBtn, #btnLoadMoreOnline, #btnLoadMoreLocal { transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s; }
#storeToggleBtn:active, #btnLoadMoreOnline:active, #btnLoadMoreLocal:active, .close-modal:active { transform: scale(0.95) !important; opacity: 0.8; }

.modal-list { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px 6px; max-height: 48vh; overflow-y: auto; padding: 8px 2px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }

@keyframes wiggle { 0%, 100% { transform: rotate(0deg); } 25% { transform: rotate(-6deg) scale(1.05); } 75% { transform: rotate(6deg) scale(1.05); } }
.wiggle-effect { animation: wiggle 1.5s infinite ease-in-out; }

.direction-container { display: flex; gap: 8px; margin-bottom: 10px; }
.dir-btn { flex: 1; padding: 10px 5px; background: var(--color-input-bg); border: 1px solid rgba(0,0,0,0.1); cursor: pointer; border-radius: 12px; transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), background 0.2s;}
.dir-btn.active { background: #0071e3; color: #fff; transform: scale(1.08);}
.dir-btn:active { transform: scale(0.92); }

.switch { position: relative; display: inline-block; width: 50px; height: 30px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; inset: 0; background-color: #ccc; transition: .4s; border-radius: 30px; }
.slider:before { position: absolute; content: ""; height: 24px; width: 24px; left: 3px; bottom: 3px; background-color: white; transition: .5s cubic-bezier(0.34, 1.56, 0.64, 1); border-radius: 50%; }
input:checked + .slider { background-color: #34c759; }
input:checked + .slider:before { transform: translateX(20px); }

#toast { position: fixed; top: 20px; left: 50%; transform: translateX(-50%) translateY(-100px); background: rgba(0,0,0,0.85); color: #fff; padding: 12px 24px; border-radius: 24px; font-size: 14px; z-index: 2000; transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1); }
#toast.show { transform: translateX(-50%) translateY(0); }

.ripple-wrapper { position: relative; overflow: hidden; }
.ripple { position: absolute; border-radius: 50%; transform: scale(0); pointer-events: none; animation: ripple-anim 600ms linear; background-color: rgba(200, 200, 200, 0.4); }
@keyframes ripple-anim { to { transform: scale(4); opacity: 0; } }

@media screen and (orientation: landscape) {
    .app-grid { grid-template-columns: repeat(auto-fit, minmax(64px, 1fr)); gap: 12px; }
    .app-container { width: min(90vw, 600px); }
}

html[dir="rtl"] { direction: rtl; }
html[dir="rtl"] .direction-container { flex-direction: row-reverse; }
html[dir="rtl"] .app-name, html[dir="rtl"] h3, html[dir="rtl"] label { text-align: right; }
html[dir="rtl"] .dock-container { flex-direction: row-reverse; }
html[dir="rtl"] .switch .slider:before { left: auto; right: 3px; }
html[dir="rtl"] input:checked + .slider:before { transform: translateX(-20px); }

@media (prefers-color-scheme: dark) {
    :root { --bg-container: rgba(30, 27, 75, 0.3); --color-modal-bg: rgba(15, 23, 42, 0.9); --color-input-bg: rgba(255, 255, 255, 0.1); }
    body { background: var(--bg-main); }
    .modal-content, #drawerWrapper { background-color: #1c1c1e !important; border: 1px solid rgba(255,255,255,0.1) !important; color: #e5e5ea !important; }
    .modal-content h3 { color: #f1f5f9 !important; }
    .app-name { color: #f1f5f9; }
    .close-modal { background: rgba(255,255,255,0.1); color: #fff; border: 1px solid rgba(255,255,255,0.05);}
    .search-box { color: #f1f5f9 !important; }
    
    #bgEffectModal label, #effectModal label,
    #bgEffectModal .input-num, #effectModal .input-num,
    #txtVisionStroke { color: #f1f5f9 !important; }

    .drawer-btn { background: rgba(255, 255, 255, 0.15) !important; box-shadow: 0 2px 8px rgba(0,0,0,0.4); }
    .drawer-btn:active { background: rgba(255, 255, 255, 0.25) !important; }
    #drawerClose { background: rgba(0, 122, 255, 0.2) !important; color: #4da6ff !important; }
    .ripple { background-color: rgba(255, 255, 255, 0.2); }
}