/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #0f0f13;
  --bg2: #16161d;
  --bg3: #1e1e2a;
  --bg4: #28283a;
  --border: #2a2a3e;
  --text: #e8e8f0;
  --text2: #9090a8;
  --text3: #606078;
  --accent: #6366f1;
  --accent2: #8b5cf6;
  --accent-glow: rgba(99, 102, 241, 0.25);
  --red: #ef4444;
  --green: #22c55e;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0,0,0,0.3);
}
html { font-size: 15px; }
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  overflow-x: hidden;
  min-height: 100vh;
}

/* ===== Layout ===== */
#app {
  display: flex;
  min-height: 100vh;
}

/* ===== Sidebar ===== */
#sidebar {
  width: 220px;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  height: 100vh;
}
.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 32px;
  padding: 0 4px;
}
.logo-text {
  font-size: 1.15rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}
.nav-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text2);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  font-family: inherit;
  text-align: left;
  width: 100%;
}
.nav-btn:hover { background: var(--bg3); color: var(--text); }
.nav-btn.active {
  background: var(--accent-glow);
  color: var(--accent);
  font-weight: 600;
}
.sidebar-footer {
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
.version {
  font-size: 0.75rem;
  color: var(--text3);
}

/* ===== Main ===== */
#main {
  flex: 1;
  padding: 32px 40px;
  max-width: 960px;
  overflow-y: auto;
}
.page-header { margin-bottom: 24px; }
.page-header h1 {
  font-size: 1.6rem;
  font-weight: 700;
  margin-bottom: 4px;
}
.subtitle {
  color: var(--text2);
  font-size: 0.9rem;
}

/* ===== Tabs ===== */
.tab-content { display: none; }
.tab-content.active { display: block; }

/* ===== Card ===== */
.card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 24px;
}

/* ===== Inputs ===== */
.input-group { margin-bottom: 18px; }
.input-group label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
}
textarea, input[type="text"], input[type="password"], select {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  transition: border-color 0.2s;
  outline: none;
}
textarea:focus, input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
textarea {
  resize: vertical;
  min-height: 100px;
  line-height: 1.5;
}
select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%239090a8' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}
.input-row {
  display: flex;
  gap: 16px;
}
.input-row .half { flex: 1; }
.password-wrapper {
  display: flex;
  position: relative;
}
.password-wrapper input { padding-right: 44px; }
.toggle-pw {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text3);
  cursor: pointer;
  padding: 4px;
  display: flex;
}
.toggle-pw:hover { color: var(--text); }
.field-hint {
  display: block;
  font-size: 0.78rem;
  color: var(--text3);
  margin-top: 4px;
}

/* ===== Button ===== */
.btn-primary {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 24px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.25s;
}
.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 8px 24px var(--accent-glow);
}
.btn-primary:active { transform: translateY(0); }
.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* ===== Upload Zone ===== */
.upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius-sm);
  padding: 32px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
  background: var(--bg3);
}
.upload-zone:hover {
  border-color: var(--accent);
  background: var(--accent-glow);
}
.upload-zone svg { color: var(--text3); margin-bottom: 8px; }
.upload-zone p { color: var(--text2); font-size: 0.9rem; }
.upload-zone p span { color: var(--accent); font-weight: 600; }
.upload-hint {
  font-size: 0.78rem;
  color: var(--text3);
  margin-top: 4px;
  display: block;
}

/* ===== Thumbnails ===== */
.thumbnails {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 12px;
}
.thumb-item {
  position: relative;
  width: 100px;
  height: 100px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 2px solid var(--border);
  background: var(--bg3);
}
.thumb-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.thumb-item .remove-btn {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: rgba(0,0,0,0.7);
  border: none;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  line-height: 1;
  opacity: 0;
  transition: opacity 0.2s;
}
.thumb-item:hover .remove-btn { opacity: 1; }

/* ===== Results ===== */
.results {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 16px;
  margin-top: 8px;
}
.result-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  transition: transform 0.2s;
}
.result-card:hover { transform: translateY(-2px); }
.result-card .img-wrapper {
  position: relative;
  background: var(--bg3);
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 300px;
}
.result-card .img-wrapper img {
  width: 100%;
  display: block;
}
.result-card .actions {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}
.btn-download {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.2s;
  text-decoration: none;
}
.btn-download:hover {
  background: var(--bg4);
  border-color: var(--accent);
  color: var(--accent);
}

