/* Palette, type and shapes follow khalayat.com: white ground, #1f2021 text,
   uppercase letterspaced labels at regular weight, near-square corners, and
   the system font stack the shop itself uses. */

:root {
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-2: #fafafa;
  --ink: #1f2021;
  --ink-2: #5d5d5d;
  --ink-3: #8b8c8c;
  --line: #dfdfdf;
  --line-soft: #ececec;
  --fill: #1f2021;
  --fill-ink: #ffffff;
  --fill-hover: #1c1d1d;
  --danger: #8a3227;
  --danger-soft: #fbf3f1;
  --radius: 2px;
  --measure: 44rem;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue",
          "SF Arabic", "Geeza Pro", "Noto Sans Arabic", "Noto Sans",
          "Liberation Sans", Arial, sans-serif;
  --tracked: .1em;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1d1d;
    --surface: #232425;
    --surface-2: #2a2b2b;
    --ink: #f1f1f1;
    --ink-2: #a6a7a7;
    --ink-3: #7d7e7e;
    --line: #343536;
    --line-soft: #2c2d2d;
    --fill: #f1f1f1;
    --fill-ink: #1c1d1d;
    --fill-hover: #ffffff;
    --danger: #d99a8e;
    --danger-soft: #2b2120;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  font-weight: 400;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

svg { fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }

button { font: inherit; color: inherit; }

img { max-width: 100%; height: auto; }

.sr {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

:focus-visible { outline: 1px solid var(--ink); outline-offset: 3px; }

/* shell ------------------------------------------------------------------ */

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  position: relative;
}

.bar {
  flex: none;
  display: grid;
  grid-template-columns: 3rem 1fr 3rem;
  align-items: center;
  gap: .5rem;
  padding: .875rem 1rem;
  padding-top: max(.875rem, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 5;
}

.mark { text-align: center; }

.mark-logo {
  display: block;
  width: auto;
  height: .8125rem;
  margin: 0 auto .3125rem;
}

@media (prefers-color-scheme: dark) {
  .mark-logo { filter: invert(1); }
}

.mark-sub {
  display: block;
  font-size: .625rem;
  letter-spacing: var(--tracked);
  text-transform: uppercase;
  color: var(--ink-3);
}

.icon-btn, .lang-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 2rem;
  background: none;
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--ink-2);
  transition: color .15s ease, border-color .15s ease;
}

.icon-btn { width: 2rem; }
.icon-btn svg { width: 1.0625rem; height: 1.0625rem; }

.lang-btn {
  padding-inline: .625rem;
  font-size: .625rem;
  letter-spacing: var(--tracked);
  text-transform: uppercase;
  border-color: var(--line);
  justify-self: end;
}

.icon-btn:hover, .lang-btn:hover { color: var(--ink); border-color: var(--ink); }

/* keeps its grid column so the logo stays centred */
.icon-btn[hidden] { display: inline-flex; visibility: hidden; pointer-events: none; }

/* focus strip ------------------------------------------------------------ */

.focus-strip {
  flex: none;
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .5rem 1rem;
  font-size: .625rem;
  letter-spacing: var(--tracked);
  text-transform: uppercase;
  color: var(--ink-2);
  background: var(--surface-2);
  border-bottom: 1px solid var(--line-soft);
}

.focus-strip[hidden] { display: none; }

.focus-clear {
  margin-inline-start: auto;
  border: 0; background: none; padding: .2rem;
  cursor: pointer; color: var(--ink-3);
  display: inline-flex;
}
.focus-clear svg { width: .8125rem; height: .8125rem; }
.focus-clear:hover { color: var(--ink); }

/* thread ----------------------------------------------------------------- */

.thread {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1.75rem 1.25rem 1.25rem;
  scrollbar-width: thin;
  scrollbar-color: var(--line) transparent;
}

.thread::-webkit-scrollbar { width: 8px; }
.thread::-webkit-scrollbar-thumb { background: var(--line); border: 3px solid var(--bg); }

