:root {
    --bg-dark: #050505;
    --surface-1: #121212;
    --surface-2: #1a1a1a;
    --surface-3: #252525;
    --glass-bg: rgba(20, 20, 20, 0.7);
    --glass-border: rgba(255, 255, 255, 0.08);
    --glass-highlight: rgba(255, 255, 255, 0.05);
    --text-primary: #ffffff;
    --text-secondary: #a0a0a0;
    --text-tertiary: #606060;
    --highlight-yellow: #FFD700;
    --accent-primary: #FFD700;
    --accent-glow: rgba(255, 215, 0, 0.15);
    --danger: #ff4444;
    --font-heading: 'Helvetica Neue', Helvetica, Arial, sans-serif;
    --font-body: 'Inter', sans-serif;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 24px;
    --shadow-soft: 0 10px 30px rgba(0, 0, 0, 0.5);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    outline: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: pan-x pan-y;
}

html {
    /* Previene zoom su input su iOS */
    -webkit-text-size-adjust: 100%;
    /* Altezza corretta su iOS standalone (Dynamic Island) */
    height: -webkit-fill-available;
}

body {
    background-color: var(--bg-dark);
    color: var(--text-primary);
    font-family: var(--font-body);
    min-height: 100vh;
    min-height: -webkit-fill-available;
    -webkit-font-smoothing: antialiased;
    scrollbar-color: var(--surface-3) var(--bg-dark);
    overscroll-behavior: none;
}

/* UI Utilities */
.hidden {
    display: none !important;
    opacity: 0;
    pointer-events: none;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.75rem 1.2rem;
    border-radius: 100px;
    font-weight: 500;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.2, 0.8, 0.2, 1);
    border: 1px solid transparent;
    white-space: nowrap;
    /* Target touch minimo 44px */
    min-height: 44px;
}

.btn-primary {
    background: var(--text-primary);
    color: var(--bg-dark);
    font-weight: 600;
}

.btn-primary:hover { transform: translateY(-2px); box-shadow: 0 5px 15px rgba(255,255,255,0.2); }

.btn-secondary {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-primary);
    border-color: var(--glass-border);
}

.btn-secondary:hover { background: rgba(255,255,255,0.1); border-color: rgba(255,255,255,0.2); }

.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
}

.btn-ghost:hover { color: var(--text-primary); background: var(--glass-highlight); }


/* =====================
   DASHBOARD
   ===================== */
#view-dashboard {
    padding: max(4rem, calc(env(safe-area-inset-top) + 2rem)) 5% max(6rem, calc(env(safe-area-inset-bottom) + 2rem));
    max-width: 1400px;
    margin: 0 auto;
    animation: fadeIn 0.5s ease-out;
}

.dashboard-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 3rem;
    padding-bottom: 2rem;
    border-bottom: 1px solid var(--glass-border);
    gap: 1rem;
    flex-wrap: wrap;
}

