/* =====================================================================
   Laser Mirror Maze — styles.css  (Agent C)
   Dark science-lab / space theme. Deep-red glowing laser, glassy mirrors.
   Responsive: desktop = stage left + controls/info right column;
   tablet/mobile (<=820px) = stacked, controls become a horizontal tap bar.
   Palette custom properties are kept in sync with js/theme.js.
   ===================================================================== */

:root {
  /* ---- Surfaces ---- */
  --bg:            #070a12;   /* page backdrop (a touch darker than board) */
  --board-bg:      #0a0e1a;   /* matches THEME.bg */
  --panel:         #0e1424;   /* cards / panels */
  --panel-2:       #141c30;   /* raised elements, button faces */
  --panel-3:       #1b2540;   /* hover surface */
  --border:        rgba(140,170,220,0.14);
  --border-strong: rgba(140,170,220,0.30);

  /* ---- Brand / accents (mirror THEME) ---- */
  --laser:         #ff2d3f;
  --laser-glow:    rgba(255,60,80,0.65);
  --mirror:        #cdebff;
  --mirror-sel:    #ffd24a;
  --source:        #ff9a3c;
  --target:        #35f0a0;
  --target-hit:    #8affce;
  --grid:          rgba(120,160,220,0.10);

  /* ---- Text ---- */
  --text:          #eaf1fb;
  --text-muted:    #9fb0c8;
  --text-dim:      #6b7c98;

  /* ---- Scoring ---- */
  --star:          #ffd24a;
  --star-empty:    rgba(255,210,74,0.20);

  /* ---- Radii / shadows ---- */
  --radius:        16px;
  --radius-sm:     10px;
  --radius-pill:   999px;
  --shadow:        0 10px 30px rgba(0,0,0,0.45);
  --shadow-glow:   0 0 24px var(--laser-glow);

  /* ---- Spacing scale ---- */
  --gap:           clamp(10px, 1.4vw, 18px);
  --pad:           clamp(12px, 1.6vw, 20px);

  /* ---- Fonts (system / web-safe only — works offline) ---- */
  --font: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial,
          "Leelawadee UI", "Thonburi", "Noto Sans Thai", "Tahoma", sans-serif;
  --font-display: "Trebuchet MS", "Segoe UI", system-ui, var(--font);

  color-scheme: dark;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  min-height: 100dvh;
  background:
    radial-gradient(1200px 700px at 80% -10%, rgba(255,45,63,0.10), transparent 60%),
    radial-gradient(900px 600px at 0% 110%, rgba(53,240,160,0.08), transparent 55%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: clamp(14px, 0.6vw + 12px, 17px);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow-x: hidden;               /* no horizontal scroll, ever */
}

img, svg, canvas { display: block; max-width: 100%; }

/* ---------------------------------------------------------- app layout */
/* Grid areas make layout robust regardless of DOM sibling order. */
#app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(280px, 26vw, 380px);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas:
    "hud      hud"
    "stage    controls"
    "stage    info";
  gap: var(--gap);
  width: 100%;
  min-height: 100dvh;
  padding:
    calc(var(--pad) + env(safe-area-inset-top))
    calc(var(--pad) + env(safe-area-inset-right))
    calc(var(--pad) + env(safe-area-inset-bottom))
    calc(var(--pad) + env(safe-area-inset-left));
}

/* ------------------------------------------------------- HUD / header */
header#hud-top {
  grid-area: hud;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--gap);
  padding: clamp(10px, 1.2vw, 16px) var(--pad);
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.level-title {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(1.05rem, 1.4vw + 0.6rem, 1.6rem);
  letter-spacing: 0.2px;
  color: var(--text);
  text-shadow: 0 0 18px rgba(255,45,63,0.25);
}
.level-title small,
.level-title .subtitle {
  display: block;
  font-family: var(--font);
  font-weight: 500;
  font-size: 0.7em;
  color: var(--text-muted);
  letter-spacing: 0;
}

.level-nav {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* stars display (shared by HUD and win modal) */
.stars {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: clamp(1.1rem, 1vw + 0.6rem, 1.5rem);
  line-height: 1;
}
.stars .star,
.stars [data-star] {
  color: var(--star-empty);
  filter: none;
  transition: color 0.2s ease, filter 0.2s ease, transform 0.2s ease;
}
.stars .star.is-filled,
.stars .star.filled,
.stars [data-star].is-filled,
.stars .star--on {
  color: var(--star);
  filter: drop-shadow(0 0 8px rgba(255,210,74,0.65));
  transform: scale(1.05);
}

/* ----------------------------------------------------------- buttons */
.btn {
  --btn-face: var(--panel-2);
  --btn-face-hover: var(--panel-3);
  appearance: none;
  -webkit-appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;                 /* comfortable tap target */
  min-width: 44px;
  padding: 10px 16px;
  font-family: var(--font-display);
  font-size: clamp(0.9rem, 0.4vw + 0.8rem, 1.05rem);
  font-weight: 600;
  line-height: 1;
  color: var(--text);
  background: var(--btn-face);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  transition: background 0.15s ease, border-color 0.15s ease,
              transform 0.08s ease, box-shadow 0.15s ease, color 0.15s ease;
}
.btn:hover { background: var(--btn-face-hover); border-color: var(--border-strong); }
.btn:active { transform: translateY(1px) scale(0.99); }
.btn:focus-visible {
  outline: 3px solid rgba(120,200,255,0.85);
  outline-offset: 2px;
}
.btn:disabled,
.btn[disabled],
.btn[aria-disabled="true"] {
  opacity: 0.42;
  cursor: not-allowed;
  filter: grayscale(0.3);
  transform: none;
  box-shadow: none;
}

/* icon-only round buttons (nav / rotate) */
.btn.btn-icon,
#btn-prev, #btn-next,
#btn-rotate-left, #btn-rotate-right {
  padding: 10px;
  border-radius: var(--radius-pill);
  font-size: 1.2rem;
}

