:root {
    color-scheme: dark light;
}

/* ROOT & BODY SETUP */
body {
    font-family: sans-serif;
    background-color: #121212;
    color: #e0e0e0;
    transition: background-color 0.3s, color 0.3s;
    margin: 0;
    padding: 20px 10px;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    min-height: 100vh;
    box-sizing: border-box;
    overflow: auto; /* Default scroll behavior */
}

body.light-mode {
    background-color: #f0f0f0;
    color: #000;
}

/* --- MOBILE MODE LOCK --- */
body.mobile-mode {
    overflow: hidden; /* Disables scrollbars */
    touch-action: none; /* Disables browser gestures globally */
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    padding: 0;
    align-items: center; /* Center vertically when locked */
}

/* --- SCALING CONTAINERS --- */
#app-container {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    flex-grow: 1;
}

#app-screen {
    width: 100%;
    max-width: 850px;
    display: flex;
    flex-direction: column;
    align-items: center;
    transform-origin: center center;
    flex-shrink: 0;
}

body.mobile-mode #app-screen {
    width: 850px; /* Fixed Base Width for scaling */
    height: 950px; /* Fixed Base Height for scaling */
    justify-content: center;
}

/* --- TOGGLE BUTTONS --- */
#mobile-btn, #dark-mode-toggle {
    position: fixed;
    top: 10px;
    z-index: 2000;
    padding: 8px 12px;
    cursor: pointer;
    background: #333;
    color: #fff;
    border: 1px solid #555;
    border-radius: 5px;
    font-size: 14px;
}

body.light-mode #mobile-btn, 
body.light-mode #dark-mode-toggle {
    background: #ddd; 
    color: #000; 
    border: 1px solid #bbb;
}

#mobile-btn { left: 10px; }
#dark-mode-toggle { right: 10px; }

/* Hide buttons when in mobile fullscreen mode */
body.mobile-mode #mobile-btn,
body.mobile-mode #dark-mode-toggle { 
    display: none; 
}

