:root {
  --bg: #14161f;
  --panel: #1f2230;
  --panel-2: #2a2e40;
  --text: #f4f5fb;
  --muted: #a3a9c2;
  --accent: #ffcc33;
  --light: #ffb703;
  --water: #3ba7ff;
  --good: #3ddc84;
  --bad: #ff5c5c;
  --radius: 18px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { opacity: .45; cursor: not-allowed; }
a { color: var(--water); }

/* ---- top bar ---- */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  padding-top: max(8px, env(safe-area-inset-top));
}
.brand { font-weight: 800; font-size: 1.25rem; letter-spacing: .02em; }
.pills { display: flex; gap: 6px; flex-wrap: wrap; flex: 1; }
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 999px;
  background: var(--panel); color: var(--muted); font-size: .8rem;
}
.pill i { width: 8px; height: 8px; border-radius: 50%; background: var(--bad); }
.pill.on { color: var(--text); }
.pill.on i { background: var(--good); }

.hold-btn {
  position: relative; overflow: hidden;
  width: 48px; height: 48px; border-radius: 50%;
  border: 0; background: var(--panel); font-size: 1.4rem;
  user-select: none; -webkit-user-select: none; touch-action: none;
}
.hold-btn .fill {
  position: absolute; inset: 0; background: var(--accent); opacity: .55;
  transform: scaleY(0); transform-origin: bottom;
}
.hold-btn.holding .fill { transform: scaleY(1); transition: transform 1s linear; }
.hold-btn .icon { position: relative; }

/* ---- stage ---- */
.stage {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 16px;
  padding: 0 16px 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

.video-area {
  container-type: size;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Sized to the camera's aspect ratio so the aiming cross lines up with the picture. */
.video-wrap {
  --ar: 1.35;
  position: relative;
  width: min(100cqw, 100cqh * var(--ar));
  aspect-ratio: var(--ar);
  border-radius: var(--radius);
  overflow: hidden;
  background: #000;
  user-select: none; -webkit-user-select: none;
}
.video-wrap.placing { cursor: crosshair; outline: 4px dashed var(--accent); outline-offset: -4px; }
#video { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.crosshair { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.no-signal, .place-hint {
  position: absolute; left: 50%; transform: translateX(-50%);
  padding: 8px 16px; border-radius: 999px; background: rgba(0, 0, 0, .7);
  font-weight: 600; white-space: nowrap;
}
.no-signal { top: 50%; transform: translate(-50%, -50%); }
.place-hint { top: 12px; color: var(--accent); }
[hidden] { display: none !important; }

/* ---- controls ---- */
.controls {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 20px;
  user-select: none; -webkit-user-select: none;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 72px);
  grid-template-rows: repeat(3, 72px);
  gap: 6px;
}
.d {
  border: 0; border-radius: 16px; background: var(--panel-2); font-size: 1.6rem;
  touch-action: none; box-shadow: 0 4px 0 #0b0c12; transition: transform .05s, box-shadow .05s;
}
.d.pressed, .d:active { transform: translateY(3px); box-shadow: 0 1px 0 #0b0c12; background: #3a3f57; }
.d.up { grid-area: 1 / 2; }
.d.left { grid-area: 2 / 1; }
.d.mid { grid-area: 2 / 2; background: var(--panel); font-size: 1.3rem; }
.d.right { grid-area: 2 / 3; }
.d.down { grid-area: 3 / 2; }

.angles { color: var(--muted); font-variant-numeric: tabular-nums; font-size: .95rem; white-space: pre; }

.fire {
  width: 150px; height: 150px; border-radius: 50%; border: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-weight: 800; font-size: 1rem; color: #14161f;
  touch-action: none; box-shadow: 0 6px 0 rgba(0, 0, 0, .45);
  transition: transform .05s, box-shadow .05s, filter .15s;
}
.fire .fire-icon { font-size: 2.6rem; line-height: 1; }
.fire.light { background: var(--light); }
.fire.water { background: var(--water); }
.fire.light:not(.active) { filter: saturate(.55) brightness(.85); }
.fire.light.active { box-shadow: 0 6px 0 rgba(0, 0, 0, .45), 0 0 36px var(--light); }
.fire.pressed { transform: translateY(4px); box-shadow: 0 2px 0 rgba(0, 0, 0, .45); }
.fire.water.active { box-shadow: 0 2px 0 rgba(0, 0, 0, .45), 0 0 40px var(--water); }

/* Portrait phones and tablets: video on top, controls below. */
@media (max-aspect-ratio: 1/1) {
  .stage { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .controls { flex-direction: row; justify-content: space-around; gap: 12px; }
  .angles { order: 3; display: none; }
}
@media (max-width: 420px) {
  .dpad { grid-template-columns: repeat(3, 60px); grid-template-rows: repeat(3, 60px); }
  .fire { width: 124px; height: 124px; }
  .pill { font-size: .7rem; padding: 3px 8px; }
  .brand { font-size: 1.05rem; }
}

/* ---- settings ---- */
dialog {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0; border: 0; border-radius: var(--radius);
  background: var(--panel); color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, .6); }
.settings { display: flex; flex-direction: column; }
.settings header {
  position: sticky; top: 0; z-index: 1;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--panel-2);
}
.settings h2 { margin: 0; font-size: 1.2rem; }
.settings .x { border: 0; background: var(--panel-2); width: 40px; height: 40px; border-radius: 50%; }
.settings section { padding: 12px 16px; border-bottom: 1px solid var(--panel-2); }
.settings h3 { margin: 4px 0 10px; font-size: 1.05rem; }
.settings label {
  display: grid; grid-template-columns: 7.5em 1fr 3.5em; align-items: center; gap: 10px;
  margin: 8px 0; color: var(--muted);
}
.settings label.check { display: flex; gap: 10px; color: var(--text); }
.settings select, .settings input[type=color] {
  grid-column: 2 / 4; min-height: 36px; border-radius: 10px; border: 0;
  background: var(--panel-2); color: var(--text); padding: 0 8px;
}
.settings input[type=color] { padding: 2px; width: 72px; }
.settings input[type=range] { width: 100%; accent-color: var(--accent); }
.settings input[type=checkbox], .settings input[type=radio] { width: 20px; height: 20px; accent-color: var(--accent); }
.settings output { color: var(--text); font-variant-numeric: tabular-nums; text-align: right; }

