@font-face {
  font-family: "Noto Sans Thai";
  src: url("../fonts/NotoSansThai-Regular.ttf") format("truetype");
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: "Noto Sans Thai";
  src: url("../fonts/NotoSansThai-Bold.ttf") format("truetype");
  font-weight: 700;
  font-display: swap;
}

:root {
  --bg: #07090f;
  --panel: #12151e;
  --chrome: rgba(180, 200, 220, 0.18);
  --chrome-edge: rgba(220, 235, 255, 0.35);
  --text: #e8eef8;
  --muted: #8b95a8;
  --accent: #06c755; /* LINE green */
  --led-empty: rgba(200, 210, 230, 0.14);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans Thai", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

#app {
  min-height: 100%;
  min-height: 100dvh;
  max-width: 430px;
  margin: 0 auto;
  position: relative;
  padding-left: var(--safe-l);
  padding-right: var(--safe-r);
  display: flex;
  flex-direction: column;
}

.screen {
  display: none;
  flex-direction: column;
  flex: 1;
  min-height: 100dvh;
  padding: calc(12px + var(--safe-top)) 16px calc(16px + var(--safe-bot));
}
.screen.active { display: flex; }
#hub.screen.active {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
}

/* —— Hub —— */
.hub-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.brand {
  display: flex;
  gap: 12px;
  align-items: center;
}
.led-dot {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #2dff7a;
  box-shadow: 0 0 12px #2dff7a, 0 0 28px rgba(45, 255, 122, 0.5);
}
.brand h1 {
  margin: 0;
  font-size: 1.55rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.tagline {
  margin: 0;
  font-size: 0.78rem;
  color: var(--muted);
}
.hub-hint {
  margin: 4px 0 14px;
  font-size: 0.85rem;
  color: var(--muted);
  line-height: 1.4;
}
.section-label {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 10px;
}
.level-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 18px;
}
.lvl-btn {
  aspect-ratio: 1;
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 14px;
  background: linear-gradient(160deg, #1a1f2c, #0e1118);
  color: var(--text);
  font-family: inherit;
  font-weight: 700;
  font-size: 1.05rem;
  cursor: pointer;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 0;
  transition: transform 0.12s ease, border-color 0.12s;
}
.lvl-btn:active { transform: scale(0.94); }
.lvl-btn .sz {
  font-size: 0.62rem;
  font-weight: 400;
  color: var(--muted);
}
.lvl-btn.cleared {
  border-color: rgba(6, 199, 85, 0.55);
  box-shadow: inset 0 0 20px rgba(6, 199, 85, 0.12);
}
.lvl-btn.cleared::after {
  content: "✓";
  position: absolute;
  top: 5px;
  right: 7px;
  font-size: 0.7rem;
  color: var(--accent);
}
.sandbox-btn {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  padding: 14px 16px;
  border-radius: 18px;
  border: 1px solid rgba(45, 255, 122, 0.25);
  background: linear-gradient(135deg, rgba(45, 255, 122, 0.12), rgba(20, 40, 60, 0.4));
  color: var(--text);
  font-family: inherit;
  text-align: left;
  cursor: pointer;
}
.sandbox-btn:active { transform: scale(0.98); }
.sandbox-btn .sb-icon { font-size: 1.6rem; }
.sandbox-btn strong { display: block; font-size: 1.05rem; }
.sandbox-btn small { color: var(--muted); font-size: 0.78rem; }
.hub-foot {
  margin-top: auto;
  padding-top: 20px;
  text-align: center;
  font-size: 0.68rem;
  color: #5a6478;
}

/* —— Game —— */
.game-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}
.game-title {
  flex: 1;
  min-width: 0;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
#mode-label {
  font-weight: 700;
  font-size: 0.95rem;
}
.pair-status {
  flex-basis: 100%;
  text-align: center;
  font-size: 0.7rem;
  line-height: 1.4;
  color: var(--muted);
  letter-spacing: 0.04em;
}
.pill {
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 700;
  border: none;
  border-radius: 999px;
  padding: 8px 12px;
  background: #1e2433;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.pill:active { opacity: 0.85; }
.pill.ghost {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.12);
  color: var(--muted);
}
.pill.primary {
  background: var(--accent);
  color: #04140a;
  padding: 12px 22px;
  font-size: 0.95rem;
}
.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,0.1);
  background: #151922;
  font-size: 1rem;
  cursor: pointer;
  flex-shrink: 0;
}