.dashboard-header h1 {
    font-family: var(--font-heading);
    font-size: clamp(1.8rem, 5vw, 3.5rem);
    letter-spacing: -0.05em;
    background: linear-gradient(135deg, #fff 0%, #888 100%);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    flex-shrink: 0;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.speech-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1.5rem;
}

.speech-card {
    background: linear-gradient(180deg, var(--surface-2) 0%, var(--surface-1) 100%);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-md);
    padding: 1.5rem;
    min-height: 180px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    transition: all 0.3s ease;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.speech-card:hover { transform: translateY(-4px); box-shadow: 0 20px 40px rgba(0,0,0,0.4); border-color: rgba(255,255,255,0.15); }

.speech-card::after {
    content: '';
    position: absolute;
    inset: 0;
    box-shadow: inset 0 0 20px rgba(255,255,255,0.03);
    pointer-events: none;
}

.speech-card h3 { font-size: 1.2rem; font-weight: 500; margin-bottom: 0.4rem; }
.speech-card .date { font-size: 0.8rem; color: var(--text-tertiary); }

.card-actions {
    display: flex;
    gap: 0.5rem;
    justify-content: flex-end;
    z-index: 2;
}

.card-actions button {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: none;
    background: var(--surface-3);
    color: var(--text-secondary);
    cursor: pointer;
    transition: 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
}

.card-actions button:hover { background: var(--text-primary); color: var(--bg-dark); }
.card-actions .delete-btn:hover { background: var(--danger); color: #fff; }


/* =====================
   EDITOR
   ===================== */
#view-editor {
    height: 100vh;
    height: -webkit-fill-available;
    display: flex;
    flex-direction: column;
    background: radial-gradient(circle at top right, #111 0%, #000 70%);
}

.editor-header {
    height: calc(64px + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) 1rem 0 0.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(10,10,10,0.8);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-bottom: 1px solid var(--glass-border);
    z-index: 20;
    gap: 0.5rem;
    flex-shrink: 0;
}

.left-actions { display: flex; align-items: center; gap: 0.25rem; min-width: 0; }
.right-actions { display: flex; align-items: center; gap: 0.5rem; flex-shrink: 0; }

.editor-title {
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 1rem;
    font-weight: 600;
    /* Larghezza flessibile su mobile */
    width: min(200px, 35vw);
    padding: 0.4rem 0.5rem;
    border-radius: 6px;
    transition: 0.2s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.editor-title:focus { background: var(--surface-2); color: #fff; }

.editor-toolbar {
    padding: 0.6rem 1rem;
    background: rgba(10,10,10,0.6);
    border-bottom: 1px solid var(--glass-border);
    display: flex;
    gap: 0.5rem;
    align-items: center;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    z-index: 10;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    flex-shrink: 0;
}

.editor-toolbar::-webkit-scrollbar { display: none; }

.separator {
    width: 1px;
    height: 24px;
    background: var(--glass-border);
    flex-shrink: 0;
}

.tool-btn {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    font-size: 1.1rem;
    cursor: pointer;
    transition: 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.tool-btn.color-btn {
    background-color: currentColor;
    border-radius: 50%;
    width: 28px;
    height: 28px;
}

.tool-btn.color-btn i { display: none; }
.tool-btn:hover { background: var(--surface-3); color: #fff; }
.tool-btn.active { background: var(--accent-primary); color: #000; box-shadow: 0 0 15px var(--accent-glow); }

.editor-canvas-container {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

#editor-canvas {
    flex: 1;
    padding: 2rem 5%;
    color: #e0e0e0;
    font-size: clamp(1rem, 3vw, 1.6rem);
    line-height: 1.6;
    outline: none;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    /* Impedisce zoom su doppio tap iOS */
    touch-action: manipulation;
}

#editor-canvas mark {
    background-color: var(--accent-primary) !important;
    color: #000 !important;
    padding: 0 4px;
    border-radius: 3px;
}

#editor-canvas u {
    text-decoration-color: var(--accent-primary);
    text-decoration-thickness: 3px;
}


/* =====================
   PROMPTER
   ===================== */
#view-prompter {
    background: #000;
    position: relative;
    /* Altezza corretta su iOS includendo safe area */
    height: 100vh;
    height: -webkit-fill-available;
}

#prompter-content-wrapper {
    height: 100%;
    overflow-y: scroll;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

#prompter-content-wrapper::-webkit-scrollbar { display: none; }
#prompter-content-wrapper { -ms-overflow-style: none; scrollbar-width: none; }

#scrolling-text {
    width: 100%;
    padding: calc(50vh + env(safe-area-inset-top)) 5vw calc(50vh + env(safe-area-inset-bottom) + 100px) 9vw;
    /* font-size gestito interamente da JS tramite applyFontSize() */
    font-size: 6rem;
    line-height: 1.5;
    font-weight: 600;
    outline: none;
    color: #fff;
}

#scrolling-text mark {
    background-color: var(--highlight-yellow) !important;
    color: #000 !important;
    padding: 0 4px;
    border-radius: 4px;
    display: inline-block;
}

#scrolling-text u {
    text-decoration: underline;
    text-decoration-color: var(--highlight-yellow);
    text-decoration-thickness: 4px;
    text-underline-offset: 6px;
}

#scrolling-text p { margin-bottom: 1.5rem; }
#scrolling-text ul, #scrolling-text ol { margin-left: 2rem; margin-bottom: 1.5rem; }
#scrolling-text li { margin-bottom: 0.75rem; padding-left: 0.5rem; }

/* Control Bar */
#control-bar {
    position: fixed;
    bottom: max(1.5rem, calc(env(safe-area-inset-bottom) + 0.75rem));
    left: 50%;
    transform: translateX(-50%);
    width: 94%;
    max-width: 800px;
    height: 72px;
    background: rgba(10,10,10,0.92);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 100px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.2rem;
    box-shadow: 0 10px 40px rgba(0,0,0,0.5);
    z-index: 350;
    opacity: 1;
    transition: opacity 0.4s ease;
}

