/* Don't let your boss kill you back — เว็บแอปออนไลน์ · WCAG 2.2 AA (คอนทราสต์ตรวจแล้วทั้ง 2 ธีม) */
:root {
  --fs: 1;
  --bg: #f3f5f8; --surface: #ffffff; --surface-2: #eef2f6; --text: #18212c; --muted: #4a5565;
  --line: #d5dce5; --field: #6b7685; --primary: #1f5fbf; --on-primary: #ffffff; --focus: #1f5fbf;
  --good: #1d7a3b; --good-bg: #e4f4e9; --bad: #b3261e; --bad-bg: #fde8e6; --warn-bg: #fff3d1; --warn: #5c4300;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 2px 8px rgba(16, 24, 40, .06);
  --radius: 16px;
}
[data-theme="dark"] {
  --bg: #0f141a; --surface: #18202a; --surface-2: #222c38; --text: #e8edf3; --muted: #b6c0cc;
  --line: #2e3a48; --field: #8b97a7; --primary: #8ab6ff; --on-primary: #0b1424; --focus: #8ab6ff;
  --good: #74d394; --good-bg: #173726; --bad: #ff8f86; --bad-bg: #3d1a18; --warn-bg: #3a2f10; --warn: #ffe29a;
  --shadow: none;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { font-size: calc(100% * var(--fs)); -webkit-text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100vh; background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans Thai", "Leelawadee UI", Tahoma, sans-serif;
  line-height: 1.55; padding-bottom: 5.5rem;
}
h1, h2 { line-height: 1.3; margin: 0 0 .5rem; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
p { margin: .25rem 0; }
a { color: var(--primary); text-underline-offset: 3px; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 6px; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip { position: absolute; left: .5rem; top: -4rem; background: var(--primary); color: var(--on-primary); padding: .75rem 1rem; border-radius: 8px; z-index: 50; }
.skip:focus { top: .5rem; }
.center { text-align: center; }
.hint { color: var(--muted); font-size: .9rem; }
.err { color: var(--bad); font-weight: 600; min-height: 0; }
.err:empty { display: none; }
.ok-msg { color: var(--good); font-weight: 600; }
.ok-msg:empty { display: none; }

/* ---------- หัว ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between; gap: .75rem;
  padding: .6rem 1rem; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: .6rem; min-width: 0; }
.logo { width: 34px; height: 34px; flex: none; fill: var(--primary); stroke: var(--primary); }
.app-name { font-weight: 700; margin: 0; font-size: 1rem; line-height: 1.25; }
.conn { display: flex; align-items: center; gap: .4rem; font-size: .85rem; color: var(--muted); margin: 0; }
.dot { width: .65rem; height: .65rem; border-radius: 50%; background: var(--muted); flex: none; }
.conn.on .dot { background: var(--good); }
.conn.off .dot { background: var(--bad); }
.icon-btn {
  min-width: 44px; min-height: 44px; border-radius: 50%; border: 2px solid var(--primary); background: transparent;
  color: var(--primary); font-weight: 800; font-size: 1.1rem; cursor: pointer;
}
.icon-btn.small { min-width: 44px; min-height: 44px; font-size: 1rem; font-style: italic; font-family: Georgia, serif; }

.banner {
  display: flex; gap: .6rem; align-items: center; margin: .75rem auto 0; max-width: 40rem; width: calc(100% - 2rem);
  background: var(--bad-bg); color: var(--bad); border: 2px solid var(--bad); border-radius: 12px; padding: .7rem 1rem;
}
.banner[hidden], .net-banner[hidden] { display: none; }
.banner-icon { font-size: 1.3rem; }
.net-banner { margin: .75rem auto 0; max-width: 40rem; width: calc(100% - 2rem); background: var(--warn-bg); color: var(--warn); border-radius: 12px; padding: .6rem 1rem; font-weight: 600; }

main { max-width: 40rem; margin: 0 auto; padding: 1rem; outline: none; }
.view[hidden] { display: none; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.1rem; margin-bottom: 1rem; box-shadow: var(--shadow); }
.card.warn { background: var(--warn-bg); color: var(--warn); border-color: var(--warn); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }

/* ---------- ปุ่ม ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem; min-height: 44px; padding: .55rem 1.1rem;
  border-radius: 999px; border: 2px solid var(--field); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.danger { border-color: var(--bad); color: var(--bad); }
.btn.ghost { border-color: transparent; color: var(--primary); text-decoration: underline; text-underline-offset: 3px; }
.btn.block { width: 100%; margin-top: .75rem; }
.btn[disabled] { opacity: .6; cursor: progress; }
.btn-row { display: flex; flex-wrap: wrap; gap: .6rem; margin-top: .75rem; }
.round-btn {
  width: 52px; height: 52px; border-radius: 50%; border: 2px solid var(--primary); background: var(--surface);
  color: var(--primary); display: grid; place-items: center; cursor: pointer; flex: none;
}
.round-btn svg { width: 24px; height: 24px; fill: currentColor; }

/* ---------- จับคู่ ---------- */
.pair-card { margin-top: 1rem; }
label { font-weight: 600; display: block; margin: .6rem 0 .25rem; }
input[type="text"], input[type="password"], input[type="number"], select {
  width: 100%; min-height: 48px; padding: .55rem .8rem; font: inherit; color: var(--text); background: var(--surface);
  border: 2px solid var(--field); border-radius: 10px;
}
#pair-code { font-family: ui-monospace, Consolas, monospace; font-size: 1.4rem; letter-spacing: .12em; text-transform: uppercase; text-align: center; }
[aria-invalid="true"] { border-color: var(--bad) !important; }

/* ---------- สถานะ ---------- */
.status-head { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; background: var(--surface-2); border-radius: 12px; padding: .6rem .8rem; }
.status-card.bad .status-head { background: var(--bad-bg); }
.status-card.good .status-head { background: var(--good-bg); }
.bell { font-size: 1.3rem; }
.posture-name { font-weight: 700; font-size: 1.15rem; margin: 0; flex: 1; min-width: 10rem; }
.status-card.bad .posture-name { color: var(--bad); }
.status-card.good .posture-name { color: var(--good); }
.badge { font-size: .85rem; font-weight: 700; padding: .15rem .6rem; border-radius: 999px; border: 2px solid currentColor; }
.badge:empty { display: none; }
.status-card.bad .badge { color: var(--bad); }
.status-card.good .badge { color: var(--good); }
.status-body { display: grid; grid-template-columns: 1fr 1.2fr; gap: 1rem; align-items: center; margin-top: .75rem; }
.label { color: var(--muted); font-size: .9rem; }
.dist-val { font-size: 2.2rem; font-weight: 800; line-height: 1.1; }
.chip { display: inline-block; font-size: .85rem; font-weight: 700; padding: .15rem .6rem; border-radius: 999px; margin-top: .3rem; }
.chip:empty { display: none; }
.chip.ok { background: var(--good-bg); color: var(--good); }
.chip.no { background: var(--bad-bg); color: var(--bad); }
.figure { margin: 0; background: var(--surface-2); border-radius: 12px; padding: .5rem; text-align: center; }
#pose-svg { width: 100%; max-width: 220px; height: auto; display: block; margin: 0 auto; }
.pose-cap { font-weight: 700; font-size: .95rem; }
.status-card.bad .pose-cap { color: var(--bad); }
.status-card.good .pose-cap { color: var(--good); }
.facts { display: grid; grid-template-columns: repeat(3, 1fr); gap: .5rem; margin: .9rem 0 0; }
.facts div { background: var(--surface-2); border-radius: 10px; padding: .45rem .6rem; }
.facts dt { font-size: .8rem; color: var(--muted); }
.facts dd { margin: 0; font-weight: 700; overflow-wrap: break-word; }

/* ---------- relax ---------- */
.relax-row { display: flex; align-items: center; gap: .75rem; margin-top: .5rem; flex-wrap: wrap; }
.timer {
  flex: 1 1 8rem; min-width: 0; margin: 0; font-size: clamp(1.8rem, 9vw, 2.4rem); font-weight: 800; text-align: center; font-variant-numeric: tabular-nums;
  border: 2px solid var(--field); border-radius: 14px; padding: .3rem .5rem;
}

/* ---------- สถิติ ---------- */
.seg { display: inline-flex; border: 2px solid var(--primary); border-radius: 999px; overflow: hidden; margin-bottom: 1rem; }
.seg.small { margin: 0; }
.seg-btn { min-height: 44px; min-width: 44px; padding: .4rem 1rem; border: 0; background: transparent; color: var(--primary); font: inherit; font-weight: 700; cursor: pointer; }
.seg-btn + .seg-btn { border-left: 2px solid var(--primary); }
.seg-btn[aria-pressed="true"] { background: var(--primary); color: var(--on-primary); }
.legend { display: flex; gap: 1rem; flex-wrap: wrap; font-size: .9rem; margin-bottom: .4rem; }
.legend > span { display: inline-flex; align-items: center; gap: .35rem; }
.sw { width: 1.1rem; height: 1.1rem; border-radius: 3px; display: inline-block; border: 2px solid; }
.sw.good { background: var(--good); border-color: var(--good); }
.sw.bad { border-color: var(--bad); background: repeating-linear-gradient(45deg, var(--bad) 0 3px, transparent 3px 6px); }
.chart-wrap svg { width: 100%; height: auto; display: block; }
.chart-wrap text { fill: var(--muted); font-size: 11px; font-family: inherit; }
.day-btns { display: grid; grid-template-columns: repeat(7, 1fr); gap: .25rem; margin-top: .3rem; }
.day-btns[hidden] { display: none; }
.day-btn { min-height: 44px; border-radius: 10px; border: 2px solid var(--line); background: var(--surface); color: var(--text); font: inherit; font-weight: 700; cursor: pointer; padding: 0; }
.day-btn[aria-pressed="true"] { border-color: var(--primary); color: var(--primary); }
.tip { margin-top: .5rem; font-weight: 600; }
.tip:empty { display: none; }
.table-toggle { margin-top: .6rem; }
.table-toggle summary { min-height: 44px; display: flex; align-items: center; cursor: pointer; color: var(--primary); font-weight: 600; }
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: .9rem; }
th, td { text-align: left; padding: .4rem .5rem; border-bottom: 1px solid var(--line); white-space: nowrap; }
.day-list { list-style: none; margin: 0; padding: 0; }
.day-list li { display: flex; justify-content: space-between; gap: .5rem; flex-wrap: wrap; padding: .55rem 0; border-bottom: 1px solid var(--line); }
.day-list li:last-child { border-bottom: 0; }
.day-list .big { font-weight: 700; }
.day-list .sub { color: var(--muted); font-size: .85rem; width: 100%; }

/* ---------- ตั้งค่า ---------- */
.row { display: flex; align-items: center; justify-content: space-between; gap: .75rem; padding: .45rem 0; flex-wrap: wrap; }
.row-label { font-weight: 600; }
.switch {
  position: relative; width: 60px; height: 34px; min-height: 44px; border: 0; background: transparent; cursor: pointer; padding: 0; flex: none;
}
.switch span { position: absolute; left: 0; top: 5px; width: 60px; height: 34px; border-radius: 999px; background: var(--surface-2); border: 2px solid var(--field); transition: background .2s; }
.switch span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: var(--field); transition: transform .2s; }
.switch[aria-checked="true"] span { background: var(--primary); border-color: var(--primary); }
.switch[aria-checked="true"] span::after { transform: translateX(26px); background: var(--on-primary); }
.field { margin: .4rem 0; border: 0; padding: 0; }
fieldset.field legend { font-weight: 600; padding: 0; margin-top: .6rem; }
.pair-inputs { display: grid; grid-template-columns: auto 1fr auto 1fr; gap: .5rem; align-items: center; }
.pair-inputs label { margin: 0; }
.check { display: flex; gap: .6rem; align-items: flex-start; margin: .7rem 0; }
.check input { width: 24px; height: 24px; margin: .25rem 0 0; flex: none; accent-color: var(--primary); }
.check label { margin: 0; font-weight: 500; }

/* ---------- เมนูล่าง ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: grid; grid-template-columns: repeat(3, 1fr);
  background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabbar[hidden] { display: none; }
.tab { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .1rem; min-height: 60px; color: var(--muted); text-decoration: none; font-size: .85rem; font-weight: 600; border-top: 3px solid transparent; }
.tab svg { width: 24px; height: 24px; fill: currentColor; }
.tab[aria-current="page"] { color: var(--primary); border-top-color: var(--primary); }

/* ---------- หน้าต่าง ---------- */
dialog { border: 0; border-radius: var(--radius); padding: 1.25rem; width: min(30rem, calc(100% - 2rem)); background: var(--surface); color: var(--text); box-shadow: 0 10px 40px rgba(0, 0, 0, .35); }
dialog::backdrop { background: rgba(10, 15, 20, .55); }
.steps { padding-left: 1.4rem; }
.steps li { margin-bottom: .5rem; }
.toast { position: fixed; left: 50%; bottom: 5.5rem; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: .6rem 1rem; border-radius: 12px; max-width: calc(100% - 2rem); font-weight: 600; z-index: 30; }
.toast:empty { display: none; }

@media (max-width: 420px) {
  .status-body { grid-template-columns: 1fr; }
  .facts { grid-template-columns: 1fr 1fr; }
  .pair-inputs { grid-template-columns: auto 1fr; }
  .dist-val { font-size: 1.9rem; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
.row-label { display: inline-flex; align-items: center; gap: .5rem; }
.row-ic { width: 22px; height: 22px; fill: var(--primary); flex: none; }
.row-ic.m0 { fill: currentColor; }
.btn-row.tight { margin-top: 0; }
#pose-svg circle, #pose-svg path { transition: none; }
.chart-wrap .pt { cursor: pointer; }
@media (max-width: 360px) { .facts { grid-template-columns: 1fr; } }

/* ================= v5 · ดีไซน์ (Apple HIG / Material 3 Expressive / WCAG 2.2) ================= */
:root { --radius: 22px; --shadow: 0 10px 24px rgba(36, 71, 181, .10), 0 2px 4px rgba(16, 24, 40, .06), inset 0 1px 0 rgba(255, 255, 255, .9); }
[data-theme="dark"] { --shadow: 0 8px 22px rgba(0, 0, 0, .35); }
.logo { width: 40px; height: 40px; border-radius: 12px; }
.card h2 { font-size: 1.1rem; }
.btn.primary { box-shadow: 0 6px 14px rgba(31, 95, 191, .28); }
.cfg-banner { margin: .75rem auto 0; max-width: 40rem; width: calc(100% - 2rem); background: var(--warn-bg); color: var(--warn); border: 2px solid var(--warn); border-radius: 12px; padding: .7rem 1rem; font-weight: 700; }
.warn-text { color: var(--warn); background: var(--warn-bg); border-radius: 10px; padding: .45rem .7rem; }
.row-label > span:not([id]) { display: flex; flex-direction: column; }
.row-title { font-weight: 600; }
.sub { color: var(--muted); font-size: .85rem; font-weight: 400; }
.settings-form { display: contents; }

/* ================= คอม / จอกว้าง ≥ 1024px: เมนูข้าง + 2 คอลัมน์ ================= */
@media (min-width: 1024px) {
  body { padding-bottom: 2rem; padding-left: 240px; }
  .topbar { margin-left: -240px; min-height: 68px; padding: .8rem 1.5rem; z-index: 12; }
  .tabbar {
    top: 68px; bottom: 0; right: auto; width: 240px; z-index: 11; grid-template-columns: 1fr; grid-auto-rows: min-content; align-content: start;
    gap: .35rem; padding: 1.25rem .9rem 1rem; border-top: 0; border-right: 1px solid var(--line);
  }
  body:has(#tabbar[hidden]) { padding-left: 0; }
  body:has(#tabbar[hidden]) .topbar { margin-left: 0; }
  .tab { flex-direction: row; justify-content: flex-start; gap: .75rem; min-height: 52px; padding: 0 1rem; border-top: 0; border-radius: 14px; font-size: 1rem; }
  .tab[aria-current="page"] { background: var(--surface-2); box-shadow: inset 4px 0 0 var(--primary); }
  .tab:hover { background: var(--surface-2); color: var(--text); }
  main { max-width: 74rem; padding: 1.5rem 2rem; }
  .banner, .net-banner, .cfg-banner { max-width: 74rem; width: calc(100% - 4rem); }
  .toast { bottom: 1.5rem; }
  #view-home:not([hidden]) { display: grid; grid-template-columns: minmax(0, 1.45fr) minmax(0, 1fr); gap: 0 1.5rem; align-items: start; }
  #view-home .status-card { grid-row: span 3; }
  #view-home .dist-val { font-size: 3rem; }
  #view-home #pose-svg { max-width: 300px; }
  #view-stats:not([hidden]) { display: grid; grid-template-columns: minmax(0, 1.7fr) minmax(0, 1fr); gap: 0 1.5rem; align-items: start; }
  #view-stats > h1, #view-stats > .seg { grid-column: 1 / -1; justify-self: start; }
  #view-settings:not([hidden]) { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 1.5rem; align-items: start; }
  #view-settings > h1, #view-settings > p.center { grid-column: 1 / -1; }
  #view-pair .pair-card { max-width: 34rem; margin: 2rem auto 1rem; }
}
