/* Plain CSS, no framework (doc/done/v0-2_implementation_plan.md): tokens, a
   small base for native elements, BEM components, then the page layout.
   Layer order is precedence order, so no specificity fights. Light only
   for now: the spec hasn't designed dark mode. */
@layer tokens, base, components, layout;

@layer tokens {
  :root {
    --bg: #fbfbfa;
    --text: #1c1c1a;
    --muted: #6b6b66;
    --accent: #2f4a3a;
    --accent-hover: #1c2e24;
    --accent-soft: #e2e9e4;
    --surface: #ecebe6;
    --rule: #e3e2dc;
    --divider: #ecebe6;
    --input-border: #c9c8c1;
    --online: #1f9d55;
    --away: #8a6a1f;
    --reachable: #a3a29b;
    --danger: #9b3324;
    --danger-border: #e0c4bf;
    --non-member: #5c5b55;
    --deploy-bg: #ffff00;
    --deploy-text: #7a6f00;

    --font: system-ui, -apple-system, "Segoe UI", sans-serif;
    --font-display: Newsreader, Georgia, serif;

    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;

    --radius: 12px;
    --target: 44px;
    --bar-height: 64px;
  }
}

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

  body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 16px/1.4 var(--font);
  }

  button, input, textarea { font: inherit; color: inherit; }
  button { cursor: pointer; }
  a { color: var(--accent); }
  a:hover { color: var(--accent-hover); }
  :focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
  ::placeholder { color: #8a8983; }
  /* Author display rules would otherwise beat the UA's [hidden]. */
  [hidden] { display: none !important; }
}

