:root {
    --bg-dark: #1e1e1e;
    --bg-card: #2d2d2d;
    --text-primary: #ffffff;
    --text-secondary: #aaaaaa;
    --primary-color: #0078d4;
    --primary-hover: #005a9e;
    --warning-color: #ff9800;
    --warning-hover: #e68a00;
    --border-color: #444444;
    --input-bg: #333333;
}

body.light-mode {
    --bg-dark: #e8ecf1;
    --bg-card: #ffffff;
    --text-primary: #1a1a1a;
    --text-secondary: #555555;
    --border-color: #cccccc;
    --input-bg: #f5f5f5;
}

body {
    margin: 0;
    font-family: 'Roboto', sans-serif;
    background-color: var(--bg-dark);
    color: var(--text-primary);
    display: flex;
    justify-content: center;
    min-height: 100vh;
    overflow-x: hidden;
}

/* Login overlay */
#login-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: var(--bg-dark);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
}
.login-card {
    background: var(--bg-card);
    padding: 32px 28px;
    border-radius: 8px;
    width: 340px;
    max-width: 90vw;
    box-shadow: 0 4px 16px rgba(0,0,0,0.5);
}
.login-card h2 { margin: 0 0 8px; font-size: 1.2em; }

.app-container {
    width: 100%;
    max-width: 900px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 10px;
    position: relative;
}

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

/* Hamburger — hidden on desktop */
.hamburger-btn { display: none !important; }

