:root {
    --app-bg: #dde2e8;
    --panel-bg: #f4f6f8;
    --toolbar-bg: #c7cfd9;
    --line-color: #9198a1;
    --text-main: #202124;
    --text-sub: #5f6368;
    --accent: #1a73e8;
    --danger: #d93025;
    --success: #137333;
    --doc-font-size: 16px;
    --doc-line-height: 1.8;
    --doc-margin-y: 5%;
    --doc-margin-x: 10%;
    --doc-bg: #f8f9fa;
    --doc-text: #202124;
    --doc-translated-default-color: #5f6368;
    --doc-translated-color: var(--doc-translated-default-color);
    --doc-translated-bg: transparent;
    --doc-translated-padding: 0;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    padding: 0;
    height: 100vh; /* 強制使用 100vh，避免 100% 計算錯誤 */
    height: 100dvh; /* 支援現代手機動態高度 */
    width: 100vw; /* 新增此行 */
    overflow: hidden !important; /* 絕對禁止外層出現任何滾動條 */
    touch-action: none; /* 絕對禁止外層的任何觸控縮放或橡皮筋滑動 */
    //overscroll-behavior-y: none;
    font-family: "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: linear-gradient(180deg, #eff2f6 0%, #d9e0e8 100%);
    color: var(--text-main);
}

button,
input,
select,
textarea {
    font: inherit;
}

.hidden {
    display: none !important;
}

.app-shell {
    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    touch-action: auto; /* 將正常的滑動權限還給內層容器 */
}

.toolbar {
    display: flex;
    align-items: center;
    gap: 6px;
    min-height: 48px;
    padding: 6px;
    background: var(--toolbar-bg);
    border-bottom: 1px solid var(--line-color);
}

.icon-btn,
.toolbar-btn,
.secondary-btn,
.hero-btn,
.settings-mode-toggle,
.route-menu button,
.doc-actions-menu button,
.mini-button-group button,
.theme-grid button,
.stepper button,
.selection-action-bar button {
    border: 1px solid #7c8794;
    background: #eef1f5;
    color: var(--text-main);
    border-radius: 0;
}

.icon-btn {
    width: 36px;
    height: 36px;
    font-size: 18px;
    flex: 0 0 auto;
}

.toolbar-btn,
.secondary-btn,
.hero-btn,
.settings-mode-toggle,
.route-menu button {
    min-height: 36px;
    padding: 0 12px;
}

.primary-btn {
    background: #d7e5fb;
    border-color: #4d84d8;
}

.grow-btn {
    flex: 1 1 auto;
}

#search_input {
    flex: 1 1 auto;
    min-width: 0;
    height: 36px;
    padding: 0 10px;
    border: 1px solid #7c8794;
    border-radius: 0;
    background: #f7f8fa;
}

#append_select {
    width: 90px;
    height: 36px;
    border: 1px solid #7c8794;
    border-radius: 0;
    background: #f7f8fa;
}

.main-stage {
    position: relative;
    flex: 1 1 auto;
    overflow: hidden;
}

.route-view {
    position: absolute;
    inset: 0;
    overflow: hidden;
}

#lookup-view {
    display: flex;
    flex-direction: column;
    background: #f3f4f6;
}

#tabbar {
    height: 40px;
    min-height: 40px;
    display: flex;
    overflow-x: auto;
    background: #d6dbe2;
    border-bottom: 2px solid #9aa4ae;
    -webkit-overflow-scrolling: touch;
}

#tabbar::-webkit-scrollbar {
    display: none;
}

.tab {
    flex-shrink: 0;
    padding: 0 16px;
    line-height: 40px;
    font-size: 14px;
    color: #333;
    border-right: 1px solid #b0b8c0;
    white-space: nowrap;
    cursor: pointer;
}

.tab.active {
    background: #f6f7f9;
    font-weight: 700;
    border-bottom: 3px solid #000;
}

#lookup-stage {
    position: relative;
    flex: 1 1 auto;
}

#lookup-stage iframe,
#ai_result_container {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    border: 0;
    background: #fff;
}

#ai_result_container {
    overflow-y: auto;
    overflow-x: hidden; /* 新增：絕對禁止橫向撐開 */
    padding: 16px;
}

