/* ==========================================================================
   Apple Design System - Qi Men Dun Jia Pro Theme (V41 Perfect Alignment Fix)
   ========================================================================== */

:root {
    --system-bg: #f4f6fa;
    --card-bg: rgba(255, 255, 255, 0.92);
    --card-border: rgba(0, 0, 0, 0.06);
    --primary: #0071e3;
    --primary-hover: #0077ed;
    --primary-light: #e8f2ff;
    --active-cyan: #00d2ff;
    --success: #34c759;
    --warning: #ff9500;
    --danger: #ff3b30;
    --text-main: #1d1d1f;
    --text-secondary: #86868b;
    --border-color: #e5e5ea;
    --input-bg: #f5f5f7;
    
    /* 五行與特色色彩 */
    --color-green: #34c759;
    --color-red: #ff3b30;
    --color-brown: #a2845e;
    --color-gold: #d97706;
    --color-blue: #0071e3;
    --color-yellow-bg: #ffd60a;
    
    --radius-card: 20px;
    --radius-btn: 12px;
    --radius-input: 10px;
    
    --shadow-card: 0 8px 24px rgba(0, 0, 0, 0.04), 0 1px 2px rgba(0, 0, 0, 0.02);
    --shadow-dock: 0 -4px 25px rgba(0, 0, 0, 0.08);
    --blur-glass: blur(20px) saturate(180%);
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    scroll-behavior: smooth;
}

/* 鎖定手機版下拉重新整理 (Prevent Pull-To-Refresh) */
html, body {
    overscroll-behavior-y: contain;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Helvetica Neue", Arial, sans-serif;
    background-color: var(--system-bg);
    color: var(--text-main);
    margin: 0;
    padding: 16px 14px 140px 14px;
    display: flex;
    justify-content: center;
    -webkit-font-smoothing: antialiased;
}

.app-container {
    width: 100%;
    max-width: 680px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

#panelsContainer {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

/* Header */
.app-header {
    text-align: center;
    padding: 8px 0 4px 0;
}
.app-header h1 {
    font-size: 24px;
    font-weight: 700;
    letter-spacing: -0.5px;
    margin: 0;
    color: var(--text-main);
}

/* Apple Cards */
.card {
    background: var(--card-bg);
    backdrop-filter: var(--blur-glass);
    -webkit-backdrop-filter: var(--blur-glass);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-card);
    padding: 18px 16px;
    box-shadow: var(--shadow-card);
}

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
    cursor: pointer;
    user-select: none;
}
.card-title-group {
    flex: 1;
    min-width: 0;
}
.card-header h2, .card-title-group h2 {
    font-size: 16px;
    font-weight: 600;
    margin: 0;
    letter-spacing: -0.3px;
}
.card-desc {
    font-size: 12px;
    color: var(--text-secondary);
    margin: 2px 0 0 0;
}

/* Form Inputs */
.form-row { display: flex; }
.gap-12 { gap: 12px; }
.flex-1 { flex: 1; }
.margin-top-12 { margin-top: 12px; }
.margin-top-16 { margin-top: 16px; }

.input-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.input-field label {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-left: 2px;
}

.apple-input {
    width: 100%;
    height: 42px;
    padding: 0 12px;
    font-size: 14.5px;
    border-radius: var(--radius-input);
    border: 1px solid var(--border-color);
    background: var(--input-bg);
    color: var(--text-main);
    transition: all 0.2s ease;
    outline: none;
}
.apple-input:focus {
    background: #ffffff;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(0, 113, 227, 0.15);
}
.text-center { text-align: center; }

/* Dropdown */
.apple-select-wrapper {
    position: relative;
    width: 100%;
}
.apple-select-wrapper::after {
    content: "〉";
    font-size: 11px;
    font-weight: bold;
    color: var(--text-secondary);
    transform: rotate(90deg);
    position: absolute;
    right: 12px;
    top: 13px;
    pointer-events: none;
}
.apple-select {
    width: 100%;
    height: 42px;
    padding: 0 30px 0 12px;
    font-size: 14px;
    font-weight: 500;
    border-radius: var(--radius-input);
    border: 1px solid var(--border-color);
    background: var(--input-bg);
    color: var(--text-main);
    appearance: none;
    -webkit-appearance: none;
    outline: none;
    cursor: pointer;
}

/* Apple Pill Dropdown ("複製同宮") */
.apple-select-wrapper.compact {
    width: auto;
    min-width: 92px;
}
.apple-select-wrapper.compact::after {
    right: 10px;
    top: 8px;
    font-size: 9px;
    color: var(--primary);
}
.apple-select.copy-tool-select {
    height: 28px;
    padding: 0 22px 0 10px;
    font-size: 12px;
    font-weight: 600;
    border-radius: 14px;
    border: 1px solid rgba(0, 113, 227, 0.2);
    background: var(--primary-light);
    color: var(--primary);
    transition: all 0.2s ease;
}

