@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;900&display=swap');

* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --navy: #0f2744;
  --navy-2: #1a3a5c;
  --navy-soft: #2d4f73;
  --heading: #0f2744;
  --orange: #e87722;
  --orange-soft: #fff4eb;
  --red: #b41e1e;
  --line: #e2e8f0;
  --line-strong: #cbd5e1;
  --muted: #64748b;
  --bg: #f1f5f9;
  --bg-grad-top: #f8fafc;
  --card: #ffffff;
  --text: #1e293b;
  --text-secondary: #475569;
  --input-bg: #fafbfc;
  --panel-bg: #f8fafc;
  --preview-bg: #ffffff;
  --template-label-bg: #0f2744;
  --template-label-text: #ffffff;
  --template-thumb-bg: #e2e8f0;
  --shadow-sm: 0 1px 3px rgba(15, 39, 68, 0.06);
  --shadow-md: 0 8px 28px rgba(15, 39, 68, 0.08);
  --shadow-lg: 0 16px 48px rgba(15, 39, 68, 0.14);
  --radius: 14px;
  --radius-lg: 20px;
  --border: var(--line);
  --brand: #0f2744;
  --brand-mid: #1a3a5c;
  --brand-soft: #2d4f73;
}

[data-theme="dark"] {
  --navy: #e2e8f0;
  --navy-2: #cbd5e1;
  --navy-soft: #94a3b8;
  --heading: #f1f5f9;
  --orange: #f59a4a;
  --orange-soft: rgba(245, 154, 74, 0.14);
  --red: #fca5a5;
  --line: #334155;
  --line-strong: #475569;
  --muted: #94a3b8;
  --bg: #0b1220;
  --bg-grad-top: #111827;
  --card: #1a2332;
  --text: #e2e8f0;
  --text-secondary: #cbd5e1;
  --input-bg: #111827;
  --panel-bg: #151f2e;
  --preview-bg: #0f172a;
  --template-label-bg: #243044;
  --template-label-text: #f1f5f9;
  --template-thumb-bg: #0f172a;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.45);
  --shadow-md: 0 8px 28px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 16px 48px rgba(0, 0, 0, 0.55);
  --border: var(--line);
  --brand: #1e3a5f;
  --brand-mid: #243044;
  --brand-soft: #334155;
}

body {
  font-family: 'Cairo', Tahoma, sans-serif;
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(232, 119, 34, 0.07), transparent),
    linear-gradient(180deg, var(--bg-grad-top) 0%, var(--bg) 100%);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
  transition: background 0.25s, color 0.25s;
}

/* ---------- Header ---------- */
.topbar {
  background: linear-gradient(135deg, var(--brand) 0%, #152f4d 55%, var(--brand-mid) 100%);
  color: #fff;
  padding: 0 20px;
  border-bottom: 3px solid var(--orange);
  box-shadow: var(--shadow-md);
}

.topbar-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 18px 8px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 20px;
}

.theme-toggle {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  font-size: 1.1rem;
  cursor: pointer;
  transition: background 0.2s, transform 0.15s;
}

.theme-toggle:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: scale(1.05);
}

[data-theme="dark"] .theme-toggle {
  background: rgba(0, 0, 0, 0.25);
}

.topbar-actions {
  margin-right: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

.topbar-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 8px 14px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 700;
  text-decoration: none;
  transition: background 0.2s, border-color 0.2s;
}

.topbar-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: var(--orange);
  color: #fff;
}

.history-badge-top {
  font-size: 0.75rem;
  opacity: 0.85;
}

.tip {
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.55;
  margin: 4px 0 10px;
}

.ctrl-tip {
  display: block;
  font-size: 0.67rem;
  font-weight: 400;
  color: var(--muted);
  line-height: 1.5;
  margin: 2px 0 0;
}

.tip-intro {
  margin-bottom: 8px;
}

.tip code {
  background: var(--panel-bg);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.7rem;
  color: var(--orange);
  border: 1px solid var(--line);
  direction: ltr;
  display: inline-block;
}

.preview-tip {
  margin: -2px 0 12px;
}

