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

body {
  font-family: 'Segoe UI', sans-serif;
  background: #fdf6ff;
  color: #333;
  min-height: 100vh;
}

header {
  position: relative;
  text-align: center;
  padding: 1.5rem;
  background: linear-gradient(135deg, #d4a8f0, #f9c6e0);
  color: white;
}

header h1 { font-size: 2rem; }
header p  { margin-top: 0.25rem; font-size: 1rem; opacity: 0.9; }

.header-settings-btn {
  position: absolute;
  top: 50%;
  right: 1.25rem;
  transform: translateY(-50%);
  color: white;
  opacity: 0.8;
  text-decoration: none;
  display: flex;
  align-items: center;
  transition: opacity 0.15s;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
}

.header-settings-btn:hover { opacity: 1; }
.header-settings-btn svg { width: 1.4rem; height: 1.4rem; }

/* =====================
   Main 3-column layout
   ===================== */
main {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1.25rem;
  padding: 1.25rem;
  align-items: start;
}

@media (min-width: 900px) {
  main {
    grid-template-columns: 260px 1fr 1fr;
  }
}

/* =====================
   Reference panel
   ===================== */
.reference-panel {
  background: white;
  border-radius: 14px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.08);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.reference-panel h2 {
  font-size: 1rem;
  font-weight: 600;
  color: #a06cd5;
}

.search-bar {
  display: flex;
  gap: 0.4rem;
}

.search-bar input {
  flex: 1;
  min-width: 0;
  padding: 0.45rem 0.7rem;
  border: 2px solid #e8d5f9;
  border-radius: 8px;
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.15s;
}

.search-bar input:focus {
  border-color: #d4a8f0;
}

.search-bar button {
  flex-shrink: 0;
  padding: 0.45rem 0.65rem;
  background: #d4a8f0;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.85rem;
  white-space: nowrap;
  transition: opacity 0.15s;
}

.search-bar button:hover { opacity: 0.85; }
.search-bar button:disabled { opacity: 0.5; cursor: not-allowed; }

#reference-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
  max-height: 480px;
  overflow-y: auto;
}

#reference-grid .placeholder-text {
  grid-column: 1 / -1;
  text-align: center;
  padding: 2rem 0;
  color: #bbb;
  font-size: 0.9rem;
}

#reference-grid img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 8px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.15s, transform 0.15s;
}

#reference-grid img:hover {
  border-color: #d4a8f0;
  transform: scale(1.03);
}

/* =====================
   Toolbar + canvas
   ===================== */
.canvas-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  align-items: center;
  padding: 0.4rem 0.5rem;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  width: fit-content;
}

