/*
  Base styles: reset, typography, page layout, navigation.
  Uses tokens from tokens.css only. Rules: docs/design.md.
*/

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--label);
  font-family: var(--font-sans);
  font-size: var(--fs-body);
  line-height: var(--lh-body);
  font-weight: var(--fw-regular);
  -webkit-font-smoothing: antialiased;
  overflow-wrap: break-word;
}

img,
svg {
  display: block;
  max-width: 100%;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

/* ---- Typography: text styles (use the class, or the matching element) ---- */

h1,
h2,
h3,
h4,
p,
ul,
ol,
dl,
figure {
  margin: 0;
}

h1,
.text-large-title {
  font-size: var(--fs-large-title);
  line-height: var(--lh-large-title);
  font-weight: var(--fw-bold);
}

.text-title-1 {
  font-size: var(--fs-title-1);
  line-height: var(--lh-title-1);
  font-weight: var(--fw-bold);
}

h2,
.text-title-2 {
  font-size: var(--fs-title-2);
  line-height: var(--lh-title-2);
  font-weight: var(--fw-bold);
}

h3,
.text-title-3 {
  font-size: var(--fs-title-3);
  line-height: var(--lh-title-3);
  font-weight: var(--fw-semibold);
}

h4,
.text-headline {
  font-size: var(--fs-headline);
  line-height: var(--lh-headline);
  font-weight: var(--fw-semibold);
}

.text-body {
  font-size: var(--fs-body);
  line-height: var(--lh-body);
}

.text-callout {
  font-size: var(--fs-callout);
  line-height: var(--lh-callout);
}

.text-subhead {
  font-size: var(--fs-subhead);
  line-height: var(--lh-subhead);
}

.text-footnote {
  font-size: var(--fs-footnote);
  line-height: var(--lh-footnote);
}

.text-caption-1 {
  font-size: var(--fs-caption-1);
  line-height: var(--lh-caption-1);
}

.text-caption-2 {
  font-size: var(--fs-caption-2);
  line-height: var(--lh-caption-2);
}

.text-secondary {
  color: var(--label-secondary);
}

.text-mono,
code {
  font-family: var(--font-mono);
  font-size: 0.9em;
}

code {
  background: var(--bg-tertiary);
  border-radius: var(--radius-sm);
  padding: 0.1em 0.4em;
}

a {
  color: var(--link);
  text-underline-offset: 0.15em;
}

a:hover {
  text-decoration-thickness: 2px;
}

/* ---- Focus: always visible for keyboard users ---- */

:focus-visible {
  outline: 3px solid var(--link);
  outline-offset: 2px;
}

/* ---- Helpers ---- */

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: var(--space-4);
  top: var(--space-2);
  z-index: 100;
  padding: var(--space-3) var(--space-4);
  background: var(--bg-elevated);
  color: var(--link);
  border-radius: var(--radius-md);
  transform: translateY(-200%);
}

.skip-link:focus {
  transform: none;
}

.icon {
  width: 1.5rem;
  height: 1.5rem;
  flex: none;
}

/* ---- Spacing helpers: vertical rhythm between blocks ---- */

.stack > * + * {
  margin-top: var(--space-4);
}

.stack-lg > * + * {
  margin-top: var(--space-8);
}

.stack-sm > * + * {
  margin-top: var(--space-2);
}

/* ---- Page: app shell (header, navigation, main, account) ---- */

.app {
  min-height: 100vh;
  min-height: 100dvh;
}

/* The app name is a large title, like the page title below it. */
.app-header {
  padding: var(--space-6) max(var(--space-4), env(safe-area-inset-right)) 0
    max(var(--space-4), env(safe-area-inset-left));
  padding-top: max(var(--space-6), calc(env(safe-area-inset-top) + var(--space-4)));
}

.app-brand {
  display: inline-flex;
  align-items: center;
  min-height: var(--hit);
  color: var(--label);
  font-size: var(--fs-large-title);
  line-height: var(--lh-large-title);
  font-weight: var(--fw-bold);
  text-decoration: none;
}

/* Bottom tab bar on phones (HIG: tab bar first on small screens) */
.app-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: flex;
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: var(--bg-secondary);
  border-top: 1px solid var(--separator);
}

