/* ============================================
   Pixia - Merge PDF studio (merge.html)
   Complements css/ai-tool.css (.at-* primitives)
   Scope: body.at-merge
   ============================================ */

body.at-merge {
  --mg-accent: #ff9800;
  --mg-accent-2: #ffc947;
  --mg-line: var(--glass-border);
  --mg-soft: var(--bg-secondary);
}

/* ---------- Layout ---------- */
.mg-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 356px;
  gap: 22px;
  align-items: start;
}

.mg-col {
  min-width: 0;
  display: grid;
  gap: 16px;
}

.mg-side {
  min-width: 0;
  display: grid;
  gap: 16px;
  /* Settings column follows the page scroll (sticky) while the main column scrolls. */
  position: sticky;
  inset-block-start: 84px;
  align-self: start;
}

@media (max-width: 1020px) {
  .mg-layout {
    grid-template-columns: 1fr;
  }
  .mg-col,
  .mg-side {
    position: static;
  }
}

.mg-panel {
  min-height: 0;
}

body.at-merge .mg-side .at-panel + .at-panel {
  margin-block-start: 0;
}

.mg-panel h2 {
  display: flex;
  align-items: center;
  gap: 9px;
}

.mg-panel h2 .mg-count {
  margin-inline-start: auto;
  font-family: var(--font-primary);
  font-size: 12px;
  font-weight: 600;
  color: var(--text-tertiary);
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  border-radius: 999px;
  padding: 3px 10px;
}

.mg-hint {
  margin: 0 0 14px;
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--text-tertiary);
}

/* ---------- Drop zone ---------- */
.mg-drop {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
  padding: 42px 22px;
  border: 1.6px dashed var(--glass-border-hover);
  border-radius: var(--at-radius-lg);
  background: var(--glass-bg);
  cursor: pointer;
  transition: border-color .2s var(--at-ease), background .2s var(--at-ease), transform .2s var(--at-ease);
}

.mg-drop:hover {
  border-color: var(--mg-accent);
}

.mg-drop.drag {
  border-color: var(--mg-accent);
  background: color-mix(in srgb, var(--mg-accent) 10%, var(--glass-bg));
  transform: scale(1.006);
}

.mg-drop input {
  display: none;
}

.mg-drop-icon {
  width: 56px;
  height: 56px;
  display: grid;
  place-items: center;
  border-radius: 16px;
  background: color-mix(in srgb, var(--mg-accent) 16%, transparent);
  color: var(--mg-accent);
  margin-block-end: 4px;
}

.mg-drop-icon svg {
  width: 28px;
  height: 28px;
}

.mg-drop-title {
  font-family: var(--font-display);
  font-size: 1.06rem;
  font-weight: 600;
  color: var(--text-primary);
}

.mg-drop-sub {
  font-size: 13px;
  color: var(--text-tertiary);
  max-width: 46ch;
  line-height: 1.6;
}

.mg-drop-cta {
  margin-block-start: 12px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 11px 22px;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--mg-accent), var(--mg-accent-2));
  color: #241701;
  font-weight: 700;
  font-size: 14px;
  box-shadow: 0 14px 30px -18px rgba(255, 152, 0, .95);
}

.mg-drop-cta svg {
  width: 17px;
  height: 17px;
}

/* compact variant once files exist */
.mg-drop.is-compact {
  flex-direction: row;
  justify-content: center;
  padding: 16px 20px;
  gap: 12px;
}

.mg-drop.is-compact .mg-drop-icon {
  width: 38px;
  height: 38px;
  border-radius: 11px;
  margin: 0;
}

.mg-drop.is-compact .mg-drop-icon svg {
  width: 20px;
  height: 20px;
}

.mg-drop.is-compact .mg-drop-sub,
.mg-drop.is-compact .mg-drop-cta {
  display: none;
}

.mg-drop.is-compact .mg-drop-title {
  font-size: .96rem;
}

/* ---------- Queue toolbar ---------- */
.mg-queue-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-block-end: 14px;
}

