:root {
  color-scheme: dark;
  --bg: #08090d;
  --panel: #141720;
  --panel-soft: #1b2030;
  --text: #f7f7fb;
  --muted: #a9adba;
  --line: #2a3142;
  --accent: #f4f0e8;
  --accent-text: #111217;
  --danger: #ff7f8e;
  --warning: #ffd27a;
  --success: #8df0b2;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.08), transparent 34rem),
    var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button,
input,
textarea {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

.app-shell {
  width: 100%;
  max-width: 430px;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  overflow: hidden;
  padding: max(10px, env(safe-area-inset-top)) 16px max(12px, env(safe-area-inset-bottom));
}

.screen {
  height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 12px;
  justify-content: center;
}

.screen[hidden] {
  display: none;
}

.center-screen {
  align-items: center;
  text-align: center;
}

.brand-block {
  display: grid;
  gap: 8px;
}

.eyebrow {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  max-width: 12ch;
  font-size: 2.35rem;
  line-height: 0.95;
  letter-spacing: 0;
}

h2 {
  font-size: 1.35rem;
  line-height: 1.1;
}

.stack,
.button-stack,
.home-actions {
  display: grid;
  gap: 12px;
}

label {
  color: var(--muted);
  font-size: 0.98rem;
}

input[type="password"],
input[type="text"],
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  padding: 15px 14px;
  outline: none;
}

textarea {
  resize: vertical;
  min-height: 112px;
}

input[type="password"]:focus,
input[type="text"]:focus,
textarea:focus {
  border-color: var(--accent);
}

.primary-button,
.secondary-button {
  min-height: 48px;
  border-radius: 8px;
  padding: 15px 18px;
  font-weight: 750;
}

.primary-button {
  background: var(--accent);
  color: var(--accent-text);
}

.secondary-button {
  background: var(--panel-soft);
  color: var(--text);
  border: 1px solid var(--line);
}

.primary-button:disabled,
.secondary-button:disabled {
  opacity: 0.55;
}

.text-button {
  width: max-content;
  padding: 8px 0;
  background: transparent;
  color: var(--muted);
  text-decoration: underline;
}

.icon-button {
  min-width: 64px;
  min-height: 40px;
  border-radius: 999px;
  padding: 9px 13px;
  background: var(--panel-soft);
  color: var(--text);
  border: 1px solid var(--line);
  font-weight: 750;
}

.floating-home {
  position: absolute;
  top: max(10px, env(safe-area-inset-top));
  right: 16px;
}

.logout-button {
  position: absolute;
  top: max(14px, env(safe-area-inset-top));
  right: 16px;
}

.top-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.muted {
  color: var(--muted);
  line-height: 1.45;
}

.warning-text,
.error-text {
  line-height: 1.4;
}

.warning-text {
  color: var(--warning);
}

.error-text {
  color: var(--danger);
}

.crop-frame {
  position: relative;
  width: min(100%, 230px);
  height: 409px;
  margin: 0 auto;
  overflow: hidden;
  background: #000;
  border: 1px solid var(--line);
  border-radius: 8px;
  touch-action: none;
}

.crop-frame::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.12);
}

#cropImage {
  position: absolute;
  top: 50%;
  left: 50%;
  max-width: none;
  user-select: none;
  -webkit-user-drag: none;
  transform-origin: center;
  will-change: transform;
}

.story-text-preview {
  position: absolute;
  left: 50%;
  top: 48%;
  max-width: 88%;
  transform: translate(-50%, -50%);
  color: #fff;
  font-size: 40px;
  font-weight: 850;
  line-height: 1.05;
  text-align: center;
  overflow-wrap: anywhere;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.75);
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.slider-row {
  display: grid;
  gap: 6px;
}

.editor-tools {
  display: grid;
  gap: 8px;
}

.editor-tools label,
.tool-row label {
  font-size: 0.82rem;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.tool-row {
  display: grid;
  gap: 6px;
}

.color-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.color-swatch {
  min-height: 36px;
  border-radius: 8px;
  background: var(--panel-soft);
  color: var(--text);
  border: 1px solid var(--line);
  font-size: 0.78rem;
  font-weight: 750;
}

.color-swatch::before {
  content: "";
  display: inline-block;
  width: 12px;
  height: 12px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--swatch);
  border: 1px solid rgba(255, 255, 255, 0.5);
  vertical-align: -1px;
}

.color-swatch.is-selected {
  border-color: var(--accent);
}

.media-preview,
.final-preview video {
  width: 100%;
  max-height: 440px;
  border-radius: 8px;
  background: #000;
  object-fit: contain;
  border: 1px solid var(--line);
}

.final-preview {
  display: grid;
  place-items: center;
}

.final-preview img {
  width: auto;
  height: min(52vh, 430px);
  max-width: 100%;
  aspect-ratio: 9 / 16;
  border-radius: 8px;
  background: #000;
  object-fit: contain;
  border: 1px solid var(--line);
}

.status-block {
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.status-block.success h2 {
  color: var(--success);
}

.status-block.error h2 {
  color: var(--danger);
}

.story-status {
  display: grid;
  gap: 10px;
  justify-items: center;
  text-align: center;
}

.story-status[hidden],
.story-viewer[hidden],
.story-controls[hidden] {
  display: none;
}

.story-loader {
  width: 30px;
  height: 30px;
}

.story-viewer {
  display: grid;
  gap: 10px;
  justify-items: center;
}

.story-frame {
  width: min(100%, 285px, calc(min(62vh, 506px) * 9 / 16));
  aspect-ratio: 9 / 16;
  overflow: hidden;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #000;
}

.story-frame img,
.story-frame video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
}

.story-meta {
  min-height: 22px;
  text-align: center;
}

.story-controls {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.story-fallback {
  display: grid;
  place-items: center;
  width: 100%;
  text-align: center;
  text-decoration: none;
}

.debug-box {
  max-height: 150px;
  overflow: auto;
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
  white-space: pre-wrap;
  font-size: 0.8rem;
}

.loader {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 4px solid var(--line);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-height: 720px) {
  .screen {
    justify-content: flex-start;
  }

  h1 {
    font-size: 2.05rem;
  }

  .crop-frame {
    width: min(100%, 210px);
    height: 373px;
  }

  .media-preview,
  .final-preview video {
    max-height: 385px;
  }

  .story-frame {
    width: min(100%, 230px, calc(min(55vh, 409px) * 9 / 16));
  }

  .final-preview img {
    height: min(50vh, 370px);
  }

  .button-stack,
  .home-actions {
    gap: 8px;
  }

  input[type="password"],
  input[type="text"],
  textarea {
    padding: 12px;
  }
}
