/* Apex Protocol - Mobile-first design system
 * Inspired by MacroFactor / EverFit / Whoop app feels.
 * Dark theme, big touch targets, card-based.
 */

:root {
  /* Surfaces */
  --bg: #0a0e14;
  --bg-elev: #161b22;
  --bg-elev-2: #1c2128;
  --bg-card: #161b22;

  /* Text */
  --text: #e6edf3;
  --text-dim: #8b949e;
  --text-muted: #6e7681;

  /* Borders */
  --border: #30363d;
  --border-soft: rgba(255, 255, 255, 0.06);

  /* Accent */
  --accent: #1a73e8;
  --accent-hover: #1557b0;
  --accent-soft: rgba(26, 115, 232, 0.15);

  /* Status colors (Whoop-inspired) */
  --green: #2ea043;
  --green-soft: rgba(46, 160, 67, 0.15);
  --yellow: #d29922;
  --yellow-soft: rgba(210, 153, 34, 0.15);
  --red: #da3633;
  --red-soft: rgba(218, 54, 51, 0.15);

  /* Typography */
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;

  /* Spacing */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 20px;
  --sp-6: 24px;
  --sp-8: 32px;

  /* Radius */
  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 14px;
  --r-pill: 999px;

  /* Layout */
  --bottom-nav-h: 64px;
  --header-h: 56px;
  --safe-bottom: env(safe-area-inset-bottom, 0);

  /* Shadows */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.3);
}

/* ===== VIEW TRANSITIONS (smooth page nav) ===== */
@view-transition {
  navigation: auto;
}

::view-transition-old(root),
::view-transition-new(root) {
  animation-duration: 0.22s;
  animation-timing-function: cubic-bezier(0.32, 0.72, 0.24, 1);
}

/* Default transition : smooth fade + subtle scale */
::view-transition-old(root) {
  animation-name: apex-fade-out;
}
::view-transition-new(root) {
  animation-name: apex-fade-in;
}

@keyframes apex-fade-out {
  to { opacity: 0; transform: scale(0.98); }
}
@keyframes apex-fade-in {
  from { opacity: 0; transform: scale(1.01); }
}

/* Swipe directional : if body[data-nav-dir] set before navigation */
body[data-nav-dir="next"] ~ ::view-transition-old(root) { animation-name: apex-slide-out-left; }
body[data-nav-dir="next"] ~ ::view-transition-new(root) { animation-name: apex-slide-in-right; }
body[data-nav-dir="prev"] ~ ::view-transition-old(root) { animation-name: apex-slide-out-right; }
body[data-nav-dir="prev"] ~ ::view-transition-new(root) { animation-name: apex-slide-in-left; }

@keyframes apex-slide-out-left { to { transform: translateX(-30%); opacity: 0; } }
@keyframes apex-slide-in-right { from { transform: translateX(30%); opacity: 0; } }
@keyframes apex-slide-out-right { to { transform: translateX(30%); opacity: 0; } }
@keyframes apex-slide-in-left { from { transform: translateX(-30%); opacity: 0; } }

/* ===== RESET ===== */

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--font);
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  overscroll-behavior-y: none;
}

a { color: var(--accent); text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; font-size: 16px; /* prevent iOS zoom */ }

/* ===== APP LAYOUT ===== */

.app-header {
  position: sticky;
  top: 0;
  z-index: 50;
  height: var(--header-h);
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border-soft);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--sp-4);
  font-weight: 600;
  font-size: 17px;
}

.app-header .title { display: flex; align-items: center; gap: var(--sp-2); }
.app-header .title .logo { font-size: 20px; }
.app-header .actions { display: flex; gap: var(--sp-2); align-items: center; }

.app-main {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-4);
  padding-bottom: calc(var(--bottom-nav-h) + var(--safe-bottom) + var(--sp-4));
  -webkit-overflow-scrolling: touch;
}

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(var(--bottom-nav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bg-elev);
  border-top: 1px solid var(--border-soft);
  display: flex;
  z-index: 1000;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.3);
  pointer-events: auto;
}

.bottom-nav .nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: transparent;
  border: none;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 11px;
  font-weight: 500;
  padding: 6px 0;
  transition: color 0.15s;
  position: relative;
  cursor: pointer;
  pointer-events: auto;
  -webkit-tap-highlight-color: rgba(26, 115, 232, 0.2);
  touch-action: manipulation;
  user-select: none;
}