.template-order-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.btn-order-edit,
.btn-order-save,
.btn-order-cancel {
  padding: 6px 14px;
  border-radius: 8px;
  border: 1px solid var(--line-strong);
  background: var(--panel-bg);
  color: var(--heading);
  font-family: inherit;
  font-size: 0.76rem;
  font-weight: 700;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}

.btn-order-edit:hover,
.btn-order-cancel:hover {
  border-color: var(--orange);
  color: var(--orange);
}

.btn-order-save {
  background: var(--orange);
  border-color: var(--orange);
  color: #fff;
}

.btn-order-save:hover {
  filter: brightness(1.08);
}

.topbar .brand {
  font-weight: 900;
  font-size: 1.55rem;
  letter-spacing: 0.3px;
  line-height: 1.2;
}

.brand-accent {
  color: var(--orange);
}

.topbar .brand-sub {
  color: #c5d3e3;
  font-size: 0.95rem;
  font-weight: 600;
}

.topbar-credit {
  margin-right: 8px;
  font-size: 0.78rem;
  font-weight: 400;
  color: #8fa8c4;
}

.topbar-credit a {
  color: #edf2f7;
  font-weight: 600;
  text-decoration: none;
  transition: color 0.2s;
}

.topbar-credit a:hover {
  color: var(--orange);
}

@media (max-width: 640px) {
  .topbar-credit { display: block; margin-right: 0; margin-top: 4px; }
}

/* ---------- Layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  gap: 20px;
  max-width: 1280px;
  margin: 0 auto;
  padding: 20px 16px 40px;
  align-items: start;
}

@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; padding: 20px 16px 40px; }
  .preview-col { order: -1; }
}

/* ---------- Preview column ---------- */
.preview-col {
  position: sticky;
  top: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 0 4px;
}

.preview-title {
  font-size: 1.1rem;
  font-weight: 800;
  color: var(--heading);
}

.preview-badge {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--orange);
  background: var(--orange-soft);
  border: 1px solid rgba(232, 119, 34, 0.25);
  padding: 4px 10px;
  border-radius: 999px;
  letter-spacing: 0.3px;
}

.phone {
  background: var(--preview-bg);
  border-radius: var(--radius-lg);
  padding: 12px;
  border: 1px solid var(--line);
  box-shadow: var(--shadow-lg);
  transition: background 0.25s, border-color 0.25s;
}

.result-box {
  position: relative;
  aspect-ratio: 9 / 16;
  border-radius: 12px;
  overflow: hidden;
  background: var(--preview-bg);
  display: flex;
  align-items: center;
  justify-content: center;
}

.result-box img,
.result-box video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.result-box canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

.result-box .placeholder {
  text-align: center;
  color: var(--muted);
  padding: 28px 20px;
}

.result-box .placeholder p {
  font-size: 1.05rem;
  font-weight: 700;
  margin-bottom: 8px;
  color: var(--text-secondary);
}

.result-box .placeholder span {
  font-size: 0.84rem;
  line-height: 1.6;
}

.preview-spinner {
  position: absolute;
  inset: 0;
  background: rgba(11, 15, 20, 0.5);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---------- Controls column ---------- */
.controls-col {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
}

.card {
  background: var(--card);
  border-radius: 12px;
  padding: 22px;
  border: 1px solid var(--line);
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.2s, background 0.25s, border-color 0.25s;
}

.card-compact {
  padding: 14px 16px;
}

.card-compact:hover {
  box-shadow: var(--shadow-md);
}

.card-fold {
  padding-top: 0;
}

.card-fold > summary {
  list-style: none;
  cursor: pointer;
}

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

.fold-title {
  margin-bottom: 0;
  padding: 14px 0 12px;
  border-bottom: none;
}

.card-fold[open] .fold-title {
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
}

.fold-title::after {
  content: '◂';
  margin-right: auto;
  color: var(--orange);
  font-size: 0.7rem;
  transition: transform 0.2s;
}

.card-fold[open] .fold-title::after {
  transform: rotate(-90deg);
}

.btn-row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.btn-row-2 .btn-save-pos { margin-bottom: 0; }

.card:hover {
  box-shadow: var(--shadow-md);
}

.card-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.95rem;
  font-weight: 800;
  color: var(--heading);
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--line);
}

