:root {
    --bg: #f4f3ed;
    --card: #fbfbf7;
    --border: rgba(72, 91, 78, .20);
    --txt: #26302c;
    --sub: #68756e;
    --accent: #4f8fa3;
    --danger: #b85a4b;
    --ok: #3f7a4f;
    --warn: #b4742d;
    --panelBg: #fbfbf7;
    --panelBorder: rgba(72, 91, 78, .20);
    --panelBorderStrong: rgba(57, 83, 66, .34);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    min-height: 100svh;
    overflow-x: hidden;
    background: linear-gradient(180deg, #f4f3ed 0%, #e7ece4 100%);
    color: var(--txt);
    font-family: "Microsoft JhengHei", system-ui, sans-serif;
}

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

/* =========================
   主版面
========================= */

.app-shell {
    display: flex;
    min-height: 100vh;
}

.main {
    flex: 1;
    min-width: 0;
}

/* =========================
   側邊欄
========================= */

.sidebar {
    position: sticky;
    top: 0;
    width: 220px;
    height: 100vh;
    flex: 0 0 220px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: #eef3ea;
    border-right: 1px solid var(--border);
    z-index: 30;
}

.side-top {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px;
    border-bottom: 1px solid rgba(148, 163, 184, .16);
}

.logo-dot {
    width: 28px;
    height: 28px;
    flex: 0 0 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    background: #476f5a;
    color: #fff;
    font-size: 16px;
    font-weight: 800;
}

.side-brand {
    display: flex;
    flex-direction: column;
    min-width: 0;
    line-height: 1.1;
}

.side-brand .name {
    font-size: 14px;
    font-weight: 800;
}

.side-brand .sub {
    color: var(--sub);
    font-size: 11px;
}

.nav {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 10px;
}

.nav a,
.side-bottom a {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px;
    border: 1px solid transparent;
    border-radius: 12px;
    color: var(--txt);
    text-decoration: none;
    transition:
        background .15s ease,
        border-color .15s ease;
}

.nav a:hover {
    background: #f0f4ee;
    border-color: rgba(71, 111, 90, .24);
}

.nav a.active {
    background: #fff;
    border-color: rgba(71, 111, 90, .30);
}

.nav .ico,
.side-bottom .ico {
    width: 30px;
    height: 30px;
    flex: 0 0 30px;
    display: grid;
    place-items: center;
    border: 1px solid rgba(72, 91, 78, .16);
    border-radius: 12px;
    background: #f0f4ee;
    font-size: 15px;
}

.side-bottom {
    margin-top: auto;
    padding: 10px 10px 14px;
    border-top: 1px solid rgba(148, 163, 184, .16);
}

.side-bottom a:hover {
    background: #fff3f1;
    border-color: rgba(184, 90, 75, .30);
}

/* =========================
   頂部
========================= */

.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
    padding: 14px 20px;
    border-bottom: 1px solid var(--border);
    background: rgba(251, 251, 247, .95);
    backdrop-filter: blur(10px);
}

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

.menu-btn {
    display: none;
    width: 38px;
    height: 38px;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: #f0f4ee;
    color: var(--txt);
    font-size: 20px;
    cursor: pointer;
}

.topbar-title-main {
    font-size: 18px;
    font-weight: 800;
}

.topbar-title-sub {
    margin-top: 2px;
    color: var(--sub);
    font-size: 11px;
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: 14px;
    color: var(--sub);
    font-size: 12px;
}

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

.user-pill {
    padding: 7px 11px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: #f0f4ee;
    color: var(--txt);
}

/* =========================
   外框與面板
========================= */

.wrap {
    width: 100%;
    max-width: 1400px;
    margin: 18px auto 40px;
    padding: 0 16px;
}

.frame {
    padding: 14px;
    border: 2px solid var(--panelBorderStrong);
    border-radius: 12px;
    background: transparent;
}

.panel {
    border: 1px solid var(--panelBorder);
    border-radius: 10px;
    background: var(--panelBg);
    transition: border-color .15s ease;
}

.panel:hover {
    border-color: rgba(79, 143, 163, .40);
}

/* =========================
   健康總覽
========================= */

.dashboard-overview {
    display: grid;
    grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr);
    gap: 14px;
    margin-bottom: 14px;
}

.health-panel {
    position: relative;
    min-height: 220px;
    padding: 18px;
    overflow: hidden;
}

