/* ========== 主題變數 ========== */
:root {
    --font-family-en: 'Noto Sans TC', sans-serif;
    --font-family-zh: 'Noto Sans TC', sans-serif;
    --primary-color: #000000;
    --primary-hover-color: #0056b3;
    --background-color: #f4f7f9;
    --card-bg-color: #ffffff;
    --text-color: #333333;
    --muted-text-color: #6c757d;
    --border-color: #e0e0e0;
    --header-bg: #ffffff;
    --header-text: #000000;
    --button-bg: #007bff;
    --button-text: #ffffff;
    --table-header-bg: #f2f2f2;
    --table-row-odd-bg: #ffffff;
    --table-row-even-bg: #f9f9f9;
    --total-row-bg: #e9ecef;
    --total-row-text: #212529;
}

/* 暗色主題 */
html.dark-theme {
    --primary-color: #0d6efd;
    --primary-hover-color: #3b8bff;
    --background-color: #121212;
    --card-bg-color: #1e1e1e;
    --text-color: #e0e0e0;
    --muted-text-color: #a0a0a0;
    --border-color: #444444;
    --header-bg: #1e1e1e;
    --header-text: #ffffff;
    --button-bg: #0d6efd;
    --button-text: #ffffff;
    --table-header-bg: #2c2c2c;
    --table-row-odd-bg: #1e1e1e;
    --table-row-even-bg: #2c2c2c;
    --total-row-bg: #343a40;
    --total-row-text: #ffffff;
}

/* 語系字型 */
html[lang="zh"],
html[lang="zh-Hant"] {
    --font-family-en: var(--font-family-zh);
}

/* ========== Reset & 全域樣式 ========== */
*,
*::before,
*::after {
    box-sizing: border-box;
}

body {
    font-family: var(--font-family-en);
    line-height: 1.6;
    margin: 0;
    background-color: var(--background-color);
    color: var(--text-color);
    transition: background-color 0.25s ease, color 0.25s ease;
}

.page-title {
    text-align: center;
    margin-top: 0px;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
}

/* ========== Navbar/Header ========== */
.navbar {
    width: 100%;
    background-color: var(--header-bg);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    transition: background-color 0.25s ease;
}

.navbar-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 56px;
    padding: 0 20px;
}

.navbar-left {
    display: flex;
    align-items: center;
    gap: 16px;
}

.menu-btn {
    background: none;
    border: none;
    color: var(--primary-color);
    font-size: 1.6em;
    cursor: pointer;
    transition: color 0.2s ease;
}

.menu-btn:hover {
    color: var(--primary-hover-color);
}

.navbar-title {
    font-size: 1.5em;
    font-weight: 700;
    color: var(--header-text);
    letter-spacing: .5px;
}

.navbar-title a {
    color: var(--header-text);
    text-decoration: none;
    cursor: pointer;
    font-weight: inherit;
}

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

.control-btn {
    background: none;
    border: none;
    color: var(--text-color);
    font-size: 1.1em;
    cursor: pointer;
    padding: 6px 8px;
    border-radius: 6px;
    transition: color 0.2s ease, background-color 0.2s ease;
    line-height: 1;
    white-space: nowrap;
}

.control-btn:hover {
    color: #fff;
    background-color: var(--primary-color);
}

.lang-switcher .control-btn.active {
    color: #fff;
    background-color: var(--primary-color);
}

/* ========== Navbar Menu ========== */
.navbar-menu {
    position: absolute;
    top: 56px;
    left: 0;
    width: 100%;
    background: var(--card-bg-color);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
    border-radius: 0 0 8px 8px;
    overflow: hidden;

    max-height: 0;
    opacity: 0;
    transform: translateY(-10px);
    display: flex;
    flex-direction: column;
    transition: max-height 0.3s ease, opacity 0.25s ease, transform 0.3s ease;
    z-index: 1100;
}

.navbar-menu.open {
    max-height: 500px;
    opacity: 1;
    transform: translateY(0);
}

.menu-link {
    display: block;
    padding: 12px 20px;
    color: var(--text-color);
    text-decoration: none;
    transition: background-color 0.2s ease, color 0.2s ease;
}

.menu-link:hover,
.menu-link.active {
    background: var(--primary-color);
    color: #fff;
}

