/* ============================================================
   iOS-style theme — mobile-first, responsive
   ============================================================ */
:root {
  /* iOS system colors (light) */
  --bg: #f2f2f7;
  --bg-elev: rgba(255, 255, 255, 0.72);
  --card: rgba(255, 255, 255, 0.78);
  --card-border: rgba(255, 255, 255, 0.6);
  --text: #1c1c1e;
  --text-2: #3c3c43;
  --text-3: rgba(60, 60, 67, 0.6);
  --separator: rgba(60, 60, 67, 0.18);
  --blur: saturate(180%) blur(20px);

  /* iOS accent / status */
  --blue: #007aff;
  --green: #34c759;
  --yellow: #ff9f0a;
  --red: #ff3b30;
  --teal: #30b0c7;

  --shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
  --radius: 22px;
  --radius-sm: 16px;

  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --bg-elev: rgba(28, 28, 30, 0.72);
    --card: rgba(28, 28, 30, 0.78);
    --card-border: rgba(255, 255, 255, 0.08);
    --text: #ffffff;
    --text-2: #ebebf5;
    --text-3: rgba(235, 235, 245, 0.6);
    --separator: rgba(84, 84, 88, 0.4);
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
    "Helvetica Neue", "Segoe UI", "Sukhumvit Set", "Noto Sans Thai", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* subtle iOS wallpaper-style gradient blobs */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(60% 40% at 15% 0%, rgba(0, 122, 255, 0.18), transparent 60%),
    radial-gradient(55% 35% at 100% 10%, rgba(52, 199, 89, 0.16), transparent 60%),
    radial-gradient(70% 50% at 50% 100%, rgba(255, 159, 10, 0.10), transparent 60%);
  pointer-events: none;
}

.scroll {
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding:
    calc(var(--safe-t)) calc(16px + var(--safe-r))
    calc(24px + var(--safe-b)) calc(16px + var(--safe-l));
  max-width: 720px;
  margin: 0 auto;
}

/* ---------- pull-to-refresh ---------- */
.ptr {
  height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  transition: height 0.25s ease;
}
.ptr.active { height: 52px; }
.ptr-spinner {
  width: 24px; height: 24px;
  border: 2.5px solid var(--separator);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- hero header ---------- */
.hero { padding: 14px 4px 18px; }
.hero-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.hero h1 {
  font-size: 34px;
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.1;
}
.subtitle {
  margin-top: 6px;
  font-size: 15px;
  color: var(--text-3);
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 6px;
}
.live-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 0 0 rgba(52, 199, 89, 0.6);
  animation: pulse 2s infinite;
}
.live-dot.stale { background: var(--text-3); animation: none; box-shadow: none; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(52, 199, 89, 0.5); }
  70% { box-shadow: 0 0 0 8px rgba(52, 199, 89, 0); }
  100% { box-shadow: 0 0 0 0 rgba(52, 199, 89, 0); }
}

.refresh-btn {
  flex: 0 0 auto;
  width: 44px; height: 44px;
  border: none;
  border-radius: 50%;
  background: var(--card);
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
  border: 0.5px solid var(--card-border);
  color: var(--blue);
  display: grid; place-items: center;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.15s ease;
}
.refresh-btn:active { transform: scale(0.9); }
.refresh-btn.spinning svg { animation: spin 0.8s linear infinite; }

/* ---------- group title ---------- */
.group-title {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--text-3);
  margin: 18px 8px 10px;
}

/* ---------- sensor section ---------- */
.sensor-block { margin-bottom: 8px; }

/* metric card grid: 2-up on mobile, scales up */
.metric-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}
@media (min-width: 560px) {
  .metric-grid { grid-template-columns: repeat(2, 1fr); }
}

