/* ==========================================================================
   Base styles: reset, typography, layout primitives, shared utilities.
   Only tokens here — no raw values (see styles/tokens.css).
   ========================================================================== */

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

html {
  scrollbar-gutter: stable; /* page doesn't shift sideways when a dialog locks scrolling */
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  min-block-size: 100dvh;
  background: var(--color-background);
  color: var(--color-on-surface);
  font-family: var(--font-family-base);
  font-size: var(--font-body-lg-size);
  line-height: var(--font-body-lg-line-height);
  font-weight: var(--font-body-lg-weight);
  letter-spacing: var(--font-body-lg-tracking);
  font-synthesis-weight: none;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  scrollbar-color: var(--color-outline-variant) transparent;
}

/* Lock page scrolling while a modal dialog or sheet is open */
body:has(dialog[open]) { overflow: hidden; }

h1, h2, h3, h4, h5, h6, p, figure, blockquote, dl, dd { margin: 0; }
ul[role="list"], ol[role="list"] { list-style: none; margin: 0; padding: 0; }
img, svg, video, canvas { display: block; max-inline-size: 100%; }
input, button, textarea, select { font: inherit; color: inherit; letter-spacing: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; }
a { color: var(--color-primary-text); }
[hidden] { display: none !important; }
::selection { background: var(--color-primary-container); color: var(--color-on-primary-container); }

/* Custom elements are inline by default; most of ours are blocks.
   Each component's CSS sets its own display. */
:not(:defined) { visibility: hidden; } /* avoid a flash of un-upgraded markup */

/* ---- Focus ------------------------------------------------------------- */
:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--focus-ring-offset);
}
:focus:not(:focus-visible) { outline: none; }
/* Page headings and the main area receive focus after navigation (for screen
   readers) but shouldn't show a ring */
[tabindex="-1"]:focus { outline: none; }

/* ---- Typography classes (Material 3 type scale) ------------------------- */
.type-display-lg  { font-size: var(--font-display-lg-size);  line-height: var(--font-display-lg-line-height);  font-weight: var(--font-display-lg-weight);  letter-spacing: var(--font-display-lg-tracking); }
.type-display-md  { font-size: var(--font-display-md-size);  line-height: var(--font-display-md-line-height);  font-weight: var(--font-display-md-weight);  letter-spacing: var(--font-display-md-tracking); }
.type-display-sm  { font-size: var(--font-display-sm-size);  line-height: var(--font-display-sm-line-height);  font-weight: var(--font-display-sm-weight);  letter-spacing: var(--font-display-sm-tracking); }
.type-headline-lg { font-size: var(--font-headline-lg-size); line-height: var(--font-headline-lg-line-height); font-weight: var(--font-headline-lg-weight); letter-spacing: var(--font-headline-lg-tracking); }
.type-headline-md { font-size: var(--font-headline-md-size); line-height: var(--font-headline-md-line-height); font-weight: var(--font-headline-md-weight); letter-spacing: var(--font-headline-md-tracking); }
.type-headline-sm { font-size: var(--font-headline-sm-size); line-height: var(--font-headline-sm-line-height); font-weight: var(--font-headline-sm-weight); letter-spacing: var(--font-headline-sm-tracking); }
.type-title-lg    { font-size: var(--font-title-lg-size);    line-height: var(--font-title-lg-line-height);    font-weight: var(--font-title-lg-weight);    letter-spacing: var(--font-title-lg-tracking); }
.type-title-md    { font-size: var(--font-title-md-size);    line-height: var(--font-title-md-line-height);    font-weight: var(--font-title-md-weight);    letter-spacing: var(--font-title-md-tracking); }
.type-title-sm    { font-size: var(--font-title-sm-size);    line-height: var(--font-title-sm-line-height);    font-weight: var(--font-title-sm-weight);    letter-spacing: var(--font-title-sm-tracking); }
.type-body-lg     { font-size: var(--font-body-lg-size);     line-height: var(--font-body-lg-line-height);     font-weight: var(--font-body-lg-weight);     letter-spacing: var(--font-body-lg-tracking); }
.type-body-md     { font-size: var(--font-body-md-size);     line-height: var(--font-body-md-line-height);     font-weight: var(--font-body-md-weight);     letter-spacing: var(--font-body-md-tracking); }
.type-body-sm     { font-size: var(--font-body-sm-size);     line-height: var(--font-body-sm-line-height);     font-weight: var(--font-body-sm-weight);     letter-spacing: var(--font-body-sm-tracking); }
.type-label-lg    { font-size: var(--font-label-lg-size);    line-height: var(--font-label-lg-line-height);    font-weight: var(--font-label-lg-weight);    letter-spacing: var(--font-label-lg-tracking); }
.type-label-md    { font-size: var(--font-label-md-size);    line-height: var(--font-label-md-line-height);    font-weight: var(--font-label-md-weight);    letter-spacing: var(--font-label-md-tracking); }
.type-label-sm    { font-size: var(--font-label-sm-size);    line-height: var(--font-label-sm-line-height);    font-weight: var(--font-label-sm-weight);    letter-spacing: var(--font-label-sm-tracking); }

