:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --side: #f0f2f5;
  --line: #e3e6ea;
  --fg: #1f2328;
  --muted: #6b7280;
  --accent: #4f46e5;         /* indigo — shared estate accent */
  --accent-strong: #4338ca;
  --accent-ink: #ffffff;
  --accent-ring: rgba(79,70,229,.20);
  --error: #c0392b;
  --ok: #37b24d;
  --app-h: 100dvh;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  -webkit-font-smoothing: antialiased; overflow: hidden; overscroll-behavior: none;
}
.view { height: var(--app-h); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

/* ---------- Login (shared look with chat) ---------- */
#login { display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 16px; padding: 32px 28px;
  box-shadow: 0 12px 40px rgba(20, 30, 50, .08); text-align: center;
}
.login-card h1 { margin: 0 0 4px; font-size: 1.8rem; }
.login-card .sub { margin: 0 0 24px; color: var(--muted); }
#login-form { display: flex; flex-direction: column; gap: 12px; }
#login-form input {
  padding: 13px 15px; font-size: 1rem; border: 1px solid var(--line);
  background: #fff; color: var(--fg); border-radius: 10px; outline: none;
}
#login-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-ring); }
#login-btn {
  margin-top: 4px; padding: 13px; font-size: 1rem; font-weight: 650;
  border: none; border-radius: 10px; background: var(--accent); color: var(--accent-ink); cursor: pointer;
}
#login-btn:hover { filter: brightness(1.05); }
#login-btn:disabled { opacity: .6; cursor: default; }
.error { color: var(--error); margin: 14px 0 0; min-height: 1.2em; font-size: .92rem; }
.login-card .home-link { display: inline-block; margin-top: 18px; color: var(--muted); text-decoration: none; font-size: .9rem; }
.login-card .home-link:hover { color: var(--accent-strong); }

/* ---------- App shell ---------- */
#app { display: flex; flex-direction: column; }

#head {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px;
  padding: 14px 18px; border-bottom: 1px solid var(--line); background: var(--panel); flex: none;
}
.head-left  { display: flex; align-items: center; gap: 10px; justify-self: start; min-width: 0; }
.head-right { display: flex; align-items: center; gap: 10px; justify-self: end;   min-width: 0; }
#brand { font-weight: 700; font-size: 1.05rem; }
/* Centered "back to the hub" link with a properly-sized, vertically-centered arrow (flex, not a
   baseline-hugging glyph). */
#head .home-link {
  justify-self: center; display: inline-flex; align-items: center; gap: 5px;
  color: var(--muted); text-decoration: none; font-size: .9rem; white-space: nowrap;
}
#head .home-link svg { flex: none; }
#head .home-link:hover { color: var(--accent-strong); }
#engine-dot { width: 9px; height: 9px; border-radius: 50%; background: #cbd2da; flex: none; }
#engine-dot.up { background: var(--ok); }
#engine-dot.down { background: var(--error); }
#me-name { font-size: .9rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#logout { border: none; background: none; color: var(--muted); cursor: pointer; font-size: .85rem; padding: 4px; flex: none; }
#logout:hover { color: var(--error); }

