:root {
  --orange: #ff7a00;
  --orange-deep: #e85f00;
  --orange-soft: #fff1e4;
  --ink: #1e2530;
  --muted: #6d7683;
  --surface: #ffffff;
  --line: #e7ebf0;
  --shadow: 0 20px 60px rgba(30, 37, 48, .16);
}

* { box-sizing: border-box; }
html, body { width: 100%; min-height: 100%; margin: 0; }
body {
  overflow: hidden;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 15%, rgba(255, 122, 0, .16), transparent 29%),
    radial-gradient(circle at 88% 84%, rgba(255, 190, 120, .22), transparent 32%),
    #f7f8fa;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; }
button:focus-visible { outline: 3px solid rgba(255, 122, 0, .35); outline-offset: 3px; }

.game-shell {
  width: 100vw;
  height: 100dvh;
  min-height: 500px;
  display: grid;
  grid-template-rows: 74px 1fr;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(230px, 1fr) auto minmax(110px, 1fr);
  align-items: center;
  gap: 20px;
  padding: 12px clamp(18px, 3vw, 46px);
  background: rgba(255,255,255,.92);
  border-bottom: 1px solid rgba(231,235,240,.92);
  backdrop-filter: blur(18px);
  z-index: 5;
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  width: 44px; height: 44px; flex: 0 0 auto;
  display: grid; place-items: center;
  color: #fff; font-size: 23px;
  border-radius: 14px;
  background: linear-gradient(145deg, #ff9b35, #ff6900);
  box-shadow: 0 10px 24px rgba(255,122,0,.28);
}
.brand strong { display: block; font-size: 16px; letter-spacing: .08em; }
.brand small { display: block; margin-top: 2px; color: var(--muted); font-size: 11px; white-space: nowrap; }

.status-row { display: flex; gap: 10px; justify-content: center; }
.status-pill {
  min-width: 92px; padding: 8px 14px;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  border-radius: 14px; background: #f5f6f8; border: 1px solid #eceff2;
}
.status-pill span { color: var(--muted); font-size: 10px; font-weight: 800; letter-spacing: .08em; }
.status-pill b { font-size: 15px; }
.toolbar { display: flex; justify-content: flex-end; gap: 8px; }
.icon-button {
  width: 42px; height: 42px; display: grid; place-items: center;
  border: 1px solid var(--line); border-radius: 13px;
  background: #fff; color: var(--ink); cursor: pointer;
  transition: transform .18s ease, box-shadow .18s ease, border-color .18s ease;
}
.icon-button:hover { transform: translateY(-2px); border-color: #ffd2aa; box-shadow: 0 9px 20px rgba(30,37,48,.09); }

.play-area {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(250px, 330px) minmax(0, 1fr);
  gap: clamp(18px, 3vw, 46px);
  padding: clamp(18px, 3vw, 42px) clamp(18px, 4vw, 60px);
}

.mission-panel {
  align-self: center;
  padding: clamp(22px, 3vw, 34px);
  border: 1px solid rgba(231,235,240,.95);
  border-radius: 28px;
  background: rgba(255,255,255,.88);
  box-shadow: 0 18px 50px rgba(30,37,48,.08);
  backdrop-filter: blur(16px);
}
.mission-badge, .eyebrow {
  display: inline-flex; align-items: center; gap: 8px;
  color: var(--orange-deep); font-weight: 900; font-size: 11px; letter-spacing: .12em;
}
.mission-panel h1 { margin: 14px 0 12px; font-size: clamp(26px, 3.2vw, 42px); line-height: 1.02; letter-spacing: -.04em; }
.mission-panel p { margin: 0; color: var(--muted); line-height: 1.62; font-size: 14px; }
.legend { display: grid; gap: 10px; margin: 24px 0; color: #424a55; font-size: 13px; font-weight: 700; }
.legend > div { display: flex; align-items: center; gap: 10px; }
.legend-orb { width: 15px; height: 15px; border-radius: 50%; box-shadow: 0 0 0 5px rgba(255,122,0,.08); }
.legend-orb.source { background: #24c7ff; }
.legend-orb.target { background: var(--orange); }
.stars-preview { padding: 16px; border-radius: 18px; background: var(--orange-soft); border: 1px solid #ffe0c2; }
.star { color: #d8dde3; font-size: 21px; letter-spacing: 2px; }
.star.active { color: #ff9a24; text-shadow: 0 3px 12px rgba(255,122,0,.24); }
.stars-preview small { display: block; margin-top: 6px; color: #a75b18; font-weight: 800; }
.mission-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 18px; }
.secondary-button {
  min-height: 46px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 14px; background: #fff;
  color: var(--ink); font-weight: 800; font-size: 12px; cursor: pointer;
  transition: transform .16s ease, border-color .16s ease, background .16s ease;
}
.secondary-button:hover { transform: translateY(-2px); border-color: #ffc998; background: #fffaf5; }
.secondary-button:disabled { opacity: .45; cursor: not-allowed; transform: none; }

.board-stage { min-width: 0; min-height: 0; position: relative; display: grid; place-items: center; align-content: center; }
.ambient { position: absolute; border-radius: 50%; filter: blur(44px); pointer-events: none; opacity: .56; }
.ambient-a { width: 220px; height: 220px; right: 8%; top: 4%; background: rgba(255,122,0,.22); }
.ambient-b { width: 180px; height: 180px; left: 7%; bottom: 2%; background: rgba(70,198,255,.16); }
.board-frame {
  position: relative;
  width: min(100%, 940px);
  padding: clamp(18px, 2.3vw, 28px);
  border-radius: 34px;
  background: linear-gradient(145deg, rgba(255,255,255,.96), rgba(246,248,251,.96));
  border: 1px solid rgba(223,228,234,.96);
  box-shadow: var(--shadow), inset 0 1px 0 #fff;
  overflow: hidden;
}
.board {
  --cols: 7;
  --rows: 4;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows), minmax(0, 1fr));
  gap: clamp(7px, 1vw, 12px);
  aspect-ratio: 1.74 / 1;
}
.tile {
  position: relative;
  min-width: 0;
  border: 1px solid #e5eaf0;
  border-radius: clamp(12px, 1.5vw, 20px);
  background: linear-gradient(145deg, #fff, #f3f5f8);
  box-shadow: inset 0 1px 0 #fff, 0 8px 18px rgba(30,37,48,.06);
  overflow: hidden;
  cursor: pointer;
  transition: transform .18s ease, border-color .2s ease, box-shadow .2s ease;
}
.tile:hover { transform: translateY(-2px); border-color: #ffc58f; box-shadow: inset 0 1px 0 #fff, 0 12px 22px rgba(30,37,48,.1); }
.tile.empty { cursor: default; border-color: rgba(229,234,240,.6); background: rgba(246,248,250,.56); box-shadow: none; }
.tile.empty:hover { transform: none; }
.tile-core {
  position: absolute; inset: 0;
  transform: rotate(calc(var(--rotation) * 90deg));
  transition: transform .22s cubic-bezier(.2,.8,.2,1);
}
.pipe { position: absolute; background: linear-gradient(90deg, #ffae55, var(--orange)); box-shadow: 0 0 12px rgba(255,122,0,.26); border-radius: 999px; }
.pipe.n { width: 12%; height: 51%; left: 44%; top: -1%; }
.pipe.e { width: 51%; height: 12%; right: -1%; top: 44%; }
.pipe.s { width: 12%; height: 51%; left: 44%; bottom: -1%; }
.pipe.w { width: 51%; height: 12%; left: -1%; top: 44%; }
.hub {
  position: absolute; width: 24%; aspect-ratio: 1; left: 38%; top: 38%; border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #ffd4a8, #ff7a00 64%, #d85100);
  box-shadow: 0 0 0 5px rgba(255,122,0,.09), 0 0 22px rgba(255,122,0,.38);
}
.tile.powered { border-color: #ffb76f; box-shadow: inset 0 0 0 2px rgba(255,122,0,.12), 0 10px 24px rgba(255,122,0,.14); }
.tile.powered .pipe { background: linear-gradient(90deg, #fff0bd, #ff9a24); box-shadow: 0 0 16px rgba(255,139,18,.74); }
.tile.endpoint-source .hub {
  width: 38%; left: 31%; top: 31%;
  background: radial-gradient(circle at 35% 30%, #fff, #69ddff 35%, #1899e5 70%, #0c5b9f);
  box-shadow: 0 0 0 7px rgba(53,190,255,.12), 0 0 28px rgba(36,190,255,.7);
}
.tile.endpoint-target .hub {
  width: 48%; left: 26%; top: 26%; border-radius: 17px;
  background: linear-gradient(145deg, #ffd18d, #ff7a00 58%, #d75600);
  box-shadow: 0 0 0 7px rgba(255,122,0,.12), 0 0 30px rgba(255,122,0,.62);
}
.tile.endpoint-target .hub::after {
  content: "♛"; position: absolute; inset: 0; display: grid; place-items: center;
  color: #fff; font-size: clamp(15px, 2vw, 28px); text-shadow: 0 2px 6px rgba(80,35,0,.34);
}
.tile.endpoint-target.solved .hub { animation: crownPulse 1.05s ease-in-out infinite alternate; }
@keyframes crownPulse { to { transform: scale(1.1); box-shadow: 0 0 0 10px rgba(255,122,0,.09), 0 0 42px rgba(255,122,0,.88); } }
.tile.wrong-hint { animation: hintPulse .72s ease-in-out 2; }
@keyframes hintPulse { 50% { transform: scale(1.06); border-color: #ff7a00; box-shadow: 0 0 0 7px rgba(255,122,0,.13); } }
.energy-flash { position: absolute; inset: 0; pointer-events: none; opacity: 0; background: radial-gradient(circle, rgba(255,198,106,.33), transparent 58%); }
.energy-flash.active { animation: flash .9s ease-out; }
@keyframes flash { 0% { opacity: 0; transform: scale(.6); } 30% { opacity: 1; } 100% { opacity: 0; transform: scale(1.2); } }
.board-tip { margin: 14px 0 0; color: var(--muted); font-size: 12px; text-align: center; font-weight: 700; }

.modal {
  position: fixed; inset: 0; z-index: 40; display: grid; place-items: center;
  padding: 24px; background: rgba(22,27,34,.42); backdrop-filter: blur(10px);
  opacity: 0; pointer-events: none; transition: opacity .24s ease;
}
.modal.visible { opacity: 1; pointer-events: auto; }
.modal-card {
  width: min(92vw, 480px); padding: 34px; border-radius: 30px; text-align: center;
  background: #fff; border: 1px solid rgba(255,255,255,.88); box-shadow: 0 28px 90px rgba(22,27,34,.28);
  transform: translateY(14px) scale(.97); transition: transform .25s ease;
}
.modal.visible .modal-card { transform: translateY(0) scale(1); }
.modal-crown, .complete-icon {
  width: 74px; height: 74px; margin: 0 auto 18px; display: grid; place-items: center;
  border-radius: 24px; color: #fff; font-size: 38px;
  background: linear-gradient(145deg, #ff9d3a, #ff6900);
  box-shadow: 0 18px 38px rgba(255,122,0,.3);
}
.modal-card h2 { margin: 10px 0 10px; font-size: 38px; letter-spacing: -.04em; }
.modal-card p { margin: 0 auto; max-width: 390px; color: var(--muted); line-height: 1.6; }
.feature-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin: 22px 0; }
.feature-row span { padding: 7px 11px; border-radius: 999px; background: #f5f6f8; color: #535c67; font-size: 11px; font-weight: 800; }
.primary-button {
  width: 100%; min-height: 54px; border: 0; border-radius: 16px; color: #fff; cursor: pointer;
  background: linear-gradient(135deg, #ff9228, #ff6900); font-weight: 900; letter-spacing: .05em;
  box-shadow: 0 14px 26px rgba(255,122,0,.25); transition: transform .16s ease, box-shadow .16s ease;
}
.primary-button:hover { transform: translateY(-2px); box-shadow: 0 18px 32px rgba(255,122,0,.34); }
.text-button { margin-top: 14px; border: 0; background: transparent; color: var(--muted); font-weight: 800; cursor: pointer; }
.earned-stars { margin: 12px 0 22px; color: #ff9828; font-size: 34px; letter-spacing: 5px; text-shadow: 0 8px 22px rgba(255,122,0,.22); }
.result-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 20px; }
.result-grid > div { padding: 14px 8px; border-radius: 16px; background: #f6f7f9; }
.result-grid span { display: block; color: var(--muted); font-size: 10px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
.result-grid b { display: block; margin-top: 4px; font-size: 18px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; z-index: 60; transform: translate(-50%, 18px);
  padding: 12px 18px; border-radius: 14px; color: #fff; background: #222a34;
  font-weight: 800; font-size: 13px; box-shadow: 0 14px 30px rgba(0,0,0,.2);
  opacity: 0; pointer-events: none; transition: opacity .2s ease, transform .2s ease;
}
.toast.visible { opacity: 1; transform: translate(-50%, 0); }

.orientation-gate { display: none; position: fixed; inset: 0; z-index: 100; place-items: center; padding: 24px; background: linear-gradient(145deg,#fff7ef,#fff); }
.rotate-card { text-align: center; max-width: 280px; }
.phone-icon { width: 86px; height: 60px; margin: 0 auto 18px; display: grid; place-items: center; border: 4px solid var(--orange); border-radius: 18px; color: var(--orange); font-size: 30px; transform: rotate(90deg); }
.rotate-card strong { display: block; font-size: 22px; }
.rotate-card span { display: block; margin-top: 8px; color: var(--muted); line-height: 1.5; }

@media (max-width: 940px) {
  .game-shell { grid-template-rows: 64px 1fr; }
  .topbar { padding: 8px 16px; grid-template-columns: 1fr auto; }
  .status-row { order: 3; grid-column: 1 / -1; display: none; }
  .toolbar { min-width: auto; }
  .play-area { grid-template-columns: 210px minmax(0,1fr); gap: 16px; padding: 14px 16px 18px; }
  .mission-panel { padding: 18px; border-radius: 22px; }
  .mission-panel h1 { font-size: 25px; }
  .legend { margin: 16px 0; }
  .mission-actions { grid-template-columns: 1fr; }
  .board-frame { border-radius: 24px; padding: 15px; }
}

@media (max-height: 650px) and (orientation: landscape) {
  .game-shell { min-height: 390px; grid-template-rows: 58px 1fr; }
  .topbar { padding-top: 7px; padding-bottom: 7px; }
  .brand-mark { width: 40px; height: 40px; }
  .play-area { padding-top: 10px; padding-bottom: 10px; }
  .mission-panel { padding: 16px; }
  .mission-panel p { font-size: 12px; line-height: 1.45; }
  .legend { margin: 12px 0; gap: 7px; }
  .stars-preview { padding: 10px 12px; }
  .board-frame { width: min(100%, 820px); padding: 14px; }
  .board-tip { margin-top: 8px; }
}

@media (orientation: portrait) and (max-width: 820px) {
  .orientation-gate { display: grid; }
}
