/* =========================================================================
   WebClaw — styles.css
   Mobile-first. Theme is dark by default (the only theme for now).
   ------------------------------------------------------------------------
   Architecture:
     1. Design tokens (CSS custom properties)
     2. Reset + base
     3. Layout shell (topbar, layout grid, composer)
     4. Top bar + status pill
     5. Thought stream + cards
     6. Sidebar panels (reflex stats, memory bars, strategies)
     7. Composer (bottom input)
     8. Drawer behavior (mobile only)
     9. Desktop breakpoint (>=768px)
    10. Accessibility helpers
   ========================================================================= */


/* 1. DESIGN TOKENS ------------------------------------------------------- */
:root {
  /* Palette — bioluminescent terminal. */
  --bg:            #0a0a0f;   /* deep charcoal base */
  --bg-elevated:   #11111a;   /* cards, panels */
  --bg-sunken:     #07070b;   /* input, code blocks */
  --border:        #1c1c28;   /* hairline separators */
  --border-strong: #2a2a3a;

  --fg:            #e6e6ef;   /* primary text */
  --fg-dim:        #8a8aa0;   /* secondary text */
  --fg-faint:      #4a4a5e;   /* tertiary / disabled */

  --accent:        #00ff88;   /* bioluminescent green */
  --accent-dim:    #00b364;   /* accent, lower contrast */
  --accent-glow:   rgba(0, 255, 136, 0.18);
  --accent-faint:  rgba(0, 255, 136, 0.08);

  --warn:          #ffb454;
  --err:           #ff5c7a;

  /* Type */
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo,
               Monaco, Consolas, "Liberation Mono", "Courier New", monospace;

  /* Radii / shadows */
  --radius-sm: 4px;
  --radius:    8px;
  --radius-lg: 12px;
  --shadow-glow: 0 0 0 1px var(--accent-glow), 0 0 24px var(--accent-faint);

  /* Layout */
  --topbar-h: 52px;
  --composer-h: 72px;
  --safe-top:    env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left, 0px);
  --safe-right:  env(safe-area-inset-right, 0px);

  /* Motion */
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --t-fast: 120ms;
  --t-med:  220ms;
}


/* 2. RESET + BASE -------------------------------------------------------- */
*,
*::before,
*::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  /* Prevent overscroll bounce on iOS so the stream doesn't rubber-band. */
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh; /* dynamic viewport for mobile browser chrome */
  /* Subtle radial gradient gives the "bioluminescent" feel without
     being noisy. Fixed so it doesn't move on scroll. */
  background:
    radial-gradient(ellipse 80% 50% at 50% 0%,
                    var(--accent-faint) 0%,
                    transparent 60%),
    var(--bg);
  background-attachment: fixed;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
}

::selection { background: var(--accent-glow); color: var(--fg); }


/* 3. LAYOUT SHELL -------------------------------------------------------- */
/* The topbar and composer are fixed in the flex column;
   .layout fills the remaining space and holds the stream + sidebar. */
.topbar,
.composer {
  flex: 0 0 auto;
}

.layout {
  flex: 1 1 auto;
  display: grid;
  /* Mobile default: single column. Desktop overrides below. */
  grid-template-columns: 1fr;
  min-height: 0; /* allow children to scroll instead of overflowing */
}


/* 4. TOP BAR ------------------------------------------------------------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  height: calc(var(--topbar-h) + var(--safe-top));
  padding: var(--safe-top) 16px 0 calc(16px + var(--safe-left));
  background: rgba(10, 10, 15, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.topbar__brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.02em;
  color: var(--fg);
}

.topbar__logo {
  width: 22px;
  height: 22px;
  color: var(--accent);
  /* Gentle breathing animation on the logo so the page feels alive
     even when nothing else is happening. */
  animation: claw-pulse 4s var(--ease) infinite;
}

.topbar__name { color: var(--fg); }
.topbar__name::after { /* subtle terminal cursor */
  content: "▍";
  margin-left: 4px;
  color: var(--accent);
  animation: blink 1.1s steps(2) infinite;
}