.mg-queue-head h2 {
  margin: 0;
}

.mg-queue-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-inline-start: auto;
}

.mg-stat {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  border-radius: 999px;
  padding: 4px 11px;
}

.mg-queue-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-block-end: 14px;
  margin-top: 20px;
}

.mg-tool-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: 9px;
  border: 1px solid var(--glass-border);
  background: var(--glass-bg);
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: color .18s var(--at-ease), border-color .18s var(--at-ease), background .18s var(--at-ease);
}

.mg-tool-btn:hover:not(:disabled) {
  color: var(--text-primary);
  border-color: var(--glass-border-hover);
}

.mg-tool-btn svg {
  width: 15px;
  height: 15px;
}

.mg-tool-btn[disabled] {
  opacity: .5;
  cursor: not-allowed;
}

.mg-tool-btn.mg-danger:hover:not(:disabled) {
  color: #ef4444;
  border-color: color-mix(in srgb, #ef4444 45%, transparent);
}

/* ---------- Empty state ---------- */
.mg-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 40px 20px;
  text-align: center;
  border: 1px dashed var(--glass-border);
  border-radius: var(--at-radius-lg);
  color: var(--text-tertiary);
  font-size: 13.5px;
}

.mg-empty svg {
  width: 34px;
  height: 34px;
  color: var(--text-muted);
}

/* ---------- File list ---------- */
.mg-files {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.mg-file {
  position: relative;
  display: grid;
  grid-template-columns: auto auto 62px minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--glass-border);
  border-radius: var(--at-radius-md);
  background: var(--bg-card);
  transition: border-color .18s var(--at-ease), box-shadow .18s var(--at-ease), transform .18s var(--at-ease);
}

.mg-file:hover {
  border-color: var(--glass-border-hover);
  box-shadow: 0 14px 30px -24px rgba(0, 0, 0, .6);
}

.mg-file.is-selected {
  border-color: var(--mg-accent);
  box-shadow: 0 0 0 1px var(--mg-accent);
}

.mg-file.is-dragging {
  opacity: .45;
}

.mg-file.is-drop-before::before,
.mg-file.is-drop-after::after {
  content: "";
  position: absolute;
  inset-inline: 6px;
  height: 3px;
  border-radius: 3px;
  background: var(--mg-accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--mg-accent) 25%, transparent);
}

.mg-file.is-drop-before::before {
  inset-block-start: -7px;
}

.mg-file.is-drop-after::after {
  inset-block-end: -7px;
}

