/* Import Bookerly font */
@import url('https://fonts.cdnfonts.com/css/bookerly');

:root {
    --bg-primary: #ffffff;
    --bg-secondary: #f9f6f1;
    --text-primary: #2c3e50;
    --text-secondary: #7f8c8d;
    --text-muted: #95a5a6;
    --accent-primary: #3498db;
    --accent-hover: #2980b9;
    --border-color: #e0e0e0;
    --border-light: #ecf0f1;
    --header-bg: #ffffff;
    --header-shadow: rgba(0, 0, 0, 0.08);
    --popup-bg: #ffffff;
    --popup-shadow: rgba(0, 0, 0, 0.3);
    --word-hover-bg: rgba(52, 152, 219, 0.1);
    --summary-hover-bg: rgba(52, 152, 219, 0.03);
}

body.dark-mode {
    --bg-primary: #1a1a1a;
    --bg-secondary: #0d0d0d;
    --text-primary: #e0e0e0;
    --text-secondary: #a0a0a0;
    --text-muted: #808080;
    --accent-primary: #5dade2;
    --accent-hover: #3498db;
    --border-color: #404040;
    --border-light: #2a2a2a;
    --header-bg: #1a1a1a;
    --header-shadow: rgba(0, 0, 0, 0.5);
    --popup-bg: #1a1a1a;
    --popup-shadow: rgba(0, 0, 0, 0.8);
    --word-hover-bg: rgba(93, 173, 226, 0.15);
    --summary-hover-bg: rgba(93, 173, 226, 0.08);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: 'Bookerly', Georgia, serif;
    font-size: 18px;
    line-height: 1.8;
    color: var(--text-primary);
    background-color: var(--bg-secondary);
    padding-top: 56px;
    padding-top: max(56px, env(safe-area-inset-top));
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    transition: background-color 0.3s ease, color 0.3s ease;
}

/* Header Styles - Minimal Icon Bar */
header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background: var(--header-bg);
    border-bottom: 1px solid var(--border-color);
    padding: 0 20px;
    height: 56px;
    box-shadow: none;
    transition: transform 0.3s ease, background-color 0.3s ease, border-color 0.3s ease;
    z-index: 1000;
}

header.hidden {
    transform: translateY(-100%);
}