/* Status pill: state classes are set by main.js. */
.status {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--fg-dim);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 999px;
  transition: color var(--t-fast) var(--ease),
              border-color var(--t-fast) var(--ease);
}

.status__dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--fg-faint);
  transition: background var(--t-fast) var(--ease),
              box-shadow var(--t-fast) var(--ease);
}

.status[data-state="ready"]    { color: var(--accent); }
.status[data-state="ready"] .status__dot {
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
}
.status[data-state="thinking"] { color: var(--warn); }
.status[data-state="thinking"] .status__dot {
  background: var(--warn);
  box-shadow: 0 0 8px var(--warn);
  animation: pulse 1s ease-in-out infinite;
}
.status[data-state="error"]    { color: var(--err); }
.status[data-state="error"] .status__dot { background: var(--err); }

/* Cycle counter */
.counter {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--fg-dim);
}
.counter__label { color: var(--fg-faint); text-transform: uppercase; letter-spacing: 0.08em; }
.counter__value {
  color: var(--fg);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  min-width: 1.5ch;
  text-align: right;
}

/* Icon button (used for sidebar toggle) */
.iconbtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius);
  color: var(--fg-dim);
  transition: background var(--t-fast) var(--ease),
              color var(--t-fast) var(--ease);
}
.iconbtn:hover { background: var(--bg-elevated); color: var(--fg); }
.iconbtn:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--accent-glow);
  color: var(--fg);
}
.iconbtn svg { width: 20px; height: 20px; }


/* 5. THOUGHT STREAM + CARDS --------------------------------------------- */
.stream {
  min-height: 0;            /* let .stream__viewport scroll */
  display: flex;
  flex-direction: column;
}

.stream__viewport {
  flex: 1 1 auto;
  overflow-y: auto;
  /* Pad for the safe area + some breathing room. */
  padding: 16px 16px 16px calc(16px + var(--safe-left));
  padding-right: calc(16px + var(--safe-right));
  display: flex;
  flex-direction: column;
  gap: 12px;
  /* Smooth scroll for programmatic appends. */
  scroll-behavior: smooth;
  /* Custom scrollbar — slim, themed. */
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
.stream__viewport::-webkit-scrollbar { width: 6px; }
.stream__viewport::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 3px;
}

/* Empty state */
.stream__empty {
  margin: auto;
  text-align: center;
  color: var(--fg-dim);
  padding: 48px 16px;
  font-family: var(--font-mono);
}
.stream__empty p { margin: 4px 0; }
.stream__empty-hint { color: var(--fg-faint); font-size: 13px; }
.stream__empty-glyph {
  font-size: 48px;
  color: var(--accent);
  opacity: 0.5;
  animation: pulse 2.4s ease-in-out infinite;
  margin-bottom: 8px;
}

/* Individual thought card */
.card {
  /* Cards are stacked newest-at-bottom; animation slides + fades in. */
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-family: var(--font-mono);
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--fg);
  position: relative;
  /* Slight left "rail" for the strategy color. */
  border-left: 2px solid var(--border-strong);
  animation: card-in var(--t-med) var(--ease) both;
  /* Long unbroken thoughts shouldn't blow out the layout. */
  overflow-wrap: anywhere;
  word-break: break-word;
}

/* Dim reflex (cached) cards */
.card.is-reflex { opacity: 0.6; border-left-color: var(--fg-faint); }
.card.is-reflex .card__text { color: var(--fg-dim); }

