/*
 * The whole stylesheet, hand-written. Served as a hashed, immutable file from routes_static.rs,
 * so it is fetched once and cached — nothing is compiled in the browser.
 *
 * Two layers: `base` styles bare elements, `components` styles the `sl-*` classes. Ordering them
 * explicitly means a component rule always beats an element rule without needing to out-specify
 * it, so every selector here can stay a single class deep.
 *
 * Kept in step with web/src/style.css in the shopping-list repo — that app and this one are two
 * front ends over the same design. It still uses Tailwind, so the rules are no longer
 * copy-pasteable; the values below are the shared contract.
 */

@import url("https://fonts.googleapis.com/css2?family=Courier+Prime:ital,wght@0,400;0,700;1,400;1,700&display=swap");

@layer base, components;

@layer base {
  :root {
    font-size: 16px;
    color-scheme: dark;
    font-synthesis: none;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;

    --sl-accent: #ff4500;
    --sl-bg: #000;
    --sl-fg: #fff;
    --sl-dim: #9ca3af;
    --sl-surface: #18181b;
    --sl-line: #3f3f46;
    --sl-disabled: #71717a;

    /* The signature: everything raised off the page sits on the same 4px diagonal. */
    --sl-offset: 4px;
    --sl-raised: var(--sl-offset) var(--sl-offset) 0 0 var(--sl-accent);
    --sl-ease: 300ms cubic-bezier(0.4, 0, 0.2, 1);
    /* One vertical rhythm, used by `.sl-flow` and overridable per container. */
    --sl-gap: 1rem;
  }

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

  html,
  body {
    margin: 0;
    overscroll-behavior-x: none;
    background-color: var(--sl-bg);
    color: var(--sl-fg);
    font-family: 'Courier Prime', monospace;
  }

  /*
   * Headings carry no size of their own — the design runs at one size, and a heading is marked by
   * case and tracking instead. See `.sl-title`.
   */
  h1,
  h2 {
    font: inherit;
    margin: 0;
  }

  p {
    margin: 0;
  }

  /* Every <small> in the app is a timestamp or a locale code. */
  small {
    font-size: 80%;
    color: var(--sl-dim);
  }
  small.sl-extra-small {
    font-size: 50%;
  }

  a {
    color: inherit;
    text-decoration: none;
  }

  /* Every label in the app wraps its input, so none of them want to be inline. */
  label {
    display: block;
  }

  /* Form controls inherit nothing by default, and the monospace face is the whole look. */
  input,
  button,
  select,
  textarea {
    font: inherit;
    color: inherit;
    background: transparent;
    margin: 0;
    appearance: none;
    border-radius: 0;
  }

  button {
    cursor: pointer;
  }

  :disabled {
    cursor: not-allowed;
  }

  ::placeholder {
    color: var(--sl-dim);
    opacity: 1;
  }

  /* Used purely as a grouping element; its default frame and legend notch are not wanted. */
  fieldset {
    margin: 0;
    padding: 0;
    border: 0;
    min-inline-size: 0;
  }

  legend {
    padding: 0;
  }

  svg {
    display: block;
  }

  table {
    border-collapse: collapse;
  }
}