.header-content {
    max-width: 100%;
    height: 100%;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.header-brand {
    display: flex;
    align-items: center;
}

.brand-icon {
    width: 24px;
    height: 24px;
    opacity: 0.9;
    color: var(--text-primary);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Icon Buttons */
.icon-btn {
    background: transparent;
    border: none;
    border-radius: 8px;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    padding: 0;
    touch-action: manipulation;
    color: var(--text-primary);
}

.icon-btn svg {
    width: 20px;
    height: 20px;
}

.icon-btn:hover {
    background-color: var(--summary-hover-bg);
}

.icon-btn:active {
    background-color: var(--word-hover-bg);
    transform: scale(0.95);
}

.icon-btn .icon-dark {
    display: none;
}

body.dark-mode .icon-btn .icon-light {
    display: none;
}

body.dark-mode .icon-btn .icon-dark {
    display: block;
}

/* Font Size Controls */
.font-size-controls {
    display: flex;
    align-items: center;
    margin-right: 4px;
}

.font-size-btn {
    background: transparent;
    border: none;
    border-radius: 6px;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    padding: 0;
    touch-action: manipulation;
    color: var(--text-primary);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-weight: 500;
}

.font-size-btn.decrease {
    font-size: 13px;
}

.font-size-btn.increase {
    font-size: 17px;
}

.font-size-btn:hover {
    background-color: var(--summary-hover-bg);
}

.font-size-btn:active {
    background-color: var(--word-hover-bg);
    transform: scale(0.95);
}

/* Loading Bar */
.loading-bar {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 4px;
    background-color: rgba(255, 255, 255, 0.2);
    display: none;
}

.loading-progress {
    height: 100%;
    background-color: #3498db;
    transition: width 0.3s ease;
}

/* Content Area */
.content {
    max-width: 800px;
    margin: 0 auto;
    padding: 40px 30px;
    padding-left: max(30px, env(safe-area-inset-left));
    padding-right: max(30px, env(safe-area-inset-right));
    padding-bottom: max(40px, env(safe-area-inset-bottom));
    background-color: var(--bg-primary);
    min-height: calc(100vh - 56px);
    box-shadow: 0 0 20px var(--header-shadow);
    transition: background-color 0.3s ease;
}

.content p {
    margin-bottom: 1.5em;
    text-align: justify;
}

.content h1, .content h2, .content h3, .content h4, .content h5, .content h6 {
    margin-top: 1.5em;
    margin-bottom: 0.75em;
    color: var(--text-primary);
    font-weight: 600;
}

.content h1 {
    font-size: 2em;
}

.content h2 {
    font-size: 1.75em;
}

.content h3 {
    font-size: 1.5em;
}

.content img {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 20px auto;
    border-radius: 4px;
}

/* Clickable Words */
.word {
    cursor: pointer;
    transition: color 0.2s ease, background-color 0.2s ease;
    padding: 2px 1px;
    border-radius: 2px;
    touch-action: manipulation;
}

.word.highlighted {
    background-color: rgba(241, 196, 15, 0.3);
    border-radius: 3px;
}

body.dark-mode .word.highlighted {
    background-color: rgba(241, 196, 15, 0.25);
}

.word:hover {
    color: var(--accent-primary);
    text-decoration: underline;
}

.word:active {
    background-color: var(--word-hover-bg);
}

/* Section Triggers */
.section-trigger {
    display: block;
    margin: 40px auto;
    padding: 12px 32px;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    background-color: transparent;
    border: 1.5px solid var(--border-color);
    border-radius: 24px;
    cursor: pointer;
    user-select: none;
    transition: all 0.25s ease;
    letter-spacing: 0.5px;
    min-height: 44px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    touch-action: manipulation;
    text-transform: uppercase;
    max-width: 160px;
}

.section-trigger:hover {
    color: var(--accent-primary);
    border-color: var(--accent-primary);
    background-color: var(--summary-hover-bg);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px var(--word-hover-bg);
}

.section-trigger:active {
    transform: translateY(0);
    box-shadow: 0 2px 6px var(--word-hover-bg);
    background-color: var(--word-hover-bg);
}

/* Popup (Bottom Sheet) */
.popup {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: var(--popup-bg);
    box-shadow: 0 -5px 30px var(--popup-shadow);
    transform: translateY(100%);
    transition: transform 0.3s ease, background-color 0.3s ease;
    z-index: 2000;
    max-height: 70vh;
    border-radius: 20px 20px 0 0;
    padding-bottom: env(safe-area-inset-bottom);
}

.popup.active {
    transform: translateY(0);
}

.popup-handle {
    width: 36px;
    height: 4px;
    background-color: var(--border-color);
    border-radius: 2px;
    margin: 12px auto 0;
}

.popup-content {
    padding: 24px 30px 35px;
    overflow-y: auto;
    max-height: calc(70vh - 16px);
}

.popup-content h3 {
    font-size: 24px;
    margin-bottom: 15px;
    color: var(--text-primary);
}

.popup-content p {
    font-size: 18px;
    line-height: 1.8;
    color: var(--text-secondary);
}

.popup-content .definition {
    margin-bottom: 20px;
    display: block;
}

.popup-content .example {
    margin-top: 20px;
    padding-top: 20px;
    border-top: 1px solid var(--border-light);
    color: var(--text-secondary);
    font-size: 17px;
    display: block;
}

.loading {
    text-align: center;
    padding: 40px;
    color: var(--text-secondary);
    font-style: italic;
}

/* Mobile Phones (Portrait) */
@media (max-width: 480px) {
    body {
        font-size: 16px;
        line-height: 1.7;
        padding-top: 56px;
    }

    header {
        padding: 0 16px;
        height: 56px;
    }

    .brand-icon {
        width: 22px;
        height: 22px;
    }

    .icon-btn {
        width: 36px;
        height: 36px;
    }

    .icon-btn svg {
        width: 18px;
        height: 18px;
    }

    .header-actions {
        gap: 4px;
    }

    .content {
        padding: 20px 15px;
        box-shadow: none;
        min-height: calc(100vh - 56px);
    }

    .content p {
        text-align: left;
        margin-bottom: 1.3em;
    }

    .content h1 {
        font-size: 1.6em;
    }

    .content h2 {
        font-size: 1.4em;
    }

    .content h3 {
        font-size: 1.2em;
    }

    .section-trigger {
        padding: 11px 28px;
        font-size: 13px;
        margin: 30px auto;
        max-width: 140px;
    }

    .popup {
        max-height: 80vh;
        border-radius: 16px 16px 0 0;
    }

    .popup-content {
        padding: 20px 20px 25px;
        max-height: calc(80vh - 16px);
    }

    .popup-content h3 {
        font-size: 20px;
    }

    .popup-content p {
        font-size: 16px;
        line-height: 1.7;
    }

    .loading {
        padding: 30px 15px;
        font-size: 15px;
    }
}

/* Mobile Phones (Landscape) & iPad Mini (Portrait) */
@media (min-width: 481px) and (max-width: 834px) {
    body {
        font-size: 17px;
        padding-top: 56px;
    }

    header {
        padding: 0 18px;
    }

    .brand-icon {
        width: 23px;
        height: 23px;
    }

    .icon-btn {
        width: 38px;
        height: 38px;
    }

    .icon-btn svg {
        width: 19px;
        height: 19px;
    }

    .header-actions {
        gap: 6px;
    }

    .content {
        padding: 30px 25px;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.08);
    }

    .content p {
        text-align: left;
    }

    .section-trigger {
        padding: 11px 30px;
        font-size: 13px;
        margin: 35px auto;
        max-width: 150px;
    }

    .popup {
        max-height: 75vh;
    }

    .popup-content {
        padding: 20px 25px 30px;
        max-height: calc(75vh - 16px);
    }

    .popup-content h3 {
        font-size: 22px;
    }

    .popup-content p {
        font-size: 17px;
    }
}

/* iPad Mini (Landscape) & Tablets */
@media (min-width: 835px) and (max-width: 1024px) {
    body {
        font-size: 18px;
        padding-top: 56px;
    }

    .content {
        padding: 35px 30px;
    }

    .content p {
        text-align: justify;
    }
}

/* Improve scrolling on iOS */
@supports (-webkit-overflow-scrolling: touch) {
    .popup-content {
        -webkit-overflow-scrolling: touch;
    }
}

/* Smooth scrolling */
html {
    scroll-behavior: smooth;
}

/* Selection styling */
::selection {
    background-color: var(--accent-primary);
    color: white;
}

/* Word Count Badge */
.icon-btn {
    position: relative;
}

.word-count-badge {
    position: absolute;
    top: 2px;
    right: 2px;
    background: var(--accent-primary);
    color: white;
    font-size: 10px;
    font-weight: 600;
    min-width: 16px;
    height: 16px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

/* Word List Panel */
.word-list-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-width: 400px;
    background: var(--popup-bg);
    box-shadow: -5px 0 30px var(--popup-shadow);
    transform: translateX(100%);
    transition: transform 0.3s ease;
    z-index: 2500;
    display: flex;
    flex-direction: column;
}

.word-list-panel.active {
    transform: translateX(0);
}

.word-list-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.word-list-header h3 {
    margin: 0;
    font-size: 18px;
    color: var(--text-primary);
    font-weight: 600;
}

.word-list-header h3 span {
    color: var(--text-secondary);
    font-weight: 400;
}

.word-list-content {
    flex: 1;
    overflow-y: auto;
    padding: 0;
    -webkit-overflow-scrolling: touch;
}

.word-list-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    padding: 40px;
    text-align: center;
    color: var(--text-secondary);
}

