:root {
  --bg: #f2f2f7;
  --surface: #ffffff;
  --text: #1d1d1f;
  --text-muted: #86868b;
  --accent: #007aff;
  --accent-soft: rgba(0, 122, 255, 0.1);
  --border: rgba(0, 0, 0, 0.08);
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tabbar-gap: 12px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  height: 100%;
}

body {
  min-height: 100%;
  min-height: 100dvh;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
}

.app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  background: var(--bg);
}

.content {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  padding: 16px 16px var(--tabbar-gap);
}

.page {
  display: none;
  flex: 1 1 0;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  animation: fadeIn 0.25s ease;
}

.page.active {
  display: flex;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.page-header {
  flex-shrink: 0;
  margin-bottom: 16px;
}

.page-header__row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.header-reset {
  flex-shrink: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
  padding: 8px 14px;
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s, background 0.2s;
}

.header-reset:active {
  background: var(--bg);
  color: var(--accent);
  border-color: rgba(0, 122, 255, 0.35);
}

.page-title {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}

.page-subtitle {
  margin-top: 4px;
  font-size: 14px;
  color: var(--text-muted);
}

/* Home — 2×2 card grid */
.feature-grid {
  flex: 1 1 0;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 12px;
}

.feature-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 20px 12px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--surface);
  box-shadow: var(--shadow);
  cursor: pointer;
  text-align: center;
  transition: transform 0.15s ease, box-shadow 0.2s ease, border-color 0.2s ease;
}

.feature-card:active {
  transform: scale(0.98);
}

.feature-card:hover {
  border-color: rgba(0, 122, 255, 0.25);
  box-shadow: 0 4px 20px rgba(0, 122, 255, 0.12);
}

.feature-card__icon {
  width: 52px;
  height: 52px;
  border-radius: 16px;
  background: var(--accent-soft);
  display: grid;
  place-items: center;
  color: var(--accent);
}

.feature-card__icon svg {
  width: 26px;
  height: 26px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.feature-card__label {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.35;
  color: var(--text);
}

/* Tasks empty state */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 32px 24px;
}

.empty-icon {
  width: 72px;
  height: 72px;
  border-radius: 20px;
  background: var(--accent-soft);
  display: grid;
  place-items: center;
  color: var(--accent);
  margin-bottom: 20px;
}

.empty-icon svg {
  width: 36px;
  height: 36px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.empty-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 8px;
}

.empty-desc {
  font-size: 14px;
  color: var(--text-muted);
  line-height: 1.5;
  max-width: 260px;
}

.task-list {
  list-style: none;
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto;
  display: none;
  -webkit-overflow-scrolling: touch;
}

.task-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  background: var(--surface);
  border-radius: 12px;
  margin-bottom: 8px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  cursor: default;
}

.task-item--clickable {
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s;
}

.task-item--clickable:active {
  background: var(--accent-soft);
  border-color: rgba(0, 122, 255, 0.25);
}

