/* ═══════════════════════════════════════════════════
   PuzzleCam — Xếp Hình & Quay Video
   iOS-style PWA | Dark Theme | Responsive
   ═══════════════════════════════════════════════════ */

/* ─── CSS Variables ─── */
:root {
  /* Colors */
  --bg-deep: #0F0E17;
  --bg-surface: #1A1932;
  --bg-elevated: #232146;
  --bg-hover: #2D2A5E;
  --border-color: rgba(255, 255, 254, 0.08);
  --border-light: rgba(255, 255, 254, 0.15);

  --primary: #FF8906;
  --primary-dark: #E07500;
  --primary-glow: rgba(255, 137, 6, 0.3);
  --secondary: #E53170;
  --accent: #F25F4C;
  --success: #00D4AA;
  --error: #F25F4C;
  --warning: #FFD93D;

  --text: #FFFFFE;
  --text-secondary: #D1D3E0;
  --text-muted: #A7A9BE;
  --text-dim: #6E7096;

  /* Typography */
  --font-display: 'Playfair Display', Georgia, 'Times New Roman', serif;
  --font-body: 'Instrument Sans', -apple-system, 'Segoe UI', sans-serif;
  --font-mono: 'SF Mono', 'Cascadia Code', 'Consolas', monospace;

  /* Spacing */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 32px;
  --space-2xl: 48px;

  /* Radius */
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-xl: 28px;
  --radius-full: 9999px;

  /* Shadows */
  --shadow-sm: 0 2px 8px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 20px rgba(0,0,0,0.4);
  --shadow-lg: 0 8px 40px rgba(0,0,0,0.5);
  --shadow-glow: 0 0 20px var(--primary-glow);

  /* Layout */
  --header-height: 56px;
  --sidebar-width: 300px;
  --bottom-nav-height: 64px;
  /* Camera PIP removed — ẩn hoàn toàn */

  /* Transitions */
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease-smooth: cubic-bezier(0.22, 1, 0.36, 1);
  --ease-bounce: cubic-bezier(0.34, 1.5, 0.64, 1);
  --duration-fast: 150ms;
  --duration-normal: 300ms;
  --duration-slow: 500ms;
}

/* ─── Reset & Base ─── */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-user-select: none;
  user-select: none;
}

body {
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg-deep);
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
  overflow: hidden;
  background: var(--bg-deep);
  position: relative;
}

/* ─── Scrollbar ─── */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--text-dim); border-radius: 4px; }

/* ─── Header ─── */
#app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-height);
  padding: 0 var(--space-md);
  background: rgba(15, 14, 23, 0.85);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-bottom: 1px solid var(--border-color);
  z-index: 100;
  position: relative;
}

.logo {
  font-family: var(--font-display);
  font-size: 20px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.3px;
}

.header-center {
  flex: 1;
  display: flex;
  justify-content: center;
}

#recording-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  background: rgba(242, 95, 76, 0.12);
  border: 1px solid rgba(242, 95, 76, 0.25);
  border-radius: var(--radius-full);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
}

.rec-dot {
  width: 8px;
  height: 8px;
  background: var(--accent);
  border-radius: 50%;
  animation: pulse-dot 1.5s ease-in-out infinite;
}

@keyframes pulse-dot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.8); }
}

#rec-timer {
  font-variant-numeric: tabular-nums;
  font-family: var(--font-mono);
  color: var(--accent);
  font-weight: 600;
}

.rec-sep { color: var(--text-dim); }

#rec-segments { color: var(--text-muted); }

.header-right { display: flex; gap: 4px; }

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 18px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--duration-fast) var(--ease-smooth);
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn.small { width: 28px; height: 28px; font-size: 14px; }
.icon-btn.tiny { width: 24px; height: 24px; font-size: 12px; }

/* ─── Nút kích hoạt ghi hình (header-right) ─── */
.rec-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  background: rgba(255,255,254,0.08);
  color: var(--text-secondary);
  border-radius: 50%;
  cursor: pointer;
  transition: all var(--duration-fast) var(--ease-smooth);
  position: relative;
}
.rec-btn:hover { background: rgba(242,95,76,0.15); }
.rec-btn:active { transform: scale(0.92); }
.rec-btn.active { background: rgba(242,95,76,0.2); box-shadow: 0 0 12px rgba(242,95,76,0.25); }