@layer components {
  /* ---------------------------------------------------------------- shell */

  /*
   * Two elements, not one: an element cannot container-query itself, so the `container-type`
   * holder and the `cqw` consumer have to be separate. `.sl-content` sizes itself from the app's
   * own inline size, not the viewport, so text stops growing once the column hits its max.
   */
  .sl-app {
    max-inline-size: 32rem;
    margin-inline: auto;
    padding: 1rem;
    min-block-size: 100vh;
    container-type: inline-size;
  }

  .sl-content {
    font-size: calc(100cqw / 15);
  }

  /*
   * The one spacing rule in the file. Anything that wants vertical rhythm between its children
   * asks for it here rather than each child carrying its own margin.
   */
  .sl-flow > * + * {
    margin-block-start: var(--sl-gap);
  }

  .sl-flow-tight {
    --sl-gap: 0.25rem;
  }

  .sl-stack {
    display: flex;
    flex-direction: column;
    gap: var(--sl-gap);
  }

  /* ---------------------------------------------------- the raised box */

  /*
   * The design has exactly one box treatment: a hard 2px frame with the accent cast behind it.
   * Declared once here; the components below only add what makes them different.
   */
  .sl-button,
  .sl-input,
  .sl-streak {
    border: 2px solid var(--sl-fg);
    background-color: var(--sl-bg);
    box-shadow: var(--sl-raised);
  }

  /* ------------------------------------------------------------ headings */

  /* A control at each end, title centred between them. Also used with just a back arrow. */
  .sl-head {
    display: flex;
    align-items: center;
    justify-content: space-between;

    & h1 {
      flex-grow: 1;
      text-align: center;
    }
  }

  /* The page header proper is set off by more than the standard rhythm. */
  .sl-nav {
    margin-block-end: 2rem;
  }

  /* Balances the back button so the title stays optically centred. */
  .sl-nav-spacer {
    inline-size: 1.5rem;
  }

  .sl-title {
    text-transform: uppercase;
    letter-spacing: -0.025em;
  }

  .sl-blurb {
    text-align: center;
    color: var(--sl-dim);
  }

  .sl-icon {
    inline-size: 1.5rem;
    block-size: 1.5rem;
  }

  /* ------------------------------------------------------------ footer */

  .sl-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 60%;

    & .sl-languages {
      flex-grow: 1;
      text-align: center;
    }
  }

  /* ------------------------------------------------------------- buttons */

  .sl-button {
    display: inline-block;
    padding: 0.5rem 1rem;
    color: var(--sl-fg);
    text-transform: uppercase;
    cursor: pointer;
    transition: all var(--sl-ease);

    /* Pressing travels down the same diagonal the shadow casts along. */
    &:hover:not(:disabled) {
      transform: translate(2px, 2px);
      box-shadow: 2px 2px 0 0 var(--sl-accent);
      background-color: var(--sl-accent);
      color: var(--sl-bg);
      border-color: var(--sl-accent);
    }

    &:active:not(:disabled) {
      transform: translate(4px, 4px);
      box-shadow: none;
    }

    &:disabled {
      opacity: 0.5;
      box-shadow: none;
      transform: none;
    }
  }

  .sl-button-wide {
    inline-size: 100%;
  }

  .sl-button-sm {
    padding: 0.25rem 0.75rem;
    white-space: nowrap;
  }

  /* Icon-sized, and sits on a shallower diagonal than a full button. */
  .sl-button-nav {
    display: inline-block;
    padding: 0.25rem 0.5rem;
    border: 2px solid var(--sl-fg);
    box-shadow: 2px 2px 0 0 var(--sl-accent);
    transition:
      color var(--sl-ease),
      background-color var(--sl-ease);

    &:hover {
      background-color: var(--sl-accent);
      color: var(--sl-bg);
    }

    &:active {
      transform: translate(2px, 2px);
      box-shadow: none;
    }
  }

  /* --------------------------------------------------------------- links */

  .sl-link {
    text-decoration: underline;
    text-decoration-thickness: 2px;
    text-underline-offset: 4px;
    cursor: pointer;

    &:hover {
      text-decoration-thickness: 4px;
    }
  }

  .sl-link-back {
    display: block;
    text-align: center;
    color: var(--sl-dim);
    text-decoration: underline;

    &:hover {
      color: var(--sl-fg);
    }
  }

  .sl-end {
    text-align: end;
  }

  /* --------------------------------------------------------------- forms */

  .sl-input {
    inline-size: 100%;
    padding: 0.5rem 0.75rem;
    color: var(--sl-fg);
    outline: none;
    transition: all var(--sl-ease);

    /* Focus trades the offset shadow for a ring, held off the field by a black gap. */
    &:focus {
      box-shadow:
        0 0 0 2px var(--sl-bg),
        0 0 0 4px var(--sl-accent);
    }
  }

  .sl-input-ro {
    inline-size: 100%;
    padding: 0.5rem 0.75rem;
    border: 2px dashed var(--sl-disabled);
    background-color: var(--sl-surface);
    color: var(--sl-disabled);
    cursor: not-allowed;
  }

  .sl-field-label {
    display: block;
    margin-block-end: 0.25rem;
  }

  .sl-error {
    text-align: center;
    color: var(--sl-accent);
  }

  /* ------------------------------------------------------------ notices */

  .sl-spacer {
    margin-block: 1.5rem;
    border-block-end: 4px dashed var(--sl-fg);
    opacity: 0.5;
  }

  .sl-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-block-end: 2rem;
    padding: 1rem;
    border: 1px solid var(--sl-line);
    background-color: var(--sl-surface);
    box-shadow: var(--sl-raised);
    color: var(--sl-dim);
  }

  .sl-empty {
    text-align: center;
    font-style: italic;
    color: var(--sl-dim);
  }

  /* ------------------------------------------------------------- streaks */

  .sl-streak {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.75rem;

    /* The [+] and [->] controls at either end. */
    & a {
      flex-shrink: 0;
      padding-inline: 0.25rem;
      white-space: nowrap;
      transition:
        color var(--sl-ease),
        background-color var(--sl-ease);

      &:hover {
        background-color: var(--sl-accent);
        color: var(--sl-bg);
      }
    }

    /* Control and text column share the row; the column takes the slack and truncates. */
    & div {
      display: flex;
      flex: 1;
      align-items: center;
      gap: 0.75rem;
      min-inline-size: 0;
    }

    & div div {
      flex-direction: column;
      align-items: stretch;
      gap: 0;
    }

    & span {
      text-transform: uppercase;
      letter-spacing: 0.025em;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    /* Two classes deep on purpose: it has to outrank the `& span` rule above it. */
    & .sl-streak-ago {
      text-transform: none;
      letter-spacing: normal;
      color: var(--sl-dim);
    }
  }

  .sl-streak-edit {
    float: inline-end;
    opacity: 0.5;
  }

  /* ------------------------------------------------------------ calendar */

  .sl-calendar {
    inline-size: 100%;
    table-layout: fixed;
    text-align: center;
    border: 2px solid var(--sl-fg);

    & th {
      padding-block: 0.25rem;
    }

    & td {
      block-size: 2.25rem;
      padding: 0;
      vertical-align: middle;
    }

    /* Scoped to tbody: the head's only row is a `:last-child` too, and it must keep its rule. */
    & tr {
      border-block-end: 2px solid var(--sl-fg);
    }

    & tbody tr:last-child {
      border-block-end: 0;
    }

    & th,
    & td {
      border-inline-end: 2px solid var(--sl-fg);

      &:last-child {
        border-inline-end: 0;
      }
    }
  }

  .sl-month-label {
    display: inline-block;
    margin-block-end: 0.5rem;
    text-transform: uppercase;
    border-block-end: 2px solid var(--sl-fg);
  }

  .sl-day {
    display: flex;
    inline-size: 100%;
    block-size: 100%;
    align-items: center;
    justify-content: center;
  }

  /* A day the streak was kept. */
  .sl-day-on {
    background-color: var(--sl-accent);
    color: var(--sl-bg);
  }
}

