:root {
  color-scheme: light;
  --bg: #e8eee4;
  --bg-deep: #1f2a1c;
  --ink: #182016;
  --muted: #5c6756;
  --card: #fbfcf8;
  --line: #d3ddd0;
  --accent: #2f5d3a;
  --accent-ink: #fff;
  --err: #9b2c2c;
  --shadow: 0 18px 50px rgba(31, 42, 28, 0.12);
  --radius: 1.1rem;
  --pad: max(1rem, env(safe-area-inset-left)) max(1rem, env(safe-area-inset-right));
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

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

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background:
    radial-gradient(1200px 500px at 50% -10%, #f7faf5 0%, transparent 60%),
    var(--bg);
  color: var(--ink);
  line-height: 1.45;
}

main {
  min-height: 100dvh;
}

h1 {
  font-size: clamp(1.35rem, 3vw, 1.7rem);
  font-weight: 650;
  letter-spacing: -0.03em;
  margin: 0;
}

.eyebrow {
  margin: 0 0 0.2rem;
  font-size: 0.78rem;
  font-weight: 650;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.lede {
  margin: 0.4rem 0 0;
  color: var(--muted);
}

#pin-screen:not([hidden]) {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: var(--pad);
  padding-top: max(1.5rem, env(safe-area-inset-top));
  padding-bottom: max(1.5rem, env(safe-area-inset-bottom));
}

.card {
  width: min(100%, 24rem);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.6rem 1.4rem 1.5rem;
}

#pin-form {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  margin-top: 1.2rem;
}

label {
  font-size: 0.85rem;
  font-weight: 600;
}

input, button {
  font: inherit;
}

input {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 0.7rem;
  padding: 0.85rem 0.9rem;
  font-size: 1.15rem;
  letter-spacing: 0.18em;
}

input:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button {
  cursor: pointer;
  border: 0;
  border-radius: 0.75rem;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 650;
  padding: 0.85rem 1rem;
  min-height: 2.85rem;
}

button:hover { filter: brightness(1.06); }
button:active { transform: translateY(1px); }

.ghost {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
}

a.ghost {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}

.header-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
}

button.danger {
  background: #6b3a2c;
}

#composite:not([hidden]) {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: max(0.75rem, env(safe-area-inset-top)) var(--pad) max(0.85rem, env(safe-area-inset-bottom));
  gap: 0.75rem;
}

.card.wide {
  width: min(100%, 36rem);
}

#confirm-stats {
  display: grid;
  grid-template-columns: 6.5rem 1fr;
  gap: 0.25rem 0.75rem;
  margin: 1.1rem 0 0;
}

#confirm-stats dt {
  font-weight: 650;
}

#confirm-stats dd {
  margin: 0;
  color: var(--muted);
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 1.2rem;
  align-items: center;
}

.progress-wrap {
  margin-top: 1rem;
}

progress {
  display: block;
  width: 100%;
  height: 0.75rem;
}

#mosaic:not([hidden]) {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  gap: 0.75rem;
}

.osd {
  flex: 1;
  min-height: min(70dvh, calc(100dvh - 8rem));
  background: var(--bg-deep);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
}

.osd:active {
  cursor: grabbing;
}

.osd img {
  display: block;
  max-width: none;
}

.mosaic-actions {
  margin-top: 0;
}

#viewer:not([hidden]) {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: max(0.75rem, env(safe-area-inset-top)) var(--pad) max(0.85rem, env(safe-area-inset-bottom));
  gap: 0.75rem;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.live {
  display: inline-block;
  width: 0.55rem;
  height: 0.55rem;
  margin-right: 0.35rem;
  border-radius: 50%;
  background: #c44536;
  box-shadow: 0 0 0 0 rgba(196, 69, 54, 0.5);
  animation: pulse 1.8s ease-out infinite;
  vertical-align: 0.05rem;
}

@keyframes pulse {
  70% { box-shadow: 0 0 0 0.45rem transparent; }
}

.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  min-height: 0;
}

.frame {
  flex: 0 1 auto;
  background: var(--bg-deep);
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
}

.frame video {
  display: block;
  width: 100%;
  height: auto;
  min-height: 12rem;
  max-height: min(58dvh, 100%);
  object-fit: contain;
  background: var(--bg-deep);
}

.pad {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 0.85rem;
  max-width: 18rem;
  width: 100%;
  margin: 0 auto;
}

.pad-zoom {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  width: 4rem;
  flex: 0 0 4rem;
}

.pad-dpad {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  width: 8.5rem;
  height: 8.5rem;
  flex: 0 0 auto;
}

