/* ========= Spark! นักสร้างวงจร — styles ========= */
:root {
  --copper: #c47a3d;
  --copper-dim: #8a5e38;
  --live: #ffc93a;
  --live-glow: #ff9d2f;
  --bulb-lit: #ffe27a;
  --battery: #e05555;
  --board-bg: #14243a;
  --slot: #1e344f;
  --slot-edge: #2b476b;
  --ink: #22303f;
  --paper: #eef3f9;
  --panel: #ffffff;
  --accent: #2f80ed;
  --danger: #e5484d;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: 'Sarabun', system-ui, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 50% -10%, #dce8fb, transparent),
    linear-gradient(160deg, #f5f8fc, #e9eff7);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
}

.app {
  width: 100%;
  max-width: 640px;
  padding: clamp(12px, 3vw, 22px);
  display: flex;
  flex-direction: column;
  gap: clamp(10px, 2vw, 16px);
}

/* ---- Topbar ---- */
.topbar { display: flex; align-items: center; justify-content: space-between; }
.title {
  font-family: 'Baloo 2', sans-serif;
  font-weight: 800;
  font-size: clamp(1.5rem, 5vw, 2.2rem);
  margin: 0;
  color: #17335c;
  letter-spacing: .5px;
}
.title span { color: var(--accent); }
.icon-btn {
  border: none; background: #fff; width: 44px; height: 44px; border-radius: 50%;
  font-size: 1.3rem; cursor: pointer; box-shadow: 0 2px 8px rgba(20,40,70,.12);
  transition: transform .1s;
}
.icon-btn:active { transform: scale(.9); }

/* ---- Info panel ---- */
.info {
  background: var(--panel); border-radius: 16px; padding: clamp(12px, 2.5vw, 18px);
  box-shadow: 0 4px 18px rgba(20,40,70,.08);
}
.info-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 6px; }
.level-name {
  font-family: 'Baloo 2', sans-serif; margin: 0;
  font-size: clamp(1.1rem, 3.5vw, 1.5rem); color: #17335c;
}
.concept-badge {
  background: linear-gradient(135deg, #ffd76a, #ffb020);
  color: #6a4a00; font-weight: 700; font-size: .82rem;
  padding: 4px 12px; border-radius: 999px; white-space: nowrap;
}
.tip { margin: 0; font-size: clamp(.9rem, 2.6vw, 1.02rem); line-height: 1.55; color: #40515f; }
.tip.flash { color: var(--accent); font-weight: 700; }

/* ---- Board ---- */
.stage { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.board {
  --cols: 3; --rows: 3;
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  gap: clamp(3px, 1vw, 8px);
  /* กว้างทุกด่านมี cols >= rows เสมอ → 72vh คุมความสูงไม่ให้ล้นจอ */
  width: min(92vw, 480px, 72vh);
  max-width: 100%;
  aspect-ratio: var(--cols) / var(--rows);
  background: var(--board-bg);
  padding: clamp(6px, 1.5vw, 12px);
  border-radius: 18px;
  box-shadow: 0 10px 30px rgba(10,25,45,.25), inset 0 0 0 2px #0d1b2c;
  transition: box-shadow .2s;
}
.board.is-short { box-shadow: 0 0 0 3px var(--danger), 0 10px 30px rgba(229,72,77,.4); animation: shake .35s; }

.cell {
  position: relative; aspect-ratio: 1; border-radius: 10px;
  min-width: 0; min-height: 0; /* ให้ 1fr ย่อได้ ไม่ให้ SVG ดันคอลัมน์ล้นจอ */
  background: var(--slot);
  box-shadow: inset 0 0 0 1.5px var(--slot-edge);
  display: flex; align-items: center; justify-content: center;
}
.cell.rotatable { cursor: pointer; touch-action: manipulation; }
.cell.rotatable:hover { background: #26426a; }
.cell.rotatable:focus-visible { outline: 3px solid var(--live); outline-offset: 2px; }
.cell.spin svg { animation: pop .2s ease-out; }
.cell.hinted { box-shadow: inset 0 0 0 3px var(--live), 0 0 14px var(--live-glow); animation: pulse 1s ease-out 2; }

.tile { width: 100%; height: 100%; display: block; }

/* wires */
.wire { stroke: var(--copper); stroke-width: 13; stroke-linecap: round; fill: none; transition: stroke .18s; }
.wire.live { stroke: var(--live); filter: drop-shadow(0 0 6px var(--live-glow)); }
.node { fill: var(--copper); transition: fill .18s; }
.node.live { fill: var(--live); filter: drop-shadow(0 0 6px var(--live-glow)); }

/* bulb */
.tile.bulb .wire { stroke: var(--copper-dim); }
.bulb-glass { fill: rgba(200,215,230,.18); stroke: #9fb3c8; stroke-width: 3; transition: all .2s; }
.filament { fill: none; stroke: #9fb3c8; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; transition: stroke .2s; }
.bulb-base { fill: #7c8699; }
.bulb-glyph.lit .bulb-glass { fill: var(--bulb-lit); stroke: #ffb020; filter: drop-shadow(0 0 12px var(--live-glow)); }
.bulb-glyph.lit .filament { stroke: #d98400; stroke-width: 3.5; }
.bulb-glyph.lit { animation: bulbpop .35s ease-out; }

/* battery */
.tile.battery .wire { stroke: var(--copper); }
.battery-glyph rect { fill: var(--battery); }
.battery-glyph .cap { fill: #b23b3b; }
.pole { font: 700 24px 'Baloo 2', sans-serif; fill: #fff; text-anchor: middle; dominant-baseline: central; }

/* insulator block */
.tile.block rect { fill: #6b4a2f; stroke: #4a3220; stroke-width: 3; }
.tile.block .hatch { stroke: #caa279; stroke-width: 4; stroke-linecap: round; }
.tile.block .glyph { font: 700 15px 'Sarabun', sans-serif; fill: #ffe9cf; text-anchor: middle; }

.short-warn {
  background: var(--danger); color: #fff; font-weight: 700;
  padding: 8px 16px; border-radius: 12px; font-size: .92rem; text-align: center;
  box-shadow: 0 4px 14px rgba(229,72,77,.4);
}

/* ---- Status bar ---- */
.statusbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.progress {
  font-family: 'Baloo 2', sans-serif; font-weight: 700; font-size: 1.05rem;
  color: #17335c; background: #fff; padding: 6px 14px; border-radius: 999px;
  box-shadow: 0 2px 8px rgba(20,40,70,.1);
}
.tools { display: flex; gap: 8px; }

/* ---- Buttons ---- */
.btn {
  font-family: 'Sarabun', sans-serif; font-weight: 700; font-size: .95rem;
  border: none; border-radius: 12px; padding: 9px 16px; cursor: pointer;
  transition: transform .1s, background .15s; touch-action: manipulation;
}
.btn:active { transform: scale(.94); }
.btn-ghost { background: #fff; color: #2c3e50; box-shadow: 0 2px 8px rgba(20,40,70,.1); }
.btn-ghost:hover { background: #f0f5fb; }
.btn-primary { background: linear-gradient(135deg, #3a90ff, #1f6fe0); color: #fff; box-shadow: 0 4px 14px rgba(31,111,224,.4); }
.btn-nav {
  background: #fff; color: #17335c; width: 44px; height: 44px; padding: 0;
  font-size: 1.6rem; line-height: 1; border-radius: 50%; box-shadow: 0 2px 8px rgba(20,40,70,.12);
}
.btn:disabled { opacity: .35; cursor: not-allowed; }

/* ---- Level nav ---- */
.levelnav { display: flex; align-items: center; justify-content: center; gap: 12px; }
.dots { display: flex; gap: 8px; }
.dot {
  width: 34px; height: 34px; border-radius: 50%; border: none; cursor: pointer;
  font-family: 'Baloo 2', sans-serif; font-weight: 700; font-size: .95rem;
  background: #fff; color: #7089a3; box-shadow: 0 2px 6px rgba(20,40,70,.1);
  transition: transform .1s;
}
.dot:active { transform: scale(.9); }
.dot.active { background: linear-gradient(135deg, #ffd76a, #ffb020); color: #6a4a00; box-shadow: 0 3px 10px rgba(255,176,32,.5); }

/* ---- Legend ---- */
.legend {
  display: flex; flex-wrap: wrap; gap: 6px 16px; justify-content: center;
  font-size: .8rem; color: #5c6f80;
}
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.sw { width: 18px; height: 6px; border-radius: 3px; display: inline-block; }
.sw-copper { background: var(--copper); }
.sw-live { background: var(--live); box-shadow: 0 0 6px var(--live-glow); }
.sw-block { width: 14px; height: 14px; border-radius: 3px; background: #6b4a2f; }
.hint-text { font-style: italic; }

/* ---- Win overlay ---- */
.overlay {
  position: fixed; inset: 0; background: rgba(15,30,50,.6); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 50;
  animation: fade .3s;
}
.overlay[hidden] { display: none; } /* ต้อง override display:flex ด้านบน ไม่งั้น hidden ไม่ทำงาน */
.win-card {
  background: #fff; border-radius: 24px; padding: clamp(24px, 6vw, 40px); text-align: center;
  max-width: 380px; width: 100%; box-shadow: 0 20px 60px rgba(10,25,45,.4);
  animation: rise .4s cubic-bezier(.2,.9,.3,1.3);
}
.spark-burst { font-size: 2.4rem; animation: pulse 1.2s infinite; }
.win-card h2 { font-family: 'Baloo 2', sans-serif; font-size: 1.8rem; margin: 8px 0; color: #17335c; }
.win-card p { color: #48586a; line-height: 1.55; margin: 0 0 22px; }
.win-actions { display: flex; gap: 10px; justify-content: center; }

/* ---- animations ---- */
@keyframes pop { 0% { transform: scale(.82) rotate(-8deg); } 100% { transform: scale(1) rotate(0); } }
@keyframes bulbpop { 0% { transform: scale(.7); } 60% { transform: scale(1.12); } 100% { transform: scale(1); } }
@keyframes pulse { 0%,100% { transform: scale(1); opacity: 1; } 50% { transform: scale(1.08); opacity: .85; } }
@keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-6px); } 40% { transform: translateX(6px); } 60% { transform: translateX(-4px); } 80% { transform: translateX(4px); } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes rise { from { opacity: 0; transform: translateY(30px) scale(.9); } to { opacity: 1; transform: translateY(0) scale(1); } }

/* ---- Landscape / wide: วางข้อมูลข้างกระดาน ---- */
@media (min-width: 780px) and (min-aspect-ratio: 1/1) {
  .app { max-width: 940px; display: grid; grid-template-columns: 1fr 1.1fr;
    grid-template-areas: "top top" "info stage" "status stage" "nav stage" "legend stage";
    align-content: start; column-gap: 24px; }
  .topbar { grid-area: top; }
  .info { grid-area: info; }
  .stage { grid-area: stage; align-self: center; }
  .statusbar { grid-area: status; }
  .levelnav { grid-area: nav; justify-content: flex-start; }
  .legend { grid-area: legend; justify-content: flex-start; }
  .board { width: min(46vw, 460px, calc(80vh * var(--cols) / var(--rows))); }
}

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