* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --state0-color: #4CAF50;
    --state1-color: #F44336;
    --bg-color: #1E1E1E;
    --text-color: #FFFFFF;
    --tile-border: #333333;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    padding: 20px;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    padding-bottom: 20px;
    border-bottom: 2px solid var(--tile-border);
}

.header h1 {
    font-size: 2em;
    font-weight: 600;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 20px;
}

.header-link {
    padding: 8px 16px;
    background-color: #2a2a2a;
    color: var(--text-color);
    text-decoration: none;
    border: 1px solid var(--tile-border);
    border-radius: 4px;
    font-size: 1em;
    transition: all 0.2s ease;
    cursor: pointer;
    font-family: inherit;
}

.header-link:hover {
    background-color: #333;
    border-color: #555;
    transform: translateY(-1px);
}

/* Sound controls */
.sound-controls {
    display: flex;
    align-items: center;
    gap: 8px;
}

.sound-mute-btn {
    font-size: 1.1em;
    padding: 8px 10px;
    line-height: 1;
    min-width: 38px;
    text-align: center;
}

.volume-slider {
    -webkit-appearance: none;
    appearance: none;
    width: 90px;
    height: 4px;
    background: #555;
    border-radius: 2px;
    outline: none;
    cursor: pointer;
    accent-color: var(--state0-color);
}

.volume-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--state0-color);
    cursor: pointer;
    transition: transform 0.15s ease;
}

.volume-slider::-webkit-slider-thumb:hover {
    transform: scale(1.2);
}

.volume-slider::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border: none;
    border-radius: 50%;
    background: var(--state0-color);
    cursor: pointer;
}

/* Autoplay unlock banner */
.sound-banner {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: #e6960a;
    color: #1a1100;
    text-align: center;
    padding: 13px 20px;
    font-weight: 600;
    font-size: 0.95em;
    z-index: 9999;
    cursor: pointer;
    animation: banner-pulse 2s ease-in-out infinite;
}

@keyframes banner-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.8; }
}

/* Modal Styles */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    overflow: auto;
}

.modal-content {
    background-color: var(--bg-color);
    margin: 5% auto;
    padding: 0;
    border: 1px solid var(--tile-border);
    border-radius: 8px;
    width: 90%;
    max-width: 800px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid var(--tile-border);
}

.modal-header h2 {
    margin: 0;
    font-size: 1.5em;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-color);
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
    padding: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background-color 0.2s ease;
}

.modal-close:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

.modal-body {
    padding: 20px;
    overflow-y: auto;
    flex: 1;
}

.logs-container {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.log-entry {
    padding: 12px;
    background-color: #2a2a2a;
    border: 1px solid var(--tile-border);
    border-radius: 4px;
    font-size: 0.9em;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
}

.log-entry.needs-help {
    border-left: 4px solid var(--state1-color);
}

.log-entry.ok {
    border-left: 4px solid var(--state0-color);
}

.log-info {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1;
    min-width: 200px;
}

.log-timestamp {
    font-size: 0.85em;
    opacity: 0.7;
    color: var(--text-color);
}

.log-device {
    font-weight: 600;
    color: var(--text-color);
}

.log-action {
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 0.85em;
    font-weight: 600;
}

.log-action.needs-help {
    background-color: var(--state1-color);
    color: white;
}

.log-action.ok {
    background-color: var(--state0-color);
    color: white;
}

.controls {
    display: flex;
    align-items: center;
    gap: 10px;
}

.controls label {
    font-size: 1em;
}

.controls select {
    padding: 8px 16px;
    background-color: #2a2a2a;
    color: var(--text-color);
    border: 1px solid var(--tile-border);
    border-radius: 4px;
    font-size: 1em;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.2s ease;
}

.controls select:hover {
    background-color: #333;
    border-color: #555;
}

.tile-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 20px;
    padding: 20px 0;
}

.tile {
    background-color: var(--state0-color);
    border: 2px solid var(--tile-border);
    border-radius: 8px;
    padding: 20px;
    cursor: move;
    transition: all 0.3s ease;
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    min-height: 150px;
    max-width: 100%;
    user-select: none;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
}

.tile-delete {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background-color: rgba(0, 0, 0, 0.5);
    color: var(--text-color);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: bold;
    line-height: 1;
    opacity: 0;
    transition: opacity 0.2s ease, background-color 0.2s ease;
    z-index: 10;
    padding: 0;
}

.tile:hover .tile-delete {
    opacity: 1;
}