.health-panel.ok {
    border-color: rgba(63, 122, 79, .35);
}

.health-panel.warn {
    border-color: rgba(180, 116, 45, .45);
}

.health-panel.danger {
    border-color: rgba(184, 90, 75, .45);
}

.health-panel.empty {
    border-color: rgba(104, 117, 110, .28);
}

.health-kicker {
    position: relative;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    color: var(--sub);
    font-size: 13px;
    font-weight: 800;
}

.health-badge {
    padding: 4px 10px;
    border: 1px solid rgba(72, 91, 78, .18);
    border-radius: 999px;
    background: #f0f4ee;
    color: var(--txt);
    font-size: 12px;
}

.health-panel.ok .health-badge {
    border-color: rgba(63, 122, 79, .32);
    background: rgba(63, 122, 79, .08);
    color: var(--ok);
}

.health-panel.warn .health-badge {
    border-color: rgba(180, 116, 45, .34);
    background: rgba(180, 116, 45, .08);
    color: var(--warn);
}

.health-panel.danger .health-badge {
    border-color: rgba(184, 90, 75, .34);
    background: rgba(184, 90, 75, .08);
    color: var(--danger);
}

.health-title {
    position: relative;
    z-index: 2;
    margin: 20px 0 8px;
    font-size: 28px;
    line-height: 1.2;
}

.health-summary {
    position: relative;
    z-index: 2;
    max-width: 600px;
    color: var(--sub);
    font-size: 14px;
    line-height: 1.7;
}

.health-meta {
    position: relative;
    z-index: 2;
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    margin-top: 18px;
}

.health-meta-item {
    min-width: 140px;
    padding: 12px;
    border: 1px solid rgba(72, 91, 78, .14);
    border-radius: 12px;
    background: #fff;
}

.health-meta-item span {
    display: block;
    margin-bottom: 6px;
    color: var(--sub);
    font-size: 12px;
}

.health-meta-item b {
    font-size: 20px;
}

/* =========================
   植物裝飾
========================= */

.plant-visual {
    position: absolute;
    right: 30px;
    bottom: 20px;
    width: 135px;
    height: 135px;
    opacity: .38;
    pointer-events: none;
}

.plant-orb {
    position: absolute;
    inset: 6px;
    border-radius: 50%;
    background: rgba(79, 143, 163, .09);
}

.plant-soil {
    position: absolute;
    right: 28px;
    bottom: 16px;
    left: 28px;
    height: 22px;
    border-radius: 50%;
    background: rgba(71, 111, 90, .25);
}

.plant-leaf {
    position: absolute;
    width: 38px;
    height: 62px;
    border-radius: 70% 10% 70% 10%;
    background: rgba(63, 122, 79, .42);
}

.leaf-a {
    bottom: 38px;
    left: 38px;
    transform: rotate(-28deg);
}

.leaf-b {
    right: 35px;
    bottom: 42px;
    transform: scaleX(-1) rotate(-28deg);
}

.plant-drop {
    position: absolute;
    top: 20px;
    left: 58px;
    width: 20px;
    height: 28px;
    border-radius: 60% 60% 70% 70%;
    background: rgba(79, 143, 163, .38);
    transform: rotate(45deg);
}

/* =========================
   感測器
========================= */

.sensor-panel {
    padding: 14px;
}

.sensor-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
}

.sensor-item {
    min-height: 96px;
    padding: 14px;
    border: 1px solid rgba(72, 91, 78, .16);
    border-radius: 12px;
    background: #fff;
}

.sensor-item.ok {
    border-color: rgba(63, 122, 79, .35);
}

.sensor-item.warn {
    border-color: rgba(180, 116, 45, .45);
}

.sensor-item.danger {
    border-color: rgba(184, 90, 75, .45);
}

.sensor-item.info {
    border-color: rgba(79, 143, 163, .30);
}

.sensor-label {
    margin-bottom: 8px;
    color: var(--sub);
    font-size: 12px;
}

.sensor-value {
    font-size: 22px;
    font-weight: 900;
}

.sensor-item.ok .sensor-value {
    color: var(--ok);
}

.sensor-item.warn .sensor-value {
    color: var(--warn);
}

.sensor-item.danger .sensor-value {
    color: var(--danger);
}

.sensor-meta {
    margin-top: 6px;
    color: var(--sub);
    font-size: 12px;
    line-height: 1.5;
}

