@import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@400;700&display=swap');

:root {
    --background-color: #F0F8FF; /* AliceBlue */
    --text-color: #333;
    --primary-color: #87CEEB; /* SkyBlue */
    --primary-hover: #6495ED; /* CornflowerBlue */
    --highlight-color: #FFD700; /* Gold */
    --destructive-color: #FF6347; /* Tomato */
    --font-main: 'Comic Neue', cursive;
    --surface-color: #ffffff;
    --item-hover-bg: #e0f0ff;
}

[data-theme="dark"] {
    --background-color: #121212;
    --text-color: #e0e0e0;
    --primary-color: #5D9CEC;
    --primary-hover: #4A89DC;
    --highlight-color: #DAA520;
    --destructive-color: #D9534F;
    --surface-color: #1e1e1e;
    --item-hover-bg: #333333;
}

body {
    font-family: var(--font-main);
    background-color: var(--background-color);
    color: var(--text-color);
    margin: 0;
    padding: 1rem;
    font-size: clamp(18px, 2.5vw, 24px);
    line-height: 1.6;
}

.hidden {
    display: none !important;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 2px solid var(--primary-color);
    padding-bottom: 0.5rem;
    margin-bottom: 1.5rem;
}

header h1 {
    margin: 0;
    font-size: 1.5em;
    color: var(--primary-hover);
}

.header-buttons {
    display: flex;
    gap: 10px;
}

.theme-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    background-color: var(--primary-color);
    color: white;
    border: none;
    padding: 0.5em 1em;
    border-radius: 8px;
    font-family: var(--font-main);
    font-size: 0.8em;
    font-weight: bold;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.theme-button svg {
    width: 1.2em;
    height: 1.2em;
    fill: currentColor;
}

.theme-button:hover {
    background-color: var(--primary-hover);
}

.theme-button.destructive {
    background-color: var(--destructive-color);
}

.theme-button.destructive:hover {
    background-color: #d9534f;
}

.theme-button:disabled {
    background-color: #ccc;
    cursor: not-allowed;
}

/* --- Reader View --- */
.input-area {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 1.5rem;
}

.input-buttons {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

#story-input {
    height: 200px;
}

.json-editors {
    display: flex;
    gap: 15px;
    margin-top: 10px;
}

.json-editor {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.json-editor label {
    margin-bottom: 5px;
    font-size: 0.8em;
    font-weight: bold;
    color: var(--primary-hover);
}

/* Common style for all textareas in creator mode */
#story-input,
.json-editor textarea {
    width: 100%;
    padding: 10px;
    border: 2px solid #ddd;
    border-radius: 8px;
    font-family: var(--font-main);
    font-size: 1em;
    box-sizing: border-box;
    resize: vertical;
}

#phonetics-editor,
#pronunciations-editor {
    border: 2px solid #ddd;
    border-radius: 8px;
    padding: 10px;
    min-height: 100px;
    max-height: 250px;
    overflow-y: auto;
    background-color: var(--surface-color);
}

.phonetic-pair,
.pronunciation-pair {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}

.phonetic-pair input,
.pronunciation-pair input {
    flex: 1;
    padding: 8px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-family: var(--font-main);
}

.phonetic-pair .arrow,
.pronunciation-pair .arrow {
    font-weight: bold;
}

.phonetic-pair .pair-controls button,
.pronunciation-pair .pair-controls button {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1.2em;
    padding: 2px;
}

#add-phonetic-btn,
#add-pronunciation-btn {
    margin-top: 10px;
    align-self: flex-start;
}

.story-container {
    background-color: var(--surface-color);
    padding: 2rem;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.05);
    font-size: 1.2em;
    line-height: 1.8;
}

.story-image {
    display: block;
    max-width: 80%;
    margin: 1.5rem auto;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.speakable-word {
    cursor: pointer;
    transition: color 0.2s ease, background-color 0.2s ease;
    padding: 2px 4px;
    border-radius: 4px;
}

.speakable-word:hover {
    background-color: var(--highlight-color);
    color: var(--text-color);
}

.speaking {
    background-color: var(--primary-color) !important;
    color: white !important;
}

/* --- Dashboard View --- */
#dashboard-view h2 {
    color: var(--primary-hover);
    border-bottom: 2px solid var(--primary-color);
    padding-bottom: 0.5rem;
}

.dashboard-controls {
    margin-bottom: 1rem;
}

#tricky-words-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 15px;
}