.tile-delete:hover {
    background-color: rgba(244, 67, 54, 0.8);
    transform: scale(1.1);
}

.tile-delete:active {
    transform: scale(0.95);
}

.tile:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
}

.tile.state-0 {
    background-color: var(--state0-color);
}

.tile.state-1 {
    background-color: var(--state1-color);
}

.tile.dragging {
    opacity: 0.5;
    z-index: 1000;
}

.tile.shake {
    animation: shake 0.5s ease-in-out;
}

@keyframes shake {
    0%, 100% {
        transform: translateX(0);
    }
    10%, 30%, 50%, 70%, 90% {
        transform: translateX(-5px);
    }
    20%, 40%, 60%, 80% {
        transform: translateX(5px);
    }
}

.tile-name {
    font-size: 1.2em;
    font-weight: 600;
    margin-bottom: 10px;
    text-align: center;
    word-wrap: break-word;
    width: 100%;
}

.tile-ip {
    font-size: 0.9em;
    opacity: 0.8;
    text-align: center;
    word-wrap: break-word;
    width: 100%;
}

.tile-status {
    font-size: 0.8em;
    margin-top: 10px;
    padding: 4px 8px;
    background-color: rgba(0, 0, 0, 0.2);
    border-radius: 4px;
    text-align: center;
}

.tile.editing .tile-name {
    outline: 2px solid var(--text-color);
    padding: 4px;
    background-color: rgba(255, 255, 255, 0.1);
}

/* Size classes */
.tile.size-small {
    width: 150px;
    max-width: 150px;
    height: 150px;
    min-height: 150px;
    padding: 15px;
}

.tile.size-small .tile-name {
    font-size: 1em;
}

.tile.size-small .tile-ip {
    font-size: 0.8em;
}

.tile.size-medium {
    width: 200px;
    max-width: 200px;
    height: 200px;
    min-height: 200px;
    padding: 20px;
}

.tile.size-large {
    width: 250px;
    max-width: 250px;
    height: 250px;
    min-height: 250px;
    padding: 25px;
}

.tile.size-large .tile-name {
    font-size: 1.4em;
}

.tile.size-xlarge {
    width: 300px;
    max-width: 300px;
    height: 300px;
    min-height: 300px;
    padding: 30px;
}

.tile.size-xlarge .tile-name {
    font-size: 1.6em;
}

.tile.size-xlarge .tile-ip {
    font-size: 1.1em;
}

/* Drag and drop visual feedback */
.tile-container.drag-over {
    background-color: rgba(255, 255, 255, 0.05);
}

/* ── Login Page ────────────────────────────────────────────────────────────── */

.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    padding: 20px;
}

.login-box {
    background-color: #2a2a2a;
    border: 1px solid var(--tile-border);
    border-radius: 8px;
    padding: 40px;
    width: 100%;
    max-width: 400px;
    text-align: center;
}

.login-box h1 {
    margin-bottom: 30px;
    font-size: 1.8em;
}

.login-box input[type="password"] {
    width: 100%;
    padding: 12px 16px;
    background-color: #1E1E1E;
    color: var(--text-color);
    border: 1px solid var(--tile-border);
    border-radius: 4px;
    font-size: 1em;
    margin-bottom: 16px;
    font-family: inherit;
}

.login-box input[type="password"]:focus {
    outline: none;
    border-color: #555;
}

.login-box button[type="submit"] {
    width: 100%;
    padding: 12px 16px;
    background-color: var(--state0-color);
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 1em;
    cursor: pointer;
    font-family: inherit;
    transition: opacity 0.2s ease;
}

.login-box button[type="submit"]:hover {
    opacity: 0.9;
}

.login-error {
    margin-top: 16px;
    color: var(--state1-color);
    font-size: 0.9em;
}

/* ── Admin / API Keys Page ─────────────────────────────────────────────────── */

.admin-content {
    max-width: 900px;
    margin: 0 auto;
}

.admin-section {
    margin-bottom: 30px;
}

.admin-section h2 {
    font-size: 1.3em;
    margin-bottom: 16px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--tile-border);
}

.key-form .form-row {
    display: flex;
    gap: 16px;
    align-items: flex-end;
    flex-wrap: wrap;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex: 1;
    min-width: 180px;
}

.form-group label {
    font-size: 0.85em;
    opacity: 0.8;
}

.form-group input,
.form-group select {
    padding: 10px 14px;
    background-color: #2a2a2a;
    color: var(--text-color);
    border: 1px solid var(--tile-border);
    border-radius: 4px;
    font-size: 1em;
    font-family: inherit;
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: #555;
}