.device-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 8px 0;
}
.device-chrome {
  width: min(92vw, 380px);
  aspect-ratio: 1;
  padding: 4.5%;
  border-radius: 22%;
  background:
    linear-gradient(145deg, rgba(255,255,255,0.22), rgba(255,255,255,0.04) 40%, rgba(0,0,0,0.15)),
    linear-gradient(320deg, rgba(160, 190, 220, 0.25), transparent 50%),
    rgba(40, 48, 62, 0.55);
  border: 1px solid var(--chrome-edge);
  box-shadow:
    0 0 0 1px rgba(0,0,0,0.4),
    0 18px 40px rgba(0,0,0,0.55),
    inset 0 1px 0 rgba(255,255,255,0.35),
    inset 0 -2px 8px rgba(0,0,0,0.25);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  position: relative;
}
.device-chrome::before {
  content: "";
  position: absolute;
  inset: 3.2%;
  border-radius: 18%;
  background: #05070c;
  box-shadow: inset 0 0 24px rgba(0,0,0,0.8);
  z-index: 0;
}
#board {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 12%;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.game-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  padding-top: 8px;
}
.hidden { display: none !important; }

/* —— Win —— */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(4, 6, 12, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: fadeIn 0.25s ease;
}
.overlay[hidden] { display: none; }
.win-card {
  background: linear-gradient(160deg, #1c2230, #10141c);
  border: 1px solid rgba(45, 255, 122, 0.35);
  border-radius: 24px;
  padding: 28px 24px 22px;
  text-align: center;
  max-width: 320px;
  width: 100%;
  box-shadow: 0 0 40px rgba(45, 255, 122, 0.15);
}
.win-burst {
  font-size: 2rem;
  color: #2dff7a;
  text-shadow: 0 0 20px #2dff7a;
  animation: pulse 0.65s ease 1;
}
.win-card h2 {
  margin: 8px 0 4px;
  font-size: 1.45rem;
}
.win-card p {
  margin: 0 0 18px;
  color: var(--muted);
  font-size: 0.88rem;
}
.win-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: stretch;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.15); opacity: 0.85; }
}

@media (max-height: 700px) {
  .hub-hint { margin-bottom: 8px; }
  .level-grid { gap: 8px; margin-bottom: 12px; }
  .device-wrap { padding: 4px 0; }
}

/* —— LINE Mini App chrome preview —— */
:root {
  --lma-header-h: 48px;
  --line-green: #06C755;
}

.lma-shell {
  display: block;
}

/* Default ON; hide only with ?chrome=0 */
html.no-line-chrome .lma-shell,
html.no-line-chrome .line-hub-banner,
html.no-line-chrome .lma-closed,
html.no-line-chrome .lma-toast {
  display: none !important;
}

.lma-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  height: calc(var(--lma-header-h) + var(--safe-top));
  padding-top: var(--safe-top);
  padding-left: max(4px, var(--safe-l));
  padding-right: max(4px, var(--safe-r));
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: linear-gradient(180deg, #11151c 0%, #0c1016 100%);
  border-bottom: 1px solid rgba(6, 199, 85, 0.35);
  box-shadow: 0 1px 0 rgba(6, 199, 85, 0.12), 0 8px 24px rgba(0, 0, 0, 0.35);
  max-width: 430px;
  margin: 0 auto;
}

.lma-close {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text);
  font-size: 1.15rem;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.lma-close:active {
  background: rgba(255, 255, 255, 0.08);
}

.lma-title-wrap {
  flex: 1;
  text-align: center;
  min-width: 0;
  pointer-events: none;
}
.lma-title {
  font-size: 0.98rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--text);
  line-height: 1.15;
}
.lma-sub {
  font-size: 0.62rem;
  color: var(--muted);
  margin-top: 1px;
}
.lma-header-spacer {
  width: 40px;
  flex-shrink: 0;
}

/* Pad app below fixed Mini App header (default ON) */
html:not(.no-line-chrome) #app {
  padding-top: calc(var(--lma-header-h) + var(--safe-top));
  min-height: 100%;
  min-height: 100dvh;
}

html:not(.no-line-chrome) .screen {
  /* safe-top already consumed by chrome header */
  padding-top: 12px;
  min-height: calc(100dvh - var(--lma-header-h) - var(--safe-top));
}

html:not(.no-line-chrome) .overlay {
  padding-top: calc(var(--lma-header-h) + var(--safe-top) + 8px);
}

/* Tighter chrome when ?line=1 */
html.in-line-preview,
body.in-line-preview {
  --lma-header-h: 42px;
}
html.in-line-preview .lma-sub,
body.in-line-preview .lma-sub {
  display: none;
}
html.in-line-preview .lma-header,
body.in-line-preview .lma-header {
  border-bottom-color: rgba(6, 199, 85, 0.5);
}
html.in-line-preview .line-hub-banner,
body.in-line-preview .line-hub-banner {
  padding: 10px 12px 12px;
  margin-bottom: 10px;
}