.mg-file.is-error {
  border-color: color-mix(in srgb, #ef4444 50%, transparent);
}

.mg-handle {
  display: grid;
  place-items: center;
  width: 26px;
  height: 40px;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  cursor: grab;
  touch-action: none;
  border-radius: 8px;
}

.mg-handle:hover {
  color: var(--text-secondary);
  background: var(--glass-bg);
}

.mg-handle:active {
  cursor: grabbing;
}

.mg-handle svg {
  width: 16px;
  height: 16px;
}

.mg-pos {
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 8px;
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  font-size: 12px;
  font-weight: 700;
  color: var(--text-secondary);
}

.mg-thumb {
  position: relative;
  width: 62px;
  height: 80px;
  border-radius: 8px;
  overflow: hidden;
  background:
    repeating-conic-gradient(rgba(128, 128, 128, .18) 0% 25%, transparent 0% 50%) 50% / 14px 14px,
    var(--bg-tertiary);
  border: 1px solid var(--glass-border);
  display: grid;
  place-items: center;
}

.mg-thumb img,
.mg-thumb canvas {
  max-width: 100%;
  max-height: 100%;
  display: block;
  object-fit: contain;
}

.mg-thumb .mg-thumb-spin {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid var(--glass-border-hover);
  border-top-color: var(--mg-accent);
  animation: at-spin .7s linear infinite;
}

.mg-thumb .mg-thumb-err {
  width: 20px;
  height: 20px;
  color: #ef4444;
}

.mg-info {
  min-width: 0;
  display: grid;
  gap: 3px;
}

.mg-name {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mg-sub {
  font-size: 12px;
  color: var(--text-tertiary);
  display: flex;
  align-items: center;
  gap: 6px;
}

.mg-sub .mg-dot {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--text-muted);
}

.mg-status {
  font-size: 12px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.mg-status.is-reading { color: var(--text-tertiary); }
.mg-status.is-ready { color: var(--accent-emerald, #10b981); }
.mg-status.is-error { color: #ef4444; }

.mg-actions {
  display: flex;
  align-items: center;
  gap: 2px;
}

.mg-icon-btn {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  border: 0;
  background: transparent;
  color: var(--text-tertiary);
  cursor: pointer;
  transition: color .16s var(--at-ease), background .16s var(--at-ease);
}

.mg-icon-btn:hover:not(:disabled) {
  color: var(--text-primary);
  background: var(--glass-bg);
}

.mg-icon-btn[disabled] {
  opacity: .4;
  cursor: not-allowed;
}

.mg-icon-btn.mg-danger:hover:not(:disabled) {
  color: #ef4444;
  background: color-mix(in srgb, #ef4444 12%, transparent);
}

.mg-icon-btn.is-on {
  color: var(--mg-accent);
  background: color-mix(in srgb, var(--mg-accent) 14%, transparent);
}

.mg-icon-btn svg {
  width: 16px;
  height: 16px;
}

@media (max-width: 560px) {
  .mg-file {
    grid-template-columns: auto 50px minmax(0, 1fr) auto;
    grid-template-areas:
      "pos thumb info actions"
      ". . pages pages";
    gap: 8px 10px;
  }
  .mg-handle { display: none; }
  .mg-move { display: none; }
  .mg-actions { gap: 0; }
  .mg-pos { grid-area: pos; align-self: start; }
  .mg-thumb { grid-area: thumb; width: 50px; height: 66px; }
  .mg-info { grid-area: info; }
  .mg-actions { grid-area: actions; }
}

/* ---------- Page organizer ---------- */
.mg-pages {
  grid-column: 1 / -1;
  margin-block-start: 4px;
  padding-block-start: 14px;
  border-block-start: 1px dashed var(--glass-border);
}

.mg-pages-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-block-end: 10px;
  font-size: 12px;
  color: var(--text-tertiary);
}

.mg-pages-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 10px;
}

.mg-page {
  position: relative;
  padding: 0;
  border: 1px solid var(--glass-border);
  border-radius: 9px;
  background: var(--bg-card);
  overflow: hidden;
  cursor: grab;
  transition: border-color .16s var(--at-ease), transform .16s var(--at-ease), box-shadow .16s var(--at-ease);
}

.mg-page:hover {
  border-color: var(--glass-border-hover);
  transform: translateY(-2px);
}

.mg-page.is-dragging {
  opacity: .4;
}

.mg-page.is-drop-before::before,
.mg-page.is-drop-after::after {
  content: "";
  position: absolute;
  inset-block: 0;
  width: 3px;
  border-radius: 3px;
  background: var(--mg-accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--mg-accent) 25%, transparent);
}

.mg-page.is-drop-before::before { inset-inline-start: -6px; }
.mg-page.is-drop-after::after { inset-inline-end: -6px; }

.mg-page-img {
  display: grid;
  place-items: center;
  aspect-ratio: 3 / 4;
  background:
    repeating-conic-gradient(rgba(128, 128, 128, .18) 0% 25%, transparent 0% 50%) 50% / 14px 14px,
    var(--bg-tertiary);
  overflow: hidden;
}

.mg-page-img img,
.mg-page-img canvas {
  max-width: 100%;
  max-height: 100%;
  display: block;
  object-fit: contain;
  transition: transform .2s var(--at-ease);
}

.mg-page-img .mg-thumb-spin {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 2px solid var(--glass-border-hover);
  border-top-color: var(--mg-accent);
  animation: at-spin .7s linear infinite;
}

.mg-page-num {
  position: absolute;
  inset-block-start: 5px;
  inset-inline-start: 5px;
  min-width: 20px;
  padding: 2px 6px;
  border-radius: 999px;
  background: color-mix(in srgb, #000 68%, transparent);
  color: #fff;
  font-size: 10.5px;
  font-weight: 700;
  text-align: center;
  pointer-events: none;
}

.mg-page-tools {
  position: absolute;
  inset-block-start: 4px;
  inset-inline-end: 4px;
  display: flex;
  gap: 3px;
  opacity: 0;
  transition: opacity .16s var(--at-ease);
}

.mg-page:hover .mg-page-tools,
.mg-page:focus-within .mg-page-tools {
  opacity: 1;
}

.mg-page-tools button {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 7px;
  border: 0;
  background: color-mix(in srgb, #000 62%, transparent);
  color: #fff;
  cursor: pointer;
}

.mg-page-tools button:hover {
  background: #ef4444;
}

.mg-page-tools button.mg-rot:hover {
  background: var(--mg-accent);
}

.mg-page-tools svg {
  width: 13px;
  height: 13px;
}

.mg-more {
  margin-block-start: 12px;
  text-align: center;
}

/* ---------- Form controls ---------- */
.mg-field {
  display: grid;
  gap: 7px;
  margin-block-end: 16px;
}

.mg-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
}

.mg-subhint {
  font-size: 11.5px;
  color: var(--text-muted);
  line-height: 1.5;
}

.mg-input,
.mg-select {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--glass-border);
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 13.5px;
  transition: border-color .16s var(--at-ease), box-shadow .16s var(--at-ease);
}

.mg-input:focus,
.mg-select:focus {
  outline: none;
  border-color: var(--mg-accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--mg-accent) 22%, transparent);
}

.mg-select {
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23999' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 15px;
  padding-inline-end: 34px;
}

[dir="rtl"] .mg-select {
  background-position: left 12px center;
}

/* switch rows */
.mg-switch {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 11px 0;
  cursor: pointer;
  border-block-end: 1px solid var(--glass-border);
}

.mg-switch:last-of-type {
  border-block-end: 0;
}

.mg-switch input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.mg-switch-track {
  position: relative;
  flex: 0 0 auto;
  width: 40px;
  height: 23px;
  border-radius: 999px;
  background: var(--bg-tertiary);
  border: 1px solid var(--glass-border);
  transition: background .2s var(--at-ease), border-color .2s var(--at-ease);
  margin-block-start: 1px;
}

.mg-switch-track::after {
  content: "";
  position: absolute;
  inset-block-start: 2px;
  inset-inline-start: 2px;
  width: 17px;
  height: 17px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  transition: transform .2s var(--at-ease);
}

.mg-switch input:checked + .mg-switch-track {
  background: var(--mg-accent);
  border-color: var(--mg-accent);
}

.mg-switch input:checked + .mg-switch-track::after {
  transform: translateX(17px);
}

[dir="rtl"] .mg-switch input:checked + .mg-switch-track::after {
  transform: translateX(-17px);
}

.mg-switch input:focus-visible + .mg-switch-track {
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--mg-accent) 30%, transparent);
}