.bottom-nav .nav-item:active {
  background: rgba(255, 255, 255, 0.05);
}

.bottom-nav .nav-item .icon {
  font-size: 22px;
  line-height: 1;
}

.bottom-nav .nav-item.active {
  color: var(--accent);
}

.bottom-nav .nav-item.active::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 24px;
  height: 3px;
  background: var(--accent);
  border-radius: 0 0 2px 2px;
}

/* ===== STATUS DOT (header) ===== */
.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--text-muted);
  display: inline-block;
}
.status-dot.ok { background: var(--green); box-shadow: 0 0 8px var(--green-soft); }
.status-dot.pending { background: var(--yellow); }
.status-dot.err { background: var(--red); }

/* ===== CARDS ===== */

.card {
  background: var(--bg-card);
  border: 1px solid var(--border-soft);
  border-radius: var(--r-lg);
  padding: var(--sp-4);
  margin-bottom: var(--sp-3);
}

.card-tight { padding: var(--sp-3); }
.card-flat { background: var(--bg-elev); }
.card h2, .card h3 {
  margin: 0 0 var(--sp-3);
  font-size: 14px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.card .stat-big {
  font-size: 36px;
  font-weight: 700;
  line-height: 1;
  letter-spacing: -1px;
}
.card .stat-unit {
  color: var(--text-dim);
  font-size: 13px;
  margin-left: var(--sp-1);
}
.card .stat-sub {
  color: var(--text-dim);
  font-size: 13px;
  margin-top: var(--sp-2);
}

/* Grid 2-cols cards */
.card-grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--sp-3);
}
.card-grid-2 .card { margin-bottom: 0; }

/* Status bar (recovery/strain visual) */
.score-ring {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 70px;
  height: 70px;
  border-radius: 50%;
  font-size: 20px;
  font-weight: 700;
  border: 4px solid var(--border);
}
.score-ring.green { border-color: var(--green); color: var(--green); }
.score-ring.yellow { border-color: var(--yellow); color: var(--yellow); }
.score-ring.red { border-color: var(--red); color: var(--red); }

/* ===== BUTTONS ===== */

.btn {
  min-height: 48px;
  padding: 0 var(--sp-5);
  border-radius: var(--r-md);
  border: none;
  font-size: 15px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  transition: opacity 0.15s, transform 0.05s;
  text-decoration: none;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; pointer-events: none; }

.btn-primary { background: var(--accent); color: white; }
.btn-primary:active { background: var(--accent-hover); }
.btn-secondary { background: var(--bg-elev-2); color: var(--text); border: 1px solid var(--border); }
.btn-ghost { background: transparent; color: var(--text); }
.btn-danger { background: var(--red); color: white; }

.btn-block { width: 100%; }
.btn-sm { min-height: 36px; padding: 0 var(--sp-3); font-size: 13px; }

/* FAB - Floating Action Button */
.fab {
  position: fixed;
  bottom: calc(var(--bottom-nav-h) + var(--safe-bottom) + var(--sp-4));
  right: var(--sp-4);
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  border: none;
  padding: 0;
  font-size: 24px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  box-shadow: var(--shadow-md);
  z-index: 90;
}
.fab:active { transform: scale(0.93); }
.fab > * { display: block; line-height: 1; }

/* ===== INPUTS ===== */

.input, textarea, select {
  width: 100%;
  min-height: 48px;
  padding: var(--sp-3) var(--sp-4);
  background: var(--bg-elev-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s;
}
.input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
}
textarea { min-height: 88px; resize: vertical; padding-top: var(--sp-3); }

.input-label {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: var(--sp-1);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  font-weight: 600;
}

.input-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--sp-3);
  margin-bottom: var(--sp-3);
}

/* Pills (selectable tags) */
.pills {
  display: flex;
  gap: var(--sp-2);
  flex-wrap: wrap;
  margin-bottom: var(--sp-3);
}
.pill {
  min-height: 36px;
  padding: 0 var(--sp-4);
  border-radius: var(--r-pill);
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  cursor: pointer;
}
.pill.active { background: var(--accent); border-color: var(--accent); color: white; }

/* ===== PHOTO ZONE ===== */