.card {
  position: relative;
  background: var(--card);
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
  border: 0.5px solid var(--card-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  overflow: hidden;
  animation: rise 0.45s cubic-bezier(0.22, 1, 0.36, 1) both;
}
@keyframes rise { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

.metric-card { display: flex; flex-direction: column; gap: 10px; min-height: 138px; }
.metric-head { display: flex; align-items: center; justify-content: space-between; }
.metric-icon { font-size: 24px; }
.status-pill {
  font-size: 11px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 100px;
  color: #fff;
  letter-spacing: 0.01em;
}
.metric-value {
  font-size: 42px;
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.metric-value .unit { font-size: 20px; font-weight: 700; color: var(--text-3); margin-left: 2px; }
.metric-label { font-size: 14px; font-weight: 600; color: var(--text-3); }

/* sparkline sits at the bottom of the card, full bleed */
.spark {
  margin: 4px -16px -16px;
  height: 40px;
  width: calc(100% + 32px);
  display: block;
}

/* status accent colors */
.s-green  { background: var(--green); }
.s-yellow { background: var(--yellow); }
.s-red    { background: var(--red); }
.s-blue   { background: var(--blue); }
.s-gray   { background: var(--text-3); }

/* ---------- footer ---------- */
.foot { margin-top: 26px; text-align: center; }
.admin-link {
  display: inline-block;
  font-size: 14px;
  font-weight: 600;
  color: var(--blue);
  text-decoration: none;
  padding: 12px 20px;
  background: var(--card);
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
  border: 0.5px solid var(--card-border);
  border-radius: 100px;
  box-shadow: var(--shadow);
}
.admin-link:active { opacity: 0.6; }
.copyright { margin-top: 14px; font-size: 12px; color: var(--text-3); }

/* ---------- skeleton loading ---------- */
.skeleton-wrap { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.skel-card {
  height: 138px;
  border-radius: var(--radius);
  background: linear-gradient(100deg, var(--card) 30%, rgba(127,127,127,0.12) 50%, var(--card) 70%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border: 0.5px solid var(--card-border);
}
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-b));
  transform: translateX(-50%) translateY(140%);
  background: rgba(40, 40, 42, 0.92);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  padding: 12px 20px;
  border-radius: 100px;
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
  box-shadow: 0 8px 30px rgba(0,0,0,0.3);
  transition: transform 0.35s cubic-bezier(0.22, 1, 0.36, 1);
  z-index: 100;
  max-width: 88vw;
  text-align: center;
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* ---------- drill-down affordance on cards ---------- */
.metric-card { cursor: pointer; transition: transform 0.12s ease; }
.metric-card:active { transform: scale(0.97); }
.drill-chevron {
  position: absolute;
  top: 16px; right: 14px;
  color: var(--text-3);
  opacity: 0.5;
}

/* ---------- card alert states ---------- */
.metric-card.alert-danger {
  border-color: rgba(255, 59, 48, 0.55);
  background-image: linear-gradient(rgba(255, 59, 48, 0.09), rgba(255, 59, 48, 0.09));
  animation: rise 0.45s cubic-bezier(0.22, 1, 0.36, 1) both, pulseRed 1.5s ease-in-out infinite;
}
.metric-card.alert-cool {
  border-color: rgba(0, 122, 255, 0.5);
  background-image: linear-gradient(rgba(0, 122, 255, 0.08), rgba(0, 122, 255, 0.08));
  animation: rise 0.45s cubic-bezier(0.22, 1, 0.36, 1) both, pulseBlue 1.6s ease-in-out infinite;
}
.metric-card.alert-warn {
  border-color: rgba(255, 159, 10, 0.5);
  background-image: linear-gradient(rgba(255, 159, 10, 0.07), rgba(255, 159, 10, 0.07));
}
/* ไอคอนสั่นเล็กน้อยตอน danger */
.metric-card.alert-danger .metric-icon { animation: shake 1.5s ease-in-out infinite; transform-origin: center; }
@keyframes pulseRed {
  0%, 100% { box-shadow: var(--shadow), 0 0 0 0 rgba(255, 59, 48, 0); }
  50% { box-shadow: var(--shadow), 0 0 0 5px rgba(255, 59, 48, 0.18); }
}
@keyframes pulseBlue {
  0%, 100% { box-shadow: var(--shadow), 0 0 0 0 rgba(0, 122, 255, 0); }
  50% { box-shadow: var(--shadow), 0 0 0 5px rgba(0, 122, 255, 0.16); }
}
@keyframes shake {
  0%, 88%, 100% { transform: rotate(0); }
  91% { transform: rotate(-9deg); }
  94% { transform: rotate(9deg); }
  97% { transform: rotate(-6deg); }
}

/* ---------- iOS bottom sheet ---------- */
.sheet-backdrop {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.4);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease;
  z-index: 90;
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}
.sheet-backdrop.show { opacity: 1; pointer-events: auto; }

.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 95;
  max-width: 720px;
  margin: 0 auto;
  background: var(--bg-elev);
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
  border-top-left-radius: 28px;
  border-top-right-radius: 28px;
  border: 0.5px solid var(--card-border);
  box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.25);
  padding: 0 18px calc(24px + var(--safe-b));
  transform: translateY(110%);
  transition: transform 0.42s cubic-bezier(0.22, 1, 0.36, 1);
  max-height: 90vh;
  overflow: hidden;
}
.sheet.show { transform: translateY(0); }