.limit { display: grid; grid-template-columns: 6.5em 1fr 3em auto; align-items: center; gap: 10px; margin: 8px 0; }
.limit span { color: var(--muted); }
.limit output { text-align: right; font-variant-numeric: tabular-nums; }
.limit button, .row button {
  border: 0; border-radius: 10px; background: var(--panel-2); padding: 8px 12px;
}
.row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.row button.primary { background: var(--accent); color: #14161f; font-weight: 700; }

.segmented { display: flex; flex-direction: column; gap: 4px; }
.segmented label { display: flex; gap: 10px; color: var(--text); }

.hint { color: var(--muted); font-size: .85rem; margin: 8px 0 0; }
.hint.good { color: var(--good); }
.hint.bad { color: var(--bad); }

.info { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; font-size: .9rem; }
.info dt { color: var(--muted); }
.info dd { margin: 0; overflow-wrap: anywhere; }

/* ---- login ---- */
.login {
  margin: auto; width: min(360px, calc(100vw - 32px));
  background: var(--panel); border-radius: var(--radius); padding: 28px 24px;
  display: flex; flex-direction: column; gap: 14px;
}
.login h1 { margin: 0 0 4px; text-align: center; }
.login input {
  font: inherit; padding: 12px 14px; border-radius: 12px; border: 0;
  background: var(--panel-2); color: var(--text);
}
.login button {
  padding: 12px; border: 0; border-radius: 12px; background: var(--accent);
  color: #14161f; font-weight: 800; font-size: 1.05rem;
}
.login .error { color: var(--bad); text-align: center; margin: 0; }