.card-compact .card-title {
  margin-bottom: 10px;
  padding-bottom: 8px;
}

.card-title::before {
  content: '';
  width: 3px;
  height: 1em;
  background: linear-gradient(180deg, var(--orange), #d4650f);
  border-radius: 3px;
  flex-shrink: 0;
}

.field-pair { margin-bottom: 8px; }

.field-label {
  display: block;
  font-weight: 700;
  color: var(--heading);
  margin: 12px 0 6px;
  font-size: 0.85rem;
}

.field-label.tight {
  margin: 0 0 4px;
  font-size: 0.82rem;
}

.card-compact .field-label:first-of-type { margin-top: 0; }

textarea,
#twitter_handle,
.template-rename input,
.style-grid select {
  width: 100%;
  padding: 9px 11px;
  border: 1.5px solid var(--line-strong);
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.88rem;
  background: var(--input-bg);
  color: var(--text);
  caret-color: var(--orange);
  transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
}

textarea::placeholder,
#twitter_handle::placeholder,
.template-rename input::placeholder {
  color: var(--muted);
  opacity: 1;
}

[data-theme="dark"] textarea,
[data-theme="dark"] #twitter_handle,
[data-theme="dark"] .template-rename input,
[data-theme="dark"] .style-grid select {
  background: #111827;
  color: #f1f5f9;
  border-color: #475569;
}

[data-theme="dark"] .style-grid select option {
  background: #1e293b;
  color: #f1f5f9;
}

textarea {
  resize: vertical;
  line-height: 1.45;
  min-height: 2.6em;
}

textarea:focus,
#twitter_handle:focus,
.style-grid select:focus {
  outline: none;
  border-color: var(--orange);
  background: var(--preview-bg);
  box-shadow: 0 0 0 3px rgba(232, 119, 34, 0.12);
}

.twitter-field {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed var(--line);
}

.twitter-toggle { margin-bottom: 6px; }

.hint {
  margin-top: 6px;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.5;
}

.hint.inline-hint {
  margin: 4px 0 8px;
}

.hint-warn {
  color: var(--orange);
  font-weight: 700;
}

.hint code {
  background: var(--panel-bg);
  padding: 2px 8px;
  border-radius: 6px;
  color: var(--red);
  font-family: Consolas, monospace;
  direction: ltr;
  display: inline-block;
  border: 1px solid var(--line);
}

/* ---------- Template grid ---------- */
.template-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.template-item {
  width: 68px;
  border: 2px solid transparent;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  background: var(--panel-bg);
  transition: transform 0.15s, border-color 0.2s, box-shadow 0.2s;
  box-shadow: var(--shadow-sm);
}

.template-item:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow-md);
}

.template-item.selected {
  border-color: var(--orange);
  box-shadow: 0 0 0 3px rgba(232, 119, 34, 0.2), var(--shadow-md);
}

.template-grid.sortable-mode .template-item {
  cursor: grab;
}

.template-grid.sortable-mode .template-item:hover {
  transform: none;
}

.template-grid.sortable-mode .template-item.dragging {
  opacity: 0.45;
  cursor: grabbing;
  border-color: var(--orange);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}

.template-thumb {
  aspect-ratio: 9 / 16;
  overflow: hidden;
  background: var(--template-thumb-bg);
}

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