@supports (backdrop-filter: blur(1px)) {
  .app-nav {
    background: color-mix(in srgb, var(--bg-secondary) 85%, transparent);
    backdrop-filter: saturate(180%) blur(20px);
    -webkit-backdrop-filter: saturate(180%) blur(20px);
  }
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: var(--tabbar-h);
  min-width: var(--hit);
  padding: var(--space-1) var(--space-2);
  color: var(--label-secondary);
  font-size: var(--fs-caption-1);
  line-height: var(--lh-caption-1);
  font-weight: var(--fw-medium);
  text-decoration: none;
}

.nav-item:hover {
  text-decoration: none;
  color: var(--label);
}

.nav-item[aria-current="page"] {
  color: var(--link);
  font-weight: var(--fw-semibold);
}

.app-main {
  padding: var(--space-6) max(var(--space-4), env(safe-area-inset-right)) var(--space-8)
    max(var(--space-4), env(safe-area-inset-left));
}

.page {
  width: 100%;
  max-width: var(--content-w);
  margin-inline: auto;
}

.page-wide {
  max-width: var(--wide-w);
}

.page-header {
  margin-bottom: var(--space-6);
}

.page-header > p {
  margin-top: var(--space-1);
  color: var(--label-secondary);
}

/* Bottom of the page on phones, bottom of the sidebar on wide screens:
   who is signed in, Log out, and the app version. */
.app-bottom {
  padding: 0 max(var(--space-4), env(safe-area-inset-right))
    calc(var(--tabbar-h) + env(safe-area-inset-bottom) + var(--space-8))
    max(var(--space-4), env(safe-area-inset-left));
}

.app-account {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  max-width: var(--content-w);
  margin-inline: auto;
  padding: var(--space-2) var(--space-2) var(--space-2) var(--space-4);
  background: var(--bg-secondary);
  border-radius: var(--radius-md);
}

.app-user {
  display: inline-flex;
  align-items: center;
  min-height: var(--hit);
  min-width: 0;
  color: inherit;
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-footer {
  margin-top: var(--space-6);
  color: var(--label-secondary);
  font-size: var(--fs-caption-1);
  line-height: var(--lh-caption-1);
  text-align: center;
}

/* Wide screens: sidebar on the leading side */
@media (min-width: 48rem) {
  .app {
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
    grid-template-rows: auto 1fr auto;
  }

  .app-header {
    grid-column: 1;
    grid-row: 1;
    padding: var(--space-6) var(--space-4) var(--space-4);
    background: var(--bg-secondary);
    border-right: 1px solid var(--separator);
  }

  .app-nav {
    grid-column: 1;
    grid-row: 2;
    position: static;
    flex-direction: column;
    gap: var(--space-1);
    padding: var(--space-2) var(--space-3);
    background: var(--bg-secondary);
    border-top: 0;
    border-right: 1px solid var(--separator);
  }

  .nav-item {
    flex: none;
    flex-direction: row;
    justify-content: flex-start;
    gap: var(--space-3);
    min-height: var(--hit);
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    font-size: var(--fs-body);
    line-height: var(--lh-body);
  }

  .nav-item:hover {
    background: var(--fill-pressed);
  }

  .nav-item[aria-current="page"] {
    background: var(--accent-tint);
  }

  .app-bottom {
    grid-column: 1;
    grid-row: 3;
    padding: var(--space-4);
    background: var(--bg-secondary);
    border-right: 1px solid var(--separator);
    border-top: 1px solid var(--separator);
  }

  .app-account {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-1);
    max-width: none;
    padding: 0;
    background: transparent;
  }

  .app-user {
    max-width: 100%;
    font-weight: var(--fw-semibold);
  }

  .app-account .btn-plain {
    margin-left: calc(var(--space-4) * -1);
  }

  .app-main {
    grid-column: 2;
    grid-row: 1 / span 3;
    padding: var(--space-10) var(--space-8) var(--space-10);
  }
}

/* ---- Page: sign-in layout (no navigation) ---- */

.auth {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 100%;
  max-width: 26rem;
  margin-inline: auto;
  padding: var(--space-8) max(var(--space-4), env(safe-area-inset-right)) var(--space-8)
    max(var(--space-4), env(safe-area-inset-left));
}

.auth-brand {
  margin-bottom: var(--space-8);
  text-align: center;
}

.auth-brand p {
  margin-top: var(--space-1);
  color: var(--label-secondary);
}

/* ---- Motion: respect Reduce Motion ---- */

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