@font-face {
    font-family: 'Comic Neue';
    src: url('/public/fonts/ComicNeue-Regular.ttf') format('truetype');
    font-weight: 400; font-style: normal; font-display: swap;
}
@font-face {
    font-family: 'Comic Neue';
    src: url('/public/fonts/ComicNeue-Bold.ttf') format('truetype');
    font-weight: 700; font-style: normal; font-display: swap;
}

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

:root {
    --primary: #4f5fc6;
    --secondary: #764ba2;
    --success: #287d32;
    --warning: #a45100;
    --danger: #c62828;
    --dark: #2c3e50;
    --light: #ecf0f1;
    --focus: #a45100;
}

body {
    font-family: 'Comic Neue', 'Comic Sans MS', 'Segoe UI', Arial, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    min-height: 100dvh;
    padding: 20px;
    padding-top: calc(20px + env(safe-area-inset-top));
    padding-right: calc(20px + env(safe-area-inset-right));
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
    padding-left: calc(20px + env(safe-area-inset-left));
    transition: background 0.5s ease;
}

body.nightmode {
    --focus: #ffd08a;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
}

.container {
    max-width: 1200px;
    margin: 0 auto;
}

header {
    background: white;
    padding: 30px;
    border-radius: 20px;
    box-shadow: 0 15px 40px rgba(0,0,0,0.2);
    margin-bottom: 30px;
    text-align: center;
    animation: slideDown 0.5s ease;
}

body.nightmode header {
    background: #2c3e50;
    color: white;
}

@keyframes slideDown {
    from { transform: translateY(-50px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

h1 {
    color: var(--primary);
    font-size: 3em;
    margin-bottom: 10px;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
}

body.nightmode h1 {
    color: #f39c12;
}

.subtitle {
    color: #666;
    font-size: 1.2em;
}

body.nightmode .subtitle {
    color: #bdc3c7;
}

.connection-badge {
    display: inline-block;
    padding: 8px 20px;
    border-radius: 20px;
    font-weight: bold;
    margin-top: 15px;
    font-size: 0.9em;
}

.connection-badge.connected { display: none; }
.connection-badge.disconnected { background: var(--danger); color: white; }
.connection-badge.connecting { background: var(--warning); color: white; }

.card {
    background: white;
    padding: 30px;
    border-radius: 20px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.15);
    margin-bottom: 30px;
    animation: fadeIn 0.5s ease;
}

body.nightmode .card {
    background: #34495e;
    color: white;
}

@keyframes fadeIn {
    from { opacity: 0; transform: scale(0.95); }
    to { opacity: 1; transform: scale(1); }
}

.card h2 {
    color: var(--primary);
    margin-bottom: 20px;
    font-size: 2em;
    border-bottom: 3px solid var(--primary);
    padding-bottom: 10px;
}

body.nightmode .card h2 {
    color: #f39c12;
    border-bottom-color: #f39c12;
}

.form-group {
    margin-bottom: 20px;
}

label {
    display: block;
    margin-bottom: 8px;
    color: #333;
    font-weight: 700;
    font-size: 1.1em;
}

body.nightmode label {
    color: #ecf0f1;
}

input, select, textarea {
    width: 100%;
    padding: 15px;
    border: 3px solid #ddd;
    border-radius: 12px;
    font-size: 1.1em;
    transition: all 0.3s;
    font-family: inherit;
}

body.nightmode input,
body.nightmode select,
body.nightmode textarea {
    background: #2c3e50;
    color: white;
    border-color: #34495e;
}

input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 15px rgba(102, 126, 234, 0.3);
    transform: scale(1.02);
}

textarea {
    resize: vertical;
    min-height: 120px;
}

.age-selector {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.age-btn {
    padding: 15px 25px;
    border: 3px solid var(--primary);
    background: white;
    border-radius: 12px;
    cursor: pointer;
    font-size: 1.1em;
    font-weight: 700;
    transition: all 0.3s;
    flex: 1;
    min-width: 80px;
}

body.nightmode .age-btn {
    background: #2c3e50;
    color: white;
}

.age-btn:hover {
    transform: translateY(-3px);
    box-shadow: 0 5px 20px rgba(102, 126, 234, 0.3);
}

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

button {
    padding: 18px 40px;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    font-size: 1.2em;
    font-weight: 700;
    transition: all 0.3s;
    font-family: inherit;
}

button:hover:not(:disabled) {
    transform: translateY(-3px);
    box-shadow: 0 8px 25px rgba(0,0,0,0.3);
}

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary { background: var(--primary); color: white; }
.btn-success { background: var(--success); color: white; }
.btn-warning { background: var(--warning); color: white; }
.btn-danger { background: var(--danger); color: white; }

.btn-group {
    display: flex;
    gap: 15px;
    flex-wrap: wrap;
}

.btn-group button {
    flex: 1;
    min-width: 150px;
}

.story-display {
    background: linear-gradient(135deg, #fff9e6 0%, #ffe6f0 100%);
    padding: 30px;
    border-radius: 20px;
    border: 3px solid var(--primary);
    min-height: 200px;
    font-size: 1.3em;
    line-height: 1.8;
    white-space: pre-wrap;
    position: relative;
    animation: storyAppear 0.5s ease;
}

body.nightmode .story-display {
    background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
    color: #ecf0f1;
}

@keyframes storyAppear {
    from { opacity: 0; transform: scale(0.9); }
    to { opacity: 1; transform: scale(1); }
}

.story-illustration {
    width: 100%;
    max-width: 500px;
    height: auto;
    border-radius: 15px;
    margin: 20px auto;
    display: block;
    box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}

.favorite-btn {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 2em;
    background: none;
    border: none;
    cursor: pointer;
    transition: transform 0.3s;
    padding: 10px;
}

.favorite-btn:hover {
    transform: scale(1.3);
}

.favorite-btn.active {
    color: #e74c3c;
    animation: heartBeat 0.5s ease;
}

@keyframes heartBeat {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.3); }
}

.loading {
    text-align: center;
    padding: 40px;
}

.loading-spinner {
    border: 5px solid #f3f3f3;
    border-top: 5px solid var(--primary);
    border-radius: 50%;
    width: 60px;
    height: 60px;
    animation: spin 1s linear infinite;
    margin: 0 auto 20px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.nightmode-toggle {
    flex: 0 0 52px;
    padding: 0;
    background: white;
    border: none;
    border-radius: 50%;
    width: 52px;
    height: 52px;
    font-size: 2em;
    cursor: pointer;
    box-shadow: 0 5px 20px rgba(0,0,0,0.3);
    transition: all 0.3s;
    z-index: 1000;
}

body.nightmode .nightmode-toggle {
    background: #2c3e50;
}

.nightmode-toggle:hover {
    transform: rotate(180deg) scale(1.1);
}

.favorites-section {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr));
    gap: 20px;
}