.tool {
  width: 1.9rem;
  height: 1.9rem;
  padding: 0;
  border: 2px solid #d4a8f0;
  border-radius: 7px;
  background: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

.tool svg {
  width: 1rem;
  height: 1rem;
  stroke-width: 2;
}

.tool:hover, .tool.active {
  background: #d4a8f0;
  color: white;
}

.tool:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.tool.danger { border-color: #f9a8c9; }
.tool.danger:hover { background: #f9a8c9; color: white; }

#drawing-canvas {
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  background: white;
  touch-action: none;
  max-width: 100%;
}

/* =====================
   Result panel
   ===================== */
.result-area {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.description-box label {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  color: #a06cd5;
  margin-bottom: 0.35rem;
}

.description-box textarea {
  width: 100%;
  min-height: 90px;
  padding: 0.6rem 0.75rem;
  border: 2px solid #e8d5f9;
  border-radius: 10px;
  font-size: 0.9rem;
  font-family: inherit;
  resize: vertical;
  outline: none;
  transition: border-color 0.15s;
  color: #333;
}

.description-box textarea:focus {
  border-color: #d4a8f0;
}

.generate-btn {
  width: 100%;
  padding: 0.9rem;
  font-size: 1.1rem;
  background: linear-gradient(135deg, #d4a8f0, #f9c6e0);
  color: white;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(212,168,240,0.4);
  transition: opacity 0.2s, transform 0.1s;
}

.generate-btn:hover  { opacity: 0.9; transform: translateY(-1px); }
.generate-btn:disabled { opacity: 0.6; cursor: not-allowed; transform: none; }

#result {
  min-height: 200px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.08);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: 1rem;
}

.save-image-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.5rem 1.1rem;
  background: white;
  color: #a06cd5;
  border: 2px solid #d4a8f0;
  border-radius: 9px;
  font-size: 0.9rem;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s, color 0.15s;
}

.save-image-btn:hover {
  background: #d4a8f0;
  color: white;
}

.save-image-btn svg {
  width: 1rem;
  height: 1rem;
  stroke-width: 2;
}

.placeholder-text { color: #bbb; font-size: 0.95rem; text-align: center; }

#result-image {
  width: 100%;
  border-radius: 8px;
}

/* =====================
   Settings page
   ===================== */
.settings-main {
  display: flex;
  justify-content: center;
  padding: 2rem 1.25rem;
}

.settings-card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 2px 16px rgba(0,0,0,0.08);
  padding: 2rem;
  width: 100%;
  max-width: 520px;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.settings-card h2 {
  font-size: 1.3rem;
  color: #333;
}

.settings-section h3 {
  font-size: 0.95rem;
  font-weight: 600;
  color: #a06cd5;
  margin-bottom: 0.3rem;
}

.settings-description {
  font-size: 0.88rem;
  color: #888;
  margin-bottom: 0.85rem;
}

.provider-option {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  padding: 0.85rem;
  border: 2px solid #e8d5f9;
  border-radius: 10px;
  cursor: pointer;
  margin-bottom: 0.6rem;
  transition: border-color 0.15s;
}

.provider-option:has(input:checked) {
  border-color: #d4a8f0;
  background: #fdf6ff;
}

.provider-option input[type="radio"] {
  margin-top: 0.2rem;
  accent-color: #d4a8f0;
  flex-shrink: 0;
}

.provider-info {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  flex: 1;
}

.provider-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.provider-info strong { font-size: 0.95rem; }

.provider-info p {
  font-size: 0.83rem;
  color: #888;
  margin: 0;
}

.provider-info code {
  font-size: 0.8rem;
  background: #f3eaff;
  padding: 0.1rem 0.3rem;
  border-radius: 4px;
}

.badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 0.1rem 0.45rem;
  border-radius: 99px;
  width: fit-content;
}

