:root {
  --bg: #f4efe6;
  --surface: #fffdf8;
  --surface-2: #f8f3ea;
  --text-primary: #1f2a2e;
  --text-secondary: #5e666a;
  --accent: #b07a3f;
  --accent-2: #d6a56a;
  --success: #2e7d32;
  --error: #c0392b;
  --border: #e5d9c8;
  --shadow: 0 10px 30px rgba(31, 42, 46, 0.08);
  --radius-card: 14px;
  --radius-control: 10px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Noto Sans SC", sans-serif;
  color: var(--text-primary);
  background: linear-gradient(140deg, #f4efe6 0%, #efe5d3 35%, #f7f3eb 100%);
}

body.modal-open {
  overflow: hidden;
}

body.thumb-touch-sorting {
  user-select: none;
}

.page-bg {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image: radial-gradient(circle at 15% 20%, rgba(176, 122, 63, 0.12), transparent 35%),
    radial-gradient(circle at 80% 10%, rgba(31, 42, 46, 0.08), transparent 30%);
}

.app-shell {
  max-width: 1280px;
  margin: 0 auto;
  padding: 20px 24px 36px;
  position: relative;
  z-index: 1;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow);
}

.app-header {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  align-items: center;
  padding: 22px 24px;
}

h1 {
  margin: 0;
  font-family: "Noto Serif SC", serif;
  font-size: 32px;
  line-height: 1.2;
}

.subtitle {
  margin: 10px 0 0;
  color: var(--text-secondary);
  font-size: 14px;
}

.status-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  border-radius: 999px;
  padding: 8px 12px;
  background: #ecf6ed;
  color: var(--success);
}

.status-chip .dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: currentColor;
}

.status-chip[data-status="running"] {
  background: #fff1dc;
  color: #9c5f25;
}

.input-zone {
  margin-top: 16px;
  display: grid;
  grid-template-columns: 7fr 5fr;
  gap: 16px;
}

.upload-card,
.params-card,
.progress-card,
.preview-card,
.history-card {
  padding: 20px;
}

h2 {
  margin: 0 0 16px;
  font-family: "Noto Serif SC", serif;
  font-size: 20px;
}

h3 {
  margin: 0 0 10px;
  font-size: 15px;
  font-weight: 600;
}

.block-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.ghost-btn,
.file-btn,
.secondary-btn,
.danger-btn,
.primary-btn {
  border: none;
  cursor: pointer;
  border-radius: var(--radius-control);
  min-height: 40px;
  padding: 0 14px;
  font-family: inherit;
  font-size: 14px;
  transition: all 0.18s ease;
}

.ghost-btn,
.file-btn,
.secondary-btn,
.danger-btn {
  background: var(--surface-2);
  color: var(--text-primary);
  border: 1px solid var(--border);
  display: inline-flex;
  align-items: center;
}

.danger-btn {
  background: #fff4f2;
  color: #b53a2f;
  border-color: #e8b8b2;
}

.primary-btn {
  background: linear-gradient(120deg, var(--accent) 0%, var(--accent-2) 100%);
  color: #fff;
  font-weight: 700;
}

.mini-btn {
  min-height: 34px;
  padding: 0 10px;
  font-size: 13px;
}

.ghost-btn:hover,
.file-btn:hover,
.secondary-btn:hover,
.danger-btn:hover {
  background: #f0e8db;
}

.danger-btn:hover {
  background: #ffe7e3;
}

.primary-btn:hover {
  filter: brightness(0.95);
}

.primary-btn:disabled,
.secondary-btn:disabled,
.ghost-btn:disabled,
.danger-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.thumb-grid {
  min-height: 130px;
  border: 1px dashed var(--border);
  border-radius: var(--radius-control);
  padding: 10px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  background: #faf6ef;
}

.thumb-item {
  position: relative;
  border-radius: 10px;
  overflow: hidden;
  aspect-ratio: 1/1;
  animation: thumb-in 0.18s ease;
  cursor: grab;
}

.thumb-item:active {
  cursor: grabbing;
}

.thumb-item.dragging {
  opacity: 0.55;
  transform: scale(0.98);
}

.thumb-item.drag-over {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.touch-drag-ghost {
  position: fixed;
  left: 0;
  top: 0;
  margin: 0;
  z-index: 1400;
  pointer-events: none;
  opacity: 0.92;
  transform: scale(1.04);
  box-shadow: 0 18px 34px rgba(31, 42, 46, 0.28);
}

.thumb-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.thumb-order {
  position: absolute;
  top: 6px;
  left: 6px;
  min-width: 34px;
  height: 24px;
  padding: 0 8px;
  border-radius: 8px;
  background: rgba(192, 57, 43, 0.92);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: "JetBrains Mono", monospace;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.18);
  z-index: 2;
}

