/* OpenCode palette — dark by default, system light on touch devices. */
:root {
  color-scheme: dark;

  --bg: #0a0a0a;
  --panel: #141414;
  --element: #1e1e1e;
  --line: #3c3c3c;
  --line-strong: #484848;
  --text: #eeeeee;
  --muted: #808080;
  --primary: #fab283;
  --primary-hover: #ffc09f;
  --on-primary: #0a0a0a;
  --secondary: #5c9cf5;
  --accent: #9d7cd8;
  --green: #7fd88f;
  --cyan: #56b6c2;
  --yellow: #e5c07b;
  --orange: #f5a742;
  --red: #e06c75;
  --code-bg: #141414;
  --user-bg: #1e1e1e;
  --user-line: #484848;
  --selection: #323232;

  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Cascadia Code", Menlo,
    Consolas, monospace;
}

/* Desktop stays dark. Touch devices follow the system light/dark preference. */
@media (hover: none) and (pointer: coarse) {
  @media (prefers-color-scheme: light) {
    :root {
      color-scheme: light;

      --bg: #ffffff;
      --panel: #fafafa;
      --element: #f5f5f5;
      --line: #d4d4d4;
      --line-strong: #b8b8b8;
      --text: #1a1a1a;
      --muted: #8a8a8a;
      --primary: #3b7dd8;
      --primary-hover: #2968c3;
      --on-primary: #ffffff;
      --secondary: #7b5bb6;
      --accent: #d68c27;
      --green: #3d9a57;
      --cyan: #318795;
      --yellow: #b0851f;
      --orange: #d68c27;
      --red: #d1383d;
      --code-bg: #f5f5f5;
      --user-bg: #f5f5f5;
      --user-line: #d4d4d4;
      --selection: #e1e1e1;
    }
  }
}

* {
  box-sizing: border-box;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}

::selection {
  background: var(--selection);
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
}

body.center {
  display: grid;
  place-items: center;
  padding: max(1rem, env(safe-area-inset-top))
    max(1rem, env(safe-area-inset-right))
    max(1rem, env(safe-area-inset-bottom))
    max(1rem, env(safe-area-inset-left));
}

.chat {
  width: min(840px, 100%);
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  padding: 0 1rem;
  padding-bottom: env(safe-area-inset-bottom);
  display: flex;
  flex-direction: column;
}

header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.9rem 0;
  border-bottom: 1px solid var(--line);
}

h1 {
  margin: 0;
  font: 600 1.05rem/1.2 var(--mono);
  letter-spacing: -0.01em;
}

.mark {
  color: var(--primary);
}

.sub {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.78rem;
}

.actions {
  display: flex;
  gap: 0.4rem;
}

.actions form {
  margin: 0;
}

button {
  border: 1px solid var(--line-strong);
  background: var(--element);
  color: var(--text);
  border-radius: 8px;
  padding: 0.55rem 0.9rem;
  font: inherit;
  cursor: pointer;
}

button:hover:not(:disabled) {
  border-color: var(--muted);
}

button:disabled {
  opacity: 0.5;
  cursor: default;
}

button.ghost {
  background: transparent;
  border-color: transparent;
  padding: 0.4rem 0.7rem;
  font-size: 0.85rem;
  color: var(--muted);
}

button.ghost:hover:not(:disabled) {
  color: var(--text);
  border-color: var(--line);
}

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

#messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1.2rem 0 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.empty {
  color: var(--muted);
  margin: auto;
}

.msg {
  border-radius: 12px;
  overflow-wrap: anywhere;
}

.msg.user {
  align-self: flex-end;
  max-width: 85%;
  padding: 0.7rem 0.9rem;
  background: var(--user-bg);
  border: 1px solid var(--user-line);
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}

.msg.assistant {
  align-self: stretch;
  max-width: 100%;
  padding: 0.8rem 1rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
}

.msg .status {
  display: block;
  color: var(--muted);
  font: italic 0.85rem/1.4 var(--mono);
}

.msg.streaming .md:not(:empty)::after {
  content: "▍";
  color: var(--primary);
  animation: blink 1s steps(1) infinite;
}

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

/* Markdown */

.md {
  font-size: 0.95rem;
  line-height: 1.62;
  overflow-wrap: anywhere;
}

.md > :first-child {
  margin-top: 0;
}

.md > :last-child {
  margin-bottom: 0;
}

.md p,
.md ul,
.md ol,
.md pre,
.md blockquote,
.md table,
.md hr {
  margin: 0 0 0.85em;
}

.md h1,
.md h2,
.md h3,
.md h4,
.md h5,
.md h6 {
  margin: 1.2em 0 0.5em;
  color: var(--accent);
  font-weight: 600;
  line-height: 1.3;
}