.template-item span {
  display: block;
  font-size: 0.55rem;
  font-weight: 600;
  padding: 3px 2px;
  text-align: center;
  background: var(--template-label-bg);
  color: var(--template-label-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.template-rename {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  margin-top: 10px;
}

.template-rename input {
  width: 100%;
  padding: 8px 10px;
  border: 1.5px solid var(--line-strong);
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.82rem;
  background: var(--input-bg);
  color: var(--text);
}

.template-rename input:focus {
  outline: none;
  border-color: var(--orange);
  box-shadow: 0 0 0 3px rgba(232, 119, 34, 0.12);
}

.btn-rename-template {
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--line-strong);
  background: var(--panel-bg);
  color: var(--heading);
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}

.btn-rename-template:hover {
  border-color: var(--orange);
  color: var(--orange);
}

/* ---------- Style panels ---------- */
.style-panel {
  margin: 6px 0 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px 10px;
  background: var(--panel-bg);
}

.style-panel summary {
  cursor: pointer;
  font-weight: 700;
  color: var(--heading);
  padding: 8px 2px;
  list-style: none;
  font-size: 0.8rem;
  user-select: none;
  display: flex;
  align-items: center;
  gap: 6px;
}

.style-panel summary::-webkit-details-marker { display: none; }

.style-panel summary::before {
  content: '◂';
  color: var(--orange);
  font-size: 0.75rem;
  transition: transform 0.2s;
}

.style-panel[open] summary::before {
  transform: rotate(-90deg);
}

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

.style-grid label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.style-ctrl-head {
  display: block;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.check-row input {
  width: 15px;
  height: 15px;
  accent-color: var(--orange);
  cursor: pointer;
  flex-shrink: 0;
}

.style-grid .style-ctrl.span-2,
.style-grid .span-2 {
  grid-column: 1 / -1;
}

.style-grid .span-2 {
  grid-column: 1 / -1;
}

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

.style-grid input[type="color"] {
  width: 100%;
  height: 30px;
  border: 2px solid var(--line-strong);
  border-radius: 6px;
  padding: 2px;
  cursor: pointer;
  background: var(--input-bg);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.12);
}

[data-theme="dark"] .style-grid input[type="color"] {
  border-color: #64748b;
  background-color: #334155;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.2);
}

.offset-grid.compact-offsets {
  gap: 10px;
  margin: 8px 0 10px;
}

.offset-item {
  padding: 8px 10px;
  background: var(--panel-bg);
  border-radius: 8px;
  border: 1px solid var(--line);
}

.offset-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 8px;
  align-items: center;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text);
}

.offset-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  grid-column: 1 / -1;
}

.offset-head b {
  font-size: 0.75rem;
  color: var(--orange);
  min-width: 2.5em;
  text-align: left;
}

.offset-row input[type="range"] {
  grid-column: 1 / -1;
  width: 100%;
  accent-color: var(--orange);
  margin: 4px 0 0;
}

.offset-item .ctrl-tip {
  margin-top: 6px;
}

.btn-reset,
.btn-save-pos,
.btn-delete-template {
  width: 100%;
  padding: 9px 12px;
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-reset {
  border: 1.5px solid var(--line-strong);
  background: var(--input-bg);
  color: var(--text);
}

.btn-reset:hover {
  border-color: var(--orange);
  color: var(--orange);
  background: var(--orange-soft);
}

.btn-save-pos {
  margin-bottom: 8px;
  border: none;
  background: linear-gradient(135deg, var(--brand) 0%, var(--brand-soft) 100%);
  color: #fff;
  font-weight: 700;
  box-shadow: 0 3px 10px rgba(15, 39, 68, 0.22);
}

[data-theme="dark"] .btn-save-pos {
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.45);
}

[data-theme="dark"] .btn-delete-template {
  background: rgba(127, 29, 29, 0.18);
  border-color: rgba(248, 113, 113, 0.35);
  color: #fca5a5;
}

[data-theme="dark"] .btn-delete-template:hover {
  background: rgba(127, 29, 29, 0.32);
  border-color: #f87171;
}

.btn-save-pos:hover {
  opacity: 0.95;
}

.btn-delete-template {
  border: 1.5px solid #fecaca;
  background: #fef2f2;
  color: #b91c1c;
}

.btn-delete-template:hover {
  background: #fee2e2;
  border-color: #b91c1c;
}

.template-upload { margin-top: 10px; }

.twitter-input-wrap.hidden { display: none; }

.check-label {
  flex-direction: row !important;
  align-items: center;
  gap: 6px !important;
  padding-top: 0;
}

.check-label.compact-check {
  font-size: 0.78rem;
  padding-top: 4px;
}

.check-label input {
  width: 15px;
  height: 15px;
  accent-color: var(--orange);
  cursor: pointer;
  flex-shrink: 0;
}