@layer components {
  .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  /* Page header: back arrow + title, or a section's title and actions. */
  .bar {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    height: var(--bar-height);
    padding: 0 var(--space-3) 0 var(--space-1);
    border-bottom: 1px solid var(--rule);
  }
  .bar__title { flex-grow: 1; margin: 0; font-size: 17px; font-weight: 600; }
  .bar--section { padding-left: var(--space-4); }
  .bar--section .bar__title { font-size: 22px; font-weight: 700; }

  /* Chats, Contacts, You: a tab bar on a phone, a rail on desktop (the
     layout layer places it). */
  .sections {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    padding: var(--space-1) 0 max(var(--space-2), env(safe-area-inset-bottom));
    border-top: 1px solid var(--rule);
  }
  .sections__link {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-height: var(--target);
    color: var(--muted);
    font-size: 12px;
    text-decoration: none;

    &:hover { color: var(--text); }
    &[aria-current="page"] { color: var(--accent); font-weight: 600; }
  }
  .sections__badge {
    position: absolute;
    top: 0;
    left: calc(50% + 4px);
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border: 2px solid var(--bg);
    font-size: 11px;

    &:empty { display: none; }
  }

  .icon-btn {
    display: grid;
    place-items: center;
    flex-shrink: 0;
    width: var(--target);
    height: var(--target);
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--text);
    text-decoration: none;
  }
  .icon-btn--accent { color: var(--accent); }

  /* Initials on a colour picked per person (MonkTalk::Person#color), set
     inline as --avatar-bg; sizes and the dot follow --size / --dot. */
  .avatar {
    --size: 44px;
    --dot: 12px;
    position: relative;
    display: grid;
    place-items: center;
    flex-shrink: 0;
    width: var(--size);
    height: var(--size);
    border-radius: 50%;
    background: var(--avatar-bg, var(--non-member));
    color: #fff;
    font-size: calc(var(--size) * 0.36);
    font-weight: 600;
  }
  .avatar--sm { --size: 36px; --dot: 10px; }
  .avatar--lg { --size: 96px; --dot: 18px; }
  .avatar__dot {
    position: absolute;
    right: -1px;
    bottom: -1px;
    width: var(--dot);
    height: var(--dot);
    border: 2px solid var(--bg);
    border-radius: 50%;
    background: var(--online);
  }
  /* Away: a ring, told from online by its shape, not only its colour. */
  .avatar__dot--away { background: var(--bg); box-shadow: inset 0 0 0 calc(var(--dot) * 0.22) var(--away); }
  /* Reachable: offline, but notifications are on (Panes.reachable). */
  .avatar__dot--reachable { background: var(--reachable); }
  .avatar__dot--offline { display: none; }
  .avatar--lg .avatar__dot { right: 2px; bottom: 2px; border-width: 3px; }

  /* A person in a list (Chats, Contacts): avatar, name, email below. */
  .row {
    display: flex;
    align-items: center;
    gap: 14px;
    width: 100%;
    min-height: 72px;
    padding: 0 var(--space-4);
    border: 0;
    border-bottom: 1px solid var(--divider);
    background: transparent;
    color: var(--text);
    text-align: left;
    text-decoration: none;

    &:hover { color: var(--text); background: color-mix(in srgb, var(--surface) 50%, transparent); }
    &[aria-current="page"] { background: var(--surface); }
  }
  .row__text { display: flex; flex-direction: column; flex-grow: 1; min-width: 0; }
  .row__title { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .row__sub { font-size: 14px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .row__sub--note { font-style: italic; }
  /* A Chats row: when, and how many unread (empty at 0). */
  .row__meta { display: flex; flex-direction: column; align-items: flex-end; gap: var(--space-1); flex-shrink: 0; }
  .row__time { font-size: 13px; color: var(--muted); }
  .row__unread, .sections__badge {
    display: grid;
    place-items: center;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 10px;
    background: var(--accent);
    color: #fff;
    font-size: 12px;
    font-weight: 700;
  }
  .row__unread:empty { visibility: hidden; }
  .row:has(.row__unread:not(:empty)) {
    & .row__title { font-weight: 700; }
    & .row__sub { color: var(--text); }
    & .row__time { color: var(--accent); font-weight: 600; }
  }

  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 52px;
    padding: 0 20px;
    border: 1px solid transparent;
    border-radius: var(--radius);
    background: var(--accent);
    color: #fff;
    font-size: 17px;
    font-weight: 600;
    text-decoration: none;

    &:hover { color: #fff; background: var(--accent-hover); }
  }
  .btn--secondary {
    border-color: var(--input-border);
    background: transparent;
    color: var(--text);

    &:hover { color: var(--text); background: var(--surface); }
  }
  .btn--danger {
    background: var(--danger);

    &:hover { background: color-mix(in srgb, var(--danger) 85%, black); }
  }
  .btn--danger-outline {
    border-color: var(--danger-border);
    background: transparent;
    color: var(--danger);

    &:hover { color: var(--danger); background: color-mix(in srgb, var(--danger) 6%, transparent); }
  }
  .btn--pill { height: 36px; padding: 0 14px; border-radius: 18px; font-size: 15px; }
  .btn--pill.btn--secondary { color: var(--accent); font-size: 14px; }
  /* Text only: Import, Log out. */
  .btn--quiet {
    height: auto;
    min-height: var(--target);
    padding: 0 10px;
    background: transparent;
    color: var(--accent);
    font-size: 15px;

    &:hover { color: var(--accent-hover); background: transparent; }
    &.btn--danger, &.btn--danger:hover { color: var(--danger); font-weight: 400; }
  }

  .label { font-size: 14px; color: var(--muted); }
  .field--text { height: auto; min-height: 76px; padding: 12px var(--space-4); }

  .field {
    width: 100%;
    height: 52px;
    resize: none;
    padding: 0 var(--space-4);
    border: 1px solid var(--input-border);
    border-radius: var(--radius);
    background: #fff;
    font-size: 17px;
  }

  .search {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    height: var(--target);
    padding: 0 14px;
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--muted);

    & input { flex-grow: 1; min-width: 0; height: 40px; border: 0; background: transparent; color: var(--text); }
  }


}

