:root {
    --primary-color: #8b4513; /* SaddleBrown */
    --secondary-color: #d2b48c; /* Tan */
    --bg-color: #fdf5e6; /* OldLace */
    --text-color: #333;
    --success-color: #2e8b57;
    --error-color: #cd5c5c;
    --input-bg: #fff;
}

/* Responsive Font Scaling */
html {
    font-size: 16px;
}

@media (max-width: 1200px) {
    html {
        font-size: 15px;
    }
}

@media (max-width: 992px) {
    html {
        font-size: 14px;
    }
}

@media (max-width: 768px) {
    html {
        font-size: 13px;
    }
}

@media (max-width: 480px) {
    html {
        font-size: 12px;
    }
}

body {
    font-family: "Kaiti", "STKaiti", "KaiTi", serif; /* 使用楷体更有古风感 */
    background-color: var(--bg-color);
    color: var(--text-color);
    margin: 0;
    padding: 20px;
    display: flex;
    justify-content: center;
    min-height: 100vh;
}

.container {
    max-width: 1400px;
    width: 90%;
    background-color: rgba(255, 255, 255, 0.8);
    padding: 40px;
    border-radius: 10px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0IiBoZWlnaHQ9IjQiPgo8cmVjdCB3aWR0aD0iNCIgaGVpZ2h0PSI0IiBmaWxsPSIjZmZmIi8+CjxyZWN0IHdpZHRoPSIxIiBoZWlnaHQ9IjEiIGZpbGw9IiNjY2MiLz4KPC9zdmc+'); /* 简单的纹理 */
    transition: margin 0.3s, width 0.3s, padding 0.3s;
}

header {
    text-align: center;
    margin-bottom: 30px;
    border-bottom: 2px solid var(--secondary-color);
    padding-bottom: 20px;
}

h1 {
    margin: 0 0 20px 0;
    font-size: clamp(2rem, 4vw, 3.5rem);
    color: var(--primary-color);
}

.controls {
    display: flex;
    justify-content: center;
    gap: 20px;
    align-items: center;
}

select {
    padding: 8px 40px 8px 16px;
    font-size: 1rem;
    border: 1px solid var(--primary-color);
    border-radius: 5px;
    background-color: var(--input-bg);
    font-family: inherit;
    color: var(--primary-color);
    cursor: pointer;
    
    /* Custom Arrow */
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238b4513' 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;
}

button {
    padding: 8px 20px;
    font-size: 1rem;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-family: inherit;
    transition: background-color 0.2s;
}

button:disabled {
    background-color: #ccc !important;
    color: #888 !important;
    cursor: not-allowed;
    box-shadow: none;
}

#new-game-btn {
    background-color: var(--primary-color);
    color: white;
}

#new-game-btn:hover {
    background-color: #69340e;
}

#poem-info {
    text-align: center;
    margin-bottom: 30px;
}

#poem-title {
    font-size: 1.8em;
    margin-bottom: 10px;
}

#poem-author {
    font-size: 1.2em;
    color: #666;
}

.poem-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
    font-size: clamp(1.2rem, 2.5vw, 2.2rem);
    margin-bottom: 30px;
}

.poem-line {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
}

.char-box {
    width: 2em;
    height: 2em;
    line-height: 2em;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 4px;
    background-color: #fff;
    user-select: none;
}

.char-input {
    width: 2em;
    height: 2em;
    font-size: 1em;
    text-align: center;
    border: 1px solid var(--primary-color);
    border-radius: 4px;
    background-color: #fff8dc;
    font-family: inherit;
    outline: none;
    padding: 0;
}

.char-input:focus,
.char-input.active-focus {
    box-shadow: 0 0 8px var(--primary-color), 0 0 0 2px rgba(139, 69, 19, 0.2);
    border-color: var(--primary-color);
    background-color: #fff;
    transform: scale(1.1);
    z-index: 10;
}