/* Text color helpers */
.text-muted   { color: var(--color-on-surface-variant); }
.text-primary { color: var(--color-primary-text); }
.text-error   { color: var(--color-error-text); }
.text-success { color: var(--color-success-text); }
.text-warning { color: var(--color-warning-text); }

/* Money and other numbers line up in columns */
.numeric { font-variant-numeric: tabular-nums; }

/* ---- Layout primitives --------------------------------------------------
   .container  centered page column with side gutters
   .stack      vertical flow with even gaps     data-gap="xs|sm|md|lg|xl"
   .cluster    horizontal row that wraps        data-gap, data-justify="between|end"
   .grid       responsive auto-fitting columns  data-min="sm|md|lg"
   ------------------------------------------------------------------------ */
.container {
  inline-size: 100%;
  max-inline-size: var(--layout-max-width);
  margin-inline: auto;
  padding-inline: var(--layout-gutter);
}
@media (min-width: 600px) {
  .container { padding-inline: var(--layout-gutter-wide); }
}

.stack { display: flex; flex-direction: column; gap: var(--_gap, var(--space-md)); }
.cluster { display: flex; flex-wrap: wrap; align-items: center; gap: var(--_gap, var(--space-xs)); }
.cluster[data-justify="between"] { justify-content: space-between; }
.cluster[data-justify="end"] { justify-content: flex-end; }
.cluster[data-nowrap] { flex-wrap: nowrap; }
/* A row of active-filter chips takes no space while there are none */
[data-active-filters]:not(:has(*)) { display: none; }

.grid {
  display: grid;
  gap: var(--_gap, var(--space-md));
  grid-template-columns: repeat(auto-fill, minmax(min(100%, var(--_min, var(--size-240))), 1fr));
}
.grid[data-min="sm"] { --_min: var(--size-112); }
.grid[data-min="md"] { --_min: var(--size-240); }
/* data-fit: stretch the items to fill the row instead of leaving empty columns */
.grid[data-fit] { grid-template-columns: repeat(auto-fit, minmax(min(100%, var(--_min, var(--size-240))), 1fr)); }

/* A heading row that may hold a button: same height either way, so side-by-side sections line up */
.section-bar { min-block-size: var(--size-touch-target); }

/* Sections you can jump to (#/settings?section=assist) stop below the sticky top bar */
.page [id] { scroll-margin-block-start: calc(var(--top-bar-height) + var(--space-md)); }

/* A short message inside a list card ("Nothing overdue") */
.list-message { padding: var(--space-md) var(--list-item-padding-x); }
.grid[data-min="lg"] { --_min: var(--size-360); }

