/* ============================================================
   RexFetch — ported from Claude Design handoff (RexFetch.html)
   Tokens + chat UI. Pairs with views/layout.pug + views/index.pug
   and public/js/components/chat-window.js.
   ============================================================ */

:root {
  /* Warm surfaces */
  --warm-50:  #faf7f2;
  --warm-100: #f3ede2;
  --warm-200: #e8dfcf;
  --warm-300: #d4c7b0;
  --warm-400: #b4a488;
  --warm-500: #7f7560;
  --warm-600: #5a5241;
  --warm-700: #3b3628;
  --warm-800: #26231a;
  --warm-900: #1a1812;

  /* Dark mode surfaces */
  --deep-50:  #f5efe2;
  --deep-100: #e8dfcf;
  --deep-200: #a89d85;
  --deep-300: #6a6252;
  --deep-400: #3d3a30;
  --deep-500: #2a2821;
  --deep-600: #201e18;
  --deep-700: #1a1813;
  --deep-800: #13110d;
  --deep-900: #0f0d0a;

  /* Discount Equipment brand (accent-only) */
  --de-navy:    #003B73;
  --de-navy-2:  #1a5a99;
  --de-green:   #2ECC40;
  --de-green-2: #26a335;
  --de-orange:  #f08a24;
  --de-orange-2:#d77314;

  /* Rex mascot palette */
  --rex-body:        #6fa4d6;
  --rex-body-2:      #4d7fb1;
  --rex-body-shadow: #2d5a87;
  --rex-joint:       var(--de-orange);
  --rex-eye:         #7be0e6;

  /* Semantic — light */
  --bg:           var(--warm-50);
  --panel:        #ffffff;
  --panel-2:      var(--warm-100);
  --ink:          var(--warm-900);
  --ink-2:        var(--warm-700);
  --ink-3:        var(--warm-500);
  --hairline:     var(--warm-200);
  --hairline-2:   var(--warm-300);
  --accent:       var(--de-navy);
  --accent-2:     var(--de-navy-2);
  --success:      var(--de-green-2);
  --highlight:    var(--de-orange);

  --focus-ring: 0 0 0 3px rgba(0,59,115,.22);
  --shadow-sm:  0 1px 2px rgba(26,24,18,.04), 0 2px 6px rgba(26,24,18,.04);
  --shadow-md:  0 2px 4px rgba(26,24,18,.04), 0 8px 24px rgba(26,24,18,.06);
  --shadow-lg:  0 4px 12px rgba(26,24,18,.06), 0 24px 48px rgba(26,24,18,.08);

  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-xl: 28px;

  --font-serif: "Source Serif 4", ui-serif, Georgia, serif;
  --font-sans:  "Inter", ui-sans-serif, system-ui, sans-serif;
  --font-mono:  "JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace;

  --max-chat: 760px;
}

[data-theme="dark"] {
  --bg:        var(--deep-900);
  --panel:     var(--deep-800);
  --panel-2:   var(--deep-700);
  --ink:       var(--deep-50);
  --ink-2:     var(--deep-100);
  --ink-3:     var(--deep-200);
  --hairline:  var(--deep-500);
  --hairline-2:var(--deep-400);
  --accent:    #6a9dd4;
  --accent-2:  #8fb8df;
  --success:   #4fd95f;
  --highlight: #f8a84a;

  --focus-ring: 0 0 0 3px rgba(106,157,212,.30);
  --shadow-sm:  0 1px 2px rgba(0,0,0,.35), 0 2px 6px rgba(0,0,0,.25);
  --shadow-md:  0 2px 4px rgba(0,0,0,.4),  0 8px 24px rgba(0,0,0,.35);
  --shadow-lg:  0 4px 12px rgba(0,0,0,.5), 0 24px 48px rgba(0,0,0,.4);

  --rex-body:        #7cb0df;
  --rex-body-2:      #5a8bbc;
  --rex-body-shadow: #1e3d5c;
}

/* Base */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  transition: background .4s ease, color .4s ease;
  overflow: hidden;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }

/* App chrome — pinned to the visible viewport so the composer stays
   at the bottom on mobile even when the empty-state content would
   otherwise push the layout beyond 100vh. 100dvh tracks the dynamic
   viewport (shrinks when the mobile URL bar is visible); 100vh is
   the fallback for older browsers. */