.hl-grid {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px dashed var(--line-strong);
}

/* ---------- Upload ---------- */
.upload-wrap {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.upload-box.has-media {
  border-color: var(--orange);
}

.btn-remove-media {
  align-self: stretch;
  padding: 8px 12px;
  font-size: 0.82rem;
  font-weight: 600;
  color: #b42318;
  background: #fef3f2;
  border: 1px solid #fecdca;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s;
}

.btn-remove-media:hover:not(:disabled) {
  background: #fee4e2;
  border-color: #f97066;
}

.btn-remove-media:disabled {
  opacity: 0.6;
  cursor: wait;
}

[data-theme="dark"] .btn-remove-media {
  color: #fda29b;
  background: rgba(180, 35, 24, 0.15);
  border-color: rgba(249, 112, 102, 0.35);
}

[data-theme="dark"] .btn-remove-media:hover:not(:disabled) {
  background: rgba(180, 35, 24, 0.28);
}

.upload-box {
  position: relative;
  border: 2px dashed var(--line-strong);
  border-radius: 10px;
  padding: 28px 16px;
  text-align: center;
  cursor: pointer;
  background: var(--input-bg);
  transition: border-color 0.2s, background 0.2s;
}

.upload-compact {
  padding: 14px 12px;
}

.upload-compact #uploadHint {
  font-size: 0.84rem;
}

.upload-compact .upload-note {
  font-size: 0.72rem;
  margin-top: 4px;
}

.upload-box:hover,
.upload-box.dragover {
  border-color: var(--orange);
  background: var(--orange-soft);
}

.upload-box input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

#uploadHint {
  font-weight: 600;
  color: var(--heading);
  font-size: 0.92rem;
}

.upload-note {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 8px;
}

/* ---------- Ratio ---------- */
.ratio-controls { margin-top: 10px; }

.compact-ratio { margin-top: 10px; }

.compact-ratio .seg {
  display: flex;
  gap: 6px;
  margin-bottom: 6px;
}

.zoom-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
}

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

.seg-item {
  flex: 1;
  border: 1.5px solid var(--line-strong);
  border-radius: 8px;
  padding: 7px 6px;
  text-align: center;
  cursor: pointer;
  font-size: 0.76rem;
  font-weight: 700;
  color: var(--text-secondary);
  background: var(--input-bg);
  transition: all 0.15s;
}

.seg-item input { display: none; }

.seg-item:has(input:checked) {
  border-color: var(--orange);
  background: var(--orange-soft);
  color: var(--orange);
  box-shadow: 0 0 0 2px rgba(232, 119, 34, 0.15);
}

[data-theme="dark"] .seg-item:has(input:checked) {
  color: #fbbf24;
}

#zoom { width: 100%; accent-color: var(--orange); }

#media_pan { width: 100%; accent-color: var(--orange); }

/* ---------- Buttons ---------- */
.btn-primary {
  width: 100%;
  padding: 15px 24px;
  background: linear-gradient(135deg, var(--orange) 0%, #d4650f 100%);
  color: #fff;
  border: none;
  border-radius: 12px;
  font-family: inherit;
  font-size: 1.02rem;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(232, 119, 34, 0.35);
  transition: transform 0.15s, box-shadow 0.2s, opacity 0.2s;
}

.btn-primary:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(232, 119, 34, 0.4);
}

.btn-primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  box-shadow: none;
}

