:root {
  --bg: #0b1020;
  --panel: #111827;
  --text: #e5e7eb;
  --muted: #9ca3af;
  --accent: #7c3aed;
  --accent2: #22c55e;
}

* {
  box-sizing: border-box
}

body {
  margin: 0;
  font: 16px system-ui, -apple-system, Segoe UI, Roboto;
  color: var(--text);
  background: linear-gradient(180deg, #0b1020, #0e1328)
}

header {
  position: sticky;
  top: 0;
  padding: 0 16px;
  background: rgba(17, 24, 39, .8);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid #1f2937
}

/* Ensure header sits above floating toolbars / result controls when scrolling */
header {
  z-index: 60; /* above most UI elements like toolbars/toasts */
}

img.logo {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  display: inline-block
}

footer {
  padding: 16px;
  background: rgba(17, 24, 39, .8);
  border-top: 1px solid #1f2937
}

footer .container {
  max-width: 1920px;
  margin: 0 auto;
  padding: 0 16px;
  display: flex;
  gap: 8px;
  align-items: center
}

footer a {
  color: #a78bfa;
  text-decoration: none
}

footer a:hover {
  text-decoration: underline
}

main {
  max-width: 1920px;
  margin: 24px auto;
  padding: 0 16px 64px
}

.card {
  background: var(--panel);
  border: 1px solid #1f2937;
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .25)
}

h1 {
  font-size: 20px
}

h2 {
  margin: 0 0 8px;
  font-size: 16px;
  color: #cbd5e1
}

label {
  display: block;
  margin: 10px 0 6px;
  color: #cbd5e1
}

input[type="text"],
input[type="number"],
input[type="password"],
textarea {
  width: 100%;
  background: #0b1020;
  color: var(--text);
  border: 1px solid #253047;
  border-radius: 12px;
  padding: 10px 12px
}

select {
  width: 100%;
  background: #0b1020;
  color: var(--text);
  border: 1px solid #253047;
  border-radius: 12px;
  padding: 10px 12px;
  appearance: none;
}

/* Add a chevron indicator to selects (right-aligned) */
select {
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="%23a78bfa" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"/></svg>');
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 16px 16px;
  padding-right: 40px; /* space for chevron */
}

/* Disabled inputs appear grayed and use same rounded style */
input:disabled,
select:disabled,
textarea:disabled {
  background: #071022;
  color: var(--muted);
  border-color: #1f2937;
  opacity: 1; /* keep full opacity but muted color */
}

textarea {
  min-height: 72px;
  resize: vertical
}

.row {
  display: grid;
  gap: 12px
}

@media(min-width:760px) {
  .row-2 {
    grid-template-columns: 1fr 1fr
  }

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

/* Improved layout: prevent source image column from becoming too wide so prompt area stays usable */
@media(min-width:760px){
  .row-2:not(.t2i){
    grid-template-columns: minmax(260px, 420px) 1fr; /* cap first column */
    align-items: start;
  }
  #sourceImageSection{ 
    max-width:420px; 
    overflow: hidden; /* prevent content from bleeding out */
  }
  #sourceImageSection .thumb{ width:100%; }
  #sourceImageSection input[type="file"]{ 
    width: 100%; 
    max-width: 100%;
    overflow: hidden;
    white-space: nowrap;
  }
}

button {
  appearance: none;
  background: var(--accent);
  color: white;
  border: 0;
  border-radius: 12px;
  padding: 10px 14px;
  font-weight: 600;
  cursor: pointer
}

button.secondary {
  background: #253047;
  color: #e5e7eb
}

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

.toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center
}

.preview {
  display: grid;
  gap: 12px
}

/* Text-to-Image mode: make prompt/negative prompt take full card width
   when source image is hidden. Applied by adding `t2i` class to .row-2 */
.row-2.t2i {
  grid-template-columns: 1fr;
}

.row-2.t2i .preview textarea {
  width: 100%;
  min-height: 140px;
}

.thumb {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 220px;
  border: 1px dashed #334155;
  border-radius: 12px;
  background: #0b1020;
  padding: 8px
}

.thumb img {
  max-width: 100%;
  max-height: 60vh;
  border-radius: 12px
}

/* Style for the result placeholder */
.result-placeholder {
  color: var(--muted);

  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 220px;
  width: 100%;
  font-size: 15px;
}

/* Constrain source image preview inside input so it doesn't dominate layout.
   Large landscape images were shrinking the prompt textareas; tall portraits stretched the card.
   Limit displayed size while preserving aspect ratio. Result image (#resultImg) keeps existing sizing. */