.rec-btn-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--success);
  transition: all var(--duration-fast) var(--ease-smooth);
}
.rec-btn.active .rec-btn-dot {
  background: var(--accent);
  animation: pulse-dot 1.5s ease-in-out infinite;
}

/* ─── App Body (Sidebar + Main) ─── */
#app-body {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* ─── Sidebar (Desktop) ─── */
#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-surface);
  border-right: 1px solid var(--border-color);
  overflow-y: auto;
  padding: var(--space-md);
  display: flex;
  flex-direction: column;
  gap: var(--space-lg);
  transition: transform var(--duration-normal) var(--ease-smooth);
}

#sidebar .sidebar-section {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.sidebar-title {
  font-family: var(--font-display);
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: 0.5px;
  text-transform: uppercase;
  margin-bottom: 4px;
}

#cam-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-secondary);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.status-dot.green { background: var(--success); box-shadow: 0 0 8px rgba(0, 212, 170, 0.4); }
.status-dot.red { background: var(--error); box-shadow: 0 0 8px rgba(242, 95, 76, 0.4); }
.status-dot.yellow { background: var(--warning); box-shadow: 0 0 8px rgba(255, 217, 61, 0.4); }

.sidebar-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 16px;
  border: none;
  border-radius: var(--radius-sm);
  font-family: var(--font-body);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--duration-fast) var(--ease-smooth);
  background: var(--bg-elevated);
  color: var(--text-secondary);
}
.sidebar-btn:hover { background: var(--bg-hover); color: var(--text); }
.sidebar-btn:active { transform: scale(0.97); }
.sidebar-btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }
.sidebar-btn.primary { background: var(--primary); color: #000; }
.sidebar-btn.primary:hover { background: var(--primary-dark); }
.sidebar-btn.danger { color: var(--error); border: 1px solid rgba(242, 95, 76, 0.3); }
.sidebar-btn.danger:hover { background: rgba(242, 95, 76, 0.1); }

#segment-list {
  max-height: 180px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.empty-msg { font-size: 12px; color: var(--text-dim); text-align: center; padding: 12px 0; }

.segment-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  background: var(--bg-elevated);
  border-radius: var(--radius-sm);
  font-size: 12px;
  cursor: pointer;
  transition: background var(--duration-fast);
}
.segment-item:hover { background: var(--bg-hover); }
.segment-item .seg-name { color: var(--text-secondary); }
.segment-item .seg-size { color: var(--text-dim); font-family: var(--font-mono); }

/* Toggle row */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
}
.toggle-row input[type="checkbox"] { display: none; }

.toggle-switch {
  position: relative;
  width: 44px;
  height: 26px;
  background: var(--text-dim);
  border-radius: 13px;
  transition: background var(--duration-fast) var(--ease-smooth);
  flex-shrink: 0;
}
.toggle-switch::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 50%;
  transition: transform var(--duration-fast) var(--ease-spring);
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}
.toggle-row input:checked + .toggle-switch { background: var(--success); }
.toggle-row input:checked + .toggle-switch::after { transform: translateX(18px); }

.grid-size-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 0;
  font-size: 13px;
  color: var(--text-secondary);
}

.size-btns { display: flex; gap: 4px; }

.size-btn {
  width: 32px;
  height: 32px;
  border: 1px solid var(--border-light);
  background: transparent;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 500;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--duration-fast) var(--ease-smooth);
}
.size-btn:hover { border-color: var(--primary); color: var(--primary); }
.size-btn.active { background: var(--primary); color: #000; border-color: var(--primary); font-weight: 600; }

/* ─── Main Content ─── */
#main-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--space-md);
  display: flex;
  flex-direction: column;
  position: relative;
  background:
    radial-gradient(ellipse 80% 60% at 20% 20%, rgba(255, 137, 6, 0.06) 0%, transparent 60%),
    radial-gradient(ellipse 60% 80% at 80% 80%, rgba(229, 49, 112, 0.05) 0%, transparent 60%),
    var(--bg-deep);
}

