/* =============================================
   Dojo Competition Tracker
   Mobile-first | Themed | Kid-friendly
   ============================================= */

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

html {
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --nav-height: 64px;
  --tap-min: 44px;
  --radius: 14px;
  --radius-sm: 8px;
  --radius-pill: 9999px;
  --shadow: 0 2px 8px rgba(0,0,0,0.1);
  --shadow-lg: 0 4px 16px rgba(0,0,0,0.15);
  --blue: #2563eb;
  --purple: #a855f7;
  --green: #22c55e;
  --red: #ef4444;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-300: #d1d5db;
  --gray-400: #9ca3af;
  --gray-500: #6b7280;
  --gray-700: #374151;
  --gray-900: #111827;
}

body {
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  color: var(--gray-900);
  background: var(--gray-50);
  min-height: 100vh;
  padding-bottom: calc(var(--nav-height) + 16px);
  -webkit-font-smoothing: antialiased;
}

/* =============================================
   Header
   ============================================= */
.app-header {
  background: linear-gradient(135deg, #1e1b4b 0%, #3730a3 50%, #6366f1 100%);
  color: #fff;
  padding: 20px 16px 16px;
  text-align: center;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

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

.header-content h1 {
  font-size: 1.75rem;
  font-weight: 900;
  letter-spacing: -0.02em;
}

.header-icon {
  font-size: 1.5rem;
}

.header-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.6);
  background: rgba(255,255,255,0.2);
  object-fit: cover;
}

/* =============================================
   Pages
   ============================================= */
.app-content {
  max-width: 720px;
  margin: 0 auto;
}

.page {
  display: none;
  padding: 16px;
  animation: fadeIn 0.25s ease;
}

.page.active {
  display: block;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* =============================================
   Bottom Nav
   ============================================= */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--nav-height);
  background: #fff;
  display: flex;
  align-items: stretch;
  justify-content: space-around;
  border-top: 1px solid var(--gray-200);
  z-index: 200;
  padding-bottom: env(safe-area-inset-bottom, 0);
  box-shadow: 0 -2px 8px rgba(0,0,0,0.06);
}

.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: none;
  border: none;
  color: var(--gray-400);
  font-size: 0.7rem;
  font-weight: 600;
  cursor: pointer;
  min-height: var(--tap-min);
  user-select: none;
  position: relative;
  transition: color 0.15s;
}

.nav-btn.active {
  color: #4f46e5;
}

.nav-btn.active::after {
  content: "";
  position: absolute;
  top: 0;
  left: 25%;
  right: 25%;
  height: 3px;
  background: #4f46e5;
  border-radius: 0 0 4px 4px;
}

.nav-icon {
  font-size: 1.4rem;
  line-height: 1;
}

.nav-label {
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* =============================================
   Section Title
   ============================================= */
.section-title {
  font-size: 1.125rem;
  font-weight: 700;
  color: var(--gray-700);
  margin-bottom: 12px;
  margin-top: 8px;
}

/* =============================================
   Score Cards
   ============================================= */
.score-cards {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin-bottom: 16px;
}

@media (min-width: 500px) {
  .score-cards {
    grid-template-columns: 1fr 1fr;
  }
}

.score-card {
  background: #fff;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.score-card-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  color: #fff;
}

.child-icon {
  font-size: 2rem;
}

.child-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.5);
  background: rgba(255,255,255,0.2);
  object-fit: cover;
}

.child-avatar-sm {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  vertical-align: middle;
  margin-right: 4px;
}

.child-name {
  font-size: 1.1rem;
  font-weight: 700;
  flex: 1;
}

.child-total {
  font-size: 1.5rem;
  font-weight: 900;
}

.score-card-body {
  padding: 12px 16px 16px;
}

.progress-label {
  font-size: 0.8rem;
  color: var(--gray-500);
  font-weight: 600;
  margin-bottom: 6px;
}

