/* Tokens first. The shell's slot is the one that must survive every redesign:
   inside an older Second Draft shell a 48px round button floats in the
   bottom-right corner, 16px in from each edge, above everything. This app draws
   its own note trigger instead, so second-draft-note.css sets --shell-slot to
   0 and puts it back only inside such a shell. Keep clear of the corner with
   var(--shell-slot), never a fixed 80px, and the space is there exactly when
   the button is. */
:root {
  --shell-switcher-size: 48px;
  --shell-switcher-inset: 16px;
  --shell-switcher-gap: 16px;
  --shell-slot: 80px; /* inset + size + gap: the corner the shell owns */

  --ink: #1f1d1b;
  --quiet: #6b6560;
  --paper: #f7f5f2;
  --card: #ffffff;
  --line: #e4dfd9;
  --accent: #5b3a5e;
  --on-accent: #ffffff;
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  /* The Ask panel and chat only wear the app's look (the README, "The panel wears the app's look"):
     named from the palette above, so the dark one below carries over. Keep them in step with the look. */
  --second-draft-ask-paper: var(--paper);
  --second-draft-ask-ink: var(--ink);
  --second-draft-ask-quiet: var(--quiet);
  --second-draft-ask-line: var(--line);
  --second-draft-ask-field: var(--card);
  --second-draft-ask-wash: var(--line);
  --second-draft-ask-accent: var(--accent);
  --second-draft-ask-on-accent: var(--on-accent);
  --second-draft-ask-font: var(--font);
  color-scheme: light dark;
}
/* The same page in a phone set to dark. --paper here is the dark theme-color
   tag in index.html, as --paper above is the light one: change them together. */
@media (prefers-color-scheme: dark) {
  :root {
    --ink: #f1ede8;
    --quiet: #a9a29b;
    --paper: #1c1b1a;
    --card: #282624;
    --line: #3b3835;
    --accent: #d9b8dc;
    --on-accent: #2a1a2b;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--paper); color: var(--ink); font: 16px/1.5 var(--font); }
body { min-height: 100dvh; }

/* The shell gives the page the whole window, behind the clock at the top and
   the gesture bar at the bottom: the background is ours to cover, the content
   ours to keep clear of both. The shell sets --shell-safe-* on <html> before
   the first layout, so nothing moves afterwards; env() answers in a plain
   browser, where the variables are absent. See docs/shell-contract.md.
   The top bar carries the top inset, the page below it the rest. */
.page {
  max-width: 1000px;
  margin: 0 auto;
  padding-right: calc(20px + max(var(--shell-safe-right, 0px), env(safe-area-inset-right, 0px)));
  padding-bottom: calc(max(var(--shell-safe-bottom, 0px), env(safe-area-inset-bottom, 0px)) + var(--shell-slot));
  padding-left: calc(20px + max(var(--shell-safe-left, 0px), env(safe-area-inset-left, 0px)));
}

/* The app's bar: the width of the window, in the flow of the page. On a phone
   it is the heading with the note trigger at its end, as the page's column
   is. On a wide screen the heading keeps to the content's column and the
   trigger goes to the far end, the top-right corner of the screen — never
   left floating in the middle of it. */
.top-bar {
  /* The app's navigation stays in reach however far the page scrolls, and the
     note trigger with it. */
  position: sticky; top: 0; z-index: 1; background: var(--paper);
  display: flex; align-items: flex-start; gap: 12px;
  margin: 0; padding-bottom: 8px;
  padding-top: calc(24px + max(var(--shell-safe-top, 0px), env(safe-area-inset-top, 0px)));
  padding-right: calc(20px + max(var(--shell-safe-right, 0px), env(safe-area-inset-right, 0px)));
  padding-left: calc(20px + max(var(--shell-safe-left, 0px), env(safe-area-inset-left, 0px)));
}
.top-bar .page-heading { flex: 1; min-width: 0; margin: 0; }
@media (min-width: 760px) {
  /* Three columns: the middle one is the page's (1000px less its padding). */
  .top-bar { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 960px) minmax(0, 1fr); gap: 0; }
  .top-bar .page-heading { grid-column: 2; }
  .top-bar .top-actions { grid-column: 3; justify-self: end; }
}
.page-heading { font-size: 1.6rem; margin: 0 0 8px; }

/* The note trigger: the app's own control, in the app's own colours, that
   opens Second Draft's note panel. It sits with the app's navigation — here at
   the end of the top bar, which stays; in an app with a bottom tab bar, in
   that bar — never over its content. 44px at least, and the
   mark keeps both of its shapes. second-draft-note.js fills in the mark and
   hides it from anyone who may not leave a note. */
.note-trigger {
  appearance: none; flex: none;
  display: inline-grid; place-items: center;
  width: 44px; height: 44px; padding: 0;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--card); color: var(--accent);
}
/* The bar's end: the install control, when there is one, then the note trigger. */
.top-actions { display: flex; flex: none; align-items: center; gap: 8px; }
.install-app {
  appearance: none; min-height: 44px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--card); color: var(--accent); font: inherit; font-weight: 600;
}
.install-app:focus-visible,
.note-trigger:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.quiet { color: var(--quiet); margin: 0; }

button, input, select, textarea { font: inherit; }
.button {
  appearance: none; border: 0; border-radius: 12px; padding: 12px 18px;
  background: var(--accent); color: var(--on-accent); cursor: pointer;
  min-height: 44px;
}
.button:disabled { opacity: 0.6; }
.button:focus-visible,
input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* The one site, its kept login, and its APIs: cards side by side on a wide window. */
.columns { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 900px) {
  .columns { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; }
  .columns .card:first-child { grid-column: 1 / -1; }
}
.card {
  background: transparent; border: 1px solid var(--line); border-radius: 16px;
  padding: 16px; min-width: 0;
}
.card h2 { margin: 0 0 8px; font-size: 1.15rem; }
.card form { display: grid; gap: 12px; margin-top: 12px; }
.field { display: grid; gap: 4px; min-width: 0; }
.field label { display: grid; gap: 4px; min-width: 0; }
.field-label { font-weight: 600; }
.field input, .field select {
  width: 100%; min-height: 44px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--paper); color: var(--ink);
}
.hint { color: var(--quiet); font-size: 0.9rem; }
.advanced { border-top: 1px solid var(--line); padding-top: 8px; display: grid; gap: 12px; }
.advanced summary { cursor: pointer; font-weight: 600; min-height: 44px; line-height: 44px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.badge {
  display: inline-block; font-size: 0.85rem; font-weight: 700;
  border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px; margin: 0 0 8px;
  background: var(--paper); color: var(--quiet);
}
.status { font-weight: 600; margin: 8px 0 0; }
.error { color: #a33; margin: 8px 0 0; }
@media (prefers-color-scheme: dark) { .error { color: #f0a3a3; } }
.result {
  white-space: pre-wrap; word-break: break-word; margin: 12px 0 0;
  background: var(--paper); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px; max-height: 320px; overflow: auto; font-size: 0.9rem;
}

/* Messages that are not the page: keep out of the shell's 80×80 corner. */
#toast {
  position: fixed; left: 16px; right: var(--shell-slot);
  bottom: calc(env(safe-area-inset-bottom, 0px) + var(--shell-switcher-inset));
  pointer-events: none;
}
#toast:empty { display: none; }
#toast > * { background: var(--ink); color: var(--paper); border-radius: 12px; padding: 12px 16px; }
