/* ============================================================
   QR Label Printer — stylesheet
   Mobile-first  |  ISO A4 print layout (2 cols × 4 rows)
   ============================================================ */

/* ── Reset & custom properties ────────────────────────────── */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #7c6fcd;
  --primary-dark: #6556ba;
  --danger: #f87171;
  --danger-bg: #fff1f2;
  --bg: #f7f5ff;
  --card-bg: #ffffff;
  --border: #e4dffa;
  --text: #1d1935;
  --text-muted: #7b75a0;
  --radius: 12px;
  --radius-sm: 6px;
  --shadow: 0 1px 3px rgba(124, 111, 205, .07), 0 1px 2px rgba(0, 0, 0, .04);
  --shadow-card: 0 4px 14px rgba(124, 111, 205, .13);
  --transition: 0.15s ease;
}

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  -webkit-text-size-adjust: 100%;
}

/* ── App header ───────────────────────────────────────────── */
.app-header {
  position: sticky;
  top: 0;
  z-index: 200;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  box-shadow: var(--shadow);
}

.app-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
  flex: 1;
  min-width: 160px;
  white-space: nowrap;
}

.logo-icon {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  color: var(--primary);
}

.entry-count {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
  order: 3;
  width: 100%;
  padding-left: 2px;
}

@media (min-width: 500px) {
  .entry-count {
    order: 0;
    width: auto;
  }

  .app-header {
    flex-wrap: nowrap;
  }
}

/* ── Buttons ──────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 16px;
  border: none;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  transition: background var(--transition), opacity var(--transition),
    border-color var(--transition), box-shadow var(--transition);
  white-space: nowrap;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn:focus-visible {
  outline: 3px solid rgba(124, 111, 205, .45);
  outline-offset: 2px;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-dark);
}

.btn-outline {
  background: transparent;
  color: var(--primary);
  border: 1.5px solid var(--primary);
}

.btn-outline:hover:not(:disabled) {
  background: #f3f0ff;
}

/* Spinner inside button during upload */
.spinner {
  display: inline-block;
  width: 13px;
  height: 13px;
  border: 2px solid rgba(255, 255, 255, .4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.65s linear infinite;
  flex-shrink: 0;
}

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

/* ── Main content area ────────────────────────────────────── */
#main-content {
  padding: 16px;
  max-width: 980px;
  margin: 0 auto;
}

/* ── Empty state ──────────────────────────────────────────── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 56px 24px;
  text-align: center;
  gap: 14px;
  color: var(--text-muted);
}

.empty-state svg {
  width: 88px;
  height: 88px;
  opacity: 0.2;
  color: var(--text);
}

.empty-state h2 {
  font-size: 1.15rem;
  color: var(--text);
}

.empty-state p {
  font-size: 0.875rem;
  line-height: 1.65;
  max-width: 320px;
}

/* ── Entries grid ─────────────────────────────────────────── */
.entries-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