/* Hub: เปิดใน LINE banner */
.line-hub-banner {
  display: none;
  margin: 4px 0 14px;
  padding: 12px 14px 14px;
  border-radius: 16px;
  border: 1px solid rgba(6, 199, 85, 0.4);
  background:
    linear-gradient(135deg, rgba(6, 199, 85, 0.16), rgba(6, 199, 85, 0.04) 55%, rgba(20, 28, 40, 0.5)),
    #121820;
  box-shadow: 0 0 0 1px rgba(6, 199, 85, 0.08), inset 0 1px 0 rgba(255, 255, 255, 0.06);
}
html:not(.no-line-chrome) .line-hub-banner {
  display: block;
}
.lhb-top {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.lhb-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  color: #04140a;
  background: var(--line-green);
  border-radius: 6px;
  padding: 3px 7px;
}
.lhb-top strong {
  font-size: 0.95rem;
}
.lhb-steps {
  margin: 0 0 12px;
  padding-left: 1.15rem;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.45;
}
.lhb-steps li {
  margin-bottom: 4px;
}
.lhb-steps li::marker {
  color: var(--line-green);
}
.lhb-copy {
  width: 100%;
  font-family: inherit;
  font-weight: 700;
  font-size: 0.88rem;
  border: none;
  border-radius: 999px;
  padding: 10px 14px;
  cursor: pointer;
  background: var(--line-green);
  color: #04140a;
}
.lhb-copy:active {
  opacity: 0.88;
  transform: scale(0.98);
}

/* Closed mock overlay */
.lma-closed {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: #0b0e14;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  padding: var(--safe-top) var(--safe-r) var(--safe-bot) var(--safe-l);
}
.lma-closed.show {
  opacity: 1;
  pointer-events: auto;
}
.lma-closed[hidden] {
  display: none !important;
}
.lma-closed-inner {
  text-align: center;
  padding: 24px;
}
.lma-closed-x {
  width: 56px;
  height: 56px;
  margin: 0 auto 14px;
  border-radius: 50%;
  background: rgba(6, 199, 85, 0.15);
  border: 1px solid rgba(6, 199, 85, 0.45);
  color: var(--line-green);
  font-size: 1.4rem;
  display: flex;
  align-items: center;
  justify-content: center;
}
.lma-closed-title {
  font-weight: 700;
  font-size: 1.1rem;
  margin-bottom: 6px;
}
.lma-closed-sub {
  font-size: 0.8rem;
  color: var(--muted);
}

/* Toast */
.lma-toast {
  position: fixed;
  left: 50%;
  bottom: calc(28px + var(--safe-bot));
  transform: translateX(-50%) translateY(12px);
  z-index: 220;
  max-width: min(90vw, 360px);
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(18, 24, 32, 0.94);
  border: 1px solid rgba(6, 199, 85, 0.45);
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 600;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
}
.lma-toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (max-height: 700px) {
  html:not(.no-line-chrome) .device-wrap {
    padding: 2px 0;
  }
  html:not(.no-line-chrome) .line-hub-banner {
    margin-bottom: 10px;
  }
}


/* —— Language toggle —— */
.hub-head-actions,
.game-bar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
.lang-toggle {
  display: inline-flex;
  align-items: center;
  padding: 2px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.14);
  gap: 0;
}
.lang-toggle button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-family: inherit;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 5px 9px;
  border-radius: 999px;
  cursor: pointer;
  line-height: 1;
  min-width: 2rem;
}
.lang-toggle button.active {
  background: var(--accent);
  color: #04140a;
  box-shadow: 0 0 10px rgba(6, 199, 85, 0.35);
}
.lang-toggle.compact button {
  padding: 4px 7px;
  font-size: 0.62rem;
  min-width: 1.75rem;
}
.lma-header .lang-toggle {
  margin-right: 2px;
}

/* Keep the solved board visible behind its immediate completion message. */
#win {
  align-items: flex-end;
  padding-bottom: calc(20px + var(--safe-bot));
  background: rgba(4, 6, 12, 0.28);
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
}
@media (prefers-reduced-motion: reduce) {
  .overlay, .win-burst { animation: none; }
}

#win .win-card { position: relative; padding: 18px 20px; }
#win .win-burst { position: absolute; left: 18px; top: 14px; font-size: 1.5rem; }
#win h2 { margin: 0 24px 6px; font-size: 1.2rem; }
#win p { margin-bottom: 14px; }
#win .win-actions { flex-direction: row; align-items: center; }
#win .win-actions .primary { flex: 1; padding: 12px; }
