:root {
  --bg: #ffffff;
  --fg: #1a1a1a;
  --muted: #6b6b6b;
  --border: #dcdcdc;
  --accent: #2f7a4f;
  --accent-fg: #ffffff;
  --card-bg: #fafafa;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  font-size: 16px;
  line-height: 1.4;
}

.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 10;
}

.topbar > .brand { justify-self: start; }
.topbar > .usage-badge { justify-self: center; }
.topbar > nav { justify-self: end; }

.brand { font-weight: 600; }

.usage-badge {
  font-size: 0.75rem;
  color: var(--muted);
  white-space: nowrap;
  text-align: center;
}

.link-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 0.9rem;
  padding: 0.5rem;
}

.container {
  max-width: 640px;
  margin: 0 auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}

.card h2 {
  margin-top: 0;
  font-size: 1.05rem;
}

form { display: flex; flex-direction: column; gap: 0.5rem; }

label { font-size: 0.85rem; color: var(--muted); margin-top: 0.5rem; }

input[type="file"],
select,
textarea {
  width: 100%;
  padding: 0.65rem;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
}

.orientation-toggle {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.25rem;
  flex: 1;
  min-width: 0;
}

.orientation-toggle legend { font-size: 0.85rem; color: var(--muted); padding: 0 0.25rem; }

/* Label sits beside its dropdown instead of stacked above it - used for
   Category, Prompt, and Print size. */
.inline-field {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-top: 0.5rem;
}

.inline-field label {
  margin-top: 0;
  white-space: nowrap;
  flex-shrink: 0;
}

.inline-field select {
  flex: 1;
  min-width: 0;
}

/* Print size (left half, via .inline-field) and orientation (right half)
   share one row. */
.form-row-split {
  display: flex;
  gap: 1rem;
  align-items: flex-start;
  margin-top: 0.5rem;
}

.form-row-split > .inline-field {
  flex: 1;
  min-width: 0;
  margin-top: 0;
}

.prompt-textarea {
  min-height: 220px;
  resize: vertical;
}

.checkbox-row {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  font-size: 0.9rem;
  color: var(--fg);
  margin-top: 0.75rem;
}

.checkbox-row input[type="checkbox"] {
  width: auto;
  margin-top: 0.2rem;
}

.primary-btn {
  margin-top: 0.75rem;
  padding: 0.85rem;
  font-size: 1rem;
  font-weight: 600;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-fg);
}

.primary-btn:active { opacity: 0.85; }

/* Standalone secondary/danger buttons - usable anywhere in the app, not
   just inside dialogs (the .dialog-actions rule further down only ever
   applied inside a <dialog>, which is why buttons using these classes
   elsewhere, like the Library tab's Select button, previously fell back to
   completely unstyled browser defaults). */
.secondary-btn,
.danger-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.6rem 1.1rem;
  font-size: 0.9rem;
  font-weight: 600;
  border-radius: var(--radius);
  cursor: pointer;
  text-decoration: none;
}

.secondary-btn {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
}

.danger-btn {
  border: 1px solid #c0392b;
  background: #c0392b;
  color: #fff;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.9rem;
}

.user-row {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem;
  font-size: 0.9rem;
}

.user-row-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.user-row-controls {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.user-row-controls select {
  width: auto;
  padding: 0.35rem 0.5rem;
  font-size: 0.85rem;
}

.user-limits-edit {
  margin-top: 0.6rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--border);
}

.limit-field {
  display: flex;
  flex-direction: column;
}

.limit-field label {
  margin-top: 0;
}

.limit-field + .limit-field {
  margin-top: 0.75rem;
}

.thumb {
  width: 52px;
  height: 52px;
  border-radius: 6px;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--border);
}

.history-meta {
  color: var(--muted);
  font-size: 0.8rem;
}

.history-group {
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 0.5rem;
  overflow: hidden;
}

.history-group summary {
  padding: 0.6rem 0.75rem;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--card-bg);
  list-style: none;
}

.history-group summary::-webkit-details-marker { display: none; }