.thread:has(.intro:not([hidden])) { justify-content: safe center; }

#messages {
  width: 100%;
  max-width: var(--measure);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* opening screen --------------------------------------------------------- */

.intro {
  width: 100%;
  max-width: var(--measure);
  margin: 0 auto;
  padding: 1rem 0 2rem;
  text-align: center;
}

.intro[hidden] { display: none; }

.intro-title {
  font-size: 1.125rem;
  font-weight: 400;
  letter-spacing: var(--tracked);
  text-transform: uppercase;
  margin: 0;
}

/* the short rule under a heading is the shop's own device */
.intro-title::after {
  content: "";
  display: block;
  width: 50px;
  margin: 1.25rem auto;
  border-bottom: 1px solid var(--ink);
}

.intro-body {
  margin: 0 auto;
  max-width: 38ch;
  color: var(--ink-2);
  font-size: .9375rem;
  text-wrap: pretty;
}

.cards {
  display: grid;
  gap: 1px;
  margin: 2.25rem 0 2rem;
  background: var(--line);
  border: 1px solid var(--line);
  text-align: start;
}

@media (min-width: 34rem) {
  .cards { grid-template-columns: 1fr 1fr; }
}

.card {
  display: block;
  width: 100%;
  text-align: start;
  padding: 1.125rem 1.25rem;
  border: 0;
  background: var(--surface);
  cursor: pointer;
  transition: background-color .15s ease;
}

.card:hover { background: var(--surface-2); }
.card[aria-pressed="true"] { background: var(--surface-2); box-shadow: inset 2px 0 0 var(--ink); }

.card-name {
  display: block;
  font-size: .8125rem;
  letter-spacing: var(--tracked);
  text-transform: uppercase;
}

.card-alt { display: block; font-size: .875rem; color: var(--ink-2); margin-top: .125rem; }

.card-meta {
  display: block;
  font-size: .6875rem;
  letter-spacing: .04em;
  color: var(--ink-3);
  margin-top: .625rem;
}

.section-label {
  font-size: .625rem;
  letter-spacing: var(--tracked);
  text-transform: uppercase;
  color: var(--ink-3);
  margin: 0 0 .875rem;
}

.chips { display: flex; flex-wrap: wrap; gap: .5rem; justify-content: center; }

.chip {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: .5rem .875rem;
  font-size: .8125rem;
  color: var(--ink-2);
  cursor: pointer;
  transition: border-color .15s ease, color .15s ease;
}

.chip:hover { border-color: var(--ink); color: var(--ink); }

/* messages --------------------------------------------------------------- */

.msg { display: flex; }
.msg.user { justify-content: flex-end; }

.bubble {
  max-width: min(85%, 34rem);
  padding: .75rem 1rem;
  border-radius: var(--radius);
  font-size: .9375rem;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.msg.user .bubble { background: var(--fill); color: var(--fill-ink); }

.msg.bot .bubble {
  background: var(--surface);
  border: 1px solid var(--line);
  max-width: min(94%, 36rem);
}

.msg.error .bubble {
  background: var(--danger-soft);
  border: 1px solid var(--danger);
  color: var(--danger);
}

.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble p { margin: 0 0 .625rem; }
.bubble ul, .bubble ol { margin: 0 0 .625rem; padding-inline-start: 1.125rem; }
.bubble li { margin-bottom: .25rem; }
.bubble strong { font-weight: 600; }

.retry {
  display: inline-flex;
  margin-top: .625rem;
  border: 1px solid currentColor;
  border-radius: var(--radius);
  background: none;
  padding: .3125rem .75rem;
  font-size: .625rem;
  letter-spacing: var(--tracked);
  text-transform: uppercase;
  cursor: pointer;
}

.caret {
  display: inline-block;
  width: .5em;
  height: 1em;
  vertical-align: -.14em;
  margin-inline-start: .1em;
  background: var(--ink);
  animation: blink 1.1s steps(2, start) infinite;
}

.dots { display: inline-flex; gap: .3125rem; padding: .3125rem .125rem; }
.dots i {
  width: .3125rem; height: .3125rem;
  background: var(--ink-3);
  animation: bounce 1.2s ease-in-out infinite;
}
.dots i:nth-child(2) { animation-delay: .15s; }
.dots i:nth-child(3) { animation-delay: .3s; }

/* jump ------------------------------------------------------------------- */

.jump {
  position: absolute;
  inset-block-end: 7.5rem;
  inset-inline-end: 1.25rem;
  width: 2.125rem; height: 2.125rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink-2);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  z-index: 4;
}
.jump svg { width: .9375rem; height: .9375rem; }
.jump[hidden] { display: none; }

