/* ===== Design Tokens ===== */
:root {
  --font: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --bg: #0a0c12;
  /* Image Studio Bg */
  --bg-panel: #11131a;
  /* Image Studio Panel */
  --bg-header: #161922;
  /* Image Studio Header */
  --bg-surface: rgba(255, 255, 255, 0.04);
  --bg-glass: rgba(30, 34, 50, 0.65);

  --border: rgba(255, 255, 255, 0.08);
  --border-hover: rgba(139, 92, 246, 0.4);

  --text: #e8eaed;
  --text-muted: #8b8fa3;
  --text-dim: #5a5e72;

  --accent: #a78bfa;
  --accent-bright: #c4b5fd;
  --accent-hover: #8b5cf6;
  --accent-glow: rgba(167, 139, 250, 0.25);

  --danger: #f87171;
  --success: #34d399;

  --radius: 8px;
  /* Unified to 8px */
  --radius-sm: 6px;
  --radius-lg: 12px;

  --header-height: 56px;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  --transition: 200ms cubic-bezier(.4, 0, .2, 1);
}

/* ===== Reset & Base ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
}

/* ===== App Shell ===== */
.app-shell {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ===== Top Bar ===== */
.top-bar {
  height: var(--header-height);
  background: rgba(15, 17, 23, 0.4);
  backdrop-filter: blur(25px);
  -webkit-backdrop-filter: blur(25px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  flex-shrink: 0;
  z-index: 10;
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  width: 24px;
  height: 24px;
  color: var(--accent);
  filter: drop-shadow(0 0 5px rgba(167, 139, 250, 0.5));
}

.logo h1 {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text);
  text-shadow: 0 0 20px rgba(167, 139, 250, 0.3);
}

.logo .accent {
  color: var(--accent);
  background: none;
  -webkit-text-fill-color: initial;
}

.back-home {
  color: var(--text-muted);
  padding: 4px;
  border-radius: 4px;
  transition: color 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-home:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.05);
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* ===== Main Container ===== */
.app-container {
  flex: 1;
  position: relative;
  z-index: 1;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
  padding: 40px 24px;
}

/* ===== Background Orbs ===== */
.bg-orbs {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}

.orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  /* Reduced blur */
  opacity: 0.6;
  /* Increased opacity */
  mix-blend-mode: screen;
}

.orb-1 {
  width: 700px;
  height: 700px;
  background: radial-gradient(circle, #8b5cf6 0%, transparent 70%);
  top: -250px;
  left: -150px;
  animation: float1 20s ease-in-out infinite;
}

.orb-2 {
  width: 600px;
  height: 600px;
  background: radial-gradient(circle, #3b82f6 0%, transparent 70%);
  bottom: -200px;
  right: -150px;
  animation: float2 25s ease-in-out infinite;
}

.orb-3 {
  width: 500px;
  height: 500px;
  background: radial-gradient(circle, #ec4899 0%, transparent 70%);
  top: 40%;
  left: 50%;
  animation: float3 18s ease-in-out infinite;
  opacity: 0.3;
}

@keyframes float1 {

  0%,
  100% {
    transform: translate(0, 0)
  }

  50% {
    transform: translate(60px, 40px)
  }
}

@keyframes float2 {

  0%,
  100% {
    transform: translate(0, 0)
  }

  50% {
    transform: translate(-50px, -30px)
  }
}

@keyframes float3 {

  0%,
  100% {
    transform: translate(0, 0)
  }

  50% {
    transform: translate(-40px, 50px)
  }
}

/* ===== App Container ===== */
.app-container {
  position: relative;
  z-index: 1;
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 24px 100px;
}

/* ===== Back Link ===== */
.back-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-dim);
  text-decoration: none;
  margin-bottom: 16px;
  padding: 6px 12px 6px 8px;
  border-radius: 100px;
  transition: all var(--transition);
  animation: fadeIn 0.4s ease-out;
}

.back-link:hover {
  color: var(--accent);
  background: rgba(167, 139, 250, 0.08);
}

/* ===== Header ===== */
.app-header {
  text-align: center;
  margin-bottom: 40px;
  animation: fadeInDown 0.6s ease-out;
}

.logo {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}

.logo-icon {
  width: 36px;
  height: 36px;
  color: var(--accent);
}

.logo h1 {
  font-size: 1.75rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--text);
}

