/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary: #2C5F8D;
    --primary-light: #3D7FB8;
    --secondary: #4A9ECA;
    --background: #F8FAFB;
    --card-bg: #FFFFFF;
    --text: #2D3748;
    --text-light: #718096;
    --border: #E2E8F0;
    --shadow: rgba(0, 0, 0, 0.1);
    --success: #48BB78;
    --spacing: 1rem;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
        'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    background: var(--background);
    color: var(--text);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: var(--spacing);
}

/* Header */
.site-header {
    display: flex;
    align-items: center;
    gap: 0.75rem 1rem;
    flex-wrap: wrap;
    padding: 0.5rem 0 1rem;
    margin-bottom: 1.5rem;
    border-bottom: 1px solid var(--border);
}

.brand {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    color: var(--primary);
    text-decoration: none;
}

.brand-logo {
    width: 40px;
    height: 40px;
    flex: none;
}

.brand-name {
    font-size: 1.6rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    line-height: 1;
}

.brand-tld {
    color: var(--secondary);
    font-weight: 600;
}

.brand-tagline {
    color: var(--text-light);
    font-size: 0.95rem;
    padding-left: 1rem;
    border-left: 1px solid var(--border);
}

.site-nav {
    margin-left: auto;
}

.site-nav a,
.site-footer a {
    color: var(--primary);
    font-weight: 500;
    text-decoration: none;
}

.site-nav a:hover,
.site-footer a:hover {
    text-decoration: underline;
}

.site-nav a[aria-current="page"] {
    color: var(--text);
}

@media (max-width: 640px) {
    .brand-tagline {
        display: none;
    }

    .brand-name {
        font-size: 1.4rem;
    }

    .brand-logo {
        width: 34px;
        height: 34px;
    }
}

/* Show more */
.show-more-btn {
    display: block;
    margin: 0 auto 2rem;
    padding: 0.85rem 1.75rem;
    border: 2px solid var(--primary);
    border-radius: 12px;
    background: var(--card-bg);
    color: var(--primary);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
}

.show-more-btn[hidden] {
    display: none;
}

@media (hover: hover) {
    .show-more-btn:hover {
        background: var(--primary);
        color: white;
    }
}

/* Footer */
.site-footer {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 0.5rem 1.5rem;
    padding: 1.5rem 0 2rem;
    margin-top: 1rem;
    border-top: 1px solid var(--border);
    color: var(--text-light);
    font-size: 0.875rem;
}

body.selection-mode .site-footer {
    padding-bottom: 6rem;
}

/* About page */
.about {
    max-width: 42rem;
    margin: 0 auto 2rem;
}

.about h1 {
    font-size: clamp(1.6rem, 4vw, 2rem);
    color: var(--primary);
    margin-bottom: 1rem;
}

.about h2 {
    font-size: 1.2rem;
    margin: 2rem 0 0.5rem;
}

.about p,
.about ul {
    margin-bottom: 0.9rem;
}

.about ul {
    padding-left: 1.25rem;
}

.about a {
    color: var(--primary);
}

.about .lead {
    font-size: 1.125rem;
}

.about .credits {
    display: grid;
    grid-template-columns: max-content 1fr;
    gap: 0.4rem 1rem;
    padding: 1rem 1.25rem;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
}

.about .credits dt {
    color: var(--text-light);
}

@media (max-width: 480px) {
    .about .credits {
        grid-template-columns: 1fr;
    }

    .about .credits dd {
        margin-bottom: 0.4rem;
    }
}

/* Search Section */
.search-section {
    margin-bottom: 2rem;
    display: flex;
    gap: 0.75rem;
    align-items: stretch;
    flex-wrap: wrap;
}

.search-section #searchInput {
    flex: 1 1 280px;
}

.select-toggle-btn {
    padding: 0 1.5rem;
    border: 2px solid var(--border);
    background: var(--card-bg);
    color: var(--text);
    border-radius: 12px;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.select-toggle-btn:hover {
    border-color: var(--primary-light);
}

.select-toggle-btn.active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

#searchInput {
    width: 100%;
    padding: 1rem;
    font-size: 1.125rem;
    border: 2px solid var(--border);
    border-radius: 12px;
    background: var(--card-bg);
    transition: all 0.2s;
}

