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

/* Prevent horizontal overflow globally */
html, body {
  overflow-x: hidden;
  max-width: 100vw;
}

/* Base */
html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow-x: hidden;
}

body {
  font-family: var(--font-family);
  font-weight: var(--font-regular);
  background-color: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.5;
  min-height: 100vh;
  width: 100%;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

/* Body scroll lock when modal is open */
body.modal-open {
  overflow: hidden;
  position: fixed;
  width: 100%;
  height: 100%;
}

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  max-width: 600px;
  width: 100%;
  margin: 0 auto;
  overflow-x: hidden;
}

/* Typography */
h1 {
  font-size: 1.5rem;
  font-weight: var(--font-medium);
  letter-spacing: -0.02em;
}

h2 {
  font-size: 1.25rem;
  font-weight: var(--font-medium);
  letter-spacing: -0.01em;
}

h3 {
  font-size: 1rem;
  font-weight: var(--font-medium);
}

p {
  color: var(--text-muted);
}

/* Views */
.view {
  display: none;
  flex-direction: column;
  min-height: 100vh;
  padding: var(--space-3);
  padding-bottom: calc(var(--space-6) + 60px);
  opacity: 0;
  transform: translateY(8px);
  transition:
    opacity var(--duration-fast) var(--ease-out),
    transform var(--duration-fast) var(--ease-out);
  max-width: 100%;
  overflow-x: hidden;
}

.view.active {
  display: flex;
  opacity: 1;
  transform: translateY(0);
}

.view.fading-out {
  opacity: 0;
  transform: translateY(-8px);
}

#main-app {
  display: flex;
  flex-direction: column;
  flex: 1;
}

#main-app.hidden {
  display: none;
}

/* Buttons */
button {
  font-family: inherit;
  font-size: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
  transition: all var(--duration-normal) var(--ease-out);
}

.btn-primary {
  background-color: var(--text-primary);
  color: var(--bg-primary);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-md);
  font-weight: var(--font-medium);
}

.btn-primary:hover {
  opacity: 0.9;
  transform: translateY(-1px);
}

.btn-primary:active {
  transform: translateY(0) scale(0.98);
}

.btn-secondary {
  background-color: var(--bg-tertiary);
  color: var(--text-primary);
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-sm);
}

.btn-secondary:hover {
  background-color: var(--bg-secondary);
}

.btn-secondary:active {
  transform: scale(0.98);
}

/* Form elements */
input, textarea {
  font-family: inherit;
  font-size: 1rem;
  background-color: var(--bg-secondary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: var(--space-2);
  width: 100%;
  transition: border-color var(--duration-fast) var(--ease-out);
}

input:focus, textarea:focus {
  outline: none;
  border-color: var(--text-muted);
}

textarea {
  resize: vertical;
  min-height: 100px;
}

label {
  display: block;
  font-size: 0.875rem;
  color: var(--text-muted);
  margin-bottom: var(--space-1);
  margin-top: var(--space-2);
}

/* Bottom Navigation */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 600px;
  display: flex;
  justify-content: space-around;
  background-color: var(--bg-secondary);
  padding: var(--space-1) 0;
  padding-bottom: calc(var(--space-1) + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border-subtle);
  z-index: 100;
}

.nav-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: var(--space-1);
  color: var(--text-muted);
  min-width: 64px;
}

.nav-btn.active {
  color: var(--text-primary);
}

.nav-icon {
  font-size: 1.25rem;
}

.nav-label {
  font-size: 0.625rem;
  font-weight: var(--font-medium);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  min-height: 10px; /* Ensure minimum readable size */
}

/* Nav Badge for notifications */
.nav-btn {
  position: relative;
}

.nav-badge {
  position: absolute;
  top: 2px;
  right: 8px;
  width: 8px;
  height: 8px;
  background-color: #e57373;
  border-radius: 50%;
  animation: nav-badge-pulse 2s ease-in-out infinite;
}

.nav-badge.hidden {
  display: none;
}

@keyframes nav-badge-pulse {
  0%, 100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.2);
    opacity: 0.7;
  }
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-3);
  z-index: 200;
  opacity: 1;
  transition: opacity var(--duration-normal) var(--ease-out);
}

.modal.hidden {
  display: none;
}

