:root {
  --bg: #0d0f14;
  --bg-elevated: #14161d;
  --surface: #1a1d26;
  --surface-2: #262a35;
  --surface-border: #2c3040;
  --text: #f2f3f5;
  --muted: #9aa0ac;
  --muted-2: #6b7280;
  --accent: #4f8cff;
  --accent-2: #6ea3ff;
  --accent-hover: #3d78e6;
  --accent-soft: rgba(79, 140, 255, 0.14);
  --danger: #f0555a;
  --success: #2ecc71;
  --radius-sm: 8px;
  --radius: 14px;
  --radius-lg: 20px;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.4);
  --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body {
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(79, 140, 255, 0.08), transparent 60%),
    var(--bg);
}

.hidden { display: none !important; }

/* ---------- Header ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--surface-border);
  position: sticky;
  top: 0;
  background: rgba(13, 15, 20, 0.85);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
}

.brand-icon {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  box-shadow: var(--shadow-sm);
}

.topbar h1 {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 0;
}

.version-tag {
  font-size: 0.68rem;
  font-weight: 600;
  color: var(--muted-2);
  background: var(--surface-2);
  padding: 2px 7px;
  border-radius: 999px;
  vertical-align: middle;
  margin-left: 2px;
}

.user-box {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.83rem;
  color: var(--muted);
}

.avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1px solid var(--surface-border);
}

.icon-btn {
  background: var(--surface);
  border: 1px solid var(--surface-border);
  color: var(--text);
  width: 32px;
  height: 32px;
  border-radius: 9px;
  font-size: 0.95rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.icon-btn:hover { border-color: var(--accent); background: var(--surface-2); }

.range-input {
  width: 100%;
  accent-color: var(--accent);
  margin: 6px 0 10px;
  cursor: pointer;
}

/* ---------- Layout ---------- */

main {
  max-width: 520px;
  margin: 0 auto;
  padding: 22px 16px 140px;
}

.screen { width: 100%; }

/* ---------- Login ---------- */

.login-card {
  background: linear-gradient(180deg, var(--surface) 0%, var(--bg-elevated) 100%);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-lg);
  padding: 40px 26px;
  text-align: center;
  margin: 12vh auto 0;
  max-width: 420px;
  box-shadow: var(--shadow-md);
}

.login-icon {
  width: 64px;
  height: 64px;
  border-radius: 16px;
  box-shadow: var(--shadow-md);
  margin-bottom: 18px;
}

.login-title {
  font-size: 1.3rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  margin: 0 0 10px;
}

.login-card p {
  color: var(--muted);
  margin: 0 0 28px;
  line-height: 1.55;
  font-size: 0.95rem;
}

.google-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

/* ---------- Botones ---------- */

.btn-primary {
  background: linear-gradient(180deg, var(--accent-2) 0%, var(--accent) 100%);
  color: white;
  border: none;
  border-radius: 12px;
  padding: 13px 20px;
  font-family: var(--font);
  font-size: 0.98rem;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  box-shadow: 0 3px 10px rgba(79, 140, 255, 0.28);
  transition: transform 0.1s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.btn-primary:hover { background: linear-gradient(180deg, var(--accent-2) 0%, var(--accent-hover) 100%); }
.btn-primary:active { transform: scale(0.98); box-shadow: 0 1px 6px rgba(79, 140, 255, 0.25); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.btn-large {
  padding: 18px;
  font-size: 1.05rem;
}

.btn-ghost {
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--surface-border);
  border-radius: 12px;
  padding: 10px 16px;
  font-family: var(--font);
  cursor: pointer;
  font-size: 0.88rem;
  font-weight: 500;
  transition: color 0.15s ease, border-color 0.15s ease, transform 0.1s ease, background 0.15s ease;
}
.btn-ghost:hover { color: var(--text); border-color: var(--accent); background: var(--surface-2); }
.btn-ghost:active { transform: scale(0.98); }

/* ---------- Cámara / captura ---------- */

.capture-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  /* --nav-bar-inset la calcula app.js con visualViewport (cubre la barra de
     Android en los casos donde el sistema sí la reporta). Como muchos
     Android instalados como app no la reportan por ninguna vía, sumamos
     además un margen fijo generoso (36px) como respaldo garantizado. */
  padding: 16px 16px calc(36px + env(safe-area-inset-bottom) + var(--nav-bar-inset, 0px));
  background: linear-gradient(to top, var(--bg) 55%, transparent);
  z-index: 50;
}

.capture-bar-row {
  max-width: 520px;
  margin: 0 auto;
  display: flex;
  gap: 10px;
}
@media (min-width: 768px) { .capture-bar-row { max-width: 640px; } }
@media (min-width: 1024px) { .capture-bar-row { max-width: 900px; } }
@media (min-width: 1400px) { .capture-bar-row { max-width: 1160px; } }