@layer layout {
  /* Logged in: on a phone one pane at a time, the list with the tab bar
     under it or a detail screen alone; from 900px the rail and both panes
     side by side (the media query at the end). */
  .app { display: flex; flex-direction: column; height: 100dvh; }
  .app__left, .app__right { display: flex; flex-direction: column; flex-grow: 1; min-height: 0; }
  .sections { order: 1; flex-shrink: 0; }
  .app[data-view="list"] .app__right,
  .app[data-view="detail"] .app__left,
  .app[data-view="detail"] .sections { display: none; }
  .list { flex-grow: 1; min-height: 0; overflow-y: auto; }
  /* Only the list or the messages scroll; the bars around them keep
     their size however long the content gets. */
  .bar, .contacts__search, .composer { flex-shrink: 0; }

  .empty {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: 0 var(--space-6);
    text-align: center;

    & p { margin: 0; }
    & .btn { height: var(--target); margin-top: var(--space-2); font-size: 16px; }
  }
  .empty__title { font-size: 17px; font-weight: 600; }
  .empty__text { font-size: 15px; color: var(--muted); }
  /* "No chats yet" goes once the first row arrives, live or not. */
  .empty:has(+ .list .row) { display: none; }

  .wordmark { margin: 0; font-family: var(--font-display); font-size: 44px; font-weight: 500; letter-spacing: -0.02em; }
  .wordmark--sm { font-size: 36px; }

  .welcome {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 40px;
    max-width: 26rem;
    min-height: 100dvh;
    margin: 0 auto;
    padding: 0 var(--space-6);

    & p { margin: 0; }
  }
  .welcome__brand { display: flex; flex-direction: column; gap: var(--space-2); text-align: center; }
  .welcome__tagline { font-size: 17px; color: var(--muted); }
  .welcome__notice { font-size: 15px; color: var(--danger); text-align: center; }
  .welcome__form { display: flex; flex-direction: column; gap: var(--space-3); }
  .welcome__sent { display: flex; flex-direction: column; gap: var(--space-4); font-size: 17px; text-align: center; }
  .welcome__hint { font-size: 15px; color: var(--muted); text-align: center; }
  .welcome__points {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    margin: 0;
    padding: 0;
    list-style: none;
    font-size: 15px;

    /* A check, drawn: currentColor in a mask, so it takes the accent. */
    & li { display: flex; gap: 10px; }
    & li::before {
      content: "";
      flex-shrink: 0;
      width: 18px;
      height: 18px;
      margin-top: 1px;
      background: var(--accent);
      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 6 9 17l-5-5'/%3E%3C/svg%3E") center / contain no-repeat;
    }
  }

  /* The open chat: header, messages (newest at the bottom), composer. */
  .chat { display: flex; flex-direction: column; flex-grow: 1; min-height: 0; }
  .chat__name { display: flex; flex-direction: column; line-height: 1.25; }
  .chat__info {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    flex-shrink: 0;
    min-width: var(--target);
    height: var(--target);
    color: var(--accent);
    font-size: 14px;
    font-weight: 500;
    text-decoration: none;
  }
  .chat__info-label { display: none; }
  .chat__status { font-size: 13px; font-weight: 400; color: var(--muted); }
  .chat__who {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-grow: 1;
    min-height: var(--target);
    color: var(--text);
    font-weight: 600;
    text-decoration: none;

    &:hover { color: var(--text); }
  }

  .msgs {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    flex-grow: 1;
    overflow-y: auto;
    padding: var(--space-4);

    /* Pushes a short history to the bottom without breaking scrolling
       (justify-content: flex-end would). A pseudo-element, since the
       message containers below have no box of their own. */
    &::before { content: ""; margin-top: auto; }
  }
  /* Earlier pages, the current page and failed sends: their messages are
     flex items of .msgs. */
  .msgs__older, .msgs__current, .msgs__failed { display: contents; }
  .msgs__earlier { align-self: center; }
  .msg {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    max-width: 75%;
  }
  .msg--mine { align-self: flex-end; align-items: flex-end; }
  .msg--theirs { align-self: flex-start; align-items: flex-start; }
  .msg__bubble {
    margin: 0;
    padding: 10px 14px;
    border-radius: 18px 18px 18px 4px;
    background: var(--surface);
    line-height: 1.4;
    overflow-wrap: anywhere;
  }
  .msg--mine .msg__bubble { border-radius: 18px 18px 4px 18px; background: var(--accent); color: #fff; }
  .msg--ephemeral .msg__bubble { padding: 9px 13px; border: 1.5px dashed #8a897f; background: var(--bg); color: var(--text); }
  .msg--mine.msg--ephemeral .msg__bubble { border-color: var(--accent); }
  /* "18:40", or "Not saved · 18:40", under each bubble (app.js writes the
     local time). */
  .msg__meta { font-size: 12px; color: var(--muted); }
  /* "Today" above the first message of each day (app.js adds them). */
  .msgs__day {
    align-self: center;
    margin: var(--space-2) 0;
    padding: 2px 10px;
    border-radius: 10px;
    background: var(--surface);
    color: var(--muted);
    font-size: 12px;
  }
  .msg--failed .msg__bubble { opacity: 0.6; }
  .msg__retry {
    padding: 0;
    border: 0;
    background: none;
    color: var(--danger);
    font-size: 12px;
    text-decoration: underline;
  }

  .banner {
    position: fixed;
    inset: 0 0 auto;
    z-index: 1;
    margin: 0;
    padding: var(--space-1) var(--space-4);
    background: var(--text);
    color: var(--bg);
    font-size: 13px;
    text-align: center;
  }

  /* "Staging · 0.4.0": which deploy a page is, for testers and
     screenshots. A flat pill at the top edge, above the bars' buttons
     (they start lower), and never in the way of a tap. */
  .deploy-badge {
    position: fixed;
    top: 2px;
    right: var(--space-3);
    z-index: 2;
    margin: 0;
    padding: 0 var(--space-2);
    border-radius: 999px;
    background: var(--deploy-bg);
    color: var(--deploy-text);
    font-size: 10px;
    line-height: 12px;
    font-weight: 600;
    pointer-events: none;
  }

  .composer {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-3);
    border-top: 1px solid var(--rule);
  }
  .composer__input {
    flex-grow: 1;
    min-width: 0;
    height: var(--target);
    padding: 0 var(--space-4);
    border: 1px solid var(--input-border);
    border-radius: 22px;
    background: #fff;
  }
  /* Off the record: the cloud-off toggle (with its name on desktop); when
     it's on, it's filled, a banner says so, and the field and the send
     button are dashed. */
  .composer__toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    flex-shrink: 0;
    min-width: var(--target);
    height: var(--target);
    border: 1px solid var(--input-border);
    border-radius: 22px;
    background: #fff;
    color: var(--non-member);
    font-size: 14px;
    cursor: pointer;

    &:has(:checked) { border-color: var(--accent); background: var(--accent); color: #fff; }
    &:has(:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }
  }
  .composer__toggle-label { display: none; }
  .composer__send { background: var(--accent); color: #fff; }
  .composer__banner {
    display: none;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
    margin: 0 var(--space-3);
    padding: 10px var(--space-3);
    border-radius: var(--radius) var(--radius) 0 0;
    background: var(--accent-soft);
    color: var(--accent-hover);
    font-size: 14px;

    & svg { flex-shrink: 0; }
    & b { font-weight: 600; }
  }
  .composer__off { margin-left: auto; padding: var(--space-1) 0; font-weight: 600; white-space: nowrap; cursor: pointer; }
  .chat:has([name=ephemeral]:checked) {
    & .composer__banner { display: flex; }
    & .composer__input { border: 1.5px dashed var(--accent); background: var(--bg); }
    & .composer__send { border: 1.5px dashed var(--accent); background: var(--bg); color: var(--accent); }
  }

  /* A person's page and You: a centred person, then what I can do. */
  .person {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-2);
    flex-grow: 1;
    width: 100%;
    max-width: 480px;
    margin-inline: auto;
    padding: var(--space-6) var(--space-5) var(--space-5);
    text-align: center;

    & p { margin: 0; }
  }
  .person__name { margin: var(--space-1) 0 0; font-size: 22px; font-weight: 600; }
  .person__actions { display: flex; gap: var(--space-2); margin-top: var(--space-3); & .btn { height: var(--target); font-size: 16px; } }
  .person__remove {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    align-self: stretch;
    margin-top: auto;
    padding-top: var(--space-4);
    border-top: 1px solid var(--rule);
    color: var(--muted);
    font-size: 14px;
    text-align: left;

    & .btn { flex-shrink: 0; height: 40px; font-size: 15px; }
  }
  /* After an autosave (app.js). */
  .saved { color: var(--accent); font-size: 13px; &:empty { display: none; } }
  .saved--error { color: var(--danger); }
  .person__email { font-size: 15px; color: var(--muted); }
  .person__status { font-size: 14px; color: var(--muted); }
  /* You: me, my name and line (autosaved), facts, Log out. */
  .you { display: flex; flex-direction: column; gap: var(--space-5); flex-grow: 1; overflow-y: auto; padding: var(--space-4); }
  .you__me { display: flex; align-items: center; gap: var(--space-4); & .avatar { --size: 72px; } }
  .you__name { display: block; font-size: 20px; font-weight: 600; }
  .you__email { display: block; font-size: 14px; color: var(--muted); }
  .you__fields { display: flex; flex-direction: column; gap: var(--space-2); & .label { margin-top: var(--space-2); } }
  .you__help { margin: 0; font-size: 13px; color: var(--muted); }
  .you__facts {
    margin: 0;
    border: 1px solid var(--rule);
    border-radius: var(--radius);

    & div { display: flex; justify-content: space-between; gap: var(--space-3); min-height: 52px; align-items: center; padding: 0 14px; }
    & div + div { border-top: 1px solid var(--rule); }
    & dd { margin: 0; overflow: hidden; color: var(--muted); font-size: 14px; text-overflow: ellipsis; }
  }
  /* Notifications (push/_settings, panes/_you): only what applies to
     push.js's state, and nothing before it has set one. */
  .push { display: flex; align-items: center; gap: var(--space-3); }
  html:not([data-push-state="off"]) :is([data-push-note="off"], [data-push-enable]),
  html:not([data-push-state="subscribed"]) :is([data-push-note="subscribed"], [data-push-disable]),
  html:not([data-push-state="denied"]) [data-push-note="denied"],
  html:not([data-push-state="needs-install"]) [data-push-note="needs-install"],
  html:not([data-push-state="unsupported"]) [data-push-note="unsupported"] { display: none; }
  .you__out { display: flex; flex-direction: column; margin-top: auto; & .btn { height: var(--target); font-size: 16px; } }

  .contacts__search { padding: var(--space-3) var(--space-4); }
  .row--pending .row__title { color: var(--muted); }
  .row--pending .row__sub { font-size: 13px; }
  .person__description { font-size: 15px; }
  .person__hint { font-size: 13px; }
  .person__fields { display: flex; flex-direction: column; gap: var(--space-2); align-self: stretch; margin-top: var(--space-4); text-align: left; }

  /* Contacts import: what the file has, then what was done. */
  .import {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    flex-shrink: 0;
    margin: var(--space-3) var(--space-4) 0;
    padding: var(--space-4);
    border: 1px solid var(--rule);
    border-radius: var(--radius);

    & p { margin: 0; font-size: 15px; }
    & .import__note { color: var(--muted); font-size: 13px; }
    & .btn { height: var(--target); font-size: 16px; }
  }
  .import__actions { display: flex; gap: var(--space-2); & .btn { flex: 1; } }
  .row--pending form { flex-shrink: 0; }

  /* Contacts' sections: a heading over each, gone when the search hides
     all of its rows. */
  .list__heading {
    margin: var(--space-4) var(--space-4) var(--space-1);
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
  }
  .list__section:not(:has([data-search]:not([hidden]))) { display: none; }
  .list__empty { margin: var(--space-6) var(--space-4); font-size: 15px; color: var(--muted); text-align: center; }

  .btn:disabled { opacity: 0.5; cursor: default; }

  /* Desktop: the rail, the section's list, and on the right a single
     person (or New chat), all always visible. */
  @media (min-width: 900px) {
    .app { display: grid; grid-template-columns: 84px 360px minmax(0, 1fr); }
    .app[data-view] .app__left, .app[data-view] .app__right { display: flex; }
    .app__left { border-right: 1px solid var(--rule); }

    .app[data-view] .sections {
      order: 0;
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: var(--space-1);
      padding: var(--space-4) 0;
      border-top: 0;
      border-right: 1px solid var(--rule);
    }
    .sections__link {
      width: 68px;
      padding: var(--space-2) 0;
      border-radius: var(--radius);

      &[aria-current="page"] { background: var(--accent-soft); color: var(--accent-hover); }
    }

    /* No back arrow: the list is always there on the left. */
    .bar__back { display: none; }
    .app__right .bar { padding-left: 20px; }
    .msgs { padding: 20px var(--space-5); }
    .msg { max-width: 60%; }
    .composer { padding: var(--space-3) var(--space-5); }
    .composer__banner { margin: 0 var(--space-5); }
    .composer__toggle { padding: 0 14px; }
    .composer__toggle-label { display: inline; }
    .chat__info { padding: 0 14px; border: 1px solid var(--input-border); border-radius: 22px; height: 36px; }
    .chat__info-label { display: inline; }

  }
}
