:root {
    /* Themes */
    /* default: daytime aquarium */
    --bg-top: #b6f4ff;
    --bg-mid: #5ad5fc;
    --bg-bottom: #0087cb;
    
    /* Variables updated by JS */
    
    --text-main: #ffffff;
    --text-dark: #12405d;
    --glass-bg: rgba(255, 255, 255, 0.25);
    --glass-border: rgba(255, 255, 255, 0.4);
    --glass-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
    --primary: #FFB347;
    --primary-hover: #FFCC80;
    --danger: #FF6B6B;
    --secondary: #48C9B0;
    --radius: 20px;
    --font-heading: 'M PLUS Rounded 1c', sans-serif;
}

body.bg-default { --bg-top: #a6f3ff; --bg-mid: #38bdf8; --bg-bottom: #0284c7; }
body.bg-sunset { --bg-top: #fdbb2d; --bg-mid: #b21f1f; --bg-bottom: #1a2a6c; }
body.bg-night { --bg-top: #0f2027; --bg-mid: #203a43; --bg-bottom: #2c5364; }
body.bg-deep { --bg-top: #0a1128; --bg-mid: #053f5c; --bg-bottom: #000000; }

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

body, html {
    margin: 0;
    padding: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    font-family: var(--font-heading);
    background-color: var(--bg-bottom); /* Safety backdrop */
    color: var(--text-dark);
    touch-action: none;
}

#app {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    position: relative;
    /* subtle pattern overlay + main background */
    background: 
        radial-gradient(circle at 50% 120%, rgba(255,255,255,0.1) 0%, transparent 60%),
        linear-gradient(180deg, var(--bg-top) 0%, var(--bg-mid) 50%, var(--bg-bottom) 100%);
    transition: background 1s ease;
}

/* Lights & Bubbles Animation */
.light-beams { position: absolute; top: 0; left: 0; width: 100%; height: 60%; pointer-events: none; z-index: 1; opacity: 0.3; }
.beam { position: absolute; width: 15%; height: 150%; background: linear-gradient(180deg, rgba(255,255,255,0.4) 0%, transparent 100%); top: -20%; border-radius: 50% / 100%; filter: blur(20px); transform-origin: top center; }
.beam:nth-child(1) { left: 10%; transform: rotate(15deg); animation: sway 8s infinite alternate ease-in-out; }
.beam:nth-child(2) { left: 50%; width: 20%; transform: rotate(-10deg); animation: sway 12s infinite alternate-reverse ease-in-out; }
.beam:nth-child(3) { left: 80%; transform: rotate(5deg); animation: sway 10s infinite alternate ease-in-out; }

@keyframes sway { 0% { opacity: 0.4; transform: rotate(5deg); } 100% { opacity: 0.7; transform: rotate(-5deg); } }

.bubbles { position: absolute; bottom: 0; left: 0; width: 100%; height: 100%; pointer-events: none; overflow: hidden; z-index: 2; }
.bubble { position: absolute; bottom: -50px; background: radial-gradient(circle at 30% 30%, rgba(255,255,255,0.8), rgba(255,255,255,0.2)); border-radius: 50%; box-shadow: inset 0 0 10px rgba(255,255,255,0.5); border: 1px solid rgba(255,255,255,0.4); animation: rise linear infinite; }
.bubble:nth-child(1) { width: 30px; height: 30px; left: 10%; animation-duration: 4s; animation-delay: 0s; }
.bubble:nth-child(2) { width: 15px; height: 15px; left: 25%; animation-duration: 3s; animation-delay: 1s; }
.bubble:nth-child(3) { width: 40px; height: 40px; left: 45%; animation-duration: 5s; animation-delay: 2s; }
.bubble:nth-child(4) { width: 20px; height: 20px; left: 65%; animation-duration: 3.5s; animation-delay: 0.5s; }
.bubble:nth-child(5) { width: 10px; height: 10px; left: 80%; animation-duration: 2.5s; animation-delay: 1.5s; }
.bubble:nth-child(6) { width: 25px; height: 25px; left: 90%; animation-duration: 4.5s; animation-delay: 0.2s; }

@keyframes rise {
    0% { transform: translateY(0) scale(1) translateX(0); opacity: 0; }
    10% { opacity: 1; }
    50% { transform: translateY(-50vh) scale(1.2) translateX(30px); }
    90% { opacity: 1; }
    100% { transform: translateY(-110vh) scale(1.5) translateX(-20px); opacity: 0; }
}

/* Glass UI Elements */
.glass-header {
    background: var(--glass-bg);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--glass-border);
    padding: 10px 0;
    text-align: center;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    z-index: 10;
}
.glass-header h1 {
    margin: 0;
    font-size: 1.4rem;
    color: #fff;
    text-shadow: 0 2px 4px rgba(0,0,0,0.3);
    letter-spacing: 2px;
}
.glass-panel {
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    box-shadow: var(--glass-shadow);
    border-radius: var(--radius);
}

/* Buttons */
.glass-btn {
    background: rgba(255,255,255,0.6);
    border: none;
    border-radius: 50px;
    padding: 10px 20px;
    font-size: 1.1rem;
    font-family: var(--font-heading);
    font-weight: bold;
    color: var(--text-dark);
    cursor: pointer;
    backdrop-filter: blur(4px);
    box-shadow: 0 4px 6px rgba(0,0,0,0.1), inset 0 2px 4px rgba(255,255,255,0.6);
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 8px;
}
.glass-btn:active { transform: scale(0.95); box-shadow: 0 2px 3px rgba(0,0,0,0.1); }
.glass-btn.primary { background: linear-gradient(135deg, var(--primary) 0%, var(--primary-hover) 100%); color: white; border: 2px solid rgba(255,255,255,0.5); }
.glass-btn.danger { background: linear-gradient(135deg, #ff8a65, var(--danger)); color: white; border: 2px solid rgba(255,255,255,0.5); }
.glass-btn.secondary { background: linear-gradient(135deg, #a8e6cf, var(--secondary)); color: white; border: 2px solid rgba(255,255,255,0.5); }
.glass-btn.icon-btn { padding: 10px; border-radius: 50%; width: 45px; height: 45px; justify-content: center; font-size: 1.2rem; }
.glass-btn[disabled] { opacity: 0.5; filter: grayscale(1); cursor: not-allowed; }

.bounce-anim { animation: pulseBtn 2s infinite ease-in-out; }
@keyframes pulseBtn {
    0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(255,179,71, 0.7); }
    50% { transform: scale(1.05); box-shadow: 0 0 0 10px rgba(255,179,71, 0); }
    100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(255,179,71, 0); }
}

/* Layout */
#aquarium-tank {
    flex: 1;
    position: relative;
    overflow: hidden;
    z-index: 5;
    /* ensure items can't be dragged out invisibly */
}

/* Tank Items */
.tank-item {
    position: absolute;
    transform-origin: center center;
    user-select: none;
    touch-action: none;
    cursor: grab;
    z-index: 100;
}
.tank-item:active { cursor: grabbing; filter: drop-shadow(0 10px 10px rgba(0,0,0,0.3)); }
.tank-item.selected { filter: drop-shadow(0 0 15px rgba(255,255,255,0.8)); }
.tank-item img {
    display: block;
    width: auto;
    max-width: none;
    pointer-events: none; /* crucial for pure container drag */
    transition: transform 0.2s ease; /* flip transition */
}
.tank-item.flipped img {
    transform: scaleX(-1);
}

/* UI Panel at the bottom */
.ui-panel {
    display: flex;
    flex-direction: column;
    padding: 10px 10px 15px 10px;
    padding-bottom: max(25px, env(safe-area-inset-bottom));
    gap: 12px;
    background: rgba(255,255,255,0.3);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-top: 1px solid rgba(255,255,255,0.5);
    box-shadow: 0 -4px 15px rgba(0,0,0,0.1);
    z-index: 2000;
}

.item-tools {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
    padding-bottom: 12px;
    border-bottom: 1px solid rgba(255,255,255,0.5);
    transition: opacity 0.3s ease, filter 0.3s ease;
}
.item-tools.disabled {
    opacity: 0.3;
    filter: grayscale(100%);
    pointer-events: none;
}

.tool-btn {
    background: white;
    border: 1px solid #eee;
    padding: 8px 2px;
    border-radius: 10px;
    font-size: clamp(0.55rem, 2.8vw, 0.85rem);
    font-family: var(--font-heading);
    font-weight: bold;
    color: #444;
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    text-align: center;
    white-space: nowrap;
    letter-spacing: -0.8px;
    overflow: hidden;
}
.tool-btn:active { background: #f0f0f0; transform: scale(0.95); }
.tool-btn.danger { color: #d32f2f; }
.tool-divider { width: 1px; background: rgba(0,0,0,0.1); margin: 0 2px; }

.global-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 5px;
}
.side-controls { display: flex; gap: 10px; }

/* Modal */
.modal {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    display: flex; justify-content: center; align-items: center;
    z-index: 9999;
}
.modal.hidden { display: none; }
.modal-backdrop {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(5px);
}
.modal-content {
    position: relative;
    width: 95%; max-width: 450px;
    max-height: 85vh;
    display: flex; flex-direction: column;
    padding: 15px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.2);
    animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes popIn { 0% { opacity: 0; transform: scale(0.8); } 100% { opacity: 1; transform: scale(1); } }

.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.modal-header h2 { margin: 0; color: var(--text-dark); font-size: 1.3rem; }
.close-modal-btn { background: none; border: none; font-size: 1.5rem; color: #666; cursor: pointer; }
.limit-text { font-size: 0.9rem; color: #555; text-align: center; margin: 0 0 10px 0; }
.highlight { color: var(--danger); font-size: 1.1rem; }

.modal-body {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
    background: rgba(255,255,255,0.4);
    border-radius: 15px;
    border: inset 2px rgba(255,255,255,0.2);
}
.image-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(75px, 1fr));
    gap: 12px;
}
.grid-item {
    position: relative;
    background: rgba(255,255,255,0.6);
    border-radius: 12px;
    padding: 8px;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0,0,0,0.05);
    transition: all 0.2s;
    display: flex;
    justify-content: center;
    align-items: center;
    aspect-ratio: 1;
}
.grid-item:active { transform: scale(0.9); }
.grid-item img {
    max-width: 100%; max-height: 100%; object-fit: contain; pointer-events: none;
}
.item-badge {
    position: absolute;
    top: -5px; right: -5px;
    background: var(--danger);
    color: white;
    min-width: 22px; height: 22px;
    border-radius: 11px;
    display: flex; justify-content: center; align-items: center;
    font-size: 0.8rem; font-weight: bold;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
    opacity: 0; transform: scale(0);
    transition: all 0.2s;
}
.grid-item.has-count .item-badge { opacity: 1; transform: scale(1); }
.item-controls {
    position: absolute;
    bottom: -10px; left: 50%; transform: translateX(-50%);
    display: flex; gap: 5px; opacity: 0; transition: 0.2s; pointer-events: none;
}
.grid-item.has-count .item-controls { opacity: 1; pointer-events: auto; }
.ctrl-btn {
    width: 24px; height: 24px; border-radius: 50%; border: none;
    background: white; color: var(--danger); font-weight: bold;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2); cursor: pointer;
}
.ctrl-btn:active { background: #eee; }

.modal-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 15px;
    padding-top: 10px;
    border-top: 1px solid rgba(0,0,0,0.1);
}
.selected-count { font-weight: bold; color: var(--text-dark); }
.count-badge { background: white; padding: 2px 8px; border-radius: 10px; margin-left: 5px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }

.bg-grid { display: flex; flex-direction: column; gap: 10px; }
.bg-option { padding: 15px; border-radius: 10px; border: 2px solid transparent; text-align: left; font-size: 1.1rem; font-weight: bold; background: rgba(255,255,255,0.7); cursor: pointer; transition: 0.2s; }
.bg-option:active { transform: scale(0.98); }
.bg-option.active { border-color: var(--primary); background: rgba(255,255,255,0.9); }

.toast {
    position: absolute;
    top: 60px;
    left: 50%;
    transform: translateX(-50%) translateY(-20px);
    background: rgba(0, 0, 0, 0.7);
    color: white;
    padding: 10px 20px;
    border-radius: 30px;
    font-size: 0.9rem;
    z-index: 10000;
    opacity: 0;
    pointer-events: none;
    text-align: center;
}
.toast.show {
    animation: fadeOutToast 1.5s linear forwards;
}
@keyframes fadeOutToast {
    0% { opacity: 1; transform: translateX(-50%) translateY(0); }
    100% { opacity: 0; transform: translateX(-50%) translateY(-10px); }
}