.capture-bar-row .btn-large {
  flex: 1;
}

@media (min-width: 768px) {
  #captureBtn { display: none; }
}

/* ---------- Galería ---------- */

.gallery-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
  gap: 10px;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.names-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 14px;
}

.name-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-sm);
  padding: 6px 6px 6px 14px;
}

.name-row-main {
  flex: 1;
  display: flex;
  align-items: baseline;
  gap: 8px;
  background: none;
  border: none;
  color: var(--text);
  font-family: var(--font);
  font-size: 0.95rem;
  font-weight: 500;
  text-align: left;
  padding: 8px 0;
  cursor: pointer;
  min-width: 0;
}
.name-row-main span.label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.name-row-main:hover { color: var(--accent-2); }

.name-count {
  color: var(--muted-2);
  font-size: 0.78rem;
  font-weight: 400;
  flex-shrink: 0;
}

.name-row-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.name-row-actions button {
  background: var(--surface-2);
  border: 1px solid var(--surface-border);
  color: var(--text);
  border-radius: 9px;
  width: 34px;
  height: 34px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.name-row-actions button:hover { border-color: var(--accent); }
.name-row-actions .name-delete:hover { background: var(--danger); border-color: var(--danger); }

.section-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text);
  margin: 0;
  letter-spacing: -0.01em;
}

.filters-card {
  background: var(--surface);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 16px;
}
.filters-card .select-input { margin-bottom: 10px; }
.filters-card .toggle-row { margin-bottom: 0; }

.gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 14px;
}

/* En pantallas grandes hay espacio de sobra: usamos más ancho y más columnas */
@media (min-width: 768px) {
  main { max-width: 640px; }
  .gallery { grid-template-columns: repeat(4, 1fr); }
}
@media (min-width: 1024px) {
  main { max-width: 900px; }
  .gallery { grid-template-columns: repeat(6, 1fr); }
}
@media (min-width: 1400px) {
  main { max-width: 1160px; }
  .gallery { grid-template-columns: repeat(8, 1fr); }
}

.gallery-item {
  position: relative;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--surface-border);
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
@media (hover: hover) {
  .gallery-item:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
    border-color: var(--surface-2);
  }
}

.day-divider {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
  margin: 10px 0 2px;
  text-transform: capitalize;
}
.day-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--surface-border);
}

.gallery-item .thumb {
  position: relative;
  aspect-ratio: 3/4;
  overflow: hidden;
  background: var(--surface-2);
}

.gallery-item .del {
  position: absolute;
  top: 4px;
  right: 4px;
  background: rgba(20, 22, 29, 0.75);
  backdrop-filter: blur(4px);
  color: white;
  border-radius: 7px;
  padding: 4px 6px;
  font-size: 0.78rem;
  text-decoration: none;
  border: none;
  cursor: pointer;
  line-height: 1;
  z-index: 5;
  transition: background 0.15s ease;
}
.gallery-item .del:hover { background: var(--danger); }

.gallery-item .name-label {
  display: block;
  font-size: 0.71rem;
  font-weight: 500;
  color: var(--muted);
  padding: 5px 6px 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: var(--surface);
  border-top: 1px solid var(--surface-border);
}

.gallery-item:not(:has(.name-extra)) .name-label {
  padding-bottom: 6px;
}

.gallery-item .name-extra {
  display: block;
  font-size: 0.65rem;
  color: var(--accent-2);
  padding: 0 6px 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: var(--surface);
}

.gallery-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  cursor: pointer;
}

.gallery-item .dl {
  position: absolute;
  bottom: 4px;
  right: 4px;
  background: rgba(20, 22, 29, 0.75);
  backdrop-filter: blur(4px);
  color: white;
  border: none;
  border-radius: 7px;
  padding: 4px 6px;
  font-size: 0.75rem;
  text-decoration: none;
  cursor: pointer;
  line-height: 1;
  z-index: 5;
  transition: background 0.15s ease;
}
.gallery-item .dl:hover { background: var(--accent); }

.gallery-item .downloaded-badge {
  position: absolute;
  top: 4px;
  left: 4px;
  background: rgba(20, 22, 29, 0.55);
  border: 1.5px solid rgba(255, 255, 255, 0.55);
  color: white;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: bold;
  padding: 0;
  cursor: pointer;
  z-index: 5;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
  transition: background 0.15s ease, border-color 0.15s ease;
}
.gallery-item .downloaded-badge.on {
  background: var(--success);
  border-color: var(--success);
}
.gallery-item .downloaded-badge:hover { border-color: var(--accent); }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

#galleryEmpty {
  text-align: center;
  padding: 30px 16px;
  font-size: 0.9rem;
}

/* ---------- Editor / preview ---------- */

.editor-wrap, .preview-wrap {
  background: var(--surface);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius);
  padding: 10px;
  display: flex;
  justify-content: center;
  margin-bottom: 14px;
  position: relative;
  box-shadow: var(--shadow-sm);
}