/*
 * Motion, such as it is. Short, stepped rather than eased, and along the same 4px diagonal the
 * buttons already travel on press — the screen should read as repainting, not dissolving.
 */

/* Page to page. Both documents have to opt in, which every page does by sharing this stylesheet.
 * Engines without cross-document view transitions simply navigate, which is the same screen
 * without the cut. */
@view-transition {
  navigation: auto;
}

::view-transition-old(root),
::view-transition-new(root) {
  animation-duration: 300ms;
  animation-timing-function: steps(3, jump-end);
}

::view-transition-old(root) {
  animation-name: sl-cut-out;
}

::view-transition-new(root) {
  animation-name: sl-cut-in;
}

@keyframes sl-cut-out {
  to {
    opacity: 0;
    transform: translate(-4px, -4px);
  }
}

@keyframes sl-cut-in {
  from {
    opacity: 0;
    transform: translate(4px, 4px);
  }
}

/*
 * Fragment swaps. Deliberately not a view transition: those snapshot the whole viewport, so the
 * displacement above would jog the entire screen for what is often a one-line change.
 *
 * htmx marks incoming content with `.htmx-added` and drops it one settle later — the same moment
 * it wires the content up and honours an `autofocus` in it. So the cut is a transition *out of*
 * that class rather than an animation held on by it: a transition outlives the class that started
 * it, where an animation would be cancelled with it, and the settle can stay at the default 20ms
 * instead of being stretched to the length of the cut. Stretching it left every swapped-in
 * fragment inert for as long as the animation ran, and a tap inside that window fell through the
 * form to the browser.
 *
 * The resting state is the visible one, so content that is never transitioned — reduced motion, a
 * throttled tab — is shown rather than hidden.
 */
.sl-swap {
  transition:
    opacity 300ms steps(3, jump-end),
    transform 300ms steps(3, jump-end);
}

.sl-swap.htmx-added {
  opacity: 0;
  transform: translate(4px, 4px);
}

@media (prefers-reduced-motion: reduce) {
  ::view-transition-old(root),
  ::view-transition-new(root) {
    animation: none;
  }

  .sl-swap {
    transition: none;
  }
}
