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

/* ============================
   CSS VARIABLES / DESIGN TOKENS
   ============================ */
:root {
  --bg-body: #030712;
  --bg-surface: rgba(15, 23, 42, 0.6);
  --bg-surface-solid: #0f172a;
  --bg-input: rgba(30, 41, 59, 0.6);
  --bg-hover: rgba(51, 65, 85, 0.4);

  --accent: #06b6d4;
  --accent-light: #22d3ee;
  --accent-dark: #0891b2;
  --accent-glow: rgba(6, 182, 212, 0.25);
  --accent-subtle: rgba(6, 182, 212, 0.08);

  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-dim: #64748b;

  --border: rgba(51, 65, 85, 0.6);
  --border-focus: var(--accent-light);
  --danger: #f43f5e;
  --success: #10b981;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-xl: 24px;
  --radius-full: 9999px;

  --shadow-sm: 0 2px 8px rgba(0,0,0,0.15);
  --shadow-md: 0 8px 30px rgba(0,0,0,0.25);
  --shadow-lg: 0 20px 60px rgba(0,0,0,0.4);
  --shadow-glow: 0 0 30px var(--accent-glow);

  --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  --transition-fast: all 0.15s ease;
  --transition-bounce: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

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

html { scroll-behavior: smooth; }

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg-body);
  color: var(--text-primary);
  min-height: 100vh;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

/* Ambient background glow */
body::before {
  content: '';
  position: fixed; top: -50%; left: -50%;
  width: 200%; height: 200%;
  background: radial-gradient(circle at 20% 50%, rgba(6,182,212,0.04) 0%, transparent 40%),
              radial-gradient(circle at 80% 20%, rgba(59,130,246,0.03) 0%, transparent 35%),
              radial-gradient(circle at 50% 80%, rgba(139,92,246,0.02) 0%, transparent 30%);
  z-index: -1;
  animation: ambientShift 20s ease-in-out infinite alternate;
}

@keyframes ambientShift {
  0% { transform: translate(0, 0) rotate(0deg); }
  100% { transform: translate(-2%, 1%) rotate(3deg); }
}

/* Scrollbar */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }

::selection { background: var(--accent); color: #000; }

/* ============================
   UTILITIES
   ============================ */
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }

/* ============================
   LOGIN SCREEN
   ============================ */
.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 2rem;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-surface);
  backdrop-filter: blur(40px);
  -webkit-backdrop-filter: blur(40px);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 3rem 2.5rem;
  box-shadow: var(--shadow-lg);
  text-align: center;
  animation: cardAppear 0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes cardAppear {
  0% { opacity: 0; transform: translateY(30px) scale(0.96); }
  100% { opacity: 1; transform: translateY(0) scale(1); }
}

.login-logo {
  font-size: 3rem;
  margin-bottom: 0.5rem;
  display: block;
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

.login-title {
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: -0.5px;
  margin-bottom: 0.25rem;
}

.login-title span { color: var(--accent); }

.login-subtitle {
  color: var(--text-dim);
  font-size: 0.85rem;
  margin-bottom: 2rem;
}

.login-input-group {
  position: relative;
  margin-bottom: 1.25rem;
}

.login-input {
  width: 100%;
  background: var(--bg-input);
  border: 1.5px solid var(--border);
  color: var(--text-primary);
  padding: 0.9rem 1.2rem 0.9rem 3rem;
  border-radius: var(--radius-md);
  font-size: 0.95rem;
  font-family: inherit;
  transition: var(--transition);
  letter-spacing: 2px;
}

.login-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
  background: var(--bg-surface-solid);
}

.login-input-icon {
  position: absolute;
  left: 1rem; top: 50%;
  transform: translateY(-50%);
  color: var(--text-dim);
  font-size: 1.1rem;
}

.login-error {
  color: var(--danger);
  font-size: 0.8rem;
  margin-bottom: 1rem;
  min-height: 1.2rem;
}

/* ============================
   BUTTONS
   ============================ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  font-family: inherit;
  font-weight: 700;
  border: none;
  cursor: pointer;
  transition: var(--transition);
  border-radius: var(--radius-md);
}

.btn-primary {
  width: 100%;
  padding: 0.9rem 1.5rem;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-dark) 100%);
  color: #fff;
  font-size: 0.95rem;
  box-shadow: var(--shadow-glow);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 0 40px var(--accent-glow);
  filter: brightness(1.1);
}

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

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none !important;
  box-shadow: none !important;
}

.btn-download {
  padding: 0.7rem 1.5rem;
  background: rgba(255,255,255,0.1);
  color: var(--text-primary);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  backdrop-filter: blur(10px);
}

.btn-download:hover {
  background: rgba(255,255,255,0.15);
  border-color: var(--accent);
  transform: translateY(-1px);
}

/* ============================
   MAIN APP LAYOUT
   ============================ */
