/* Custom Properties / Theme Variables */
:root[data-theme="light"] {
    --primary: #2563eb;
    --secondary: #4f46e5;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --background: #f8fafc;
    --text: #1e293b;
    --text-muted: #64748b;
    --card: #ffffff;
    --glass-bg: rgba(255, 255, 255, 0.7);
    --glass-border: rgba(209, 213, 219, 0.5);
    --success: #10b981;
    --error: #ef4444;
}

:root[data-theme="dark"] {
    --primary: #3b82f6;
    --secondary: #8b5cf6;
    --accent: #3b82f6;
    --accent-hover: #60a5fa;
    --background: #0f172a;
    --text: #f8fafc;
    --text-muted: #94a3b8;
    --card: #1e293b;
    --glass-bg: rgba(30, 41, 59, 0.7);
    --glass-border: rgba(51, 65, 85, 0.7);
    --success: #10b981;
    --error: #ef4444;
}

/* Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Segoe UI', system-ui, sans-serif;
}

html {
    scroll-behavior: smooth;
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

body {
    background: var(--background);
    color: var(--text);
    line-height: 1.6;
    transition: background-color 0.3s, color 0.3s;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
}

/* Utility Classes */
.hidden { display: none !important; }
.mt-4 { margin-top: 1rem; }
.mt-6 { margin-top: 1.5rem; }
.mb-4 { margin-bottom: 1rem; }
.w-full { width: 100%; }
.section-title { margin-bottom: 1rem; color: var(--accent); font-size: clamp(1.25rem, 3vw, 1.75rem); }

/* ──────────────────────────────────────────
   Header & Nav
   ────────────────────────────────────────── */
header {
    background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
    color: white;
    padding: 0.875rem 1rem;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

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

.brand h1 {
    font-size: clamp(1.15rem, 3.5vw, 1.5rem);
    font-weight: 700;
    white-space: nowrap;
}

nav {
    display: flex;
    gap: 1.5rem;
}
nav a {
    color: white;
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    transition: opacity 0.3s;
    white-space: nowrap;
    font-size: 0.95rem;
}
nav a:hover { opacity: 0.8; }

/* Hamburger Button */
.hamburger {
    display: none;
    flex-direction: column;
    gap: 5px;
    cursor: pointer;
    padding: 8px;
    min-width: 44px;
    min-height: 44px;
    justify-content: center;
    align-items: center;
    border-radius: 0.5rem;
    transition: background 0.2s;
    -webkit-tap-highlight-color: transparent;
}
.hamburger:hover,
.hamburger:active { background: rgba(255, 255, 255, 0.15); }
.hamburger span {
    width: 22px;
    height: 2.5px;
    background: white;
    border-radius: 2px;
    transition: transform 0.3s ease, opacity 0.3s ease;
    transform-origin: center;
}

/* Animated X when open */
.hamburger.open span:nth-child(1) { transform: translateY(7.5px) rotate(45deg); }
.hamburger.open span:nth-child(2) { opacity: 0; }
.hamburger.open span:nth-child(3) { transform: translateY(-7.5px) rotate(-45deg); }

/* ──────────────────────────────────────────
   Main Container
   ────────────────────────────────────────── */
.container {
    max-width: 1200px;
    margin: 2rem auto;
    padding: 0 1rem;
    flex-grow: 1;
    width: 100%;
}

/* ──────────────────────────────────────────
   Tabs & Controls
   ────────────────────────────────────────── */
.nav-container {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 1rem;
    margin-bottom: 2rem;
}

.tab-buttons { display: flex; gap: 0.5rem; }

.tab-button {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    color: var(--text);
    padding: 0.75rem 1.5rem;
    border-radius: 0.5rem;
    cursor: pointer;
    font-weight: 600;
    backdrop-filter: blur(10px);
    transition: background 0.3s, color 0.3s, border-color 0.3s, transform 0.15s;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    font-size: clamp(0.85rem, 2vw, 1rem);
    min-height: 44px;
    -webkit-tap-highlight-color: transparent;
}

.tab-button:hover { background: rgba(59, 130, 246, 0.1); }
.tab-button:active { transform: scale(0.97); }
.tab-button.active { background: var(--accent); color: white; border-color: var(--accent); }

.controls { display: flex; gap: 0.5rem; }

.btn {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    color: var(--text);
    padding: 0.75rem 1.25rem;
    border-radius: 0.5rem;
    cursor: pointer;
    font-weight: 500;
    transition: background 0.3s, color 0.3s, transform 0.15s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    font-size: clamp(0.85rem, 2vw, 1rem);
    min-height: 44px;
    -webkit-tap-highlight-color: transparent;
}

.btn:hover { background: rgba(59, 130, 246, 0.1); }
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); color: white; border: none; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { background: var(--error); color: white; border: none; }
.btn.success { background: var(--success); color: white; border: none; }
.btn.outline { background: transparent; }