.thumb-remove {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 26px;
  height: 26px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.62);
  color: #fff;
  cursor: pointer;
}

.empty-tip {
  color: var(--text-secondary);
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  grid-column: 1 / -1;
}

.copy-row,
.bgm-row,
.cover-row,
.param-group {
  margin-top: 18px;
}

.param-sections {
  display: grid;
  gap: 12px;
}

.param-fold {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fbf7ef;
}

.param-fold > summary {
  list-style: none;
  cursor: pointer;
  padding: 12px 14px;
  font-size: 14px;
  font-weight: 700;
  position: relative;
}

.param-fold > summary::-webkit-details-marker {
  display: none;
}

.param-fold > summary::after {
  content: "▾";
  position: absolute;
  right: 12px;
  top: 10px;
  font-size: 14px;
  transform: rotate(-90deg);
  transition: transform 0.2s ease;
}

.param-fold[open] > summary::after {
  transform: rotate(0deg);
}

.param-fold > :not(summary) {
  padding: 2px 14px 14px;
}

.param-fold .param-group:first-of-type {
  margin-top: 0;
}

textarea {
  width: 100%;
  min-height: 132px;
  resize: vertical;
  border: 1px solid var(--border);
  border-radius: var(--radius-control);
  padding: 10px 12px;
  font-family: inherit;
  font-size: 14px;
  color: var(--text-primary);
  background: #fff;
}

textarea:focus,
input:focus {
  outline: 2px solid rgba(176, 122, 63, 0.2);
  border-color: var(--accent);
}