.app-wrapper {
  max-width: 1440px;
  margin: 0 auto;
  padding: 1.5rem;
  animation: fadeIn 0.5s ease forwards;
}

@keyframes fadeIn { 0% { opacity: 0; } 100% { opacity: 1; } }

/* Header */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 1.25rem;
  margin-bottom: 1.5rem;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  gap: 1rem;
}

.brand h1 {
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: -0.5px;
}

.brand h1 span { color: var(--accent); }
.brand p { color: var(--text-dim); font-size: 0.8rem; margin-top: 2px; }

.header-actions { display: flex; align-items: center; gap: 0.75rem; }

.status-pill {
  display: flex; align-items: center; gap: 0.4rem;
  font-size: 0.75rem; color: var(--text-secondary);
  background: var(--bg-input); padding: 0.4rem 0.9rem;
  border-radius: var(--radius-full); border: 1px solid var(--border);
}

.status-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 8px var(--success);
  animation: blink 2s infinite;
}

@keyframes blink { 0%,100% { opacity: 1; } 50% { opacity: 0.4; } }

.btn-logout {
  padding: 0.4rem 0.9rem;
  background: rgba(244, 63, 94, 0.1);
  color: var(--danger);
  border: 1px solid rgba(244, 63, 94, 0.3);
  font-size: 0.75rem;
  border-radius: var(--radius-full);
  cursor: pointer;
  font-family: inherit;
  font-weight: 600;
  transition: var(--transition);
}

.btn-logout:hover { background: rgba(244, 63, 94, 0.2); }

/* Split Layout */
.studio-layout {
  display: grid;
  grid-template-columns: 380px 1fr;
  gap: 1.5rem;
  align-items: start;
}

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

/* ============================
   GLASS PANELS
   ============================ */
.panel {
  background: var(--bg-surface);
  backdrop-filter: blur(30px);
  -webkit-backdrop-filter: blur(30px);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  box-shadow: var(--shadow-md);
}

/* ============================
   TAB GROUP
   ============================ */
.tab-group {
  display: flex;
  background: var(--bg-body);
  padding: 3px;
  border-radius: var(--radius-md);
  margin-bottom: 1.5rem;
  border: 1px solid var(--border);
  gap: 3px;
}

.tab-btn {
  flex: 1;
  padding: 0.55rem 0.4rem;
  background: transparent;
  border: none;
  border-radius: 7px;
  color: var(--text-dim);
  font-size: 0.82rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: var(--transition-fast);
  position: relative;
}

.tab-btn:hover { color: var(--text-secondary); }

.tab-btn.active {
  background: var(--bg-hover);
  color: var(--accent);
  box-shadow: var(--shadow-sm);
}

/* ============================
   FORM ELEMENTS
   ============================ */
.form-group { margin-bottom: 1.15rem; }

.form-label {
  display: block;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-secondary);
  margin-bottom: 0.4rem;
}

.input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 0.65rem 0.9rem;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  font-family: inherit;
  transition: var(--transition-fast);
}

.input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow);
}

.input::placeholder { color: var(--text-dim); }

textarea.input { resize: vertical; min-height: 75px; line-height: 1.5; }

select.input {
  appearance: none;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%2394a3b8' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10l-5 5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  padding-right: 2rem;
}

input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; }
input[type=number] { -moz-appearance: textfield; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0.75rem; }

/* ============================
   UPLOAD BOXES
   ============================ */
.upload-box {
  position: relative;
  border: 2px dashed var(--border);
  background: var(--bg-input);
  border-radius: var(--radius-md);
  padding: 1.25rem 0.75rem;
  text-align: center;
  cursor: pointer;
  transition: var(--transition);
  min-height: 90px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  overflow: hidden;
}

.upload-box:hover { border-color: var(--accent); background: var(--accent-subtle); }

.upload-icon { font-size: 1.4rem; opacity: 0.6; margin-bottom: 0.3rem; }
.upload-text { font-size: 0.7rem; color: var(--text-dim); line-height: 1.4; }

.upload-preview {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  border-radius: calc(var(--radius-md) - 2px);
  display: none;
}