.dpad-body {
  grid-column: 1 / -1;
  grid-row: 1 / -1;
  pointer-events: none;
  position: relative;
}

.dpad-body::before,
.dpad-body::after {
  content: "";
  position: absolute;
  background: #726f64;
}

.dpad-body::before {
  top: 0;
  bottom: 0;
  left: 33.34%;
  right: 33.34%;
  border-radius: 0.7rem;
}

.dpad-body::after {
  left: 0;
  right: 0;
  top: 33.34%;
  bottom: 33.34%;
  border-radius: 0.7rem;
}

.pad-dpad .dpad-up { grid-column: 2; grid-row: 1; }
.pad-dpad .dpad-left { grid-column: 1; grid-row: 2; }
.pad-dpad .dpad-right { grid-column: 3; grid-row: 2; }
.pad-dpad .dpad-down { grid-column: 2; grid-row: 3; }

.pad button {
  min-width: 0;
  min-height: 3rem;
  padding: 0;
  margin: 0;
  font-size: 1.15rem;
  line-height: 0;
  display: grid;
  place-items: center;
  appearance: none;
  -webkit-appearance: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  background: #726f64;
}

.pad-zoom button {
  border-radius: 0.75rem;
  width: 100%;
  height: auto;
  min-height: 0;
  aspect-ratio: 1 / 1;
}

.pad-zoom button svg {
  width: 1.81rem;
  height: 1.81rem;
}

.pad-dpad button {
  z-index: 1;
  min-height: 0;
  background: transparent;
  color: #d5ddd2;
  border-radius: 0.45rem;
}

.pad button svg {
  width: 1.45rem;
  height: 1.45rem;
  display: block;
  margin: 0;
}

.pad-dpad button svg {
  width: 1.15rem;
  height: 1.15rem;
}

.pad-zoom button.is-held,
.pad-zoom button:active {
  background: #8b887c;
}

.pad-dpad button.is-held,
.pad-dpad button:active {
  background: rgba(255, 255, 255, 0.22);
  transform: none;
}

.sheet-handle {
  display: none;
}

.error {
  color: var(--err);
  margin: 0.75rem 0 0;
  font-size: 0.95rem;
}

.frame .error {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 1rem;
  background: rgba(18, 20, 16, 0.82);
  color: #f3d0d0;
  text-align: center;
}

@media (min-width: 800px) {
  .stage {
    flex-direction: row;
    align-items: stretch;
  }

  .frame {
    flex: 1;
    min-height: calc(100dvh - 6.5rem);
  }

  .frame video {
    height: 100%;
    max-height: none;
  }

  .pad {
    margin: auto 0;
    width: auto;
    max-width: none;
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  #viewer { padding-top: 0.4rem; }
  header h1 { font-size: 1.1rem; }
  .stage { flex-direction: row; }
  .frame { min-height: auto; }
  .pad {
    width: auto;
    margin: auto 0;
  }
  .pad button { min-height: 2.5rem; }
}

html:has(#viewer.phone:not([hidden])),
body:has(#viewer.phone:not([hidden])) {
  overflow: hidden;
  height: 100%;
  overscroll-behavior: none;
  background: #ffffff;
}

#viewer.phone:not([hidden]) {
  padding: 0;
  gap: 0;
  height: 100dvh;
  max-height: 100dvh;
  background: var(--bg-deep);
  overflow: hidden;
}

#viewer.phone header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 5;
  padding: max(0.5rem, env(safe-area-inset-top)) 0.85rem 1.4rem;
  background: linear-gradient(to bottom, rgba(18, 20, 16, 0.55), transparent);
}

#viewer.phone header h1,
#viewer.phone #unmute {
  display: none;
}

#viewer.phone .eyebrow {
  color: #e8eee4;
}

#viewer.phone .ghost {
  color: #e8eee4;
  border-color: rgba(232, 238, 228, 0.35);
  min-height: 2.2rem;
  padding: 0.35rem 0.7rem;
}

#viewer.phone .stage,
#viewer.phone .frame {
  flex: 1;
  min-height: 0;
  height: 100%;
  border-radius: 0;
}

#viewer.phone .frame {
  overflow: hidden;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

#viewer.phone .frame video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  max-height: none;
  min-height: 0;
  object-fit: cover;
}

.unmute-hint {
  display: none;
}

#viewer.phone .unmute-hint:not([hidden]) {
  display: block;
  position: absolute;
  z-index: 4;
  left: 50%;
  bottom: calc(3.1rem + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  margin: 0;
  padding: 0.45rem 0.8rem;
  border-radius: 999px;
  background: rgba(18, 20, 16, 0.62);
  color: #f3f6f0;
  font-size: 0.85rem;
  font-weight: 650;
  letter-spacing: 0.02em;
  pointer-events: none;
}