/* =========================
   統計列
========================= */

.summary-strip {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 0;
    margin-bottom: 14px;
    overflow: hidden;
}

.summary-item {
    padding: 14px 16px;
    border-right: 1px solid rgba(72, 91, 78, .14);
}

.summary-item:last-child {
    border-right: none;
}

.summary-item span {
    display: block;
    margin-bottom: 6px;
    color: var(--sub);
    font-size: 12px;
}

.summary-item b {
    display: block;
    color: var(--txt);
    font-size: 24px;
    font-weight: 900;
}

.summary-item.ok b {
    color: var(--ok);
}

.summary-item.warn b {
    color: var(--warn);
}

.summary-item.danger b {
    color: var(--danger);
}

/* =========================
   趨勢圖區塊
   桌面版左右各 50%
========================= */

.dashboard-charts {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
    align-items: stretch;
    margin-bottom: 14px;
}

.chart-card {
    min-width: 0;
    padding: 14px;
}

.ai-analysis-card,
.chart-main {
    min-width: 0;
    height: 390px;
    min-height: 390px;
    display: flex;
    flex-direction: column;
}

.chart-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    flex: 0 0 auto;
}

.chart-head-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
    flex-wrap: wrap;
}

.section-h {
    margin: 4px 0 6px;
    color: var(--txt);
    font-size: 15px;
    font-weight: 900;
}

.chart-sub {
    color: var(--sub);
    font-size: 12px;
    line-height: 1.45;
}

.trend-meta {
    color: var(--sub);
    font-size: 11px;
    white-space: nowrap;
}

.chart-range-select {
    min-width: 112px;
    height: 34px;
    padding: 0 10px;
    border: 1px solid rgba(72, 91, 78, .22);
    border-radius: 9px;
    outline: none;
    background: #fff;
    color: var(--txt);
    font-size: 12px;
    cursor: pointer;
}

.chart-range-select:hover {
    border-color: rgba(79, 143, 163, .52);
}

.chart-range-select:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(79, 143, 163, .12);
}

/* =========================
   AI 指標
========================= */

.ai-metric-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
    flex: 0 0 auto;
    margin-top: 12px;
}

.ai-metric-item {
    min-width: 0;
    min-height: 74px;
    padding: 10px;
    border: 1px solid rgba(72, 91, 78, .16);
    border-radius: 12px;
    background: #fff;
}

.ai-metric-item span {
    display: block;
    margin-bottom: 8px;
    color: var(--sub);
    font-size: 12px;
}

.ai-metric-item b {
    display: block;
    overflow-wrap: anywhere;
    color: var(--txt);
    font-size: 20px;
    font-weight: 900;
}

#aiLeafCover {
    color: var(--ok);
}

#aiRedRatio {
    color: var(--danger);
}

#aiStatus.ok {
    color: var(--ok);
}

#aiStatus.warn {
    color: var(--warn);
}

#aiStatus.danger {
    color: var(--danger);
}

#aiStatus.info {
    color: var(--sub);
}

/* =========================
   AI Chart.js 圖表
========================= */

.ai-chart-wrapper {
    position: relative;
    width: 100%;
    height: 190px;
    min-height: 190px;
    flex: 0 0 190px;
    margin-top: 12px;
    padding: 8px 6px 2px;
    overflow: hidden;
    border: 1px solid rgba(72, 91, 78, .14);
    border-radius: 12px;
    background: #fff;
}

.ai-chart-wrapper canvas {
    display: block;
    width: 100% !important;
    height: 100% !important;
}

.chart-empty {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 20px;
    background: rgba(255, 255, 255, .88);
    color: var(--sub);
    font-size: 13px;
    text-align: center;
}

.chart-empty[hidden] {
    display: none;
}

/* =========================
   土壤濕度 Chart.js 圖表
========================= */

.soil-chart-wrapper {
    position: relative;
    width: 100%;
    height: 230px;
    min-height: 230px;
    flex: 1 1 0;
    margin-top: 12px;
    padding: 8px 6px 2px;
    overflow: hidden;
    border: 1px solid rgba(72, 91, 78, .14);
    border-radius: 12px;
    background: #fff;
}

.soil-chart-wrapper canvas {
    display: block;
    width: 100% !important;
    height: 100% !important;
}

