/* ===================================================================
   ENGLISH READER — Dark Theme Mobile-First
   Font: Inter (Google Fonts)
   Design: Premium dark UI, glassmorphism, smooth animations
   =================================================================== */

/* --- CSS Custom Properties --- */
:root {
    --bg-primary: #0a0a0f;
    --bg-secondary: #12121a;
    --bg-card: #1a1a26;
    --bg-elevated: #22223a;
    --bg-hover: #2a2a44;
    --bg-input: #14141e;

    --text-primary: #e8e8f0;
    --text-secondary: #9898b0;
    --text-muted: #5e5e78;
    --text-accent: #b8a0ff;

    --accent: #7c6df6;
    --accent-hover: #9b8fff;
    --accent-glow: rgba(124, 109, 246, 0.25);
    --accent-bg: rgba(124, 109, 246, 0.08);

    --success: #4ade80;
    --warning: #fbbf24;
    --error: #f87171;

    --border: #2a2a3a;
    --border-light: #1e1e2e;

    --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 12px 48px rgba(0, 0, 0, 0.6);
    --shadow-popup: 0 8px 32px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(124, 109, 246, 0.15);

    --radius: 12px;
    --radius-sm: 8px;
    --radius-lg: 20px;
    --radius-xl: 24px;

    --font: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
    --ease: cubic-bezier(0.4, 0, 0.2, 1);
    --ease-spring: cubic-bezier(0.32, 0.72, 0, 1);
}

/* --- Reset & Base --- */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
    scroll-behavior: smooth;
}

body {
    font-family: var(--font);
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100dvh;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overflow-x: hidden;
}

/* --- Typography --- */
h1 { font-size: 1.25rem; font-weight: 700; }
h2 { font-size: 1.1rem; font-weight: 600; }
h3 { font-size: 1rem; font-weight: 600; }

/* --- Layout --- */
.container {
    max-width: 640px;
    margin: 0 auto;
    padding: 0 16px;
}

/* ===================================================================
   HEADER
   =================================================================== */
#app-header {
    position: sticky;
    top: 0;
    z-index: 50;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: rgba(10, 10, 15, 0.85);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-bottom: 1px solid var(--border-light);
}

#app-header h1 {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--text-primary);
}

#app-header h1 .logo-icon {
    font-size: 1.4rem;
}

#btn-settings {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: none;
    background: var(--bg-card);
    color: var(--text-secondary);
    border-radius: 50%;
    font-size: 1.2rem;
    cursor: pointer;
    transition: all 0.2s var(--ease);
}

#btn-settings:hover,
#btn-settings:active {
    background: var(--bg-elevated);
    color: var(--text-primary);
    transform: rotate(30deg);
}

/* ===================================================================
   INPUT SECTION — Nhập đoạn văn
   =================================================================== */
#input-section {
    padding: 24px 16px;
    max-width: 640px;
    margin: 0 auto;
}

#input-section .intro {
    color: var(--text-secondary);
    font-size: 0.875rem;
    margin-bottom: 16px;
    line-height: 1.5;
}

#text-input {
    width: 100%;
    min-height: 180px;
    padding: 16px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-family: var(--font);
    font-size: 1rem;
    line-height: 1.7;
    resize: vertical;
    transition: border-color 0.2s var(--ease);
}

#text-input::placeholder {
    color: var(--text-muted);
}

#text-input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

#btn-start {
    display: block;
    width: 100%;
    margin-top: 16px;
    padding: 14px 24px;
    background: linear-gradient(135deg, var(--accent), #6c5ce7);
    color: white;
    font-family: var(--font);
    font-size: 1rem;
    font-weight: 600;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.2s var(--ease);
}

#btn-start:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 20px var(--accent-glow);
}

#btn-start:active {
    transform: translateY(0);
}

/* ===================================================================
   READER SECTION — Hiển thị và đọc văn bản
   =================================================================== */
#reader-section {
    padding: 0 16px 120px;
    max-width: 640px;
    margin: 0 auto;
}

#reader-toolbar {
    display: flex;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px solid var(--border-light);
    margin-bottom: 20px;
}

#btn-back {
    padding: 8px 14px;
    background: var(--bg-card);
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s var(--ease);
}

#btn-back:hover {
    background: var(--bg-elevated);
    color: var(--text-primary);
}

/* Nội dung văn bản đã render */
#reader-content {
    font-size: 1.15rem;
    line-height: 2;
    letter-spacing: 0.01em;
    word-spacing: 0.05em;
}

