/* ================= FONTS ================= */
@font-face {
  font-family: "Ethnocentric";
  src: url("../font/ethnocentric.italic.ttf") format("truetype");
  font-weight: normal;
  font-style: normal;
}

@font-face {
  font-family: "IBM Plex Sans Thai";
  src: url("../font/IBMPlexSansThai-Regular.ttf") format("truetype");
  font-weight: 400;
  font-style: normal;
}

@font-face {
  font-family: "IBM Plex Sans Thai";
  src: url("../font/IBMPlexSansThai-Medium.ttf") format("truetype");
  font-weight: 500;
  font-style: normal;
}

@font-face {
  font-family: "JetBrains Mono";
  src: url("../font/JetBrainsMono-Regular.ttf") format("truetype");
  font-weight: 400;
  font-style: normal;
}

@font-face {
  font-family: "JetBrains Mono";
  src: url("../font/JetBrainsMono-Bold.ttf") format("truetype");
  font-weight: 700;
  font-style: normal;
}

/* ================= THEME TOKENS ================= */
:root {
  --cyan: #00ffff;
  --green: #00ff55;
  --bg: #05080b;
  --line: rgba(0, 255, 255, 0.14);
  --panel: rgba(0, 255, 170, 0.045);
  --ink: #7fa6ab;
  --font-display: 'Ethnocentric', sans-serif;
  --font-mono: 'JetBrains Mono', monospace;
  --font-thai: 'IBM Plex Sans Thai', sans-serif;
}

/* ================= RESET / BASE ================= */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  background: var(--bg);
}

#app {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  color: var(--ink);
  font-family: var(--font-mono);
  background: var(--bg);
}

/* พื้นหลัง: รูป bg เบลอ (ขยายเกินขอบ กัน blur ขอบจาง) */
#app::before {
  content: "";
  position: absolute;
  inset: -24px;
  background: url('../images/bg.jpg') center / cover no-repeat;
  filter: blur(2px) brightness(0.75) saturate(1.1);
  transform: scale(1.08);
  z-index: 0;
  pointer-events: none;
}

/* ม่านโปร่งใส (frosted glass) ทับรูปเบลออีกชั้น */
#app::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(6, 10, 14, 0.4);
  z-index: 1;
  pointer-events: none;
}

/* ================= ICONS ================= */
.ico {
  flex: none;
  display: block;
  width: 18px;
  height: 18px;
  stroke-width: 2;
}

/* ================= TOP BAR ================= */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: clamp(10px, 2.4vh, 18px) clamp(14px, 3vw, 28px);
  border-bottom: 1px solid var(--line);
  z-index: 2;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand .ico {
  width: clamp(22px, 4vw, 28px);
  height: clamp(22px, 4vw, 28px);
  color: var(--green);
  filter: drop-shadow(0 0 6px rgba(0, 255, 85, 0.5));
}

.brand-name {
  font-family: var(--font-display);
  font-size: clamp(12px, 2.3vw, 17px);
  letter-spacing: 1px;
  color: #eafcff;
  white-space: nowrap;
}

.conn {
  display: flex;
  align-items: center;
  gap: clamp(8px, 1.5vw, 12px);
}

#deviceSuffix {
  width: clamp(76px, 11vw, 100px);
  padding: 7px 10px;
  background: rgba(0, 255, 255, 0.06);
  color: var(--cyan);
  border: 1.5px solid var(--line);
  border-radius: 8px;
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 1px;
  text-transform: uppercase;
  outline: none;
  transition: border-color .2s, box-shadow .2s;
}

#deviceSuffix:focus {
  border-color: var(--cyan);
  box-shadow: 0 0 10px rgba(0, 255, 255, 0.35);
}

#deviceSuffix::placeholder {
  color: rgba(0, 255, 255, 0.4);
  letter-spacing: 0;
  text-transform: none;
  font-family: var(--font-thai);
}

#btnConnect {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 15px;
  background: rgba(0, 255, 255, 0.06);
  color: var(--cyan);
  border: 1.5px solid var(--cyan);
  border-radius: 8px;
  font-family: var(--font-display);
  font-size: 12px;
  letter-spacing: 1px;
  text-transform: uppercase;
  cursor: pointer;
  box-shadow: 0 0 12px rgba(0, 255, 255, 0.25);
  transition: color .25s, border-color .25s, background .25s, box-shadow .25s;
}

#btnConnect .ico {
  width: 16px;
  height: 16px;
}

#btnConnect:not(.connected) {
  animation: btnpulse 2.4s ease-in-out infinite;
}

#btnConnect.connected {
  color: var(--green);
  border-color: var(--green);
  background: rgba(0, 255, 85, 0.08);
  box-shadow: 0 0 16px rgba(0, 255, 85, 0.45);
}

/* hidden, but JS still writes the connected device name here */
#device-name {
  display: none;
}

/* ================= HERO : LAST LAP ================= */
.hero {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(4px, 1.6vh, 14px);
  z-index: 2;
}