.tricky-word-item {
    background-color: var(--surface-color);
    padding: 15px;
    border-radius: 8px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.05);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.tricky-word-item .word {
    font-size: 1.5em;
    font-weight: bold;
}

.tricky-word-item .count {
    font-size: 1.5em;
    font-weight: bold;
    background-color: var(--primary-color);
    color: white;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* --- Modal Styles --- */
.modal {
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.5);
    display: flex;
    justify-content: center;
    align-items: center;
}

.modal-content {
    background-color: var(--surface-color);
    padding: 2rem;
    border-radius: 12px;
    width: 90%;
    max-width: 500px;
    text-align: center;
    position: relative; /* This is crucial for positioning the close button */
}

.modal-close-btn {
    position: absolute;
    top: 10px;
    right: 15px;
    background: transparent;
    border: none;
    font-size: 2rem;
    line-height: 1;
    color: #aaa;
    cursor: pointer;
    padding: 0;
    transition: color 0.2s ease;
}

.modal-close-btn:hover {
    color: #333;
}


#story-modal .modal-content {
    display: flex;
    flex-direction: column;
    max-height: 85vh; /* Set a max height for the modal content */
}

#story-list {
    display: flex;
    flex-direction: column;
    margin: 20px 0;
    overflow-y: auto; /* Make this area scrollable */
    flex-grow: 1; /* Allow it to take up available space */
    min-height: 100px; /* Ensure it has some height even if empty */
}

.story-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: var(--background-color);
    padding: 15px;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.2s ease;
    font-weight: bold;
}

.story-item-title {
    flex-grow: 1;
}

.story-share-btn {
    background-color: var(--primary-hover);
    font-size: 0.7em;
    padding: 0.4em 0.8em;
    margin-left: 15px;
}

.story-delete-btn {
    font-size: 0.7em;
    padding: 0.4em 0.8em;
    margin-left: 15px;
}

.story-group {
    border: 1px solid #eee;
    border-radius: 8px;
    margin-bottom: 10px;
}

.story-group-header {
    background-color: var(--primary-color);
    color: white;
    padding: 10px 15px;
    font-weight: bold;
    cursor: pointer;
    position: relative;
    user-select: none;
    /* Round the top corners */
    border-top-left-radius: 8px;
    border-top-right-radius: 8px;
}

.story-group-header::before {
    content: '▼';
    position: absolute;
    right: 15px;
    transition: transform 0.2s ease;
}

.story-group-header.collapsed::before {
    transform: rotate(-90deg);
}

.story-group-header.collapsed {
    /* Round all corners when collapsed */
    border-radius: 8px;
}

.story-group-content {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 10px;
    /* This will not scroll; the parent list will */
}
.story-item:hover {
    background-color: var(--item-hover-bg);
}

/* --- Syllable Pop-up --- */
.syllable-popup {
    position: absolute;
    background-color: var(--primary-color);
    color: white;
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 1.1em;
    font-weight: bold;
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
    z-index: 1001;
    pointer-events: none; /* Don't let it interfere with other clicks */
    opacity: 0;
    white-space: nowrap; /* Prevent the pop-up from wrapping */
    transform: translateY(10px);
    transition: opacity 0.2s ease, transform 0.2s ease;
}
.syllable-popup.show {
    opacity: 1;
    transform: translateY(0);
}

.load-options {
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: stretch; /* Make buttons full width */
    justify-content: center;
    margin-top: 1rem;
}

/* --- PeerJS Modal Styles --- */
#peer-role-selection {
    display: flex;
    flex-direction: column;
    gap: 15px;
    margin-bottom: 1.5rem;
}

#host-id-display {
    background-color: var(--background-color);
    font-size: 1.5em;
    font-weight: bold;
    padding: 1rem;
    border-radius: 8px;
    letter-spacing: 2px;
    border: 2px dashed var(--primary-color);
}

#join-id-input {
    width: 100%;
    padding: 0.5em;
    font-size: 1.2em;
    text-align: center;
    border: 2px solid #ddd;
    border-radius: 8px;
    margin-bottom: 10px;
    box-sizing: border-box;
}

.modal-footer-buttons {
    margin-top: 1.5rem;
    display: flex;
    justify-content: center;
    gap: 15px;
}

.role-selector {
    display: flex;
    gap: 1rem;
    justify-content: center;
    margin-bottom: 1.5rem;
}