/* Time Picker Section */
.time-section {
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.time-grid {
    display: grid;
    grid-template-columns: 1.2fr 1fr;
    gap: 10px;
}
.time-picker {
    display: flex;
    align-items: center;
    gap: 6px;
}
.time-picker .colon {
    font-weight: 700;
    color: var(--text-secondary);
}

/* Segmented Control Buttons */
.toggle-segmented-group {
    display: flex;
    background: var(--input-bg);
    padding: 3px;
    border-radius: 10px;
    gap: 4px;
}
.segmented-item { flex: 1; }
.segmented-item input { display: none; }
.segmented-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 32px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}
.segmented-item input:checked + .segmented-btn {
    background: #ffffff;
    color: var(--text-main);
    font-weight: 600;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

/* Checkbox & Radio Buttons */
.grid-5 { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }

.btn-check {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 36px;
    background: var(--input-bg);
    border-radius: 8px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-main);
    cursor: pointer;
    transition: all 0.15s ease;
    border: 1px solid transparent;
    user-select: none;
}
input[type="checkbox"], input[type="radio"] { display: none; }
input[type="checkbox"]:checked + .btn-check, input[type="radio"]:checked + .btn-check {
    background: var(--primary-light);
    color: var(--primary);
    border-color: var(--primary);
    font-weight: 600;
}

/* Buttons */
.apple-btn {
    width: 100%;
    height: 42px;
    border: none;
    border-radius: var(--radius-btn);
    font-size: 14.5px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}
.apple-btn:active { transform: scale(0.98); }

.btn-primary-gradient {
    background: linear-gradient(135deg, #0071e3, #005bb5);
    color: #ffffff;
    box-shadow: 0 4px 12px rgba(0, 113, 227, 0.22);
}
.btn-secondary {
    background: var(--input-bg);
    color: var(--text-main);
}

/* 標題區域：可點擊整個容器展開/隱藏 */
.panel-title-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    user-select: none;
    transition: opacity 0.15s ease;
}
.panel-title-container:hover {
    opacity: 0.85;
}

.panel-title {
    font-size: 14.5px;
    font-weight: 600;
    color: var(--primary);
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
}

.panel-header-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.apple-pill-btn {
    height: 28px;
    padding: 0 12px;
    font-size: 12px;
    font-weight: 600;
    border-radius: 14px;
    border: none;
    background: var(--primary-light);
    color: var(--primary);
    cursor: pointer;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

/* ==========================================================================
   3x3 九宮配置視覺盤 (字號一致與黃圈精準居中佈局)
   ========================================================================== */

#jiugongGridWrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    transition: max-height 0.3s ease, opacity 0.3s ease, margin 0.3s ease;
    overflow: hidden;
}

.jiugong-grid.classic-style {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    aspect-ratio: 1 / 1;
    width: 100%;
    max-width: 420px;
    margin: 0 auto;
    gap: 1.5px;
    background: #e5e5ea;
    border: 1px solid #d1d1d6;
    border-radius: 14px;
    overflow: hidden;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.03);
}

.jiugong-grid.classic-style .jiugong-cell {
    background: #ffffff;
    aspect-ratio: 1 / 1;
    padding: 6px 5px;
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    box-sizing: border-box;
    overflow: hidden;
}

/* 中宮：「陰盤(無中)」置中 */
.jiugong-grid.classic-style .cell-center {
    background: #f8f8fa;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    text-align: center;
}

.cell-content-center {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    font-size: 13px;
    font-weight: 600;
    color: #8e8e93;
    letter-spacing: 0.3px;
}

.jiugong-grid.classic-style .palace-num {
    position: absolute;
    right: 4px;
    bottom: 2px;
    font-size: 10px;
    font-weight: 600;
    color: #c7c7cc;
    pointer-events: none;
}

.jiugong-grid.classic-style .cell-inner {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    justify-content: space-between;
}

/* 1. 頂欄槽位 (暗干膠囊與馬空圓標) */
.grid-top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    min-height: 16px;
}

.grid-angan-tag {
    font-size: 9.5px;
    font-weight: 700;
    color: var(--primary);
    background: rgba(0, 113, 227, 0.08);
    border: 1px solid rgba(0, 113, 227, 0.15);
    padding: 0.5px 4px;
    border-radius: 4px;
    line-height: 1.15;
    letter-spacing: 0.1px;
    white-space: nowrap;
}

.grid-status-badges {
    display: flex;
    gap: 2.5px;
}

/* 2. 主陣列槽位 (左欄與右欄) */
.grid-main-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex: 1;
    width: 100%;
    margin-top: 2px;
    padding: 0;
}

/* 左欄 (天神、天干、地干、地神) */
.grid-col-left {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 13.5px;
    font-weight: 600;
    line-height: 1.25;
    align-items: flex-start;
}

/* 右欄 (九星、八門) */
.grid-col-right {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 13.5px;
    font-weight: 600;
    line-height: 1.25;
    align-items: flex-end;
    min-width: 18px;
}