.logo .accent {
  background: linear-gradient(135deg, var(--accent), #818cf8);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.95rem;
  font-weight: 400;
}

/* ===== Upload Zone ===== */
.upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius-lg);
  padding: 60px 32px;
  text-align: center;
  cursor: pointer;
  transition: border-color var(--transition), background var(--transition), transform var(--transition);
  background: var(--bg-surface);
  animation: fadeInUp 0.6s ease-out 0.1s both;
}

.upload-zone:hover,
.upload-zone.drag-over {
  border-color: var(--accent);
  background: var(--accent-glow);
  transform: translateY(-2px);
}

.upload-zone.drag-over {
  border-style: solid;
}

.upload-icon-wrapper {
  width: 72px;
  height: 72px;
  margin: 0 auto 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: linear-gradient(135deg, rgba(167, 139, 250, 0.15), rgba(129, 140, 248, 0.10));
  color: var(--accent);
  transition: transform var(--transition);
}

.upload-zone:hover .upload-icon-wrapper {
  transform: translateY(-4px);
}

.upload-icon {
  width: 32px;
  height: 32px;
}

.upload-content h2 {
  font-size: 1.2rem;
  font-weight: 600;
  margin-bottom: 4px;
}

.upload-content p {
  color: var(--text-muted);
  font-size: 0.9rem;
}

.browse-link {
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
  font-weight: 500;
  transition: color var(--transition);
}

.browse-link:hover {
  color: var(--accent-bright);
}

.upload-hint {
  display: inline-block;
  margin-top: 12px;
  font-size: 0.75rem;
  color: var(--text-dim);
  background: var(--bg-card);
  padding: 4px 12px;
  border-radius: 100px;
}

/* ===== Toolbar ===== */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-glass);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 16px;
  margin-bottom: 20px;
  animation: fadeInDown 0.4s ease-out;
  position: sticky;
  top: 12px;
  z-index: 10;
}

.toolbar-left,
.toolbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.toolbar-center {
  display: flex;
  align-items: center;
}

.toolbar-divider {
  width: 1px;
  height: 24px;
  background: var(--border);
  margin: 0 4px;
}

.page-count {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 500;
  white-space: nowrap;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: 6px;
  /* Matched Image Studio */
  font-family: var(--font);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  padding: 8px 16px;
  white-space: nowrap;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

.btn-subtle {
  background: transparent;
  color: var(--text-muted);
}

.btn-subtle:hover:not(:disabled) {
  color: var(--text);
  background: rgba(255, 255, 255, 0.05);
}

.btn-icon {
  width: 32px;
  /* Fixed size like Image Studio */
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid transparent;
  /* Match structure */
  border-radius: 6px;
  color: var(--text-muted);
  padding: 0;
}

.btn-icon:hover:not(:disabled) {
  color: var(--text);
  background: rgba(255, 255, 255, 0.05);
  border-color: var(--border);
}

.btn-danger:hover:not(:disabled) {
  background: rgba(248, 113, 113, 0.1);
  color: var(--danger);
  border-color: rgba(248, 113, 113, 0.2);
}

.btn-primary {
  background: linear-gradient(135deg, #7c3aed, #6d28d9);
  color: #fff;
  font-weight: 600;
  box-shadow: none;
  /* Image Studio is flatter */
}

.btn-primary:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(124, 58, 237, 0.3);
}

.btn-primary:active:not(:disabled) {
  transform: translateY(0);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.1);
}

/* ===== Page Grid ===== */
.page-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
  animation: fadeIn 0.4s ease-out;
}

.page-card {
  position: relative;
  background: var(--bg-card);
  border: 2px solid transparent;
  border-radius: var(--radius);
  padding: 8px;
  cursor: grab;
  transition: all var(--transition);
  animation: cardIn 0.3s ease-out both;
}

.page-card:hover {
  background: var(--bg-card-hover);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

.page-card.selected {
  border-color: var(--accent);
  background: rgba(167, 139, 250, 0.08);
  box-shadow: 0 0 0 1px var(--accent), 0 4px 20px var(--accent-glow);
}

.page-card.sortable-ghost {
  opacity: 0.4;
  transform: scale(0.95);
}

.page-card.sortable-chosen {
  cursor: grabbing;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  z-index: 20;
}

.page-canvas-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 8.5/11;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: #1a1c28;
  display: flex;
  align-items: center;
  justify-content: center;
}

.page-canvas-wrapper canvas {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.page-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 4px 2px;
}