.word-list-empty svg {
    width: 48px;
    height: 48px;
    margin-bottom: 16px;
    opacity: 0.5;
}

.word-list-empty p {
    margin: 0;
    font-size: 14px;
    line-height: 1.5;
}

.word-item {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-light);
}

.word-item-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 6px;
}

.word-item-word {
    font-weight: 600;
    font-size: 16px;
    color: var(--text-primary);
}

.word-item-delete {
    background: none;
    border: none;
    padding: 4px;
    cursor: pointer;
    color: var(--text-muted);
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.word-item-delete:hover {
    color: #e74c3c;
    background: rgba(231, 76, 60, 0.1);
}

.word-item-delete svg {
    width: 16px;
    height: 16px;
}

.word-item-definition {
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.5;
    margin: 0;
}

.word-list-actions {
    display: flex;
    padding: 16px 20px;
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}

.word-list-actions > *:not(:last-child) {
    margin-right: 10px;
}

/* Action Buttons */
.action-btn {
    flex: 1;
    padding: 12px 16px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    touch-action: manipulation;
    transition: all 0.2s ease;
}

.action-btn:active {
    transform: scale(0.98);
}

.action-btn.primary {
    background: var(--accent-primary);
    color: white;
    border-color: var(--accent-primary);
}

.action-btn.primary:hover {
    background: var(--accent-hover);
}

.action-btn.danger {
    color: #e74c3c;
    border-color: #e74c3c;
    background: transparent;
}

.action-btn.danger:hover {
    background: rgba(231, 76, 60, 0.1);
}

/* Export Modal */
.export-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 3000;
    padding: 20px;
}

