:root {
  --bg: #0f1216;
  --fg: #e8eaed;
  --muted: #93989f;
  --primary: #4f8cff;
  --primary-hover: #3a72e0;
  --danger: #e0524f;
  --border: #262a31;
  --image-width: 172px;
  --image-height: 288px;
  --display-scale: 2;
  /* Reserved vertical space for heading, hint, capture button, and page
     padding. The stage will shrink (preserving aspect ratio) if the
     viewport can't fit both this chrome and the display-scale size. */
  --camera-chrome-space: 14rem;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 1.5rem 1rem 4rem;
}

h1 { font-size: 1.75rem; margin: 0 0 0.5rem; }
h2 { font-size: 1.25rem; margin: 1.5rem 0 0.75rem; }
p  { margin: 0.5rem 0; }
.subtle { color: var(--muted); font-size: 0.9rem; }
.error  { color: var(--danger); }
.inline { display: inline; }

.welcome {
  text-align: center;

  p {
    line-height: 1.5;
  }
}

#user-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}
#user-bar .btn.link { padding: 0; }
.user-bar-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.qr-link {
  font-size: 1.2rem;
  line-height: 1;
}
h1.page-title {
  text-align: center;
  margin: 0;
}
#title-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 0.5rem;
  margin: 0.25rem 0 0.5rem;
}
#name-display,
#name-input { grid-column: 2; justify-self: center; }
#edit-name-btn { grid-column: 3; justify-self: start; }
.title-btn {
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}
.title-btn:hover {
  color: var(--fg);
  background: #1a1f26;
}
#name-input {
  font-size: 1.75rem;
  font-weight: bold;
  text-align: center;
  background: transparent;
  border: none;
  border-bottom: 2px solid var(--border);
  color: var(--fg);
  padding: 0.25rem 0;
  width: 14rem;
  max-width: 100%;
  outline: none;
}
#name-input:focus {
  border-bottom-color: var(--primary);
}

input[type="text"] {
  display: block;
  width: 100%;
  padding: 0.75rem 1rem;
  margin: 0.75rem 0;
  font-size: 1rem;
  background: #1a1f26;
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
input[type="text"]:focus {
  outline: none;
  border-color: var(--primary);
}

.btn {
  display: inline-block;
  padding: 0.75rem 1.25rem;
  font-weight: 500;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #1a1f26;
  color: var(--fg);
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { background: #232830; }
.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}
.btn.primary:hover { background: var(--primary-hover); }
.btn.link {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 0.5rem 0;
}
.btn.link:hover { color: var(--fg); background: transparent; }
.btn.delete { background: var(--danger); border-color: var(--danger); color: white; }

#frame-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin: 1rem 0;
}

#frame {
  position: relative;
  /* Width and height are computed from the same three constraints so they
     stay in the --image-width : --image-height ratio. (We don't use
     aspect-ratio because var() lengths can't appear directly in a <ratio>.) */
  --stage-width: min(
    calc(var(--image-width) * var(--display-scale)),
    calc((100svh - var(--camera-chrome-space)) * var(--image-width) / var(--image-height)),
    calc(100vw - 5rem)
  );
  width: var(--stage-width);
  height: calc(var(--stage-width) * var(--image-height) / var(--image-width));
  background: #000;
  border-radius: 6px;
  overflow: hidden;
  flex-shrink: 0;
  user-select: none;
  -webkit-user-select: none;
}
#frame[data-mode="camera"][data-state="live"],
#frame[data-mode="camera"][data-state="countdown"] {
  touch-action: none;
  cursor: grab;
}
#frame[data-mode="camera"][data-state="live"]:active,
#frame[data-mode="camera"][data-state="countdown"]:active {
  cursor: grabbing;
}
#frame.is-current {
  outline: 3px solid #ffd84d;
  outline-offset: -3px;
}

#carousel-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  -webkit-user-drag: none;
}

.frame-btn {
  position: absolute;
  width: 36px;
  height: 36px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.65);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  cursor: pointer;
  z-index: 3;
}
.frame-btn.star { top: 0.5rem; left: 0.5rem; color: rgba(255, 255, 255, 0.85); }
.frame-btn.star:hover { color: #ffd84d; }
.frame-btn.star.active { color: #ffd84d; }
.frame-btn.delete { top: 0.5rem; right: 0.5rem; }
.frame-btn.delete:hover { background: var(--danger); }
.frame-btn.switch-cam { bottom: 0.5rem; right: 0.5rem; font-size: 1.1rem; }

.nav-btn {
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: #1a1f26;
  color: var(--fg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  cursor: pointer;
  flex-shrink: 0;
}
.nav-btn:hover { background: #232830; }
.nav-btn:active { background: #2a3038; }

@media (max-width: 480px) {
  .nav-btn {
    width: 36px;
    height: 36px;
    font-size: 1.1rem;
  }
}
#preview {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  pointer-events: none;
  max-width: none;
  display: block;
}
#camera-viewport {
  position: absolute;
  inset: 0;
  border: 2px solid white;
  outline: 2000px solid rgba(0, 0, 0, 0.55);
  pointer-events: none;
  box-sizing: border-box;
}
#countdown {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 8rem;
  font-weight: 700;
  color: white;
  text-shadow: 0 4px 16px rgba(0,0,0,0.6);
  background: rgba(0,0,0,0.2);
}
#camera-hint {
  text-align: center;
}
#camera-status {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  color: var(--muted);
}
.capture-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: white;
  color: #111;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
  padding: 0;
}
.capture-btn:hover { background: #f0f0f0; }
.capture-btn:active { background: #e0e0e0; }
.capture-btn:disabled { opacity: 0.6; cursor: not-allowed; }

.photo-grid {
  list-style: none;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 0.5rem;
}
.photo-thumb {
  position: relative;
  aspect-ratio: 172 / 288;
  border-radius: 8px;
  overflow: hidden;
}
.photo-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.photo-thumb .btn.delete,
.photo-thumb .btn.star {
  position: absolute;
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.65);
  border-color: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95rem;
  color: white;
  margin: 0;
}
.photo-thumb .btn.delete {
  top: 0.25rem;
  right: 0.25rem;
}
.photo-thumb .btn.delete:hover {
  background: var(--danger);
  border-color: var(--danger);
}
.photo-thumb .btn.star {
  top: 0.25rem;
  left: 0.25rem;
  color: rgba(255, 255, 255, 0.85);
}
.photo-thumb .btn.star:hover {
  color: #ffd84d;
}
.photo-thumb .btn.star.active {
  color: #ffd84d;
}
.photo-thumb.current {
  outline: 3px solid #ffd84d;
  outline-offset: -3px;
}

#action-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  justify-content: center;
  margin: 0.5rem 0 1rem;
}

.qr {
  text-align: center;

  p.subtle a {
    color: white;
  }
}
.qr-frame {
  background: white;
  border-radius: 16px;
  padding: 1rem;
  margin: 1.5rem auto;
  max-width: min(80vh, 600px);
}
.qr-frame svg {
  display: block;
  width: 100%;
  height: auto;
}

@media (max-width: 480px) {
  main { padding: 1rem 0.75rem 3rem; }
}
