/* HackTheLearn AI agent chat. Mobile first, light/dark, iOS Safari safe. */

:root {
  --bg: #f6f7fb;
  --bg-accent: radial-gradient(1200px 600px at 10% -10%, #e3e8ff 0%, transparent 60%),
    radial-gradient(900px 500px at 110% 10%, #ffe9f3 0%, transparent 55%);
  --panel: #ffffff;
  --panel-glass: rgba(255, 255, 255, 0.78);
  --text: #141824;
  --muted: #5b6275;
  --border: #e3e6ee;
  --accent: #4f46e5;
  --accent-2: #db2777;
  --accent-text: #ffffff;
  --bubble-user: linear-gradient(135deg, #4f46e5, #7c3aed);
  --bubble-bot: #f1f3f9;
  --focus: #2563eb;
  --ok: #16a34a;
  --shadow: 0 10px 40px rgba(20, 24, 36, 0.08), 0 2px 8px rgba(20, 24, 36, 0.05);
  --radius: 18px;
  --font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1017;
    --bg-accent: radial-gradient(1200px 600px at 10% -10%, #1c2247 0%, transparent 60%),
      radial-gradient(900px 500px at 110% 10%, #3a1630 0%, transparent 55%);
    --panel: #151a24;
    --panel-glass: rgba(21, 26, 36, 0.78);
    --text: #e8ebf3;
    --muted: #9aa3b8;
    --border: #262d3c;
    --accent: #818cf8;
    --accent-2: #f472b6;
    --accent-text: #0d1017;
    --bubble-user: linear-gradient(135deg, #6366f1, #8b5cf6);
    --bubble-bot: #1e2431;
    --focus: #93c5fd;
    --ok: #4ade80;
    --shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
  }
}

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

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

body {
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  background-image: var(--bg-accent);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

a { color: var(--accent); }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  border-radius: 6px;
}

.sr-only {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

[hidden] { display: none !important; }

/* ---------- Page layout ---------- */

.page {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}

.site-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  text-decoration: none;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.site-tag {
  color: var(--muted);
  font-size: 13px;
}

.site-footer {
  padding: 8px 16px calc(10px + env(safe-area-inset-bottom));
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}
.site-footer p { margin: 0; }

/* ---------- Chat panel ---------- */

.chat {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  background: var(--panel);
  border-top: 1px solid var(--border);
  overflow: hidden;
}

@media (min-width: 640px) {
  .chat {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin: 4px auto 8px;
    width: calc(100% - 32px);
  }
}

.chat-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-glass);
  -webkit-backdrop-filter: saturate(160%) blur(12px);
  backdrop-filter: saturate(160%) blur(12px);
  position: relative;
  z-index: 1;
}

.avatar {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  font-weight: 700;
  font-size: 16px;
}

.chat-head-text { min-width: 0; }

.chat-head h1 {
  margin: 0;
  font-size: 16px;
  line-height: 1.3;
  font-weight: 650;
  overflow-wrap: anywhere;
}

.chat-sub {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--ok);
  display: inline-block;
}

.messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}

.msg {
  display: flex;
  max-width: 88%;
  animation: rise 0.18s ease-out;
}
.msg--user { align-self: flex-end; }
.msg--assistant { align-self: flex-start; }

.bubble {
  padding: 10px 14px;
  border-radius: 16px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.msg--user .bubble {
  background: var(--bubble-user);
  color: #fff;
  border-bottom-right-radius: 6px;
}
.msg--user .bubble a { color: #fff; }
.msg--assistant .bubble {
  background: var(--bubble-bot);
  color: var(--text);
  border-bottom-left-radius: 6px;
}
.msg--note .bubble {
  background: transparent;
  color: var(--muted);
  border: 1px dashed var(--border);
  font-size: 14px;
}

.typing .bubble {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  min-height: 40px;
}
.typing i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  opacity: 0.5;
  animation: blink 1.2s infinite ease-in-out;
}
.typing i:nth-child(2) { animation-delay: 0.15s; }
.typing i:nth-child(3) { animation-delay: 0.3s; }

@keyframes blink {
  0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
  40% { opacity: 0.9; transform: translateY(-2px); }
}
@keyframes rise {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

/* ---------- Human check / ending ---------- */

.human-check,
.ending {
  margin: 0 16px 12px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--bubble-bot);
}
.human-check p,
.ending p { margin: 0 0 10px; }
.human-check p { font-weight: 600; }
.turnstile-slot { min-height: 65px; }

.badge-test {
  display: inline-block;
  vertical-align: middle;
  margin-left: 4px;
  padding: 1px 8px;
  border-radius: 999px;
  background: #f59e0b;
  color: #1a1200;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.08em;
}

.key-check label {
  display: block;
  font-weight: 600;
  margin-bottom: 8px;
}
.key-check input {
  width: 100%;
  font: inherit;
  font-size: 16px; /* no iOS zoom */
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  -webkit-appearance: none;
  appearance: none;
}
.key-check input:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 1px;
  border-color: transparent;
}
.key-check input[aria-invalid="true"] { border-color: #dc2626; }
.key-hint {
  margin: 6px 0 0 !important;
  font-size: 13px;
  color: var(--muted);
  font-weight: 400 !important;
}
.key-error {
  margin: 6px 0 0 !important;
  font-size: 14px;
  font-weight: 600 !important;
  color: #dc2626;
}
@media (prefers-color-scheme: dark) {
  .key-error { color: #f87171; }
  .key-check input[aria-invalid="true"] { border-color: #f87171; }
}

.ending { text-align: left; }
.ending p:last-child { margin-bottom: 0; }
.ending-links {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}
.ending-links a {
  display: inline-flex;
  align-items: center;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  font-size: 14px;
}
.ending-links a:hover { border-color: var(--accent); }

/* ---------- Composer ---------- */

.composer {
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--panel);
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.composer textarea {
  flex: 1 1 auto;
  min-width: 0;
  resize: none;
  font: inherit;
  font-size: 16px; /* >= 16px prevents iOS zoom on focus */
  line-height: 1.4;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 12px;
  max-height: 140px;
  -webkit-appearance: none;
  appearance: none;
}
.composer textarea:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 1px;
  border-color: transparent;
}
.composer textarea:disabled { opacity: 0.6; }

.send {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  transition: transform 0.1s ease, opacity 0.15s ease;
}
.send:active { transform: scale(0.94); }
.send:disabled { opacity: 0.4; cursor: not-allowed; }
.send:focus-visible { outline-offset: 3px; border-radius: 50%; }

.composer-meta {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-top: 6px;
  font-size: 12px;
  color: var(--muted);
}
.counter.is-near { color: var(--accent-2); }

.privacy {
  margin: 4px 0 0;
  font-size: 12px;
  color: var(--muted);
}

.noscript {
  position: fixed;
  inset: auto 0 0 0;
  margin: 0;
  padding: 12px;
  text-align: center;
  background: var(--panel);
}

/* ---------- Embedded (?embed=1): only the chat panel, filling the iframe ---------- */

html.is-embed body { background: var(--panel); background-image: none; }
html.is-embed .site-header,
html.is-embed .site-footer { display: none; }
html.is-embed .page { padding: 0; }
html.is-embed .chat {
  max-width: none;
  width: 100%;
  margin: 0;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
}
