body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    margin: 0;
    background-color: #f0f2f5;
    color: #333;
    line-height: 1.6;
}

.container {
    display: flex;
    height: 100vh;
    padding: 20px;
    box-sizing: border-box;
    gap: 20px;
}

.left-panel, .right-panel {
    background-color: #fff;
    padding: 25px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    overflow-y: auto; /* 改為 auto，讓內容決定是否滾動 */
    display: flex; /* 添加 flex 以便內部元素可以控制大小 */
    flex-direction: column; /* 縱向排列 */
}

.left-panel {
    width: 35%;
    /* flex-grow: 0; flex-shrink: 0; -- 固定寬度 */
}

.right-panel {
    width: 65%;
    /* flex-grow: 1; -- 佔據剩餘寬度 */
}

.header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 2px solid #e0e0e0;
    margin-bottom: 20px;
    padding-bottom: 10px;
}

h1 {
    font-size: 1.8em;
    color: #1a73e8;
    margin: 0;
    padding: 0;
    border-bottom: none;
}

.lang-selector-wrap {
    display: flex;
    align-items: center;
}

.lang-selector-wrap select {
    padding: 6px 12px;
    border: 1px solid #dadce0;
    border-radius: 6px;
    background-color: #fff;
    color: #3c4043;
    font-size: 0.9em;
    font-weight: 500;
    cursor: pointer;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.lang-selector-wrap select:focus {
    border-color: #1a73e8;
    box-shadow: 0 0 0 2px rgba(26, 115, 232, 0.2);
}

h2 { /* 試卷預覽標題 */
    font-size: 1.5em;
    color: #3c4043;
    margin-top: 20px; /* 與思考區塊間隔 */
    margin-bottom: 15px;
    flex-shrink: 0; /* 標題不縮小 */
}

.control-group {
    margin-bottom: 20px;
}

label {
    display: block;
    font-weight: 600;
    margin-bottom: 8px;
    color: #5f6368;
}

input[type="file"],
textarea {
    width: 100%;
    padding: 10px;
    border: 1px solid #dadce0;
    border-radius: 4px;
    box-sizing: border-box;
    font-size: 0.95em;
}

input[type="file"] {
    padding: 8px;
}

textarea {
    min-height: 100px;
    resize: vertical;
}

button#generateBtn {
    background-color: #1a73e8;
    color: white;
    border: none;
    padding: 12px 20px;
    font-size: 1em;
    font-weight: 600;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s ease-in-out;
    width: 100%;
    margin-top: 10px;
}

button#generateBtn:hover {
    background-color: #1765cc;
}

button#generateBtn:disabled {
    background-color: #ccc;
    cursor: not-allowed;
}


#statusArea {
    margin-top: 20px;
    padding: 10px;
    background-color: #e8f0fe;
    border: 1px solid #d2e3fc;
    border-radius: 4px;
    font-size: 0.9em;
    color: #3c4043;
    flex-shrink: 0; /* 狀態區不縮小 */
}

/* 思考過程區域樣式 */
.collapsible-think-area {
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    margin-bottom: 20px;
    background-color: #fafbfc;
    flex-shrink: 0; /* 思考區塊整體不縮小，但內部內容可滾動 */
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
    overflow: hidden;
}

.collapsible-think-area summary {
    padding: 12px 16px;
    font-weight: 600;
    cursor: pointer;
    background-color: #f1f3f4;
    border-bottom: 1px solid #e0e0e0;
    border-radius: 8px 8px 0 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    list-style: none; /* 去除原生箭頭，使用乾淨的 flex 佈局 */
}

.collapsible-think-area summary::-webkit-details-marker {
    display: none;
}

.think-summary-title {
    display: flex;
    align-items: center;
    gap: 6px;
    color: #202124;
}

.think-summary-title::before {
    content: "▶";
    font-size: 0.75em;
    color: #5f6368;
    transition: transform 0.2s ease;
    margin-right: 4px;
}

