/* ==========================================================================
   1. ROOT & THEME VARIABLES
   ========================================================================== */
:root {
    --font-primary: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --font-monospace: 'SF Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', monospace;

    /* Light Theme */
    --bg-color: #f0f2f5;
    --surface-color: #ffffff;
    --text-primary: #1c1c1e;
    --text-secondary: #6e6e73;
    --text-tertiary: #8a8a8e;
    --border-color: #e1e4e8;
    --modal-bg: #ffffff;
    --icon-color: #555555;
    --accent-color: #007AFF;
    --accent-color-dark: #0056b3;
    --danger-bg: #ff3b30;
    --danger-text: #ffffff;
    --success-bg: #34c759;
    --success-text: #ffffff;
    --warn-bg: #ff9500;
    --warn-text: #ffffff;
    --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    --shadow-strong: 0 15px 50px rgba(0, 0, 0, 0.15);
}

body.dark-mode {
    --bg-color: #000000;
    --surface-color: #1a1a1a;
    --text-primary: #ffffff;
    --text-secondary: #a0a0a5;
    --text-tertiary: #5a5a5f;
    --border-color: #2c2c2e;
    --modal-bg: #121212;
    --icon-color: #e5e5e5;
    --accent-color: #0A84FF;
    --accent-color-dark: #004c8f;
    --danger-bg: #ff453a;
    --danger-text: #ffffff;
    --success-bg: #30d158;
    --success-text: #ffffff;
    --warn-bg: #ff9f0a;
    --warn-text: #ffffff;
    --shadow: 0 1px 0 rgba(255, 255, 255, 0.1);
    --shadow-strong: 0 15px 50px rgba(0, 0, 0, 0.3);
}


/* ==========================================================================
   2. GLOBAL RESETS & BASE STYLES
   ========================================================================== */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    height: 100%;
    overscroll-behavior: none;
}

body {
    font-family: var(--font-primary);
    background-color: var(--bg-color);
    color: var(--text-primary);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: background-color 0.3s ease, color 0.3s ease;
    line-height: 1.5;
}


/* ==========================================================================
   3. LAYOUT & CORE COMPONENTS
   ========================================================================== */
main {
    flex-grow: 1;
    display: flex;
}

textarea#textInput {
    flex-grow: 1;
    width: 100%;
    border: none;
    resize: none;
    outline: none;
    background-color: transparent;
    color: var(--text-primary);
    font-family: var(--font-primary);
    font-size: clamp(1.5rem, 5vw, 2.25rem); /* Responsive font size */
    line-height: 1.45;
    padding: max(20px, env(safe-area-inset-top)) 25px 280px 25px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
}

footer {
    position: fixed;
    inset: auto 0 0 0; /* Modern way to set bottom, left, right */
    padding: 12px 16px max(12px, env(safe-area-inset-bottom));
    background-color: var(--surface-color);
    box-shadow: var(--shadow);
    z-index: 100;
    transition: background-color 0.3s ease;
}


/* ==========================================================================
   4. BUTTONS & INTERACTIVE ELEMENTS
   ========================================================================== */
button,
[role="button"] {
    border: none;
    background: none;
    cursor: pointer;
    font-family: inherit;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    -webkit-user-select: none;
    outline: none;
    transition: transform 0.15s cubic-bezier(0.4, 0, 0.2, 1), background-color 0.2s ease, opacity 0.2s ease;
}

button:active {
    transform: scale(0.96);
}

.icon {
    width: 1.25em; /* Use em for size relative to font-size */
    height: 1.25em;
    fill: currentColor;
}

#playBtn, #pauseBtn {
    width: 100%;
    padding: 20px;
    border-radius: 20px;
    font-size: 1.2rem;
    background-color: var(--accent-color);
    color: var(--surface-color);
    font-weight: 700;
    gap: 12px;
    min-height: 64px;
}

#pauseBtn {
    background-color: var(--warn-bg);
}

#playBtn.speaking {
    background-color: var(--warn-bg);
    color: var(--warn-text);
    box-shadow: 0 4px 15px -2px #c87500;
}

#playBtn.waiting {
    opacity: 0.7;
    cursor: wait;
}

#clearBtn, #templatesBtn {
    position: fixed;
    z-index: 101;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
    padding: 18px 32px;
    border-radius: 50px;
    font-size: 1.1rem;
    font-weight: 700;
    gap: 10px;
    min-height: 56px;
    transition: opacity 0.3s ease;
    min-width: 200px;
}

#clearBtn {
    inset: auto 25px calc(102px + env(safe-area-inset-bottom)) auto;
    background-color: var(--danger-bg);
    color: var(--danger-text);
}

#templatesBtn {
    inset: auto 25px calc(180px + env(safe-area-inset-bottom)) auto;
    background-color: var(--success-bg);
    color: var(--success-text);
}

#settingsBtn {
    position: absolute;
    inset: max(12px, env(safe-area-inset-top)) 12px auto auto;
    z-index: 1000;
    background: rgba(128, 128, 128, 0.15);
    backdrop-filter: blur(15px);
    -webkit-backdrop-filter: blur(15px);
    padding: 15px;
    border-radius: 50%;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
    min-width: 56px;
    min-height: 56px;
    transition: opacity 0.3s ease;
}

#settingsBtn svg {
    fill: var(--icon-color);
    width: 26px;
    height: 26px;
}

#forceUpdateBtn {
    padding: 8px;
    background: var(--accent-color);
    border-radius: 10px;
    color: var(--surface-color);
}