/* Strategy-specific accent on the rail. */
.card[data-strategy="search"]     { border-left-color: #6ec1ff; }
.card[data-strategy="connect"]    { border-left-color: #c89bff; }
.card[data-strategy="synthesize"] { border-left-color: var(--accent); }
.card[data-strategy="reflect"]    { border-left-color: var(--warn); }
.card[data-strategy="question"]   { border-left-color: #ff8aa8; }

.card__meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  color: var(--fg-faint);
  margin-bottom: 6px;
  text-transform: lowercase;
  letter-spacing: 0.04em;
}
.card__time  { font-variant-numeric: tabular-nums; }
.card__icon  { font-size: 13px; }
.card__strategy { color: var(--fg-dim); }
.card__reflex {
  margin-left: auto;
  font-size: 12px;
  color: var(--fg-faint);
  display: inline-flex;
  align-items: center;
  gap: 3px;
}

.card__text { white-space: pre-wrap; }


/* 6. SIDEBAR PANELS ----------------------------------------------------- */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  /* Mobile: hidden by default, slides up as a drawer.
     Desktop: pinned on the right (overridden in section 9). */
  position: fixed;
  inset: 0 0 0 auto;
  width: min(320px, 88vw);
  padding: 16px;
  padding-top: calc(var(--topbar-h) + var(--safe-top) + 8px);
  padding-bottom: calc(var(--composer-h) + var(--safe-bottom) + 8px);
  background: var(--bg);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  transform: translateX(100%);
  transition: transform var(--t-med) var(--ease);
  z-index: 20;
}
.sidebar.is-open { transform: translateX(0); }

.panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 14px 12px;
}

.panel__title {
  margin: 0 0 10px;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--fg-faint);
}

/* Reflex stats: definition list styled as key/value rows. */
.stats { margin: 0; }
.stats__row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 4px 0;
  font-family: var(--font-mono);
  font-size: 13px;
}
.stats__row dt { color: var(--fg-dim); }
.stats__row dd {
  margin: 0;
  color: var(--fg);
  font-variant-numeric: tabular-nums;
  font-weight: 500;
}

/* Memory tier bars: a row of small dots. JS toggles .is-on to fill. */
.tiers {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.tiers__row {
  display: grid;
  grid-template-columns: 44px 1fr auto;
  align-items: center;
  gap: 8px;
  font-family: var(--font-mono);
  font-size: 12px;
}
.tiers__label {
  color: var(--fg-dim);
  text-transform: lowercase;
}
.tiers__bar {
  --filled: 0;            /* set by JS */
  --tier-scale: 12;       /* max segments in this tier */
  display: block;
  height: 8px;
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-radius: 999px;
  position: relative;
  overflow: hidden;
}
.tiers__bar::after {
  content: "";
  position: absolute;
  inset: 0;
  width: calc(var(--filled) / var(--tier-scale) * 100%);
  background: linear-gradient(90deg,
              var(--accent-dim) 0%,
              var(--accent)     100%);
  box-shadow: 0 0 8px var(--accent-glow);
  transition: width var(--t-med) var(--ease);
}
/* Cold tier is colder, both in color and saturation. */
.tiers__row[data-tier="cold"]  .tiers__bar::after {
  background: var(--border-strong);
  box-shadow: none;
}
.tiers__row[data-tier="warm"]  .tiers__bar::after {
  background: linear-gradient(90deg, var(--border-strong), var(--accent-dim));
}
.tiers__count {
  color: var(--fg);
  font-variant-numeric: tabular-nums;
  min-width: 2ch;
  text-align: right;
}

/* Strategy rotation list. */
.strategies {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-family: var(--font-mono);
  font-size: 13px;
}
.strategies__item {
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  color: var(--fg-dim);
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid transparent;
  transition: color var(--t-fast) var(--ease),
              background var(--t-fast) var(--ease),
              border-color var(--t-fast) var(--ease);
}
.strategies__item.is-next {
  color: var(--accent);
  background: var(--accent-faint);
  border-color: var(--accent-glow);
  /* The "next" strategy has a soft pulse to draw the eye. */
  animation: pulse 2.4s ease-in-out infinite;
}


/* 7. COMPOSER ------------------------------------------------------------ */
.composer {
  position: sticky;
  bottom: 0;
  z-index: 10;
  padding: 10px 12px calc(10px + var(--safe-bottom))
           calc(12px + var(--safe-left));
  padding-right: calc(12px + var(--safe-right));
  background: rgba(10, 10, 15, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-top: 1px solid var(--border);
}

.composer__form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 920px;
  margin: 0 auto;
}

.composer__input {
  flex: 1 1 auto;
  resize: none;
  min-height: 44px;
  max-height: 160px;
  padding: 12px 14px;
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.4;
  color: var(--fg);
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  outline: none;
  transition: border-color var(--t-fast) var(--ease),
              box-shadow var(--t-fast) var(--ease);
  /* On iOS, prevent the input from zooming the page on focus. */
  font-size: max(14px, 16px);
}
.composer__input::placeholder { color: var(--fg-faint); }
.composer__input:focus {
  border-color: var(--accent-dim);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.composer__submit {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: var(--bg);
  border-radius: var(--radius);
  transition: transform var(--t-fast) var(--ease),
              box-shadow var(--t-fast) var(--ease),
              opacity var(--t-fast) var(--ease);
}
.composer__submit svg { width: 18px; height: 18px; }
.composer__submit:hover  { box-shadow: 0 0 16px var(--accent-glow); }
.composer__submit:active { transform: scale(0.96); }
.composer__submit:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  box-shadow: none;
}
.composer__submit:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--accent-glow);
}


