/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #0891b2;
  --primary-dark: #0e7490;
  --primary-light: #ecfeff;
  --accent: #f59e0b;
  --accent-light: #fef3c7;
  --success: #10b981;
  --success-light: #d1fae5;
  --danger: #ef4444;
  --danger-light: #fee2e2;
  --muted: #94a3b8;
  --muted-light: #f1f5f9;
  --bg: #f0f9ff;
  --card: #ffffff;
  --text: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --border: #e2e8f0;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.12);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-xs: 6px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
}

html { font-size: 16px; scroll-behavior: smooth; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* ===== App Shell ===== */
.app-container {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 16px 100px;
  min-height: 100vh;
}

/* ===== Header ===== */
.app-header {
  background: linear-gradient(135deg, var(--primary-dark), var(--primary));
  color: white;
  padding: 20px 0 0;
  position: sticky;
  top: 0;
  z-index: 50;
}

.header-inner {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.app-title {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.app-title .icon { margin-right: 6px; }

.user-badge {
  font-size: 0.8rem;
  background: rgba(255,255,255,0.2);
  padding: 4px 12px;
  border-radius: 20px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 4px;
}

.user-badge .edit-name {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  font-size: 0.75rem;
  padding: 2px;
  opacity: 0.7;
  transition: opacity 0.2s;
}

.user-badge .edit-name:hover { opacity: 1; }

/* ===== Tab Bar ===== */
.tab-bar {
  display: flex;
  gap: 0;
  max-width: 640px;
  margin: 0 auto;
  padding: 0 16px;
  background: transparent;
  border-bottom: none;
}

.tab-btn {
  flex: 1;
  padding: 12px 8px 10px;
  text-align: center;
  font-size: 0.85rem;
  font-weight: 600;
  color: rgba(255,255,255,0.65);
  background: transparent;
  border: none;
  cursor: pointer;
  border-bottom: 3px solid transparent;
  transition: all 0.2s;
  position: relative;
}

.tab-btn:hover { color: rgba(255,255,255,0.9); }

.tab-btn.active {
  color: white;
  border-bottom-color: white;
}

.tab-btn .tab-icon {
  display: block;
  font-size: 1.1rem;
  margin-bottom: 2px;
}

/* ===== Tab Content ===== */
.tab-content { display: none; padding-top: 20px; }
.tab-content.active { display: block; }

/* ===== Cards ===== */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 16px;
  border: 1px solid var(--border);
  transition: box-shadow 0.2s;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.card-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
}

.card-subtitle {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* ===== Medicine Slots (Today Tab) ===== */
.slot-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.slot-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 600;
  border: 2px solid var(--border);
  background: var(--card);
  cursor: pointer;
  transition: all 0.2s;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.slot-btn:hover { border-color: var(--muted); }

.slot-btn.pending {
  border-color: var(--border);
  color: var(--text-secondary);
  background: var(--muted-light);
}

.slot-btn.taken {
  border-color: var(--success);
  color: var(--success);
  background: var(--success-light);
}

.slot-btn.missed {
  border-color: var(--danger);
  color: var(--danger);
  background: var(--danger-light);
  cursor: default;
}

.slot-btn .status-icon { font-size: 1rem; }

/* ===== Fiber Toggle ===== */
.fiber-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
}

.fiber-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 0.95rem;
}

.fiber-toggle {
  position: relative;
  width: 52px;
  height: 28px;
  background: var(--border);
  border-radius: 14px;
  cursor: pointer;
  transition: background 0.3s;
  border: none;
  padding: 0;
  flex-shrink: 0;
}

.fiber-toggle.taken {
  background: var(--success);
}

.fiber-toggle::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  background: white;
  border-radius: 50%;
  transition: transform 0.3s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.fiber-toggle.taken::after {
  transform: translateX(24px);
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 20px;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
}

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

.btn-primary {
  background: var(--primary);
  color: white;
}
.btn-primary:hover { background: var(--primary-dark); }

.btn-success {
  background: var(--success);
  color: white;
}
.btn-success:hover { background: #059669; }

.btn-danger {
  background: var(--danger);
  color: white;
}
.btn-danger:hover { background: #dc2626; }

.btn-outline {
  background: transparent;
  border: 2px solid var(--border);
  color: var(--text-secondary);
}
.btn-outline:hover { border-color: var(--muted); color: var(--text); }

.btn-google {
  background: #ffffff;
  color: #1f2937;
  border: 2px solid #d1d5db;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn-google:hover {
  background: #f9fafb;
  border-color: #9ca3af;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 0.8rem;
}

.btn-full {
  width: 100%;
  justify-content: center;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* ===== Save Status ===== */
.save-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  z-index: 100;
  box-shadow: 0 -4px 12px rgba(0,0,0,0.08);
}

.save-bar-inner {
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.save-status {
  font-size: 0.85rem;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 6px;
}

.save-status.saved { color: var(--success); }
.save-status.error { color: var(--danger); }

/* ===== Medicines List ===== */
.medicine-card {
  border-left: 4px solid var(--primary);
  padding: 16px 20px;
}

.medicine-card.inactive {
  border-left-color: var(--muted);
  opacity: 0.7;
}

.medicine-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.medicine-name {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
}

.medicine-slots {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.slot-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--primary-light);
  color: var(--primary-dark);
}

.slot-tag.once { background: var(--accent-light); color: #92400e; }

.medicine-meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 6px;
}

.medicine-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.medicine-actions .btn {
  padding: 4px 8px;
  font-size: 0.75rem;
}

/* ===== Status Toggle (active/inactive) ===== */
.status-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}

.toggle-switch {
  position: relative;
  width: 40px;
  height: 22px;
  background: var(--border);
  border-radius: 11px;
  cursor: pointer;
  transition: background 0.3s;
  border: none;
  padding: 0;
}

.toggle-switch.active { background: var(--success); }

.toggle-switch::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  background: white;
  border-radius: 50%;
  transition: transform 0.3s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.toggle-switch.active::after { transform: translateX(18px); }

.status-label {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-secondary);
}

/* ===== Modal ===== */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 200;
  align-items: flex-end;
  justify-content: center;
  backdrop-filter: blur(4px);
}

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