.history-group summary::before {
  content: '▸';
  display: inline-block;
  margin-right: 0.4rem;
  transition: transform 0.15s ease;
}

.history-group[open] summary::before {
  transform: rotate(90deg);
}

.history-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 0.5rem;
  padding: 0.6rem;
}

.grid-thumb-wrap {
  position: relative;
  border: none;
  background: none;
  padding: 0;
  cursor: pointer;
  border-radius: 6px;
  overflow: hidden;
  line-height: 0;
}

.grid-thumb {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  display: block;
  background: var(--border);
  border-radius: 6px;
}

.grid-download-btn {
  position: absolute;
  bottom: 4px;
  left: 4px;
  width: 1.8rem;
  height: 1.8rem;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
  text-decoration: none;
}

.grid-pin-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 1.4rem;
  height: 1.4rem;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  line-height: 1;
}

#error-detail-modal {
  width: 90vw;
  max-width: 480px;
  max-height: 80vh;
  overflow-y: auto;
}

#ai-warning-modal {
  width: 90vw;
  max-width: 480px;
  max-height: 85vh;
  overflow-y: auto;
}

#ai-warning-modal p {
  font-size: 0.9rem;
  line-height: 1.5;
}

#error-detail-text {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.8rem;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem;
}

#describe-modal {
  width: 90vw;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
}

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

.describe-thumb-small {
  width: 56px;
  height: 56px;
  border-radius: 6px;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--border);
}

.describe-header-meta {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.85rem 0 0.5rem;
}

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin: 0 0 0.3rem;
}

.icon-btn {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 1rem;
  padding: 0.2rem;
  color: var(--muted);
}

#describe-prompt-text,
#describe-response-text {
  white-space: pre-wrap;
}

dialog {
  border: none;
  border-radius: var(--radius);
  padding: 1.25rem;
  max-width: 90vw;
  width: 360px;
}

dialog::backdrop { background: rgba(0, 0, 0, 0.4); }

#prompt-fullscreen-modal {
  width: 100vw;
  height: 100vh;
  max-width: 100vw;
  max-height: 100vh;
  margin: 0;
  padding: 1rem;
  flex-direction: column;
}

/* display must only apply while actually open - a <dialog> is hidden by
   default via the browser's own stylesheet (no [open] attribute), and an
   unconditional "display: flex" on the bare selector overrides that at
   equal-or-higher specificity, making it visible all the time regardless
   of whether showModal() was ever called. */
#prompt-fullscreen-modal[open] {
  display: flex;
}

.fullscreen-close-btn {
  align-self: flex-end;
  flex-shrink: 0;
  width: 2.2rem;
  height: 2.2rem;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  font-size: 1.1rem;
  cursor: pointer;
  margin-bottom: 0.75rem;
}

.fullscreen-textarea {
  flex: 1;
  width: 100%;
  font-size: 1.1rem;
  line-height: 1.5;
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
  resize: none;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1rem;
  flex-wrap: wrap;
}

/* Normalize every button/link inside a dialog's action row to the same
   shape - plain <button>Cancel</button> elements otherwise render with the
   browser's own default sizing/corners, which looks inconsistent next to
   .primary-btn. This has higher specificity than .primary-btn alone (an
   extra element selector), so it overrides that class's own padding/margin
   for buttons here while leaving its background/color (and the standalone
   full-width Generate/Add/Save buttons elsewhere on the site) untouched. */
.dialog-actions button,
.dialog-actions a {
  margin-top: 0;
  padding: 0.6rem 1.1rem;
  font-size: 0.9rem;
  font-weight: 600;
  border-radius: var(--radius);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.dialog-actions button:not(.primary-btn):not(.danger-btn),
.dialog-actions a:not(.primary-btn) {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
}

.dialog-actions button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.centered-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  text-align: center;
  padding: 2rem;
}

.tab-row {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 0.5rem;
}

.tab-btn {
  flex: 1;
  padding: 0.55rem;
  font-size: 0.85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--muted);
}

.tab-btn.active {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
  font-weight: 600;
}

