/* ═══════════════════════════════════════════════════════════════
   DESIGN SYSTEM - CUSTOM EASING & VARIABLES
   Following Emil Kowalski's animation principles
   ═══════════════════════════════════════════════════════════════ */

:root {
  /* Custom easing curves - stronger than CSS defaults */
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --ease-in-out: cubic-bezier(0.77, 0, 0.175, 1);

  /* Timing values for consistency */
  --duration-fast: 160ms;
  --duration-normal: 200ms;
  --duration-slow: 300ms;
}

/* ═══════════════════════════════════════════════════════════════
   BASE STYLES
   ═══════════════════════════════════════════════════════════════ */

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

html,
body {
  max-width: 100%;
  overflow-x: hidden;
}

/* On a touch screen a long press on an expense asks to pin it, so it must not select text */
@media (pointer: coarse) {
  [data-pin-id] {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
  }
}

#pin-dialog::backdrop {
  background: rgb(15 23 42 / 0.5);
}

/* ═══════════════════════════════════════════════════════════════
   RTL SELECT STYLING
   ═══════════════════════════════════════════════════════════════ */

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%23475569' viewBox='0 0 16 16'%3E%3Cpath d='M7.247 11.14L2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: left 1rem center;
  padding-left: 2.5rem;
}

/* ═══════════════════════════════════════════════════════════════
   IOS ANTI-ZOOM
   ═══════════════════════════════════════════════════════════════ */

input,
select,
textarea {
  font-size: 16px !important;
}

/* ═══════════════════════════════════════════════════════════════
   BUTTON INTERACTIONS
   Emil's Rule: Buttons must feel responsive with scale(0.97)
   ═══════════════════════════════════════════════════════════════ */

.btn-primary {
  transition: transform var(--duration-fast) var(--ease-out),
              background-color var(--duration-fast) var(--ease-out);
}

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

/* Generic button fallback */
button {
  transition: transform var(--duration-fast) var(--ease-out);
}

button:active {
  transform: scale(0.97);
  transition-duration: 80ms; /* instant on press; the release below is the slower settle */
}

/* Delete button in expense cards */
.delete-btn {
  transition: transform var(--duration-fast) var(--ease-out),
              background-color var(--duration-normal) var(--ease-out);
}

.delete-btn:active {
  transform: scale(0.95);
}

/* ═══════════════════════════════════════════════════════════════
   HOVER STATES - ONLY FOR DEVICES WITH HOVER CAPABILITY
   Prevents false positives on touch devices
   ═══════════════════════════════════════════════════════════════ */

@media (hover: hover) and (pointer: fine) {
  .btn-primary:hover:not(:disabled) {
    opacity: 0.95;
  }

  .delete-btn:hover {
    background-color: rgb(254 242 242);
  }
}

/* ═══════════════════════════════════════════════════════════════
   REDUCED MOTION SUPPORT
   Respects user preference - removes transforms, keeps opacity
   ═══════════════════════════════════════════════════════════════ */

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

  /* Keep button press feedback but remove scale */
  .btn-primary:active,
  button:active,
  .delete-btn:active {
    transform: none;
    opacity: 0.8;
  }
}

/* ═══════════════════════════════════════════════════════════════
   FOCUS STATES - ACCESSIBILITY
   ═══════════════════════════════════════════════════════════════ */

input:focus,
select:focus,
textarea:focus,
button:focus-visible {
  outline: 2px solid #059669;
  outline-offset: 2px;
}

/* ═══════════════════════════════════════════════════════════════
   CATEGORY CARDS - Radio Button Grid
   Emil's Rule: Interactive elements must feel responsive
   ═══════════════════════════════════════════════════════════════ */

/* Category card labels */
input[name="category"] + label {
  transition: all var(--duration-normal) var(--ease-out);
}