.upload-clear {
  position: absolute; top: 6px; right: 6px;
  background: var(--danger); color: #fff;
  border: none;
  width: 22px; height: 22px;
  border-radius: 50%;
  font-size: 0.8rem; font-weight: 700;
  cursor: pointer;
  display: none;
  z-index: 5;
  transition: var(--transition-fast);
  line-height: 1;
}

.upload-clear:hover { transform: scale(1.15); }

/* Ref Gallery */
.ref-gallery {
  display: flex; gap: 0.4rem;
  overflow-x: auto; margin-top: 0.5rem;
  padding-bottom: 0.4rem;
}

.ref-item {
  width: 44px; height: 44px; flex-shrink: 0;
  border-radius: 6px; object-fit: cover;
  border: 1.5px solid var(--border);
  transition: var(--transition-fast);
}

.ref-item:hover { border-color: var(--accent); transform: scale(1.08); }

/* ============================
   ACCORDION
   ============================ */
.accordion {
  border-top: 1px solid var(--border);
  padding-top: 0.75rem;
  margin-top: 0.5rem;
}

.accordion-trigger {
  display: flex; justify-content: space-between; align-items: center;
  cursor: pointer; color: var(--text-dim);
  font-size: 0.78rem; font-weight: 600;
  padding: 0.4rem 0;
  transition: var(--transition-fast);
  user-select: none;
}

.accordion-trigger:hover { color: var(--text-secondary); }

.accordion-arrow {
  transition: transform 0.3s ease;
  font-size: 0.65rem;
}