.md h1 {
  font-size: 1.3em;
  padding-bottom: 0.25em;
  border-bottom: 1px solid var(--line);
}

.md h2 {
  font-size: 1.15em;
}

.md h3 {
  font-size: 1.02em;
}

.md h4,
.md h5,
.md h6 {
  color: var(--text);
  font-size: 1em;
}

.md strong {
  color: var(--orange);
  font-weight: 600;
}

.md em {
  color: var(--yellow);
}

.md a {
  color: var(--primary);
  text-decoration: underline;
  text-decoration-color: var(--line-strong);
  text-underline-offset: 2px;
}

.md a:hover {
  color: var(--primary-hover);
  text-decoration-color: currentColor;
}

.md ul,
.md ol {
  padding-left: 1.4rem;
}

.md li {
  margin: 0.2em 0;
}

.md li::marker {
  color: var(--primary);
}

.md blockquote {
  padding: 0.15rem 0 0.15rem 0.9rem;
  border-left: 3px solid var(--yellow);
  color: var(--muted);
}

.md blockquote > :last-child {
  margin-bottom: 0;
}

.md code {
  padding: 0.12em 0.35em;
  border-radius: 5px;
  background: var(--element);
  color: var(--green);
  font: 0.88em/1.5 var(--mono);
}

.md pre {
  padding: 0.85rem 1rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--code-bg);
  overflow-x: auto;
}

.md pre code {
  display: block;
  padding: 0;
  border: 0;
  background: none;
  color: var(--text);
  font-size: 0.84rem;
  line-height: 1.55;
  white-space: pre;
}

.md hr {
  border: 0;
  border-top: 1px solid var(--line);
  margin: 1.2em 0;
}

.md table {
  display: block;
  width: max-content;
  max-width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
  font-size: 0.9em;
}

.md th,
.md td {
  padding: 0.4rem 0.65rem;
  border: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.md th {
  background: var(--element);
  font-weight: 600;
}

/* Composer */

#composer {
  flex: 0 0 auto;
  display: flex;
  gap: 0.6rem;
  align-items: flex-end;
  border-top: 1px solid var(--line);
  padding: 0.8rem 0 0.2rem;
}

textarea {
  flex: 1;
  min-width: 0;
  min-height: 2.9rem;
  max-height: 40dvh;
  resize: none;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  padding: 0.65rem 0.8rem;
  font: inherit;
  field-sizing: content;
}

textarea::placeholder {
  color: var(--muted);
}

textarea:focus,
input:focus {
  outline: none;
  border-color: var(--primary);
}

#send {
  flex: 0 0 auto;
  background: var(--primary);
  border-color: transparent;
  color: var(--on-primary);
  font-weight: 600;
}

#send:hover:not(:disabled) {
  background: var(--primary-hover);
}

.hint {
  flex: 0 0 auto;
  color: var(--muted);
  font: 0.72rem/1.4 var(--mono);
  margin: 0.35rem 0 0.6rem;
}

.err {
  color: var(--red);
  font-size: 0.85rem;
  margin: 0.2rem 0;
}

/* Login */

.login {
  width: min(380px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 1.6rem;
}

.login h1 {
  font-size: 1.3rem;
}

.login .sub {
  margin-bottom: 1.2rem;
}

.login form {
  display: flex;
  gap: 0.5rem;
}

.login input {
  flex: 1;
  min-width: 0;
  background: var(--element);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 0.55rem 0.7rem;
  color: var(--text);
  font: inherit;
}

.login .err {
  margin-top: 0.9rem;
}

@media (max-width: 600px) {
  .chat {
    padding-left: max(0.75rem, env(safe-area-inset-left));
    padding-right: max(0.75rem, env(safe-area-inset-right));
  }

  header {
    padding: 0.7rem 0;
  }

  h1 {
    font-size: 0.95rem;
  }

  .sub {
    display: none;
  }

  .actions {
    gap: 0.15rem;
  }

  button.ghost {
    padding: 0.45rem 0.5rem;
    font-size: 0.78rem;
  }

  #messages {
    padding: 0.9rem 0 0.75rem;
    gap: 0.7rem;
  }

  .msg.user {
    max-width: 92%;
  }

  .msg.assistant {
    padding: 0.75rem 0.8rem;
  }

  .md {
    font-size: 0.93rem;
  }

  textarea {
    font-size: 16px;
    padding: 0.55rem 0.7rem;
  }

  #send {
    padding: 0.55rem 0.8rem;
  }
}

@media (hover: none) {
  .hint {
    display: none;
  }
}

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