/* ──────────────────────────────────────────
   Glass Cards
   ────────────────────────────────────────── */
.glass-card {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(10px);
    border-radius: 1rem;
    padding: 1.5rem;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}

/* ──────────────────────────────────────────
   Dictionary Search
   ────────────────────────────────────────── */
.search-container {
    display: flex;
    gap: 1rem;
    margin-bottom: 2rem;
}

.search-wrapper {
    position: relative;
    flex: 1;
    display: flex;
}

.search-input {
    width: 100%;
    background: var(--card);
    border: 2px solid var(--glass-border);
    color: var(--text);
    padding: 1rem 3rem 1rem 1.5rem;
    border-radius: 0.75rem;
    font-size: clamp(1rem, 2.5vw, 1.1rem);
    outline: none;
    transition: border-color 0.3s;
    min-height: 48px;
}

.search-input:focus { border-color: var(--accent); }

.clear-btn {
    position: absolute;
    right: 0.75rem;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 1.2rem;
    padding: 0.5rem;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ──────────────────────────────────────────
   Results Grid
   ────────────────────────────────────────── */
.sections-grid {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 1.5rem;
    margin-top: 1.5rem;
}

.word-header {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 1.5rem;
}
.word-header h2 {
    font-size: clamp(1.75rem, 5vw, 2.5rem);
    color: var(--accent);
    text-transform: capitalize;
    word-break: break-word;
}
.word-header p {
    color: var(--text-muted);
    font-size: clamp(0.95rem, 2.5vw, 1.1rem);
}
.play-pronunciation { align-self: flex-start; margin-top: 0.5rem; }

.section h3 {
    font-size: clamp(1.05rem, 2.5vw, 1.25rem);
    color: var(--accent);
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* ──────────────────────────────────────────
   Lists
   ────────────────────────────────────────── */
.definition-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}
.definition-list li {
    background: rgba(0, 0, 0, 0.05);
    padding: 1rem;
    border-radius: 0.5rem;
    border-left: 4px solid var(--accent);
    font-size: clamp(0.9rem, 2.2vw, 1rem);
    line-height: 1.6;
}
[data-theme="dark"] .definition-list li { background: rgba(255, 255, 255, 0.05); }

.interactive-list {
    list-style: none;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}
.interactive-list li {
    background: var(--card);
    border: 1px solid var(--glass-border);
    padding: 0.5rem 1rem;
    border-radius: 2rem;
    cursor: pointer;
    font-size: clamp(0.8rem, 2vw, 0.9rem);
    transition: background 0.3s, color 0.3s, border-color 0.3s, transform 0.15s;
    min-height: 36px;
    display: flex;
    align-items: center;
    -webkit-tap-highlight-color: transparent;
}
.interactive-list li:hover { background: var(--accent); color: white; border-color: var(--accent); }
.interactive-list li:active { transform: scale(0.95); }

/* ──────────────────────────────────────────
   Text-to-Speech
   ────────────────────────────────────────── */
.tts-textarea {
    width: 100%;
    height: 150px;
    background: var(--card);
    color: var(--text);
    border: 2px solid var(--glass-border);
    border-radius: 0.75rem;
    padding: 1rem;
    font-size: clamp(1rem, 2.5vw, 1.1rem);
    resize: vertical;
    outline: none;
    margin-bottom: 1.5rem;
    min-height: 100px;
}
.tts-textarea:focus { border-color: var(--accent); }

.tts-controls {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
    margin-bottom: 1.5rem;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}
.control-group label {
    font-size: clamp(0.85rem, 2vw, 0.95rem);
    font-weight: 500;
}
.select {
    background: var(--card);
    color: var(--text);
    border: 1px solid var(--glass-border);
    padding: 0.75rem;
    border-radius: 0.5rem;
    outline: none;
    width: 100%;
    min-height: 44px;
    font-size: 0.95rem;
}
.slider {
    width: 100%;
    accent-color: var(--accent);
    min-height: 30px;
    cursor: pointer;
}

.tts-buttons { display: flex; gap: 1rem; }

/* ──────────────────────────────────────────
   Visualizer
   ────────────────────────────────────────── */
.visualizer {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    height: 60px;
    margin-bottom: 2rem;
    background: var(--glass-bg);
    border-radius: 1rem;
    border: 1px solid var(--glass-border);
    overflow: hidden;
}

.visualizer-bar {
    width: 6px;
    background: var(--accent);
    border-radius: 3px;
    animation: bounce 1s infinite ease-in-out;
}

@keyframes bounce {
    0%, 100% { height: 10px; }
    50% { height: 40px; }
}

/* ──────────────────────────────────────────
   Modal
   ────────────────────────────────────────── */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(5px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    padding: 1rem;
}
.modal-content {
    width: 100%;
    max-width: 500px;
    max-height: 80vh;
    max-height: 80dvh;
    overflow-y: auto;
    position: relative;
    animation: modalSlideIn 0.3s ease;
}
@keyframes modalSlideIn {
    from { opacity: 0; transform: translateY(20px) scale(0.97); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}
.close-modal {
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    background: none;
    border: none;
    color: var(--text);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0.5rem;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.5rem;
    transition: background 0.2s;
}
.close-modal:hover { background: rgba(0, 0, 0, 0.1); }
.modal-title { padding-right: 2.5rem; }
.modal-history-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-top: 1rem;
}
.modal-history-list li {
    padding: 0.75rem 1rem;
    background: var(--card);
    border: 1px solid var(--glass-border);
    border-radius: 0.5rem;
    cursor: pointer;
    transition: background 0.3s, border-color 0.3s, transform 0.15s;
    min-height: 44px;
    display: flex;
    align-items: center;
    font-size: clamp(0.9rem, 2.2vw, 1rem);
}
.modal-history-list li:hover { background: rgba(59, 130, 246, 0.1); border-color: var(--accent); }
.modal-history-list li:active { transform: scale(0.98); }

/* ──────────────────────────────────────────
   Status Messages
   ────────────────────────────────────────── */
.loading, .error {
    text-align: center;
    padding: 1.5rem;
    border-radius: 0.5rem;
    margin-bottom: 2rem;
    font-size: clamp(0.9rem, 2.2vw, 1rem);
}
.loading { background: rgba(59, 130, 246, 0.1); color: var(--accent); }
.error { background: rgba(239, 68, 68, 0.1); color: var(--error); }

/* ──────────────────────────────────────────
   Etymology
   ────────────────────────────────────────── */
.etymology-content {
    font-size: clamp(0.9rem, 2.2vw, 1rem);
    line-height: 1.7;
}

/* ──────────────────────────────────────────
   Footer
   ────────────────────────────────────────── */
footer {
    background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
    color: white;
    text-align: center;
    padding: 1.5rem 1rem;
    margin-top: auto;
    font-size: clamp(0.85rem, 2vw, 0.95rem);
}
footer a { color: white; text-decoration: underline; }

/* ══════════════════════════════════════════
   RESPONSIVE: Tablet (≤ 1024px)
   ══════════════════════════════════════════ */
@media (max-width: 1024px) {
    .sections-grid {
        grid-template-columns: 1fr 1fr;
        gap: 1.25rem;
    }

    .container {
        margin: 1.5rem auto;
    }
}

/* ══════════════════════════════════════════
   RESPONSIVE: Small Tablet / Large Phone (≤ 768px)
   ══════════════════════════════════════════ */
@media (max-width: 768px) {
    /* Header – Mobile Nav Toggle */
    header nav,
    #nav {
        display: none;
        position: absolute;
        top: 100%;
        left: 0;
        width: 100%;
        background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
        flex-direction: column;
        padding: 1rem;
        box-shadow: 0 8px 16px -4px rgba(0, 0, 0, 0.15);
        animation: navSlideDown 0.25s ease;
    }
    @keyframes navSlideDown {
        from { opacity: 0; transform: translateY(-8px); }
        to   { opacity: 1; transform: translateY(0); }
    }
    header nav.active,
    #nav.active { display: flex; }
    nav a {
        padding: 0.875rem 1rem;
        border-radius: 0.5rem;
        font-size: 1rem;
    }
    nav a:hover,
    nav a:active { background: rgba(255, 255, 255, 0.12); }
    .hamburger { display: flex; }

    /* Nav Tabs & Controls: Grid for touch targets */
    .nav-container {
        flex-direction: column;
        gap: 0.75rem;
    }
    .tab-buttons {
        display: grid;
        grid-template-columns: 1fr 1fr;
        width: 100%;
        gap: 0.5rem;
    }
    .controls {
        display: grid;
        grid-template-columns: 1fr 1fr;
        width: 100%;
        gap: 0.5rem;
    }
    .tab-button,
    .controls .btn {
        justify-content: center;
    }

    /* Search stacks vertically */
    .search-container {
        flex-direction: column;
        gap: 0.75rem;
    }
    #searchBtn {
        width: 100%;
        padding: 0.875rem;
    }

    /* Results go single-column */
    .sections-grid {
        grid-template-columns: 1fr;
    }

    /* TTS controls – 2-col on tablet */
    .tts-controls {
        grid-template-columns: repeat(2, 1fr);
        gap: 1rem;
    }

    /* TTS Buttons stack */
    .tts-buttons {
        flex-direction: column;
    }
    .tts-buttons .btn {
        width: 100%;
        padding: 0.875rem;
    }

    /* Glass-card padding trimmed */
    .glass-card { padding: 1.125rem; }

    /* Container reduced margins */
    .container {
        margin: 1.25rem auto;
    }
}

