:root {
    --primary-color: #6366f1;
    --primary-hover: #4f46e5;
    --secondary-color: #f1f5f9;
    --secondary-hover: #e2e8f0;
    --danger-color: #ef4444;
    --success-color: #22c55e;
    --text-main: #0f172a;
    --text-muted: #64748b;
    --glass-bg: rgba(255, 255, 255, 0.7);
    --glass-border: rgba(255, 255, 255, 0.5);
    --glass-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.15);
    
    /* Layout Variables */
    --numerical-gap: 0.5rem;
    --numerical-sq-size: min(calc((55vh - 2rem) / 5), calc((100vw - 13.5rem) / 4));
    --numerical-grid-width: calc((var(--numerical-sq-size) * 4) + (var(--numerical-gap) * 3));
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Inter', sans-serif;
}

body {
    min-height: 100vh;
    color: var(--text-main);
    background: linear-gradient(135deg, #e0c3fc 0%, #8ec5fc 100%);
    display: flex;
    flex-direction: column;
}

.login-bg {
    display: flex;
    align-items: center;
    justify-content: center;
}

.admin-bg { background: linear-gradient(135deg, #fdfbfb 0%, #ebedee 100%); }
.tablet-bg { 
    background: linear-gradient(135deg, #f6d365 0%, #fda085 100%); 
    height: 100vh;
    overflow: hidden;
}

/* Glassmorphism */
.glass-container {
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 20px;
    box-shadow: var(--glass-shadow);
    padding: 2rem;
}

.glass-nav {
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--glass-border);
    padding: 1rem 2rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

/* Typography */
h1, h2, h3 { margin-bottom: 1rem; color: var(--text-main); }
p { margin-bottom: 1.5rem; color: var(--text-muted); }

/* Forms */
.input-group { margin-bottom: 1.5rem; }
.input-group label { display: block; margin-bottom: 0.5rem; font-weight: 500; }
input, select, textarea {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    font-size: 1rem;
    outline: none;
    transition: border-color 0.2s;
}
input:focus, textarea:focus { border-color: var(--primary-color); }

/* Buttons */
button {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    font-size: 1rem;
    cursor: pointer;
    transition: transform 0.1s, background-color 0.2s;
}
button:active { transform: scale(0.98); }
button:disabled { opacity: 0.6; cursor: not-allowed; }

.btn-primary { background: var(--primary-color); color: white; }
.btn-primary:hover { background: var(--primary-hover); }

.btn-secondary { background: var(--secondary-color); color: var(--text-main); }
.btn-secondary:hover { background: var(--secondary-hover); }

.btn-success { background: var(--success-color); color: white; }
.btn-danger { background: var(--danger-color); color: white; }

/* Login Specific */
.login-card { width: 100%; max-width: 400px; text-align: center; }
.error-msg { color: var(--danger-color); margin-top: 1rem; font-size: 0.9rem; }

/* Admin Specific */
.admin-layout { display: grid; grid-template-columns: 1fr 2fr; gap: 2rem; padding: 2rem; flex: 1; }
.create-user-form { display: grid; grid-template-columns: 1fr 1fr 1fr auto; gap: 1rem; margin-bottom: 2rem; }
.users-list { list-style: none; }
.users-list li {
    padding: 1rem;
    background: rgba(255,255,255,0.5);
    border-radius: 8px;
    margin-bottom: 0.5rem;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
}
.users-list li:hover { background: rgba(255,255,255,0.8); }
.users-list li.selected { border: 2px solid var(--primary-color); }
.json-editor { height: 400px; font-family: monospace; margin: 1rem 0; resize: vertical; }

/* Tablet Grid */
.tablet-main { 
    padding: 2rem 2rem 2rem 2rem; 
    flex: 1;
    display: flex; 
    align-items: center; 
    justify-content: center; 
    gap: 2rem;
    overflow: hidden;
}

.global-nav-sidebar {
    position: absolute;
    left: 2rem;
    bottom: 2rem;
    display: grid;
    grid-template-columns: repeat(3, auto);
    grid-template-rows: auto auto auto;
    gap: 0.5rem;
    align-content: end;
    justify-content: center;
}

.global-nav-btn {
    width: clamp(45px, 6vw, 75px);
    height: clamp(45px, 6vw, 75px);
    border-radius: 12px;
    font-size: clamp(0.45rem, 0.9vw, 0.65rem);
    text-align: center;
    font-weight: bold;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 2px;
    box-sizing: border-box;
    overflow: hidden;
    line-height: 1.1;
    overflow-wrap: anywhere;
    border: 2px solid rgba(255,255,255,0.6);
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    background: rgba(255,255,255,0.8);
    backdrop-filter: blur(5px);
    cursor: pointer;
    transition: transform 0.1s, box-shadow 0.1s, filter 0.1s;
}

.global-nav-btn img {
    width: 60%;
    height: 60%;
    object-fit: contain;
}

.global-nav-btn[data-btn-id="UP"] {
    grid-column: 2;
    grid-row: 2;
}
.global-nav-btn[data-btn-id="DOWN"] {
    grid-column: 2;
    grid-row: 3;
}
.global-nav-btn[data-btn-id="LEFT"] {
    grid-column: 1;
    grid-row: 3;
}
.global-nav-btn[data-btn-id="RIGHT"] {
    grid-column: 3;
    grid-row: 3;
}
.global-nav-btn[data-btn-id="BACKSPACE"] {
    grid-column: 1 / span 3;
    grid-row: 1;
    justify-self: center;
    margin-bottom: 2rem;
}

.active-btn {
    transform: scale(0.95);
    box-shadow: 0 2px 4px -1px rgba(0, 0, 0, 0.1);
    filter: brightness(0.9);
}
.button-grid {
    display: grid;
    gap: 1.5rem;
    width: 100%;
    /* height removed so grid can naturally fit inside flex container */
    place-items: center;
}

.button-grid.layout-grid {
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(6, 1fr);
    max-width: 1000px;
}

.button-grid.layout-numerical {
    --gap-size: var(--numerical-gap);
    gap: var(--numerical-gap);
    /* 5 rows means 4 gaps (4 * 1.5rem = 6rem) */
    /* 4 cols means 3 gaps (3 * 1.5rem = 4.5rem). Plus 4rem for main padding, 2rem for sidebar gap, and approx 8vw for sidebar width (~10rem) = 16.5rem */
    --sq-size: var(--numerical-sq-size);
    
    grid-template-columns: repeat(4, var(--sq-size));
    grid-auto-rows: var(--sq-size);
    justify-content: center;
    align-content: center;
    width: 100%;
}

.button-grid.layout-numerical .tablet-btn {
    width: 100%;
    height: 100%;
    font-size: clamp(0.7rem, 2vw, 1rem);
}

.button-grid.catalog-mode.layout-numerical .tablet-btn {
    font-size: clamp(0.5rem, 1.5vw, 0.8rem);
}

.button-grid.layout-numerical .tablet-btn:nth-child(8) {
    grid-row: span 2;
}

.button-grid.layout-numerical .tablet-btn:nth-child(15) {
    grid-row: span 2;
}

.button-grid.layout-numerical .tablet-btn:nth-child(16) {
    grid-column: span 2;
}

.button-grid.layout-qwerty {
    grid-template-columns: repeat(10, 1fr);
    grid-template-rows: repeat(3, 1fr);
    max-width: 1200px;
}

.tablet-btn {
    border-radius: 16px;
    font-size: clamp(0.9rem, 2.5vw, 1.25rem);
    font-weight: 700;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 0.75rem;
    color: #1e293b;
    background: rgba(255,255,255,0.8);
    backdrop-filter: blur(5px);
    border: 2px solid rgba(255,255,255,0.6);
    transition: transform 0.1s, box-shadow 0.1s, filter 0.1s;
}

.hidden { display: none !important; }

/* Modal */
.modal {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.5);
    display: flex; align-items: center; justify-content: center;
    z-index: 1000;
}
.modal-content { max-width: 500px; text-align: center; }

/* Extended Header & Breadcrumbs */
.header-extended {
    flex-direction: column;
    align-items: stretch;
    gap: 1rem;
    padding: 1rem 2rem;
}

.header-top-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
}

.status-indicators {
    display: flex;
    gap: 1rem;
}

.status-pill {
    background: rgba(0, 0, 0, 0.1);
    padding: 0.4rem 1rem;
    border-radius: 20px;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-main);
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.05);
}