/* Hover state (only on devices with hover) */
@media (hover: hover) and (pointer: fine) {
  input[name="category"] + label:hover {
    transform: translateY(-2px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  }
}

/* Active press feedback */
input[name="category"] + label:active {
  transform: scale(0.97);
}

/* Checked state gets no press animation (it's already selected) */
input[name="category"]:checked + label:active {
  transform: none;
}

@media (prefers-reduced-motion: reduce) {
  input[name="category"] + label:hover,
  input[name="category"] + label:active {
    transform: none;
  }
}

/* ═══════════════════════════════════════════════════════════════
   SAVINGS CATEGORY CARDS & TRANSACTION TYPE
   ═══════════════════════════════════════════════════════════════ */

/* Savings category card labels */
input[name="savings-category"] + label,
input[name="transaction-type"] + label {
  transition: all var(--duration-normal) var(--ease-out);
}

/* Hover state (only on devices with hover) */
@media (hover: hover) and (pointer: fine) {
  input[name="savings-category"] + label:hover,
  input[name="transaction-type"] + label:hover {
    transform: translateY(-2px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  }
}

/* Active press feedback */
input[name="savings-category"] + label:active,
input[name="transaction-type"] + label:active {
  transform: scale(0.97);
}

/* Checked state gets no press animation */
input[name="savings-category"]:checked + label:active,
input[name="transaction-type"]:checked + label:active {
  transform: none;
}

@media (prefers-reduced-motion: reduce) {
  input[name="savings-category"] + label:hover,
  input[name="savings-category"] + label:active,
  input[name="transaction-type"] + label:hover,
  input[name="transaction-type"] + label:active {
    transform: none;
  }
}

/* ═══════════════════════════════════════════════════════════════
   NAVIGATION - RESPONSIVE SPA
   Following design-taste-frontend rules for shape consistency
   ═══════════════════════════════════════════════════════════════ */

.nav-btn {
  color: #64748b; /* slate-500 */
  transition: transform var(--duration-fast) var(--ease-out),
              background-color var(--duration-normal) var(--ease-out),
              color var(--duration-normal) var(--ease-out);
}

.nav-btn-active {
  background-color: #d1fae5; /* emerald-100 */
  color: #059669; /* emerald-600 */
}

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

@media (hover: hover) and (pointer: fine) {
  .nav-btn:hover:not(.nav-btn-active) {
    background-color: #f1f5f9; /* slate-100 */
  }

  .nav-btn-active:hover {
    background-color: #a7f3d0; /* emerald-200 */
  }
}

/* ═══════════════════════════════════════════════════════════════
   EXPENSE CARD ANIMATIONS
   ═══════════════════════════════════════════════════════════════ */

.expense-card {
  transition: transform var(--duration-normal) var(--ease-out),
              box-shadow var(--duration-normal) var(--ease-out);
  /* A long list: only the cards in view are laid out, so moving to this screen is immediate.
     96px is a typical card's content, until the card has been seen once. */
  content-visibility: auto;
  contain-intrinsic-size: auto 96px;
}

@media (hover: hover) and (pointer: fine) {
  .expense-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  }
}

/* ═══════════════════════════════════════════════════════════════
   TABLE STYLES - Desktop View & Sticky Columns
   ═══════════════════════════════════════════════════════════════ */

table thead th {
  font-weight: 600;
  letter-spacing: 0.01em; /* small text: slight positive tracking (no uppercase in Hebrew) */
}

table tbody tr {
  transition: background-color var(--duration-normal) var(--ease-out);
}

/* Sort indicators */
.sort-indicator {
  display: inline;
  font-size: 0.75rem;
  margin-right: 0.25rem;
  transition: opacity var(--duration-fast) var(--ease-out);
}

.sort-indicator.hidden {
  opacity: 0;
  visibility: hidden;
}

/* Sticky column shadow effect when scrolling */
.overflow-x-auto {
  position: relative;
}

table tbody tr:hover .sticky {
  background-color: rgb(249 250 251); /* gray-50 */
}

/* ═══════════════════════════════════════════════════════════════
   LOADING SPINNER ANIMATION
   ═══════════════════════════════════════════════════════════════ */

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

.animate-spin {
  animation: spin 1s linear infinite;
}

@media (prefers-reduced-motion: reduce) {
  .animate-spin {
    animation: none;
  }
}

/* ═══════════════════════════════════════════════════════════════
   TOAST NOTIFICATION
   ═══════════════════════════════════════════════════════════════ */

#toast-container {
  max-width: 90vw;
  width: auto;
}

/* Ensure toast is above everything */
#toast-container > div {
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

@media (prefers-reduced-motion: reduce) {
  #toast-container > div {
    transition: opacity 0.3s ease !important;
    transform: none !important;
  }
}

/* ═══════════════════════════════════════════════════════════════
   NAVIGATION TABS - Mobile Horizontal Scroll
   ═══════════════════════════════════════════════════════════════ */

/* Hide scrollbar on navigation container */
#nav-container::-webkit-scrollbar {
  display: none;
}

/* Prevent tabs from shrinking */
.nav-btn {
  flex-shrink: 0;
  min-width: 130px;
}

/* On desktop, center the nav items if they all fit */
@media (min-width: 768px) {
  #nav-container {
    justify-content: center;
  }
}

/* On mobile, enable smooth horizontal scrolling */
@media (max-width: 767px) {
  #nav-container {
    justify-content: flex-start;
    scroll-snap-type: x mandatory;
  }

  .nav-btn {
    scroll-snap-align: center;
  }
}


/* ═══════════════════════════════════════════════════════════════
   APPLE DESIGN LAYER
   Response, materials, typography, native feel, accessibility
   ═══════════════════════════════════════════════════════════════ */

/* Native feel: full-height app shell that respects the notch and home bar.
   The side insets are page-coloured borders, since the body itself carries the header colour. */
.app-shell {
  /* Pinned to the screen instead of sized by 100dvh: the page itself can never scroll or be
     pushed up (by the keyboard) or leave a gap below, so top and bottom stay put */
  position: fixed;
  inset: 0;
  height: auto;
  border-left: env(safe-area-inset-left) solid #f8fafc; /* slate-50 */
  border-right: env(safe-area-inset-right) solid #f8fafc;
  overscroll-behavior: none;
}