/* ===== Settings ===== */
.settings-card { max-width: 520px; }
.settings-status {
  margin-top: 12px;
  font-size: 0.85rem;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  display: none;
}
.settings-status.success {
  display: block;
  background: rgba(34, 197, 94, 0.1);
  color: var(--green);
  border: 1px solid rgba(34, 197, 94, 0.2);
}
.settings-status.error {
  display: block;
  background: rgba(239, 68, 68, 0.1);
  color: var(--red);
  border: 1px solid rgba(239, 68, 68, 0.2);
}

/* ===== Floating Loading Widget ===== */
.loading-widget {
  position: fixed;
  bottom: 24px;
  right: 24px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 999;
  min-width: 200px;
  animation: widgetIn 0.3s ease;
}
@keyframes widgetIn {
  from { opacity: 0; transform: translateY(16px) scale(0.95); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
.loading-widget-spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-widget-text { flex: 1; }
.loading-widget-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
}
.loading-widget-sub {
  font-size: 0.72rem;
  color: var(--text3);
}
.loading-widget-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--bg3);
  border-radius: 0 0 var(--radius) var(--radius);
  overflow: hidden;
}
.loading-widget-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  animation: progress 2s ease-in-out infinite;
}
@keyframes progress {
  0% { width: 0%; }
  50% { width: 70%; }
  100% { width: 90%; }
}

/* ===== Lightbox ===== */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  cursor: zoom-out;
  animation: lbFadeIn 0.2s ease;
}
@keyframes lbFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
.lightbox-content {
  max-width: 92vw;
  max-height: 90vh;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: default;
}
.lightbox-content img {
  max-width: 100%;
  max-height: 90vh;
  object-fit: contain;
  border-radius: 6px;
  box-shadow: 0 8px 48px rgba(0,0,0,0.6);
}
.lightbox-close {
  position: fixed;
  top: 20px;
  right: 24px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.15);
  color: #fff;
  font-size: 26px;
  font-weight: 300;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
  z-index: 2001;
  line-height: 1;
}
.lightbox-close:hover { background: rgba(255,255,255,0.18); }
.lightbox-download {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 24px;
  background: var(--accent);
  color: white;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.2s;
  z-index: 2001;
  font-family: inherit;
}
.lightbox-download:hover {
  background: var(--accent2);
  transform: translateX(-50%) translateY(-1px);
}
@media (max-width: 768px) {
  .lightbox-content { max-width: 96vw; }
  .lightbox-close { top: 12px; right: 12px; width: 36px; height: 36px; font-size: 22px; }
  .lightbox-download { bottom: 20px; padding: 8px 18px; }
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: 80px;
  right: 24px;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 500;
  z-index: 1000;
  animation: slideUp 0.3s ease;
  max-width: 400px;
}
.toast.error {
  background: rgba(239, 68, 68, 0.15);
  color: #fca5a5;
  border: 1px solid rgba(239, 68, 68, 0.3);
}
.toast.success {
  background: rgba(34, 197, 94, 0.15);
  color: #86efac;
  border: 1px solid rgba(34, 197, 94, 0.3);
}
@keyframes slideUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== Notification Badge ===== */
.api-notice {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: rgba(99, 102, 241, 0.08);
  border: 1px solid rgba(99, 102, 241, 0.2);
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  color: var(--text2);
  margin-bottom: 18px;
}
.api-notice .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text3);
  flex-shrink: 0;
}
.api-notice .dot.ready { background: var(--green); }
.api-notice .dot.missing { background: var(--red); }

