:root {
    --page: #f7f6f3;
    --paper: #ffffff;
    --ink: #2f2f2f;
    --muted: #787774;
    --line: #e8e6e1;
    --soft: #efede8;
    --hover: #f1f1ef;
    --accent: #3578c6;
    --accent-soft: #e8f1fb;
    --red: #d44c47;
    --red-soft: #fdebec;
    --green: #2f8f5b;
    --green-soft: #e7f4ed;
    --yellow: #b7791f;
    --shadow: 0 24px 70px rgba(31, 29, 24, 0.1), 0 4px 16px rgba(31, 29, 24, 0.05);
    color-scheme: light;
}

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

body {
    font-family: "Source Han Serif SC", "Noto Serif CJK SC", "Songti SC", SimSun, serif;
    letter-spacing: 0;
    background-color: var(--page);
    color: var(--ink);
    min-height: 100vh;
    line-height: 1.6;
}

/* Layout */
.app {
    width: min(1240px, calc(100vw - 36px));
    margin: 0 auto;
    padding: 40px 0 60px;
}

/* Header */
.topbar {
    margin-bottom: 40px;
    border-bottom: 1px solid var(--line);
    padding-bottom: 20px;
}

h1.title {
    margin: 0;
    font-size: clamp(31px, 5vw, 42px);
    line-height: 1.1;
    font-weight: 700;
    color: var(--ink);
}

.subtitle {
    display: block;
    color: var(--muted);
    font-size: 16px;
    margin-top: 8px;
    font-family: system-ui, -apple-system, sans-serif;
    font-weight: 400;
}

/* Search Box */
.search-container {
    margin: 30px 0;
}

.input-wrapper {
    display: flex;
    gap: 12px;
}

#searchInput {
    flex-grow: 1;
    font-family: "Source Han Serif SC", "Noto Serif CJK SC", "Songti SC", SimSun, serif;
    font-size: 1.2rem;
    padding: 12px 16px;
    background: var(--paper);
    border: 1px solid rgba(120, 119, 116, 0.2);
    border-radius: 6px;
    color: var(--ink);
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}

#searchInput:focus {
    border-color: var(--muted);
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

#searchInput::placeholder {
    color: #a0a09e;
}

.search-btn {
    font-family: system-ui, -apple-system, sans-serif;
    font-size: 14px;
    background: var(--paper);
    border: 1px solid rgba(120, 119, 116, 0.2);
    border-radius: 6px;
    padding: 12px 24px;
    color: var(--ink);
    cursor: pointer;
    transition: all 150ms ease;
    box-shadow: 0 1px 2px rgba(0,0,0,0.03);
    font-weight: 500;
}

.search-btn:hover {
    background: var(--hover);
    border-color: #c8c3b8;
}

.search-btn:active {
    transform: translateY(1px);
}

.idiom-btn {
    background: var(--paper);
    border: 1px solid rgba(120, 119, 116, 0.2);
    padding: 10px 16px;
    border-radius: 8px;
    font-size: 16px;
    color: var(--ink);
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 1px 2px rgba(31, 29, 24, 0.04);
    font-family: inherit;
    font-weight: 500;
}

.idiom-btn:hover {
    background: #fff;
    border-color: var(--ink);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(31, 29, 24, 0.06);
}

.idiom-btn.active {
    background: var(--ink);
    color: var(--paper);
    border-color: var(--ink);
}

.idiom-control {
    height: 38px;
    padding: 0 16px;
    background: rgba(255, 255, 255, 0.8);
    border: 1px solid rgba(120, 119, 116, 0.3);
    border-radius: 8px;
    font-size: 14px;
    color: var(--ink);
    cursor: pointer;
    box-shadow: 0 1px 2px rgba(31, 29, 24, 0.04);
    transition: all 0.2s ease;
    font-family: inherit;
    outline: none;
    font-weight: 500;
}

select.idiom-control {
    appearance: none;
    -webkit-appearance: none;
    padding-right: 36px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2337352f' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    background-size: 16px;
}

.idiom-control:hover {
    background: #fff;
    border-color: var(--ink);
}