.accordion-body {
  max-height: 0; overflow: hidden;
  transition: max-height 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.accordion-body.open { max-height: 500px; }

.accordion-inner { padding-top: 0.75rem; }

/* ============================
   TOGGLE SWITCH
   ============================ */
.toggle-row {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 0.6rem;
  padding: 0.35rem 0;
}

.toggle-text { font-size: 0.8rem; color: var(--text-secondary); font-weight: 500; }

.toggle {
  position: relative;
  width: 38px; height: 20px;
  background: var(--border);
  border-radius: 20px;
  cursor: pointer;
  transition: var(--transition);
  flex-shrink: 0;
}

.toggle::after {
  content: '';
  position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px;
  background: #fff;
  border-radius: 50%;
  transition: var(--transition);
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

.toggle.on { background: var(--accent); }
.toggle.on::after { transform: translateX(18px); }

/* ============================
   CANVAS / OUTPUT AREA
   ============================ */
.canvas-area {
  position: relative;
  min-height: 520px;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  border-radius: var(--radius-lg);
  overflow: hidden;
  background:
    linear-gradient(var(--bg-surface-solid) 1px, transparent 1px),
    linear-gradient(90deg, var(--bg-surface-solid) 1px, transparent 1px);
  background-size: 30px 30px;
  background-color: rgba(3,7,18,0.5);
  border: 1px solid var(--border);
}

.canvas-placeholder {
  text-align: center;
  color: var(--text-dim);
  padding: 2rem;
  animation: fadeIn 0.5s ease;
}

.canvas-placeholder .icon { font-size: 2.5rem; margin-bottom: 0.75rem; opacity: 0.5; }
.canvas-placeholder h3 { font-size: 1rem; font-weight: 600; margin-bottom: 0.3rem; color: var(--text-secondary); }
.canvas-placeholder p { font-size: 0.8rem; }

.result-img {
  max-width: 95%; max-height: 55vh;
  object-fit: contain;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  animation: imgReveal 0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes imgReveal {
  0% { opacity: 0; transform: scale(0.92) translateY(15px); }
  100% { opacity: 1; transform: scale(1) translateY(0); }
}

/* Action bar */
.action-bar {
  display: flex; justify-content: space-between; align-items: center;
  margin-top: 1rem; padding: 0.75rem 1rem;
  background: var(--bg-surface);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  animation: fadeIn 0.4s ease;
  width: 100%;
}

.meta-badges { display: flex; gap: 0.5rem; flex-wrap: wrap; }

.badge {
  font-size: 0.7rem; font-weight: 600;
  padding: 0.3rem 0.6rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  color: var(--text-secondary);
}

.badge strong { color: var(--accent); }

/* Enhanced prompt */
.enhanced-prompt {
  margin-top: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.78rem;
  color: var(--text-dim);
  font-style: italic;
  line-height: 1.5;
  max-height: 80px;
  overflow-y: auto;
}

.enhanced-prompt-label {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
  margin-bottom: 0.3rem;
}

/* ============================
   LOADING OVERLAY
   ============================ */
.loading-overlay {
  position: absolute; inset: 0;
  background: rgba(3, 7, 18, 0.85);
  backdrop-filter: blur(8px);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  z-index: 50;
}

.loader-box { width: 260px; }

.loader-header {
  display: flex; justify-content: space-between;
  font-size: 0.8rem; font-weight: 600;
  margin-bottom: 0.5rem;
}

.loader-status { color: var(--accent); animation: blink 1.5s infinite; }
.loader-pct { color: var(--text-primary); font-size: 1.4rem; font-weight: 800; }

.loader-track {
  width: 100%; height: 5px;
  background: var(--border); border-radius: 3px;
  overflow: hidden;
}

.loader-fill {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, var(--accent-dark), var(--accent-light));
  transition: width 0.3s ease;
  border-radius: 3px;
  box-shadow: 0 0 12px var(--accent-glow);
}

/* ============================
   THOUGHTS STRIP
   ============================ */
.thoughts-section {
  width: 100%;
  margin-top: 1rem;
  padding: 1rem;
  border-top: 1px solid var(--border);
}

.thoughts-section h4 {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
  margin-bottom: 0.75rem;
}

.thoughts-row {
  display: flex; gap: 0.75rem;
  overflow-x: auto; padding-bottom: 0.5rem;
}

.thought-thumb {
  width: 80px; height: 80px; flex-shrink: 0;
  object-fit: cover;
  border-radius: var(--radius-sm);
  border: 2px solid var(--border);
  opacity: 0.75;
  cursor: pointer;
  transition: var(--transition);
}

.thought-thumb:hover {
  opacity: 1;
  border-color: var(--accent);
  transform: scale(1.06);
}

/* ============================
   ERROR ALERT
   ============================ */
.error-toast {
  position: absolute; top: 1rem; left: 1rem; right: 1rem;
  background: rgba(244, 63, 94, 0.15);
  border: 1px solid rgba(244, 63, 94, 0.4);
  backdrop-filter: blur(12px);
  color: #fda4af;
  padding: 0.9rem 1.1rem;
  border-radius: var(--radius-md);
  font-size: 0.82rem;
  z-index: 60;
  display: flex; justify-content: space-between; align-items: start; gap: 0.75rem;
  animation: slideDown 0.3s ease;
}

@keyframes slideDown {
  0% { opacity: 0; transform: translateY(-10px); }
  100% { opacity: 1; transform: translateY(0); }
}

.error-toast strong { display: block; margin-bottom: 0.2rem; color: #fecdd3; }

.error-close {
  background: none; border: none; color: inherit;
  cursor: pointer; font-size: 1.1rem; padding: 0;
  opacity: 0.7; transition: var(--transition-fast);
}

.error-close:hover { opacity: 1; }

/* ============================
   RESPONSIVE
   ============================ */
@media (max-width: 640px) {
  body { padding: 0; }
  .app-wrapper { padding: 1rem; }
  .brand h1 { font-size: 1.2rem; }
  .grid-2 { grid-template-columns: 1fr; }
  .canvas-area { min-height: 350px; }
  .login-card { padding: 2rem 1.5rem; }
}

/* ============================
   GALLERY & ASSETS
   ============================ */
.gallery-section {
  background: var(--surface);
  border-radius: var(--radius-md);
  padding: 1rem;
  margin-top: 1.5rem;
  margin-bottom: 1rem;
  border: 1px solid var(--border);
}
.gallery-scroll {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
  gap: 0.5rem;
  max-height: 200px;
  overflow-y: auto;
  padding-right: 0.25rem;
}
.gallery-scroll::-webkit-scrollbar { width: 4px; }
.gallery-scroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.gallery-item {
  position: relative;
  border-radius: var(--radius-sm);
  overflow: hidden;
  aspect-ratio: 1/1;
  cursor: pointer;
  border: 2px solid transparent;
  transition: var(--transition-fast);
}
.gallery-item:hover { border-color: var(--accent); }
.gallery-item img {
  width: 100%; height: 100%; object-fit: cover;
}
.gallery-item-delete {
  position: absolute;
  top: 2px; right: 2px;
  background: rgba(0,0,0,0.7);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 18px; height: 18px;
  font-size: 10px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.gallery-item-delete:hover { background: var(--danger); }

/* Remove previous hidden class on modals if needed by overriding display */
#galleryModal:not(.hidden) { display: flex !important; }