/* ===== Responsive ===== */
@media (max-width: 768px) {
  #app { flex-direction: column; }
  #sidebar {
    width: 100%;
    height: auto;
    position: static;
    flex-direction: row;
    padding: 10px 12px;
    border-right: none;
    border-bottom: 1px solid var(--border);
    align-items: center;
    gap: 8px;
  }
  .logo { margin-bottom: 0; margin-right: 4px; flex-shrink: 0; }
  .logo svg { width: 28px; height: 28px; }
  .logo-text { font-size: 0.95rem; }
  nav {
    flex-direction: row;
    gap: 6px;
    flex: 1;
    display: flex;
  }
  .nav-btn {
    padding: 10px 8px;
    font-size: 0.8rem;
    justify-content: center;
    border-radius: var(--radius-sm);
  }
  /* Main buttons: show icon + text side by side */
  .nav-btn[data-tab="txt2img"],
  .nav-btn[data-tab="img2img"] {
    flex: 1;
    gap: 6px;
    padding: 10px 8px;
    min-width: 0;
  }
  .nav-btn[data-tab="txt2img"] span,
  .nav-btn[data-tab="img2img"] span {
    display: inline;
    font-size: 0.78rem;
    white-space: nowrap;
  }
  /* Settings + History: compact, icon only */
  .nav-btn[data-tab="settings"],
  .nav-btn[data-tab="history"] {
    width: 42px;
    flex: 0 0 42px;
    padding: 10px;
  }
  .nav-btn[data-tab="settings"] span,
  .nav-btn[data-tab="history"] span {
    display: none;
  }
  .nav-btn svg { width: 18px; height: 18px; flex-shrink: 0; }
  .sidebar-footer { display: none; }
  #main {
    padding: 20px 16px;
  }
  .results {
    grid-template-columns: 1fr;
  }
  .input-row {
    flex-direction: column;
    gap: 0;
  }
  .thumb-item {
    width: 80px;
    height: 80px;
  }
  .page-header h1 { font-size: 1.3rem; }
  .loading-widget {
    bottom: 12px;
    right: 12px;
    left: 12px;
    min-width: unset;
  }
}
@media (max-width: 480px) {
  #main { padding: 16px 12px; }
  .card { padding: 16px; }
  .result-card .img-wrapper { min-height: 200px; }
}

/* ===== History ===== */
.history-loading {
  text-align: center;
  padding: 48px 0;
  color: var(--text3);
  font-size: 0.9rem;
}
.history-empty {
  text-align: center;
  padding: 60px 20px;
  color: var(--text3);
}
.history-empty svg { margin-bottom: 12px; }
.history-empty p {
  font-size: 1rem;
  color: var(--text2);
  margin-bottom: 4px;
}
.history-empty span { font-size: 0.85rem; }
.history-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding: 0 4px;
}
.history-count {
  font-size: 0.82rem;
  color: var(--text3);
}
.btn-clear {
  padding: 6px 14px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text2);
  font-size: 0.8rem;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.2s;
}
.btn-clear:hover {
  border-color: var(--red);
  color: var(--red);
  background: rgba(239, 68, 68, 0.08);
}
.history-item {
  display: flex;
  gap: 12px;
  padding: 12px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
  transition: border-color 0.2s;
  align-items: flex-start;
}
.history-item:hover { border-color: var(--accent); }
.history-img {
  width: 72px;
  height: 72px;
  border-radius: 6px;
  overflow: hidden;
  flex-shrink: 0;
  background: var(--bg3);
  cursor: zoom-in;
  position: relative;
}
.history-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.history-count-badge {
  position: absolute;
  top: 3px;
  right: 3px;
  background: rgba(0,0,0,0.65);
  color: #fff;
  font-size: 0.65rem;
  padding: 1px 5px;
  border-radius: 4px;
}
.history-info {
  flex: 1;
  min-width: 0;
}
.history-meta {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 4px;
  flex-wrap: wrap;
}
.history-mode {
  font-size: 0.7rem;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--accent-glow);
  color: var(--accent);
  font-weight: 600;
}
.history-ratio {
  font-size: 0.72rem;
  color: var(--text3);
}
.history-time {
  font-size: 0.72rem;
  color: var(--text3);
  margin-left: auto;
}
.history-prompt {
  font-size: 0.82rem;
  color: var(--text2);
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.history-del {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--text3);
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.2s;
  margin-top: 2px;
}
.history-del:hover {
  background: rgba(239, 68, 68, 0.12);
  color: var(--red);
}
@media (max-width: 768px) {
  .history-img { width: 60px; height: 60px; }
  .history-prompt { -webkit-line-clamp: 1; }
}