#searchInput:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(44, 95, 141, 0.1);
}

/* Category Navigation: wrapping chips on wider screens */
.category-nav {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 2rem;
}

.category-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    min-height: 44px;
    padding: 0.5rem 0.6rem 0.5rem 1rem;
    border: 2px solid var(--border);
    background: var(--card-bg);
    color: var(--text);
    border-radius: 999px;
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s, color 0.2s;
    white-space: nowrap;
}

.category-count {
    min-width: 1.75rem;
    padding: 0.1rem 0.45rem;
    border-radius: 999px;
    background: var(--background);
    color: var(--text-light);
    font-size: 0.8rem;
    font-variant-numeric: tabular-nums;
    text-align: center;
}

@media (hover: hover) {
    .category-btn:hover {
        border-color: var(--primary-light);
    }
}

.category-btn:focus-visible,
.category-sheet-btn:focus-visible {
    outline: 3px solid rgba(44, 95, 141, 0.35);
    outline-offset: 2px;
}

.category-btn.active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.category-btn.active .category-count {
    background: rgba(255, 255, 255, 0.2);
    color: white;
}

/* Phones: one button that opens a bottom sheet instead of chips */
.category-sheet-btn {
    display: none;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0 1rem;
    min-height: 48px;
    border: 2px solid var(--border);
    background: var(--card-bg);
    color: var(--text);
    border-radius: 12px;
    font-size: 1rem;
    cursor: pointer;
    min-width: 0;
}

.category-sheet-btn-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.category-sheet {
    margin: auto 0 0;
    width: 100%;
    max-width: 100%;
    max-height: 80vh;
    border: none;
    border-radius: 20px 20px 0 0;
    padding: 0 1rem calc(1rem + env(safe-area-inset-bottom));
    background: var(--card-bg);
    color: var(--text);
    overflow-y: auto;
}

.category-sheet[open] {
    animation: sheetSlideUp 0.25s ease-out;
}

.category-sheet::backdrop {
    background: rgba(0, 0, 0, 0.5);
}

@keyframes sheetSlideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

.category-sheet-head {
    position: sticky;
    top: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 0 0.75rem;
    background: var(--card-bg);
}

.category-sheet-head h2 {
    font-size: 1.125rem;
}

.category-sheet-close {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 50%;
    background: var(--background);
    font-size: 1.5rem;
    cursor: pointer;
}

.category-sheet-list {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.5rem;
}

.category-sheet-list .category-btn {
    justify-content: space-between;
    min-height: 52px;
    border-radius: 12px;
    white-space: normal;
    text-align: left;
}

@media (max-width: 640px) {
    .search-section {
        margin-bottom: 1.5rem;
    }

    .search-section #searchInput {
        flex-basis: 100%;
    }

    .category-sheet-btn {
        display: flex;
        flex: 1 1 0;
    }

    .select-toggle-btn {
        flex: 0 0 auto;
        min-height: 48px;
    }

    .category-nav {
        display: none;
    }
}

/* Word Count */
.word-count {
    color: var(--text-light);
    margin-bottom: 1rem;
    font-size: 0.875rem;
}

/* Word Grid */
.word-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

@media (max-width: 640px) {
    .word-grid {
        grid-template-columns: 1fr;
    }
}

.word-card {
    background: var(--card-bg);
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 2px 8px var(--shadow);
    transition: all 0.3s;
    cursor: pointer;
    position: relative;
}

.word-card-checkbox {
    position: absolute;
    top: 0.75rem;
    left: 0.75rem;
    width: 1.75rem;
    height: 1.75rem;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.95);
    border: 2px solid var(--border);
    display: none;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    color: var(--primary);
    z-index: 2;
    box-shadow: 0 1px 3px var(--shadow);
}

body.selection-mode .word-card-checkbox {
    display: flex;
}

body.selection-mode .word-card-actions {
    opacity: 0.4;
    pointer-events: none;
}