.hero-label {
  font-family: var(--font-display);
  font-size: clamp(11px, min(2.6vw, 4.2vh), 20px);
  letter-spacing: clamp(4px, 1.4vw, 9px);
  padding-left: clamp(4px, 1.4vw, 9px);
  color: var(--cyan);
  text-transform: uppercase;
  text-shadow: 0 0 12px rgba(0, 255, 255, 0.5);
}

.hero-time {
  font-family: var(--font-display);
  font-size: clamp(44px, min(13.8vw, 32vh), 140px);
  line-height: 0.9;
  letter-spacing: -2px;
  color: var(--green);
  font-variant-numeric: tabular-nums;
  text-shadow: 0 0 30px rgba(0, 255, 85, 0.45), 0 0 80px rgba(0, 255, 85, 0.18);
}

/* ================= STATS ================= */
.stats {
  display: flex;
  gap: clamp(8px, 1.6vw, 16px);
  padding: clamp(8px, 1.8vh, 16px) clamp(14px, 3vw, 28px) clamp(14px, 3vh, 24px);
  z-index: 2;
}

.stat {
  flex: 1;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: clamp(8px, 1.6vh, 14px) clamp(8px, 1.4vw, 15px);
  display: flex;
  flex-direction: column;
  gap: clamp(4px, 1vh, 8px);
}

.stat-head {
  display: flex;
  align-items: center;
  gap: 7px;
}

.stat .ico {
  width: clamp(13px, 2.2vw, 16px);
  height: clamp(13px, 2.2vw, 16px);
  color: var(--cyan);
  opacity: 0.85;
}

.stat-label {
  font-size: clamp(8px, 1.4vw, 11px);
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--cyan);
  opacity: 0.7;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stat-value {
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: clamp(15px, min(4.4vw, 6.5vh), 34px);
  line-height: 1;
  color: var(--green);
  font-variant-numeric: tabular-nums;
}

/* accent: cyan = counters/clock, green = lap performance */
.stat.cyan .stat-value {
  color: var(--cyan);
}

.stat.best {
  border-color: rgba(0, 255, 85, 0.4);
  box-shadow: inset 0 0 22px rgba(0, 255, 85, 0.1);
}

.stat.best .ico {
  color: var(--green);
  opacity: 1;
}

/* ================= ROTATE HINT (portrait only) ================= */
.rotate-hint {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 50;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  background: var(--bg);
  color: var(--cyan);
  text-align: center;
  font-family: var(--font-thai);
  font-size: 16px;
  line-height: 1.6;
  padding: 24px;
}

.rotate-hint .ico {
  width: 60px;
  height: 60px;
  color: var(--green);
  animation: rotatehint 2.2s ease-in-out infinite;
}

@media (orientation: portrait) {
  .rotate-hint {
    display: flex;
  }
}

/* ================= PAGE-LOAD MOTION ================= */
.topbar {
  animation: rise .55s ease both;
}

.hero {
  animation: rise .55s ease .08s both;
}

.stats {
  animation: rise .55s ease .16s both;
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(14px);
  }

  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes btnpulse {

  0%,
  100% {
    box-shadow: 0 0 10px rgba(0, 255, 255, 0.2);
  }

  50% {
    box-shadow: 0 0 20px rgba(0, 255, 255, 0.5);
  }
}

@keyframes rotatehint {

  0%,
  100% {
    transform: rotate(0);
  }

  50% {
    transform: rotate(-90deg);
  }
}

/* ================= RESPONSIVE ================= */
/* จอเตี้ยมาก (มือถือแนวนอนตัวเล็ก) → บีบ padding/gap ให้พอดี ไม่ล้น */
@media (max-height: 400px) {
  .topbar { padding-top: 6px; padding-bottom: 6px; }
  .toolbar { padding-top: 4px; padding-bottom: 4px; }
  .hero { gap: 2px; }
  .stats { padding-top: 6px; padding-bottom: 8px; }
  .stat { padding: 6px 10px; gap: 3px; }
}

/* จอใหญ่ (แท็บเล็ตแนวนอน/เดสก์ท็อป) → ขยายตัวเลขให้สมส่วนกับจอ */
@media (min-width: 1100px) and (min-height: 600px) {
  .hero-label { font-size: clamp(20px, 2.2vw, 30px); }
  .hero-time { font-size: clamp(140px, 14.5vw, 208px); }
  .stat-value { font-size: clamp(34px, 3.4vw, 52px); }
}

/* ================= ALERT TOAST (center) ================= */
#alert-toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.92);
  background: rgba(8, 12, 18, 0.92);
  color: var(--cyan);
  padding: 20px 30px;
  border: 2px solid var(--cyan);
  border-radius: 10px;
  font-family: var(--font-thai);
  font-size: 15px;
  line-height: 1.5;
  text-align: center;
  max-width: 80vw;
  z-index: 1000;
  box-shadow: 0 0 25px rgba(0, 255, 255, 0.5),
              0 0 60px rgba(0, 255, 255, 0.15);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}

