* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #07a2b2;
  --primary-dark: #058a98;
  --primary-light: #e8f7f9;
  --bg: #f4fafb;
  --text: #2c3e50;
  --text-light: #5a6c7d;
  --white: #ffffff;
  --shadow: rgba(7, 162, 178, 0.15);
  --radius: 12px;
}

html, body {
  font-family: 'Tajawal', sans-serif;
  background-color: var(--bg);
  color: var(--text);
  direction: rtl;
  height: 100%;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ─── App Shell: full viewport, no scroll ──── */
.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

/* ─── Header ─────────────────────────────── */
header {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  padding: 8px 16px;
  text-align: center;
  box-shadow: 0 2px 10px var(--shadow);
  flex-shrink: 0;
  z-index: 10;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.header-logo {
  height: 28px;
  width: auto;
}

header h1 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--white);
}

/* ─── Sections ────────────────────────────── */
.section {
  display: none;
  flex: 1;
  overflow: hidden;
}

.section.active {
  display: flex;
  flex-direction: column;
  animation: fadeIn 0.25s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* ═══════════════════════════════════════════
   Section 1: Template Selection
   3 cols x 2 rows — fits in viewport
   ═══════════════════════════════════════════ */
#section-select {
  align-items: center;
  justify-content: center;
  padding: 8px 10px;
  gap: 6px;
}

.section-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--primary-dark);
  text-align: center;
  flex-shrink: 0;
}

.section-subtitle {
  font-size: 0.75rem;
  color: var(--text-light);
  text-align: center;
  flex-shrink: 0;
}

.templates-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 8px;
  width: 100%;
  max-width: 500px;
  flex: 1;
  min-height: 0;
}

.template-card {
  cursor: pointer;
  border-radius: var(--radius);
  overflow: hidden;
  border: 2px solid transparent;
  transition: all 0.2s ease;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  background: var(--white);
  position: relative;
  min-height: 0;
}

.template-card:hover,
.template-card:active {
  border-color: var(--primary);
  box-shadow: 0 4px 14px var(--shadow);
}

.template-card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.template-card .template-label {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(to top, rgba(0,0,0,0.7), transparent);
  color: var(--white);
  text-align: center;
  padding: 14px 4px 6px;
  font-size: 0.7rem;
  font-weight: 700;
}

/* ═══════════════════════════════════════════
   Section 2: Preview + Controls
   Fits entirely in viewport — no scroll
   ═══════════════════════════════════════════ */
#section-preview {
  padding: 8px 12px;
  gap: 6px;
  align-items: center;
}

#section-preview.active {
  display: flex;
  flex-direction: column;
}

/* Input row */
.controls {
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex-shrink: 0;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.input-group label {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--primary-dark);
}

#name-input {
  width: 100%;
  padding: 10px 14px;
  font-size: 1.1rem;
  font-family: 'Tajawal', sans-serif;
  font-weight: 700;
  direction: rtl;
  text-align: center;
  border: 2px solid #d0e8eb;
  border-radius: 10px;
  background: var(--white);
  color: var(--text);
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

#name-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(7, 162, 178, 0.12);
}

#name-input::placeholder {
  color: #b0c4c8;
  font-weight: 400;
}

.error-msg {
  color: #e74c3c;
  font-size: 0.7rem;
  text-align: center;
  min-height: 14px;
  font-weight: 500;
}

/* Preview image — takes all remaining space */
.preview-wrapper {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

#preview-img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: var(--radius);
  box-shadow: 0 4px 20px rgba(0,0,0,0.12);
  display: block;
}

/* Buttons row */
.btn-row {
  display: flex;
  gap: 8px;
  width: 100%;
  max-width: 400px;
  flex-shrink: 0;
}

.btn {
  flex: 1;
  padding: 10px 16px;
  font-size: 0.9rem;
  font-family: 'Tajawal', sans-serif;
  font-weight: 700;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s ease;
  text-align: center;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: var(--white);
  box-shadow: 0 3px 10px var(--shadow);
}

.btn-primary:hover {
  box-shadow: 0 5px 16px rgba(7, 162, 178, 0.3);
}

.btn-secondary {
  background: var(--white);
  color: var(--primary-dark);
  border: 2px solid #d0e8eb;
}

.btn-secondary:hover {
  background: var(--primary-light);
  border-color: var(--primary);
}

/* ─── Canvas (hidden) ─────────────────────── */
#canvas {
  display: none;
}

/* ─── Footer ─────────────────────────────── */
footer {
  text-align: center;
  padding: 4px;
  color: var(--text-light);
  font-size: 0.65rem;
  flex-shrink: 0;
}

footer a {
  color: var(--primary);
  text-decoration: none;
  font-weight: 700;
}

/* ─── Desktop / Tablet ─────────────────────── */
@media (min-width: 768px) {
  header { padding: 12px 20px; }
  header h1 { font-size: 1.2rem; }
  .header-logo { height: 34px; }

  #section-select { padding: 16px; gap: 10px; }
  .section-title { font-size: 1.2rem; }
  .section-subtitle { font-size: 0.85rem; }
  .templates-grid { max-width: 600px; gap: 12px; }
  .template-card .template-label { font-size: 0.8rem; padding: 16px 6px 8px; }

  #section-preview { padding: 12px 20px; gap: 10px; }
  #name-input { font-size: 1.2rem; padding: 12px 16px; }
  .btn { font-size: 1rem; padding: 12px 20px; }
  footer { font-size: 0.75rem; padding: 6px; }
}