.collapsible-think-area[open] .think-summary-title::before {
    transform: rotate(90deg);
}

.think-badge {
    display: inline-flex;
    align-items: center;
    font-size: 0.85em;
    font-weight: 500;
    color: #1a73e8;
    background-color: #e8f0fe;
    padding: 3px 10px;
    border-radius: 12px;
    border: 1px solid #d2e3fc;
}

/* 轉圈圈旋轉動畫 */
@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.inline-spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid #d2e3fc;
    border-top-color: #1a73e8;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    vertical-align: middle;
    margin-right: 6px;
    box-sizing: border-box;
}

.status-spinner {
    display: inline-block;
    width: 13px;
    height: 13px;
    border: 2px solid rgba(26, 115, 232, 0.3);
    border-top-color: #1a73e8;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    vertical-align: middle;
    margin-right: 8px;
    box-sizing: border-box;
}

.stream-thinking-indicator {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    margin-top: 8px;
    font-size: 0.88em;
    color: #1a73e8;
    background-color: #e8f0fe;
    border-radius: 6px;
    font-family: inherit;
    animation: pulseThinking 2s ease-in-out infinite;
}

@keyframes pulseThinking {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.65; }
}


#examOutput {
    /* flex-grow: 1; -- 讓它填滿剩餘空間 */
    border: 1px solid #dadce0;
    border-radius: 4px;
    padding: 20px;
    overflow-y: auto;
    background-color: #f8f9fa;
    font-size: 0.95em;
    min-height: 200px; /* 確保有個最小高度 */
    display: flex; /* Forcing it to be a flex item if its parent is flex */
    flex-direction: column; /* Its children will stack vertically */
    flex-grow: 1; /* This will make it take up remaining space */
}


/* 通用 Markdown 樣式 (也適用於思考過程) */
.markdown-body h1, .markdown-body h2, .markdown-body h3 {
    margin-top: 1.2em;
    margin-bottom: 0.6em;
    font-weight: 600;
}
.markdown-body h1 { font-size: 1.6em; }
.markdown-body h2 { font-size: 1.4em; }
.markdown-body h3 { font-size: 1.2em; }
.markdown-body p { margin-bottom: 0.8em; }
.markdown-body ul, .markdown-body ol { margin-bottom: 0.8em; padding-left: 2em; }
.markdown-body li { margin-bottom: 0.3em; }
.markdown-body pre {
    background-color: #f0f0f0; /* Code block background */
    padding: 10px;
    border-radius: 4px;
    overflow-x: auto;
    margin-bottom: 1em;
    border: 1px solid #ddd; /* Add border to code blocks */
}
.markdown-body code { /* Inline code */
    font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
    background-color: #e8e8e8; /* Slightly different for inline code */
    padding: 0.2em 0.4em;
    border-radius: 3px;
    font-size: 0.9em;
}
.markdown-body pre code { /* Code inside pre, reset some styles */
    background-color: transparent;
    padding: 0;
    border: none;
    font-size: 1em; /* Reset to pre's font size */
}
.markdown-body blockquote {
    border-left: 3px solid #ccc;
    padding-left: 1em;
    margin-left: 0;
    color: #555;
}
.markdown-body table {
    border-collapse: collapse;
    width: auto;
    margin-bottom: 1em;
}
.markdown-body th, .markdown-body td {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: left;
}
.markdown-body th {
    background-color: #f2f2f2;
}

.stream-preview {
    border: 1px dashed #ccc; /* 給臨時預覽區一個不同的邊框以便區分 */
    padding: 15px;
    white-space: pre-wrap; /* 保持換行和空格，類似 <pre> */
    font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace; /* 使用等寬字體 */
    font-size: 0.9em;
    line-height: 1.5;
    max-height: 400px; /* 也可以給臨時區一個最大高度 */
    overflow-y: auto;
    background-color: #fdfdfd;
}

.export-controls {
    margin-bottom: 10px; 
    text-align: right; 
}

