/*
 * Kept deliberately plain: no framework, no reset library. The whole app is one screen with a
 * handful of cards, so hand-written CSS is smaller than the loader for anything else and there is
 * nothing to go stale.
 *
 * Colours match the Android app (ChartView.java) so the two look like the same product.
 * env(safe-area-inset-*) keeps content clear of the iPhone notch and home indicator.
 */
:root {
    --bg: #0d1117;
    --panel: #161b22;
    --panel-2: #1c2129;
    --line: #21262d;
    --axis: #30363d;
    --fg: #c9d1d9;
    --muted: #8b949e;
    --temp: #f0883e;
    --humi: #58a6ff;
    --warn: #d29922;
    --ok: #3fb950;
    --bad: #f85149;
}

* { box-sizing: border-box; }

/*
 * The `hidden` attribute only sets display:none in the user-agent stylesheet, and any author rule
 * outranks that regardless of specificity. So `#chart { display: block }` below would keep the
 * canvas on screen even while hidden, drawing the chart and the list on top of each other. This
 * is not decoration: without it, `hidden` silently does nothing on any styled element.
 */
[hidden] { display: none !important; }

html, body { margin: 0; padding: 0; }

body {
    background: var(--bg);
    color: var(--fg);
    font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
          "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
    padding: 0 12px calc(20px + env(safe-area-inset-bottom));
    max-width: 720px;
    margin: 0 auto;
}

/* ---- header ---- */
.bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: calc(10px + env(safe-area-inset-top)) 2px 10px;
    position: sticky;
    top: 0;
    background: var(--bg);
    z-index: 5;
}

.bar-title { font-size: 17px; font-weight: 600; }

.bar-right { display: flex; align-items: center; gap: 8px; }

.pill {
    font-size: 12px;
    padding: 3px 9px;
    border-radius: 999px;
    border: 1px solid var(--line);
    color: var(--muted);
    background: var(--panel);
    white-space: nowrap;
}

.pill.on   { color: var(--ok);   border-color: rgba(63, 185, 80, .45); }
.pill.warn { color: var(--warn); border-color: rgba(210, 153, 34, .45); }
.pill.off  { color: var(--bad);  border-color: rgba(248, 81, 73, .45); }

/* ---- cards ---- */
.card {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 12px;
    margin-bottom: 12px;
}

.readout { display: flex; gap: 10px; }

.metric { flex: 1; text-align: center; }

.metric-label { font-size: 12px; color: var(--muted); margin-bottom: 2px; }

.metric-value {
    font-size: 40px;
    font-weight: 600;
    line-height: 1.05;
    font-variant-numeric: tabular-nums;
}

.metric.temp .metric-value { color: var(--temp); }
.metric.humi .metric-value { color: var(--humi); }

.metric-value .unit { font-size: 16px; font-weight: 400; color: var(--muted); margin-left: 2px; }

.meta {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    font-size: 12px;
    color: var(--muted);
    padding: 0 4px 12px;
    font-variant-numeric: tabular-nums;
}

/* ---- alarm banner ---- */
.banner {
    border-radius: 10px;
    padding: 9px 12px;
    margin-bottom: 12px;
    font-size: 14px;
    background: rgba(210, 153, 34, .14);
    border: 1px solid rgba(210, 153, 34, .5);
    color: var(--warn);
}

.banner.bad { background: rgba(248, 81, 73, .14); border-color: rgba(248, 81, 73, .5); color: var(--bad); }

.banner button {
    float: right;
    background: none;
    border: 1px solid currentColor;
    color: inherit;
    border-radius: 6px;
    font-size: 12px;
    padding: 1px 8px;
    margin-left: 8px;
}

/* ---- tabs ---- */
.tabs { display: flex; gap: 6px; margin-bottom: 10px; }

.tab {
    flex: 0 0 auto;
    background: var(--panel-2);
    border: 1px solid var(--line);
    color: var(--muted);
    border-radius: 8px;
    padding: 5px 14px;
    font-size: 13px;
}

.tab.on { color: var(--fg); border-color: var(--axis); background: #22282f; }

/* ---- chart ---- */
#chart {
    display: block;
    width: 100%;
    height: 250px;
    border-radius: 8px;
    touch-action: pan-y;
}

/* ---- list ---- */
.list {
    height: 250px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 12px;
}

.list-head, .list-row {
    display: grid;
    grid-template-columns: 1fr auto auto auto;
    gap: 8px;
    padding: 4px 2px;
    font-variant-numeric: tabular-nums;
}

.list-head {
    color: var(--muted);
    position: sticky;
    top: 0;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
}

.list-row { border-bottom: 1px solid rgba(33, 38, 45, .6); }
.list-row .t-temp { color: var(--temp); text-align: right; }
.list-row .t-humi { color: var(--humi); text-align: right; }
.list-row .t-batt { color: var(--muted); text-align: right; }
.list-row.hot .t-temp, .list-row.hot .t-humi { color: var(--bad); }
.list .more { color: var(--muted); padding: 8px 2px; font-size: 12px; }
.list .empty { color: var(--muted); padding: 24px 0; text-align: center; font-family: inherit; }

.stats {
    color: var(--muted);
    font-size: 12px;
    padding-top: 10px;
    line-height: 1.6;
    white-space: pre-line;
    font-variant-numeric: tabular-nums;
}

/* ---- actions ---- */
.actions { display: flex; gap: 10px; margin-bottom: 12px; }

button {
    font: inherit;
    cursor: pointer;
}

.actions button, .sheet-actions button, .wide {
    flex: 1;
    padding: 11px;
    border-radius: 10px;
    border: 1px solid var(--line);
    background: var(--panel);
    color: var(--fg);
}

.actions button:active, .sheet-actions button:active { background: var(--panel-2); }

button.primary { background: #1f6feb; border-color: #1f6feb; color: #fff; font-weight: 600; }
button.danger  { color: var(--bad); border-color: rgba(248, 81, 73, .4); }
button.ghost   { background: transparent; }

.icon-btn {
    width: 34px;
    height: 34px;
    border-radius: 9px;
    border: 1px solid var(--line);
    background: var(--panel);
    color: var(--fg);
    font-size: 16px;
    line-height: 1;
}

/* ---- modal ---- */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(1, 4, 9, .72);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    z-index: 50;
    padding: 0;
}

.sheet {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 14px 14px 0 0;
    width: 100%;
    max-width: 720px;
    max-height: 92vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
}

@media (min-width: 560px) {
    .modal { align-items: center; padding: 20px; }
    .sheet { border-radius: 14px; max-height: 86vh; }
}

.sheet h2 { margin: 0 0 12px; font-size: 16px; }

.sheet label { display: block; font-size: 12px; color: var(--muted); margin: 10px 0 3px; }

.sheet input[type=text], .sheet input[type=password], .sheet input[type=number],
.sheet input[type=datetime-local] {
    width: 100%;
    padding: 9px 10px;
    border-radius: 9px;
    border: 1px solid var(--line);
    background: var(--bg);
    color: var(--fg);
    font: 14px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.sheet input:focus { outline: none; border-color: #1f6feb; }

.row2 { display: flex; gap: 10px; }
.row2 > * { flex: 1; min-width: 0; }

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

.quick { display: flex; gap: 8px; margin: 10px 0 2px; }

.quick button {
    flex: 1;
    padding: 7px;
    font-size: 13px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: var(--panel-2);
    color: var(--muted);
}

.sheet-actions { display: flex; gap: 10px; margin-top: 18px; }

.checkline { display: flex; align-items: center; gap: 8px; margin-top: 14px; font-size: 14px; }
.checkline input { width: 18px; height: 18px; }
