:root {
    --brand: #0f5132;
    --brand-2: #198754;
    --bg: #f3f5f4;
    --card-border: #e3e8e5;
    --text-muted: #6c757d;
    --good: #20c05c;
    --bad: #adb5bd;
    --bs-font-sans-serif: "IBM Plex Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
    --bs-primary: var(--brand-2);
    --bs-primary-rgb: 25, 135, 84;
    --bs-link-color: var(--brand-2);
    --bs-link-hover-color: var(--brand);
}

body { background: var(--bg); min-height: 100vh; }

.btn-primary { --bs-btn-bg: var(--brand-2); --bs-btn-border-color: var(--brand-2); --bs-btn-hover-bg: var(--brand); --bs-btn-hover-border-color: var(--brand); --bs-btn-active-bg: var(--brand); }

.min-w-0 { min-width: 0; }

/* ---- app bar ---- */
.appbar {
    position: sticky; top: 0; z-index: 1030;
    background: var(--brand); color: #fff;
    padding-top: env(safe-area-inset-top);
    box-shadow: 0 1px 0 rgba(0, 0, 0, .08);
}
.appbar-inner { max-width: 1100px; margin: 0 auto; height: 56px; padding: 0 12px; display: flex; align-items: center; gap: 12px; }
.brand { color: #fff; text-decoration: none; font-weight: 600; display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.brand i { font-size: 1.25rem; }
.tenant-chip { font-size: .8rem; background: rgba(255, 255, 255, .15); border-radius: 999px; padding: 2px 10px; max-width: 40vw; }
.btn-appbar { color: #fff; border: 0; width: 40px; height: 40px; display: inline-flex; align-items: center; justify-content: center; border-radius: 10px; font-size: 1.15rem; }
.btn-appbar:hover, .btn-appbar:focus-visible { color: #fff; background: rgba(255, 255, 255, .15); }

/* ---- page ---- */
.page { max-width: 1100px; margin: 0 auto; padding: 16px 12px calc(32px + env(safe-area-inset-bottom)); }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.back-link { display: inline-flex; align-items: center; gap: 2px; font-size: .875rem; text-decoration: none; margin-bottom: 4px; }
.card { border-color: var(--card-border); border-radius: 14px; }
.section-title { font-size: 1rem; font-weight: 600; margin-bottom: 10px; }
.form-narrow { max-width: 560px; }

/* ---- login ---- */
.login-wrap { max-width: 400px; margin: 8vh auto 0; }
.login-logo { width: 64px; height: 64px; border-radius: 18px; background: var(--brand); color: #fff; display: inline-flex; align-items: center; justify-content: center; font-size: 2rem; margin-bottom: 12px; }

/* ---- stats ---- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 16px; }
.stat { background: #fff; border: 1px solid var(--card-border); border-radius: 14px; padding: 12px; display: flex; flex-direction: column; }
.stat-num { font-size: 1.6rem; font-weight: 600; line-height: 1.1; }
.stat-label { font-size: .8rem; color: var(--text-muted); }
.stat-good .stat-num { color: var(--brand-2); }
.stat-bad .stat-num { color: #6c757d; }

.map { height: 280px; }
@media (min-width: 768px) { .map { height: 360px; } }

/* ---- device cards ---- */
.device-grid { display: grid; grid-template-columns: 1fr; gap: 10px; }
@media (min-width: 576px) { .device-grid { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 992px) { .device-grid { grid-template-columns: repeat(3, 1fr); } }

.device-card {
    display: block; background: #fff; border: 1px solid var(--card-border); border-radius: 14px;
    padding: 14px; color: inherit; text-decoration: none; transition: border-color .15s, box-shadow .15s;
}
.device-card:hover, .device-card:focus-visible { border-color: var(--brand-2); box-shadow: 0 2px 10px rgba(15, 81, 50, .08); color: inherit; }
.device-card.is-disabled { opacity: .55; }
.device-name { font-weight: 600; }
.device-meta, .device-seen { font-size: .78rem; color: var(--text-muted); }
.device-seen { margin-top: 8px; }

.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; margin-top: 7px; background: var(--bad); }
.dot-on { background: var(--good); box-shadow: 0 0 0 3px rgba(32, 192, 92, .2); }
h1 .dot { margin-top: 0; }

.metrics { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 12px; margin: 12px 0 0; }
.metrics dt { font-size: .75rem; font-weight: 400; color: var(--text-muted); }
.metrics dd { margin: 0; font-size: 1.1rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.metrics small, .metric-value small { font-size: .72rem; font-weight: 400; color: var(--text-muted); margin-left: 2px; }

/* ---- device page ---- */
.metric-tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
@media (min-width: 768px) { .metric-tiles { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.metric-tile {
    display: flex; flex-direction: column; align-items: flex-start; text-align: left; gap: 2px;
    background: #fff; border: 1px solid var(--card-border); border-radius: 14px; padding: 12px; min-width: 0;
}
.metric-tile.active { border-color: var(--brand-2); box-shadow: inset 0 0 0 1px var(--brand-2); }
.metric-label { font-size: .78rem; color: var(--text-muted); max-width: 100%; }
.metric-value { font-size: 1.35rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.metric-time { font-size: .72rem; color: var(--text-muted); }

.chart-box { position: relative; height: 260px; }
@media (min-width: 768px) { .chart-box { height: 340px; } }
.chart-empty { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--text-muted); }

.kv { display: grid; grid-template-columns: minmax(110px, max-content) 1fr; gap: 6px 16px; font-size: .9rem; }
.kv dt { font-weight: 400; color: var(--text-muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
@media (max-width: 575.98px) { .kv { grid-template-columns: 1fr; gap: 0; } .kv dd { margin-bottom: 8px; } }

.key-card { border-color: #f1c40f; background: #fffdf3; }
.empty { padding: 40px 16px; }

/* ---- tabs ---- */
.tabs { background: var(--brand); }
.tabs-inner { max-width: 1100px; margin: 0 auto; padding: 0 8px; display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.tabs-inner::-webkit-scrollbar { display: none; }
.tabs a {
    color: rgba(255, 255, 255, .75); text-decoration: none; white-space: nowrap; font-size: .9rem;
    padding: 8px 10px 10px; border-bottom: 3px solid transparent; display: inline-flex; gap: 6px; align-items: center;
}
.tabs a:hover { color: #fff; }
.tabs a.active { color: #fff; border-bottom-color: #fff; font-weight: 500; }

/* ---- lists ---- */
.row-card { background: #fff; border: 1px solid var(--card-border); border-radius: 14px; padding: 12px 14px; display: flex; align-items: center; gap: 12px; }
.row-card + .row-card { margin-top: 8px; }
.row-title { font-weight: 600; }
.row-sub { font-size: .8rem; color: var(--text-muted); }
.badge-firing { background: #dc3545; }
.event { display: flex; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--card-border); font-size: .9rem; }
.event:last-child { border-bottom: 0; }
.event-icon { flex: none; font-size: 1.1rem; }
.event-msg { white-space: pre-line; }
.code-box { font-size: 2rem; font-weight: 600; letter-spacing: .3em; font-family: ui-monospace, monospace; background: var(--bg); border-radius: 12px; padding: 10px 16px; display: inline-block; }