/* primary CTA — glowing red laser button */
.btn.btn-primary,
#btn-next-level {
  --btn-face: linear-gradient(180deg, #ff4356, var(--laser));
  --btn-face-hover: linear-gradient(180deg, #ff5567, #ff2233);
  color: #fff;
  border-color: rgba(255,120,130,0.7);
  box-shadow: 0 6px 18px rgba(255,45,63,0.35), 0 0 22px var(--laser-glow);
  text-shadow: 0 1px 2px rgba(0,0,0,0.35);
}
.btn.btn-primary:hover,
#btn-next-level:hover {
  box-shadow: 0 8px 24px rgba(255,45,63,0.5), 0 0 30px var(--laser-glow);
}

/* success accent CTA (optional use) */
.btn.btn-success {
  --btn-face: linear-gradient(180deg, #4dffb0, var(--target));
  --btn-face-hover: linear-gradient(180deg, #6affc0, #21e08e);
  color: #04231a;
  border-color: rgba(120,255,200,0.7);
  box-shadow: 0 6px 18px rgba(53,240,160,0.3), 0 0 22px rgba(53,240,160,0.5);
}

/* hint button subtle amber tint */
#btn-hint {
  border-color: rgba(255,210,74,0.4);
  color: #ffe39a;
}
#btn-hint:hover { background: rgba(255,210,74,0.10); }

/* ------------------------------------------------------------- stage */
main#stage {
  grid-area: stage;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  padding: clamp(6px, 1vw, 14px);
  background:
    radial-gradient(120% 120% at 50% 0%, rgba(120,160,220,0.06), transparent 60%),
    var(--board-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow), inset 0 0 60px rgba(0,0,0,0.5);
  overflow: hidden;
}
/* thin neon frame accent */
main#stage::after {
  content: "";
  position: absolute;
  inset: 6px;
  border-radius: calc(var(--radius) - 4px);
  border: 1px solid rgba(120,200,255,0.08);
  pointer-events: none;
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;            /* JS handles gestures; no page scroll/zoom */
  border-radius: calc(var(--radius) - 6px);
  cursor: pointer;
}

/* ------------------------------------------------------- controls */
aside#controls {
  grid-area: controls;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: var(--pad);
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
aside#controls .controls-row,
aside#controls .row {
  display: flex;
  gap: 8px;
}
aside#controls .btn { flex: 1 1 auto; }

.angle-readout {
  margin-top: 2px;
  padding: 10px 14px;
  font-family: "SFMono-Regular", ui-monospace, "Consolas", "Menlo", monospace;
  font-size: clamp(0.95rem, 0.6vw + 0.7rem, 1.2rem);
  font-weight: 700;
  text-align: center;
  color: var(--target);
  background: rgba(53,240,160,0.08);
  border: 1px solid rgba(53,240,160,0.28);
  border-radius: var(--radius-sm);
  letter-spacing: 0.5px;
  text-shadow: 0 0 12px rgba(53,240,160,0.4);
}

/* ------------------------------------------------------- info panel */
section#info-panel {
  grid-area: info;
  padding: var(--pad);
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow-y: auto;
}
section#info-panel h2,
section#info-panel h3 {
  margin: 0 0 8px;
  font-family: var(--font-display);
  font-size: clamp(1rem, 0.8vw + 0.7rem, 1.2rem);
  color: var(--text);
}
.science-note {
  margin: 0;
  font-size: 0.98em;
  line-height: 1.6;
  color: var(--text-muted);
}
.science-note strong,
.science-note b { color: var(--text); }
.science-note + .science-note { margin-top: 10px; }
/* subtle "did you know" accent bar */
.science-note.tip,
.science-note[data-kind="tip"] {
  padding-left: 12px;
  border-left: 3px solid var(--laser);
}

/* --------------------------------------------------------- modals */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(16px, 4vw, 32px);
  background: rgba(4,7,14,0.72);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  animation: modal-fade 0.2s ease both;
}
.modal.hidden { display: none; }   /* required convention */