.spark {
    display: block;
    width: 100%;
    height: 230px;
    min-height: 230px;
    flex: 0 0 230px;
    margin-top: 16px;
}

.spark polyline {
    fill: none;
    stroke: var(--accent);
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
    opacity: .95;
}

.spark polyline.is-empty {
    opacity: 0;
}

.spark .gridline {
    stroke: rgba(104, 117, 110, .14);
    stroke-width: .8;
}

.spark .area {
    fill: rgba(79, 143, 163, .12);
    stroke: none;
}

.spark-empty,
.notice {
    margin-top: 8px;
    color: var(--sub);
    font-size: 12px;
    line-height: 1.5;
}

.notice {
    min-height: 18px;
}

/* =========================
   告警表格
========================= */

.alerts-panel {
    padding: 12px 14px 14px;
}

.table-wrap {
    margin-top: 8px;
    overflow: auto;
    border: 1px solid rgba(72, 91, 78, .13);
    border-radius: 10px;
}

.table {
    width: 100%;
    min-width: 760px;
    border-collapse: collapse;
    background: #fff;
}

.table th,
.table td {
    padding: 10px;
    border-bottom: 1px solid rgba(148, 163, 184, .14);
    color: var(--txt);
    font-size: 12px;
    text-align: left;
    vertical-align: middle;
}

.table tbody tr:last-child td {
    border-bottom: none;
}

.table tbody tr:hover {
    background: rgba(240, 244, 238, .55);
}

.table th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: #f0f4ee;
    color: var(--sub);
    font-weight: 800;
}

.badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    border: 1px solid rgba(148, 163, 184, .22);
    border-radius: 999px;
    background: #fff;
    font-size: 11px;
    white-space: nowrap;
}

.badge.ok {
    border-color: rgba(63, 122, 79, .30);
    color: var(--ok);
}

.badge.warn {
    border-color: rgba(180, 116, 45, .34);
    color: var(--warn);
}

.badge.danger {
    border-color: rgba(184, 90, 75, .34);
    color: var(--danger);
}

.btn {
    padding: 7px 12px;
    border: 1px solid rgba(148, 163, 184, .22);
    border-radius: 999px;
    background: #fff;
    color: var(--txt);
    font-size: 12px;
    cursor: pointer;
}

.btn:hover {
    border-color: rgba(71, 111, 90, .34);
    background: #f0f4ee;
}

.btn.primary {
    border-color: #476f5a;
    background: #476f5a;
    color: #fff;
    font-weight: 800;
}

.btn.primary:hover {
    border-color: #3f654f;
    background: #3f654f;
}

.muted {
    color: var(--sub);
}

.overlay {
    display: none;
}

/* =========================
   響應式
========================= */