/* Nav links — always visible on desktop */
.header-nav {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Recording bar — second row below header */
.recording-bar {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 6px 12px;
    margin-bottom: 4px;
}
.recording-dot { color: #d32f2f; font-size: 1.4em; line-height: 1; animation: pulse 1s infinite; }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:0.3} }

.status-chip {
    padding: 6px 12px;
    border-radius: 16px;
    font-size: 0.85em;
    font-weight: 500;
}

.status-chip.disconnected { background-color: #d32f2f; color: white; }
.status-chip.connected { background-color: #388e3c; color: white; }

.card {
    background-color: var(--bg-card);
    border-radius: 8px;
    padding: 20px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.3);
    overflow-x: hidden;
    max-width: 100%;
}

/* Stack cards with consistent spacing */
.app-content {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.tabs {
    display: flex;
    gap: 10px;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 20px;
    scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }

.tab-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    padding: 10px 15px;
    cursor: pointer;
    font-size: 1em;
    border-bottom: 2px solid transparent;
    transition: all 0.2s;
}

.tab-btn:hover {
    color: var(--text-primary);
}

.tab-btn.active {
    color: var(--primary-color);
    border-bottom-color: var(--primary-color);
}

.radio-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 20px;
}

.grid-inputs {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 15px;
    margin-bottom: 20px;
}

.input-group {
    display: flex;
    flex-direction: column;
    gap: 5px;
}

input[type="text"], input[type="number"], .grid-inputs select {
    background-color: var(--input-bg);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 10px;
    border-radius: 4px;
    font-size: 1em;
}

.btn {
    padding: 10px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 1em;
    font-weight: 500;
    transition: background-color 0.2s;
}

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

.btn-warning { background-color: var(--warning-color); color: black; }
.btn-warning:hover { background-color: var(--warning-hover); }

.app-log pre {
    background-color: #111;
    border: 1px solid #2a2a2a;
    padding: 15px;
    border-radius: 4px;
    height: 150px;
    min-height: 80px;
    max-height: 80vh;
    max-width: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    color: #4db8ff;
    font-family: monospace;
    font-size: 0.9em;
    margin: 0;
    resize: vertical;
    white-space: pre-wrap;
    word-break: break-all;
}
body.light-mode .app-log pre {
    background-color: #e4e4e4;
    border-color: #c8c8c8;
    color: #1a6d9a;
}

/* Device type badges */
.type-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.75em;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.type-badge.type-wificonnect { background: #1565c0; color: #fff; }
.type-badge.type-zgateway    { background: #6a1b9a; color: #fff; }
.type-badge.type-zconnect    { background: #00695c; color: #fff; }
.type-badge.type-zsensor     { background: #e65100; color: #fff; }
.type-badge.type-zinput      { background: #2e7d32; color: #fff; }
.type-badge.type-zrepeater   { background: #4e342e; color: #fff; }

/* Device row */
.device-row:hover { background: rgba(255,255,255,0.03); }

/* Select device button */
.btn-select-device {
    background: var(--border-color);
    color: var(--text-primary);
    border: none;
    border-radius: 3px;
    cursor: pointer;
}
.btn-select-device:hover { background: var(--primary-color); color: #fff; }

/* Header toggle buttons */
.btn-header {
    background: var(--bg-card);
    color: var(--text-secondary);
    padding: 6px 14px;
    font-size: 0.85em;
    line-height: 1.4;
    border: 1px solid var(--border-color);
    vertical-align: middle;
}
.btn-header:hover { background: var(--primary-color); color: #fff; }
.btn-header.active { background: var(--primary-color); color: #fff; border-color: var(--primary-color); }
.btn-header .material-icons { font-size: 1.15em; vertical-align: middle; }

/* Help overlay + slide-in panel */
#help-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.5); z-index: 100;
}
.help-panel {
    position: fixed; top: 0; right: 0; width: 520px; max-width: 100%; height: 100%;
    background: var(--bg-card); color: var(--text-primary); z-index: 101;
    padding: 24px 20px; overflow-y: auto;
    box-shadow: -4px 0 12px rgba(0,0,0,0.3);
    transform: translateX(100%);
    transition: transform 0.3s ease;
}
.help-panel.open { transform: translateX(0); }
.help-panel h2 { margin-top: 0; color: var(--text-primary); font-size: 1.2em; border-bottom: 1px solid var(--border-color); padding-bottom: 10px; }
.help-panel h3 { margin: 18px 0 6px; font-size: 0.95em; color: var(--primary-color); }
.help-panel ul { margin: 4px 0 12px; padding-left: 18px; }
.help-panel li { margin: 3px 0; font-size: 0.82em; line-height: 1.45; color: var(--text-secondary); }
.help-panel code { background: var(--input-bg); padding: 1px 5px; border-radius: 3px; font-size: 0.9em; }

/* ================================================================
   Responsive — mobile & tablet
   ================================================================ */

@media (max-width: 900px) {
    .app-container { padding: 16px 14px; gap: 10px; }

    /* Header — title smaller, controls wrap */
    .app-header {
        flex-wrap: wrap;
        gap: 6px;
        padding-bottom: 6px;
    }
    .app-header h1 { font-size: 1.05em; margin: 0; flex: 1; }

    .header-controls { gap: 4px; flex: 0; }

    /* Show hamburger, hide nav by default */
    .hamburger-btn { display: inline-flex !important; }
    .header-nav {
        display: none;
        position: absolute;
        top: 100%;
        right: 0;
        background: var(--bg-card);
        border: 1px solid var(--border-color);
        border-radius: 6px;
        flex-direction: column;
        gap: 0;
        padding: 4px 0;
        z-index: 50;
        min-width: 160px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.4);
    }
    .header-nav.open { display: flex; }
    .header-nav .btn-header {
        border: none;
        border-radius: 0;
        width: 100%;
        text-align: left;
        padding: 12px 16px;
        font-size: 0.95em;
    }
    .header-nav .btn-header:hover { background: rgba(255,255,255,0.06); }

    .btn-header {
        padding: 8px 12px;
        font-size: 0.85em;
        min-height: 40px;
    }
    #sel-active-device { max-width: 100% !important; flex: 1; min-width: 0; }

    /* Cards — tighter padding */
    .card { padding: 12px; }

    /* Tabs — horizontal scroll without visible scrollbar */
    .tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        gap: 2px;
        flex-wrap: nowrap;
        padding-bottom: 2px;
        margin-bottom: 14px;
    }
    .tabs::-webkit-scrollbar { display: none; }
    .tab-btn {
        padding: 10px 12px;
        font-size: 0.88em;
        white-space: nowrap;
        flex-shrink: 0;
        min-height: 40px;
    }

    /* Grid inputs — single column */
    .grid-inputs {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    /* Radio groups — more spacing for fingers */
    .radio-group { gap: 8px; }
    .radio-group label {
        padding: 6px 0;
        font-size: 0.95em;
        display: flex;
        align-items: center;
        gap: 8px;
    }
    .radio-group input[type="radio"] {
        width: 20px; height: 20px;
    }

    /* Device list rows — stack vertically */
    .device-row {
        flex-wrap: wrap;
        gap: 4px !important;
        padding: 8px 0 !important;
    }

    /* Buttons — larger touch area */
    .btn {
        padding: 12px 18px;
        font-size: 0.95em;
        min-height: 44px;
    }

    /* Inputs — larger for fingers, prevent iOS zoom */
    input[type="text"], input[type="number"], select, .grid-inputs select {
        font-size: 16px !important;
        padding: 12px 10px;
        min-height: 44px;
    }

    /* Log — reduce height, disable resize */
    .app-log pre {
        height: 120px;
        font-size: 0.78em;
        padding: 10px;
        resize: none;
    }

    .help-panel { padding: 16px 14px; }
    .help-panel h2 { font-size: 1em; }
    .help-panel h3 { font-size: 0.88em; }
    .help-panel li { font-size: 0.82em; }

    /* Tighter spacing for panels */
    .input-group label { font-size: 0.9em; }
}

@media (max-width: 480px) {
    .app-header h1 { font-size: 0.95em; }

    .tab-btn { padding: 8px 10px; font-size: 0.82em; min-height: 36px; }

    .device-row { font-size: 0.82em; }
    .type-badge { font-size: 0.72em; padding: 2px 6px; }

    .app-log pre { height: 100px; font-size: 0.72em; }

    .btn { padding: 10px 14px; font-size: 0.9em; min-height: 40px; }
    input[type="text"], input[type="number"], select, .grid-inputs select {
        padding: 10px 8px;
        min-height: 40px;
    }
}