.progress-bar-bg {
  width: 100%;
  height: 12px;
  background: var(--gray-100);
  border-radius: var(--radius-pill);
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  border-radius: var(--radius-pill);
  transition: width 0.4s cubic-bezier(0.4,0,0.2,1);
  min-width: 4px;
}

.progress-bar-fill.joint {
  background: linear-gradient(90deg, var(--blue), var(--purple)) !important;
}

.vouchers-earned, .days-earned {
  font-size: 0.85rem;
  color: var(--gray-500);
  margin-top: 8px;
}

/* =============================================
   Joint Goal
   ============================================= */
.joint-goal-card {
  background: #fff;
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 20px;
  border: 2px dashed #818cf8;
  box-shadow: var(--shadow);
  text-align: center;
}

.joint-goal-card h3 {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 4px;
}

.joint-goal-card p {
  font-size: 0.85rem;
  color: var(--gray-500);
  margin-bottom: 8px;
}

/* =============================================
   Today's Tasks
   ============================================= */
.child-tasks {
  background: #fff;
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
  border-left: 4px solid var(--gray-300);
}

.child-tasks h3 {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 10px;
}

.task-list {
  list-style: none;
}

.task-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 4px;
  border-bottom: 1px solid var(--gray-100);
  min-height: var(--tap-min);
  flex-wrap: wrap;
}

.task-item:last-child {
  border-bottom: none;
}

.task-item.done {
  opacity: 0.5;
}

.task-item label {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  cursor: pointer;
  min-height: var(--tap-min);
}

.task-item input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 28px;
  height: 28px;
  min-width: 28px;
  border: 3px solid var(--gray-300);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  transition: all 0.15s;
}

.task-item input[type="checkbox"]:checked {
  background: var(--green);
  border-color: var(--green);
}

.task-item input[type="checkbox"]:checked::after {
  content: "\2713";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  font-size: 1rem;
  font-weight: 800;
}

.task-name {
  font-size: 0.95rem;
  font-weight: 500;
  flex: 1;
}

.task-points {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--green);
  white-space: nowrap;
}

.task-points.negative {
  color: var(--red);
}

.task-count {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--gray-500);
  padding: 2px 8px;
  background: var(--gray-100);
  border-radius: var(--radius-pill);
}

/* Small buttons */
.btn-small {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  height: 32px;
  border: none;
  border-radius: 6px;
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  padding: 0 8px;
  transition: all 0.15s;
}

.btn-small.btn-add {
  background: var(--blue);
  color: #fff;
  font-size: 1.1rem;
}

.btn-small.btn-undo {
  background: var(--gray-100);
  color: var(--gray-500);
  font-size: 1rem;
}

.btn-small.btn-undo:hover {
  background: var(--gray-200);
}

.btn-small.btn-negative {
  background: var(--red);
  color: #fff;
}

.btn-small.btn-delete {
  background: var(--gray-100);
  color: var(--red);
  font-size: 1.2rem;
}

.btn-small.btn-delete:hover {
  background: #fee2e2;
}

/* Deductions */
.deductions {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 2px dashed #fca5a5;
}

.deductions h4 {
  font-size: 0.85rem;
  color: var(--red);
  font-weight: 700;
  margin-bottom: 8px;
}

.deduction-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  flex-wrap: wrap;
}

.deduction-logged {
  font-size: 0.75rem;
  color: var(--red);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: #fee2e2;
  padding: 2px 8px;
  border-radius: var(--radius-pill);
}

/* =============================================
   History
   ============================================= */
.history-filters {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.filter-tab {
  padding: 8px 16px;
  border: 2px solid var(--gray-200);
  border-radius: var(--radius-pill);
  background: #fff;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  min-height: var(--tap-min);
}

.filter-tab.active {
  background: var(--gray-900);
  color: #fff;
  border-color: var(--gray-900);
}

.history-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap-min);
  padding: 8px 20px;
  border: 2px solid var(--gray-200);
  border-radius: var(--radius);
  font-family: var(--font);
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  background: #fff;
  color: var(--gray-700);
  transition: all 0.15s;
}

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

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

