:root {
  --bg: #f4f5f4;
  --surface: #ffffff;
  --surface-2: #f8f9f8;
  --text: #1c1d1f;
  --muted: #5f6368;
  --border: #e0e1dd;
  --accent: #1f4e79;
  --accent-soft: #e6edf5;
  --ok: #1e7a46;
  --ok-soft: #e3f3ea;
  --warn: #9a6200;
  --warn-soft: #fbf0d9;
  --bad: #b3261e;
  --bad-soft: #fbe6e4;
  --series-1: #1f4e79;
  --series-2: #b3261e;
  --series-3: #9a6200;
  --font: "IBM Plex Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111213;
    --surface: #1a1b1d;
    --surface-2: #202124;
    --text: #e8e8e6;
    --muted: #a0a3a8;
    --border: #2e3033;
    --accent: #8db8e3;
    --accent-soft: #1d2a38;
    --ok: #6fcf97;
    --ok-soft: #16301f;
    --warn: #e6b450;
    --warn-soft: #33290f;
    --bad: #f28b82;
    --bad-soft: #3a1c1a;
    --series-1: #8db8e3;
    --series-2: #f28b82;
    --series-3: #e6b450;
    color-scheme: dark;
  }
}

html[data-lang="th"] [lang="en"],
html[data-lang="en"] [lang="th"] {
  display: none;
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.6;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

button, input { font: inherit; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

.wrap { max-width: 1240px; margin: 0 auto; padding: 0 16px; }

/* Top bar */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar-inner { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 56px; }
.brand { font-weight: 600; color: var(--text); white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand small { color: var(--muted); font-weight: 400; margin-left: 6px; }
.top-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; justify-content: flex-end; }
.who { color: var(--muted); font-size: 14px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 6px 14px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text); font-size: 14px; cursor: pointer;
}
.btn:hover { border-color: var(--accent); text-decoration: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--surface); font-weight: 500; }
.btn-primary:hover { filter: brightness(1.08); }
.btn[disabled] { opacity: 0.6; cursor: default; }

.lang-toggle { color: var(--muted); }
html[data-lang="th"] .lang-opt[data-opt="th"],
html[data-lang="en"] .lang-opt[data-opt="en"] { color: var(--accent); font-weight: 600; }
.lang-sep { opacity: 0.5; }

/* Auth pages */
.auth { min-height: calc(100vh - 57px); display: grid; place-items: center; padding: 32px 16px; }
.auth-card {
  width: 100%; max-width: 380px; padding: 28px 24px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
}
.auth-card h1 { margin: 0 0 4px; font-size: 1.35rem; font-weight: 600; }
.auth-card .sub { margin: 0 0 20px; color: var(--muted); font-size: 14px; }
.field { display: block; margin-bottom: 14px; }
.field span.label { display: block; margin-bottom: 4px; font-size: 14px; color: var(--muted); }
.field input {
  width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface-2); color: var(--text);
}
.field input:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px var(--accent-soft); }
.auth-card .btn-primary { width: 100%; padding: 10px; margin-top: 6px; }
.msg { margin: 0 0 14px; padding: 8px 12px; border-radius: 6px; font-size: 14px; }
.msg:empty { display: none; }
.msg-bad { background: var(--bad-soft); color: var(--bad); }
.msg-info { background: var(--accent-soft); color: var(--accent); }
.hint { color: var(--muted); font-size: 13px; margin: -6px 0 14px; }

/* Dashboard layout */
.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 24px 0 16px; }
.page-head h1 { margin: 0; font-size: 1.4rem; font-weight: 600; }
.refresh { color: var(--muted); font-size: 13px; }

.layout { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 20px; align-items: start; padding-bottom: 40px; }
@media (max-width: 900px) { .layout { grid-template-columns: minmax(0, 1fr); } }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; }
.panel-pad { padding: 16px 20px; }
.panel h2 { margin: 0 0 12px; font-size: 1.05rem; font-weight: 600; }
.panel h3 { margin: 0 0 10px; font-size: 0.95rem; font-weight: 600; }