.btn-download {
  display: block;
  text-align: center;
  padding: 13px;
  background: linear-gradient(135deg, #0f2744, #1a3a5c);
  color: #fff;
  text-decoration: none;
  border-radius: 12px;
  font-weight: 700;
  font-size: 0.95rem;
  transition: opacity 0.2s, transform 0.15s;
  box-shadow: 0 4px 14px rgba(15, 39, 68, 0.2);
}

[data-theme="dark"] .btn-download {
  background: linear-gradient(135deg, #334155, #475569);
  color: #f8fafc;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
}

.btn-download:hover {
  opacity: 0.95;
  transform: translateY(-1px);
}

.status-msg {
  text-align: center;
  font-size: 0.88rem;
  font-weight: 600;
  min-height: 1.4em;
  padding: 4px 8px;
  border-radius: 8px;
}

.status-msg.error {
  color: #b91c1c;
  background: #fef2f2;
}

[data-theme="dark"] .status-msg.error {
  color: #fca5a5;
  background: #3f1d1d;
}

.status-msg.success {
  color: #15803d;
  background: #f0fdf4;
}

[data-theme="dark"] .status-msg.success {
  color: #86efac;
  background: #14332a;
}

.hidden { display: none !important; }

/* ---------- History ---------- */
.history-card {
  margin-top: 14px;
  width: 100%;
}

.history-count {
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 600;
}

.history-empty.hidden {
  display: none;
}

.history-filters {
  display: flex;
  gap: 6px;
  margin: 0 0 10px;
}

.history-filter {
  flex: 1;
  padding: 7px 8px;
  border: 1.5px solid var(--line-strong);
  border-radius: 8px;
  background: var(--input-bg);
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 0.76rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s;
}

.history-filter:hover {
  border-color: var(--orange);
}

.history-filter.active {
  border-color: var(--orange);
  background: var(--orange-soft);
  color: var(--orange);
}

[data-theme="dark"] .history-filter.active {
  color: #fbbf24;
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 280px;
  overflow-y: auto;
}

.history-item {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--input-bg);
}

.history-item-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 4px;
}

.history-kind {
  font-weight: 700;
  color: var(--orange);
}

.history-title {
  margin: 0;
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--heading);
  line-height: 1.35;
}

.history-template {
  margin: 2px 0 8px;
  font-size: 0.72rem;
  color: var(--muted);
}

.history-actions {
  display: flex;
  gap: 8px;
}

.history-dl,
.history-del {
  flex: 1;
  text-align: center;
  padding: 6px 8px;
  border-radius: 8px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--heading);
  text-decoration: none;
}

.history-dl:hover {
  border-color: var(--orange);
  color: var(--orange);
}

.history-del {
  color: #b42318;
  border-color: #fecdca;
  background: #fef3f2;
}

[data-theme="dark"] .history-del {
  color: #fda29b;
  background: rgba(180, 35, 24, 0.15);
  border-color: rgba(249, 112, 102, 0.35);
}

.history-view {
  flex: 1;
  text-align: center;
  padding: 6px 8px;
  border-radius: 8px;
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  border: 1px solid var(--orange);
  background: var(--orange-soft);
  color: var(--orange);
  font-family: inherit;
}

[data-theme="dark"] .history-view {
  color: #fbbf24;
}

.history-actions-3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
}

.history-actions-3 .history-dl,
.history-actions-3 .history-del,
.history-actions-3 .history-view {
  flex: unset;
  font-family: inherit;
}

.btn-history-link {
  display: block;
  text-align: center;
  padding: 12px 16px;
  margin-top: 4px;
  border-radius: 12px;
  border: 1.5px solid var(--line-strong);
  background: var(--card);
  color: var(--heading);
  font-weight: 700;
  font-size: 0.92rem;
  text-decoration: none;
  transition: border-color 0.2s, color 0.2s, background 0.2s;
}

.btn-history-link:hover {
  border-color: var(--orange);
  color: var(--orange);
  background: var(--orange-soft);
}

.history-badge {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 600;
}

/* ---------- History page ---------- */
.history-back {
  color: #edf2f7;
  text-decoration: none;
  font-weight: 700;
  font-size: 0.88rem;
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: background 0.2s;
}

.history-back:hover {
  background: rgba(255, 255, 255, 0.12);
}

.history-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  gap: 20px;
  max-width: 1280px;
  margin: 0 auto;
  padding: 20px 16px 40px;
  align-items: start;
}

@media (max-width: 960px) {
  .history-layout {
    grid-template-columns: 1fr;
  }
  .history-preview-col { order: -1; }
}