/* --- MAIN UI STYLES --- */
h1 { margin-top: 20px; margin-bottom: 10px; }
h2 { font-size: 1.2em; border-bottom: 1px solid #444; padding-bottom: 5px; margin-top: 0; }
body.light-mode h2 { border-bottom-color: #ccc; }

#audio-status {
    margin: 5px auto 15px;
    padding: 8px 12px;
    font-weight: bold;
    border-radius: 5px;
    min-width: 250px;
    text-align: center;
}
#audio-status.ready { color: #33dd33; background-color: rgba(51, 221, 51, 0.1); }
#audio-status.error { color: #ff5555; background-color: rgba(255, 85, 85, 0.1); }
#audio-status.suspended { color: #ffaa00; background-color: rgba(255, 170, 0, 0.1); }

.panel {
    background: #282828;
    padding: 20px;
    border-radius: 8px;
    margin-bottom: 20px;
    width: 100%;
    box-sizing: border-box;
    box-shadow: 0 4px 6px rgba(0,0,0,0.3);
}
body.light-mode .panel { background: #d8d8d8; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }

/* --- TIGHTEN UI IN MOBILE MODE --- */
body.mobile-mode h1 { margin: 0 0 5px 0; font-size: 1.8em; }
body.mobile-mode #audio-status { margin-bottom: 5px; padding: 4px 8px; }
body.mobile-mode .panel { margin-bottom: 10px; padding: 15px; }
body.mobile-mode h2 { margin-bottom: 10px; }
body.mobile-mode .action-buttons { margin: 10px 0; }

.controls-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 20px;
    margin-bottom: 10px;
}
.control-group {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
}
.control-group label { font-size: 0.9em; font-weight: bold; }

/* UNLOCK CHECKBOX STYLE */
.unlock-label {
    font-size: 0.8em !important;
    display: flex;
    align-items: center;
    gap: 4px;
    margin-top: 4px;
    cursor: pointer;
    opacity: 0.8;
    user-select: none;
}
.unlock-label:hover {
    opacity: 1;
}

select, button, input[type="range"] {
    padding: 5px;
    border-radius: 4px;
    background-color: #333;
    color: #e0e0e0;
    border: 1px solid #555;
}
body.light-mode select, body.light-mode button {
    background-color: #fff;
    color: #000;
    border: 1px solid #ccc;
}
button { cursor: pointer; padding: 8px 15px; font-weight: bold; }
button:active { filter: brightness(1.2); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

/* --- TEXT AREA (WITH SCROLLING FIX) --- */
textarea {
    width: 100%;
    height: 80px;
    background: #1e1e1e;
    color: #fff;
    border: 1px solid #555;
    border-radius: 4px;
    padding: 10px;
    box-sizing: border-box;
    font-size: 1.1em;
    resize: vertical;
    /* OVERRIDE TOUCH-ACTION TO ALLOW SCROLLING IN LOCKED SCREEN */
    touch-action: pan-y; 
    overflow-y: auto;
}
body.light-mode textarea { background: #fff; color: #000; border: 1px solid #ccc; }
body.mobile-mode textarea { height: 70px; } /* Slightly taller for usability */

.action-buttons {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 15px;
    margin: 15px 0;
}
.loop-label { display: flex; align-items: center; gap: 5px; cursor: pointer; }

/* --- VISUAL DISPLAY (WITH SCROLLING FIX) --- */
.visual-display {
    min-height: 60px;
    max-height: 150px; /* Prevent it from expanding out of screen bounds */
    background: #111;
    border: 2px inset #333;
    padding: 15px;
    border-radius: 5px;
    font-size: 2em;
    font-family: monospace;
    letter-spacing: 2px;
    word-wrap: break-word;
    text-align: left;
    color: #ffaa00;
    /* OVERRIDE TOUCH-ACTION TO ALLOW SCROLLING IN LOCKED SCREEN */
    touch-action: pan-y; 
    overflow-y: auto;
}
body.mobile-mode .visual-display { max-height: 120px; } /* Keep it contained on mobile */

body.light-mode .visual-display { background: #eee; border-color: #ccc; color: #dd6600; }

.visual-display span { transition: color 0.1s; }
.visual-display span.active { color: #fff; background: #ffaa00; border-radius: 2px; }
body.light-mode .visual-display span.active { color: #fff; background: #dd6600; }

/* --- NEW / UPDATED DECODER DISPLAYS --- */
.decoder-label {
    display: block;
    font-size: 0.9em;
    font-weight: bold;
    margin-top: 15px;
    margin-bottom: 5px;
    text-align: left;
    width: 100%;
}

.wpm-display {
    text-align: center;
    font-weight: bold;
    color: #33dd33;
    background: rgba(51, 221, 51, 0.1);
    padding: 8px;
    border-radius: 5px;
    margin-top: 10px;
}
body.light-mode .wpm-display {
    color: #1a881a;
    background: rgba(26, 136, 26, 0.1);
}

.raw-display {
    min-height: 50px;
    font-size: 1.5em;
    color: #aaa;
    white-space: pre-wrap; /* allow wrapping for dots and spaces */
    letter-spacing: 3px;
}
body.light-mode .raw-display {
    color: #555;
}

.decoded-display {
    min-height: 80px;
    color: #66aaff; 
    font-size: 1.8em; 
    letter-spacing: normal; 
}
body.light-mode .decoded-display { 
    color: #0055cc; 
}

.instructions { font-size: 0.8em; color: #aaa; margin-top: 10px; text-align: center; }
body.light-mode .instructions { color: #555; }

/* --- SIGNAL METER --- */
#signal-meter {
    width: 150px;
    height: 15px;
    background: #111;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid #555;
}
#signal-level {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, green, yellow, red);
    transition: width 0.05s;
}