/* ══════════════════════════════════════════
   RESPONSIVE: Phones (≤ 576px)
   ══════════════════════════════════════════ */
@media (max-width: 576px) {
    header {
        padding: 0.75rem;
    }

    .container {
        margin: 1rem auto;
        padding: 0 0.75rem;
    }

    .nav-container { margin-bottom: 1.25rem; }

    /* TTS controls go single-col on phones */
    .tts-controls {
        grid-template-columns: 1fr;
        gap: 1rem;
    }

    /* Glass card */
    .glass-card {
        padding: 1rem;
        border-radius: 0.75rem;
    }

    /* Visualizer smaller */
    .visualizer {
        height: 50px;
        gap: 3px;
    }
    .visualizer-bar { width: 4px; }

    /* Word header */
    .word-header { margin-bottom: 1rem; }

    /* Search input */
    .search-input {
        padding: 0.875rem 2.5rem 0.875rem 1rem;
    }
}

/* ══════════════════════════════════════════
   RESPONSIVE: Very Small Phones (≤ 400px)
   ══════════════════════════════════════════ */
@media (max-width: 400px) {
    .brand h1 { font-size: 1rem; }

    .tab-button {
        padding: 0.625rem 0.5rem;
        font-size: 0.8rem;
        gap: 0.35rem;
    }
    .controls .btn {
        padding: 0.625rem 0.5rem;
        font-size: 0.8rem;
    }

    .definition-list li {
        padding: 0.75rem;
    }

    .interactive-list li {
        padding: 0.4rem 0.75rem;
        font-size: 0.8rem;
    }

    footer {
        padding: 1rem 0.75rem;
    }
}