.magnifier {
  position: fixed;
  width: 130px;
  height: 130px;
  border-radius: 50%;
  border: 3px solid var(--accent);
  box-shadow: var(--shadow-md);
  pointer-events: none;
  z-index: 150;
  background: #000;
}

.zoom-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 10px;
}
.zoom-controls .btn-ghost {
  padding: 8px 14px;
}

.template-editor-wrap {
  overflow: auto;
  max-height: 65vh;
  justify-content: flex-start;
  touch-action: none;
}

#editorCanvas, #previewCanvas, #templateCanvas {
  max-width: 100%;
  max-height: 65vh;
  touch-action: none;
  border-radius: var(--radius-sm);
}

/* templateCanvas necesita poder crecer más allá de eso al hacer zoom
   (el contenedor se encarga de scrollear) */
#templateCanvas {
  max-width: none;
  max-height: none;
}

.editor-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}
.editor-actions .btn-ghost, .editor-actions .btn-primary {
  flex: 1;
}

.btn-full {
  width: 100%;
  margin-top: 10px;
}

.field-label {
  display: block;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 500;
  margin: 18px 0 7px;
}

.text-input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--surface-border);
  color: var(--text);
  border-radius: 12px;
  padding: 12px 14px;
  font-family: var(--font);
  font-size: 1rem;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.text-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

#nameSuggestHint {
  margin: 7px 0 0;
  color: var(--accent-2);
  font-weight: 500;
}

.template-fields {
  background: var(--surface);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius);
  padding: 12px 14px 4px;
  margin-top: 14px;
}

.template-field-row {
  margin-bottom: 10px;
}

.template-field-row label {
  display: block;
  color: var(--muted);
  font-size: 0.8rem;
  margin-bottom: 5px;
}

.template-box-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 12px 0;
}

.template-box-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 0.9rem;
}

.template-box-row .box-color {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  flex-shrink: 0;
}

.template-box-row .box-label {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.box-label-main {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.box-preview {
  font-size: 0.78rem;
  color: var(--accent-2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.template-box-row button {
  background: var(--surface-2);
  border: 1px solid var(--surface-border);
  color: var(--text);
  border-radius: 8px;
  width: 28px;
  height: 28px;
  cursor: pointer;
  flex-shrink: 0;
}
.template-box-row button:hover { background: var(--danger); border-color: var(--danger); }

.select-input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--surface-border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 14px;
  font-family: var(--font);
  font-size: 0.93rem;
  margin-bottom: 12px;
  transition: border-color 0.15s ease;
}
.select-input:focus {
  outline: none;
  border-color: var(--accent);
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--text);
  font-size: 0.88rem;
  font-weight: 500;
  margin-bottom: 12px;
  cursor: pointer;
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 26px;
  flex-shrink: 0;
}

.toggle-switch input {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
  z-index: 1;
}

.toggle-track {
  position: absolute;
  inset: 0;
  background: var(--surface-2);
  border: 1px solid var(--surface-border);
  border-radius: 999px;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 50%;
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s ease;
}

.toggle-switch input:checked ~ .toggle-track {
  background: var(--accent);
  border-color: var(--accent);
}
.toggle-switch input:checked ~ .toggle-track .toggle-thumb {
  transform: translateX(18px);
}

.filter-toggle {
  display: flex;
  gap: 20px;
  justify-content: center;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 500;
  margin-top: 12px;
}

/* ---------- Lightbox ---------- */

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(6, 7, 10, 0.94);
  backdrop-filter: blur(6px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 300;
  padding: 20px;
}

.lightbox-imgwrap {
  width: 100%;
  height: 78vh;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}

.lightbox img {
  max-width: 100%;
  max-height: 78vh;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  cursor: grab;
  user-select: none;
  -webkit-user-drag: none;
  will-change: transform;
}
.lightbox img:active { cursor: grabbing; }

.lightbox-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: var(--surface-2);
  border: 1px solid var(--surface-border);
  color: var(--text);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 1.05rem;
  cursor: pointer;
  transition: background 0.15s ease;
}
.lightbox-close:hover { background: var(--danger); border-color: var(--danger); }

.lightbox-dl {
  width: auto;
  padding: 12px 26px;
  text-decoration: none;
  text-align: center;
}

/* ---------- Toast / overlay ---------- */

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-2);
  border: 1px solid var(--surface-border);
  color: var(--text);
  padding: 11px 18px;
  border-radius: 12px;
  font-size: 0.9rem;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  z-index: 100;
  max-width: 90vw;
}
.toast.error { background: var(--danger); border-color: var(--danger); }

.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(6, 7, 10, 0.7);
  backdrop-filter: blur(3px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  z-index: 200;
}

#loadingText {
  font-weight: 500;
  color: var(--muted);
}

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

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