/* 天干橫排一行 */
.stem-row {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 1px 3px;
    font-size: 13.5px;
}

/* 單個天干與其 12 長生/刑墓標籤組件 */
.stem-item {
    display: inline-flex;
    align-items: baseline;
    white-space: nowrap;
}

/* 12 長生標記 */
.cs-tag {
    font-size: 8.5px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-left: 0.5px;
}

.sub-tag {
    font-size: 8.5px;
    color: var(--danger);
    font-weight: 600;
    margin-left: 0.5px;
}

/* 精準五行色彩映射 */
.c-green { color: var(--color-green) !important; font-weight: 600; }
.c-red { color: var(--color-red) !important; font-weight: 700; }
.c-brown { color: var(--color-brown) !important; font-weight: 600; }
.c-gold { color: var(--color-gold) !important; font-weight: 600; }
.c-blue { color: var(--color-blue) !important; font-weight: 700; }
.c-gan { color: var(--text-main) !important; font-weight: 600; }

/* 雙字九星「芮禽」專用平排類別 (字號與其他九星與八門 100% 完全一致) */
.two-char-star {
    font-size: inherit !important;
    letter-spacing: normal;
    white-space: nowrap;
    display: inline-flex;
    flex-direction: row;
    align-items: center;
    line-height: inherit;
}

/* 黃色圓形標籤 (馬星 / 空亡 - 100% 精準垂直水平居中) */
.badge-circle {
    width: 16px;
    height: 16px;
    background-color: var(--color-yellow-bg);
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-size: 9.5px;
    font-weight: 700;
    color: #000000;
    line-height: 16px;
    padding: 0;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
}

.apple-textarea {
    width: 100%;
    height: 200px;
    border-radius: var(--radius-input);
    border: 1px solid var(--border-color);
    background: var(--input-bg);
    padding: 10px;
    font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 12.5px;
    line-height: 1.45;
    color: var(--text-main);
    resize: none;
    outline: none;
}

/* Floating Dock */
.apple-dock-wrapper {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: center;
    z-index: 1000;
    pointer-events: none;
}

.apple-dock {
    pointer-events: auto;
    width: 100%;
    max-width: 680px;
    height: 70px;
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: var(--blur-glass);
    -webkit-backdrop-filter: var(--blur-glass);
    border-radius: 24px 24px 0 0;
    border-top: 1px solid rgba(0, 0, 0, 0.05);
    box-shadow: var(--shadow-dock);
    display: flex;
    align-items: center;
    justify-content: space-around;
    padding: 6px 20px;
    padding-bottom: max(6px, env(safe-area-inset-bottom));
}

.dock-item {
    flex: 1;
    max-width: 180px;
    height: 52px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    border-radius: 16px;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    color: var(--text-secondary);
    gap: 2px;
}

.dock-item:active {
    transform: scale(0.95);
}

.dock-item .dock-icon {
    font-size: 18px;
    line-height: 1;
}

.dock-item .dock-label {
    font-size: 12px;
    font-weight: 700;
    letter-spacing: -0.2px;
}

.dock-item.btn-reset {
    color: var(--danger);
}
.dock-item.btn-reset:hover {
    background: rgba(255, 59, 48, 0.06);
}

.dock-item.btn-scroll {
    color: var(--primary);
}
.dock-item.btn-scroll:hover {
    background: rgba(0, 113, 227, 0.06);
}

.dock-item.btn-primary {
    background: var(--active-cyan);
    color: #000000;
    box-shadow: 0 4px 14px rgba(0, 210, 255, 0.35);
}
.dock-item.btn-primary .dock-label {
    color: #000000;
    font-weight: 800;
}

/* ==========================================================================
   手機版特化適應 (iPhone 12 Pro 390px 等微型螢幕防裁切 RWD)
   ========================================================================== */
@media (max-width: 480px) {
    body { padding: 10px 6px 120px 6px; }
    .card { padding: 14px 10px; }
    .time-grid { grid-template-columns: 1fr; }
    
    .jiugong-grid.classic-style { max-width: 100%; }
    .jiugong-grid.classic-style .jiugong-cell { padding: 5px 3.5px; }
    
    .grid-col-left, .grid-col-right {
        font-size: 12px;
        gap: 2.5px;
        line-height: 1.18;
    }
    .stem-row {
        font-size: 12px;
        gap: 1px 2px;
    }
    .cs-tag, .sub-tag {
        font-size: 8px;
        letter-spacing: -0.3px;
    }
    .two-char-star {
        font-size: inherit !important;
        letter-spacing: normal;
    }
    .grid-angan-tag {
        font-size: 8.5px;
        padding: 0.5px 3px;
    }
    .badge-circle {
        width: 15px;
        height: 15px;
        font-size: 8.5px;
        line-height: 15px;
        padding: 0;
    }
    .apple-dock { padding: 6px 10px; }
}