.modal {
  background: white;
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-width: 640px;
  max-height: 90vh;
  overflow-y: auto;
  padding: 24px 20px 32px;
  animation: slideUp 0.3s ease;
}

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

.modal-handle {
  width: 40px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 0 auto 20px;
}

.modal-title {
  font-size: 1.2rem;
  font-weight: 700;
  margin-bottom: 20px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 24px;
}

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

/* ===== Form Fields ===== */
.form-group {
  margin-bottom: 18px;
}

.form-label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.form-input {
  width: 100%;
  padding: 10px 14px;
  border: 2px solid var(--border);
  border-radius: var(--radius-xs);
  font-size: 1rem;
  color: var(--text);
  background: var(--card);
  transition: border-color 0.2s;
  font-family: var(--font);
}

.form-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(8,145,178,0.12);
}

.form-hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 500;
  border: 2px solid var(--border);
  cursor: pointer;
  transition: all 0.2s;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.checkbox-label.checked {
  border-color: var(--primary);
  background: var(--primary-light);
  color: var(--primary-dark);
}

.checkbox-label input { display: none; }

.duration-group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.duration-group .form-input {
  width: 80px;
  text-align: center;
}

.duration-label {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

/* ===== Stats ===== */
.stats-summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-bottom: 20px;
}

.stat-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  text-align: center;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.stat-value {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--primary);
  line-height: 1.2;
}

.stat-label {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-muted);
  margin-top: 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-bar {
  height: 6px;
  background: var(--muted-light);
  border-radius: 3px;
  margin-top: 10px;
  overflow: hidden;
}