.notice-banner {
  background: #fff4e0;
  border: 1px solid #e8c88a;
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  font-size: 0.85rem;
  color: #6b4e00;
  margin-bottom: 0.75rem;
}

.primary-btn:disabled {
  opacity: 0.5;
}

.photo-actions-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.photo-add-links {
  display: flex;
  gap: 0.6rem;
}

.photo-add-link {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.5rem 0.9rem;
  font-weight: 700;
  font-size: 0.9rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--fg);
  cursor: pointer;
}

.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 0.5rem;
  max-height: 55vh;
  overflow-y: auto;
  margin-top: 0.5rem;
}

.library-grid-btn {
  position: relative;
  border: 2px solid transparent;
  background: none;
  padding: 0;
  border-radius: 6px;
  cursor: pointer;
  line-height: 0;
}

.library-grid-btn.selected {
  border-color: var(--accent);
}

.library-grid-btn img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 4px;
  display: block;
}

.library-grid-check {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 1.1rem;
  height: 1.1rem;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 0.65rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

#library-modal {
  width: 90vw;
  max-width: 480px;
}

/* --- Photo pool: multi-select upload + Library both feed this same grid.
   Each item: numbered (matching send order to Gemini), a delete icon, and
   hover (desktop) / tap (mobile) to enlarge via #pool-enlarge-overlay. */
.photo-pool {
  position: relative; /* anchors the enlarge preview above the grid */
  margin-top: 0.5rem;
}

.pool-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 0.5rem;
}

.pool-thumb-wrap {
  position: relative;
  aspect-ratio: 1;
  border-radius: 6px;
  overflow: hidden;
  background: var(--border);
  cursor: pointer;
}

.pool-thumb-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.pool-item.uploading .pool-thumb-wrap img,
.pool-item.error .pool-thumb-wrap img {
  opacity: 0.4;
}

.pool-spinner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.pool-spinner::after {
  content: '';
  width: 18px;
  height: 18px;
  border: 3px solid rgba(255, 255, 255, 0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.pool-error-badge {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #c0392b;
  font-weight: 700;
  font-size: 1.1rem;
}

.pool-position {
  position: absolute;
  top: 2px;
  left: 2px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 0.65rem;
  line-height: 1;
  padding: 0.15rem 0.35rem;
  border-radius: 4px;
}

.pool-delete-btn {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 1.3rem;
  height: 1.3rem;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 0.7rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

/* A small preview floating next to the hovered/tapped thumbnail - not a
   full-screen overlay, so it never covers any thumbnail (including its own
   delete button). top/left/bottom are set inline by showEnlarge() in
   app.js, computed per-thumb (above it if there's room, below otherwise);
   position: fixed so those viewport coordinates are correct regardless of
   where this sits in the DOM. Height is intentionally not fixed - it's
   sized by the image's own aspect ratio at a fixed width. */
#pool-enlarge-overlay {
  position: fixed;
  width: 180px;
  border-radius: 16px;
  overflow: hidden;
  z-index: 200;
}

#pool-enlarge-overlay[hidden] { display: none; }

#pool-enlarge-overlay img {
  display: block;
  width: 100%;
  height: auto;
}

#preview-modal {
  width: 90vw;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
}

#preview-modal img {
  display: block;
  width: 100%;
  max-height: 60vh;
  object-fit: contain;
  border-radius: 8px;
  background: var(--border);
}

.preview-image-wrap {
  position: relative;
  min-height: 200px;
}

.image-loading-spinner[hidden] { display: none; }

.image-loading-spinner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--border);
  border-radius: 8px;
}

.image-loading-spinner::after {
  content: '';
  width: 28px;
  height: 28px;
  border: 3px solid rgba(0, 0, 0, 0.15);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

#spinner-overlay {
  position: fixed;
  inset: 0;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  z-index: 100;
}

#spinner-overlay[hidden] { display: none; }

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

#spinner-text {
  color: var(--fg);
  font-size: 0.95rem;
  margin: 0;
}

/* Wider screens: give the layout a bit more breathing room, still capped */
@media (min-width: 640px) {
  .container { padding: 2rem 1rem; }
}