.char-input.correct {
    background-color: #e0f0e0;
    border-color: var(--success-color);
    color: var(--success-color);
}

.char-input.incorrect {
    background-color: #ffe0e0;
    border-color: var(--error-color);
}

.game-actions {
    display: flex;
    justify-content: center;
    gap: 20px;
    margin-top: 30px;
    
    /* Sticky footer optimization */
    position: sticky;
    bottom: 0;
    z-index: 900;
    /* background-color: rgba(253, 245, 230, 0.95); */
    padding: 15px 10px;
    /* box-shadow: 0 -2px 10px rgba(0,0,0,0.05); */
}

body.pool-bottom.pool-visible .game-actions {
    bottom: 30vh; /* Sit above the pool */
}

.primary-btn {
    background-color: var(--success-color);
    color: white;
    padding: 10px 30px;
    font-size: 1.2em;
}

.primary-btn:hover {
    background-color: #236b42;
}

.secondary-btn {
    background-color: var(--secondary-color);
    color: var(--primary-color);
    padding: 10px 20px;
}

.secondary-btn:hover {
    background-color: #c1a278;
}

#message-area {
    text-align: center;
    margin-top: 20px;
    font-size: 1.2em;
    min-height: 1.5em;
    color: var(--primary-color);
}

/* Selection Pool Base Styles */
.selection-pool {
    position: fixed;
    background-color: rgba(253, 245, 230, 0.95); /* OldLace with opacity */
    box-shadow: 0 0 15px rgba(0,0,0,0.1);
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 15px;
    z-index: 1000;
    box-sizing: border-box;
    transition: all 0.3s ease-in-out;
    overflow-y: auto; /* Allow scrolling if content overflows */
    font-size: clamp(1.2rem, 2vw, 2rem);
}

/* Position: Bottom (Default) */
body.pool-bottom .selection-pool {
    bottom: 0;
    left: 0;
    width: 100%;
    height: auto;
    max-height: 30vh;
    padding: 20px;
    border-top: 2px solid var(--secondary-color);
    flex-direction: row;
}

body.pool-bottom .container {
    padding-bottom: 140px; /* Default fallback */
}

/* When pool is visible and at bottom, fix game-actions above it */
body.pool-visible.pool-bottom .container {
    padding-bottom: calc(var(--pool-height, 0px) + 90px) !important;
}

body.pool-visible.pool-bottom .game-actions {
    position: fixed;
    bottom: calc(var(--pool-height, 0px) + 0px);
    left: 0;
    width: 100%;
    z-index: 999; /* Below pool (1000) but above content */
    background-color: rgba(253, 245, 230, 0.95);
    padding: 10px 0;
    margin-top: 0;
    box-shadow: 0 -2px 10px rgba(0,0,0,0.05);
}

/* Position: Left */
body.pool-left .selection-pool {
    top: 0;
    left: 0;
    width: auto; /* Allow auto width */
    min-width: 200px;
    max-width: 50vw; /* Prevent taking too much space */
    height: 100%;
    padding: 20px 10px;
    border-right: 2px solid var(--secondary-color);
    flex-direction: column; /* Stack vertically */
    align-content: flex-start; /* Align content to the left (top for column wrap) */
    justify-content: flex-start; /* Align items to top */
    padding-top: 80px; /* Avoid header overlap if needed, or just aesthetics */
}

body.pool-left .container {
    /* Margin will be handled by JS dynamically */
    /* margin-left: 200px; */ 
    width: auto; /* Let flex/block handle width, JS handles margin */
}

/* Position: Right */
body.pool-right .selection-pool {
    top: 0;
    right: 0;
    width: auto; /* Allow auto width */
    min-width: 200px;
    max-width: 50vw;
    height: 100%;
    padding: 20px 10px;
    border-left: 2px solid var(--secondary-color);
    flex-direction: column; /* Stack vertically */
    align-content: flex-start; /* Align content to the left */
    justify-content: flex-start; /* Align items to top */
    padding-top: 80px;
}

