:root {
    --primary-color: #3498db;
    --text-color: #333;
    --bg-color: #f8f9fa;
    --card-bg: #fff;
    --border-color: #e9ecef;
    --spacing: 16px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    color: var(--text-color);
    background-color: var(--bg-color);
    line-height: 1.6;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Header */
.app-header {
    background: var(--card-bg);
    padding: var(--spacing);
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    z-index: 100;
    display: flex;
    flex-direction: column;
    gap: 10px;
    position: sticky;
    top: 0;
}

.header-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.doc-title {
    font-size: 1.1rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 70%;
}

.doc-select-btn {
    font-size: 0.9rem;
    color: var(--primary-color);
    background: none;
    border: none;
    cursor: pointer;
}

.search-box {
    position: relative;
    width: 100%;
}

.search-input {
    width: 100%;
    padding: 10px 15px;
    border: 1px solid var(--border-color);
    border-radius: 20px;
    background: var(--bg-color);
    font-size: 0.95rem;
    transition: all 0.3s ease;
}

.search-input:focus {
    outline: none;
    border-color: var(--primary-color);
    background: #fff;
    box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
}

/* Main Content */
.app-content {
    flex: 1;
    overflow-y: auto;
    padding: var(--spacing);
    position: relative;
    /* Ensure smooth scrolling */
    scroll-behavior: smooth;
    width: 100%; /* Ensure full width usage */
}

.page-container {
    background: var(--card-bg);
    padding: 15px; /* Reduced padding for mobile */
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    /* Remove min-height: 100% to allow natural height */
    min-height: auto; 
    /* Ensure content can expand */
    height: auto;
    max-width: 100%; /* Allow full width on mobile */
    margin: 0 auto;
    /* Ensure text wrapping */
    overflow-wrap: break-word;
    word-wrap: break-word;
    /* Prevent horizontal scroll */
    overflow-x: hidden;
}

/* Ensure highlights are visible but don't break layout */
mark {
    background-color: #ffeb3b;
    color: #000;
    padding: 2px 0;
    border-radius: 2px;
}

.content-html {
    font-size: 17px; /* Slightly larger base size */
    line-height: 2; /* Increased line height for better readability */
    letter-spacing: 0.5px; /* Add slight letter spacing */
    color: #2c3e50; /* Softer dark color */
    padding: 0 5px; /* Add internal padding */
    text-align: justify; /* Justify text for clean edges */
}

/* Ensure paragraph content wraps correctly */
.content-html p, 
.content-html div,
.content-html span,
.content-html table,
.content-html tbody,
.content-html tr,
.content-html td,
.content-html {
    white-space: normal !important; /* Force normal wrapping */
    word-break: break-all !important; /* Aggressively break words to prevent overflow */
    overflow-wrap: break-word !important; /* Standard property for breaking */
    word-wrap: break-word !important; /* Legacy support */
    max-width: 100% !important; /* Ensure no element exceeds container width */
    box-sizing: border-box !important;
}

/* Restore spacing for block elements */
.content-html p,
.content-html div {
    margin-bottom: 1.5em;
}

/* Specific fix for highlighting span wrapper */
.content-html span {
    display: inline; /* Keep spans inline */
}

/* Ensure images don't overflow */
.content-html img {
    max-width: 100% !important;
    height: auto !important;
    display: block;
    margin: 10px auto;
    border-radius: 4px;
}

.loading, .error-msg, .empty-state {
    text-align: center;
    padding: 40px 20px;
    color: #666;
}

.error-msg {
    color: #e74c3c;
}

/* Footer / Controls */
.app-footer {
    background: #ffffff;
    border-top: 1px solid #eaeaea;
    display: flex;
    flex-direction: column;
    z-index: 100;
    padding-bottom: env(safe-area-inset-bottom);
}

.nav-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 15px;
    background: #fff;
}

.search-summary-bar {
    background: #f1f8ff;
    padding: 8px var(--spacing);
    border-bottom: 1px solid #d0e3ff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.9rem;
    color: var(--primary-color);
}

.hot-keywords {
    padding: 8px var(--spacing);
    color: #555;
    display: flex;
    align-items: center;
}
.hot-label {
    margin-right: 8px;
    font-size: 0.9rem;
    display: none;
    white-space: nowrap;
    flex-shrink: 0;
}
.hot-list {
    display: flex;
    flex-wrap: nowrap; /* Force single line */
    overflow-x: auto; /* Allow horizontal scrolling if needed */
    gap: 8px;
    white-space: nowrap; /* Prevent text wrapping */
    width: auto; /* Adapt to content */
    min-width: 0; /* Allow shrinking for scroll */
    scrollbar-width: none; /* Hide scrollbar Firefox */
    align-items: center;
}
.hot-list::-webkit-scrollbar {
    display: none; /* Hide scrollbar Chrome/Safari */
}
.hot-chip {
    padding: 6px 10px;
    border: 1px solid var(--border-color);
    border-radius: 16px;
    background: #fff;
    cursor: pointer;
    font-size: 0.9rem;
}
.hot-chip:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
}

.close-search-btn {
    background: none;
    border: none;
    color: #666;
    font-size: 1.2rem;
    cursor: pointer;
    line-height: 1;
}