/* Từng từ là 1 span có thể tap */
.word {
    display: inline;
    padding: 2px 1px;
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.15s var(--ease), color 0.15s var(--ease);
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

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

.word.selected {
    background: var(--accent-glow);
    color: var(--accent-hover);
    box-shadow: 0 0 8px var(--accent-glow);
}

.word.has-detail {
    border-bottom: 1px dotted var(--accent);
}

/* ===================================================================
   WORD POPUP — Popup nổi khi chọn từ
   =================================================================== */
#word-popup {
    position: fixed;
    display: flex;
    gap: 4px;
    padding: 6px;
    background: rgba(26, 26, 38, 0.95);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-popup);
    z-index: 100;
    opacity: 0;
    transform: scale(0.9) translateY(4px);
    transition: opacity 0.2s var(--ease), transform 0.2s var(--ease);
    pointer-events: none;
}

#word-popup.visible {
    opacity: 1;
    transform: scale(1) translateY(0);
    pointer-events: auto;
}

#word-popup button {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 10px 16px;
    background: transparent;
    color: var(--text-primary);
    border: none;
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s var(--ease);
}

#word-popup button:hover,
#word-popup button:active {
    background: var(--bg-hover);
}

#word-popup .divider {
    width: 1px;
    background: var(--border);
    margin: 4px 0;
}

/* ===================================================================
   DETAIL PANEL — Bottom sheet chi tiết từ
   =================================================================== */
#detail-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 199;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s var(--ease);
}

#detail-overlay.visible {
    opacity: 1;
    pointer-events: auto;
}

#detail-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 75dvh;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    z-index: 200;
    display: flex;
    flex-direction: column;
    transform: translateY(100%);
    transition: transform 0.4s var(--ease-spring);
    overflow: hidden;
}

#detail-panel.visible {
    transform: translateY(0);
}

/* Thanh kéo */
#detail-panel::before {
    content: '';
    display: block;
    width: 36px;
    height: 4px;
    background: var(--text-muted);
    border-radius: 2px;
    margin: 10px auto 4px;
    opacity: 0.5;
}

#detail-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 20px 12px;
    border-bottom: 1px solid var(--border-light);
    flex-shrink: 0;
}

#detail-word {
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--accent-hover);
}

#detail-phonetic {
    font-size: 0.95rem;
    color: var(--text-secondary);
    margin-left: 10px;
    font-weight: 400;
}

#detail-close {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-card);
    border: none;
    border-radius: 50%;
    color: var(--text-secondary);
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s var(--ease);
}

#detail-close:hover {
    background: var(--bg-elevated);
    color: var(--text-primary);
}

#detail-content {
    flex: 1;
    overflow-y: auto;
    padding: 16px 20px;
    -webkit-overflow-scrolling: touch;
}

/* Loading state */
#detail-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    padding: 32px 0;
    color: var(--text-secondary);
}

/* Kết quả chi tiết */
.detail-context {
    background: var(--accent-bg);
    border-left: 3px solid var(--accent);
    padding: 10px 14px;
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    margin-bottom: 16px;
    font-size: 0.9rem;
    color: var(--text-accent);
}

.detail-meaning {
    margin-bottom: 16px;
}

.detail-pos {
    display: inline-block;
    padding: 3px 10px;
    background: var(--bg-elevated);
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--accent-hover);
    margin-bottom: 8px;
}

.detail-def {
    padding: 8px 0 8px 12px;
    border-left: 2px solid var(--border);
    margin-bottom: 8px;
}

.detail-def-en {
    font-size: 0.9rem;
    color: var(--text-primary);
}

.detail-def-vi {
    font-size: 0.9rem;
    color: var(--success);
    margin-top: 2px;
}

.detail-example {
    margin-top: 6px;
    padding: 8px 12px;
    background: var(--bg-card);
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
}

.detail-example-en {
    color: var(--text-secondary);
    font-style: italic;
}

.detail-example-vi {
    color: var(--text-muted);
    margin-top: 2px;
}

/* Nút nghe phát âm trong detail panel */
#detail-audio {
    padding: 12px 20px 20px;
    flex-shrink: 0;
}

#detail-play {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 12px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-family: var(--font);
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s var(--ease);
}

#detail-play:hover {
    background: var(--bg-elevated);
    border-color: var(--accent);
}

#detail-play.loading {
    opacity: 0.6;
    pointer-events: none;
}

/* ===================================================================
   SETTINGS MODAL — Cài đặt
   =================================================================== */
#settings-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 299;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s var(--ease);
}

#settings-overlay.visible {
    opacity: 1;
    pointer-events: auto;
}

#settings-modal {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 85dvh;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    z-index: 300;
    display: flex;
    flex-direction: column;
    transform: translateY(100%);
    transition: transform 0.4s var(--ease-spring);
    overflow: hidden;
}