.sheet-grabber { padding: 10px 0 4px; display: flex; justify-content: center; cursor: grab; }
.sheet-grabber span { width: 38px; height: 5px; border-radius: 3px; background: var(--separator); display: block; }

.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 2px 14px; }
.sheet-title { display: flex; align-items: center; gap: 12px; }
.sheet-icon {
  font-size: 26px;
  width: 46px; height: 46px;
  display: grid; place-items: center;
  background: var(--card);
  border-radius: 14px;
  border: 0.5px solid var(--card-border);
}
.sheet-head h2 { font-size: 20px; font-weight: 800; letter-spacing: -0.02em; }
.sheet-sub { font-size: 13px; color: var(--text-3); margin-top: 2px; }
.sheet-close {
  width: 32px; height: 32px;
  border: none; border-radius: 50%;
  background: var(--card);
  border: 0.5px solid var(--card-border);
  color: var(--text-3);
  font-size: 14px; font-weight: 700;
  cursor: pointer;
}
.sheet-close:active { opacity: 0.6; }

/* segmented control (iOS) */
.segmented {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  background: var(--separator);
  border-radius: 10px;
  padding: 2px;
  margin-bottom: 16px;
}
.segmented button {
  border: none;
  background: transparent;
  color: var(--text-2);
  font-size: 13px;
  font-weight: 600;
  padding: 7px 0;
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.2s ease;
}
.segmented button.active {
  background: var(--bg-elev);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

/* stats row */
.stat-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 14px;
}
.stat {
  background: var(--card);
  border: 0.5px solid var(--card-border);
  border-radius: var(--radius-sm);
  padding: 12px;
  text-align: center;
}
.stat-k { display: block; font-size: 12px; color: var(--text-3); font-weight: 600; margin-bottom: 4px; }
.stat-v { display: block; font-size: 22px; font-weight: 800; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }

/* chart */
.chart-wrap {
  position: relative;
  background: var(--card);
  border: 0.5px solid var(--card-border);
  border-radius: var(--radius-sm);
  padding: 14px 8px 10px;
  min-height: 230px;
  overflow: hidden;
}
.chart-wrap svg { width: 100%; height: 210px; display: block; }
.chart-loading {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  background: var(--card);
}
.chart-axis { fill: var(--text-3); font-size: 10px; font-weight: 600; }
.chart-grid { stroke: var(--separator); stroke-width: 1; }
.chart-empty { text-align: center; color: var(--text-3); padding: 80px 0; font-size: 14px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