#exportHtmlBtn {
    background-color: #28a745; 
    color: white;
    border: none;
    padding: 8px 15px;
    font-size: 0.9em;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s ease-in-out;
}

#exportHtmlBtn:hover:not(:disabled) {
    background-color: #218838;
}

#exportHtmlBtn:disabled {
    background-color: #ccc;
    cursor: not-allowed;
    opacity: 0.7;
}

/* 如果你還沒有為 .stream-preview 添加樣式，也一併加入 */
.stream-preview {
    border: 1px dashed #ccc;
    padding: 15px;
    white-space: pre-wrap;
    font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
    font-size: 0.9em;
    line-height: 1.5;
    max-height: 300px;
    overflow-y: auto;
    background-color: #fdfdfd;
    margin-bottom: 10px;
}

/* 需要持續顯示的提醒（例如教材超過上下文上限而被截斷） */
.notice-area {
    margin: 0 0 12px 0;
    padding: 10px 14px;
    border: 1px solid #f0c36d;
    border-left: 4px solid #e6a817;
    border-radius: 4px;
    background-color: #fdf6e3;
    color: #6b4e00;
    font-size: 0.9em;
    line-height: 1.6;
}

/* --- 教材選取區 --- */
.material-header {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
}
.material-actions { white-space: nowrap; }
.link-btn {
    background: none;
    border: none;
    padding: 0 4px;
    color: #1a73e8;
    cursor: pointer;
    font-size: 0.85em;
    text-decoration: underline;
    width: auto;
}
.link-btn:hover { color: #0b57d0; }

.material-summary {
    margin: 6px 0 8px 0;
    font-size: 0.85em;
    color: #5f6368;
}
.material-summary.over-limit { color: #b3261e; font-weight: 600; }

.page-list {
    max-height: 320px;
    overflow-y: auto;
    border: 1px solid #dadce0;
    border-radius: 4px;
    background-color: #fff;
}
.page-item { border-bottom: 1px solid #eceff1; }
.page-item:last-child { border-bottom: none; }
.page-item.unselected { background-color: #f8f9fa; }

.page-item-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 10px;
    font-size: 0.88em;
}
.page-item-header input[type="checkbox"] { flex: none; margin: 0; width: auto; }
.page-item-label { flex: 1; cursor: pointer; }
.page-item.unselected .page-item-label { color: #80868b; }
.page-meta { color: #5f6368; font-size: 0.9em; }
.page-badge {
    flex: none;
    padding: 1px 6px;
    border-radius: 9px;
    background-color: #e8f0fe;
    color: #1a56c4;
    font-size: 0.78em;
}
.page-badge.warn { background-color: #fce8e6; color: #b3261e; }
.page-toggle {
    flex: none;
    background: none;
    border: none;
    color: #1a73e8;
    cursor: pointer;
    font-size: 0.85em;
    padding: 0;
    width: auto;
    text-decoration: underline;
}
.page-text {
    width: 100%;
    box-sizing: border-box;
    margin: 0;
    padding: 8px 10px;
    border: none;
    border-top: 1px solid #eceff1;
    font-family: inherit;
    font-size: 0.85em;
    line-height: 1.5;
    resize: vertical;
    background-color: #fbfcfd;
}
.page-text:focus { outline: 2px solid #a8c7fa; outline-offset: -2px; }

/* --- Responsive and touch adaptation --- */
html {
    -webkit-text-size-adjust: 100%;
}

*, *::before, *::after {
    box-sizing: border-box;
}

button, select, input, textarea {
    font: inherit;
}

@media (max-width: 920px) {
    body {
        background-color: #f5f7fb;
    }

    .container {
        display: block;
        height: auto;
        min-height: 100dvh;
        padding: clamp(10px, 3vw, 20px);
    }

    .left-panel,
    .right-panel {
        width: 100%;
        overflow: visible;
        padding: clamp(16px, 4vw, 24px);
        border-radius: 12px;
    }

    .left-panel {
        margin-bottom: 14px;
    }

    #examOutput {
        min-height: 280px;
        overflow: visible;
    }

    .page-list {
        max-height: min(48dvh, 420px);
        overscroll-behavior: contain;
    }
}

@media (max-width: 600px) {
    body {
        font-size: 16px;
        line-height: 1.55;
    }

    .container {
        padding: 0;
    }

    .left-panel,
    .right-panel {
        border-radius: 0;
        box-shadow: none;
        padding: 18px 16px;
    }

    .left-panel {
        margin-bottom: 8px;
        border-bottom: 8px solid #f0f2f5;
    }

    .header-row {
        align-items: flex-start;
        gap: 12px;
        margin-bottom: 18px;
    }

    h1 {
        max-width: 70%;
        font-size: clamp(1.35rem, 6vw, 1.65rem);
        line-height: 1.2;
    }

    h2 {
        margin-top: 16px;
        font-size: 1.3rem;
    }

    .lang-selector-wrap select {
        min-height: 44px;
        max-width: 132px;
        padding: 8px 32px 8px 10px;
        font-size: 1rem;
    }

    .control-group {
        margin-bottom: 18px;
    }

    input[type="file"],
    textarea {
        min-height: 44px;
        padding: 12px;
        font-size: 16px;
    }

    input[type="file"] {
        max-width: 100%;
        overflow: hidden;
    }

    textarea {
        min-height: 132px;
    }

    button#generateBtn,
    #exportHtmlBtn {
        min-height: 48px;
        padding: 12px 16px;
        font-size: 1rem;
    }

    #statusArea {
        margin-top: 14px;
        padding: 12px;
    }

    .collapsible-think-area summary {
        min-height: 48px;
        padding: 12px;
        gap: 8px;
    }

    .think-summary-title {
        min-width: 0;
    }

    .think-badge {
        flex: none;
        max-width: 45%;
    }

    .export-controls {
        text-align: left;
    }

    #exportHtmlBtn {
        width: 100%;
    }

    #examOutput {
        min-height: 52dvh;
        padding: 15px;
        overflow-wrap: anywhere;
    }

    .markdown-body img {
        max-width: 100%;
        height: auto;
    }

    .markdown-body table {
        display: block;
        width: 100%;
        max-width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .markdown-body th,
    .markdown-body td {
        min-width: 7rem;
        padding: 8px 10px;
    }

    .markdown-body ul,
    .markdown-body ol {
        padding-left: 1.4em;
    }

    .material-header {
        align-items: flex-start;
    }

    .material-actions {
        display: flex;
        gap: 4px;
    }

    .link-btn,
    .page-toggle {
        min-width: 44px;
        min-height: 44px;
        padding: 8px;
        text-decoration-thickness: 1px;
        text-underline-offset: 3px;
    }

    .page-item-header {
        align-items: flex-start;
        min-height: 52px;
        padding: 6px 8px;
        gap: 6px;
    }

    .page-item-header input[type="checkbox"] {
        width: 22px;
        height: 22px;
        margin-top: 9px;
    }

    .page-item-label {
        min-width: 0;
        padding: 10px 2px;
        line-height: 1.35;
    }

    .page-meta {
        display: block;
        margin-top: 2px;
    }

    .page-badge {
        margin-top: 10px;
    }

    .page-text {
        min-height: 180px;
        font-size: 16px;
    }
}

@media (max-width: 370px) {
    .header-row {
        flex-direction: column;
    }

    h1,
    .lang-selector-wrap,
    .lang-selector-wrap select {
        width: 100%;
        max-width: none;
    }

    .material-header {
        display: block;
    }

    .material-actions {
        margin-top: 4px;
    }
}

@media (orientation: landscape) and (max-height: 520px) and (max-width: 920px) {
    #examOutput {
        min-height: 70dvh;
    }

    .page-list {
        max-height: 70dvh;
    }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        scroll-behavior: auto !important;
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