.role-panel {
    animation: fadeIn 0.3s ease-in;
    text-align: center;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.sdp-box {
    width: 100%;
    padding: 0.5rem;
    font-family: monospace;
    font-size: 0.8rem;
    margin-bottom: 0.5rem;
    background: var(--background-color);
    color: var(--text-color);
    border: 1px solid #ddd;
}

.code-display {
    font-size: 2.5rem;
    font-weight: bold;
    letter-spacing: 8px;
    background: var(--background-color);
    padding: 1rem;
    text-align: center;
    border: 2px dashed var(--primary-color);
    border-radius: 8px;
    margin: 1rem 0;
    user-select: all;
    color: var(--text-color);
}

.role-btn {
    padding: 0.5rem 2rem;
    border: 1px solid var(--primary-color);
    background: white;
    color: var(--primary-color);
    cursor: pointer;
    border-radius: 4px;
    font-weight: bold;
    transition: all 0.2s;
    font-family: var(--font-main);
    font-size: 1em;
}

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

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

.peer-item-actions button[disabled] span {
    display: inline-block;
    width: 1em;
    height: 1em;
    border: 2px solid currentColor;
    border-bottom-color: transparent;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* --- Info Icon & Modal --- */
.info-icon {
    background: none;
    border: none;
    font-size: 1.2rem;
    cursor: pointer;
    margin-left: 0.5rem;
    vertical-align: middle;
    opacity: 0.7;
    transition: opacity 0.2s;
}
.info-icon:hover {
    opacity: 1;
}
.info-content h3 {
    margin-top: 1rem;
    margin-bottom: 0.5rem;
    color: var(--primary-hover);
}

/* --- Recent Peers List --- */
#recent-peers-list {
    margin: 0 0 10px 0;
    color: var(--primary-hover);
    font-size: 1em;
}

.peer-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    background-color: var(--background-color);
    border: 1px solid #ddd;
    border-radius: 8px;
    margin-bottom: 8px;
}

.peer-info {
    display: flex;
    flex-direction: column;
    text-align: left;
}

.peer-name {
    font-weight: bold;
}

.peer-last-seen {
    font-size: 0.8em;
    color: #666;
}

.peer-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.remove-peer-btn {
    background: transparent;
    border: none;
    color: var(--text-color);
    opacity: 0.5;
    font-size: 1.5rem;
    line-height: 1;
    padding: 0 0.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    cursor: pointer;
}

.remove-peer-btn:hover {
    color: #dc3545;
    background-color: rgba(220, 53, 69, 0.1);
}

/* --- Settings Modal --- */
#settings-modal .modal-content {
    text-align: left; /* Override the default centered text for this modal */
}

#settings-modal h4 {
    margin-top: 0;
    border-bottom: 1px solid #eee;
    padding-bottom: 0.5rem;
    margin-bottom: 1rem;
}

#settings-modal .settings-description {
    font-size: 0.9em;
    color: #666;
    margin-bottom: 1.5rem;
}

#stories-folder-status {
    background-color: var(--background-color);
    border: 2px dashed var(--primary-color);
    border-radius: 8px;
    padding: 1rem;
    margin-bottom: 1.5rem;
}

#stories-folder-status p {
    margin: 0;
    font-size: 1em;
    color: var(--text-color);
}

#stories-folder-status strong {
    color: var(--primary-hover);
}

#folder-status-text {
    font-weight: bold;
}

/* --- AI Image Generation Placeholders --- */
.ai-prompt-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 200px;
    background-color: var(--surface-color);
    border: 2px dashed var(--border-color);
    border-radius: 12px;
    margin: 1.5rem 0;
    padding: 20px;
    color: var(--text-color);
    text-align: center;
    gap: 12px;
}

.ai-prompt-placeholder.pending,
.ai-prompt-placeholder.generating {
    border-color: var(--primary-color);
    color: var(--primary-hover);
}

.ai-prompt-placeholder.failed {
    border-color: var(--destructive-color);
    background-color: rgba(255, 99, 71, 0.05);
}

.ai-prompt-placeholder .spinner {
    width: 30px;
    height: 30px;
    border: 4px solid rgba(0, 0, 0, 0.1);
    border-left-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

.retry-image-btn {
    background-color: var(--destructive-color);
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 6px;
    cursor: pointer;
    font-family: var(--font-main);
    font-weight: bold;
    transition: opacity 0.2s;
}

.retry-image-btn:hover {
    opacity: 0.9;
}