#viewer.phone.sheet-open .unmute-hint {
  bottom: calc(25dvh + env(safe-area-inset-bottom, 0px) + 0.45rem);
}

#viewer.phone .controls {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 6;
  display: flex;
  flex-direction: column;
  height: 25dvh;
  min-height: 10.75rem;
  background: #ffffff;
  border-radius: 1rem 1rem 0 0;
  box-shadow: 0 -10px 28px rgba(18, 20, 16, 0.18);
  transform: translateY(calc(100% - 2.35rem - env(safe-area-inset-bottom, 0px)));
  transition: transform 0.28s ease;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  touch-action: none;
}

#viewer.phone.sheet-open .controls {
  transform: translateY(0);
}

#viewer.phone .sheet-handle {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 2.35rem;
  width: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 1rem 1rem 0 0;
  background: #ffffff;
  min-height: 2.35rem;
  transform: none;
}

#viewer.phone .sheet-handle:active {
  transform: none;
  filter: none;
}

#viewer.phone .sheet-grip {
  display: block;
  width: 4.75rem;
  height: 0.3rem;
  border-radius: 999px;
  background: #e1e1e6;
}

#viewer.phone .pad {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  justify-content: space-between;
  gap: 1rem;
  width: 100%;
  max-width: none;
  flex: 1;
  min-height: 0;
  margin: 0;
  padding: 0 1.7rem 0.55rem;
}

#viewer.phone .pad-zoom {
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  gap: 0.4rem;
  width: 3.88rem;
  flex: 0 0 3.88rem;
}

#viewer.phone .pad-dpad {
  width: min(8.61rem, 44.1vw);
  height: min(8.61rem, 44.1vw);
  align-self: center;
}

#viewer.phone .pad-dpad button svg {
  width: 1.21rem;
  height: 1.21rem;
}

#viewer.phone .pad-zoom button {
  min-height: 0;
  width: 100%;
  aspect-ratio: 1 / 1;
}

@media (orientation: landscape) {
  #viewer.phone header {
    right: auto;
    width: auto;
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
    gap: 0;
  }

  #viewer.phone .header-actions {
    position: fixed;
    left: max(0.85rem, env(safe-area-inset-left, 0px));
    bottom: max(0.85rem, env(safe-area-inset-bottom, 0px));
    top: auto;
    right: auto;
    z-index: 7;
  }

  #viewer.phone .unmute-hint:not([hidden]),
  #viewer.phone.sheet-open .unmute-hint {
    left: 50%;
    right: auto;
    top: max(0.55rem, env(safe-area-inset-top, 0px));
    bottom: auto;
    transform: translateX(-50%);
    z-index: 5;
  }

  #viewer.phone .controls {
    left: auto;
    right: 0;
    top: 0;
    bottom: 0;
    width: 25vw;
    min-width: 11.75rem;
    height: auto;
    min-height: 0;
    flex-direction: row;
    border-radius: 0;
    box-shadow: -10px 0 28px rgba(18, 20, 16, 0.18);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    padding-top: env(safe-area-inset-top, 0px);
    padding-right: env(safe-area-inset-right, 0px);
    transform: translateX(calc(100% - 2.35rem - env(safe-area-inset-right, 0px)));
  }

  #viewer.phone.sheet-open .controls {
    transform: translateX(0);
  }

  #viewer.phone .sheet-handle {
    flex: 0 0 2.35rem;
    width: 2.35rem;
    height: auto;
    min-height: 0;
    align-self: stretch;
    border-radius: 0;
  }

  #viewer.phone .sheet-grip {
    width: 0.3rem;
    height: 4.75rem;
  }

  #viewer.phone .pad {
    flex-direction: column;
    align-items: center;
    justify-content: space-evenly;
    padding: 0.4rem 0.7rem 0.4rem 0.35rem;
  }

  #viewer.phone .pad-zoom {
    flex-direction: row;
    width: auto;
    flex: 0 0 auto;
    gap: 0.5rem;
  }

  #viewer.phone .pad-zoom button {
    width: 3.88rem;
    height: 3.88rem;
    flex: 0 0 3.88rem;
    min-height: 0;
    aspect-ratio: 1 / 1;
  }

  #viewer.phone .pad-dpad {
    width: min(8.61rem, 100%);
    height: min(8.61rem, 54.6dvh);
  }
}

@media (prefers-reduced-motion: reduce) {
  .live { animation: none; }
  button:active { transform: none; }
  #viewer.phone .controls { transition: none; }
}