.idiom-control:active {
    transform: translateY(1px);
}

.search-hint {
    margin-top: 10px;
    font-size: 13px;
    color: var(--muted);
    font-family: system-ui, -apple-system, sans-serif;
    padding-left: 2px;
}

/* Mode Switch Styles */
.mode-switch {
    display: flex;
    background: transparent;
    border: 1px solid var(--ink);
    border-radius: 8px;
    padding: 2px;
    height: 38px;
    box-sizing: border-box;
}

.mode-btn {
    background: transparent;
    border: none;
    padding: 0 16px;
    border-radius: 6px;
    font-size: 14px;
    color: var(--ink);
    cursor: pointer;
    transition: all 0.2s ease;
    font-weight: 500;
}

.mode-btn.active {
    background: var(--ink);
    color: var(--paper);
}

/* Blur Effect for Test Mode */
.blur-text {
    filter: blur(8px) opacity(0.5);
    pointer-events: none;
    user-select: none;
    transition: filter 0.4s ease, opacity 0.4s ease;
}

/* Reveal Button */
.reveal-btn-container {
    text-align: center;
    margin-top: 20px;
}

.reveal-btn {
    height: 38px;
    padding: 0 24px;
    border-radius: 8px;
    background: var(--ink);
    color: var(--paper);
    border: 1px solid var(--ink);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    display: none;
}

.reveal-btn:hover {
    background: #333;
    border-color: #333;
}

/* Sentence Preview */
.sentence-preview-container {
    background: var(--paper);
    border: 1px solid rgba(120, 119, 116, 0.14);
    border-radius: 10px;
    padding: 24px 30px;
    margin-bottom: 40px;
    box-shadow: 0 2px 8px rgba(31, 29, 24, 0.04);
}

.sentence-header {
    margin-bottom: 20px;
    border-bottom: 1px solid var(--line);
    padding-bottom: 12px;
}

.sentence-header h3 {
    margin: 0;
    font-size: 18px;
    color: var(--ink);
}

.sentence-sequence {
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
    align-items: flex-end;
}

.sentence-char {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    min-width: 60px;
}

.sentence-char .ancient-img {
    height: 60px;
    width: auto;
    object-fit: contain;
}

.sentence-char .modern-char {
    font-size: 18px;
    font-weight: bold;
    color: var(--ink);
    border-top: 1px dashed var(--line);
    padding-top: 8px;
    width: 100%;
    text-align: center;
}

.sentence-char .ancient-text {
    font-size: 11px;
    color: var(--muted);
    text-align: center;
    font-family: system-ui, -apple-system, sans-serif;
    max-width: 70px;
    line-height: 1.2;
}

/* Results Container */
.results-container {
    display: grid;
    grid-template-columns: 1fr;
    gap: 24px;
    margin-bottom: 40px;
}

@media (min-width: 768px) {
    .results-container {
        grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
        align-items: start;
    }
}

/* Panel (Card) */
.panel {
    background: var(--paper);
    border: 1px solid rgba(120, 119, 116, 0.14);
    border-radius: 10px;
    padding: 24px 30px;
    box-shadow: 0 2px 8px rgba(31, 29, 24, 0.04);
    max-width: 100%;
    min-width: 0;
}

.char-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
    border-bottom: 1px solid var(--line);
    padding-bottom: 12px;
}

.char-title {
    font-size: 32px;
    font-weight: 700;
    color: var(--ink);
    margin: 0;
    line-height: 1;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 12px;
}

/* Button */
.action-btn {
    border: 1px solid #d8d5cc;
    background: rgba(255, 255, 255, 0.72);
    min-height: 32px;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 13px;
    color: var(--ink);
    cursor: pointer;
    transition: background 150ms ease, border-color 150ms ease;
    box-shadow: 0 1px 1px rgba(31, 29, 24, 0.04);
    font-family: system-ui, -apple-system, sans-serif;
}

.action-btn:hover {
    background: #fff;
    border-color: #c8c3b8;
}

/* Loading */
.loading-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--muted);
    font-size: 13px;
    font-family: system-ui, -apple-system, sans-serif;
}