/* ---------- Gallery ---------- */
#gallery { flex: 1; overflow-y: auto; padding: 20px 18px 28px; }
#gallery .empty {
  margin: 40px auto; text-align: center; color: var(--muted); max-width: 44ch;
  line-height: 1.5; text-wrap: balance;
}
#grid {
  display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  max-width: 1100px; margin: 0 auto;
}
.tile {
  position: relative; aspect-ratio: 1 / 1; border-radius: 12px; overflow: hidden;
  background: var(--side); border: 1px solid var(--line); cursor: pointer;
}
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile.pending, .tile.error { cursor: default; }
.tile .tile-cap {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 18px 10px 8px;
  font-size: .78rem; color: #fff; line-height: 1.3;
  background: linear-gradient(transparent, rgba(0,0,0,.62));
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.tile.pending .center, .tile.error .center {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 12px; padding: 14px; text-align: center;
  font-size: .82rem; color: var(--muted);
}
.tile.error .center { color: var(--error); }
.spinner {
  width: 30px; height: 30px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Composer ---------- */
#composer {
  border-top: 1px solid var(--line); background: var(--panel); flex: none;
  padding: 12px 16px; display: flex; flex-direction: column; gap: 10px;
}
#composer #prompt {
  resize: none; max-height: 140px; padding: 12px 14px; font: inherit; width: 100%;
  border: 1px solid var(--line); border-radius: 12px; outline: none; background: #fff; color: var(--fg);
  overflow-y: hidden;
}
#composer #prompt:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-ring); }
.controls { display: flex; gap: 10px; align-items: center; }
#size {
  padding: 10px 12px; font: inherit; border: 1px solid var(--line); border-radius: 10px;
  background: #fff; color: var(--fg); cursor: pointer;
}
.ghost {
  padding: 10px 14px; font: inherit; border: 1px solid var(--line); border-radius: 10px;
  background: #fff; color: var(--muted); cursor: pointer;
}
.ghost:hover, .ghost.on { border-color: var(--accent); color: var(--accent-strong); }
#generate {
  margin-left: auto; padding: 11px 22px; font-weight: 650; border: none; border-radius: 12px;
  background: var(--accent); color: var(--accent-ink); cursor: pointer; min-height: 44px;
}
#generate:hover { filter: brightness(1.05); }
#generate:disabled { opacity: .55; cursor: default; }
#advanced { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
#advanced #negative {
  flex: 1; min-width: 200px; padding: 10px 12px; font: inherit; border: 1px solid var(--line);
  border-radius: 10px; outline: none; background: #fff; color: var(--fg);
}
#advanced #negative:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-ring); }
.num { display: inline-flex; align-items: center; gap: 6px; font-size: .88rem; color: var(--muted); }
.num input {
  width: 82px; padding: 8px 10px; font: inherit; border: 1px solid var(--line);
  border-radius: 8px; outline: none; background: #fff; color: var(--fg);
}
.num input:focus { border-color: var(--accent); }

/* ---------- Lightbox ---------- */
#lightbox {
  position: fixed; inset: 0; z-index: 40; background: rgba(15, 18, 26, .82);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.lb-inner {
  background: var(--panel); border-radius: 14px; overflow: hidden; max-width: 900px;
  max-height: 92vh; display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.4);
}
#lb-img { max-width: 100%; max-height: 66vh; object-fit: contain; background: #0b0d12; }
.lb-meta { padding: 14px 18px 18px; overflow-y: auto; }
#lb-prompt { margin: 0 0 6px; line-height: 1.45; }
#lb-params { margin: 0 0 12px; font-size: .85rem; }
.lb-actions { display: flex; gap: 10px; align-items: center; }
.lb-actions a, .lb-actions button {
  font: inherit; padding: 9px 16px; border-radius: 10px; cursor: pointer; text-decoration: none;
}
#lb-download { background: var(--accent); color: var(--accent-ink); border: none; font-weight: 600; }
#lb-download:hover { filter: brightness(1.05); }
#lb-reuse { background: #fff; color: var(--fg); border: 1px solid var(--line); }
#lb-reuse:hover { border-color: var(--accent); color: var(--accent-strong); }
#lb-close {
  position: fixed; top: 16px; right: 18px; z-index: 41; color: #fff;
  background: rgba(0,0,0,.35); border-radius: 50%; width: 40px; height: 40px;
}
.icon-btn { border: none; font-size: 1.5rem; line-height: 1; color: var(--fg); cursor: pointer; padding: 0; }

/* Delete is a bare trash icon at the right edge of the lightbox actions — no box, sized to the
   height of the Download/Reuse buttons. */
#lb-delete {
  margin-left: auto; padding: 0; border: none; background: none; border-radius: 0;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--error); cursor: pointer;
}
#lb-delete svg { width: 30px; height: 30px; }
#lb-delete:hover { color: #9b1c1c; }

/* Delete confirmation popup (sits above the lightbox). */
#confirm-pop {
  position: fixed; inset: 0; z-index: 60; background: rgba(15, 18, 26, .55);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.confirm-card {
  background: var(--panel); border-radius: 14px; padding: 22px 22px 18px; width: 100%;
  max-width: 340px; box-shadow: 0 20px 60px rgba(0, 0, 0, .4); text-align: center;
}
.confirm-card p { margin: 0 0 18px; line-height: 1.45; }
.confirm-row { display: flex; gap: 10px; justify-content: center; }
.confirm-row button { font: inherit; padding: 10px 18px; border-radius: 10px; cursor: pointer; }
#confirm-cancel { background: #fff; color: var(--fg); border: 1px solid var(--line); }
#confirm-cancel:hover { border-color: var(--accent); color: var(--accent-strong); }
.danger.solid { background: var(--error); color: #fff; border: none; font-weight: 600; }
.danger.solid:hover { filter: brightness(1.05); }

@media (max-width: 620px) {
  #grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
  #me-name { max-width: 90px; }
  #generate { padding: 11px 16px; }
}