@media (min-width: 480px) {
  .entries-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (min-width: 720px) {
  .entries-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (min-width: 980px) {
  .entries-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* ── Page divider (visual hint between pages) ─────────────── */
.page-divider {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0 2px;
}

.page-divider-label {
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .06em;
  white-space: nowrap;
}

.page-divider-line {
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* ── Card ─────────────────────────────────────────────────── */
.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  transition: box-shadow var(--transition), transform var(--transition);
}

.card:hover {
  box-shadow: var(--shadow-card);
  transform: translateY(-1px);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px 0;
}

.card-position {
  font-size: 0.68rem;
  color: var(--text-muted);
  font-weight: 500;
}

.delete-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.75rem;
  padding: 4px 7px;
  border-radius: 5px;
  line-height: 1;
  transition: color var(--transition), background var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.delete-btn:hover {
  color: var(--danger);
  background: var(--danger-bg);
}

.delete-btn:focus-visible {
  outline: 2px solid var(--danger);
  outline-offset: 2px;
}

.crop-card-btn {
  background: none;
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--text-muted);
  font-size: 0.72rem;
  font-weight: 500;
  padding: 4px 7px;
  border-radius: 5px;
  line-height: 1;
  margin-left: auto;
  transition: color var(--transition), background var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.crop-card-btn:hover {
  color: var(--primary);
  background: #f3f0ff;
}

.crop-card-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.card-image-wrap {
  flex: 1;
  padding: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 110px;
  background: #faf8ff;
}

.card-image-wrap img {
  max-width: 100%;
  max-height: 130px;
  object-fit: contain;
  display: block;
  border-radius: 4px;
}

.card-label {
  padding: 8px 10px 10px;
}

.text-input {
  width: 100%;
  padding: 8px 10px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-family: inherit;
  color: var(--text);
  background: #faf8ff;
  transition: border-color var(--transition), background var(--transition), box-shadow var(--transition);
  outline: none;
  -webkit-appearance: none;
}

.text-input:focus {
  border-color: var(--primary);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(124, 111, 205, .14);
}

/* ── Toast notification ───────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: #1e293b;
  color: #fff;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
  transition: transform 0.3s ease, opacity 0.3s ease;
  opacity: 0;
  pointer-events: none;
  white-space: nowrap;
  z-index: 1000;
  max-width: calc(100vw - 32px);
  white-space: normal;
  text-align: center;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* ── Print root — hidden on screen ───────────────────────── */
#print-root {
  display: none;
}

/* ── Crop modal ───────────────────────────────────────────── */
.crop-modal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 500;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.crop-modal.open {
  display: flex;
}

.crop-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}

.crop-modal-panel {
  position: relative;
  z-index: 1;
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
  padding: 20px;
  width: min(92vw, 560px);
  max-height: calc(100dvh - 32px);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.crop-modal-header h2 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
}

.crop-modal-hint {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 3px;
}

.crop-canvas-container {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #111;
  border-radius: var(--radius-sm);
  overflow: hidden;
}

#crop-canvas {
  display: block;
  cursor: crosshair;
  max-width: 100%;
  max-height: 55dvh;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.crop-modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1.5px solid transparent;
}

.btn-ghost:hover:not(:disabled) {
  background: var(--bg);
  color: var(--text);
}

/* ============================================================
   PRINT STYLES — ISO A4 portrait  |  2 cols × 4 rows
   ============================================================ */
@media print {
  @page {
    size: A4 portrait;
    margin: 10mm;
  }

  /* Strip any screen-inherited dimensions that cause a phantom second page */
  html, body {
    margin: 0 !important;
    padding: 0 !important;
    min-height: 0 !important;
    height: auto !important;
    background: white !important;
  }

  /* Suppress all screen UI */
  body>*:not(#print-root) {
    display: none !important;
  }

  /* Show print pages */
  #print-root {
    display: block !important;
    margin: 0;
    padding: 0;
  }

  /* One A4 content area per page
     Usable area with 10mm margins: 190mm × 277mm
     Using 265mm height to give iOS Safari rendering headroom */
  .print-page {
    width: 190mm;
    height: 265mm;
    display: grid;
    grid-template-columns: repeat(2, calc((190mm - 3mm) / 2));
    grid-template-rows: repeat(4, calc((265mm - 9mm) / 4));
    gap: 3mm;
    box-sizing: border-box;
    overflow: hidden;
    /* Page break after each page except the last */
    break-after: page;
    page-break-after: always;
  }

  .print-page:last-child {
    break-after: auto;
    page-break-after: auto;
  }

  /* Individual QR code cell — no background/border */
  .print-cell {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 1mm 2mm;
    overflow: hidden;
    box-sizing: border-box;
    gap: 1.5mm;
  }

  .print-cell img {
    max-width: 100%;
    max-height: 45mm;
    object-fit: contain;
    display: block;
    flex-shrink: 1;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .print-label {
    font-size: 7.5pt;
    font-family: system-ui, Arial, Helvetica, sans-serif;
    text-align: center;
    word-break: break-word;
    overflow-wrap: break-word;
    color: #000;
    line-height: 1.3;
    max-width: 100%;
    flex-shrink: 0;
    margin: 0;
  }

  /* Hide spinner in print context (shouldn't appear, but safety net) */
  .spinner {
    display: none !important;
  }
}