/* 桌面 hover 展開 & 游標移出收起 */
@media (min-width: 769px) {
    .navbar:hover .navbar-menu {
        max-height: 500px;
        opacity: 1;
        transform: translateY(0);
    }

    .navbar:not(:hover) .navbar-menu {
        max-height: 0;
        opacity: 0;
        transform: translateY(-10px);
    }
}

/* 預設（桌面）隱藏 menu-controls */
.menu-controls {
    display: none;
}

/* ========== 手機版：控制鍵移入選單底部 ========== */
@media (max-width: 768px) {
    .navbar-controls {
        display: none;
    }

    .navbar-menu {
        position: fixed;
        top: 56px;
        left: 0;
        right: 0;
        border-radius: 0;
    }

    .navbar-menu.open {
        max-height: 100vh;
    }

    .navbar-menu .menu-controls {
        margin-top: auto;
        padding: 12px 20px;
        display: flex;
        flex-direction: column;
        gap: 10px;
        border-top: 1px solid var(--border-color);
    }

    .navbar-menu .menu-controls .control-btn {
        text-align: left;
        font-size: 1em;
        padding: 10px;
        border-radius: 6px;
    }

    .navbar-menu .menu-controls .lang-switcher {
        display: flex;
        gap: 10px;
    }
}

/* ========== 主要內容 ========== */
.main {
    padding: 76px 0 20px;
}

.instructions-text {
    white-space: pre-line;
    line-height: 1.6;
    margin: 0.5em 0;
    font-size: 1em;
    color: var(--text-color);
}

html.dark-theme .instructions-text {
    color: var(--text-color);
}

.card {
    background-color: var(--card-bg-color);
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
    padding: 22px;
    transition: background-color 0.25s ease, box-shadow 0.25s ease;
}

.card+.card {
    margin-top: 24px;
}

.card-title {
    margin: 0 0 10px 0;
    font-weight: 700;
    color: var(--header-text);
    font-size: 1.2em;
}

.card-title h2 {
    margin: 0;
    font-size: 1.2em;
}

/* ========== 表單 ========== */
.form-row {
    display: grid;
    grid-template-columns: repeat(4, minmax(160px, 1fr));
    gap: 16px 20px;
    align-items: end;
}

.form-group {
    min-width: 160px;
}

.form-group-inline {
    display: flex;
    align-items: center;
    gap: 10px;
}

.form-group-inline label {
    display: inline-flex;
    align-items: center;
    margin-bottom: 0;
}

input[type="checkbox"] {
    width: 18px;
    height: 18px;
    vertical-align: middle;
}

label {
    display: block;
    margin-bottom: 8px;
    font-size: .92em;
    color: var(--muted-text-color);
}

select,
input[type="number"],
input[type="text"] {
    width: 100%;
    height: 42px;
    padding: 10px 12px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background-color: var(--card-bg-color);
    color: var(--text-color);
    font-size: 1em;
    transition: border-color 0.2s ease, background-color 0.25s ease;
}

select:focus,
input[type="number"]:focus,
input[type="text"]:focus {
    outline: none;
    border-color: var(--primary-color);
}

/* 按鈕 */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 42px;
    padding: 0 12px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 1em;
    transition: background-color 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}

/* 讓 form-group 內的按鈕寬度與 select 一致 */
.form-group .btn {
    width: 100%;
}

.btn-primary {
    background-color: var(--button-bg);
    color: var(--button-text);
}

.btn-primary:hover {
    background-color: var(--primary-hover-color);
}

.btn-danger {
    background-color: #dc3545;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    padding: 5px 10px;
    font-size: 0.9em;
}

.btn-danger:hover {
    background-color: #c82333;
}

.remove-btn {
    padding: 5px 10px;
    height: 100%;
}

/* ========== 表格 ========== */
.table-responsive {
    overflow: auto;
    position: relative;
}

table {
    width: 100%;
    border-collapse: collapse;
    min-width: 760px;
}

.expand-cell {
    padding-left: 20px;
    text-align: left;
}

th,
td {
    padding: 12px 14px;
    border-bottom: 1px solid var(--border-color);
    text-align: center;
    line-height: 1.5;
    vertical-align: middle;
}

thead th {
    background-color: var(--table-header-bg);
    font-weight: 600;
    text-align: center;
}

tbody tr:nth-child(odd) {
    background-color: var(--table-row-odd-bg);
}

tbody tr:nth-child(even) {
    background-color: var(--table-row-even-bg);
}

tbody tr:hover {
    background-color: rgba(0, 123, 255, 0.06);
}