/* Container for the matrix of numbers */
.search-cards-container {
    padding: 10px var(--spacing);
    background: #f8f9fa;
    border-bottom: 1px solid var(--border-color);
    /* Remove flex row layout */
    display: grid;
    /* Create responsive grid: min 32px per item, auto fill - Tighter grid */
    grid-template-columns: repeat(auto-fill, minmax(32px, 1fr));
    gap: 6px; /* Reduced gap */
    /* Fixed height for approx 3 rows */
    height: auto;
    max-height: 150px; 
    overflow-y: auto;
    align-content: start;
}

/* Pagination controls for result pages */
.search-pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 8px;
    background: #fff;
    border-top: 1px solid var(--border-color);
    gap: 15px;
    font-size: 0.9rem;
    color: #666;
}

.search-page-btn {
    background: #ffeb3b; /* Yellow background */
    border: 1px solid #e0e0e0;
    border-radius: 4px;
    padding: 4px 10px;
    cursor: pointer;
    font-size: 0.8rem;
    color: #333; /* Ensure text is readable */
    font-weight: 500;
}

.search-page-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    background: #e0e0e0; /* Gray out when disabled */
    color: #999;
}

/* Minimalist pagination style search result */
.search-card {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 32px; /* Smaller size */
    height: 32px;
    background: #fff;
    border: 1px solid var(--border-color);
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    font-size: 0.8rem; /* Smaller font */
    font-weight: 600;
    color: var(--text-color);
    margin: 0 auto; /* Center in grid cell */
}

.search-card:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
}

.search-card.active {
    background-color: var(--primary-color);
    border-color: var(--primary-color);
    color: #fff;
    box-shadow: 0 2px 5px rgba(52, 152, 219, 0.4);
    transform: scale(1.1);
}

/* Hide snippet and header since we only show number */
.search-card-header,
.search-card-snippet {
    display: none;
}

.nav-btn {
    padding: 6px 16px;
    border: 1px solid #ddd;
    background: #fff;
    border-radius: 20px;
    font-size: 0.9rem;
    color: #555;
    cursor: pointer;
    transition: all 0.2s;
}

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

.nav-btn:active:not(:disabled) {
    background: var(--bg-color);
}

.page-info {
    font-size: 0.9rem;
    color: #666;
}

/* Search Results Overlay - REMOVED/HIDDEN as we now use footer results */
.search-results-panel {
    display: none !important; /* Force hide old dropdown */
}

.search-card.locked-card {
    background: #f0f0f0;
    color: #999;
    position: relative;
    border: 1px solid #ddd;
}
.search-card.locked-card:hover {
    background: #e0e0e0;
}
.lock-icon {
    font-size: 0.7rem;
    position: absolute;
    top: 2px;
    right: 2px;
}
.modal-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.5); z-index: 2000;
    display: none; justify-content: center; align-items: center;
}
.modal-overlay.active { display: flex; }
.modal-content {
    background: #fff; width: 90%; max-width: 400px;
    border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    display: flex; flex-direction: column; max-height: 80vh;
}
.modal-header {
    padding: 15px 20px; border-bottom: 1px solid #eee;
    display: flex; justify-content: space-between; align-items: center;
    font-weight: bold;
}
.modal-close {
    background: none; border: none; font-size: 1.5rem; cursor: pointer; color: #999;
}
.doc-list {
    overflow-y: auto; padding: 10px 0;
}
.doc-item {
    padding: 12px 20px; border-bottom: 1px solid #f5f5f5; cursor: pointer;
}
.doc-item:hover { background: #f9f9f9; }
.doc-item-title { font-weight: 500; margin-bottom: 4px; }
.doc-item-meta { font-size: 0.85rem; color: #888; }
.header-controls { display: flex; align-items: center; justify-content: flex-start; gap: 8px; }

/* Responsive adjustments */
@media (min-width: 768px) {
    .page-container {
        padding: 40px; /* More padding on desktop */
        max-width: 800px; /* Constrain width on desktop */
    }

    .app-header {
        flex-direction: row;
        align-items: center;
    }
    
    .doc-title {
        font-size: 1.6rem;
        font-weight: 700;
        color: #2c3e50;
        letter-spacing: 0.5px;
    }
    .header-top { width: 50%; flex-direction: column; align-items: flex-start; gap: 6px; }
    .header-controls { margin-top: 4px; }
    .search-box {
        max-width: 640px;
        margin: 0 auto;
    }
    #personalCenterBtn {
        display: none;
        background: none;
        color: #e67e22;
        border: 1px solid #e67e22;
        border-radius: 18px;
        padding: 6px 12px;
        font-size: 0.9rem;
        white-space: nowrap;
        word-break: keep-all;
        cursor: pointer;
    }
    #personalCenterBtn.active-display {
        display: inline-block !important;
    }
    #loginBtn {
    display: inline-block !important;
    background: none !important;
    color: #27ae60;
    border: 1px solid #eaeaea;
    border-radius: 18px;
    padding: 6px 12px;
    font-size: 0.9rem;
    white-space: nowrap;
    word-break: keep-all;
    cursor: pointer;
}
#docSelectBtn {
    color: #3498db;
    border: none;
    background: none;
    padding: 0;
    font-size: 0.95rem;
    white-space: nowrap;
    word-break: keep-all;
}
    
    .search-results-panel {
        top: 100%;
        width: 350px;
        right: 0;
        left: auto;
        border-radius: 0 0 8px 8px;
        box-shadow: 0 4px 10px rgba(0,0,0,0.1);
    }
    .hot-label {
        display: inline;
    }
    .hot-keywords { padding-top: 6px; }
}