/* ─── Puzzle Section ─── */
#puzzle-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  min-height: 0;
}

.puzzle-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.puzzle-header h2 {
  font-family: var(--font-display);
  font-size: 22px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.puzzle-stats {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.puzzle-stats :first-child { color: var(--primary); font-size: 18px; }

#puzzle-container {
  flex: 1;
  display: flex;
  gap: var(--space-md);
  min-height: 0;
}

/* Board */
#puzzle-board {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255,255,255,0.03);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-color);
  min-height: 320px;
  position: relative;
  overflow: hidden;
}

#board-canvas {
  display: block;
  margin: auto;
  object-fit: contain;
  max-width: 100%;
  max-height: 100%;
}

/* Tray */
#puzzle-tray {
  width: 200px;
  min-width: 200px;
  background: var(--bg-surface);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.tray-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  font-size: 12px;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border-color);
}

#tray-pieces {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(48px, 1fr));
  gap: 6px;
  padding: 8px;
  overflow-y: auto;
  align-content: start;
}

.tray-piece {
  position: relative;
  cursor: grab;
  touch-action: none;
  transition: transform var(--duration-fast) var(--ease-spring), box-shadow var(--duration-fast);
  border-radius: 4px;
  aspect-ratio: 1;
}
.tray-piece:hover { transform: scale(1.08); z-index: 2; }
.tray-piece:active { cursor: grabbing; transform: scale(0.95); }
.tray-piece.placed { opacity: 0.3; pointer-events: none; }
.tray-piece.selected {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: 4px;
  box-shadow: 0 0 20px var(--primary-glow);
  transform: scale(1.05);
  z-index: 3;
}
.tray-piece canvas { width: 100%; height: 100%; display: block; border-radius: 4px; }

/* ─── Completion Overlay ─── */
#completion-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 14, 23, 0.85);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  animation: fadeIn var(--duration-normal) var(--ease-smooth);
}

#completion-overlay.hidden { display: none; }

.completion-card {
  text-align: center;
  padding: var(--space-2xl);
  background: var(--bg-surface);
  border-radius: var(--radius-xl);
  border: 1px solid var(--border-light);
  box-shadow: var(--shadow-lg);
  animation: scaleIn var(--duration-slow) var(--ease-spring);
}

.completion-icon {
  font-size: 64px;
  margin-bottom: var(--space-md);
  animation: bounce 1s var(--ease-bounce) infinite;
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-12px); }
}

.completion-card h2 {
  font-family: var(--font-display);
  font-size: 28px;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: var(--space-sm);
}

.completion-card p {
  color: var(--text-muted);
  margin-bottom: var(--space-lg);
}

.btn-primary {
  padding: 12px 32px;
  background: var(--primary);
  color: #000;
  border: none;
  border-radius: var(--radius-full);
  font-family: var(--font-body);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--duration-fast) var(--ease-smooth);
  box-shadow: var(--shadow-glow);
}
.btn-primary:hover { background: var(--primary-dark); transform: translateY(-1px); }
.btn-primary:active { transform: scale(0.97); }
.btn-primary:disabled { opacity: 0.4; cursor: not-allowed; transform: none; box-shadow: none; }

/* ─── Bottom Navigation (Mobile) ─── */
#bottom-nav {
  display: none;
  height: var(--bottom-nav-height);
  background: rgba(26, 25, 50, 0.9);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-top: 1px solid var(--border-color);
  padding-bottom: env(safe-area-inset-bottom, 0);
  z-index: 100;
}

.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 10px;
  cursor: pointer;
  transition: all var(--duration-fast) var(--ease-smooth);
  position: relative;
  padding: 4px 0;
}
.nav-btn .nav-icon { font-size: 22px; transition: transform var(--duration-fast); }
.nav-btn.active { color: var(--primary); }
.nav-btn.active .nav-icon { transform: scale(1.1); }
.nav-btn.active::after {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 24px;
  height: 3px;
  background: var(--primary);
  border-radius: 0 0 3px 3px;
}