.ai-result-content {
    min-height: 100%;
    width: 100%; /* 新增：確保寬度不超過父容器 */
    font-size: 14px;
    line-height: 1.6;
    color: #202124;
    word-wrap: break-word; /* 新增：允許長字串換行 */
    overflow-wrap: break-word; /* 新增：現代標準的換行屬性 */
}

.empty-note,
.lookup-status {
    margin-top: 48px;
    text-align: center;
    color: #9aa0a6;
}

.lookup-thinking {
    color: #1a73e8;
    font-weight: 700;
}

.lookup-error {
    color: #d93025;
    font-weight: 700;
}

.lookup-text-result {
    padding: 10px;
    color: #333;
}

.faw-title {
    display: block;
    margin-bottom: 8px;
    color: #1a73e8;
    font-size: 18px;
    font-weight: 700;
}

.faw-subtitle {
    display: block;
    margin-top: 12px;
    margin-bottom: 4px;
    color: #5f6368;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

.faw-tag {
    display: inline-block;
    margin-right: 4px;
    padding: 2px 8px;
    border-radius: 12px;
    background: #e8f0fe;
    color: #1a73e8;
    font-size: 11px;
    font-weight: 500;
    vertical-align: middle;
}

.faw-example {
    margin: 8px 0;
    padding: 8px 12px;
    border-left: 3px solid #dadce0;
    border-radius: 0 4px 4px 0;
    background: #f8f9fa;
    color: #444;
    font-size: 13px;
    font-style: italic;
}

#doc-view {
    background: linear-gradient(180deg, #f1f4f7 0%, #e2e7ed 100%);
}

.doc-empty-state {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 14px;
    padding: 24px 20px;
}

.doc-empty-state h1 {
    margin: 0;
    font-size: 28px;
    letter-spacing: 0.02em;
}

.doc-empty-state p {
    margin: 0;
    color: var(--text-sub);
    line-height: 1.6;
}

.hero-btn {
    background: #d7e5fb;
    border-color: #5e89d1;
}

.doc-content {
    height: 100%;
    width: 100%; /* 確保不會超過外層的 route-view */
    overflow-y: auto;
    overflow-x: hidden; /* 絕對封殺橫向溢出 */
    padding: var(--doc-margin-y) var(--doc-margin-x);
    background: var(--doc-bg);
    color: var(--doc-text);
    font-size: var(--doc-font-size);
    line-height: var(--doc-line-height);
    box-sizing: border-box; /* 確保 padding 包含在 width 內 */
}

.doc-content.theme-light {
    --doc-bg: #f8f9fa;
    --doc-text: #202124;
    --doc-translated-default-color: #5f6368;
}

.doc-content.font-serif {
    font-family: Georgia, "Times New Roman", serif;
}

.doc-content.font-mono {
    font-family: Consolas, "Courier New", monospace;
}

.doc-content.theme-dark {
    --doc-bg: #202124;
    --doc-text: #f1f3f4;
    --doc-translated-default-color: #b7c4d8;
}

.doc-content.theme-sepia {
    --doc-bg: #f4ecd8;
    --doc-text: #5b4636;
    --doc-translated-default-color: #5d6773;
}

.doc-chunk {
    margin-bottom: 1.1em;
}

.doc-summary-view {
    display: none;
}

.doc-content.summary-mode .doc-summary-view {
    display: block;
}

.doc-content.summary-mode .doc-chunk {
    display: none;
}

.doc-summary-status {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: baseline;
    margin-bottom: 1em;
    color: var(--doc-translated-color, var(--text-sub));
}

.doc-summary-status strong {
    color: var(--accent);
}

.doc-summary-body > *:first-child {
    margin-top: 0;
}

.doc-summary-body > *:last-child {
    margin-bottom: 0;
}

.doc-plain-chunk {
    margin-bottom: 0;
}

.doc-chunk.pending .doc-original {
    opacity: 0.72;
}

.doc-page {
    margin-bottom: 2em;
    padding-bottom: 2em;
}

.doc-page:not(:last-child) {
    border-bottom: 1px dashed rgba(128, 128, 128, 0.3);
}

.doc-original,
.doc-translated,
.doc-plain-text {
    /* 遇到超長字串、無空格網址或連續符號時，強制在邊界處截斷換行 */
    word-break: break-all;
    overflow-wrap: anywhere; 
    max-width: 100%;
}

.doc-original > *:first-child,
.doc-translated > *:first-child {
    margin-top: 0;
}

.doc-original > *:last-child,
.doc-translated > *:last-child {
    margin-bottom: 0;
}

.doc-original h1,
.doc-original h2,
.doc-original h3,
.doc-original h4,
.doc-original h5,
.doc-original h6,
.doc-translated h1,
.doc-translated h2,
.doc-translated h3,
.doc-translated h4,
.doc-translated h5,
.doc-translated h6 {
    margin-top: 1.5em;
    margin-bottom: 0.5em;
    line-height: 1.2;
}

.doc-original p,
.doc-translated p {
    margin-top: 0;
    margin-bottom: 1em;
}

.doc-original table,
.doc-translated table {
    width: 100%;
    margin-bottom: 1em;
    border-collapse: collapse;
}

.doc-original th,
.doc-original td,
.doc-translated th,
.doc-translated td {
    padding: 8px;
    border: 1px solid currentColor;
}

.doc-original pre,
.doc-translated pre {
    overflow-x: auto;
    padding: 1em;
    border-radius: 4px;
    background: rgba(128, 128, 128, 0.1);
}

.doc-original blockquote,
.doc-translated blockquote {
    margin-left: 0;
    padding-left: 1em;
    border-left: 4px solid rgba(128, 128, 128, 0.3);
    opacity: 0.85;
}

.doc-plain-text {
    margin: 0;
    white-space: pre-wrap;
    word-break: break-word;
    tab-size: 4;
    font: inherit;
}

.doc-plain-chunk .doc-original pre,
.doc-plain-chunk .doc-translated pre {
    overflow-x: visible;
    padding: 0;
    border-radius: 0;
    background: transparent;
}

.doc-plain-chunk + .doc-plain-chunk {
    margin-top: 0;
}

.doc-translated {
    display: none;
    color: var(--doc-translated-color, inherit);
}

.doc-translated > * {
    background: var(--doc-translated-bg);
    padding: var(--doc-translated-padding, 0);
    border-radius: 4px;
    box-decoration-break: clone;
    -webkit-box-decoration-break: clone;
}

.doc-chunk.has-translation .doc-translated {
    display: block;
    margin-top: 10px;
}

.doc-chunk[data-view-mode="original"] .doc-translated {
    display: none;
}

.doc-chunk[data-view-mode="translated"] .doc-original {
    display: none;
}

.doc-chunk[data-view-mode="translated"]:not(.has-translation) .doc-original {
    display: block;
}

.settings-view {
    background: #eef2f6;
}

.settings-topbar {
    display: flex;
    gap: 8px;
    padding: 10px;
    border-bottom: 1px solid #b5bdc7;
    background: #c7cfd9;
}

.settings-done-btn {
    flex: 0 0 auto;
    min-width: 72px;
}

.settings-mode-toggle {
    flex: 1 1 auto;
    min-height: 44px;
    font-weight: 700;
}

.settings-scroll {
    height: calc(100% - 65px);
    overflow-y: auto;
    padding: 12px;
}

.settings-card {
    margin-bottom: 14px;
    padding: 12px;
    background: #f8fafc;
    border: 1px solid #a6b0bb;
}

.settings-row,
.control-row,
.translated-theme-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

.control-row,
.translated-theme-row {
    margin-top: 12px;
}

.status-pill {
    padding: 4px 8px;
    border: 1px solid currentColor;
    font-size: 12px;
    font-weight: 700;
}

.status-pill.offline {
    color: var(--danger);
}

.status-pill.connecting {
    color: #b06000;
}

.status-pill.online {
    color: var(--success);
}

.settings-actions {
    display: flex;
    gap: 8px;
    margin-top: 10px;
}

.settings-actions .secondary-btn {
    flex: 1 1 auto;
}

.helper-line {
    margin-top: 10px;
    color: var(--text-sub);
    font-size: 12px;
}

.scanner-box {
    margin-top: 10px;
    padding: 6px;
    border: 1px solid #a6b0bb;
    background: #fff;
}

.radio-grid {
    display: grid;
    gap: 10px;
    margin-top: 12px;
}

.radio-grid label,
.translated-radio-group label {
    font-size: 13px;
}

.other-lang-option {
    display: flex;
    align-items: center;
    gap: 8px;
}

.other-lang-option input[type="text"] {
    flex: 1 1 auto;
    min-width: 0;
}

.mini-button-group,
.theme-grid,
.translated-radio-group {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

.mini-button-group button,
.theme-grid button {
    min-height: 30px;
    padding: 0 10px;
}

.mini-button-group button.active,
.theme-grid button.active {
    background: #d7e5fb;
    border-color: #4d84d8;
}

.stepper {
    display: flex;
    align-items: center;
    gap: 10px;
}

.stepper button {
    width: 28px;
    height: 28px;
}

.stepper span {
    min-width: 44px;
    text-align: center;
    font-weight: 700;
}

.color-grid {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 12px;
}

.color-grid > div {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

.translated-color-option {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.translated-color-option input[type="color"] {
    width: 36px;
    height: 28px;
    padding: 0;
}

.settings-list {
    display: grid;
    gap: 8px;
    margin-top: 10px;
}

.settings-item {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    padding: 8px;
    border: 1px solid #b5bdc7;
    background: #fff;
}

.settings-item strong {
    display: block;
    font-size: 14px;
}

.settings-item small {
    display: block;
    margin-top: 4px;
    color: var(--text-sub);
    word-break: break-word;
}

.item-actions {
    display: flex;
    gap: 6px;
    align-items: flex-start;
}

.item-actions button {
    min-height: 28px;
    padding: 0 8px;
    border: 1px solid #7c8794;
    background: #eef1f5;
}

.add-form {
    display: grid;
    gap: 8px;
    margin-top: 12px;
}

.add-form input,
.other-lang-option input[type="text"] {
    min-height: 36px;
    padding: 0 10px;
    border: 1px solid #7c8794;
    background: #fff;
}

.route-menu {
    position: fixed;
    top: 54px;
    left: 8px;
    z-index: 50;
    display: grid;
    gap: 4px;
    width: 156px;
    padding: 6px;
    border: 1px solid #7c8794;
    background: #f7f8fa;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
}

.route-menu button {
    text-align: left;
    background: #eceff3;
}

.doc-actions-menu {
    position: fixed;
    top: 54px;
    right: 8px;
    z-index: 50;
    display: grid;
    gap: 4px;
    width: 156px;
    padding: 6px;
    border: 1px solid #7c8794;
    background: #f7f8fa;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
}

.doc-actions-menu button {
    min-height: 36px;
    padding: 0 12px;
    text-align: left;
    background: #eceff3;
}

.selection-action-bar {
    position: fixed;
    left: 50%;
    bottom: 18px;
    z-index: 70;
    display: flex;
    gap: 10px;
    padding: 10px;
    transform: translateX(-50%);
    border: 1px solid #7c8794;
    background: rgba(255, 255, 255, 0.96);
    box-shadow: 0 10px 24px rgba(0, 0, 0, 0.18);
}

.selection-action-bar button {
    min-height: 34px;
    min-width: 92px;
    padding: 0 14px;
}

.toast {
    position: fixed;
    left: 50%;
    bottom: 72px;
    z-index: 80;
    max-width: calc(100vw - 32px);
    padding: 10px 14px;
    transform: translateX(-50%);
    border: 1px solid rgba(255, 255, 255, 0.15);
    background: rgba(32, 33, 36, 0.9);
    color: #fff;
    font-size: 13px;
    text-align: center;
}

/* 自訂調色盤樣式 */
.swatch-group {
    display: flex;
    align-items: center;
    gap: 6px;
}

.swatch {
    width: 24px;
    height: 24px;
    border-radius: 4px;
    border: 1px solid #a6b0bb;
    padding: 0;
    cursor: pointer;
    transition: transform 0.1s;
}

.swatch.active {
    border: 2px solid #202124;
    transform: scale(1.15);
}

.hex-input {
    width: 68px;
    height: 26px;
    padding: 0 4px;
    font-family: monospace;
    font-size: 13px;
    text-align: center;
    border: 1px solid #7c8794;
    text-transform: lowercase;
}

#doc-file-input {
    display: none;
}

@media (max-width: 480px) {
    .toolbar-btn {
        padding: 0 10px;
        font-size: 13px;
    }

    .settings-actions {
        flex-wrap: wrap;
    }

    .settings-actions .secondary-btn {
        flex: 1 1 calc(50% - 8px);
    }

    .translated-theme-row {
        align-items: flex-start;
        flex-direction: column;
    }

    .selection-action-bar {
        width: calc(100vw - 24px);
        justify-content: center;
    }

    .selection-action-bar button {
        flex: 1 1 auto;
    }
}
