:root {
    --card-width: 150px;
    --card-height: 120px;
    --grid-gap: 15px;
    --bg-color: #f4f7f9;
    --card-bg: #ffffff;
    --primary-color: #007bff;
    --on-color: #28a745; /* 綠色 */
    --off-color: #6c757d; /* 灰色 */
    --placeholder-color: #999; /* 佔位符或連線失敗的灰色 */
    --warning-color: #ffc107;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: var(--bg-color);
    margin: 0;
    padding: 20px 0;
    display: flex;
    flex-direction: column;
    align-items: center; /* 讓所有內容居中 */
    color: #333;
}

h2 {
    color: var(--primary-color);
    text-align: center;
    margin-bottom: 10px;
}

/* 頂部控制區 */
.controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 20px;
    border-radius: 8px;
    background-color: var(--card-bg);
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
    margin-bottom: 20px;
    width: 90%; 
    max-width: calc(var(--card-width) * 4 + var(--grid-gap) * 3); /* 確保與 Grid 寬度匹配 */
}

/* 狀態顏色 */
#mqtt-status {
    font-weight: bold;
    color: var(--placeholder-color); 
    transition: color 0.3s;
}
#mqtt-status.connected {
    color: var(--on-color); 
}
#mqtt-status.error {
    color: #dc3545; 
}

/* 編輯模式按鈕樣式 */
.edit-button {
    background-color: var(--primary-color);
    color: white;
    border: none;
    padding: 8px 15px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 14px;
    transition: background-color 0.2s, transform 0.3s;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.edit-button.small-button {
    padding: 5px 10px;
    font-size: 13px;
    float: right;
    margin-top: 10px;
}

.edit-button .material-icons {
    font-size: 20px;
    transition: transform 0.3s;
}
.edit-button.active .material-icons {
    transform: rotate(45deg); /* 進入編輯模式後旋轉 */
    color: var(--warning-color);
}
.edit-button.active {
    background-color: #dc3545; /* 進入編輯模式後變紅 */
}
.edit-button:hover {
    background-color: #0056b3;
}


/* 網格佈局 - 預設為手機 (2x6) */
.tile-grid {
    display: grid;
    /* 修正: 確保手機版是 2 欄 */
    grid-template-columns: repeat(2, var(--card-width)); 
    grid-template-rows: repeat(6, var(--card-height));
    gap: var(--grid-gap);
    /* 計算 2 欄的總寬度 */
    width: calc(var(--card-width) * 2 + var(--grid-gap)); 
    max-width: 100%; 
    margin-bottom: 30px;
}

/* 響應式調整：平板及電腦使用 4x3 佈局 */
@media (min-width: 768px) {
    .tile-grid {
        /* 修正: 確保電腦版是 4 欄 */
        grid-template-columns: repeat(4, var(--card-width));
        grid-template-rows: repeat(3, var(--card-height));
        /* 計算 4 欄的總寬度 */
        width: calc(var(--card-width) * 4 + var(--grid-gap) * 3);
    }
}

/* 卡片樣式 */
.tile-card {
    background-color: var(--card-bg);
    border-radius: 12px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.08);
    padding: 15px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: flex-start;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s, background-color 0.2s;
    position: relative;
    user-select: none;
    overflow: hidden;
    /* 修正: 確保卡片預設是 flex 佈局，以在 grid 中正確顯示 */
    display: flex !important; 
}

.tile-card:hover {
    box-shadow: 0 6px 15px rgba(0, 0, 0, 0.15);
}

/* 隱藏按鍵的樣式 */
.tile-card[data-visible="0"] {
    display: none !important; /* 確保在非編輯模式下隱藏 */
}

/* 編輯模式下，隱藏的按鍵也要顯示出來，但加上淡化效果 */
.edit-mode .tile-card[data-visible="0"] {
    display: flex !important; 
    opacity: 0.4;
    border: 2px dashed var(--placeholder-color);
    animation: none; 
}

/* 其他卡片樣式保持不變... */

.tile-icon {
    font-size: 36px;
    color: var(--off-color);
    margin-bottom: 5px;
    transition: color 0.3s;
}

.light-on {
    color: var(--on-color);
    text-shadow: 0 0 5px rgba(40, 167, 69, 0.5); 
}
.light-off {
    color: var(--off-color);
}

.tile-text {
    font-weight: 600;
    font-size: 15px;
    line-height: 1.2;
    color: #333;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    width: 100%;
}

.tile-status {
    font-size: 13px;
    color: var(--off-color);
}


/* 編輯模式視覺效果 */
.tile-grid.edit-mode .tile-card {
    animation: shake 0.5s infinite alternate; 
    cursor: pointer;
    border: 2px solid var(--primary-color);
}

@keyframes shake {
    0% { transform: translate(1px, 1px) rotate(0deg); }
    100% { transform: translate(-1px, -1px) rotate(-0.5deg); }
}

/* MQTT 日誌區 */
#log-output {
    width: 90%;
    max-width: calc(var(--card-width) * 4 + var(--grid-gap) * 3); 
    height: 150px;
    background-color: #333;
    color: #00ff00;
    padding: 10px;
    border-radius: 8px;
    overflow-y: scroll;
    font-family: monospace;
    font-size: 12px;
    white-space: pre-wrap;
    box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.5);
}

/* --- Modal 彈出視窗樣式 --- */
.modal {
    display: none; 
    position: fixed;
    z-index: 20; 
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgba(0,0,0,0.4); 
}

.modal-content {
    background-color: #fefefe;
    margin: 10% auto; 
    padding: 20px;
    border: 1px solid #888;
    width: 90%; 
    max-width: 400px;
    border-radius: 12px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    position: relative;
}

.modal-content h3 {
    color: var(--primary-color);
    margin-top: 0;
    border-bottom: 1px solid #eee;
    padding-bottom: 10px;
}

.close-button {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close-button:hover,
.close-button:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-content label {
    display: block;
    margin-bottom: 5px;
    font-weight: 600;
}

.modal-content input[type="text"],
.modal-content select {
    width: 100%;
    padding: 10px;
    margin-bottom: 10px;
    border: 1px solid #ccc;
    border-radius: 8px;
    box-sizing: border-box; 
}

.modal-content .status-msg {
    margin-top: 5px;
    font-size: 13px;
    font-weight: bold;
    color: #333;
    clear: both;
}