.btn-create {
    padding: 10px 24px;
    background-color: var(--state0-color);
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 1em;
    cursor: pointer;
    font-family: inherit;
    white-space: nowrap;
    transition: opacity 0.2s ease;
}

.btn-create:hover {
    opacity: 0.9;
}

.new-key-display {
    background-color: #1a3a1a;
    border: 1px solid var(--state0-color);
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 30px;
}

.new-key-display h3 {
    margin-bottom: 8px;
    color: var(--state0-color);
}

.new-key-display p {
    font-size: 0.9em;
    opacity: 0.8;
    margin-bottom: 12px;
}

.new-key-field {
    margin-bottom: 14px;
}

.new-key-field label {
    display: block;
    font-size: 0.8em;
    opacity: 0.6;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 6px;
}

.key-copy-row {
    display: flex;
    gap: 10px;
    align-items: center;
}

.key-copy-row code {
    flex: 1;
    padding: 10px 14px;
    background-color: #1E1E1E;
    border: 1px solid var(--tile-border);
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    font-size: 0.85em;
    word-break: break-all;
    color: var(--text-color);
}

.btn-copy {
    padding: 10px 16px;
    background-color: #2a2a2a;
    color: var(--text-color);
    border: 1px solid var(--tile-border);
    border-radius: 4px;
    cursor: pointer;
    font-family: inherit;
    transition: background-color 0.2s ease;
}

.btn-copy:hover {
    background-color: #333;
}


.btn-dismiss {
    padding: 8px 16px;
    background-color: transparent;
    color: var(--text-color);
    border: 1px solid var(--tile-border);
    border-radius: 4px;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.9em;
    transition: background-color 0.2s ease;
}

.btn-dismiss:hover {
    background-color: #2a2a2a;
}

.keys-container {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.key-entry {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 16px;
    background-color: #2a2a2a;
    border: 1px solid var(--tile-border);
    border-radius: 4px;
    gap: 16px;
}

.key-entry.expired {
    opacity: 0.5;
}

.key-info {
    flex: 1;
}

.key-name {
    font-weight: 600;
    margin-bottom: 6px;
}

.key-meta {
    display: flex;
    gap: 20px;
    font-size: 0.85em;
    opacity: 0.7;
    flex-wrap: wrap;
}

.key-meta code {
    font-family: 'Courier New', monospace;
    color: var(--text-color);
}

.key-meta .expired {
    color: var(--state1-color);
    opacity: 1;
    font-weight: 600;
}

.btn-revoke {
    padding: 8px 16px;
    background-color: transparent;
    color: var(--state1-color);
    border: 1px solid var(--state1-color);
    border-radius: 4px;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.85em;
    transition: all 0.2s ease;
    white-space: nowrap;
}

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

/* ── Mobile / Responsive ────────────────────────────────────────────────────── */

@media (max-width: 768px) {
    body {
        padding: 12px;
    }

    /* Header stacks vertically */
    .header {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
        margin-bottom: 16px;
        padding-bottom: 12px;
    }

    .header h1 {
        font-size: 1.4em;
    }

    .header-right {
        width: 100%;
        gap: 8px;
        flex-wrap: wrap;
    }

    .header-link {
        padding: 7px 12px;
        font-size: 0.9em;
    }

    .sound-mute-btn {
        padding: 7px 9px;
    }

    .volume-slider {
        width: 70px;
    }

    /* Tile grid: tighter gap on mobile, JS still controls column count */
    .tile-container {
        gap: 12px;
        padding: 12px 0;
    }

    /* Tile size classes keep their min-height but fill the grid cell width */
    .tile.size-small,
    .tile.size-medium,
    .tile.size-large,
    .tile.size-xlarge {
        width: 100%;
    }

    /* Admin page */
    .admin-content {
        padding: 0;
    }

    .key-copy-row {
        flex-direction: column;
        align-items: stretch;
    }

    .key-entry {
        flex-direction: column;
        align-items: flex-start;
    }

    .key-meta {
        gap: 8px;
    }

    /* Modal */
    .modal-content {
        margin: 0;
        width: 100%;
        max-height: 100vh;
        border-radius: 0;
        border-left: none;
        border-right: none;
    }
}

@media (max-width: 480px) {
    /* Controls wrap more tightly */
    .controls label {
        display: none;
    }

    .header-right {
        gap: 6px;
    }

    .header-link {
        padding: 6px 10px;
        font-size: 0.85em;
    }
}