.vertical-nav-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
    margin-top: 0.5rem;
    width: 100%;
}

.vertical-nav-btn {
    width: 100%;
    max-width: 1000px;
    padding: 0.5rem;
    font-size: 1rem;
    font-weight: 600;
    background: var(--primary-color);
    color: #000;
    border: none;
    border-radius: 6px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    cursor: pointer;
    transition: background 0.2s;
}

.vertical-nav-btn:hover {
    background: var(--primary-hover);
}

body.layout-qwerty .vertical-nav-btn,
body.layout-qwerty .display-panel {
    max-width: 1200px;
}

body.layout-grid .vertical-nav-btn,
body.layout-grid .display-panel {
    max-width: 1000px;
}

body.layout-numerical .vertical-nav-btn,
body.layout-numerical .display-panel {
    max-width: var(--numerical-grid-width);
}

/* Display Panel */
.display-panel {
    width: 100%;
    margin-top: -0.75rem;
    align-self: center; /* Center within the flex column header */
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
}

.display-img {
    height: clamp(50px, 10vh, 140px);
    object-fit: contain;
    border-radius: 12px;
}

.display-content {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1.5rem;
}

.display-icon {
    font-size: clamp(1.5rem, 3vw, 2rem);
}

.display-text {
    font-size: clamp(0.9rem, 2vw, 1.4rem);
    font-weight: 700;
    color: var(--text-main);
}