.modal-content {
  background-color: var(--bg-secondary);
  border-radius: var(--radius-lg);
  padding: var(--space-3);
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-soft);
}

.modal-content h3 {
  margin-bottom: var(--space-2);
}

.modal-actions {
  display: flex;
  gap: var(--space-2);
  margin-top: var(--space-3);
  justify-content: flex-end;
}

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

/* Scrollbar */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--bg-tertiary);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-subtle);
}

/* =============================================
   RESPONSIVE / MOBILE STYLES
   ============================================= */

/* Touch device detection */
@media (hover: none) and (pointer: coarse) {
  /* Active state feedback for all buttons */
  button:active,
  .btn-primary:active,
  .btn-secondary:active {
    transform: scale(0.96);
  }

  .nav-btn:active {
    transform: scale(0.95);
    background-color: var(--bg-tertiary);
  }

  /* Remove hover effects that don't work well on touch */
  .btn-primary:hover {
    transform: none;
  }
}

/* Tablet breakpoint */
@media (max-width: 768px) {
  .view {
    padding: var(--space-2);
    padding-bottom: calc(var(--space-5) + 70px);
  }

  h1 {
    font-size: 1.375rem;
  }

  h2 {
    font-size: 1.125rem;
  }

  .modal-content {
    max-width: calc(100vw - 32px);
    max-height: calc(100vh - 32px);
    overflow-y: auto;
  }
}

/* Phone breakpoint */
@media (max-width: 480px) {
  .view {
    padding: var(--space-1) var(--space-2);
    padding-bottom: calc(var(--space-4) + 60px);
  }

  h1 {
    font-size: 1.25rem;
  }

  h2 {
    font-size: 1rem;
  }

  h3 {
    font-size: 0.9375rem;
  }

  /* Larger touch targets for buttons */
  button {
    min-height: 44px;
  }

  .btn-primary,
  .btn-secondary {
    padding: var(--space-2) var(--space-3);
    min-height: 44px;
  }

  /* Modals - safe area and viewport constraints */
  .modal {
    padding: var(--space-2);
    padding-top: calc(var(--space-2) + env(safe-area-inset-top));
    padding-bottom: calc(var(--space-2) + env(safe-area-inset-bottom));
  }

  .modal-content {
    max-width: calc(100vw - 24px);
    max-height: calc(100vh - 48px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    padding: var(--space-2);
  }

  .modal-actions {
    flex-wrap: wrap;
    gap: var(--space-1);
  }

  .modal-actions button {
    flex: 1;
    min-width: 100px;
  }

  /* Modal close button - ensure 44px touch target */
  .modal-close,
  .week-details-close,
  .close-history {
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* Bottom nav adjustments */
  .bottom-nav {
    padding: var(--space-1) var(--space-1);
    padding-bottom: calc(var(--space-1) + env(safe-area-inset-bottom));
  }

  .nav-btn {
    min-width: 52px;
    padding: 8px 4px;
    min-height: 52px;
  }

  .nav-icon {
    font-size: 1.125rem;
  }

  .nav-label {
    font-size: 0.5625rem;
  }

  /* Form elements - larger touch targets */
  input, textarea {
    padding: var(--space-2);
    font-size: 16px; /* Prevents iOS zoom on focus */
    min-height: 44px;
  }

  textarea {
    min-height: 80px;
  }
}

/* Small phone breakpoint */
@media (max-width: 320px) {
  .view {
    padding: var(--space-1);
    padding-bottom: calc(var(--space-4) + 65px);
  }

  h1 {
    font-size: 1.125rem;
  }

  h2 {
    font-size: 0.9375rem;
  }

  h3 {
    font-size: 0.875rem;
  }

  /* Ensure minimum readable text size */
  p, span, label {
    font-size: max(0.75rem, 12px);
  }

  .modal-content {
    max-width: calc(100vw - 16px);
    padding: var(--space-2);
  }

  .nav-btn {
    min-width: 44px;
    padding: 6px 2px;
  }

  .nav-label {
    font-size: 0.5rem;
  }

  /* Ensure buttons remain tappable */
  button {
    min-height: 40px;
  }

  .btn-primary,
  .btn-secondary {
    min-height: 40px;
    padding: var(--space-1) var(--space-2);
  }
}