.export-modal.active {
    display: flex;
}

.export-modal-content {
    background: var(--popup-bg);
    border-radius: 12px;
    padding: 24px;
    max-width: 500px;
    width: 100%;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
}

.export-modal-content h3 {
    margin: 0 0 8px 0;
    color: var(--text-primary);
    font-size: 18px;
}

.export-modal-content p {
    margin: 0 0 16px 0;
    color: var(--text-secondary);
    font-size: 14px;
    line-height: 1.5;
}

.export-textarea {
    flex: 1;
    min-height: 200px;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-family: 'SF Mono', Monaco, 'Courier New', monospace;
    font-size: 11px;
    resize: none;
    background: var(--bg-secondary);
    color: var(--text-primary);
    line-height: 1.4;
    -webkit-overflow-scrolling: touch;
}

.export-actions {
    display: flex;
    margin-top: 16px;
}

.export-actions > *:first-child {
    margin-right: 12px;
}

/* Toast Notification */
.toast {
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--text-primary);
    color: var(--bg-primary);
    padding: 12px 24px;
    border-radius: 8px;
    font-size: 14px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    opacity: 0;
    transition: opacity 0.3s ease, transform 0.3s ease;
    z-index: 4000;
    text-align: center;
    max-width: calc(100% - 40px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* Overlay for panels */
.panel-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
    z-index: 2400;
}

.panel-overlay.active {
    opacity: 1;
    visibility: visible;
}

/* Mobile adjustments for word list */
@media (max-width: 480px) {
    .word-list-panel {
        max-width: 100%;
    }

    .word-list-header {
        padding: 14px 16px;
    }

    .word-item {
        padding: 14px 16px;
    }

    .word-list-actions {
        padding: 14px 16px;
        flex-wrap: wrap;
    }

    .word-list-actions > * {
        flex: 1 1 calc(50% - 5px);
    }

    .word-list-actions > *:not(:last-child) {
        margin-right: 0;
    }

    .word-list-actions > *:nth-child(1),
    .word-list-actions > *:nth-child(2) {
        margin-bottom: 10px;
    }

    .word-list-actions > *:nth-child(1) {
        margin-right: 10px;
    }

    .word-list-actions > *:nth-child(3) {
        flex: 1 1 100%;
    }

    .action-btn {
        padding: 14px 10px;
        font-size: 13px;
    }

    .export-modal-content {
        padding: 20px;
        margin: 10px;
    }

    .export-textarea {
        min-height: 150px;
        font-size: 10px;
    }

    .toast {
        bottom: 80px;
        padding: 10px 20px;
        font-size: 13px;
    }
}