html.dark-theme tbody tr:hover {
    background-color: rgba(13, 110, 253, 0.12);
}

/* 可排序欄位圖示與縮排 */
th.sortable {
    cursor: pointer;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

th.sortable>span {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    line-height: 1.5;
}

th.sortable i {
    margin-left: 4px;
    font-size: 0.95em;
}

/* 指定欄寬下限（依實際資料調整） */
thead th#th-building {
    min-width: 150px;
}

thead th#th-currentLevel,
thead th#th-targetLevel {
    min-width: 50px;
}

thead th#th-upgradeTime {
    min-width: 180px;
}

thead th#th-requiredBread,
thead th#th-requiredWood,
thead th#th-requiredStone,
thead th#th-requiredIron,
thead th#th-requiredTruegold {
    min-width: 100px;
}

thead th#th-actions {
    min-width: 80px;
}

/* 右側動作列 sticky */
.table-responsive th:last-child,
.table-responsive td:last-child,
.table-responsive tfoot td:last-child {
    position: sticky;
    right: 0;
    z-index: 2;
    background: var(--table-header-bg);
}

/* 總計列 */
tfoot tr#total-row {
    background-color: var(--total-row-bg);
    color: var(--total-row-text);
    font-weight: 700;
}

tfoot tr#total-row td {
    border-bottom: none;
}

/* ========== 計算面板（時間 / 資源） ========== */
#calculation-panels {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 18px;
}

.panel {
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 18px;
    background-color: var(--card-bg-color);
}

.panel h3 {
    margin: 0 0 10px;
    font-size: 1.05em;
    font-weight: 700;
    color: var(--header-text);
}

.panel-body {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 24px;
    align-items: center;
}

.panel-inputs .form-group {
    margin-bottom: 12px;
}

.input-group {
    display: flex;
    align-items: center;
}

.input-group input {
    border-right: none;
    border-radius: 8px 0 0 8px;
}

.input-group-text {
    height: 42px;
    padding: 0 12px;
    background-color: var(--table-header-bg);
    border: 1px solid var(--border-color);
    border-left: none;
    border-radius: 0 8px 8px 0;
    line-height: 40px;
    color: var(--text-color);
}

.panel-result {
    text-align: right;
    border-left: 2px solid var(--primary-color);
    padding-left: 24px;
}

.panel-result h4 {
    margin: 0 0 8px;
    font-weight: 600;
    color: var(--muted-text-color);
    text-align: left;
}

#expected-time-result {
    font-size: 2em;
    font-weight: 800;
    margin: 0;
    color: var(--primary-color);
    text-align: left;
}

.resource-grid {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.resource-item {
    display: flex;
    justify-content: space-between;
    font-size: 1em;
}

.resource-item span:first-child {
    color: var(--muted-text-color);
}

.resource-item span:last-child {
    font-weight: 700;
    color: var(--primary-color);
}

/* ========== RWD ========== */
@media (max-width: 992px) {
    #calculation-panels {
        grid-template-columns: 1fr;
    }

    .panel-body {
        grid-template-columns: 1fr;
        gap: 18px;
    }
}

@media (max-width: 768px) {
    .navbar-container {
        flex-direction: column;
        align-items: stretch;
        height: auto;
        gap: 8px;
        padding: 10px 20px 12px;
    }

    .form-row {
        grid-template-columns: repeat(2, minmax(160px, 1fr));
    }

    thead th {
        font-size: .95em;
    }
}

@media (max-width: 576px) {
    .card {
        padding: 14px;
    }

    .form-row {
        grid-template-columns: 1fr;
    }

    th,
    td {
        padding: 8px 10px;
        font-size: .96em;
    }

    select,
    input[type="number"],
    input[type="text"],
    .btn,
    .btn-primary,
    .btn-danger {
        height: 42px;
        font-size: .95em;
        padding: 0 14px;
    }

    .remove-btn {
        padding: 5px 10px;
        height: 100%;
    }

    #expected-time-result {
        font-size: 1.6em;
    }
}

.text-right {
    text-align: right;
}

.text-center {
    text-align: center;
}

.btn-circle {
    height: 100%;
    aspect-ratio: 1 / 1;
    border-radius: 50%;
    align-items: center;
    justify-content: center;
}

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

/* 進站時整個 body 隱藏 */
html.init-loading body {
    visibility: hidden;
}