body.pool-right .container {
    /* Margin will be handled by JS dynamically */
    /* margin-right: 200px; */
    width: auto;
}

.selection-pool.hidden {
    display: none !important;
}

.pool-char {
    width: 2.5em;
    height: 2.5em;
    font-size: 1em;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--input-bg);
    border: 1px solid var(--secondary-color);
    border-radius: 5px;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    transition: all 0.2s;
    user-select: none;
    flex-shrink: 0; /* Prevent shrinking */
}

.pool-char:hover {
    transform: translateY(-5px);
    border-color: var(--primary-color);
    box-shadow: 0 5px 10px rgba(0,0,0,0.2);
}

.pool-char:active {
    transform: translateY(0);
}

.pool-char.used {
    opacity: 0.5;
    background-color: #e0e0e0;
    cursor: default;
    pointer-events: none;
    border-color: #ccc;
}

@media (max-width: 800px) {
    /* Force bottom layout on smaller screens */
    .selection-pool {
        bottom: 0 !important;
        left: 0 !important;
        right: auto !important;
        top: auto !important;
        width: 100% !important;
        height: auto !important;
        max-height: 30vh !important;
        padding: 10px !important;
        border-top: 2px solid var(--secondary-color) !important;
        border-left: none !important;
        border-right: none !important;
        flex-direction: row !important;
        
        font-size: 0.9rem; /* Smaller pool font */
    }
    
    .container {
        margin-left: 0 !important;
        margin-right: 0 !important;
        width: 100% !important;
    }

    body.pool-visible .container {
        padding-bottom: 35vh !important;
    }
    
    body.pool-visible .game-actions {
        bottom: 30vh !important;
    }
    
    h1 {
        font-size: 1.8em;
    }

    .poem-container {
        font-size: 1rem;
    }

    .selection-pool {
        gap: 8px;
    }
}

/* Default container styles (when pool is hidden or bottom) */
.container {
    transition: margin 0.3s, width 0.3s, padding 0.3s;
}

/* Modal Styles */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
    opacity: 1;
    transition: opacity 0.3s;
}

.modal.hidden {
    opacity: 0;
    pointer-events: none;
    display: none; /* Hide fully */
}

.hidden {
    display: none !important;
}