.photo-zone {
  border: 2px dashed var(--border);
  border-radius: var(--r-lg);
  padding: var(--sp-6) var(--sp-4);
  text-align: center;
  background: var(--bg-elev);
  cursor: pointer;
  margin-bottom: var(--sp-3);
  min-height: 180px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
}
.photo-zone:active { background: var(--bg-elev-2); }
.photo-zone img { max-width: 100%; max-height: 280px; border-radius: var(--r-md); }
.photo-zone .icon { font-size: 36px; opacity: 0.7; }
.photo-zone .hint { color: var(--text-dim); font-size: 13px; }
.file-input { display: none; }

/* ===== TABS ===== */

.tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
  padding: 0 var(--sp-4);
  position: fixed;
  top: var(--header-h);
  left: 0;
  right: 0;
  background-color: #0a0e14;
  z-index: 40;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
  height: 48px;
}

/* Push content below fixed tabs */
body:has(.tabs) .app-main {
  padding-top: calc(48px + var(--sp-4));
}
.tab {
  flex: 1;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--text-dim);
  font-size: 14px;
  font-weight: 600;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
  height: 48px;
}
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* Desktop adapt - tabs centered with max-width */
@media (min-width: 768px) {
  .tabs { left: 50%; transform: translateX(-50%); max-width: 720px; }
}

.panel { display: none; }
.panel.active { display: block; }

/* ===== LISTS ===== */

.list-item {
  background: var(--bg-card);
  border: 1px solid var(--border-soft);
  border-radius: var(--r-md);
  padding: var(--sp-3) var(--sp-4);
  margin-bottom: var(--sp-2);
  display: flex;
  align-items: center;
  gap: var(--sp-3);
}
.list-item .body { flex: 1; min-width: 0; }
.list-item .title-row { font-weight: 600; font-size: 14px; }
.list-item .meta { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
.list-item .right { color: var(--text-dim); font-size: 13px; }

/* Macros mini display */
.macros-mini {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text);
}
.macros-mini .sep { color: var(--text-muted); margin: 0 4px; }

/* Progress bar */
.bar {
  height: 6px;
  background: var(--bg-elev-2);
  border-radius: var(--r-pill);
  overflow: hidden;
  margin-top: var(--sp-1);
}
.bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: var(--r-pill);
  transition: width 0.3s;
}
.bar-fill.green { background: var(--green); }
.bar-fill.yellow { background: var(--yellow); }
.bar-fill.red { background: var(--red); }

/* ===== UTILS ===== */
.row { display: flex; gap: var(--sp-3); align-items: center; }
.row > * { flex: 1; }
.between { display: flex; justify-content: space-between; align-items: center; }
.center { text-align: center; }
.text-dim { color: var(--text-dim); }
.text-sm { font-size: 13px; }
.text-xs { font-size: 12px; }
.mt-1 { margin-top: var(--sp-1); } .mt-2 { margin-top: var(--sp-2); } .mt-3 { margin-top: var(--sp-3); } .mt-4 { margin-top: var(--sp-4); }
.mb-1 { margin-bottom: var(--sp-1); } .mb-2 { margin-bottom: var(--sp-2); } .mb-3 { margin-bottom: var(--sp-3); } .mb-4 { margin-bottom: var(--sp-4); }

/* ===== TOAST ===== */
.toast {
  position: fixed;
  top: calc(var(--header-h) + var(--sp-3));
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: var(--sp-3) var(--sp-4);
  z-index: 200;
  box-shadow: var(--shadow-md);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; pointer-events: auto; transform: translateX(-50%) translateY(0); }
.toast.ok { border-color: var(--green); color: var(--green); }
.toast.err { border-color: var(--red); color: var(--red); }

/* ===== LOADING ===== */
.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== DRAWER (slide-out for conversations) ===== */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 110;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.drawer-backdrop.open { opacity: 1; pointer-events: auto; }

.drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 80%;
  max-width: 320px;
  background: var(--bg-elev);
  z-index: 120;
  transform: translateX(-100%);
  transition: transform 0.25s ease-out;
  display: flex;
  flex-direction: column;
}
.drawer.open { transform: translateX(0); }
.drawer .drawer-header {
  padding: var(--sp-4);
  border-bottom: 1px solid var(--border-soft);
  font-weight: 600;
}
.drawer .drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-3);
}

/* ===== DESKTOP ADAPTATION ===== */
@media (min-width: 768px) {
  .app-main { max-width: 720px; margin: 0 auto; padding: var(--sp-6); }
  .app-header { max-width: 100%; padding: 0 var(--sp-6); }
  .bottom-nav { max-width: 720px; left: 50%; transform: translateX(-50%); border-radius: var(--r-lg) var(--r-lg) 0 0; }
}