.stat-bar-fill {
  height: 100%;
  border-radius: 3px;
  background: var(--success);
  transition: width 0.6s ease;
}

.stat-bar-fill.medium { background: var(--accent); }
.stat-bar-fill.low { background: var(--danger); }

.stats-table {
  width: 100%;
  border-collapse: collapse;
}

.stats-table th {
  text-align: left;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 8px 12px;
  border-bottom: 2px solid var(--border);
}

.stats-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

.stats-table tr:last-child td { border-bottom: none; }

.stats-med-name {
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}

.stats-med-name .status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.stats-med-name .status-dot.active { background: var(--success); }
.stats-med-name .status-dot.inactive { background: var(--muted); }

.stats-pct {
  font-weight: 700;
  text-align: right;
}

.stats-pct.good { color: var(--success); }
.stats-pct.ok { color: var(--accent); }
.stats-pct.bad { color: var(--danger); }

.stats-empty {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
}

.stats-empty .empty-icon { font-size: 2.5rem; margin-bottom: 12px; }

/* ===== Name Prompt ===== */
.name-prompt-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 300;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(6px);
}

.name-prompt-overlay.open { display: flex; }

.name-prompt {
  background: white;
  border-radius: 20px;
  padding: 32px 28px;
  max-width: 380px;
  width: 90%;
  text-align: center;
  animation: fadeIn 0.3s ease;
  box-shadow: var(--shadow-lg);
}

@keyframes fadeIn {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}

.name-prompt-icon {
  font-size: 3rem;
  margin-bottom: 12px;
}

.name-prompt h2 {
  font-size: 1.3rem;
  font-weight: 700;
  margin-bottom: 6px;
}

.name-prompt p {
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-bottom: 20px;
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: white;
  padding: 10px 20px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 500;
  z-index: 250;
  opacity: 0;
  transition: all 0.3s;
  pointer-events: none;
  white-space: nowrap;
}

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

.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* ===== Loading Spinner ===== */
.loading-screen {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 400;
  transition: opacity 0.4s;
}

.loading-screen.hidden {
  opacity: 0;
  pointer-events: none;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 4px solid var(--muted-light);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.loading-text {
  margin-top: 12px;
  font-size: 0.9rem;
  color: var(--text-secondary);
}

/* ===== Responsive ===== */
@media (min-width: 480px) {
  .app-container { padding: 0 24px 100px; }
  .header-inner { padding: 0 24px; }
  .tab-bar { padding: 0 24px; }
  .save-bar { padding: 12px 24px; }
}

@media (min-width: 640px) {
  .modal-overlay { align-items: center; }
  .modal {
    border-radius: 20px;
    max-height: 80vh;
  }
  .modal-handle { display: none; }
  .stats-summary { grid-template-columns: repeat(3, 1fr); }
}

/* ===== Print ===== */
@media print {
  .app-header { position: static; }
  .tab-bar, .save-bar { display: none; }
  .tab-content { display: block !important; }
}

/* ===== Today Header ===== */
.today-header {
  margin-bottom: 20px;
}

.today-date {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.today-greeting {
  font-size: 1.4rem;
  font-weight: 700;
  margin-top: 2px;
  color: var(--text);
}

/* ===== Empty state for medicines tab ===== */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
}

.empty-state .empty-icon { font-size: 2.5rem; margin-bottom: 12px; }
.empty-state p { margin-bottom: 16px; }

/* ===== Day summary pill in today tab ===== */
.day-summary {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.summary-pill {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 16px;
  font-size: 0.8rem;
  font-weight: 600;
}

.summary-pill.done { background: var(--success-light); color: #065f46; }
.summary-pill.pending-badge { background: var(--muted-light); color: var(--text-secondary); }
.summary-pill.missed-badge { background: var(--danger-light); color: #991b1b; }

/* ===== Medicine detail row ===== */
.medicine-detail-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 0;
}

.medicine-detail-name {
  font-weight: 600;
  font-size: 0.95rem;
}