[data-gap="none"] { --_gap: var(--space-none); }
[data-gap="2xs"]  { --_gap: var(--space-2xs); }
[data-gap="xs"]   { --_gap: var(--space-xs); }
[data-gap="sm"]   { --_gap: var(--space-sm); }
[data-gap="md"]   { --_gap: var(--space-md); }
[data-gap="lg"]   { --_gap: var(--space-lg); }
[data-gap="xl"]   { --_gap: var(--space-xl); }
[data-gap="2xl"]  { --_gap: var(--space-2xl); }

.grow { flex: 1 1 auto; min-inline-size: 0; }

/* An image that belongs on paper (signatures, logos): shown on white, like the printed document */
.paper-image {
  display: block;
  max-block-size: var(--size-96);
  inline-size: auto;
  max-inline-size: 100%;
  padding: var(--space-xs);
  border-radius: var(--shape-sm);
  background: var(--doc-color-background);
  object-fit: contain;
}

/* A list of app-list-item rows */
.list { display: flex; flex-direction: column; margin: 0; padding: 0; }

/* Hide visually but keep available to screen readers */
.visually-hidden {
  position: absolute !important;
  inline-size: 1px !important;   /* style-check: ignore */
  block-size: 1px !important;    /* style-check: ignore */
  margin: -1px !important;       /* style-check: ignore */
  padding: 0 !important;
  overflow: hidden !important;
  clip-path: inset(50%) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

/* ---- State layer ---------------------------------------------------------
   Add class="state-layer" to any interactive surface. A flat overlay of the
   current text color fades in on hover / focus / press (Material 3).
   ------------------------------------------------------------------------ */
.state-layer { position: relative; isolation: isolate; }
.state-layer::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: var(--z-behind);
  border-radius: inherit;
  background: currentColor;
  opacity: var(--opacity-0);
  pointer-events: none;
  transition: opacity var(--motion-duration-short) var(--motion-easing-linear);
}
@media (hover: hover) {
  .state-layer:hover::before { opacity: var(--state-hover-opacity); }
}
.state-layer:focus-visible::before { opacity: var(--state-focus-opacity); }
.state-layer:active::before { opacity: var(--state-pressed-opacity); }
.state-layer:disabled::before,
.state-layer[aria-disabled="true"]::before { opacity: var(--opacity-0); }

/* ---- Touch target ----------------------------------------------------------
   Add class="touch-target" to small controls: an invisible area of at least
   48×48 receives taps even when the visible control is smaller.
   ------------------------------------------------------------------------ */
.touch-target { position: relative; }
.touch-target::after {
  content: "";
  position: absolute;
  inset-block-start: 50%;
  inset-inline-start: 50%;
  inline-size: max(100%, var(--size-touch-target));
  block-size: max(100%, var(--size-touch-target));
  transform: translate(-50%, -50%);
}

/* ---- Spinner animation, shared by app-progress and loading buttons ---- */
@keyframes app-spin { to { transform: rotate(1turn); } }

/* ==========================================================================
   App shell: navigation + page area + floating action button
   Phones: bottom bar (content leaves room at the bottom).
   600px+: navigation rail on the left (content shifts right).
   ========================================================================== */
.skip-link {
  position: absolute;
  inset-block-start: var(--space-xs);
  inset-inline-start: var(--space-xs);
  z-index: var(--z-fab);
  padding: var(--space-xs) var(--space-md);
  border-radius: var(--shape-full);
  background: var(--color-primary);
  color: var(--color-on-primary);
  transform: translateY(-200%);
}
.skip-link:focus { transform: none; }

/* Documents being printed are rendered here (see lib/print.js and styles/print.css) */
#print-root { display: none; }

/* The container the router creates for each page; it doesn't affect layout */
.view { display: contents; }