.task-item__main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.task-id {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.task-elapsed {
  flex-shrink: 0;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.task-item__right {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.task-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(0, 122, 255, 0.2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.task-status {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.03em;
}

.task-status.is-pending {
  color: #8e8e93;
}

.task-status.is-running {
  color: #ff9500;
}

.task-status.is-succeeded {
  color: #34c759;
}

.task-status.is-failed {
  color: #ff3b30;
}

.task-status.is-unknown {
  color: #8e8e93;
}

/* Bottom tab bar */
.tab-bar {
  display: flex;
  flex-shrink: 0;
  height: calc(56px + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.tab {
  flex: 1;
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: color 0.2s;
}

.tab.active {
  color: var(--accent);
}

.tab svg {
  width: 22px;
  height: 22px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Subpage (hide tab bar) */
.app.subpage-mode .tab-bar {
  display: none;
}

.app.subpage-mode .content {
  padding-bottom: 16px;
}

.subpage-header {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
  margin-bottom: 16px;
}

.subpage-back {
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  display: grid;
  place-items: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s;
}

.subpage-back:active {
  background: var(--bg);
}

.subpage-back svg {
  width: 20px;
  height: 20px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.subpage-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
  text-transform: lowercase;
}

.subpage-body {
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding-bottom: 8px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.field-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: 0.02em;
}

.prompt-input {
  width: 100%;
  min-height: 120px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  font-family: inherit;
  resize: vertical;
  box-shadow: var(--shadow);
  transition: border-color 0.2s, box-shadow 0.2s;
}

.prompt-input::placeholder {
  color: #aeaeb2;
}

.prompt-input:focus {
  outline: none;
  border-color: rgba(0, 122, 255, 0.45);
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.12);
}

.option-section {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.chip-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
  padding: 10px 16px;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}

.chip.active {
  background: var(--accent-soft);
  border-color: rgba(0, 122, 255, 0.35);
  color: var(--accent);
  font-weight: 600;
}

.chip:active {
  transform: scale(0.98);
}

.generate-btn {
  flex-shrink: 0;
  width: 100%;
  margin-top: 12px;
  border: none;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  padding: 16px;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.15s;
}

.generate-btn:active:not(:disabled) {
  transform: scale(0.98);
  opacity: 0.9;
}

.generate-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.field-hint {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.4;
}

.frame-upload-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.frame-upload {
  position: relative;
}

.frame-upload__btn {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  aspect-ratio: 3 / 4;
  border: 1.5px dashed var(--border);
  border-radius: 16px;
  background: var(--surface);
  padding: 0;
  cursor: pointer;
  overflow: hidden;
  box-shadow: var(--shadow);
  transition: border-color 0.2s;
  -webkit-appearance: none;
  appearance: none;
}

.frame-upload__btn:active {
  transform: scale(0.98);
}

.frame-upload.has-image .frame-upload__btn {
  border-style: solid;
  border-color: rgba(0, 122, 255, 0.25);
}

.frame-upload__placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 100%;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
}

.frame-upload__placeholder svg {
  width: 28px;
  height: 28px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.frame-upload.has-image .frame-upload__placeholder {
  display: none;
}

.frame-upload__preview {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  object-position: center;
}

.frame-upload__remove {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  z-index: 1;
}

.frame-upload__btn--wide {
  aspect-ratio: 16 / 9;
}

.r2v-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.r2v-add-btn {
  flex-shrink: 0;
  border: 1px solid rgba(0, 122, 255, 0.35);
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  padding: 8px 14px;
  cursor: pointer;
}

.r2v-add-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.r2v-ref-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.r2v-ref-card {
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  padding: 14px;
  box-shadow: var(--shadow);
}

.r2v-ref-card__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}

.r2v-ref-card__title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.r2v-ref-card__remove {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  padding: 4px 8px;
}

.r2v-ref-card__remove:disabled,
.r2v-ref-card__remove[hidden] {
  display: none;
}

.r2v-type-group {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.r2v-type-btn {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  padding: 10px 12px;
  cursor: pointer;
}

.r2v-type-btn.active {
  background: var(--accent-soft);
  border-color: rgba(0, 122, 255, 0.35);
  color: var(--accent);
  font-weight: 600;
}

.r2v-ref-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.r2v-ref-panel[hidden] {
  display: none;
}

.r2v-ref-card > .r2v-voice-upload {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.r2v-ref-image-row {
  max-width: 160px;
}

.r2v-ref-image-row .frame-upload__btn {
  aspect-ratio: 1 / 1;
}

.r2v-s3-upload {
  position: relative;
  border: 1.5px dashed var(--border);
  border-radius: 16px;
  background: var(--surface);
  padding: 16px;
  box-shadow: var(--shadow);
}

.r2v-s3-upload__btn {
  width: 100%;
  border: none;
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 14px;
  font-weight: 600;
  padding: 12px 16px;
  cursor: pointer;
}

.r2v-s3-upload__btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.r2v-s3-upload__progress {
  margin-top: 12px;
}

.r2v-s3-upload__progress-track {
  height: 6px;
  border-radius: 999px;
  background: rgba(0, 122, 255, 0.12);
  overflow: hidden;
}

.r2v-s3-upload__progress-bar {
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: var(--accent);
  transition: width 0.2s ease;
}

.r2v-s3-upload__progress-bar.is-indeterminate {
  width: 35% !important;
  animation: r2vProgressPulse 1.2s ease-in-out infinite;
}

@keyframes r2vProgressPulse {
  0% { transform: translateX(-120%); }
  100% { transform: translateX(320%); }
}

.r2v-s3-upload__progress-text {
  display: block;
  margin-top: 8px;
  font-size: 12px;
  color: var(--accent);
  line-height: 1.4;
}

.r2v-s3-upload__url {
  display: block;
  margin-top: 8px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--accent);
  word-break: break-all;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  text-decoration: none;
}

.r2v-s3-upload__url:hover {
  text-decoration: underline;
}

.r2v-s3-upload__status {
  margin-top: 10px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--text-muted);
  white-space: pre-wrap;
  word-break: break-all;
}

.r2v-s3-upload__status.is-uploading {
  color: var(--accent);
}

.r2v-s3-upload__status.is-done {
  color: var(--text);
}

.r2v-s3-upload__remove {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}

.r2v-first-frame {
  max-width: 240px;
}

.ve-ref-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.ve-ref-card {
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  padding: 14px;
  box-shadow: var(--shadow);
}

.ve-ref-card__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}

.ve-ref-card__title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.ve-ref-card__remove {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  padding: 4px 8px;
}

.ve-ref-image-row {
  max-width: 160px;
}

.ve-ref-image-row .frame-upload__btn {
  aspect-ratio: 1 / 1;
}

.prompt-input--compact {
  min-height: 72px;
}

.text-input {
  width: 100%;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  box-shadow: var(--shadow);
  transition: border-color 0.2s, box-shadow 0.2s;
}

.text-input::placeholder {
  color: #aeaeb2;
  font-family: inherit;
}

.text-input:focus {
  outline: none;
  border-color: rgba(0, 122, 255, 0.45);
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.12);
}

/* API Key dialog */
.api-key-dialog {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.api-key-dialog.visible {
  display: flex;
}

.api-key-dialog__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.api-key-dialog__panel {
  position: relative;
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 24px 20px 20px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.api-key-dialog__title {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
}

.api-key-dialog__desc {
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-muted);
  margin-top: -8px;
}

.api-key-dialog__current {
  font-size: 12px;
  line-height: 1.5;
  color: var(--text-muted);
  word-break: break-all;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--bg);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.api-key-dialog__actions {
  display: flex;
  gap: 10px;
  margin-top: 4px;
}

.dialog-btn {
  flex: 1;
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  padding: 14px;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.15s;
}

.dialog-btn:active {
  transform: scale(0.98);
}

.dialog-btn--primary {
  background: var(--accent);
  color: #fff;
}

.dialog-btn--secondary {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
}

.api-key-dialog.required .dialog-btn--secondary {
  display: none;
}

.app-dialog {
  position: fixed;
  inset: 0;
  z-index: 1100;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.app-dialog.visible {
  display: flex;
}

.app-dialog__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.app-dialog__panel {
  position: relative;
  width: 100%;
  max-width: 340px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 24px 20px 20px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.app-dialog__title {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  text-align: center;
}

.app-dialog__message {
  font-size: 15px;
  line-height: 1.6;
  color: var(--text-muted);
  text-align: center;
  white-space: pre-wrap;
  word-break: break-word;
}

.app-dialog__message--left {
  text-align: left;
}

.dialog-btn--full {
  width: 100%;
}

/* Preview page */
.preview-body {
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.preview-frame {
  flex: 1 1 0;
  min-height: 0;
  position: relative;
  border-radius: 16px;
  background: #000;
  border: 1px solid var(--border);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.preview-loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: rgba(255, 255, 255, 0.75);
  font-size: 14px;
  z-index: 1;
}

.preview-loading.hidden {
  display: none;
}

.preview-video {
  display: none;
  width: 100%;
  height: 100%;
  max-height: 100%;
  object-fit: contain;
  background: #000;
}

.preview-video.loaded {
  display: block;
}

.preview-meta {
  flex-shrink: 0;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  background: var(--surface);
  font-size: 11px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--text-muted);
  word-break: break-all;
  line-height: 1.4;
  text-align: left;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}

.preview-meta:active {
  background: var(--accent-soft);
  border-color: rgba(0, 122, 255, 0.35);
  color: var(--accent);
}

.preview-meta.is-copied {
  color: var(--accent);
  border-color: rgba(0, 122, 255, 0.35);
}