/* ══════════════════════════════════════════
   RESPONSIVENESS: Landscape Phones
   ══════════════════════════════════════════ */
@media (max-height: 500px) and (orientation: landscape) {
    header { padding: 0.5rem 1rem; }
    .brand h1 { font-size: 1.1rem; }
    .container { margin: 0.75rem auto; }
    .tts-textarea { height: 100px; min-height: 80px; }
    .visualizer { height: 40px; }
    .nav-container { margin-bottom: 1rem; }
}

/* ══════════════════════════════════════════
   Safe Area (Notched Devices)
   ══════════════════════════════════════════ */
@supports (padding: env(safe-area-inset-bottom)) {
    footer {
        padding-bottom: calc(1.5rem + env(safe-area-inset-bottom));
    }
    header {
        padding-top: calc(0.875rem + env(safe-area-inset-top));
    }
}

/* ══════════════════════════════════════════
   Motion Preferences
   ══════════════════════════════════════════ */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ══════════════════════════════════════════
   Print Styles
   ══════════════════════════════════════════ */
@media print {
    header, footer, .hamburger, .controls, .tab-buttons,
    .search-container, .tts-buttons, .visualizer { display: none !important; }
    body { background: white; color: black; }
    .glass-card { background: white; border: 1px solid #ddd; box-shadow: none; backdrop-filter: none; }
}