.app {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100vh;
  height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 28px;
  border-bottom: 1px solid var(--hairline);
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 32px; height: 32px;
  border-radius: 10px;
  background: var(--ink);
  color: var(--bg);
  display: grid; place-items: center;
  flex-shrink: 0;
}
.brand-mark svg { width: 22px; height: 22px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.05; }
.brand-name {
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 19px;
  letter-spacing: -0.01em;
}
.brand-sub {
  font-size: 11px;
  color: var(--ink-3);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
}

.topbar-actions { display: flex; align-items: center; gap: 8px; }

.icon-btn {
  width: 36px; height: 36px;
  display: grid; place-items: center;
  border-radius: 10px;
  color: var(--ink-2);
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--panel-2); color: var(--ink); }
.icon-btn svg { width: 18px; height: 18px; }

/* Chat container */
.chat-viewport {
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
.chat-scroll { flex: 1; overflow-y: auto; }
.chat-scroll::-webkit-scrollbar { width: 10px; }
.chat-scroll::-webkit-scrollbar-thumb {
  background: var(--hairline-2);
  border-radius: 10px;
  border: 2px solid var(--bg);
}
.chat-inner {
  max-width: var(--max-chat);
  margin: 0 auto;
  padding: 48px 28px 180px;
}

/* Empty state */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding-top: 24px;
}
.empty-rex {
  width: 200px; height: 250px;
  margin-bottom: 20px;
  background-image: url('/images/rex-idle.png');
  background-repeat: no-repeat;
  background-size: contain;
  background-position: center;
  animation: rex-idle-float 6s ease-in-out infinite;
}
@keyframes rex-idle-float {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-6px); }
}
.empty h1 {
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: clamp(30px, 3.6vw, 40px);
  line-height: 1.2;
  letter-spacing: -0.02em;
  margin: 0 0 18px;
  color: var(--ink);
  max-width: 640px;
  white-space: nowrap;
}
@media (max-width: 620px) {
  .empty h1 { white-space: normal; font-size: 30px; }
}
.empty h1 em { font-style: italic; color: var(--ink-2); }
.empty p {
  font-size: 15px;
  color: var(--ink-3);
  max-width: 440px;
  margin: 0 0 40px;
  text-wrap: pretty;
}
.empty-tip {
  max-width: 520px;
  margin: 0 0 36px;
  padding: 14px 20px;
  border-radius: var(--radius-md);
  background: color-mix(in srgb, var(--highlight) 8%, var(--panel));
  border: 1px solid color-mix(in srgb, var(--highlight) 30%, var(--hairline));
  color: var(--ink-2);
  font-size: 14.5px;
  line-height: 1.55;
  text-align: left;
}
.empty-tip strong {
  color: var(--ink);
  font-weight: 600;
}

/* Starter prompts */
.starters {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 18px;
  text-align: left;
}
.starter-group {
  display: grid;
  grid-template-columns: 120px 1fr;
  gap: 20px;
  align-items: start;
}
.starter-label {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 14px;
  color: var(--ink-3);
  padding-top: 10px;
  display: flex; align-items: center; gap: 8px;
}
.starter-label::before {
  content: "";
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--grp-dot, var(--de-navy));
  display: inline-block;
  flex-shrink: 0;
}
.starter-group[data-group="wholegoods"]  { --grp-dot: var(--de-navy); }
.starter-group[data-group="accessories"] { --grp-dot: var(--de-orange); }
.starter-group[data-group="parts"]       { --grp-dot: var(--de-green-2); }

.starter-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 9px 14px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--hairline);
  font-size: 13.5px;
  color: var(--ink-2);
  transition: all .15s;
  cursor: pointer;
  line-height: 1.3;
  text-align: left;
}
.chip:hover {
  border-color: var(--grp-dot, var(--hairline-2));
  color: var(--ink);
  transform: translateY(-1px);
  box-shadow: var(--shadow-sm);
}
.chip:focus-visible { outline: 0; box-shadow: var(--focus-ring); }

/* Messages */
.msg {
  display: grid;
  grid-template-columns: 36px 1fr;
  gap: 14px;
  margin-bottom: 32px;
  animation: msg-in .35s ease-out;
}
@keyframes msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}
.msg-avatar { width: 32px; height: 32px; margin-top: 2px; }
.msg-avatar.user {
  background: var(--warm-200);
  color: var(--ink-2);
  border-radius: 50%;
  display: grid; place-items: center;
  font-family: var(--font-serif);
  font-size: 14px;
  font-weight: 500;
}
[data-theme="dark"] .msg-avatar.user { background: var(--deep-500); color: var(--ink-2); }
.msg-avatar.rex { width: 32px; height: 32px; }