.speaking-indicator {
    font-size: clamp(1.5rem, 3vw, 2rem);
    animation: speakingPulse 1s infinite alternate;
}

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

.display-phrase {
    font-size: clamp(0.8rem, 2vw, 1.2rem);
    font-style: italic;
    color: var(--primary-color);
    text-align: center;
    width: 100%;
    margin-top: 0.5rem;
}

/* 3-Way Visuals Mode Toggle */
.btn-label, .btn-emoji, .btn-image {
    display: none;
}

/* Base or Text Mode */
.button-grid:not(.mode-icon):not(.mode-image) .btn-label,
.button-grid.mode-text .btn-label {
    display: block;
}

/* Icon Mode */
.button-grid.mode-icon .btn-emoji {
    display: block;
    font-size: 3rem;
    line-height: 1;
}

/* Image Mode */
.button-grid.mode-image .btn-image {
    display: flex;
    width: 100%;
    height: 100%;
    align-items: center;
    justify-content: center;
}

.btn-image img {
    max-width: 90%;
    max-height: 90%;
    object-fit: contain;
}

/* Responsive adjustments for Mobile & Tablets */
@media (max-width: 900px) {
    :root {
        --numerical-sq-size: min(calc((60vh - 2rem) / 5), calc((100vw - 3.5rem) / 4));
    }
    
    .tablet-bg {
        height: auto;
        min-height: 100vh;
        overflow-y: auto;
    }

    .tablet-main {
        flex-direction: column;
        justify-content: flex-start;
        padding-bottom: 4rem;
        gap: 2rem;
        overflow: visible;
    }

    .global-nav-sidebar {
        position: static;
        margin-top: 1rem;
        margin-bottom: 2rem;
        align-content: center;
    }
}