.history-preview-col {
  position: sticky;
  top: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.history-sidebar {
  max-height: calc(100vh - 120px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.history-sidebar .card-title {
  flex-shrink: 0;
}

.history-list-page {
  max-height: none;
  flex: 1;
  overflow-y: auto;
  padding-left: 2px;
}

.history-list-page .history-item {
  display: grid;
  grid-template-columns: 52px 1fr;
  gap: 10px;
  align-items: start;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.history-list-page .history-item:hover {
  border-color: var(--orange);
}

.history-list-page .history-item.selected {
  border-color: var(--orange);
  box-shadow: 0 0 0 2px rgba(232, 119, 34, 0.2);
}

.history-thumb {
  width: 52px;
  height: 52px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--template-thumb-bg);
  border: 1px solid var(--line);
}

.history-thumb-video {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  color: var(--orange);
  background: var(--panel-bg);
}

.history-item-body {
  min-width: 0;
}

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

.spinner {
  display: inline-block;
  width: 34px;
  height: 34px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

.btn-primary .spinner {
  width: 18px;
  height: 18px;
  border-width: 2px;
  vertical-align: middle;
  margin-right: 8px;
}

/* ---------- Footer ---------- */
.site-footer {
  text-align: center;
  padding: 22px 16px 30px;
  color: var(--muted);
  font-size: 0.86rem;
  border-top: 1px solid var(--line);
  background: linear-gradient(180deg, var(--card) 0%, var(--bg) 100%);
  transition: background 0.25s;
}

.site-footer p {
  margin: 0;
  line-height: 1.7;
}

.site-footer a {
  color: var(--text-secondary);
  font-weight: 700;
  text-decoration: none;
  transition: color 0.2s;
}

.site-footer a:hover {
  color: var(--orange);
}

/* ---------- Auth pages ---------- */
.auth-body {
  min-height: 100vh;
  background: linear-gradient(160deg, var(--bg-grad-top) 0%, var(--bg) 55%, var(--orange-soft) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}

.auth-shell {
  width: 100%;
  max-width: 440px;
  padding: 24px 16px;
}

.auth-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 32px 28px 26px;
}

.auth-brand {
  font-size: 1.35rem;
  font-weight: 900;
  color: var(--brand);
  text-align: center;
  margin-bottom: 8px;
}

.auth-title {
  font-size: 1.45rem;
  font-weight: 800;
  color: var(--heading);
  text-align: center;
  margin-bottom: 6px;
}

.auth-sub {
  text-align: center;
  color: var(--muted);
  font-size: 0.92rem;
  margin-bottom: 22px;
  line-height: 1.6;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.auth-field input {
  padding: 11px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--input-bg);
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
}

.auth-field input:focus {
  outline: none;
  border-color: var(--orange);
  box-shadow: 0 0 0 3px var(--orange-soft);
}

.auth-hint {
  font-size: 0.82rem;
  color: var(--muted);
  margin-top: -4px;
}

.auth-error {
  color: var(--red);
  background: rgba(180, 30, 30, 0.08);
  border: 1px solid rgba(180, 30, 30, 0.2);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.9rem;
  line-height: 1.5;
}

.auth-submit {
  margin-top: 4px;
  padding: 13px 16px;
  border: none;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--orange) 0%, #d96a18 100%);
  color: #fff;
  font-family: inherit;
  font-size: 1.02rem;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.15s, opacity 0.15s;
}

.auth-submit:hover:not(:disabled) {
  transform: translateY(-1px);
}

.auth-submit:disabled {
  opacity: 0.65;
  cursor: wait;
}

.auth-foot {
  text-align: center;
  margin-top: 20px;
  font-size: 0.9rem;
  color: var(--muted);
}

.auth-foot a {
  color: var(--orange);
  font-weight: 700;
  text-decoration: none;
}

.auth-foot a:hover {
  text-decoration: underline;
}

.topbar-user {
  font-size: 0.88rem;
  color: var(--muted);
  margin-left: 8px;
}

.topbar-logout {
  border: 1px solid var(--line-strong);
  background: transparent;
  color: var(--text-secondary);
  border-radius: 8px;
  padding: 6px 12px;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
}

.topbar-logout:hover {
  border-color: var(--orange);
  color: var(--orange);
}