#imgThumb img {
  /* Fit within a reasonable box; adjust as needed */
  max-width: min(420px, 100%);
  max-height: 420px;
  width: auto;
  height: auto;
  object-fit: contain;
}

.muted {
  color: var(--muted);
  font-size: 13px
}

pre.log {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font: 12px ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  max-height: 240px;
  overflow: auto;
  background: #0b1020;
  border: 1px solid #253047;
  border-radius: 12px;
  padding: 12px
}

.pill {
  display: inline-flex;
  gap: 8px;
  background: #0b1020;
  border: 1px solid #253047;
  border-radius: 999px;
  padding: 8px 12px;
  align-items: center
}

.spacer {
  height: 8px
}

.success {
  color: var(--accent2)
}

.error {
  color: #ef4444
}

.grid-cols-2 {
  display: grid;
  gap: 12px;
  grid-template-columns: 1fr 1fr
}

.two-col {
  display: grid;
  gap: 12px
}

@media(min-width:1280px) {
  .two-col {
    grid-template-columns: 1fr 1fr;
    align-items: start
  }
}

/* Make the top two cards (API Key + Mode) sit side-by-side at >=768px */
@media(min-width:768px) {
  .two-col.top-two {
    grid-template-columns: 1fr 1fr;
    align-items: stretch;
  }
}

/* Ensure cards inside the top-two container stretch to equal height and content aligns */
@media(min-width:768px) {
  .two-col.top-two .card {
    display: flex;
    flex-direction: column;
    height: 100%;
  }
  /* Keep the card's main content from stretching oddly; allow internal spanners to grow */
  .two-col.top-two .card > .row,
  .two-col.top-two .card > .toolbar,
  .two-col.top-two .card > .thumb,
  .two-col.top-two .card > .preview {
    flex: 0 0 auto;
  }
  .two-col.top-two .card > .row:last-child {
    margin-top: auto;
  }
}

/* Loading spinner next to run status */
.loading::before {
  content: "";
  display: inline-block;
  width: 14px;
  height: 14px;
  margin-right: 8px;
  border: 2px solid var(--muted);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  vertical-align: -4px;
}

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

/* Drag & drop highlight for image thumb */
.thumb.drop-hover {
  border-color: var(--accent);
  background: #0c1226
}

/* Toast notifications */
#toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 9999;
  display: none;
  background: #0b1020;
  color: var(--text);
  border: 1px solid #253047;
  border-radius: 12px;
  padding: 10px 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .35)
}

#toast.show {
  display: block
}

#toast.success {
  border-left: 4px solid var(--accent2)
}

#toast.error {
  border-left: 4px solid #ef4444
}

/* Mode toggle styles */
.mode-toggle {
  display: flex;
  gap: 0;
  border-radius: 8px;
  padding: 4px;
  max-width: 320px;
  label:not(last-of-type) {
    margin-right: 10px;
  }
}

.mode-toggle input[type="radio"] {
  display: none;
}

.mode-toggle label {
  flex: 1;
  text-align: center;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
  user-select: none;
  font-weight: 500;
}

.mode-toggle label:hover {
  background: #374151;
}

.mode-toggle input[type="radio"]:checked + label {
  background: var(--accent);
  color: white;
  box-shadow: 0 2px 4px rgba(124, 58, 237, 0.3);
}

/* Style range inputs to use the app accent color */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 28px; /* gives room for custom thumb */
  background: transparent; /* we'll style the track with pseudo elements */
}

/* WebKit track */
input[type="range"]::-webkit-slider-runnable-track {
  height: 8px;
  background: linear-gradient(90deg, var(--accent) 0%, var(--accent) var(--_p, 50%), #253047 var(--_p, 50%));
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,0.03);
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  margin-top: -6px; /* center the thumb on the track */
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 2px 6px rgba(0,0,0,0.5);
  border: 3px solid rgba(17,17,27,0.6);
  cursor: pointer;
}

/* Firefox */
input[type="range"]::-moz-range-track {
  height: 8px;
  background: #253047;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,0.03);
}

input[type="range"]::-moz-range-progress {
  height: 8px;
  background: var(--accent);
  border-radius: 999px;
}

input[type="range"]::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent);
  border: 3px solid rgba(17,17,27,0.6);
  box-shadow: 0 2px 6px rgba(0,0,0,0.5);
}

/* IE/Edge (legacy) */
input[type="range"]::-ms-track {
  height: 8px;
  background: transparent;
  border-color: transparent;
  color: transparent;
}
input[type="range"]::-ms-fill-lower {
  background: var(--accent);
  border-radius: 999px;
}
input[type="range"]::-ms-fill-upper {
  background: #253047;
  border-radius: 999px;
}