.spinner {
    width: 14px;
    height: 14px;
    border: 2px solid var(--line);
    border-top-color: var(--muted);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

.error-message {
    color: var(--red);
    font-size: 13px;
    background: var(--red-soft);
    padding: 6px 10px;
    border-radius: 6px;
    font-family: system-ui, -apple-system, sans-serif;
}

/* Content */
.char-content {
    overflow-x: auto;
}

/* Custom Scrollbar */
.char-content::-webkit-scrollbar {
    height: 6px;
}
.char-content::-webkit-scrollbar-track {
    background: var(--soft);
    border-radius: 3px;
}
.char-content::-webkit-scrollbar-thumb {
    background: #d3d1cb;
    border-radius: 3px;
}
.char-content::-webkit-scrollbar-thumb:hover {
    background: var(--muted);
}

/* Parsed Table */
#yanbian_result {
    width: 100%;
    border-collapse: separate;
    border-spacing: 8px;
    min-width: max-content;
}

#yanbian_result td {
    padding: 16px;
    text-align: center;
    vertical-align: top;
    border-radius: 6px;
}

#yanbian_result td:empty {
    display: none !important;
}

#yanbian_result td.VariantListA:empty,
#yanbian_result td.VariantListB:empty {
    display: none !important;
    border: none !important;
}

.VariantListA, .VariantListB {
    background: #fdfdfc;
    border: 1px solid var(--line);
}

.charValue {
    max-width: 60px;
    height: auto;
    margin-bottom: 10px;
    filter: none; /* Removed the invert effect from dark mode */
}

/* Hide Unnecessary Original Elements */
.Information, 
.RemindMessage, 
.ZiList,
#StartOrder,
tr:first-child td[colspan="10"] table,
#yanbian_result tr:has(td[colspan="10"]),
#yanbian_result td[colspan="10"] {
    display: none !important;
    border: none !important;
    padding: 0 !important;
    margin: 0 !important;
    height: 0 !important;
}

#yanbian_result br {
    display: block;
    content: "";
    margin-top: 4px;
}

#yanbian_result td {
    font-size: 14px;
    color: var(--ink);
    line-height: 1.5;
}

/* Footer */
.app-footer {
    margin-top: 60px;
    padding-top: 20px;
    border-top: 1px solid var(--line);
    text-align: center;
    font-family: system-ui, -apple-system, sans-serif;
    font-size: 13px;
    color: var(--muted);
    line-height: 1.6;
}

.app-footer a {
    color: var(--muted);
    text-decoration: underline;
    text-decoration-color: rgba(120, 119, 116, 0.4);
    transition: color 0.2s;
}

.app-footer a:hover {
    color: var(--ink);
    text-decoration-color: var(--ink);
}

/* Responsive optimizations for mobile */
@media (max-width: 650px) {
    /* Header layout */
    .controls-header {
        flex-direction: column;
        align-items: stretch !important;
        gap: 12px !important;
    }
    
    .controls-actions {
        width: 100%;
        flex-wrap: wrap;
        gap: 12px;
    }
    
    .mode-switch {
        width: 100%;
        height: 44px;
        flex: none;
    }
    
    .mode-switch .mode-btn {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 0;
    }
    
    #bankSelect {
        flex: 1;
        min-width: 0;
        height: 44px;
    }
    
    #refreshBtn {
        flex: 0 0 auto;
        height: 44px;
    }

    /* Grid adaptations */
    .results-container {
        grid-template-columns: 1fr;
    }
    
    .input-wrapper, .search-input-wrapper {
        flex-direction: column;
    }
    
    #searchInput {
        width: 100%;
    }
    
    .search-btn, #searchBtn {
        width: 100%;
    }
    
    /* Sentence preview container */
    .sentence-preview-container {
        padding: 16px 12px;
    }
    
    .sentence-sequence {
        justify-content: center;
        gap: 12px;
    }
    
    .sentence-header {
        flex-direction: column;
        gap: 10px;
    }
    
    .sentence-header .action-btn {
        align-self: flex-start;
    }
}

@media (max-width: 650px) {
    .panel {
        padding: 16px 14px;
    }
}