.control-group { display: flex; gap: 1rem; align-items: center; }

.play-btn {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: #fff;
    color: #000;
    border: none;
    font-size: 1.6rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.play-btn:hover { transform: scale(1.1); box-shadow: 0 0 20px rgba(255,255,255,0.3); }

.icon-btn {
    background: transparent;
    border: none;
    color: #fff;
    font-size: 1.4rem;
    cursor: pointer;
    opacity: 0.7;
    /* Area touch minima 44px */
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
}

.icon-btn:hover { opacity: 1; }

.slider-container {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    color: #fff;
}

input[type=range] {
    -webkit-appearance: none;
    appearance: none;
    background: transparent;
    width: 70px;
}

input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    height: 18px;
    width: 18px;
    border-radius: 50%;
    background: #fff;
    margin-top: -7px;
    cursor: pointer;
}

input[type=range]::-webkit-slider-runnable-track {
    width: 100%;
    height: 4px;
    background: #333;
    cursor: pointer;
    border-radius: 2px;
}

/* Touch controls (nascosti su desktop, visibili su mobile) */
.touch-controls {
    display: none;
    align-items: center;
    gap: 0.2rem;
}

.touch-ctrl-btn {
    width: 40px;
    height: 40px;
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

.touch-ctrl-label {
    color: rgba(255,255,255,0.5);
    font-size: 1rem;
    display: flex;
    align-items: center;
}

.touch-ctrl-divider {
    width: 1px;
    height: 20px;
    background: rgba(255,255,255,0.15);
    margin: 0 0.2rem;
}

/* Reading Indicator */
#reading-indicator {
    position: fixed;
    top: 50%;
    left: 0;
    transform: translateY(-50%);
    width: 0;
    height: 0;
    border-top: 24px solid transparent;
    border-bottom: 24px solid transparent;
    border-left: 32px solid var(--highlight-yellow);
    z-index: 9998;
    pointer-events: none;
}

#reading-indicator.hidden {
    display: none !important;
}


/* =====================
   TOAST
   ===================== */
#toast-container {
    position: fixed;
    bottom: max(2rem, calc(env(safe-area-inset-bottom) + 1rem));
    right: 1rem;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    max-width: calc(100vw - 2rem);
}

.toast {
    background: var(--surface-2);
    border: 1px solid var(--glass-border);
    padding: 0.875rem 1.25rem;
    border-radius: var(--radius-md);
    color: #fff;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    box-shadow: var(--shadow-soft);
    animation: toastSlide 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
    min-width: 220px;
    font-size: 0.9rem;
}

.toast-success { border-left: 4px solid #4CAF50; }
.toast-error { border-left: 4px solid var(--danger); }
.toast i { font-size: 1.1rem; }


/* =====================
   IMPORT OVERLAY
   ===================== */
#upload-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.7);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 1rem;
    animation: fadeIn 0.3s ease-out;
}