/* Focus state */
input[type="range"]:focus {
  outline: none;
}
input[type="range"]:focus::-webkit-slider-thumb {
  box-shadow: 0 0 0 6px rgba(124,58,237,0.12), 0 2px 6px rgba(0,0,0,0.5);
}

/* Disabled */
input[type="range"]:disabled::-webkit-slider-thumb,
input[type="range"]:disabled::-moz-range-thumb {
  background: #334155;
  border-color: #111827;
  cursor: not-allowed;
  opacity: 0.7;
}

/* Utility: update the WebKit track fill position using a CSS variable that JS can set if desired. Defaults handled by gradient stop above. */

/* Quota Counter Styles */
.quota-counter {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  background: rgba(17, 24, 39, 0.8);
  border-radius: 8px;
  font-size: 14px;
  position: relative;
  cursor: pointer;
}

.quota-progress {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.progress-ring {
  transform: rotate(-90deg);
}

.progress-ring-circle-bg {
  fill: none;
  stroke: #374151;
  stroke-width: 2;
}

.progress-ring-circle {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-dasharray: 100.53 100.53;
  stroke-dashoffset: 100.53;
  transition: stroke-dashoffset 0.3s ease;
}

.quota-percentage {
  position: absolute;
  font-size: 10px;
  font-weight: 600;
  color: var(--text);
}

.quota-text {
  color: var(--muted);
  font-weight: 500;
}

/* Tooltip Styles */
.tooltip {
  position: absolute;
  bottom: -30px;
  left: 50%;
  transform: translateX(-50%);
  background: #1f2937;
  color: var(--text);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  white-space: nowrap;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s, visibility 0.2s;
  z-index: 1000;
  pointer-events: none;
}

.tooltip::after {
  content: '';
  position: absolute;
  top: -4px;
  left: 50%;
  transform: translateX(-50%);
  border-width: 0 4px 4px 4px;
  border-style: solid;
  border-color: transparent transparent #1f2937 transparent;
}

.quota-counter:hover .tooltip,
.quota-counter:focus .tooltip {
  opacity: 1;
  visibility: visible;
}

/* Single quota class can be styled via .quota-counter or .quota if needed */

/* Image Grid Styles */
.image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  margin-top: 16px;
}

@media(min-width: 768px) {
  .image-grid {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 20px;
  }
}

@media(min-width: 1200px) {
  .image-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  }
}

.image-grid-item {
  position: relative;
  background: #0b1020;
  border: 1px solid #253047;
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.2s ease;
  aspect-ratio: 1;
}

.image-grid-item:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(124, 58, 237, 0.15);
}

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

/* Thumbnail loading overlay */
.image-grid-item.loading-thumb::after {
  content: '';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, rgba(11,16,32,0.5), rgba(11,16,32,0.6));
}

.image-grid-item.loading-thumb::before {
  content: '';
  width: 36px;
  height: 36px;
  border: 3px solid rgba(255,255,255,0.12);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .9s linear infinite;
  z-index: 5;
}

.image-grid-item .overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.5));
  color: white;
  padding: 12px;
  font-size: 12px;
  backdrop-filter: blur(3px);
}

.image-grid-item .checkbox {
  position: absolute;
  top: 8px;
  left: 8px;
  width: 24px;
  height: 24px;
  background: rgba(0, 0, 0, 0.8);
  border: 2px solid rgba(255, 255, 255, 0.8);
  border-radius: 6px;
  display: none;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
  backdrop-filter: blur(4px);
  transition: all 0.2s ease;
}

.image-grid-item .checkbox:hover {
  border-color: var(--accent);
  background: rgba(124, 58, 237, 0.2);
}

.image-grid-item .checkbox.checked {
  background: var(--accent);
  border-color: var(--accent);
  transform: scale(1.1);
}

.image-grid-item .checkbox.checked::after {
  content: '✓';
  color: white;
  font-size: 14px;
  font-weight: bold;
}

.selection-mode .image-grid-item .checkbox {
  display: flex;
}

.selection-mode .image-grid-item.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(124, 58, 237, 0.3);
}

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 60px 20px;
  color: var(--muted);
  background: rgba(11, 16, 32, 0.3);
  border: 2px dashed #334155;
  border-radius: 16px;
  margin: 20px 0;
}

.empty-state::before {
  content: '🎨';
  display: block;
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.6;
}

/* Modal Styles */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.85);
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  backdrop-filter: blur(8px);
  animation: modalFadeIn 0.2s ease-out;
}

