@layer base, wren, pages;

@layer base {
  :root {
    color-scheme: light dark;
    font-family: system-ui, sans-serif;
    line-height: 1.5;
    -webkit-text-size-adjust: none;
    text-size-adjust: none;

    --rule: color-mix(in srgb, currentColor 15%, transparent);
    --selected: color-mix(in srgb, currentColor 12%, transparent);
    --muted: color-mix(in srgb, currentColor 60%, transparent);
    --danger: light-dark(#b42318, #f97066);
    --accent: seagreen;
    --carried: color-mix(in srgb, var(--accent) 45%, transparent);
  }

  *, *::before, *::after { box-sizing: border-box; }
  body { margin: 0 auto; max-width: 80rem; padding: 1rem; }
  h1, h2, h3 { line-height: 1.2; text-wrap: balance; }
  p { text-wrap: pretty; }
  svg { display: block; max-width: 100%; }
  input, button, select, textarea { font: inherit; }
  table { border-collapse: collapse; font-variant-numeric: tabular-nums; }
  th, td { padding: 0.125rem 0.5rem; text-align: start; vertical-align: baseline; }
  th { font-weight: normal; }
  thead th { color: var(--muted); }
  tfoot > tr > * { font-weight: bold; }
  tfoot > tr:first-child > * { border-block-start: 1px solid var(--rule); }
  progress { accent-color: var(--accent); }
  dl { display: grid; grid-template-columns: auto 1fr; gap: 0.25rem 1rem; }
  dt { font-weight: bold; }
  dd { margin: 0; }

  a { color: inherit; text-decoration-color: var(--muted); text-decoration-thickness: 1px; text-underline-offset: 0.2em; }
  a:hover { text-decoration-color: currentColor; text-decoration-thickness: 2px; }

  .num { text-align: end; white-space: nowrap; }
  .danger { color: var(--danger); }
  .muted { color: var(--muted); }
  .scroll-x { overflow-x: auto; }
  .pill { font-size: 0.8em; border: 1px solid; border-radius: 1em; padding: 0 0.5em; }
  .plain-button { all: unset; cursor: pointer; }
  :focus-visible { outline: 2px solid var(--accent); }
}

@layer pages {
  body:has(#nav) { max-width: none; padding: 0; height: 100dvh; display: flex; flex-direction: column; }
  #app:has(> #nav) { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto minmax(0, 1fr); }
  #app:has(> #inspector) { grid-template-columns: minmax(0, 1fr) 22rem; }
  #nav { grid-area: 1 / 1 / auto / -1; }
  #edit-bar { grid-area: 2 / -2; }
  #main { grid-area: 3 / 1; }
  #inspector { grid-area: 3 / 2; }
  #app:has(> #inspector) #main { grid-row: 2 / -1; }
  #app:has(> #inspector) #edit-bar { border-inline-start: 1px solid var(--rule); flex-wrap: wrap; }
  #app:has(> #inspector) .edit-bar-last { order: 1; flex-basis: 100%; }
  #app:has(> #inspector) #edit-bar > a { margin-inline-start: auto; }
  #edit-bar { display: flex; gap: 0.5rem; align-items: baseline; padding: 0.375rem 1rem; border-block-end: 1px solid var(--rule); font-size: 0.875rem; }
  .edit-bar-last { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
  #main, #inspector { padding: 1rem; min-width: 0; overflow-y: auto; overscroll-behavior: contain; }
  #nav { display: flex; flex-wrap: wrap; gap: 0.25rem 1.5rem; align-items: baseline; padding: 0.5rem 1rem; border-block-end: 1px solid var(--rule); }
  #inspector { border-inline-start: 1px solid var(--rule); font-size: 0.875rem; }
  @media (max-width: 60rem) {
    body:has(#nav) { display: block; height: auto; }
    #app:has(> #nav) { display: block; }
    #main, #inspector { overflow-y: visible; }
  }

  #nav a { text-decoration: none; }
  #nav a:hover { text-decoration: underline; }
  #nav > p, #nav > h2 { margin: 0; font-size: 1rem; }
  #nav ul { display: flex; flex-wrap: wrap; gap: 0.25rem 1rem; list-style: none; margin: 0; padding: 0; }
  .account { display: flex; gap: 0.5rem; align-items: baseline; justify-content: end; }
  #nav .account { margin-inline-start: auto; }
  #demo-banner { flex-basis: 100%; font-size: 0.875rem; }
  #demo-banner p { margin: 0; }
  #demo-banner a { text-decoration: underline; }

  .history { list-style: none; padding: 0; display: grid; gap: 0.25rem; }

  /* The overview */
  .overview h1 { margin-block: 0 0.75rem; }
  .overview-band { display: flex; flex-wrap: wrap; gap: 1rem 2.5rem; align-items: start; margin-block-end: 1rem; }
  .overview-band h2, .overview-details h2, .overview-table .section th { margin: 0 0 0.25rem; font-size: 0.8125rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
  .overview-band dl { margin: 0; }
  .overview-band dt { font-weight: normal; }
  .year-progress { margin: 0.5rem 0 0; max-width: 14rem; font-size: 0.875rem; }
  .year-progress progress { display: block; width: 100%; }
  .overview-details { margin-block-start: 2rem; }
  .overview-details { --form-label: 10rem; }
  .overview-chart { flex: 0 1 22rem; min-width: 14rem; }
  .overview-table { width: 100%; font-size: 0.875rem; }
  .overview-table th, .overview-table td { padding-inline: 0.375rem; }
  .overview-table thead th { font-size: 0.8125rem; border-block-end: 1px solid var(--rule); }
  .overview-table .section th { padding-block-start: 0.75rem; }
  .overview-table .total > tr > * { font-weight: bold; border-block-start: 1px solid var(--rule); }

  .toolbar { display: flex; flex-wrap: wrap; gap: 1rem; align-items: baseline; justify-content: space-between; }
  .toolbar h1 small { font-size: 0.6em; font-weight: normal; color: var(--muted); }
  .options { display: flex; flex-wrap: wrap; gap: 1rem; align-items: center; margin-block-end: 1rem; }
  .year-banner { margin: 0 0 0.5rem; padding: 0.25rem 0.5rem; background: var(--selected); }

  /* The item tree */
  .tree { --row-columns: 1.25rem minmax(14rem, 1fr) auto 10rem 4.25rem; }
  .tree.all-years { --row-columns: 1.25rem minmax(14rem, 1fr) auto repeat(6, 7rem) 4.25rem; }
  .row {
    position: relative; display: grid; grid-template-columns: var(--row-columns);
    gap: 0.5rem; align-items: baseline; padding: 0.125rem 0.25rem;
  }
  .row.head { font-weight: bold; }
  .row:not(.head):hover { background: color-mix(in srgb, currentColor 5%, transparent); }
  .row .name { display: flex; align-items: baseline; gap: 0.125rem; padding-inline-start: calc(var(--depth, 0) * 1.25rem); min-width: 0; }
  .row .name .inline-field { flex: 1; min-width: 0; }
  .row .inline-field-input[readonly] { cursor: inherit; }
  .row .value { display: flex; gap: 0.375rem; justify-content: end; align-items: baseline; }
  .row .value .action-button { font-size: 0.75em; }
  .row-handle { cursor: grab; color: var(--muted); user-select: none; visibility: hidden; }
  .row:hover .row-handle { visibility: visible; }
  .chevron { display: inline-block; flex: none; width: 1.25em; text-align: center; }
  .row[aria-current="true"] { background: var(--selected); }
  .row[data-drop="before"]::before, .row[data-drop="after"]::after {
    content: ""; position: absolute; inset-inline: 0; height: 2px; background: var(--accent);
  }
  .row[data-drop="before"]::before { top: 0; }
  .row[data-drop="after"]::after { bottom: 0; }
  .row[data-drop="into"] { outline: 2px solid var(--accent); outline-offset: -2px; }
  /* Icons */
  .icon-growth { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M2 11.5l4-4 3 3 5-5.5M10 5h4v4'/%3E%3C/svg%3E"); }
  .icon-derived { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6.5 9.5l3-3M7.25 4.75l1-1a2.47 2.47 0 0 1 3.5 3.5l-1 1M8.75 11.25l-1 1a2.47 2.47 0 0 1-3.5-3.5l1-1'/%3E%3C/svg%3E"); }
  .icon-sensitive { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3.75 7.5h8.5v6h-8.5zM5.5 7.5V5.25a2.5 2.5 0 0 1 5 0V7.5'/%3E%3C/svg%3E"); }
  .icon-person { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='8' cy='5.25' r='2.5'/%3E%3Cpath d='M3 13.5c0-2.5 2.2-4 5-4s5 1.5 5 4'/%3E%3C/svg%3E"); }
  .icon-label { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M2.5 2.5h5l6 6-5 5-6-6z'/%3E%3Cpath d='M5.5 5.5h.01'/%3E%3C/svg%3E"); }
  .icon-note { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M2.75 13.25l.6-3L10.5 3.1l2.4 2.4-7.15 7.15zM9 4.6l2.4 2.4'/%3E%3C/svg%3E"); }
  .icon-ended { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M8 2.5l6 10.75H2zM8 6.75v3M8 11.6h.01'/%3E%3C/svg%3E"); }
  .badges { display: flex; gap: 0.5rem; align-items: center; white-space: nowrap; color: var(--muted); }
  .row-actions { justify-self: end; display: flex; gap: 0.125rem; }
  .row-actions > button { padding: 0 0.375rem; }
  .row-delete, .row-add { visibility: hidden; color: var(--muted); }
  .row:hover :is(.row-delete, .row-add), :is(.row-delete, .row-add):focus-visible { visibility: visible; }
  .row-add:hover, .row-delete:hover { color: inherit; }
  .row-menu-button[aria-expanded="true"] { anchor-name: --row-menu; }
  .row-menu {
    position-anchor: --row-menu; position-area: block-end span-inline-start; margin: 0;
    padding: 0.25rem; border: 1px solid var(--rule); min-width: 14rem;
  }
  .row-menu > button, .row-menu summary { display: block; width: 100%; padding: 0.25rem 0.5rem; text-align: start; background: none; border: none; color: inherit; cursor: pointer; }
  .row-menu > button:hover, .row-menu summary:hover { background: var(--selected); }
  .row-menu .form { padding: 0.5rem; }
  .row-menu-title { margin: 0; padding: 0.25rem 0.5rem; font-weight: bold; border-block-end: 1px solid var(--rule); }
  .row-menu-sort { display: flex; gap: 0.25rem; align-items: baseline; padding: 0.25rem 0.5rem; }
  .row-menu-sort > span { flex: 1; }
  .row-menu-sort > button { padding: 0 0.375rem; background: none; border: 1px solid var(--rule); color: inherit; cursor: pointer; font-size: 0.8125rem; }
  .row-menu-sort > button:hover { background: var(--selected); }
  .row-menu-move { display: block; width: 100%; margin-block: 0.125rem; padding: 0.25rem; background: none; border: none; color: inherit; cursor: pointer; }
  .tree-add-button .named { font-weight: bold; }
  .tree-add { padding: 0.25rem 0.25rem 0.25rem 3.25rem; }
  .tree-add-button { color: var(--muted); anchor-name: --tree-add; }
  .tree-add-button:hover { color: inherit; }
  .tree-add-menu { position-anchor: --tree-add; position-area: block-end span-inline-end; }
  .category-button { anchor-name: --category; }
  .category-menu { position-anchor: --category; position-area: block-end span-inline-end; }
  .category-menu .browse-link { display: flex; gap: 2rem; justify-content: space-between; padding: 0.25rem 0.5rem; text-decoration: none; }
  .category-menu .browse-link:hover, .category-menu .browse-link[aria-current] { background: var(--selected); }
  .category-keys { margin: 0; padding: 0.25rem 0.5rem; font-size: 0.8125rem; border-block-start: 1px solid var(--rule); }

  .tags { display: flex; flex-wrap: wrap; gap: 0.25rem; align-items: baseline; }
  .pill-remove { padding-inline-start: 0.125rem; }
  .tag-input { width: 8rem; }
  #inspector textarea { width: 100%; min-height: 4rem; }

  /* A comparison */
  .comparison tbody tr:first-child > * { border-block-start: 1px solid var(--rule); }
  .comparison .difference { font-weight: 600; }
  .changes { padding-inline-start: 1.25rem; }

  /* The range slider */
  .range-slider { --thumb: 0.625rem; display: block; padding-inline: calc(var(--thumb) / 2); }
  .range-slider-track { position: relative; display: block; height: 1rem; }
  .range-slider-track::before, .range-slider-track::after { content: ""; position: absolute; top: calc(50% - 0.5px); height: 1px; }
  .range-slider-track::before { inset-inline: 0; background: var(--muted); opacity: 0.5; }
  .range-slider-track::after {
    inset-inline: calc(var(--low) / var(--max) * 100%) calc((var(--max) - var(--high)) / var(--max) * 100%);
    top: calc(50% - 1px); height: 2px; background: currentColor;
  }
  .range-slider-handle {
    position: absolute; inset-block: 0; inset-inline: calc(var(--thumb) / -2); width: calc(100% + var(--thumb)); height: 100%; margin: 0;
    appearance: none; background: none; pointer-events: none; z-index: 1;
  }
  .range-slider-handle::-webkit-slider-runnable-track { background: none; }
  .range-slider-handle::-moz-range-track { background: none; }
  .range-slider-handle::-webkit-slider-thumb {
    appearance: none; pointer-events: auto; cursor: ew-resize; box-sizing: border-box;
    width: var(--thumb); height: var(--thumb); border-radius: 50%; background: Canvas; border: 1.5px solid CanvasText;
  }
  .range-slider-handle::-moz-range-thumb {
    pointer-events: auto; cursor: ew-resize; box-sizing: border-box;
    width: var(--thumb); height: var(--thumb); border-radius: 50%; background: Canvas; border: 1.5px solid CanvasText;
  }
  .range-slider-handle[data-open]::-webkit-slider-thumb { border: 0; border-radius: 0; background: CanvasText; clip-path: polygon(20% 0, 100% 50%, 20% 100%); }
  .range-slider-handle[data-open]::-moz-range-thumb { border: 0; border-radius: 0; background: CanvasText; clip-path: polygon(20% 0, 100% 50%, 20% 100%); }
  .range-slider-handle:focus-visible { outline: none; }
  .range-slider-handle:focus-visible::-webkit-slider-thumb { outline: 2px solid var(--accent); outline-offset: 1px; }
  .range-slider-handle:focus-visible::-moz-range-thumb { outline: 2px solid var(--accent); outline-offset: 1px; }
  .range-slider-stops {
    display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); margin-inline: calc(-50% / var(--max));
    font-size: 0.6875rem; text-align: center; white-space: nowrap; color: var(--muted);
  }
  .range-slider-stops > span { position: relative; }
  .range-slider-stops > span::before { content: ""; position: absolute; inset-inline-start: calc(50% - 0.5px); bottom: calc(100% + 0.5rem - 2.5px); width: 1px; height: 5px; background: currentColor; }
  .range-slider-stops > :not([data-in-range]) { opacity: 0.5; }

  /* An item's inspector */
  #inspector dl { grid-template-columns: auto minmax(0, 1fr); align-items: baseline; }
  .inspector-section { display: grid; gap: 0.375rem; margin-block-start: 0.75rem; padding-block-start: 0.75rem; border-block-start: 1px solid var(--rule); }
  .inspector-row { display: flex; gap: 0.75rem; align-items: baseline; justify-content: space-between; min-width: 0; }
  .inspector-label { margin: 0; color: var(--muted); white-space: nowrap; }
  .inspector-row .inline-field { width: 8rem; }
  .inspector-row .action-button, .year-table .action-button { padding: 0; border: none; background: none; color: inherit; cursor: pointer; text-decoration: underline; text-decoration-color: var(--muted); text-underline-offset: 0.2em; }
  .inspector-section .choice-buttons { justify-self: start; font-size: inherit; }
  .inspector-section .choice-buttons-option > span { padding-inline: 0.375rem; }
  .growth-details { display: grid; gap: 0.375rem; }
  .growth-details .inspector-row .inline-field { width: 5.5rem; }
  .year-table { width: 100%; }
  .year-table th, .year-table td { padding-inline: 0 0.5rem; white-space: nowrap; }
  .year-table th { color: var(--muted); }
  .year-table .inline-field { width: 5rem; }
  .year-table .action-button { margin-inline-start: 0.375rem; }
  .trend, .trend-chart { margin: 0 0 0.75rem; }
  .trend-chart figcaption { font-size: 0.8125rem; color: var(--muted); }
  #inspector .combobox { display: block; }
  #inspector .inline-field-error { margin-block-start: 0.25rem; }
  #inspector .title { font-size: 1.25rem; }

  /* The cohort grid */
  .cohorts { font-size: 0.875rem; }
  .cohorts th, .cohorts td { padding: 0; text-align: end; }
  .cohorts th, .cohorts td.num, .cohorts td > .num { padding: 0.25rem; white-space: nowrap; }
  .cohorts tbody th, .cohorts tfoot th { text-align: start; }
  .cohorts .inline-field-input { min-width: 4.25rem; padding: 0.25rem; outline-offset: -2px; }
  .cohorts .inline-field-error { padding: 0 0.5rem 0.25rem; }
  .cohorts td:focus-within { background: var(--carried); }
  .cohort-0 { background: color-mix(in srgb, var(--accent) 20%, transparent); }
  .cohort-1 { background: color-mix(in srgb, var(--accent) 15%, transparent); }
  .cohort-2 { background: color-mix(in srgb, var(--accent) 10%, transparent); }
  .cohort-3 { background: color-mix(in srgb, var(--accent) 5%, transparent); }
  .cohorts.carry-over :is(tr:has(> td:nth-child(2):focus-within) + tr > td:nth-child(3), tr:has(> td:nth-child(2):focus-within) + tr + tr > td:nth-child(4), tr:has(> td:nth-child(2):focus-within) + tr + tr + tr > td:nth-child(5), tr:has(> td:nth-child(2):focus-within) + tr + tr + tr + tr > td:nth-child(6), tr:has(> td:nth-child(2):focus-within) + tr + tr + tr + tr + tr > td:nth-child(7)) { background: var(--carried); }
  .cohorts.carry-over :is(tr:has(> td:nth-child(3):focus-within) + tr > td:nth-child(4), tr:has(> td:nth-child(3):focus-within) + tr + tr > td:nth-child(5), tr:has(> td:nth-child(3):focus-within) + tr + tr + tr > td:nth-child(6), tr:has(> td:nth-child(3):focus-within) + tr + tr + tr + tr > td:nth-child(7)) { background: var(--carried); }
  .cohorts.carry-over :is(tr:has(> td:nth-child(4):focus-within) + tr > td:nth-child(5), tr:has(> td:nth-child(4):focus-within) + tr + tr > td:nth-child(6), tr:has(> td:nth-child(4):focus-within) + tr + tr + tr > td:nth-child(7)) { background: var(--carried); }
  .cohorts.carry-over :is(tr:has(> td:nth-child(5):focus-within) + tr > td:nth-child(6), tr:has(> td:nth-child(5):focus-within) + tr + tr > td:nth-child(7)) { background: var(--carried); }
  .cohorts.carry-over :is(tr:has(> td:nth-child(6):focus-within) + tr > td:nth-child(7)) { background: var(--carried); }

  /* The playground */
  .playground-range { max-width: 22rem; }
  .contact-rows-list { display: grid; gap: 0.25rem; justify-items: start; }
  .contact-row { display: flex; gap: 0.5rem; align-items: baseline; }
  .playground-grid .inline-field { width: 7rem; }
}