.upload-content {
    background: linear-gradient(135deg, var(--surface-2) 0%, var(--surface-1) 100%);
    border: 1px solid var(--glass-border);
    padding: 2rem;
    border-radius: var(--radius-lg);
    box-shadow: 0 40px 80px rgba(0,0,0,0.6);
    width: 100%;
    max-width: 460px;
    text-align: center;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.25rem;
}

.upload-content .upload-icon {
    font-size: 2.5rem;
    color: var(--accent-primary);
    filter: drop-shadow(0 0 15px var(--accent-glow));
}

.upload-content h2 { font-family: var(--font-heading); font-size: 1.6rem; color: var(--text-primary); }
.upload-content p { color: var(--text-secondary); line-height: 1.5; font-size: 0.9rem; }

.close-overlay {
    position: absolute;
    top: 1rem;
    right: 1rem;
    background: transparent;
    border: none;
    color: var(--text-tertiary);
    font-size: 1.4rem;
    cursor: pointer;
    transition: 0.2s;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.close-overlay:hover { background: var(--surface-3); color: #fff; }

/* Bold Text */
strong, b { color: var(--highlight-yellow); font-weight: 700; }


/* iOS Fullscreen Tip */
#ios-fs-tip {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.75);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    animation: fadeIn 0.25s ease-out;
}

.ios-tip-content {
    background: var(--surface-2);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-lg);
    padding: 2rem 1.5rem;
    text-align: center;
    max-width: 300px;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
}

.ios-tip-content strong {
    color: var(--text-primary);
    font-size: 1rem;
    -webkit-text-fill-color: var(--text-primary);
}

.ios-tip-content p {
    color: var(--text-secondary);
    font-size: 0.9rem;
    line-height: 1.6;
    margin: 0.25rem 0 1rem;
}

.ios-tip-content p strong {
    color: #fff;
    -webkit-text-fill-color: #fff;
}

.ios-tip-content button {
    background: var(--text-primary);
    color: var(--bg-dark);
    border: none;
    border-radius: 100px;
    padding: 0.6rem 1.5rem;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    font-family: var(--font-body);
    min-height: 44px;
}

/* =====================
   ANIMATIONS
   ===================== */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(20px); }
    to   { opacity: 1; transform: translateY(0); }
}

@keyframes toastSlide {
    from { opacity: 0; transform: translateX(50px); }
    to   { opacity: 1; transform: translateX(0); }
}


/* =====================
   RESPONSIVE — TABLET (max 768px)
   ===================== */
@media (max-width: 768px) {

    #view-dashboard {
        padding: max(2rem, calc(env(safe-area-inset-top) + 1.5rem)) 4% max(5rem, calc(env(safe-area-inset-bottom) + 2rem));
    }

    .dashboard-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 1rem;
        margin-bottom: 2rem;
    }

    .header-actions {
        width: 100%;
        justify-content: flex-start;
    }

    .header-actions .btn {
        font-size: 0.8rem;
        padding: 0.65rem 1rem;
    }

    /* Nascondi label testo sui bottoni secondari, tieni solo icona */
    #btn-export-all span, #btn-export-all { font-size: 0.8rem; }

    .speech-grid {
        grid-template-columns: 1fr 1fr;
        gap: 1rem;
    }

    .speech-card { min-height: 140px; padding: 1.2rem; }
    .speech-card h3 { font-size: 1rem; }

    .editor-title { width: min(160px, 30vw); font-size: 0.9rem; }

    #editor-canvas { padding: 1.5rem 4%; font-size: 1.1rem; }

    /* Prompter control bar su mobile: più compatta */
    #control-bar {
        height: 66px;
        padding: 0 1rem;
        width: 96%;
    }

    .control-group { gap: 0.5rem; }

    input[type=range] { width: 55px; }

    .slider-container { gap: 0.25rem; font-size: 0.9rem; }
}


/* =====================
   RESPONSIVE — MOBILE (max 480px)
   ===================== */