@keyframes modalFadeIn {
  from {
    opacity: 0;
    backdrop-filter: blur(0px);
  }
  to {
    opacity: 1;
    backdrop-filter: blur(8px);
  }
}

.modal-content {
  background: var(--panel);
  border: 1px solid #1f2937;
  border-radius: 20px;
  max-width: 90vw;
  max-height: 90vh;
  overflow: auto;
  box-shadow: 0 25px 80px rgba(0, 0, 0, 0.6);
  animation: modalSlideIn 0.3s ease-out;
}

@keyframes modalSlideIn {
  from {
    transform: scale(0.9) translateY(20px);
    opacity: 0;
  }
  to {
    transform: scale(1) translateY(0);
    opacity: 1;
  }
}

/* Closing animation (fade + subtle shrink) */
@keyframes modalFadeOut {
  from {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
  to {
    opacity: 0;
    transform: scale(0.96) translateY(8px);
  }
}

.modal.closing {
  animation: modalFadeOut 0.18s ease-in forwards;
}

.modal.closing .modal-content {
  animation: modalFadeOut 0.18s ease-in forwards;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px 16px;
  border-bottom: 1px solid #1f2937;
}

.modal-header h3 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.modal-header button {
  min-width: 36px;
  height: 36px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 600;
}

.modal-body {
  padding: 24px;
  text-align: center;
}

.modal-body img {
  max-width: 100%;
  max-height: 60vh;
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
}

.modal-metadata {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-top: 24px;
  text-align: left;
}

.metadata-item {
  background: linear-gradient(135deg, #0b1020, #0e1328);
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 14px;
  border: 1px solid #1f2937;
}

.metadata-item strong {
  color: var(--accent);
  display: block;
  margin-bottom: 4px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.modal-actions {
  display: flex;
  gap: 12px;
  padding: 20px 24px;
  border-top: 1px solid #1f2937;
  flex-wrap: wrap;
  justify-content: center;
}

@media(min-width: 768px) {
  .modal-actions {
    justify-content: flex-end;
  }
}

.modal-actions button {
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
  padding: 10px 16px;
  border-radius: 10px;
  font-weight: 500;
  transition: all 0.2s ease;
}

.modal-actions button:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.modal-actions button span {
  font-size: 16px;
}

/* Make the delete button prominent (red background) */
#modalDeleteBtn {
  background: #ef4444;
  color: white;
  border: none;
}

#modalDeleteBtn:hover {
  background: #dc2626;
  transform: translateY(-1px);
}

/* Match the modal delete button styling for the bulk delete button in the gallery */
#deleteSelectedBtn {
  background: #ef4444;
  color: white;
  border: none;
}

#deleteSelectedBtn:hover {
  background: #dc2626;
  transform: translateY(-1px);
}

/* Collapsible header styles */
.collapsible-header {
  user-select: none;
  transition: all 0.2s ease;
  border-radius: 8px;
}

.collapsible-header:hover {
  background: rgba(255, 255, 255, 0.03);
  margin: -8px;
  padding: 8px;
}

.collapsible-header button {
  font-size: 12px;
  padding: 6px 12px;
  border-radius: 8px;
  transition: all 0.2s ease;
  font-weight: 500;
}

.collapsible-header button:hover {
  background: #374151;
  transform: translateY(-1px);
}

/* Animation for log container */
#logContainer {
  transition: all 0.3s ease;
  overflow: hidden;
}

#logContainer.collapsed {
  display: none;
}

/* Enhanced toolbar styling */
.toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.toolbar button {
  font-size: 13px;
  font-weight: 500;
  transition: all 0.2s ease;
}

.toolbar button:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.toolbar button.secondary:hover {
  background: #374151;
}

/* Image grid header toolbar */
.card h2 + .toolbar button {
  padding: 8px 12px;
  font-size: 12px;
}

/* Loading state improvements */
.loading::before {
  content: "";
  display: inline-block;
  width: 14px;
  height: 14px;
  margin-right: 8px;
  border: 2px solid var(--muted);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  vertical-align: -4px;
}

/* Responsive improvements */
@media(max-width: 767px) {
  .modal-content {
    margin: 10px;
    max-width: calc(100vw - 20px);
    max-height: calc(100vh - 20px);
  }
  
  .modal-actions {
    flex-direction: column;
  }
  
  .modal-actions button {
    width: 100%;
    justify-content: center;
  }
  
  .toolbar {
    justify-content: center;
  }
  
  .image-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 12px;
  }
}

/* Ensure modal appears above header */
.modal {
  z-index: 10000;
}