.modal-content {
    background-color: var(--bg-color);
    width: 90%;
    max-width: 600px;
    height: 80vh; /* Fixed height instead of max-height */
    border-radius: 10px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.3);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
    from { transform: translateY(-50px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.modal-header {
    padding: 15px 20px;
    background-color: var(--secondary-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: var(--primary-color);
}

.modal-header h2 {
    margin: 0;
    font-size: 1.5rem;
}

.close-modal {
    font-size: 2rem;
    cursor: pointer;
    line-height: 1;
}

.close-modal:hover {
    color: #fff;
}

.modal-search-container {
    padding: 10px 20px;
    background-color: #f0f0f0;
    border-bottom: 1px solid #ddd;
}

#poem-search {
    width: 100%;
    padding: 8px 12px;
    font-size: 1rem;
    border: 1px solid #ccc;
    border-radius: 5px;
    box-sizing: border-box; /* Ensure padding doesn't affect width */
    font-family: inherit;
}

#poem-search:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 5px rgba(139, 69, 19, 0.2);
}

.poem-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.poem-item {
    padding: 12px 15px;
    border-bottom: 1px solid #ddd;
    cursor: pointer;
    transition: background-color 0.2s;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.poem-item:last-child {
    border-bottom: none;
}

.poem-item:hover {
    background-color: rgba(139, 69, 19, 0.1);
}

.poem-item-title {
    font-weight: bold;
    font-size: 1.1em;
    color: var(--primary-color);
}

.poem-item-author {
    color: #666;
    font-size: 0.9em;
}

/* Sorting Mode Styles */
.sort-char {
    width: 2em;
    height: 2em;
    line-height: 2em;
    text-align: center;
    border: 1px solid var(--primary-color);
    border-radius: 4px;
    background-color: #fff;
    cursor: grab;
    margin: 0 2px;
    user-select: none;
    transition: transform 0.2s ease, box-shadow 0.2s ease, background-color 0.2s;
    box-sizing: border-box; /* Ensure border width changes don't affect layout size */
}

.sort-char:hover {
    transform: translateY(-2px);
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.sort-char:active {
    cursor: grabbing;
}

.sort-char.dragging {
    opacity: 0.5;
    transform: scale(1.1);
    box-shadow: 0 5px 15px rgba(0,0,0,0.2);
    z-index: 100;
}

.sort-char.over {
    /* Keep border width 1px to prevent layout shift, use style and color to differentiate */
    border: 1px dashed var(--primary-color);
    background-color: rgba(222, 184, 135, 0.3);
    box-shadow: inset 0 0 3px rgba(0,0,0,0.2);
}

.sort-char.selected {
    background-color: var(--secondary-color);
    color: #fff;
    transform: scale(1.1);
    box-shadow: 0 0 10px rgba(0,0,0,0.2);
    z-index: 10;
}

.sort-char.correct {
    background-color: var(--success-color);
    color: #fff;
    border-color: var(--success-color);
}

.sort-char.incorrect {
    background-color: var(--error-color);
    color: #fff;
    border-color: var(--error-color);
}

.sort-char.no-transition {
    transition: none !important;
}

/* Confirm Modal Styles */
.modal-content.small-modal {
    height: auto;
    max-width: 400px;
    min-height: auto;
}

.modal-body {
    padding: 25px 20px;
    font-size: 1.1em;
    color: var(--text-color);
    text-align: center;
    line-height: 1.5;
}

.modal-footer {
    padding: 15px 20px;
    display: flex;
    justify-content: center;
    gap: 20px;
    background-color: rgba(222, 184, 135, 0.2);
    border-top: 1px solid rgba(0,0,0,0.05);
}

/* --- Mobile & Responsive Improvements --- */

@media (max-width: 768px) {
    /* Layout Adjustments */
    body {
        padding: 10px;
    }

    .container {
        padding: 20px 15px;
        width: 100%;
        box-shadow: none;
        background-color: rgba(255, 255, 255, 0.95);
    }

    h1 {
        margin-bottom: 15px;
        line-height: 1.2;
    }

    /* Controls: Stack vertically or wrap nicely */
    .controls {
        flex-wrap: wrap;
        gap: 10px;
    }

    .controls select, 
    .controls button {
        flex: 1 1 auto; /* Grow to fill space */
        min-width: 45%; /* 2 per row approx */
        padding: 10px;
        font-size: 1rem;
    }

    /* Poem Display: Ensure characters are touch-friendly but fit */
    .poem-container {
        margin-bottom: 20px;
        width: 100%;
    }

    .poem-line {
        justify-content: center;
        width: 100%;
    }

    .char-box, .char-input {
        /* Slightly larger relative size for touch, but rely on base font scaling */
        width: 2.2em;
        height: 2.2em;
        line-height: 2.2em;
    }

    /* Game Actions: Touch friendly */
    .game-actions {
        padding: 10px;
        flex-wrap: wrap;
        gap: 10px;
    }

    .game-actions button {
        flex: 1 1 40%;
        padding: 12px 0;
    }
}

@media (max-width: 480px) {
    /* Small Mobile Specifics */
    .controls select, 
    .controls button {
        min-width: 100%; /* Stack fully on very small screens */
    }

    .game-actions button {
        flex: 1 1 100%; /* Stack actions on very small screens */
    }

    .poem-container {
        font-size: 1.1rem; /* Reset font size if clamp makes it too small */
    }
    
    .modal-content {
        width: 95%;
        height: 90vh;
    }

    /* Larger touch targets for selection pool on mobile */
    .pool-char {
        width: 3em; 
        height: 3em;
    }
}