.page-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 70%;
}

.page-number {
  font-size: 0.65rem;
  color: var(--text-dim);
  font-weight: 600;
  background: var(--bg-surface);
  padding: 2px 8px;
  border-radius: 100px;
}

.page-select-indicator {
  position: absolute;
  top: 12px;
  left: 12px;
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: var(--bg-glass);
  backdrop-filter: blur(8px);
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
  z-index: 5;
}

.page-card.selected .page-select-indicator {
  background: var(--accent);
  border-color: var(--accent);
}

.page-card.selected .page-select-indicator::after {
  content: '';
  width: 10px;
  height: 6px;
  border-left: 2px solid #fff;
  border-bottom: 2px solid #fff;
  transform: rotate(-45deg) translateY(-1px);
}

.page-rotation-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  font-size: 0.6rem;
  font-weight: 700;
  color: var(--accent);
  background: var(--bg-glass);
  backdrop-filter: blur(8px);
  padding: 2px 6px;
  border-radius: 4px;
  z-index: 5;
  display: none;
}

.page-card[data-rotation]:not([data-rotation="0"]) .page-rotation-badge {
  display: block;
}

/* ===== Action Bar ===== */
.action-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 20px;
  animation: fadeInUp 0.4s ease-out;
}

/* ===== Toast ===== */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
}

.toast {
  background: var(--bg-glass);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 20px;
  font-size: 0.85rem;
  color: var(--text);
  box-shadow: var(--shadow);
  animation: toastIn 0.3s ease-out;
  display: flex;
  align-items: center;
  gap: 10px;
}

.toast.success {
  border-left: 3px solid var(--success);
}

.toast.error {
  border-left: 3px solid var(--danger);
}

.toast.info {
  border-left: 3px solid var(--accent);
}

.toast-exit {
  animation: toastOut 0.3s ease-in forwards;
}

/* ===== Utility ===== */
.hidden {
  display: none !important;
}

/* ===== Animations ===== */
@keyframes fadeIn {
  from {
    opacity: 0
  }

  to {
    opacity: 1
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(16px)
  }

  to {
    opacity: 1;
    transform: translateY(0)
  }
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-12px)
  }

  to {
    opacity: 1;
    transform: translateY(0)
  }
}

@keyframes cardIn {
  from {
    opacity: 0;
    transform: scale(0.9)
  }

  to {
    opacity: 1;
    transform: scale(1)
  }
}

@keyframes cardAppend {
  0% {
    opacity: 0;
    transform: scale(0.6) translateY(30px);
  }

  50% {
    opacity: 1;
    transform: scale(1.04) translateY(-4px);
  }

  70% {
    transform: scale(0.98) translateY(1px);
  }

  100% {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes glowPulse {
  0% {
    box-shadow: 0 0 0 0 rgba(167, 139, 250, 0.4);
  }

  50% {
    box-shadow: 0 0 20px 4px rgba(167, 139, 250, 0.25);
  }

  100% {
    box-shadow: 0 0 0 0 rgba(167, 139, 250, 0);
  }
}

.page-card.card-new {
  animation: cardAppend 0.45s cubic-bezier(.34, 1.56, .64, 1) both,
    glowPulse 1.2s ease-out 0.4s both;
}

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translateX(40px)
  }

  to {
    opacity: 1;
    transform: translateX(0)
  }
}

@keyframes toastOut {
  from {
    opacity: 1;
    transform: translateX(0)
  }

  to {
    opacity: 0;
    transform: translateX(40px)
  }
}

@keyframes shake {

  0%,
  100% {
    transform: translateX(0)
  }

  20% {
    transform: translateX(-6px)
  }

  40% {
    transform: translateX(6px)
  }

  60% {
    transform: translateX(-4px)
  }

  80% {
    transform: translateX(4px)
  }
}

.shake {
  animation: shake 0.4s ease-in-out;
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
  width: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 100px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-dim);
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .app-container {
    padding: 24px 16px 80px;
  }

  .page-grid {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 10px;
  }

  .toolbar {
    flex-wrap: wrap;
    gap: 8px;
  }

  .toolbar-center {
    order: 3;
    width: 100%;
    justify-content: center;
  }

  .upload-zone {
    padding: 40px 20px;
  }

  .action-bar {
    flex-direction: column;
  }
}

/* ===== Loading spinner on page cards ===== */
.page-loading {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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