.app-main {
  display: block;
  min-block-size: 100dvh;
  padding-block-end: calc(var(--nav-bar-height) + env(safe-area-inset-bottom));
}
@media (min-width: 600px) {
  .app-main {
    padding-block-end: 0;
    padding-inline-start: var(--nav-rail-width);
  }
  .app-fab-compact { display: none !important; }
}

/* A page's content column. data-width="form" for narrower forms (Settings). */
.page {
  display: flex;
  flex-direction: column;
  gap: var(--space-lg);
  padding-block: var(--space-md) var(--space-2xl);
}
/* Form pages keep the same left edge as the page title, just a narrower column */
.page[data-width="form"] > * { inline-size: 100%; max-inline-size: var(--layout-form-width); }
/* Leave room so the floating button never covers the last item on phones */
@media (max-width: 599.98px) {
  body:has(.app-fab-compact:not([hidden])) .page { padding-block-end: calc(var(--fab-size) + var(--space-2xl)); }
}

/* A titled group of settings or fields inside a card */
.section-heading { display: flex; flex-direction: column; gap: var(--space-2xs); margin-block-end: var(--space-md); }

/* ---- Split layout: main content + side pane (e.g. editor form + live preview)
   840px+: side by side, the side pane stays in view while you scroll.
   Phones/tablets: one pane at a time, switched with [data-show="main|aside"]
   (the page shows tabs, marked .split__tabs, only on narrow screens). */
.split { display: grid; gap: var(--space-lg); align-items: start; }
.split__main, .split__aside { min-inline-size: 0; }
@media (min-width: 840px) {
  .split { grid-template-columns: minmax(0, 11fr) minmax(0, 9fr); }
  .split__aside {
    position: sticky;
    inset-block-start: calc(var(--top-bar-height) + var(--space-md));
    max-block-size: calc(100dvh - var(--top-bar-height) - var(--space-xl));
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  .split__tabs { display: none !important; }
}
@media (max-width: 839.98px) {
  .split[data-show="main"] > .split__aside,
  .split[data-show="aside"] > .split__main { display: none; }
}

/* A small, readable table (e.g. the restore preview) */
.simple-table { inline-size: 100%; border-collapse: collapse; font-size: var(--font-body-md-size); line-height: var(--font-body-md-line-height); }
.simple-table :is(th, td) { padding: var(--space-xs) var(--space-sm); border-block-end: var(--border-width-hairline) solid var(--color-outline-variant); text-align: start; }
.simple-table thead th { color: var(--color-on-surface-variant); font-weight: var(--font-weight-500); }
.simple-table .numeric { text-align: end; }

/* Search bar that opens the search page (Material 3 "search bar" look) */
.search-launcher {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  min-block-size: var(--search-launcher-height);
  padding-inline: var(--space-md);
  border-radius: var(--search-launcher-radius);
  background: var(--search-launcher-bg);
  color: var(--search-launcher-fg);
  font-size: var(--font-body-lg-size);
  line-height: var(--font-body-lg-line-height);
  text-decoration: none;
}
.search-launcher__text { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search-launcher__key {
  padding: var(--space-none) var(--space-xs);
  border: var(--border-width-hairline) solid var(--color-outline-variant);
  border-radius: var(--shape-xs);
  font: inherit;
  font-size: var(--font-label-md-size);
}
@media (hover: none) { .search-launcher__key { display: none; } }

/* ---------------------------------------------------------------------------
   Settings: two compact navigation rows, then one section (same on every screen)
     row 1   .tabs                  the areas: Business · Documents · Smart tools · Data
     row 2   .settings__sections    that area's sections as small cards with their status
   --------------------------------------------------------------------------- */
.settings { gap: var(--space-md); }
.settings > * { inline-size: 100%; max-inline-size: var(--layout-form-width); }
.settings__detail { display: flex; flex-direction: column; gap: var(--space-md); min-inline-size: 0; }

/* Row 1: tabs — a rounded track with a green pill on the current one; the search box sits at the end */
.settings__bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-md);
}
.tabs {
  display: inline-flex;
  gap: var(--space-3xs);
  min-inline-size: 0;
  padding: var(--space-2xs);
  border: var(--border-width-hairline) solid var(--tabs-track-border);
  border-radius: calc(var(--tabs-radius) + var(--space-2xs));
  background: var(--tabs-track-bg);
}
.tabs__tab {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-block-size: var(--size-control-md);
  padding-inline: var(--space-md);
  border-radius: var(--tabs-radius);
  color: var(--tabs-fg);
  font-size: var(--font-label-lg-size);
  line-height: var(--font-label-lg-line-height);
  font-weight: var(--font-label-lg-weight);
  letter-spacing: var(--font-label-lg-tracking);
  text-decoration: none;
  white-space: nowrap;
}
.tabs__tab[aria-current] { background: var(--tabs-active-bg); color: var(--tabs-active-fg); }
.settings__search { flex: 0 1 var(--size-240); }
.settings__search-toggle { display: none; }