@media (max-width: 1100px) {
    .dashboard-overview {
        grid-template-columns: 1fr;
    }

    .dashboard-charts {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 900px) {
    .app-shell {
        display: block;
        width: 100%;
        max-width: 100%;
        margin: 0;
    }

    .main {
        width: 100%;
        max-width: 100%;
        margin: 0;
    }

    .wrap {
        width: 100%;
        max-width: 100%;
        margin: 14px 0 30px;
        padding: 0 12px;
    }

    .menu-btn {
        display: inline-grid;
        place-items: center;
    }

    .sidebar {
        position: fixed;
        top: 0;
        left: -250px;
        width: 240px;
        height: 100vh;
        flex: none;
        transition: left .25s ease;
        box-shadow: 14px 0 28px rgba(38, 48, 44, .12);
    }

    .sidebar.open {
        left: 0;
    }

    .overlay {
        position: fixed;
        inset: 0;
        z-index: 25;
        display: none;
        background: rgba(36, 48, 52, .30);
    }

    .overlay.show {
        display: block;
    }

    .summary-strip {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .dashboard-charts {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 10px;
    }

    .ai-analysis-card,
    .chart-main {
        height: auto;
        min-height: 0;
    }

    .ai-chart-wrapper {
        height: 220px;
        min-height: 220px;
        flex-basis: 220px;
    }

    .spark {
        height: 200px;
        min-height: 200px;
        flex-basis: 200px;
    }
}

@media (max-width: 768px) {
    .topbar {
        padding: 11px 12px;
    }

    .topbar-clock {
        display: none;
    }

    .wrap,
    .frame {
        width: 100%;
        max-width: 100%;
    }

    .health-panel {
        min-height: 250px;
    }

    .plant-visual {
        right: 8px;
        bottom: 10px;
        transform: scale(.82);
        transform-origin: right bottom;
    }

    .sensor-grid {
        grid-template-columns: 1fr;
    }

    .chart-head {
        flex-direction: column;
        align-items: stretch;
    }

    .chart-head-actions {
        justify-content: space-between;
    }

    .table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .btn.primary {
        padding: 6px 10px;
        font-size: 11px;
        white-space: nowrap;
    }
}

@media (max-width: 600px) {
    .wrap {
        margin: 0;
        padding: 0;
    }

    .frame {
        margin: 0;
        padding: 10px;
        border-right: none;
        border-left: none;
        border-radius: 0;
    }

    .panel {
        border-radius: 12px;
    }

    .user-pill {
        max-width: 120px;
        overflow: hidden;
        font-size: 11px;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .health-title {
        font-size: 22px;
    }

    .health-summary {
        max-width: 75%;
    }

    /* AI 與土壤圖表固定左右兩欄 */
    .dashboard-charts {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
    }

    .chart-card {
        min-width: 0;
        padding: 8px;
        overflow: hidden;
    }

    .ai-analysis-card,
    .chart-main {
        min-width: 0;
        height: auto;
        min-height: 0;
    }

    .chart-head {
        gap: 6px;
    }

    .section-h {
        margin: 2px 0 4px;
        font-size: 12px;
    }

    .chart-sub {
        font-size: 9px;
        line-height: 1.35;
    }

    .trend-meta {
        font-size: 9px;
        white-space: normal;
    }

    .chart-head-actions {
        align-items: stretch;
        flex-direction: column;
        gap: 5px;
    }

    .chart-range-select {
        width: 100%;
        min-width: 0;
        height: 28px;
        padding: 0 5px;
        font-size: 9px;
    }

    /* AI 三個數值保持橫向 */
    .ai-metric-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 5px;
        margin-top: 8px;
    }

    .ai-metric-item {
        min-width: 0;
        min-height: 60px;
        padding: 6px;
        border-radius: 8px;
    }

    .ai-metric-item span {
        margin-bottom: 4px;
        font-size: 8px;
        line-height: 1.25;
    }

    .ai-metric-item b {
        font-size: 13px;
        line-height: 1.2;
    }

    /* 左右圖表使用相同高度 */
    .ai-chart-wrapper {
        height: 165px;
        min-height: 165px;
        flex-basis: 165px;
        margin-top: 8px;
        padding: 4px 2px 1px;
    }

    .spark {
        height: 165px;
        min-height: 165px;
        flex-basis: 165px;
        margin-top: 8px;
    }

    .notice,
    .spark-empty {
        margin-top: 5px;
        font-size: 9px;
        line-height: 1.35;
    }
}

@media (max-width: 560px) {
    .summary-strip {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .summary-item {
        padding: 12px;
        border-bottom: 1px solid rgba(72, 91, 78, .12);
    }

    .summary-item:nth-child(2n) {
        border-right: none;
    }

    .summary-item:nth-last-child(-n + 2) {
        border-bottom: none;
    }

    .summary-item b {
        font-size: 22px;
    }

    .chart-head-actions {
        align-items: stretch;
        flex-direction: column;
    }

    .chart-range-select {
        width: 100%;
    }
}

@media (max-width: 420px) {
    .topbar-title-main {
        font-size: 16px;
    }

    .health-summary {
        max-width: 100%;
    }

    .plant-visual {
        opacity: .18;
    }

    .health-meta-item {
        flex: 1;
        min-width: 120px;
    }
}

/* 儀表板趨勢圖：固定雙欄並保留下方內容的頁面捲動 */
body.dashboard-page {
    overflow-y: auto !important;
}

body.dashboard-page .main,
body.dashboard-page .wrap {
    height: auto !important;
    min-height: 0;
    overflow: visible !important;
}

body.dashboard-page .dashboard-charts {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    align-items: stretch;
}

body.dashboard-page .dashboard-charts > .chart-card {
    grid-column: span 1 !important;
    height: clamp(280px, 42vw, 390px);
    min-height: 0;
}

body.dashboard-page .ai-chart-wrapper,
body.dashboard-page .soil-chart-wrapper {
    height: auto !important;
    min-height: 0 !important;
    flex: 1 1 0;
}