.badge.free  { background: #d4f5e9; color: #1a7a4a; }
.badge.paid  { background: #fde8d8; color: #a04a10; }

.hf-model-row {
  display: none;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.hf-model-row label {
  font-size: 0.82rem;
  color: #666;
  white-space: nowrap;
}

.hf-model-row input {
  flex: 1;
  padding: 0.35rem 0.6rem;
  border: 2px solid #e8d5f9;
  border-radius: 7px;
  font-size: 0.82rem;
  font-family: monospace;
  outline: none;
}

.hf-model-row input:focus { border-color: #d4a8f0; }

.cf-model-row {
  display: none;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.cf-model-row label {
  font-size: 0.82rem;
  color: #666;
  white-space: nowrap;
}

.cf-model-row select {
  flex: 1;
  padding: 0.35rem 0.6rem;
  border: 2px solid #e8d5f9;
  border-radius: 7px;
  font-size: 0.82rem;
  font-family: monospace;
  outline: none;
  background: white;
}

.cf-model-row select:focus { border-color: #d4a8f0; }

.settings-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.save-btn {
  padding: 0.65rem 1.5rem;
  background: linear-gradient(135deg, #d4a8f0, #f9c6e0);
  color: white;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.95rem;
  transition: opacity 0.15s;
}

.save-btn:hover { opacity: 0.88; }

.save-confirm {
  font-size: 0.88rem;
  color: #1a7a4a;
  font-weight: 600;
}

.save-confirm.hidden { display: none; }

/* =====================
   Settings modal
   ===================== */
.settings-modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 1rem;
}

.settings-modal.hidden { display: none; }

.settings-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.5);
  cursor: pointer;
}

.settings-modal-card {
  position: relative;
  max-height: 85vh;
  overflow-y: auto;
  width: 100%;
  max-width: 520px;
  padding: 1.25rem;
  gap: 1rem;
}

.settings-modal-card .provider-option {
  padding: 0.65rem;
  margin-bottom: 0.4rem;
}

.settings-modal-card .settings-description {
  margin-bottom: 0.6rem;
}

.settings-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.settings-close-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: #aaa;
  display: flex;
  align-items: center;
  padding: 0.25rem;
  border-radius: 6px;
  transition: color 0.15s;
}

.settings-close-btn:hover { color: #333; }
.settings-close-btn svg { width: 1.2rem; height: 1.2rem; }

/* =====================
   Lightbox
   ===================== */
.lightbox {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.lightbox.hidden { display: none; }

.lightbox-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.65);
  cursor: pointer;
}

.lightbox img {
  position: relative;
  max-width: 90vw;
  max-height: 85vh;
  border-radius: 12px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.4);
}

/* =====================
   BYOP Account section
   ===================== */
.account-status {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.85rem;
  border: 2px solid #e8d5f9;
  border-radius: 10px;
  transition: border-color 0.15s;
}

.account-status.logged-in {
  border-color: #a8e6c0;
  background: #f0fff5;
}

.account-avatar {
  width: 2.2rem;
  height: 2.2rem;
  border-radius: 50%;
  background: #e8d5f9;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  overflow: hidden;
}

.account-avatar svg {
  width: 1.2rem;
  height: 1.2rem;
  color: #a06cd5;
}

.account-avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.account-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.account-name {
  font-weight: 600;
  font-size: 0.9rem;
  color: #333;
}

.account-detail {
  font-size: 0.78rem;
  color: #888;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.login-btn {
  padding: 0.45rem 1rem;
  background: linear-gradient(135deg, #d4a8f0, #f9c6e0);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 600;
  white-space: nowrap;
  transition: opacity 0.15s;
}

.login-btn:hover { opacity: 0.85; }

.logout-btn {
  padding: 0.45rem 1rem;
  background: white;
  color: #e06080;
  border: 2px solid #f9a8c9;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}

.logout-btn:hover {
  background: #f9a8c9;
  color: white;
}

.logout-btn.hidden,
.login-btn.hidden { display: none; }

/* =====================
   Model select row
   ===================== */
.model-select-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.model-select-row select {
  flex: 1;
  padding: 0.45rem 0.6rem;
  border: 2px solid #e8d5f9;
  border-radius: 8px;
  font-size: 0.88rem;
  font-family: inherit;
  outline: none;
  background: white;
  color: #333;
  cursor: pointer;
}

.model-select-row select:focus {
  border-color: #d4a8f0;
}

/* =====================
   Canvas login overlay
   ===================== */
.canvas-wrapper {
  position: relative;
  display: inline-block;
}

.canvas-login-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  backdrop-filter: blur(3px);
  transition: opacity 0.3s;
}

.canvas-login-overlay.hidden {
  display: none;
}

.canvas-login-card {
  background: white;
  border-radius: 16px;
  padding: 1.5rem 2rem;
  text-align: center;
  max-width: 340px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.25);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.canvas-login-card svg {
  width: 2rem;
  height: 2rem;
  color: #d4a8f0;
  stroke-width: 1.5;
}

.canvas-login-card p {
  font-size: 1rem;
  font-weight: 700;
  color: #333;
  margin: 0;
}

.canvas-login-sub {
  font-size: 0.8rem;
  color: #888;
  line-height: 1.3;
}

.canvas-login-btn {
  margin-top: 0.5rem;
  padding: 0.5rem 1.5rem;
  background: linear-gradient(135deg, #d4a8f0, #f9c6e0);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  transition: opacity 0.15s;
}

.canvas-login-btn:hover { opacity: 0.85; }
