/* Swim Results: the native-feeling layout for the installed app.

   EVERY rule here starts with html[data-native="1"]. That attribute is only set
   (by the small script in base.html's <head>) when the site runs as the
   installed app, or in narrow browser windows if PWA_NATIVE_UI_IN_BROWSER is on.
   So the normal website never sees any of these styles.

   Contents: 1 basics · 2 top bar + large titles · 3 tab bar · 4 controls
             5 bottom sheets · 6 All Races · 7 page transitions
*/

/* ---- 1. Basics ------------------------------------------------------------ */
html[data-native="1"] {
  --appbar-h: 44px;
  --tabbar-h: 54px;
  --chrome-bg: color-mix(in srgb, var(--surface) 82%, transparent);
  --hairline: color-mix(in srgb, var(--text) 14%, transparent);
  -webkit-tap-highlight-color: transparent;   /* no grey flash on tap */
  -webkit-text-size-adjust: 100%;
}
html[data-native="1"] body {
  touch-action: manipulation;                 /* no double-tap zoom delay */
  overscroll-behavior-y: none;                /* no rubber-band showing behind the bars */
}
html[data-native="1"] .native-only { display: block; }
html[data-native="1"] nav.side { display: none; }               /* replaced by the tab bar */
html[data-native="1"] .layout { grid-template-columns: 1fr; min-height: 100dvh; }
html[data-native="1"] main {
  max-width: 760px; margin: 0 auto;
  padding: 0 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
}
html[data-native="1"] .layout.nonav main {                     /* login pages: no bars */
  padding-top: calc(env(safe-area-inset-top) + 8px);
  padding-bottom: calc(env(safe-area-inset-bottom) + 24px);
}
html[data-native="1"] .card { border-radius: 14px; }
/* App chrome isn't selectable text; results data still is. */
html[data-native="1"] .appbar, html[data-native="1"] .tabbar, html[data-native="1"] button,
html[data-native="1"] .btn, html[data-native="1"] .sheet-handle, html[data-native="1"] .pagehead {
  -webkit-user-select: none; user-select: none;
}

/* ---- 2. Top bar and large titles ------------------------------------------ */
/* The top bar sits under the iPhone status bar (black-translucent), so it pads
   itself by the safe-area inset. Its small centred title fades in once the
   page's large title has scrolled away (native.js toggles .compact). */
html[data-native="1"] .appbar {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  position: sticky; top: 0; z-index: 30;
  margin: 0 calc(50% - 50vw);           /* stretch to the screen edges, past main's padding */
  height: calc(var(--appbar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
  background: var(--chrome-bg);
  -webkit-backdrop-filter: saturate(180%) blur(18px); backdrop-filter: saturate(180%) blur(18px);
  border-bottom: 0.5px solid transparent;
  transition: border-color .2s;
}
html[data-native="1"] .appbar.compact { border-bottom-color: var(--hairline); }
html[data-native="1"] .appbar-title {
  grid-column: 2; font-weight: 600; font-size: 17px; opacity: 0; transition: opacity .2s;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 55vw;
}
html[data-native="1"] .appbar.compact .appbar-title { opacity: 1; }
html[data-native="1"] .appbar-right { grid-column: 3; justify-self: end; }
html[data-native="1"] main > h1:first-of-type,
html[data-native="1"] .pagehead h1 {
  font-size: 34px; line-height: 1.15; letter-spacing: -0.02em; margin: 6px 0 10px;
}

/* Rounded "chip" buttons: swimmer picker, All Races filter. */
html[data-native="1"] .chip {
  display: inline-flex; align-items: center; gap: 6px; max-width: 46vw;
  min-height: 34px; padding: 6px 12px; border-radius: 999px; font-size: 15px;
  background: color-mix(in srgb, var(--btn-bg) 14%, transparent); color: var(--link); border: 0;
}
html[data-native="1"] .chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
html[data-native="1"] .chip svg { width: 14px; height: 14px; flex: none; fill: none; stroke: currentColor; stroke-width: 2.5; }

/* ---- 3. Bottom tab bar ---------------------------------------------------- */
html[data-native="1"] .tabbar {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: var(--chrome-bg);
  -webkit-backdrop-filter: saturate(180%) blur(18px); backdrop-filter: saturate(180%) blur(18px);
  border-top: 0.5px solid var(--hairline);
}
html[data-native="1"] .tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: var(--tabbar-h); min-width: 44px;               /* 44px+ touch target */
  color: var(--muted); text-decoration: none; font-size: 11px; font-weight: 500;
}
html[data-native="1"] .tabbar a svg {
  width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round;
}
html[data-native="1"] .tabbar a.on { color: var(--link); }
html[data-native="1"] .tabbar a:active { opacity: .6; }
html[data-native="1"] .pwa-toast { bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 12px); }