.modal .modal-card,
.modal > .card,
.modal > div {
  width: 100%;
  max-width: 520px;
  max-height: calc(100dvh - 2 * clamp(16px, 4vw, 32px));
  overflow-y: auto;
  padding: clamp(20px, 3vw, 32px);
  text-align: center;
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow), 0 0 40px rgba(255,45,63,0.15);
  animation: modal-pop 0.25s cubic-bezier(0.2, 0.9, 0.3, 1.2) both;
}

.modal h1, .modal h2 {
  margin: 0 0 12px;
  font-family: var(--font-display);
  font-size: clamp(1.4rem, 2vw + 0.8rem, 2rem);
  color: var(--text);
  text-shadow: 0 0 22px rgba(255,45,63,0.3);
}
.modal p {
  margin: 0 0 12px;
  color: var(--text-muted);
  line-height: 1.6;
}
.modal .stars { justify-content: center; margin: 8px 0 18px; font-size: 2rem; }

/* กล่องเคล็ดลับใน intro — กะทัดรัด ไม่ดันเนื้อหาจนต้อง scroll */
.modal .law-box {
  margin-top: 4px;
  padding: 10px 14px;
  background: rgba(255,45,63,0.08);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
}
.modal .law-box h3 { margin: 0 0 6px; font-size: 1rem; color: var(--text); }
.modal .law-box p { margin: 0; font-size: 0.95rem; line-height: 1.5; }
.modal .modal-actions,
.modal .actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 18px;
}
.modal .modal-actions .btn,
.modal .actions .btn { flex: 1 1 auto; min-width: 140px; }

@keyframes modal-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes modal-pop {
  from { opacity: 0; transform: translateY(12px) scale(0.96); }
  to   { opacity: 1; transform: translateY(0)    scale(1); }
}

/* ============================================================
   RESPONSIVE — tablet / mobile: stack vertically
   ============================================================ */
@media (max-width: 820px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto auto;
    grid-template-areas:
      "hud"
      "stage"
      "controls"
      "info";
    gap: 12px;
  }

  header#hud-top { justify-content: space-between; }

  /* give the stacked stage height via aspect ratio (16:10) */
  main#stage {
    width: 100%;
    aspect-ratio: 16 / 10;
    min-height: 0;
  }

  /* controls become a horizontal tap bar with big targets */
  aside#controls {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 10px;
  }
  aside#controls .controls-row,
  aside#controls .row { flex: 1 1 100%; }
  aside#controls .btn {
    flex: 1 1 0;
    min-width: 64px;
    min-height: 48px;             /* extra-comfy on touch */
  }
  .angle-readout { flex: 1 1 100%; order: -1; }  /* readout on top of bar */
}

/* very small phones */
@media (max-width: 420px) {
  :root { --pad: 10px; --gap: 10px; }
  .level-title { font-size: 1.05rem; }
  aside#controls .btn { font-size: 0.9rem; padding: 10px 8px; }
  .modal .modal-actions .btn,
  .modal .actions .btn { flex: 1 1 100%; }
}

/* short landscape phones — keep stage from eating the whole screen */
@media (max-width: 820px) and (max-height: 520px) and (orientation: landscape) {
  #app {
    grid-template-columns: minmax(0,1fr) clamp(200px, 34vw, 300px);
    grid-template-rows: auto minmax(0,1fr);
    grid-template-areas:
      "hud    hud"
      "stage  controls"
      "stage  info";
  }
  main#stage { aspect-ratio: auto; }
  aside#controls { flex-direction: column; }
}

/* respect reduced-motion preference */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

/* ============================================================
   INTEGRATION OVERRIDES (integrator) — กันเนื้อหาล้นขวาบนจอแคบ
   grid/flex item ต้องตั้ง min-width:0 ไม่งั้น content กว้างจะดันทะลุ track
   ============================================================ */
#app > header, #app > main, #app > aside, #app > section { min-width: 0; min-height: 0; }

/* กลุ่มดาว + ปุ่มเปลี่ยนด่าน ให้เป็นแถวเดียวชิดขวา ยุบได้ */
.hud-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: flex-end;
  min-width: 0;
}
header#hud-top { flex-wrap: wrap; }
.level-title { min-width: 0; overflow-wrap: anywhere; }

/* ป้ายมุม: ห้ามล้น ให้ตัดบรรทัดได้ */
.angle-readout { min-width: 0; max-width: 100%; overflow-wrap: anywhere; word-break: break-word; }

/* ระหว่างชาร์จเป้า (ค้างลำแสง 2 วิ) ให้ป้ายเรืองเขียว */
.angle-readout.charging {
  color: var(--target, #35f0a0);
  border-color: var(--target, #35f0a0);
  box-shadow: 0 0 14px rgba(53, 240, 160, 0.45);
  font-weight: 700;
}

/* ปุ่ม CTA ใน modal ให้ติดล่างเสมอ (กันหลุด fold ตอนเนื้อหายาว) */
.modal .modal-actions,
.modal .actions {
  position: sticky;
  bottom: 0;
  padding-top: 14px;
  padding-bottom: 2px;
  background: linear-gradient(180deg, rgba(15,20,34,0) 0%, var(--panel) 45%);
}