/* Run list */
.runs { list-style: none; margin: 0; padding: 6px; max-height: 70vh; overflow-y: auto; }
@media (max-width: 900px) { .runs { max-height: 260px; } }
.run-item {
  display: block; width: 100%; text-align: left; padding: 10px 12px; margin: 0;
  border: 1px solid transparent; border-radius: 8px; background: transparent; color: var(--text); cursor: pointer;
}
.run-item:hover { background: var(--surface-2); }
.run-item[aria-current="true"] { background: var(--accent-soft); border-color: var(--accent); }
.run-item .row1 { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.run-item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.run-item .meta { color: var(--muted); font-size: 13px; }
.empty { color: var(--muted); padding: 16px; margin: 0; }

/* Status pill */
.pill { display: inline-block; padding: 1px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.pill-running { background: var(--ok-soft); color: var(--ok); }
.pill-finished { background: var(--accent-soft); color: var(--accent); }
.pill-stale { background: var(--warn-soft); color: var(--warn); }
.pill-large { font-size: 14px; padding: 3px 14px; }

/* Detail */
.detail-head { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; align-items: flex-start; }
.detail-head h2 { margin: 0; font-size: 1.25rem; }
.detail-head .file { color: var(--muted); font-family: var(--mono); font-size: 12px; overflow-wrap: anywhere; }
.status-line { margin: 12px 0 0; font-size: 14px; }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 4px 20px; margin: 14px 0 0; font-size: 14px; }
.facts dt { color: var(--muted); font-size: 12px; }
.facts dd { margin: 0 0 6px; }

.banner { margin: 16px 0 0; padding: 10px 14px; border-radius: 8px; font-size: 14px; }
.banner-ok { background: var(--ok-soft); color: var(--ok); }
.banner-warn { background: var(--warn-soft); color: var(--warn); }
.banner-bad { background: var(--bad-soft); color: var(--bad); }

.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; margin-top: 16px; }
.kpi { padding: 12px 14px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); }
.kpi .label { color: var(--muted); font-size: 12px; }
.kpi .value { font-size: 1.45rem; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1.3; }
.kpi .note { color: var(--muted); font-size: 12px; }
.kpi.bad .value { color: var(--bad); }
.kpi.warn .value { color: var(--warn); }

.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 20px; margin-top: 20px; }

/* Charts */
.chart { width: 100%; height: 200px; display: block; }
.chart .axis { stroke: var(--border); }
.chart .tick { fill: var(--muted); font-size: 11px; font-family: var(--font); }
.chart .line { fill: none; stroke-width: 2; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 6px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 5px; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 6px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 500; font-size: 12px; position: sticky; top: 0; background: var(--surface); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.mono { font-family: var(--mono); font-size: 12px; }
.events-scroll { max-height: 420px; overflow-y: auto; }
.kind { display: inline-block; padding: 0 8px; border-radius: 4px; font-size: 12px; font-weight: 500; background: var(--surface-2); }
.kind-slow { background: var(--warn-soft); color: var(--warn); }
.kind-no_reply, .kind-reboot, .kind-link_lost, .kind-clear_failed, .kind-write_failed { background: var(--bad-soft); color: var(--bad); }
.kind-start, .kind-stop, .kind-sim_start, .kind-sim_stop { background: var(--accent-soft); color: var(--accent); }

.footer { color: var(--muted); font-size: 12px; padding-top: 0; padding-bottom: 32px; }

.mt-16 { margin-top: 16px; }
.events-hint { margin: -6px 0 10px; }

/* Password fields: show/hide button inside the input */
.pw-wrap { position: relative; display: block; }
.pw-wrap input { padding-right: 64px; }
.pw-eye {
  position: absolute; top: 50%; right: 6px; transform: translateY(-50%);
  padding: 2px 8px; border: 0; border-radius: 4px; background: transparent;
  color: var(--accent); font-size: 13px; cursor: pointer;
}
.pw-eye:hover { background: var(--accent-soft); }

.auth-foot { margin: 16px 0 0; color: var(--muted); font-size: 13px; text-align: center; }

/* Strength meter: four segments */
.strength { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; margin: -6px 0 4px; }
.strength .seg { height: 6px; border-radius: 3px; background: var(--border); transition: background 0.2s; }
.strength.level-bad .seg:nth-child(1) { background: var(--bad); }
.strength.level-1 .seg:nth-child(-n+1) { background: var(--bad); }
.strength.level-2 .seg:nth-child(-n+2) { background: var(--warn); }
.strength.level-3 .seg:nth-child(-n+3) { background: var(--ok); }
.strength.level-4 .seg { background: var(--ok); }
.strength-label { margin: 0 0 12px; font-size: 13px; color: var(--muted); min-height: 1.6em; }

/* Rules checklist */
.tips { list-style: none; margin: 0 0 16px; padding: 10px 12px; border-radius: 6px; background: var(--surface-2); font-size: 13px; }
.tips li { position: relative; padding-left: 20px; margin: 3px 0; color: var(--muted); }
.tips li[data-rule]::before { content: ""; position: absolute; left: 4px; top: 0.6em; width: 7px; height: 7px; border-radius: 50%; background: var(--border); }
.tips li.ok { color: var(--ok); }
.tips li.ok::before { background: var(--ok); }
.tips li.bad { color: var(--bad); }
.tips li.bad::before { background: var(--bad); }
.tips li.tip-advice { padding-left: 0; margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border); }