.btn-save, .btn.btn-save {
  background: var(--green);
  color: #fff;
  border-color: var(--green);
}

.btn-cancel, .btn.btn-cancel {
  background: var(--gray-100);
  color: var(--gray-700);
  border-color: var(--gray-200);
}

.btn-danger, .btn.btn-danger {
  background: var(--red);
  color: #fff;
  border-color: var(--red);
}

.history-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.85rem;
  background: #fff;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.history-table thead th {
  background: var(--gray-900);
  color: #fff;
  padding: 10px 12px;
  text-align: left;
  font-weight: 700;
  white-space: nowrap;
  position: sticky;
  top: 0;
}

.history-table td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--gray-100);
  white-space: nowrap;
}

.history-table tr:hover td {
  background: var(--gray-50);
}

td.positive {
  color: var(--green);
  font-weight: 700;
}

td.negative {
  color: var(--red);
  font-weight: 700;
}

.empty-state {
  text-align: center;
  color: var(--gray-400);
  padding: 40px 16px;
  font-style: italic;
}

/* Responsive table wrapper */
#history-content {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* =============================================
   Modal (manual entry)
   ============================================= */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal {
  background: #fff;
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.25);
}

.modal h3 {
  font-size: 1.2rem;
  font-weight: 800;
  margin-bottom: 16px;
  text-align: center;
}

.modal label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--gray-700);
  margin-bottom: 12px;
}

.modal input,
.modal select {
  display: block;
  width: 100%;
  min-height: var(--tap-min);
  padding: 8px 12px;
  border: 2px solid var(--gray-300);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  margin-top: 4px;
  font-family: var(--font);
}

.modal input:focus,
.modal select:focus {
  outline: none;
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.15);
}

.modal-buttons {
  display: flex;
  gap: 8px;
  margin-top: 20px;
}

.modal-buttons .btn {
  flex: 1;
}

/* =============================================
   Goals Page
   ============================================= */
.goal-card {
  background: #fff;
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.goal-card h3 {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 8px;
}

.goal-stats {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
  color: var(--gray-500);
  margin-bottom: 8px;
}

#goals-content h2 {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--gray-700);
  margin: 20px 0 8px;
}

#goals-content h2:first-child {
  margin-top: 0;
}

#goals-content > p {
  font-size: 0.85rem;
  color: var(--gray-500);
  margin-bottom: 12px;
}

.joint-breakdown {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 8px;
  font-size: 0.85rem;
  font-weight: 600;
}

.joint-breakdown h4 {
  font-size: 0.8rem;
  color: var(--gray-500);
  margin-bottom: 2px;
}

.contribution-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.child-avatar-xs {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  vertical-align: middle;
}

.redemption-history {
  margin-top: 12px;
  font-size: 0.85rem;
}

.redemption-history h4 {
  font-size: 0.8rem;
  color: var(--gray-500);
  margin-bottom: 4px;
}

.redemption-history ul {
  list-style: disc;
  padding-left: 20px;
}

.redemption-history li {
  margin-bottom: 2px;
  color: var(--gray-600);
}

/* =============================================
   Responsive & Accessibility
   ============================================= */

@supports (padding: env(safe-area-inset-bottom)) {
  .bottom-nav {
    padding-bottom: env(safe-area-inset-bottom);
    height: calc(var(--nav-height) + env(safe-area-inset-bottom));
  }
  body {
    padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom) + 16px);
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* =============================================
   Read-only / TV Display Mode
   ============================================= */
.task-status {
  font-size: 1.4rem;
  line-height: 1;
  min-width: 28px;
  text-align: center;
}

body.read-only .history-actions,
body.read-only .btn-small.btn-undo,
body.read-only .btn-small.btn-delete,
body.read-only .btn-small.btn-negative,
body.read-only .btn-small.btn-add {
  display: none !important;
}
