html,
body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    background-color: #f8fafc;
    /* 增加：防止手机端字体自动放大 */
    -webkit-text-size-adjust: 100%;
}

.grid-container {
    margin: 10px auto;
    width: 95vw;
    max-width: 500px;
    aspect-ratio: 1 / 1;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 2px;
    background: #475569;
    border: 3px solid #475569;
    box-sizing: border-box;
}

.grid-item {
    background: #ffffff;
    position: relative;
    box-sizing: border-box;
    overflow: hidden;
    /* 增加：强制统一行高，防止在不同设备上偏移 */
    line-height: 1.2;
}

/* 1. 宫位名称 */
.grid-item .position {
    position: absolute;
    bottom: 4px;
    /* 稍微提升一点，防止贴边 */
    left: 50%;
    transform: translateX(-50%);
    font-size: 18px;
    color: #94a3b8;
    white-space: nowrap;
    z-index: 1;
}

/* 2. 中轴线三元素：神、星、门 */
/* 增加统一的 class 处理中轴对齐 */
.grid-item .shen,
.grid-item .star,
.grid-item .door {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    text-align: center;
    width: 100%;
}

.grid-item .shen {
    top: 10%;
    font-size: 24px;
    font-weight: bold;
}

.grid-item .star {
    top: 32%;
    font-size: 28px;
    font-weight: bold;
}

.grid-item .door {
    top: 56%;
    font-size: 28px;
    font-weight: bold;
}

/* 寄天干 (左侧) 与 天盘干 (右侧) */
.grid-item .ji-gan,
.grid-item .tianpan-gan {
    position: absolute;
    top: 42%;
    /* 微调高度以对齐中轴线 */
    font-weight: bold;
    line-height: 1;
    z-index: 5;
}

.grid-item .ji-gan {
    left: 6px;
    /* 适当缩小间距防止窄屏溢出 */
    font-size: 20px;
}

.grid-item .tianpan-gan {
    right: 6px;
    font-size: 20px;
}

/* 地盘干 */
.grid-item .dipan-gan {
    position: absolute;
    right: 6px;
    bottom: 12%;
    font-size: 20px;
    font-weight: bold;
    line-height: 1;
    z-index: 5;
}

/* 驿马与空亡 符号 */
.grid-item .yima,
.grid-item .kongwang {
    position: absolute;
    left: 6px;
    font-size: 18px; /* Reduced from 20px */
    line-height: 1;
    z-index: 5;
}

.grid-item .yima {
    top: 10%;
    /* 对齐 神 的高度 */
}

.grid-item .kongwang {
    top: 10%;
    /* 默认在顶部，与驿马重合（如果都在） */
}

.grid-item .kongwang.stacked {
    top: 26%;
    /* 当 驿马 存在时，下移到中间 */
}

/* 时干红框优化 */
.grid-item span[style*="outline"] {
    display: inline-block;
    padding: 1px;
    /* 确保红框不会撑大盒子 */
    box-sizing: border-box;
}

/* 关键：针对 Telegram WebView 和小屏幕手机的适配 */
@media (max-width: 430px) {

    .grid-item .star,
    .grid-item .door {
        font-size: 20px;
    }

    .grid-item .shen {
        font-size: 16px;
    }

    .grid-item .tianpan-gan,
    .grid-item .dipan-gan,
    .grid-item .ji-gan {
        font-size: 17px;
    }

    .grid-item .position {
        font-size: 11px;
        bottom: 2px;
    }

    /* 缩小间距防止重叠 */
    .grid-item .ji-gan {
        left: 4px;
    }

    .grid-item .tianpan-gan,
    .grid-item .dipan-gan {
        right: 4px;
    }

    /* 驿马与空亡 缩放 */
    .grid-item .yima,
    .grid-item .kongwang {
        left: 4px;
        font-size: 14px; /* Reduced from 16px */
    }
}

/* 中五宫特殊样式：灰字且更小 */
.grid-item:nth-child(5) .star,
.grid-item:nth-child(5) .door {
    color: grey !important;
    font-size: 26px; /* -2 from 28px */
}

@media (max-width: 430px) {
    .grid-item:nth-child(5) .star,
    .grid-item:nth-child(5) .door {
        font-size: 18px; /* -2 from 20px */
    }
}

@media (max-width: 340px) {
    .grid-item:nth-child(5) .star,
    .grid-item:nth-child(5) .door {
        font-size: 16px; /* -2 from 18px */
    }
}

/* 针对极窄屏幕（如 iPhone SE） */
@media (max-width: 340px) {

    .grid-item .star,
    .grid-item .door {
        font-size: 18px;
    }

    .grid-item .tianpan-gan,
    .grid-item .dipan-gan,
    .grid-item .ji-gan {
        font-size: 15px;
    }
}