.word-card.selected {
    outline: 3px solid var(--primary);
    outline-offset: -3px;
}

.word-card.selected .word-card-checkbox {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.word-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 24px var(--shadow);
}

.word-card-image {
    width: 100%;
    height: 200px;
    object-fit: cover;
    background: var(--background);
    display: block;
}

.image-missing {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 200px;
    background: var(--background);
    color: var(--text-light);
    border: 2px dashed var(--border);
}

.word-card-content {
    padding: 1.25rem;
}

.word-card-title {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 0.5rem;
}

.word-card-category {
    display: inline-block;
    padding: 0.25rem 0.75rem;
    background: var(--background);
    color: var(--text-light);
    border-radius: 12px;
    font-size: 0.875rem;
    margin-bottom: 0.5rem;
}

.word-card-actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 1rem;
}

.btn {
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 8px;
    font-size: 0.875rem;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

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

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

.btn-secondary {
    background: var(--background);
    color: var(--text);
}

.btn-secondary:hover {
    background: var(--border);
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    z-index: 1000;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.modal.active {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.modal-content {
    background: var(--card-bg);
    border-radius: 16px;
    max-width: 600px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    position: relative;
    animation: modalSlideIn 0.3s ease-out;
}

@keyframes modalSlideIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.modal-close {
    position: sticky;
    top: 0;
    right: 0;
    float: right;
    background: var(--background);
    border: none;
    font-size: 2rem;
    width: 3rem;
    height: 3rem;
    border-radius: 50%;
    cursor: pointer;
    margin: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
}

.modal-close:hover {
    background: var(--border);
}

#modalBody {
    padding: 2rem;
}

.modal-word-image {
    width: 100%;
    height: auto;
    border-radius: 12px;
    margin-bottom: 1.5rem;
}

.modal-word-title {
    font-size: 2.5rem;
    font-weight: 700;
    color: var(--primary);
    margin-bottom: 1rem;
}

.modal-word-category {
    display: inline-block;
    padding: 0.5rem 1rem;
    background: var(--background);
    color: var(--text-light);
    border-radius: 16px;
    font-size: 1rem;
    margin-bottom: 1.5rem;
}

.modal-section {
    margin-bottom: 1.5rem;
}

.modal-section-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 0.75rem;
}

.modal-section-content {
    color: var(--text);
    line-height: 1.8;
}

.audio-player {
    margin-top: 1rem;
}

.audio-player audio {
    width: 100%;
    border-radius: 8px;
}

/* Empty State */
.empty-state {
    text-align: center;
    padding: 4rem 2rem;
    color: var(--text-light);
}

.empty-state p {
    font-size: 1.25rem;
}

/* Loading State */
.loading {
    text-align: center;
    padding: 4rem 2rem;
    color: var(--text-light);
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Selection Bar: floating pill on wide screens, docked panel on phones */
.selection-bar {
    position: fixed;
    bottom: 1rem;
    left: 50%;
    transform: translateX(-50%);
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
    padding: 0.5rem 0.5rem 0.5rem 1.25rem;
    display: flex;
    align-items: center;
    gap: 1.25rem;
    z-index: 500;
    max-width: calc(100vw - 2rem);
    white-space: nowrap;
}

.selection-bar[hidden] { display: none; }

.selection-summary {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.selection-count {
    font-weight: 600;
    color: var(--primary);
    margin-right: 0.5rem;
    font-variant-numeric: tabular-nums;
}

.link-btn {
    background: none;
    border: none;
    padding: 0.5rem;
    border-radius: 8px;
    font: inherit;
    font-size: 0.875rem;
    color: var(--text-light);
    cursor: pointer;
}

.link-btn:disabled {
    opacity: 0.4;
    cursor: default;
}

.selection-actions {
    display: flex;
    gap: 0.5rem;
}

.selection-actions .btn {
    justify-content: center;
    min-height: 2.5rem;
}

.selection-actions .btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

@media (hover: hover) {
    .link-btn:not(:disabled):hover {
        background: var(--background);
        color: var(--text);
    }
}

.print-dropdown {
    position: relative;
}

.print-menu {
    position: absolute;
    bottom: calc(100% + 0.5rem);
    right: 0;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 4px 16px var(--shadow);
    padding: 0.25rem;
    min-width: 140px;
    display: flex;
    flex-direction: column;
}

.print-menu[hidden] { display: none; }

.print-menu button {
    background: transparent;
    border: none;
    text-align: left;
    padding: 0.625rem 0.875rem;
    font-size: 0.95rem;
    color: var(--text);
    border-radius: 8px;
    cursor: pointer;
}

.print-menu button:hover {
    background: var(--background);
}

@media (max-width: 640px) {
    .selection-bar {
        left: 0;
        right: 0;
        bottom: 0;
        transform: none;
        max-width: none;
        border-radius: 16px 16px 0 0;
        border-width: 1px 0 0;
        box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.1);
        padding: 0.5rem 1rem calc(0.75rem + env(safe-area-inset-bottom, 0px));
        flex-direction: column;
        align-items: stretch;
        gap: 0.5rem;
    }

    .selection-summary .selection-count {
        margin-right: auto;
    }

    .selection-actions {
        display: grid;
        grid-template-columns: 1fr 1fr;
    }

    .selection-actions .btn {
        width: 100%;
        min-height: 2.75rem;
        font-size: 1rem;
    }

    .print-menu {
        left: 0;
        right: 0;
    }

    .print-menu button {
        padding: 0.875rem 1rem;
        font-size: 1rem;
    }

    body.selection-mode .site-footer {
        padding-bottom: 9rem;
    }
}

/* Print container (hidden except when printing) */
.print-container {
    display: none;
}

/* Print Styles */
@media print {
    @page { margin: 1cm; }

    html,
    body {
        background: #fff !important;
    }

    .print-container {
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    body > *:not(#printContainer) {
        display: none !important;
    }

    .print-container {
        display: block !important;
    }

    .print-page {
        page-break-after: always;
        break-after: page;
        width: 100%;
        height: 100vh;
        display: flex;
        flex-direction: column;
        gap: 0.4cm;
        color: #000;
    }

    .print-header,
    .print-footer {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 1rem;
        flex: 0 0 auto;
    }

    .print-header {
        padding-bottom: 0.25cm;
        border-bottom: 1px solid #ccc;
    }

    .print-brand {
        display: flex;
        align-items: center;
        gap: 0.4rem;
        font-size: 1.1rem;
        font-weight: 700;
        color: var(--primary);
    }

    .print-brand .brand-tld {
        font-weight: 400;
    }

    .print-logo {
        width: 1.75rem;
        height: 1.75rem;
    }

    .print-title {
        font-size: 0.9rem;
        color: #555;
    }

    .print-footer {
        padding-top: 0.2cm;
        border-top: 1px solid #ccc;
        font-size: 0.7rem;
        color: #555;
    }

    .print-grid {
        flex: 1 1 auto;
        min-height: 0;
        display: grid;
        gap: 0.5cm;
    }

    .print-page:last-child {
        page-break-after: auto;
    }

    .print-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        overflow: hidden;
        break-inside: avoid;
    }

    .print-item img {
        max-width: 100%;
        max-height: 100%;
        object-fit: contain;
        flex: 1 1 auto;
        min-height: 0;
    }

    .print-label {
        margin-top: 0.5rem;
        font-size: 1.25rem;
        font-weight: 600;
        text-align: center;
        flex: 0 0 auto;
    }

    .print-1up .print-grid {
        grid-template-columns: 1fr;
        grid-template-rows: 1fr;
    }
    .print-1up .print-label {
        font-size: 2rem;
    }

    .print-2x2 .print-grid {
        grid-template-columns: 1fr 1fr;
        grid-template-rows: 1fr 1fr;
    }
    .print-2x2 .print-label {
        font-size: 1.5rem;
    }

    .print-4x4 .print-grid {
        grid-template-columns: repeat(4, 1fr);
        grid-template-rows: repeat(4, 1fr);
    }
    .print-4x4 .print-label {
        font-size: 0.9rem;
    }
}