.mg-switch-text {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.mg-switch-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  line-height: 1.35;
}

.mg-switch-desc {
  font-size: 11.5px;
  color: var(--text-muted);
  line-height: 1.5;
}

.mg-subcontrol {
  margin: 4px 0 14px;
  padding-inline-start: 52px;
  display: grid;
  gap: 12px;
}

[dir="rtl"] .mg-subcontrol {
  padding-inline-start: 0;
  padding-inline-end: 52px;
}

.mg-advanced {
  border-block-start: 1px solid var(--glass-border);
  margin-block-start: 6px;
  padding-block-start: 12px;
}

.mg-advanced summary {
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-secondary);
  list-style: none;
  display: flex;
  align-items: center;
  gap: 7px;
}

.mg-advanced summary::-webkit-details-marker { display: none; }

.mg-advanced summary::after {
  content: "";
  width: 7px;
  height: 7px;
  margin-inline-start: auto;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(45deg);
  transition: transform .2s var(--at-ease);
}

.mg-advanced[open] summary::after {
  transform: rotate(-135deg);
}

.mg-advanced-grid {
  display: grid;
  gap: 12px;
  margin-block-start: 14px;
}

/* ---------- Summary ---------- */
.mg-summary-rows {
  display: grid;
  gap: 2px;
}