/* 8. DRAWER (mobile only) ---------------------------------------------- */
.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 15;
  animation: fade-in var(--t-fast) var(--ease);
}
.backdrop[hidden] { display: none; }


/* 9. DESKTOP BREAKPOINT ------------------------------------------------- */
/* >= 768px: sidebar becomes a pinned right column. Drawer bits
   and the mobile toggle button go away. */
@media (min-width: 768px) {
  .layout {
    /* Stream + sidebar. The sidebar is fixed-width and the stream
       takes the rest. */
    grid-template-columns: 1fr 320px;
  }

  .sidebar {
    position: sticky;
    top: var(--topbar-h);
    /* Override the mobile drawer styling. */
    inset: auto;
    width: auto;
    height: calc(100dvh - var(--topbar-h) - var(--composer-h));
    transform: none;
    padding: 16px 16px 16px 12px;
    background: transparent;
    border-left: 1px solid var(--border);
    overflow-y: auto;
  }

  .sidebar-toggle { display: none; }
  .backdrop       { display: none; }
}


/* 10. ACCESSIBILITY HELPERS --------------------------------------------- */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* Respect users who want reduced motion: keep the state visible
   but don't animate. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}


/* KEYFRAMES -------------------------------------------------------------- */
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.45; }
}

@keyframes blink {
  0%, 50%  { opacity: 1; }
  50.01%, 100% { opacity: 0; }
}

@keyframes card-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes claw-pulse {
  0%, 100% { filter: drop-shadow(0 0 0 transparent); }
  50%      { filter: drop-shadow(0 0 6px var(--accent-glow)); }
}

/* JS adds these classes transiently to trigger single-shot animations.
   We never let them persist — main.js removes them after setTimeout. */
@keyframes flash {
  0%, 100% { background-color: transparent; }
  50%      { background-color: var(--accent-glow); }
}
@keyframes sent {
  0%, 100% { border-color: var(--border); }
  50%      { border-color: var(--accent); }
}

.strategy-indicator.flash { animation: flash 0.6s var(--ease) 1; }
.composer__input.sent     { animation: sent 0.4s var(--ease) 1; }
.tiers__bar.pulse         { animation: pulse 0.6s var(--ease) 1; }

/* Active strategy pill — the live indicator */
.strategy-indicator {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--font-mono);
  font-size: 14px;
  color: var(--accent);
  padding: 8px 12px;
  border-radius: var(--radius);
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  transition: background-color var(--t-fast) var(--ease);
}
.strategy-indicator__icon { font-size: 16px; }
.strategy-indicator__name { font-weight: 500; }