/* ---- 4. Controls ---------------------------------------------------------- */
html[data-native="1"] button, html[data-native="1"] .btn {
  min-height: 44px; border-radius: 10px; padding: 10px 16px; font-size: 16px;
  display: inline-flex; align-items: center; justify-content: center;
  transition: transform .08s, opacity .08s;
}
html[data-native="1"] button:active:not(:disabled), html[data-native="1"] .btn:active { transform: scale(.97); opacity: .85; }
html[data-native="1"] input, html[data-native="1"] select, html[data-native="1"] textarea {
  min-height: 44px; border-radius: 10px; font-size: 16px;
}
html[data-native="1"] input[type="radio"], html[data-native="1"] input[type="checkbox"] { min-height: 0; }
/* Only real pointers get hover effects; touch gets :active instead. */
@media (hover: none) {
  html[data-native="1"] tr.race:hover td { background: none; }
}
html[data-native="1"] tr.race:active { opacity: .7; }
html[data-native="1"] form.busy button[type="submit"],
html[data-native="1"] form.busy button:not([type]) { opacity: .6; }

/* Settings: give the import progress its own card look. */
html[data-native="1"] .bar { height: 8px; }

/* ---- 5. Bottom sheets (built on <dialog>, see native.js) ------------------ */
html[data-native="1"] dialog.sheet {
  position: fixed; inset: auto 0 0 0; margin: 0 auto; width: 100%; max-width: 640px;
  max-height: 92dvh; overflow: auto; overscroll-behavior: contain;
  border: 0; border-radius: 16px 16px 0 0; padding: 6px 16px calc(20px + env(safe-area-inset-bottom));
  background: var(--surface); color: var(--text); box-shadow: 0 -6px 30px #0004;
}
html[data-native="1"] dialog.sheet[open] { animation: sheet-up .28s cubic-bezier(.2,.8,.2,1); }
html[data-native="1"] dialog.sheet.closing { animation: sheet-down .2s ease-in forwards; }
html[data-native="1"] dialog.sheet::backdrop { background: #0006; animation: fade-in .2s; }
html[data-native="1"] .sheet-handle {
  display: block; width: 100%; padding: 6px 0 10px; border: 0; background: none; min-height: 0; cursor: grab;
}
/* The sheet focuses its handle when it opens; the × and Esc are the visible ways out. */
html[data-native="1"] .sheet-handle:focus { outline: none; }
html[data-native="1"] .sheet-handle::before {
  content: ""; display: block; margin: 0 auto; width: 38px; height: 5px; border-radius: 3px; background: var(--hairline);
}
html[data-native="1"] .sheet h2 { margin: 0 0 10px; font-size: 20px; }
html[data-native="1"] .sheet-list { list-style: none; margin: 0; padding: 0; }
html[data-native="1"] .sheet-list a {
  display: flex; justify-content: space-between; align-items: center; min-height: 48px;
  padding: 0 4px; border-bottom: 0.5px solid var(--hairline); color: var(--text); text-decoration: none;
}
html[data-native="1"] .sheet-list a.on::after { content: "✓"; color: var(--link); font-weight: 700; }
html[data-native="1"] .sheet .card { box-shadow: none; padding: 0; margin: 0; }
html[data-native="1"] .sheet.loading .sheet-body { opacity: .45; pointer-events: none; transition: opacity .15s; }
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: none; } }
@keyframes sheet-down { to { transform: translateY(100%); } }
@keyframes fade-in { from { opacity: 0; } }

/* ---- 6. All Races in the app ---------------------------------------------- */
/* Filters and the race panel move into bottom sheets (native.js), so the page
   shows the large title, a filter chip and the list of race cards. */
html[data-native="1"] .top { display: block; margin: 0; }
html[data-native="1"] .pagehead { align-items: center; }
html[data-native="1"] .pagehead .btn { min-height: 36px; padding: 6px 14px; border-radius: 999px; font-size: 15px; }
html[data-native="1"] .racechips { display: flex; gap: 8px; margin: 0 0 10px; flex-wrap: wrap; }
html[data-native="1"] .filters form { gap: 14px; }
html[data-native="1"] #raceTable tr.race { padding: 12px 10px; }
/* One scrolling area (the page), not a box inside it; table headers stick under the top bar. */
html[data-native="1"] .scroll { max-height: none; overflow: visible; }
html[data-native="1"] th { top: calc(var(--appbar-h) + env(safe-area-inset-top)); }

/* ---- 7. Page transitions --------------------------------------------------- */
/* A gentle cross-fade between pages in the installed app (browsers without the
   View Transitions API simply switch pages as normal). Off when the phone asks
   for reduced motion. The bars keep their own names so they stay still. */
@media (display-mode: standalone) and (prefers-reduced-motion: no-preference) {
  @view-transition { navigation: auto; }
}
html[data-native="1"] .appbar { view-transition-name: appbar; }
html[data-native="1"] .tabbar { view-transition-name: tabbar; }
::view-transition-old(root) { animation: 160ms ease-out both vt-out; }
::view-transition-new(root) { animation: 220ms ease-out both vt-in; }
@keyframes vt-out { to { opacity: 0; } }
@keyframes vt-in { from { opacity: 0; transform: translateY(6px); } }
@media (prefers-reduced-motion: reduce) {
  html[data-native="1"] dialog.sheet[open], html[data-native="1"] dialog.sheet.closing,
  html[data-native="1"] dialog.sheet::backdrop { animation: none; }
}