.mg-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 9px 0;
  border-block-end: 1px solid var(--glass-border);
  font-size: 13.5px;
}

.mg-row:last-child {
  border-block-end: 0;
}

.mg-row-key {
  color: var(--text-secondary);
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.mg-row-key svg {
  width: 15px;
  height: 15px;
  color: var(--text-muted);
}

.mg-row-val {
  font-weight: 700;
  color: var(--text-primary);
}

.mg-empty-note {
  padding: 16px 0;
  text-align: center;
  font-size: 13px;
  color: var(--text-tertiary);
}

/* ---------- Run area ---------- */
.mg-run-btn {
  width: 100%;
  padding: 15px 22px;
  font-size: 15.5px;
}

.mg-run-alt {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
  margin-block-start: 9px;
}

.mg-result {
  display: grid;
  gap: 12px;
}

.mg-success {
  display: flex;
  align-items: flex-start;
  gap: 11px;
  padding: 13px 14px;
  border-radius: var(--at-radius-md);
  border: 1px solid color-mix(in srgb, var(--accent-emerald, #10b981) 45%, transparent);
  background: color-mix(in srgb, var(--accent-emerald, #10b981) 10%, transparent);
}

.mg-success-icon {
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--accent-emerald, #10b981);
  color: #fff;
}

.mg-success-icon svg {
  width: 17px;
  height: 17px;
}

.mg-success-body {
  display: grid;
  gap: 3px;
}

.mg-success-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-primary);
}

.mg-success-desc {
  font-size: 12.5px;
  color: var(--text-secondary);
  line-height: 1.5;
}

/* ---------- Preview ---------- */
.mg-preview {
  margin-block-start: 4px;
}

.mg-preview-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-block-end: 6px;
  scrollbar-width: thin;
}

.mg-preview-page {
  flex: 0 0 auto;
  width: 74px;
  border: 1px solid var(--glass-border);
  border-radius: 7px;
  overflow: hidden;
  background: var(--bg-tertiary);
  display: grid;
  place-items: center;
  aspect-ratio: 3 / 4;
}

.mg-preview-page img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.mg-preview-page .mg-thumb-spin {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 2px solid var(--glass-border-hover);
  border-top-color: var(--mg-accent);
  animation: at-spin .7s linear infinite;
}

.mg-preview-more {
  flex: 0 0 auto;
  width: 74px;
  aspect-ratio: 3 / 4;
  display: grid;
  place-items: center;
  border: 1px dashed var(--glass-border);
  border-radius: 7px;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-tertiary);
}

/* ---------- Result size / file chip ---------- */
.mg-file-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--glass-border);
  background: var(--bg-secondary);
  font-size: 12.5px;
  color: var(--text-secondary);
}

.mg-file-chip svg {
  width: 16px;
  height: 16px;
  color: var(--mg-accent);
  flex: 0 0 auto;
}

.mg-file-chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- Progress (overrides .at-progress fill) ---------- */
body.at-merge .at-progress-fill {
  background: linear-gradient(90deg, var(--mg-accent), var(--mg-accent-2));
}

.mg-progress-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-block-start: 9px;
}

.mg-progress-row .at-progress-msg {
  margin: 0;
}

/* ---------- Sticky run panel highlight ---------- */
.mg-run-panel {
  background:
    linear-gradient(180deg, color-mix(in srgb, var(--mg-accent) 7%, transparent), transparent 60%),
    var(--bg-card);
}

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .mg-drop,
  .mg-file,
  .mg-page,
  .mg-switch-track,
  .mg-switch-track::after {
    transition: none !important;
  }
}