#settings-modal.visible {
    transform: translateY(0);
}

#settings-modal::before {
    content: '';
    display: block;
    width: 36px;
    height: 4px;
    background: var(--text-muted);
    border-radius: 2px;
    margin: 10px auto 4px;
    opacity: 0.5;
}

#settings-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 20px 12px;
    border-bottom: 1px solid var(--border-light);
    flex-shrink: 0;
}

#settings-header h2 {
    color: var(--text-primary);
}

#settings-close {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-card);
    border: none;
    border-radius: 50%;
    color: var(--text-secondary);
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s var(--ease);
}

#settings-close:hover {
    background: var(--bg-elevated);
    color: var(--text-primary);
}

#settings-content {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    -webkit-overflow-scrolling: touch;
}

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

.setting-group label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.setting-group select,
.setting-group textarea {
    width: 100%;
    padding: 12px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-family: var(--font);
    font-size: 0.9rem;
    transition: border-color 0.2s var(--ease);
}

.setting-group select:focus,
.setting-group textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

.setting-group textarea {
    min-height: 200px;
    resize: vertical;
    line-height: 1.6;
    font-size: 0.8rem;
}

.setting-hint {
    font-size: 0.78rem;
    color: var(--text-muted);
    margin-top: 6px;
    line-height: 1.4;
}

#settings-footer {
    display: flex;
    gap: 10px;
    padding: 12px 20px 20px;
    flex-shrink: 0;
}

#btn-reset-settings {
    flex: 1;
    padding: 12px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-secondary);
    font-family: var(--font);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s var(--ease);
}

#btn-reset-settings:hover {
    background: var(--bg-elevated);
    color: var(--text-primary);
}

#btn-save-settings {
    flex: 2;
    padding: 12px;
    background: linear-gradient(135deg, var(--accent), #6c5ce7);
    border: none;
    border-radius: var(--radius);
    color: white;
    font-family: var(--font);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s var(--ease);
}

#btn-save-settings:hover {
    box-shadow: 0 4px 16px var(--accent-glow);
}

/* ===================================================================
   TOAST — Thông báo nhỏ
   =================================================================== */
#audio-toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(80px);
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 20px;
    background: rgba(26, 26, 38, 0.95);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--border);
    border-radius: 50px;
    box-shadow: var(--shadow-lg);
    color: var(--text-primary);
    font-size: 0.85rem;
    z-index: 400;
    opacity: 0;
    transition: all 0.3s var(--ease-spring);
    pointer-events: none;
    white-space: nowrap;
}

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

#audio-toast.error {
    border-color: var(--error);
    color: var(--error);
}

/* ===================================================================
   SPINNER — Loading indicator
   =================================================================== */
.spinner {
    width: 28px;
    height: 28px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.spinner-small {
    width: 16px;
    height: 16px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* ===================================================================
   UTILITIES
   =================================================================== */
.hidden {
    display: none !important;
}

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

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

/* ===================================================================
   RESPONSIVE — Desktop
   =================================================================== */
@media (min-width: 768px) {
    #input-section,
    #reader-section {
        max-width: 720px;
        padding-left: 32px;
        padding-right: 32px;
    }

    #text-input {
        min-height: 220px;
        font-size: 1.05rem;
    }

    #reader-content {
        font-size: 1.2rem;
        line-height: 2.1;
    }

    #detail-panel {
        max-width: 560px;
        left: 50%;
        transform: translateX(-50%) translateY(100%);
        border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    }

    #detail-panel.visible {
        transform: translateX(-50%) translateY(0);
    }

    #settings-modal {
        max-width: 500px;
        left: 50%;
        transform: translateX(-50%) translateY(100%);
        border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    }

    #settings-modal.visible {
        transform: translateX(-50%) translateY(0);
    }
}

/* Tối ưu cho màn hình nhỏ */
@media (max-width: 380px) {
    #reader-content {
        font-size: 1.05rem;
        line-height: 1.9;
    }

    #word-popup button {
        padding: 8px 12px;
        font-size: 0.85rem;
    }
}

/* ===================================================================
   SAFE AREA — Hỗ trợ iPhone notch
   =================================================================== */
@supports (padding: env(safe-area-inset-bottom)) {
    #detail-audio {
        padding-bottom: calc(20px + env(safe-area-inset-bottom));
    }

    #settings-footer {
        padding-bottom: calc(20px + env(safe-area-inset-bottom));
    }

    #audio-toast {
        bottom: calc(24px + env(safe-area-inset-bottom));
    }
}