.copy-actions {
  margin-top: 8px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.file-name {
  color: var(--text-secondary);
  font-size: 13px;
}

.hint {
  margin: 6px 0 0;
  font-size: 12px;
  color: var(--text-secondary);
}

.radio-inline,
.radio-grid {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.radio-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.radio-grid.two-col {
  grid-template-columns: 1fr 1fr;
}

.radio-inline label,
.radio-grid label {
  background: #f9f4eb;
  border: 1px solid var(--border);
  border-radius: 10px;
  min-height: 40px;
  padding: 7px 10px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.radio-inline label:has(input:checked),
.radio-grid label:has(input:checked) {
  border-color: var(--accent);
  background: #fff4e5;
}

.total-duration {
  margin: 8px 0 0;
  color: var(--text-secondary);
}

.param-grid-inputs {
  margin-top: 14px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.param-grid-inputs label {
  font-size: 13px;
  color: var(--text-secondary);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.param-grid-inputs input {
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: var(--radius-control);
  padding: 0 10px;
  font-size: 14px;
}

.form-actions {
  margin-top: 18px;
  display: flex;
  gap: 10px;
}

.error-tip {
  min-height: 20px;
  margin: 8px 0 0;
  color: var(--error);
  font-size: 13px;
}

.output-zone {
  margin-top: 16px;
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

.card-head-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.timers {
  display: flex;
  gap: 14px;
  font-size: 13px;
  color: var(--text-secondary);
}

.timers strong {
  font-family: "JetBrains Mono", monospace;
  color: var(--text-primary);
}

.log-box {
  background: #11181c;
  color: #dbe6eb;
  border-radius: 10px;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  height: 112px;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 10px;
  font-family: "JetBrains Mono", monospace;
  font-size: 12px;
  line-height: 1.5;
}

.stage-progress {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fbf7ef;
  padding: 12px 12px 10px;
  margin-bottom: 12px;
}

.stage-track {
  position: relative;
  height: 6px;
  margin: 0 10%;
}

.stage-track-bg,
.stage-track-fill {
  position: absolute;
  left: 0;
  top: 0;
  height: 6px;
  border-radius: 999px;
}

.stage-track-bg {
  width: 100%;
  background: #e7dccd;
}

.stage-track-fill {
  width: 0%;
  background: linear-gradient(90deg, #b07a3f 0%, #d6a56a 100%);
  transition: width 0.2s ease;
}

.stage-nodes {
  margin-top: 10px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.stage-node {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 4px;
  position: relative;
}

.stage-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid #d7c6b0;
  background: #fffdf8;
  transition: all 0.2s ease;
}

.stage-copy {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.stage-topline {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stage-title {
  font-size: 12px;
  color: var(--text-secondary);
}

.stage-status {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 22px;
  padding: 0 8px;
  border-radius: 999px;
  border: 1px solid #ddd2c1;
  background: #fff8ee;
  color: var(--text-secondary);
  font-size: 11px;
  font-weight: 600;
  line-height: 1;
}

.stage-time {
  font-size: 12px;
  color: var(--text-primary);
  font-family: "JetBrains Mono", monospace;
}

.stage-node[data-state="done"] .stage-dot {
  border-color: #2e7d32;
  background: #2e7d32;
}

.stage-node[data-state="active"] .stage-dot {
  border-color: #b07a3f;
  background: #b07a3f;
  box-shadow: 0 0 0 4px rgba(176, 122, 63, 0.16);
}

.stage-node[data-state="failed"] .stage-dot {
  border-color: #c0392b;
  background: #c0392b;
}

.stage-node[data-state="active"] .stage-title {
  color: #8a5e2f;
  font-weight: 700;
}

.stage-node[data-state="done"] .stage-status {
  border-color: rgba(46, 125, 50, 0.22);
  background: rgba(46, 125, 50, 0.1);
  color: var(--success);
}

.stage-node[data-state="active"] .stage-status {
  border-color: rgba(176, 122, 63, 0.26);
  background: rgba(176, 122, 63, 0.12);
  color: #8a5e2f;
}

.stage-node[data-state="failed"] .stage-status {
  border-color: rgba(192, 57, 43, 0.25);
  background: rgba(192, 57, 43, 0.1);
  color: var(--error);
}

.log-line {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  max-width: 100%;
  animation: log-in 0.12s ease;
}

.log-line.error {
  color: #ff6a6a;
}

.video-frame {
  width: 100%;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
  aspect-ratio: 16 / 9;
  margin: 0 auto;
}

.video-frame.portrait {
  aspect-ratio: 9 / 16;
  max-width: 450px;
}

#finalVideo {
  width: 100%;
  height: 100%;
  display: block;
  background: #000;
}

.disabled-link {
  pointer-events: none;
  opacity: 0.6;
}

.history-list {
  display: grid;
  gap: 10px;
}

.history-item {
  border: 1px solid var(--border);
  background: #faf6ef;
  border-radius: 10px;
  padding: 10px;
  display: grid;
  grid-template-columns: 120px 1fr auto;
  gap: 12px;
  align-items: center;
}

.history-thumb {
  width: 120px;
  height: 68px;
  object-fit: cover;
  border-radius: 8px;
  background: #ddd;
}

.history-meta {
  display: grid;
  gap: 4px;
  font-size: 13px;
  color: var(--text-secondary);
}

.history-meta strong {
  color: var(--text-primary);
}

.history-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}

.pagination {
  margin-top: 12px;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  align-items: center;
}

.pagination button {
  min-height: 36px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
  padding: 0 10px;
  cursor: pointer;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.success-modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(31, 42, 46, 0.35);
}

.success-modal-backdrop[hidden] {
  display: none;
}

.success-modal {
  width: min(420px, calc(100vw - 32px));
  border-radius: 14px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  background: var(--surface);
  padding: 18px;
}

.success-modal h3 {
  margin: 0;
  font-family: "Noto Serif SC", serif;
  font-size: 22px;
}

.success-modal p {
  margin: 10px 0 0;
  color: var(--text-secondary);
  font-size: 14px;
}

.success-modal-actions {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.reveal-up {
  opacity: 0;
  transform: translateY(10px);
  animation: reveal-up 0.24s ease forwards;
}

.reveal-up.delay-1 {
  animation-delay: 0.08s;
}

.reveal-up.delay-2 {
  animation-delay: 0.16s;
}

@keyframes reveal-up {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes thumb-in {
  from {
    opacity: 0;
    transform: scale(0.96);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes log-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 1279px) {
  .input-zone {
    grid-template-columns: 1fr 1fr;
  }

  .param-grid-inputs {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 767px) {
  .app-shell {
    padding: 14px;
  }

  h1 {
    font-size: 26px;
  }

  .app-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
    padding: 18px;
  }

  .input-zone {
    grid-template-columns: 1fr;
  }

  .upload-card,
  .params-card,
  .progress-card,
  .preview-card,
  .history-card {
    padding: 16px;
  }

  .thumb-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .radio-grid.two-col,
  .history-item {
    grid-template-columns: 1fr;
  }

  .radio-inline.compact,
  .radio-grid.two-col,
  .param-grid-inputs {
    grid-template-columns: 1fr;
  }

  .radio-inline {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .radio-grid.voice-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .radio-grid.transition-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .radio-inline label,
  .radio-grid label {
    width: 100%;
  }

  .history-thumb {
    width: 100%;
    height: auto;
    aspect-ratio: 16 / 9;
  }

  .card-head-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .timers {
    flex-direction: column;
    gap: 6px;
    width: 100%;
    padding: 10px 12px;
    border-radius: 12px;
    background: #faf6ef;
    border: 1px solid var(--border);
  }

  .stage-progress {
    padding: 12px;
  }

  .stage-track {
    display: none;
  }

  .stage-nodes {
    margin-top: 0;
    grid-template-columns: 1fr;
    gap: 0;
  }

  .stage-node {
    display: grid;
    grid-template-columns: 24px minmax(0, 1fr);
    align-items: start;
    text-align: left;
    column-gap: 12px;
    padding: 0 0 16px;
  }

  .stage-node:last-child {
    padding-bottom: 0;
  }

  .stage-node::before {
    content: "";
    position: absolute;
    left: 6px;
    top: 18px;
    bottom: -2px;
    width: 2px;
    background: #e1d3bf;
  }

  .stage-node:last-child::before {
    display: none;
  }

  .stage-dot {
    grid-column: 1;
    grid-row: 1;
    margin-top: 4px;
    z-index: 1;
  }

  .stage-copy {
    grid-column: 2;
    align-items: flex-start;
    gap: 6px;
    min-width: 0;
  }

  .stage-topline {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    width: 100%;
    flex-wrap: wrap;
  }

  .stage-title {
    font-size: 13px;
    line-height: 1.35;
  }

  .stage-status {
    min-height: 24px;
    font-size: 11px;
  }

  .stage-time {
    font-size: 12px;
  }

  .form-actions,
  .history-actions {
    width: 100%;
    flex-wrap: wrap;
  }

  .form-actions button,
  .history-actions button,
  .history-actions a {
    flex: 1 1 calc(50% - 6px);
    justify-content: center;
  }

  .copy-actions {
    gap: 8px;
  }

  .copy-actions .file-name {
    width: 100%;
  }
}

@media (min-width: 768px) {
  .param-fold {
    border: none;
    background: transparent;
  }

  .param-fold > summary {
    display: none;
  }

  .param-fold > :not(summary) {
    padding: 0;
  }
}

@media (max-width: 480px) {
  .app-shell {
    padding: 10px;
  }

  .upload-card,
  .params-card,
  .progress-card,
  .preview-card,
  .history-card,
  .app-header {
    padding: 14px;
  }

  h1 {
    font-size: 24px;
  }

  .subtitle {
    margin-top: 8px;
    font-size: 13px;
  }

  .status-chip {
    padding: 7px 11px;
    font-size: 13px;
  }

  .thumb-grid {
    gap: 6px;
    padding: 8px;
  }

  .thumb-remove {
    width: 24px;
    height: 24px;
  }

  .thumb-order {
    min-width: 30px;
    height: 22px;
    font-size: 11px;
    top: 5px;
    left: 5px;
  }

  .radio-inline,
  .radio-grid,
  .param-grid-inputs {
    grid-template-columns: 1fr;
  }

  .radio-grid.voice-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .radio-grid.transition-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .radio-inline label,
  .radio-grid label {
    min-height: 42px;
    font-size: 12px;
  }

  .timers {
    font-size: 12px;
  }

  .stage-node {
    column-gap: 10px;
    padding-bottom: 14px;
  }

  .stage-title {
    font-size: 12px;
  }

  .stage-time {
    font-size: 11px;
  }

  .log-box {
    height: 108px;
    font-size: 11px;
  }
}

/* Auth overlay */
.auth-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: rgba(31, 42, 46, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
}

.auth-dialog {
  background: var(--surface);
  border-radius: var(--radius-card);
  padding: 40px 36px;
  width: 340px;
  box-shadow: 0 20px 60px rgba(31, 42, 46, 0.2);
  text-align: center;
}

.auth-dialog h2 {
  font-family: "Noto Serif SC", serif;
  font-size: 1.3rem;
  margin: 0 0 20px;
}

.auth-dialog input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-control);
  font-size: 1rem;
  margin-bottom: 8px;
  outline: none;
}

.auth-dialog input:focus {
  border-color: var(--accent);
}

.auth-dialog .auth-error {
  color: var(--error);
  font-size: 0.85rem;
  min-height: 1.2em;
  margin: 0 0 12px;
}

.auth-dialog .primary-btn {
  width: 100%;
}