/* Row 2: the sections of the current tab */
.settings__sections {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, var(--size-160)), 1fr));
  gap: var(--space-xs);
}
.section-link {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  min-block-size: var(--size-56);
  padding: var(--space-xs);
  border: var(--border-width-hairline) solid var(--color-outline-variant);
  border-radius: var(--shape-lg);
  background: var(--color-surface-container-low);
  color: var(--color-on-surface);
  text-decoration: none;
}
.section-link[aria-current] {
  border-color: var(--color-primary-tint-border);
  background: var(--color-primary-tint);
  color: var(--color-on-surface);
}
.section-link[aria-current] app-icon { color: var(--color-primary-text); }
.section-link app-icon { flex: none; color: var(--color-on-surface-variant); }
.section-link__text { display: flex; flex-direction: column; min-inline-size: 0; }
.section-link__label {
  font-size: var(--font-label-lg-size);
  line-height: var(--font-label-lg-line-height);
  font-weight: var(--font-label-lg-weight);
  letter-spacing: var(--font-label-lg-tracking);
}
.section-link__status {
  overflow: hidden;
  color: var(--color-on-surface-variant);
  font-size: var(--font-body-sm-size);
  line-height: var(--font-body-sm-line-height);
  text-overflow: ellipsis;
  white-space: nowrap;
}
.section-link__status:empty { display: none; }

/* While searching, the results take the place of the sections and the fields */
.settings[data-searching] :is(.settings__sections, .settings__detail) { display: none; }

/* Phones: four equal tabs, sections two per row (name only), search opens from the top bar */
@media (max-width: 599.98px) {
  .settings__bar { flex-wrap: wrap; gap: var(--space-xs); }
  .tabs { display: flex; inline-size: 100%; }
  .tabs__tab { flex: 1 1 0; padding-inline: var(--space-2xs); }
  .settings__sections { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .section-link { min-block-size: var(--size-touch-target); }
  .section-link__status { display: none; }
  .settings__search { display: none; order: -1; flex-basis: 100%; margin-block-end: var(--space-none); }
  .settings[data-search-open] .settings__search { display: block; }
  .settings__search-toggle { display: inline-flex; }
}

/* A field reached from "Search settings": outlined for a moment so the eye finds it */
[data-highlight] {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--space-2xs);
  border-radius: var(--shape-xs);
}
/* "Reset … to the defaults" at the end of a section */
.settings__reset { display: flex; justify-content: flex-start; }

/* Headings and page titles use the heading font (Plus Jakarta Sans when installed, else Inter) */
:is(h1, h2, h3, .type-display-lg, .type-display-md, .type-display-sm, .type-headline-lg, .type-headline-md, .type-headline-sm, .type-title-lg) {
  font-family: var(--font-family-heading);
}
/* The search bar on the dashboard gets the same quiet edge as cards */
.search-launcher { border: var(--border-width-hairline) solid var(--color-outline-variant); }