#alert-toast.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

#alert-toast.error {
  color: #ff6666;
  border-color: #ff3333;
  box-shadow: 0 0 25px rgba(255, 51, 51, 0.5),
              0 0 60px rgba(255, 51, 51, 0.15);
}

/* ================= LAP DELTA FLASH (flash light สไตล์ ProductGaugeFiBT) ================= */
/* ขอบจอเรืองแสง กลางจอใส → ยังอ่านเวลาได้ | เขียว = เร็วกว่ารอบที่แล้ว, แดง = ช้ากว่า */
#lap-flash {
  --flash-rgb: 0, 255, 85;
  position: fixed;
  inset: 0;
  z-index: 40;
  pointer-events: none;
  opacity: 0;
  background: radial-gradient(ellipse at center,
      rgba(var(--flash-rgb), 0) 42%,
      rgba(var(--flash-rgb), 0.28) 80%,
      rgba(var(--flash-rgb), 0.55) 100%);
  box-shadow: inset 0 0 120px 24px rgba(var(--flash-rgb), 0.55);
}

#lap-flash.faster { --flash-rgb: 0, 255, 85; }    /* เร็วกว่ารอบที่แล้ว = เขียว */
#lap-flash.slower { --flash-rgb: 255, 45, 45; }   /* ช้ากว่ารอบที่แล้ว = แดง */

/* กระพริบ 3 ครั้งแล้วหยุด (ProductGaugeFiBT ใช้ infinite สำหรับเตือนรอบ, อันนี้เตือนรอบเดียวจบ) */
#lap-flash.blink { animation: lapFlash 0.42s steps(1, end) 3; }

@keyframes lapFlash {
  0%, 49%   { opacity: 0; }
  50%, 100% { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  #lap-flash.blink { animation: none; opacity: 0.85; }
}

/* ================= TOOLBAR (ใต้ header) ================= */
/* ปุ่มลอยมุมบนของโซน hero — ไม่กินพื้นที่แนวตั้ง (เลขกลางขยับขึ้นได้เต็ม) */
.toolbar {
  position: absolute;
  top: clamp(8px, 1.5vh, 14px);
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;   /* STATS ซ้าย / CLEAR(start) ขวา */
  padding: 0 clamp(14px, 3vw, 28px);
  pointer-events: none;             /* ให้คลิกทะลุพื้นที่ว่างตรงกลางได้ */
  z-index: 3;
}

.toolbar .icon-btn { pointer-events: auto; }

.icon-btn {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: clamp(34px, 6vw, 42px);
  height: clamp(34px, 6vw, 42px);
  background: rgba(0, 255, 255, 0.06);
  color: var(--cyan);
  border: 1.5px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  transition: border-color .2s, box-shadow .2s, background .2s;
}

.icon-btn:hover {
  border-color: var(--cyan);
  box-shadow: 0 0 10px rgba(0, 255, 255, 0.3);
}

.icon-btn .ico {
  width: 20px;
  height: 20px;
}

/* ================= LAP STATS PANEL ================= */
#stats-panel {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(12px, 3vh, 28px);
  background: rgba(3, 6, 9, 0.72);
  backdrop-filter: blur(3px);
}

#stats-panel.hidden { display: none; }

.stats-card {
  width: min(560px, 92vw);
  max-height: 86vh;
  display: flex;
  flex-direction: column;
  background: #0a1014;
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 0 40px rgba(0, 255, 255, 0.12);
  overflow: hidden;
  animation: rise .3s ease both;
}

.stats-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 18px;
  border-bottom: 1px solid var(--line);
}

.stats-title {
  font-family: var(--font-display);
  font-size: 15px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--cyan);
}

.stats-body {
  overflow-y: auto;
  padding: 4px 0;
}

.lap-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-mono);
}

.lap-table th {
  position: sticky;
  top: 0;
  background: #0a1014;
  padding: 9px 18px;
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  text-align: right;
  color: var(--ink);
}

.lap-table th:first-child { text-align: left; }

.lap-table td {
  padding: 9px 18px;
  font-size: 15px;
  text-align: right;
  color: #cfeef0;
  border-top: 1px solid rgba(0, 255, 255, 0.06);
  font-variant-numeric: tabular-nums;
}

.lap-table td:first-child {
  text-align: left;
  color: var(--ink);
}

.lap-table .gap { color: #ff8a8a; }   /* ช้ากว่ารอบเร็วสุด */

.lap-table tr.best td {
  color: var(--green);
  background: rgba(0, 255, 85, 0.07);
}

.lap-table tr.best .gap {
  color: var(--green);
  font-weight: 700;
}

.lap-table td.stats-empty {
  padding: 28px 18px;
  text-align: center;
  color: var(--ink);
  font-family: var(--font-thai);
}