/* iOS colours the status bar area from the page background, so html and body take the
   header colour of the current space. The light page colour lives on <main>. */
:root { --space-color: #059669; }
:root:has(header.bg-violet-600) { --space-color: #7c3aed; }
:root:has(header.bg-blue-600) { --space-color: #2563eb; }

/* Only the top (the status bar and the header behind it) is the space colour. Below it the page
   colour shows, so if iOS leaves a gap under the app (a stale 100dvh after the keyboard closes)
   it is not a green block. */
html,
body {
  background: linear-gradient(var(--space-color) calc(env(safe-area-inset-top) + 120px), #f8fafc 0);
}

html {
  min-height: 100%;
}

main {
  padding-bottom: env(safe-area-inset-bottom);
  overscroll-behavior-y: contain;
}

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

button,
label,
select,
[role="button"] {
  touch-action: manipulation; /* removes the tap delay, keeps pinch-zoom */
}

button,
.nav-btn {
  user-select: none;
  -webkit-user-select: none;
}

/* Typography: system font, tracking and leading that follow size */
body {
  font-family: -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  font-optical-sizing: auto;
}

h1 {
  letter-spacing: -0.02em;
  line-height: 1.15;
}

h2,
h3 {
  letter-spacing: -0.01em;
  line-height: 1.25;
}

/* Top bar: opaque, no blur, so iOS can match the status bar area to one solid colour.
   Shared = green; my own space = violet; someone else's space (the admin viewing it) = blue */
header.app-top {
  padding-top: calc(1rem + env(safe-area-inset-top));
}

header.bg-emerald-600 { background-color: #059669; }
header.bg-violet-600 { background-color: #7c3aed; }
header.bg-blue-600 { background-color: #2563eb; }

/* Home-screen app only: recent iOS blurs the top ~20-25px of the app. Starting the header's
   content 20px down leaves that blur on plain header colour. A browser tab is unchanged. */
@media (display-mode: standalone) {
  header.app-top {
    padding-top: calc(20px + env(safe-area-inset-top));
  }
}

/* Opaque: nothing scrolls under the tab bar, so a blur only smeared the green header behind it
   whenever iOS shifted the page */
#main-nav {
  background-color: #fff;
  border-bottom: 0.5px solid rgba(15, 23, 42, 0.12);
}

#toast-container > div {
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
}

/* Cards: hairline borders and soft depth, like a grouped list */
#expense-form,
.expense-card {
  border-color: rgba(15, 23, 42, 0.08);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 4px 16px rgba(15, 23, 42, 0.04);
}

/* Spatial continuity: a view settles into place instead of popping in */
@keyframes view-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: none; }
}

/* Only when moving between screens: the app opens on a finished screen, with no fade-in */
.view-switched .view-section:not(.hidden) {
  animation: view-in 280ms var(--ease-out) both;
}

/* Press feedback: the release settles back slowly after an instant press */
.nav-btn:active,
.btn-primary:active,
input[name="category"] + label:active,
input[name="savings-category"] + label:active,
input[name="transaction-type"] + label:active {
  transition-duration: 80ms;
}

/* Focus ring: soft halo instead of a hard outline */
input:focus,
select:focus,
textarea:focus {
  outline: none;
  box-shadow: 0 0 0 4px rgba(5, 150, 105, 0.22);
  border-color: #059669;
}

/* Text size follows the user's setting: no fixed px on form controls */
input,
select,
textarea {
  font-size: max(16px, 1rem) !important; /* 16px floor keeps iOS from zooming in */
}

/* Accessibility signals */
@media (prefers-reduced-motion: reduce) {
  .view-switched .view-section:not(.hidden) {
    animation: view-fade 200ms ease both;
  }
  @keyframes view-fade {
    from { opacity: 0; }
    to   { opacity: 1; }
  }
}

@media (prefers-reduced-transparency: reduce) {
  #main-nav { background-color: #fff; -webkit-backdrop-filter: none; backdrop-filter: none; }
  #toast-container > div { -webkit-backdrop-filter: none; backdrop-filter: none; }
}

@media (prefers-contrast: more) {
  #main-nav { border-bottom: 1px solid #0f172a; }
  #expense-form,
  .expense-card { border-color: #475569; }
  .nav-btn { color: #334155; }
}

/* Property: its own tabs instead of the budget's (body.property-mode is set by app.js) */
body:not(.property-mode) .property-nav { display: none !important; }
body.property-mode .nav-btn:not(.property-nav) { display: none !important; }
body.property-mode .nav-btn-active { background-color: #fef3c7; color: #b45309; } /* amber-100 / amber-700 */
@media (hover: hover) and (pointer: fine) {
  body.property-mode .nav-btn-active:hover { background-color: #fde68a; } /* amber-200 */
}