.favorite-card {
    min-width: 0;
    overflow-wrap: anywhere;
    background: white;
    padding: 20px;
    border-radius: 15px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.1);
    cursor: pointer;
    transition: all 0.3s;
    position: relative;
}

body.nightmode .favorite-card {
    background: #2c3e50;
    color: white;
}

.favorite-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}

.favorite-card h3 {
    color: var(--primary);
    margin-bottom: 10px;
    padding-right: 36px;
}

body.nightmode .favorite-card h3 {
    color: #f39c12;
}

.favorite-card p {
    font-size: 0.95em;
    color: #666;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

body.nightmode .favorite-card p {
    color: #bdc3c7;
}

.delete-btn {
    padding: 0;
    position: absolute;
    top: 10px;
    right: 10px;
    background: var(--danger);
    color: white;
    border: none;
    border-radius: 50%;
    width: 30px;
    height: 30px;
    cursor: pointer;
    font-size: 1.2em;
    transition: all 0.3s;
}

.delete-btn:hover {
    transform: scale(1.2);
}

.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: #999;
}

.empty-state-icon {
    font-size: 5em;
    margin-bottom: 20px;
}

.personalization-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr));
    gap: 15px;
}

.tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    border-bottom: 3px solid #ddd;
}

.tab {
    padding: 15px 30px;
    background: #f5f5f5;
    border: none;
    border-radius: 12px 12px 0 0;
    cursor: pointer;
    font-weight: 700;
    font-size: 1.1em;
    transition: all 0.3s;
}

body.nightmode .tab {
    background: #2c3e50;
    color: white;
}

.tab:hover {
    background: #e0e0e0;
}

body.nightmode .tab:hover {
    background: #34495e;
}