@media (max-width: 480px) {

    #view-dashboard { padding: max(1.5rem, calc(env(safe-area-inset-top) + 1rem)) 4% max(6rem, calc(env(safe-area-inset-bottom) + 2rem)); }

    .dashboard-header { margin-bottom: 1.5rem; padding-bottom: 1.5rem; }

    /* Header: titolo sopra, bottoni sotto */
    .header-actions {
        gap: 0.4rem;
    }

    /* Bottoni header più compatti: mostra solo icona + testo breve */
    #btn-export-all { padding: 0.6rem 0.8rem; }
    #btn-import-new { padding: 0.6rem 0.8rem; }
    #btn-new-speech { padding: 0.6rem 0.9rem; }

    .speech-grid { grid-template-columns: 1fr; gap: 0.875rem; }

    .speech-card { min-height: 120px; padding: 1rem; }

    /* Editor: toolbar scorribile, testo più grande per leggibilità */
    .editor-header { height: calc(56px + env(safe-area-inset-top)); padding-top: env(safe-area-inset-top); }

    .editor-title { width: 120px; font-size: 0.85rem; }

    .right-actions .btn { padding: 0.5rem 0.75rem; font-size: 0.8rem; }

    /* Nasconde label "Save" e "Start Prompt" su schermi piccoli */
    #btn-save span, #btn-start-prompter span { display: none; }

    #editor-canvas { padding: 1rem 4%; font-size: 1rem; }

    /* Prompter: testo più grande su telefono verticale */
    #scrolling-text {
        padding: calc(50vh + env(safe-area-inset-top)) 4vw calc(50vh + env(safe-area-inset-bottom) + 100px) 8vw;
        /* font-size gestito da JS */
    }

    /* Control bar su mobile: mostra pulsanti touch, nascondi slider */
    #control-bar { height: 62px; }

    .play-btn { width: 52px; height: 52px; font-size: 1.4rem; }

    /* Nascondi slider, mostra pulsanti touch */
    #speed-control,
    #fontsize-control { display: none; }

    .touch-controls { display: flex; }

    /* Toast più piccolo */
    .toast { min-width: 180px; font-size: 0.85rem; padding: 0.75rem 1rem; }
}


/* =====================
   LANDSCAPE MOBILE
   ===================== */
@media (max-height: 500px) and (orientation: landscape) {

    /* In landscape lo schermo è basso: usiamo padding fisso minimo
       La freccia indicatore è al 50% dell'altezza — il testo deve scorrerci attorno */
    #scrolling-text {
        padding-top: 30vh;
        padding-bottom: calc(30vh + 56px); /* spazio per la control bar */
        padding-left: calc(max(4vw, env(safe-area-inset-left) + 1rem) + 32px); /* spazio per la freccia */
        padding-right: max(4vw, env(safe-area-inset-right) + 1rem);
        /* font-size gestito da JS */
    }

    /* Control bar landscape: compatta, attaccata in basso */
    #control-bar {
        height: 48px;
        bottom: max(0.25rem, env(safe-area-inset-bottom));
        left: max(1rem, calc(env(safe-area-inset-left) + 0.5rem));
        right: max(1rem, calc(env(safe-area-inset-right) + 0.5rem));
        /* override del left/transform per usare left+right invece di center */
        transform: none;
        width: auto;
        border-radius: 12px;
        padding: 0 0.75rem;
    }

    /* Freccia: rimane al 50% verticale, già in posizione corretta */
    #reading-indicator {
        border-top: 18px solid transparent;
        border-bottom: 18px solid transparent;
        border-left: 24px solid var(--highlight-yellow);
    }

    .play-btn { width: 40px; height: 40px; font-size: 1.1rem; }

    .icon-btn { width: 32px; height: 32px; font-size: 1rem; }

    /* In landscape nascondi slider, mostra touch controls */
    #speed-control,
    #fontsize-control { display: none; }

    .touch-controls { display: flex; gap: 0.25rem; }

    .touch-ctrl-btn { width: 32px; height: 32px; font-size: 0.85rem; }
}