/* ─── Recordings Sheet (Mobile) ─── */
.sheet {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-height: 70dvh;
  background: var(--bg-surface);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  z-index: 150;
  display: flex;
  flex-direction: column;
  box-shadow: 0 -8px 40px rgba(0,0,0,0.5);
  animation: slideUp var(--duration-normal) var(--ease-smooth);
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.sheet.hidden { display: none; }

.sheet-handle {
  width: 36px;
  height: 4px;
  background: var(--text-dim);
  border-radius: 2px;
  margin: 8px auto;
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px var(--space-md);
  border-bottom: 1px solid var(--border-color);
}

.sheet-header h3 { font-size: 16px; color: var(--text); }

.sheet-body {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-md);
}

.sheet-footer {
  padding: var(--space-md);
  border-top: 1px solid var(--border-color);
  text-align: center;
}

.sheet-footer .btn-primary { width: 100%; }

/* ─── Toast ─── */
#toast-container {
  position: fixed;
  top: calc(var(--header-height) + var(--space-sm));
  left: 50%;
  transform: translateX(-50%);
  z-index: 300;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.toast {
  padding: 10px 20px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-full);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  animation: toastIn var(--duration-normal) var(--ease-spring), toastOut var(--duration-normal) var(--ease-smooth) 3s forwards;
  pointer-events: auto;
  backdrop-filter: blur(12px);
}

@keyframes toastIn {
  from { opacity: 0; transform: translateY(-20px) scale(0.9); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes toastOut {
  from { opacity: 1; transform: translateY(0) scale(1); }
  to { opacity: 0; transform: translateY(-20px) scale(0.9); }
}

/* ─── Utility ─── */
.hidden { display: none !important; }

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

@keyframes scaleIn {
  from { opacity: 0; transform: scale(0.8); }
  to { opacity: 1; transform: scale(1); }
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

/* ─── Confetti particles ─── */
.confetti-piece {
  position: fixed;
  width: 10px;
  height: 10px;
  pointer-events: none;
  z-index: 250;
  animation: confettiFall linear forwards;
}

@keyframes confettiFall {
  0% { opacity: 1; transform: translateY(-10vh) rotate(0deg); }
  100% { opacity: 0; transform: translateY(110vh) rotate(720deg); }
}

/* ═══════════════════════════════════════════════════
   RESPONSIVE
   ═══════════════════════════════════════════════════ */

/* ─── Mobile (≤ 768px) ─── */
@media (max-width: 768px) {
  :root {
    --header-height: 48px;
    /* Camera PIP removed — ẩn hoàn toàn */
  }

  #sidebar { display: none; }

  #bottom-nav { display: flex; }

  #main-content {
    padding: var(--space-sm);
    padding-bottom: 0;
  }

  .puzzle-header { padding: 0 4px; }
  .puzzle-header h2 { font-size: 18px; }

  #puzzle-container {
    flex-direction: column;
    gap: var(--space-sm);
  }

  #puzzle-tray {
    width: 100%;
    min-width: unset;
    max-height: 140px;
  }

  #tray-pieces {
    grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
    max-height: 100px;
  }

  /* Show indicator in header on mobile */
  #recording-indicator { font-size: 11px; padding: 2px 8px; }

  .header-right .icon-btn { width: 32px; height: 32px; font-size: 16px; }
  .logo { font-size: 17px; }

  /* Recordings tab shows as sheet */
  #recordings-sheet:not(.hidden) ~ #bottom-nav .nav-btn[data-tab="recordings"] {
    color: var(--primary);
  }
}

/* ─── Tablet (768-1024px) ─── */
@media (min-width: 768px) and (max-width: 1024px) {
  :root {
    --sidebar-width: 240px;
  }

  #bottom-nav { display: none; }

  #puzzle-tray {
    width: 160px;
    min-width: 160px;
  }

  #tray-pieces {
    grid-template-columns: repeat(auto-fill, minmax(42px, 1fr));
  }
}

/* ─── Desktop (>1024px) ─── */
@media (min-width: 1024px) {
  #bottom-nav { display: none; }
  #recordings-sheet { display: none; }

  #puzzle-tray {
    width: 220px;
    min-width: 220px;
  }

  #tray-pieces {
    grid-template-columns: repeat(auto-fill, minmax(52px, 1fr));
  }
}