/* composer --------------------------------------------------------------- */

.composer {
  flex: none;
  padding: .875rem 1.25rem;
  padding-bottom: max(.875rem, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg);
}

.field {
  display: flex;
  align-items: flex-end;
  gap: .5rem;
  max-width: var(--measure);
  margin: 0 auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: .25rem .25rem .25rem .875rem;
  transition: border-color .15s ease;
}

.field:focus-within { border-color: var(--ink); }

#input {
  flex: 1;
  border: 0;
  background: none;
  resize: none;
  font: inherit;
  font-size: 1rem;
  line-height: 1.5;
  color: var(--ink);
  padding: .5rem 0;
  max-height: 8.5rem;
  outline: none;
}

#input::placeholder { color: var(--ink-3); }

.send {
  flex: none;
  width: 2.25rem; height: 2.25rem;
  border: 0;
  border-radius: var(--radius);
  background: var(--fill);
  color: var(--fill-ink);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: opacity .15s ease, background-color .15s ease;
}

.send svg { width: 1rem; height: 1rem; stroke-width: 1.75; }
.send .ic-stop { display: none; fill: currentColor; stroke: none; }
.send:disabled { opacity: .25; cursor: default; }
.send:not(:disabled):hover { background: var(--fill-hover); }

body.streaming .send .ic-send { display: none; }
body.streaming .send .ic-stop { display: block; }
body.streaming .send { background: var(--surface); color: var(--ink-2); border: 1px solid var(--line); }

.foot {
  max-width: var(--measure);
  margin: .75rem auto 0;
  font-size: .6875rem;
  line-height: 1.5;
  color: var(--ink-3);
  text-align: center;
}

/* arabic ----------------------------------------------------------------- */

/* Arabic joins its letters, so tracking pulls words apart and uppercase does
   nothing. It also reads better slightly larger and looser. */
[dir="rtl"] .mark-sub,
[dir="rtl"] .focus-strip,
[dir="rtl"] .section-label,
[dir="rtl"] .card-name,
[dir="rtl"] .intro-title,
[dir="rtl"] .retry {
  letter-spacing: normal;
  text-transform: none;
}

[dir="rtl"] .mark-sub,
[dir="rtl"] .focus-strip,
[dir="rtl"] .section-label { font-size: .75rem; }
[dir="rtl"] .card-name { font-size: .9375rem; }
[dir="rtl"] .intro-title { font-size: 1.25rem; }
[dir="rtl"] .intro-body, [dir="rtl"] .chip { font-size: .9375rem; }
[dir="rtl"] .foot { font-size: .75rem; }
.bubble[dir="rtl"] { line-height: 1.85; }

/* motion ----------------------------------------------------------------- */

@keyframes blink { 50% { opacity: 0; } }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .4; }
  30% { transform: translateY(-.25rem); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
  .caret { animation: none; opacity: .6; }
}

@media (min-width: 48rem) {
  .thread { padding: 3rem 1.5rem 1.5rem; }
  .intro-title { font-size: 1.25rem; }
  .mark-logo { height: .875rem; }
}