.match { margin: -8px 0 14px; font-size: 13px; min-height: 1.6em; }
.match.ok { color: var(--ok); }
.match.bad { color: var(--bad); }

/* Fleet: one list entry and an overview card for cabinets sharing a stamp */
.run-item.run-fleet .name { font-weight: 600; }
.fleet-meta { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.fleet-counts { display: flex; gap: 6px; flex-wrap: wrap; }
.pill-problem { background: var(--bad-soft); color: var(--bad); }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 8px; margin-top: 16px; }
.tile {
  display: block; width: 100%; text-align: left; padding: 9px 12px; cursor: pointer;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); font: inherit;
}
.tile:hover { border-color: var(--accent); }
.tile-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.tile-name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile-dot { flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.tile-line { display: block; font-size: 12px; color: var(--muted); margin-top: 2px; }
.tile-sev-0 { border-color: var(--bad); background: var(--bad-soft); }
.tile-sev-0 .tile-dot { background: var(--bad); }
.tile-sev-0 .tile-line { color: var(--bad); }
.tile-sev-1 { border-color: var(--warn); background: var(--warn-soft); }
.tile-sev-1 .tile-dot { background: var(--warn); }
.tile-sev-1 .tile-line { color: var(--warn); }
.tile-sev-3 .tile-dot { background: var(--accent); }
.tile-more { border-style: dashed; text-align: center; }
.tile-more .tile-line { margin: 0; color: var(--accent); }
.tiles-hint { margin: 10px 0 0; }
.back-link { margin-bottom: 12px; }

/* Account menu in the top bar: name button, opens change password / sign out */
.account { position: relative; }
.account > summary { list-style: none; max-width: 180px; }
.account > summary::-webkit-details-marker { display: none; }
.account .who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); font-size: 14px; }
.account .caret { flex: none; width: 0; height: 0; border-left: 4px solid transparent; border-right: 4px solid transparent; border-top: 5px solid var(--muted); }
.account[open] .caret { transform: rotate(180deg); }
.account-menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 20; min-width: 190px; padding: 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}
.account-menu a, .account-menu button {
  display: block; width: 100%; padding: 8px 10px; border: 0; border-radius: 6px; background: transparent;
  color: var(--text); font: inherit; font-size: 14px; text-align: left; cursor: pointer;
}
.account-menu a:hover, .account-menu button:hover { background: var(--surface-2); text-decoration: none; }
.account-menu button { color: var(--bad); }
.topbar-inner { flex-wrap: nowrap; }
.top-actions { flex-wrap: nowrap; gap: 8px; }
@media (max-width: 420px) {
  .brand { font-size: 15px; }
  .account > summary { max-width: 120px; }
}

/* Run list: "show more" under the first entries */
.list-more {
  display: block; width: 100%; margin-top: 2px; padding: 8px 12px; border: 0; border-radius: 8px;
  background: transparent; color: var(--accent); font: inherit; font-size: 14px; text-align: center; cursor: pointer;
}
.list-more:hover { background: var(--surface-2); }
