:root {
  --ink: #e9eef8;
  --ink-dim: #a9b4c9;
  --ink-faint: #6f7c95;
  --gold: #d9b878;
  --gold-soft: rgba(217, 184, 120, 0.35);
  --azure: #6fb3ff;
  --azure-glow: rgba(111, 179, 255, 0.35);
  --glass: rgba(8, 14, 30, 0.58);
  --glass-strong: rgba(6, 11, 25, 0.78);
  --line: rgba(190, 210, 255, 0.14);
  --line-strong: rgba(217, 184, 120, 0.4);
  --user-bubble: rgba(32, 58, 110, 0.62);
  --danger: #ff8c8c;
  --radius: 18px;
  --maxw: 860px;
  --serif: "Cinzel", "Trajan Pro", Georgia, serif;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: #050914;
  color: var(--ink);
  font: 15px/1.6 var(--sans);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }

/* ------------------------------------------------------------ background */
.cosmos { position: fixed; inset: 0; z-index: 0; overflow: hidden; background: #050914; }
.cosmos-art {
  position: absolute; inset: -3%;
  background: url("/assets/bg-portrait.jpg") center / cover no-repeat;
  animation: drift 90s ease-in-out infinite alternate;
  will-change: transform;
}
@media (orientation: landscape) and (min-aspect-ratio: 1/1) {
  .cosmos-art { background-image: image-set(url("/assets/bg-wide-1280.jpg") 1x, url("/assets/bg-wide.jpg") 2x); }
}
@media (orientation: landscape) and (min-width: 1400px) {
  .cosmos-art { background-image: url("/assets/bg-wide.jpg"); }
}
.cosmos-veil {
  position: absolute; inset: 0;
  background:
    radial-gradient(ellipse at 50% 40%, rgba(5, 9, 20, 0.05) 0%, rgba(5, 9, 20, 0.45) 70%, rgba(5, 9, 20, 0.75) 100%),
    linear-gradient(180deg, rgba(5, 9, 20, 0.35) 0%, rgba(5, 9, 20, 0.1) 30%, rgba(5, 9, 20, 0.55) 100%);
  transition: background 0.6s ease;
}
body.chatting .cosmos-veil { background-color: rgba(5, 9, 20, 0.32); }
@keyframes drift {
  from { transform: scale(1) rotate(0deg); }
  to { transform: scale(1.06) rotate(1.2deg); }
}
@media (prefers-reduced-motion: reduce) { .cosmos-art { animation: none; } }

/* ------------------------------------------------------------ common */
.glass {
  background: var(--glass);
  border: 1px solid var(--line);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.04);
  backdrop-filter: blur(18px) saturate(140%);
  -webkit-backdrop-filter: blur(18px) saturate(140%);
}
.wordmark {
  font-family: var(--serif); font-weight: 600; letter-spacing: 0.32em; margin: 0;
  background: linear-gradient(180deg, #fff6e0 0%, var(--gold) 55%, #a8834a 100%);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  text-shadow: 0 0 30px rgba(217, 184, 120, 0.15);
  padding-left: 0.32em; /* optically balance trailing letter-spacing */
}
.wordmark.small { font-size: 17px; letter-spacing: 0.28em; }
.crest {
  width: 64px; height: 64px; margin: 0 auto 14px; border-radius: 50%;
  display: grid; place-items: center; position: relative;
  border: 1px solid var(--line-strong);
  box-shadow: 0 0 0 6px rgba(217, 184, 120, 0.06), 0 0 40px var(--azure-glow), inset 0 0 18px rgba(111, 179, 255, 0.25);
  background: radial-gradient(circle at 50% 45%, rgba(111, 179, 255, 0.35), rgba(5, 9, 20, 0.6) 62%);
}
.crest::after {
  content: ""; position: absolute; inset: 6px; border-radius: 50%;
  border: 1px dashed rgba(217, 184, 120, 0.35);
  animation: spin 60s linear infinite;
}
.crest span { font-family: var(--serif); font-size: 15px; color: var(--gold); letter-spacing: 0.06em; }
.crest.big { width: 84px; height: 84px; }
.crest.big span { font-size: 19px; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .crest::after { animation: none; } }

.icon-btn {
  width: 40px; height: 40px; flex: none; border-radius: 12px; border: 1px solid transparent;
  background: transparent; display: grid; place-items: center; cursor: pointer; color: var(--ink-dim);
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.icon-btn:hover, .icon-btn:focus-visible { background: rgba(111, 179, 255, 0.12); color: var(--ink); border-color: var(--line); outline: none; }
.icon-btn svg, .send svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

/* ------------------------------------------------------------ login */
.login {
  position: relative; z-index: 1; min-height: 100dvh; display: grid; place-items: center;
  padding: max(24px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
  overflow-y: auto; height: 100dvh;
}
.login-card { width: min(400px, 100%); border-radius: 26px; padding: 36px 30px 26px; text-align: center; animation: rise 0.7s ease both; }
.login-card .wordmark { font-size: clamp(28px, 7vw, 36px); }
.tagline { margin: 8px 0 26px; color: var(--ink-dim); font-size: 12.5px; letter-spacing: 0.08em; text-transform: uppercase; line-height: 1.7; }
.field { display: block; text-align: left; margin-bottom: 14px; }
.field span { display: block; font-size: 12px; color: var(--ink-dim); margin: 0 0 6px 2px; letter-spacing: 0.04em; }
.field input {
  width: 100%; padding: 13px 14px; border-radius: 12px; border: 1px solid var(--line);
  background: rgba(3, 7, 18, 0.6); color: var(--ink); font: 16px var(--sans); outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.field input:focus { border-color: var(--gold-soft); box-shadow: 0 0 0 3px rgba(217, 184, 120, 0.12); }
.login-error { min-height: 20px; margin: 4px 0 8px; color: var(--danger); font-size: 13px; }
.btn-primary {
  width: 100%; padding: 13px; border-radius: 12px; border: 1px solid rgba(217, 184, 120, 0.55); cursor: pointer;
  background: linear-gradient(180deg, rgba(217, 184, 120, 0.28), rgba(217, 184, 120, 0.12));
  color: #fff3d6; font-weight: 600; letter-spacing: 0.2em; text-transform: uppercase; font-size: 13px;
  transition: transform 0.15s, box-shadow 0.2s, background 0.2s;
}
.btn-primary:hover { box-shadow: 0 0 24px rgba(217, 184, 120, 0.25); background: linear-gradient(180deg, rgba(217, 184, 120, 0.38), rgba(217, 184, 120, 0.18)); }
.btn-primary:active { transform: translateY(1px); }
.btn-primary:disabled { opacity: 0.6; cursor: progress; }
.fineprint { margin: 18px 0 0; font-size: 11.5px; color: var(--ink-faint); letter-spacing: 0.06em; }
@keyframes rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }

/* ------------------------------------------------------------ shell */
.shell {
  position: relative; z-index: 1; height: 100dvh; display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
}
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  background: linear-gradient(180deg, rgba(5, 9, 20, 0.75), rgba(5, 9, 20, 0));
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.top-actions { display: flex; gap: 4px; }
.pill {
  font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase; padding: 3px 9px 3px 20px; border-radius: 999px;
  border: 1px solid var(--line); color: var(--ink-dim); position: relative; white-space: nowrap;
}
.pill::before { content: ""; position: absolute; left: 8px; top: 50%; width: 6px; height: 6px; margin-top: -3px; border-radius: 50%; background: #5ee0a0; box-shadow: 0 0 8px #5ee0a0; }
.pill.engine::before { background: var(--gold); box-shadow: 0 0 8px var(--gold); }

.thread { flex: 1; overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; padding: 8px 16px 20px; }
.thread::-webkit-scrollbar { width: 8px; }
.thread::-webkit-scrollbar-thumb { background: rgba(190, 210, 255, 0.15); border-radius: 8px; }

.welcome { max-width: 620px; margin: clamp(24px, 9vh, 96px) auto 24px; text-align: center; animation: rise 0.8s ease both; }
.welcome .wordmark { font-size: clamp(30px, 8vw, 52px); }
.welcome p { color: var(--ink-dim); margin: 14px auto 22px; max-width: 520px; text-shadow: 0 1px 12px rgba(0, 0, 0, 0.8); }
.suggestions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.chip {
  padding: 9px 15px; border-radius: 999px; border: 1px solid var(--line); cursor: pointer; font-size: 13.5px;
  background: rgba(8, 14, 30, 0.55); color: var(--ink); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  transition: border-color 0.2s, background 0.2s, transform 0.15s;
}
.chip:hover { border-color: var(--gold-soft); background: rgba(20, 32, 64, 0.7); transform: translateY(-1px); }

/* ------------------------------------------------------------ messages */
.messages { max-width: var(--maxw); margin: 0 auto; display: flex; flex-direction: column; gap: 18px; }
.msg { display: flex; flex-direction: column; animation: rise 0.35s ease both; min-width: 0; }
.msg.user { align-items: flex-end; }
.msg .role { font-size: 11px; letter-spacing: 0.18em; text-transform: uppercase; color: var(--ink-faint); margin: 0 6px 5px; }
.msg.assistant .role { color: var(--gold); font-family: var(--serif); letter-spacing: 0.24em; }
.bubble {
  max-width: min(100%, 760px); padding: 13px 17px; border-radius: var(--radius); overflow-wrap: anywhere; min-width: 0;
  border: 1px solid var(--line);
}
.msg.user .bubble { background: var(--user-bubble); border-color: rgba(111, 179, 255, 0.25); border-bottom-right-radius: 6px; white-space: pre-wrap; }
.msg.assistant .bubble { background: var(--glass-strong); border-bottom-left-radius: 6px; backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); width: 100%; }
.msg.error .bubble { border-color: rgba(255, 140, 140, 0.4); }

.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 { font-family: var(--serif); color: #f3e3bf; letter-spacing: 0.04em; line-height: 1.3; margin: 1.1em 0 0.5em; font-weight: 600; }
.md h1 { font-size: 1.3em; } .md h2 { font-size: 1.18em; } .md h3 { font-size: 1.06em; } .md h4 { font-size: 1em; }
.md p { margin: 0.6em 0; }
.md ul, .md ol { margin: 0.5em 0; padding-left: 1.4em; }
.md li { margin: 0.2em 0; }
.md code { font-family: var(--mono); font-size: 0.88em; background: rgba(111, 179, 255, 0.12); padding: 1px 5px; border-radius: 5px; }
.md pre { background: rgba(0, 0, 0, 0.45); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; overflow-x: auto; }
.md pre code { background: none; padding: 0; font-size: 0.85em; }
.md blockquote { margin: 0.6em 0; padding: 4px 12px; border-left: 3px solid var(--gold-soft); color: var(--ink-dim); }
.md a { color: var(--azure); }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }
.md .table-wrap { overflow-x: auto; margin: 0.7em 0; border: 1px solid var(--line); border-radius: 10px; }
.md table { border-collapse: collapse; width: 100%; font-size: 0.92em; }
.md th, .md td { padding: 7px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.md th { background: rgba(111, 179, 255, 0.08); font-weight: 600; color: #dfe8fb; white-space: nowrap; }
.md tr:last-child td { border-bottom: 0; }
.md td.pass { color: #7fe0ad; } .md td.fail { color: var(--danger); } .md td.warn { color: #f5cf7a; } .md td.ne { color: var(--ink-faint); }

.att-row { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 8px; justify-content: flex-end; max-width: 100%; }
.att {
  display: inline-flex; align-items: center; gap: 8px; max-width: 260px; padding: 6px 10px; border-radius: 10px;
  background: rgba(8, 14, 30, 0.7); border: 1px solid var(--line); font-size: 12.5px; color: var(--ink-dim);
}
.att img { width: 34px; height: 34px; object-fit: cover; border-radius: 6px; }
.att .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att .x { border: 0; background: none; cursor: pointer; color: var(--ink-faint); padding: 0 2px; font-size: 16px; line-height: 1; }
.att .x:hover { color: var(--ink); }
.att .ext { font: 600 10px var(--mono); color: var(--gold); border: 1px solid var(--gold-soft); border-radius: 4px; padding: 1px 4px; }

.artifacts { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; margin-top: 12px; }
.artifact {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 12px; cursor: pointer; text-align: left;
  border: 1px solid var(--line); background: rgba(111, 179, 255, 0.06); color: var(--ink); min-width: 0;
  transition: border-color 0.2s, background 0.2s;
}
.artifact:hover { border-color: var(--gold-soft); background: rgba(111, 179, 255, 0.12); }
.artifact .ico { flex: none; width: 34px; height: 34px; border-radius: 8px; display: grid; place-items: center; font: 600 10px var(--mono); color: var(--gold); border: 1px solid var(--gold-soft); }
.artifact .lbl { font-size: 12.5px; line-height: 1.35; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow-wrap: anywhere; }
.artifact.preview { order: -1; grid-column: 1 / -1; padding: 0; overflow: hidden; display: block; background: #fff; }
.artifact.preview img { display: block; width: 100%; max-height: 420px; object-fit: contain; background: #fff; }
.artifact.preview .cap { display: block; padding: 6px 10px; font-size: 12px; background: var(--glass-strong); color: var(--ink-dim); }

.typing { display: inline-flex; align-items: center; gap: 10px; color: var(--ink-dim); font-size: 13.5px; }
.orbit { width: 22px; height: 22px; border-radius: 50%; border: 1px solid var(--gold-soft); position: relative; animation: spin 1.6s linear infinite; }
.orbit::after { content: ""; position: absolute; top: -3px; left: 50%; margin-left: -3px; width: 6px; height: 6px; border-radius: 50%; background: var(--azure); box-shadow: 0 0 10px var(--azure); }

/* ------------------------------------------------------------ composer */
.composer-wrap {
  padding: 6px max(12px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  background: linear-gradient(0deg, rgba(5, 9, 20, 0.85), rgba(5, 9, 20, 0)); position: relative;
}
.pending { max-width: var(--maxw); margin: 0 auto; display: flex; flex-wrap: wrap; gap: 8px; }
.pending:not(:empty) { margin-bottom: 8px; }
.composer {
  max-width: var(--maxw); margin: 0 auto; display: flex; align-items: flex-end; gap: 4px;
  padding: 7px; border-radius: 22px; transition: border-color 0.2s, box-shadow 0.2s;
}
.composer:focus-within { border-color: var(--gold-soft); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45), 0 0 0 3px rgba(217, 184, 120, 0.08); }
.composer textarea {
  flex: 1; min-width: 0; resize: none; border: 0; outline: none; background: transparent; color: var(--ink);
  font: 16px/1.5 var(--sans); padding: 8px 6px; max-height: 40vh; min-height: 40px;
}
.composer textarea::placeholder { color: var(--ink-faint); }
.send {
  width: 40px; height: 40px; flex: none; border-radius: 14px; cursor: pointer; display: grid; place-items: center;
  border: 1px solid rgba(217, 184, 120, 0.55); background: linear-gradient(180deg, rgba(217, 184, 120, 0.4), rgba(217, 184, 120, 0.15)); color: #fff3d6;
  transition: opacity 0.2s, box-shadow 0.2s;
}
.send:hover:not(:disabled) { box-shadow: 0 0 18px rgba(217, 184, 120, 0.35); }
.send:disabled { opacity: 0.35; cursor: default; }
.disclaimer { text-align: center; margin: 7px 0 0; font-size: 11px; color: var(--ink-faint); letter-spacing: 0.04em; }
.drop-hint {
  position: fixed; inset: 12px; z-index: 5; border: 2px dashed var(--gold-soft); border-radius: 24px; display: grid; place-items: center;
  background: rgba(5, 9, 20, 0.6); font-family: var(--serif); letter-spacing: 0.2em; color: var(--gold); pointer-events: none;
}

.viewer { position: fixed; inset: 0; z-index: 10; background: rgba(2, 4, 10, 0.92); display: grid; place-items: center; padding: 16px; }
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; background: #fff; border-radius: 8px; }
.viewer-close { position: absolute; top: max(12px, env(safe-area-inset-top)); right: 12px; background: rgba(8, 14, 30, 0.8); }

/* ------------------------------------------------------------ small screens */
@media (max-width: 600px) {
  body { font-size: 15px; }
  .thread { padding: 4px 10px 16px; }
  .bubble { padding: 11px 14px; }
  .msg.user .bubble { max-width: 88%; }
  .disclaimer { display: none; }
  .icon-btn { width: 38px; height: 38px; }
  .login-card { padding: 30px 22px 22px; border-radius: 22px; }
  .artifacts { grid-template-columns: 1fr 1fr; }
}
@media (max-height: 520px) and (orientation: landscape) {
  .welcome { margin-top: 8px; } .welcome .crest { display: none; }
}