.msg-role {
  font-family: var(--font-serif);
  font-size: 13px;
  color: var(--ink-3);
  font-style: italic;
  margin-bottom: 4px;
}
.msg-body {
  font-size: 15.5px;
  line-height: 1.65;
  color: var(--ink);
}
.msg-body p { margin: 0 0 10px; }
.msg-body p:last-child { margin-bottom: 0; }
.msg-body ul, .msg-body ol { padding-left: 22px; margin: 6px 0 10px; }
.msg-body li { margin-bottom: 4px; }
.msg-body strong { color: var(--ink); font-weight: 600; }
.msg-body code {
  font-family: var(--font-mono);
  font-size: 13.5px;
  background: var(--panel-2);
  padding: 1px 6px;
  border-radius: 4px;
}
.msg-body a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
  transition: color .15s, text-decoration-thickness .15s;
}
.msg-body a:hover {
  color: var(--accent-2);
  text-decoration-thickness: 2px;
}
.msg-body a:focus-visible {
  outline: 0;
  box-shadow: var(--focus-ring);
  border-radius: 2px;
}

/* Streaming caret */
.caret {
  display: inline-block;
  width: 8px; height: 1.1em;
  background: var(--accent);
  vertical-align: -0.2em;
  margin-left: 2px;
  animation: caret-blink 1s steps(2) infinite;
  border-radius: 1px;
}
@keyframes caret-blink { 50% { opacity: 0; } }

/* Tool-call row */
.tool-row {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  margin: 4px 0 14px;
  padding: 8px 14px;
  border: 1px solid var(--hairline);
  border-radius: 999px;
  background: var(--panel);
  font-size: 13px;
  color: var(--ink-2);
  max-width: 100%;
  cursor: pointer;
  transition: all .15s;
}
.tool-row:hover { border-color: var(--hairline-2); background: var(--panel-2); }
.tool-row.running {
  border-color: color-mix(in srgb, var(--highlight) 40%, var(--hairline));
  background: color-mix(in srgb, var(--highlight) 8%, var(--panel));
}
.tool-row .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--success);
  flex-shrink: 0;
}
.tool-row.running .dot {
  background: var(--highlight);
  animation: dot-pulse 1.2s ease-in-out infinite;
}
@keyframes dot-pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50%      { transform: scale(1.4); opacity: .6; }
}
.tool-row .tool-name { font-family: var(--font-mono); font-size: 12.5px; color: var(--ink); }
.tool-row .tool-arg  { color: var(--ink-3); font-size: 12.5px; max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool-row .tool-meta { color: var(--ink-3); font-size: 12px; margin-left: auto; font-variant-numeric: tabular-nums; }

.tool-expand {
  display: none;
  margin: 0 0 14px;
  padding: 14px 16px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-md);
  background: var(--panel-2);
  font-size: 13px;
  color: var(--ink-2);
}
.tool-row.expanded + .tool-expand { display: block; }
.tool-expand pre {
  margin: 6px 0 0;
  font-family: var(--font-mono);
  font-size: 12.5px;
  color: var(--ink-2);
  white-space: pre-wrap;
  word-break: break-word;
}
.tool-expand .label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-3);
}

/* Thinking / digging row */
.thinking-row {
  display: flex; align-items: center; gap: 16px;
  padding: 14px 18px;
  border-radius: var(--radius-lg);
  background: var(--panel-2);
  border: 1px solid var(--hairline);
  margin-bottom: 14px;
  animation: msg-in .3s ease-out;
}
.thinking-rex {
  width: 104px; height: 78px;
  flex-shrink: 0;
}
.digging-rex {
  width: 100%; height: 100%;
  background-image: url('/images/rex-digging.png');
  background-repeat: no-repeat;
  background-size: contain;
  background-position: center;
  animation: rex-dig 1.1s ease-in-out infinite;
  transform-origin: 50% 85%;
}
@keyframes rex-dig {
  0%, 100% { transform: translate(0, 0) rotate(0deg); }
  25%      { transform: translate(-2px, -5px) rotate(-5deg); }
  50%      { transform: translate(0, -7px) rotate(0deg); }
  75%      { transform: translate(2px, -4px) rotate(5deg); }
}
[data-dig="subtle"]     .digging-rex { animation-duration: 1.6s; }
[data-dig="mechanical"] .digging-rex { animation-duration: .75s; }