.tab.active {
    background: var(--primary);
    color: white;
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

/* Focus ben visibile da tastiera */
button:focus-visible, [role="button"]:focus-visible, a:focus-visible, .tab:focus-visible {
    outline: 3px solid var(--focus);
    outline-offset: 2px;
}
.close-modal, .delete-btn { min-width: 44px; min-height: 44px; }
.delete-btn { width: 44px; height: 44px; top: 4px; right: 4px; }
.tab-icon { margin-right: 6px; }
.privacy-note { margin: 6px 0 0; }
.privacy-note ul { padding-left: 22px; line-height: 1.6; font-size: 0.95em; color: #555; }
body.nightmode .privacy-note ul { color: #cfd8e3; }
.install-btn {
    min-height: 44px; padding: 8px 16px; font-size: 1em; border-radius: 999px;
    background: var(--primary); color: white; margin: 0 auto;
}
.install-btn[hidden] { display: none; }
@media (max-width: 420px) { .install-btn { padding: 8px 12px; font-size: .9em; } }
.favorites-tools { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.favorites-tools[hidden] { display: none; }
.favorites-tools input { flex: 1 1 200px; min-width: 0; min-height: 44px; }
.favorites-tools select { flex: 0 1 180px; min-width: 0; min-height: 44px; }
.favorites-backup { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.favorites-section { margin-top: 16px; }
.progress { height: 10px; background: rgba(102,126,234,.2); border-radius: 999px; overflow: hidden; margin: 6px auto 0; max-width: 360px; }
.progress-fill { height: 100%; width: 0; background: linear-gradient(90deg, var(--primary), var(--secondary)); border-radius: 999px; transition: width .6s ease; }

.reading-tools { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 15px; }
.reading-tools .chip { min-width: 44px; }
.story-display { font-size: var(--story-size, 1.3em); }
.next-actions { margin-top: 20px; }
.next-actions[hidden] { display: none; }

/* Lettura serale: solo la storia, tonalità calde e poco luminose */
.reading-exit {
    position: fixed; z-index: 1100; top: calc(12px + env(safe-area-inset-top)); right: calc(12px + env(safe-area-inset-right));
    min-height: 44px; padding: 8px 16px; font-size: 1em; border-radius: 999px;
    background: rgba(255,214,150,.15); color: #f2d9b0; border: 1px solid rgba(255,214,150,.4);
}
.reading-exit[hidden] { display: none; }
body.reading-mode { background: #0d0a06; --focus: #ffd08a; }
body.reading-mode header, body.reading-mode .tabs, body.reading-mode footer,
body.reading-mode .header-bar, body.reading-mode .header-bar + div,
body.reading-mode #favorites-tab, body.reading-mode #chi_sono-tab,
body.reading-mode #create-tab > .card:not(#storyCard), body.reading-mode .reading-tools,
body.reading-mode #storyCard > h2 { display: none; }
body.reading-mode #storyCard { background: transparent; box-shadow: none; padding: 48px 0 0; }
body.reading-mode .story-display {
    background: #16110a; color: #e8d3ae; border-color: #3a2e1c; --story-size: 1.7em;
}
body.reading-mode .favorite-btn { display: none; }
body.reading-mode .btn-group button { filter: brightness(.7); }
.field-hint { margin-top: 6px; font-size: 0.9em; color: #666; }
.field-hint[hidden] { display: none; }
body.nightmode .field-hint { color: #bdc3c7; }
.link-btn {
    display: inline; padding: 0; min-height: 0; background: none; border: none; font-size: inherit;
    font-weight: 700; color: var(--primary); text-decoration: underline; cursor: pointer;
}
.link-btn:hover { transform: none; }
body.nightmode .link-btn { color: #f39c12; }
#illustrateBtn[hidden] { display: none; }

.chip-group { display: flex; flex-wrap: wrap; gap: 10px; }
#genreHint, #settingHint { margin-bottom: 10px; }
.chip {
    max-width: 100%;
    padding: 10px 16px; min-height: 44px; border: 2px solid var(--primary); background: white;
    color: var(--dark); border-radius: 999px; font-size: 1em; font-weight: 700;
}
.chip:hover { transform: translateY(-2px); box-shadow: 0 4px 14px rgba(102,126,234,.3); }
.chip[aria-pressed="true"] { background: var(--primary); color: white; }
body.nightmode .chip { background: #2c3e50; color: white; }
body.nightmode .chip[aria-pressed="true"] { background: var(--primary); }

.more-details { margin-bottom: 20px; }
.more-details summary {
    cursor: pointer; font-weight: 700; color: var(--primary); min-height: 44px;
    display: flex; align-items: center; list-style-position: inside;
}
body.nightmode .more-details summary { color: #f39c12; }
.more-details .personalization-grid { margin-top: 10px; }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

.offline-banner {
    position: fixed; left: 0; right: 0; z-index: 1000;
    top: calc(70px + env(safe-area-inset-top));
    margin: 0 auto; width: fit-content; max-width: calc(100vw - 32px);
    background: var(--warning); color: #fff; font-weight: 700;
    padding: 8px 18px; border-radius: 20px; box-shadow: 0 2px 10px rgba(0,0,0,.25);
    text-align: center;
}
.offline-banner[hidden] { display: none; }

@media (max-width: 768px) {
    .card { padding: 20px 16px; }
    .story-display { padding: 22px 16px; overflow-wrap: anywhere; }
    .btn-group button { min-width: 0; }
    .favorites-backup .chip { padding: 10px 12px; }
    h1 { font-size: 2em; }
    .age-selector { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
    .age-btn { min-width: 0; padding: 14px 10px; }
    .btn-group { flex-direction: column; }
    .btn-group button { width: 100%; }
}

/* Audio visualizer */
.audio-visualizer {
    display: none;
    height: 100px;
    background: rgba(102, 126, 234, 0.1);
    border-radius: 10px;
    margin: 20px 0;
    position: relative;
    overflow: hidden;
}

.audio-visualizer.active {
    display: block;
}

.wave {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, var(--primary), var(--secondary));
    opacity: 0.3;
    animation: wave 2s ease-in-out infinite;
}

#audioLoading {
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% { opacity: 1; }
  50% { opacity: 0.6; }
  100% { opacity: 1; }
}

@keyframes wave {
    0%, 100% { transform: translateX(-50%) scaleY(0.5); }
    50% { transform: translateX(0) scaleY(1); }
}

       /* Nuovi stili per account e footer */
.header-bar {
    gap: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    padding-top: calc(15px + env(safe-area-inset-top));
    padding-left: calc(20px + env(safe-area-inset-left));
    padding-right: calc(20px + env(safe-area-inset-right));
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 999;
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(10px);
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

body.nightmode .header-bar {
    background: rgba(44, 62, 80, 0.9);
}

.account-btn {
    min-width: 44px;
    min-height: 44px;
    color: var(--dark);
    background: none;
    border: none;
    font-size: 2em;
    cursor: pointer;
    padding: 10px;
    border-radius: 50%;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    gap: 10px;
}

.account-btn:hover {
    background: rgba(102, 126, 234, 0.2);
    transform: scale(1.1);
}

body.nightmode .account-btn { color: #ecf0f1; }
body.nightmode .nightmode-toggle { color: #ffd08a; }

.account-name {
    font-size: 0.9em;
    font-weight: 700;
    color: var(--primary);
}

body.nightmode .account-name {
    color: #f39c12;
}

/* Footer */
footer {
    background: #2c3e50;
    color: #bdc3c7;
    padding: 30px 20px;
    text-align: center;
    font-size: 0.9em;
    margin-top: 50px;
    border-top: 3px solid var(--primary);
}

body.nightmode footer {
    background: #1a252f;
}

.company-info {
    max-width: 800px;
    margin: 0 auto;
    line-height: 1.8;
}

.company-info strong {
    color: white;
}

/* Modale Account */
.modal {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.7);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.modal.active {
    display: flex;
}

.modal-content {
    position: relative;
    max-height: calc(100dvh - 32px);
    overflow-y: auto;
    background: white;
    padding: 56px 24px 24px;
    border-radius: 20px;
    width: 90%;
    max-width: 400px;
    box-shadow: 0 20px 50px rgba(0,0,0,0.3);
    animation: fadeIn 0.3s ease;
}

body.nightmode .modal-content {
    background: #2c3e50;
    color: white;
}

.modal h2 {
    text-align: center;
    margin-bottom: 20px;
    color: var(--primary);
}

body.nightmode .modal h2 {
    color: #f39c12;
}

.close-modal {
    padding: 0;
    width: 44px;
    height: 44px;
    position: absolute;
    top: 8px;
    right: 8px;
    font-size: 1.8em;
    background: none;
    border: none;
    cursor: pointer;
    color: #999;
}

.close-modal:hover {
    color: var(--danger);
}



/* Smartphone: le schede diventano una barra in basso, raggiungibile col pollice */
@media (max-width: 768px) {
    body { padding-bottom: calc(86px + env(safe-area-inset-bottom)); }
    header { padding: 14px 12px; margin-bottom: 16px; border-radius: 16px; }
    header h1 { font-size: 1.6em; margin-bottom: 4px; }
    header .subtitle { font-size: 1em; }
    .connection-badge { margin-top: 8px; padding: 4px 14px; }
    .tabs {
        position: fixed; left: 0; right: 0; bottom: 0; z-index: 998; margin: 0; gap: 0;
        border: none; background: rgba(255,255,255,0.96); backdrop-filter: blur(10px);
        box-shadow: 0 -2px 12px rgba(0,0,0,0.18);
        padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    }
    body.nightmode .tabs { background: rgba(26,26,46,0.96); }
    .tab, body.nightmode .tab, body.nightmode .tab:hover {
        flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
        gap: 2px; min-height: 60px; padding: 6px 4px; border-radius: 0; background: transparent;
        font-size: 0.85em; color: #555;
    }
    body.nightmode .tab, body.nightmode .tab:hover { color: #cfd8e3; }
    .tab-icon { margin: 0; font-size: 1.5em; line-height: 1; }
    .tab-long { display: none; }
    .tab.active, body.nightmode .tab.active { background: transparent; color: var(--primary); box-shadow: inset 0 3px 0 var(--primary); }
    body.nightmode .tab.active { color: #f39c12; box-shadow: inset 0 3px 0 #f39c12; }
}

/* hidden deve prevalere anche sui controlli che usano display:flex. */
[hidden] { display: none !important; }
.server-data-action { margin-top: 20px; text-align: center; }