#forceUpdateBtn .icon {
    width: 20px;
    height: 20px;
}


/* ==========================================================================
   5. MODAL & SETTINGS
   ========================================================================== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background-color: rgba(0, 0, 0, 0.65);
    z-index: 2000;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
}

.modal-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.modal-content {
    background-color: var(--modal-bg);
    color: var(--text-primary);
    width: clamp(300px, 95vw, 600px); /* Responsive width */
    max-height: 90dvh;
    overflow-y: auto;
    border-radius: 24px;
    padding: 20px;
    box-shadow: var(--shadow-strong);
    transform: scale(0.95) translateY(10px);
    transition: transform 0.3s cubic-bezier(0.19, 1, 0.22, 1), background-color 0.3s ease;
    border: 1px solid var(--border-color);
}

.modal-overlay.active .modal-content {
    transform: scale(1) translateY(0);
}

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

.modal-header h2 {
    font-size: 1.3rem;
    font-weight: 700;
}

.version-container {
    display: flex;
    align-items: center;
    gap: 12px;
}

#versionDisplay {
    font-size: 0.9rem;
    color: var(--text-secondary);
    font-weight: 600;
    font-family: var(--font-monospace);
}

.settings-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 15px;
}

.setting-item.full-width {
    grid-column: 1 / -1;
}

.setting-item {
    background-color: var(--surface-color);
    border-radius: 12px;
    border: 1px solid var(--border-color);
    overflow: hidden;
}

.setting-label {
    font-weight: 600;
    font-size: 0.95rem;
}

.toggle-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
}

.close-btn {
    width: 100%;
    padding: 16px;
    background-color: var(--success-bg);
    color: var(--success-text);
    border-radius: 12px;
    margin-top: 20px;
    font-size: 1rem;
    font-weight: 700;
}


/* ==========================================================================
   6. FORM ELEMENTS & CUSTOM CONTROLS
   ========================================================================== */
/* Custom Select */
select#voiceSelect {
    -webkit-appearance: none;
    appearance: none;
    background-color: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 12px;
    font-size: 0.9rem;
    color: var(--text-primary);
    width: 100%;
    outline: none;
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23888'%3e%3cpath d='M8 11.25a.625.625 0 0 1-.442-.183l-3.5-3.5a.625.625 0 1 1 .884-.884L8 9.932l3.058-3.057a.625.625 0 0 1 .884.884l-3.5 3.5a.625.625 0 0 1-.442.183Z'/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 12px center;
    background-size: 16px;
    cursor: pointer;
}

/* Accordion/Submenu */
details.submenu {
    background-color: transparent; /* Let parent .setting-item handle bg */
}

summary.setting-label {
    cursor: pointer;
    padding: 14px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    list-style: none; /* Hide marker in Firefox */
}
summary.setting-label::-webkit-details-marker { display: none; } /* Hide marker in Chrome/Safari */

.arrow-icon {
    width: 18px;
    height: 18px;
    transition: transform 0.3s ease;
    fill: var(--text-tertiary);
}

details[open] > summary .arrow-icon {
    transform: rotate(180deg);
}

.submenu-content {
    padding: 0 14px 14px;
    border-top: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* Range Sliders */
.slider-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.slider-group .setting-label {
    display: flex;
    justify-content: space-between;
}

.range-value {
    font-weight: 600;
    color: var(--accent-color);
    font-size: 0.9em;
}

input[type=range] {
    -webkit-appearance: none;
    width: 100%;
    background: transparent;
    margin: 6px 0;
    cursor: pointer;
}

input[type=range]::-webkit-slider-runnable-track {
    width: 100%;
    height: 6px;
    background: var(--bg-color);
    border-radius: 6px;
    border: 1px solid var(--border-color);
}

input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    height: 24px;
    width: 24px;
    border-radius: 50%;
    background: #ffffff;
    border: 1px solid var(--border-color);
    box-shadow: 0 1px 4px rgba(0,0,0,0.15);
    margin-top: -10px; /* (thumb height - track height) / 2 */
}

/* Toggle Switch */
.switch {
    position: relative;
    width: 48px;
    height: 28px;
    flex-shrink: 0;
}

.switch input { display: none; }

.slider {
    position: absolute;
    cursor: pointer;
    inset: 0;
    background-color: var(--bg-color);
    border: 1px solid var(--border-color);
    transition: background-color .3s;
    border-radius: 28px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 22px;
    width: 22px;
    left: 2px;
    top: 2px;
    background-color: white;
    box-shadow: 0 1px 2px rgba(0,0,0,0.2);
    transition: transform .3s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: var(--success-bg);
    border-color: var(--success-bg);
}

input:checked + .slider:before {
    transform: translateX(20px);
}


/* ==========================================================================
   7. TABLET LANDSCAPE OPTIMIZATIONS
   ========================================================================== */
@media (orientation: landscape) and (min-width: 768px) {
    textarea#textInput {
        font-size: clamp(1.75rem, 4vw, 2.5rem);
        padding: 25px 30px 300px 30px;
    }

    #playBtn {
        min-height: 72px;
        font-size: 1.35rem;
    }

    #clearBtn {
        min-height: 64px;
        font-size: 1.2rem;
        padding: 20px 36px;
    }

    #settingsBtn {
        min-width: 64px;
        min-height: 64px;
        padding: 19px;
    }

    #settingsBtn svg {
        width: 30px;
        height: 30px;
    }

    .modal-content {
        max-width: 700px;
        max-height: 85vh;
    }
}