.thinking-text { display: flex; flex-direction: column; gap: 2px; }
.thinking-text .primary {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 15px;
  color: var(--ink);
}
.thinking-text .secondary {
  font-size: 12.5px;
  color: var(--ink-3);
  font-variant-numeric: tabular-nums;
}

/* Error */
.error-row {
  display: flex; align-items: start; gap: 12px;
  padding: 14px 16px;
  border-radius: var(--radius-md);
  background: color-mix(in srgb, var(--highlight) 8%, var(--panel));
  border: 1px solid color-mix(in srgb, var(--highlight) 30%, var(--hairline));
  margin-bottom: 14px;
  font-size: 14px;
}
.error-row svg { width: 18px; height: 18px; color: var(--highlight); flex-shrink: 0; margin-top: 2px; }

/* Composer */
.composer-dock {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  /* max() keeps the iPhone home-indicator from sitting under the composer
     when viewport-fit=cover is set in the meta tag. */
  padding: 48px 28px max(24px, env(safe-area-inset-bottom));
  background: linear-gradient(to top,
    var(--bg) 0%,
    var(--bg) 55%,
    color-mix(in srgb, var(--bg) 85%, transparent) 75%,
    color-mix(in srgb, var(--bg) 40%, transparent) 90%,
    transparent 100%);
  pointer-events: none;
  z-index: 5;
}
.composer {
  max-width: var(--max-chat);
  margin: 0 auto;
  pointer-events: auto;
  background: var(--panel);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  transition: border-color .2s, box-shadow .2s;
}
.composer:focus-within {
  border-color: var(--accent);
  box-shadow: var(--shadow-md), var(--focus-ring);
}
.composer-input-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
}
.composer textarea {
  flex: 1 1 auto;
  border: 0;
  outline: 0;
  resize: none !important;
  background: transparent;
  font-family: var(--font-sans);
  /* 16px minimum prevents iOS Safari from auto-zooming on focus. */
  font-size: 16px;
  line-height: 1.5;
  color: var(--ink);
  min-height: 24px;
  max-height: 200px;
  height: 24px;
  padding: 0 6px;
  margin: 0;
  min-width: 0;
  width: 100%;
  overflow-x: hidden;
  overflow-y: hidden;
  overflow-wrap: break-word;
  word-break: break-word;
  white-space: pre-wrap;
  display: block;
  vertical-align: middle;
}
.composer textarea::placeholder { color: var(--ink-3); }

.send-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--ink);
  color: var(--bg);
  display: grid; place-items: center;
  transition: all .15s;
  flex-shrink: 0;
}
.send-btn:hover { background: var(--accent); }
.send-btn:disabled { background: var(--hairline-2); cursor: not-allowed; }
.send-btn svg { width: 16px; height: 16px; }
.send-btn.active { background: var(--accent); }
.send-btn.active:hover { background: var(--accent-2); }

/* Density + accent modifiers (no UI to toggle in prod, but attributes
   can be set inline if we ever want to experiment) */
[data-density="airy"]    { --max-chat: 720px; }
[data-density="airy"]    .chat-inner { padding: 60px 28px 200px; }
[data-density="airy"]    .msg { margin-bottom: 40px; }
[data-density="airy"]    .msg-body { font-size: 16.5px; line-height: 1.75; }

[data-density="compact"] .chat-inner { padding: 28px 28px 160px; }
[data-density="compact"] .msg { margin-bottom: 20px; }
[data-density="compact"] .msg-body { font-size: 14.5px; line-height: 1.55; }
[data-density="compact"] .empty { padding-top: 8px; }
[data-density="compact"] .empty-rex { width: 128px; height: 163px; margin-bottom: 14px; }

[data-accent="green"]  { --accent: var(--de-green-2); --accent-2: var(--de-green);  --focus-ring: 0 0 0 3px rgba(46,204,64,.22); }
[data-accent="orange"] { --accent: var(--de-orange-2); --accent-2: var(--de-orange); --focus-ring: 0 0 0 3px rgba(240,138,36,.25); }

/* Responsive */
@media (max-width: 640px) {
  .chat-inner { padding: 24px 18px 180px; }
  .composer-dock { padding: 12px 18px max(18px, env(safe-area-inset-bottom)); }
  .starter-group { grid-template-columns: 1fr; gap: 8px; }
  .starter-label { padding-top: 0; }
  .topbar { padding: 14px 18px; }
  .brand-sub { display: none; }
}
