:root {
  --bg: #ffffff;
  --sidebar: #f9f9f9;
  --text: #0d0d0d;
  --muted: #5d5d5d;
  --border: #e5e5e5;
  --hover: #ececec;
  --active: #e3e3e3;
  --user-bubble: #f4f4f4;
  --composer: #ffffff;
  --composer-border: #d9d9d9;
  --solid: #0d0d0d;
  --solid-text: #ffffff;
  --code-bg: #f6f6f6;
  --error: #b91c1c;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  --sidebar-width: 260px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #212121;
    --sidebar: #171717;
    --text: #ececec;
    --muted: #b4b4b4;
    --border: #333333;
    --hover: #2a2a2a;
    --active: #303030;
    --user-bubble: #303030;
    --composer: #303030;
    --composer-border: #3d3d3d;
    --solid: #ffffff;
    --solid-text: #0d0d0d;
    --code-bg: #171717;
    --error: #f87171;
    --shadow: none;
  }
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.6;
}

button,
textarea,
select,
input {
  font: inherit;
  color: inherit;
}

button {
  border: 0;
  background: transparent;
  cursor: pointer;
}

svg {
  width: 20px;
  height: 20px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* --- Sign in --- */

.signin {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 12px;
  min-height: 100dvh;
  padding: 24px;
  text-align: center;
}

.signin h1 {
  margin: 0;
  font-size: 2rem;
  font-weight: 600;
}

.signin p {
  margin: 0;
  color: var(--muted);
}

.signin-status {
  min-height: 1.6em;
}

/* --- Layout --- */

.app {
  display: flex;
  height: 100dvh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  flex: none;
  width: var(--sidebar-width);
  padding: 8px;
  background: var(--sidebar);
  overflow: hidden;
}

.sidebar-collapsed .sidebar {
  display: none;
}

.sidebar-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 4px 8px 10px;
}

.brand {
  font-weight: 600;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  color: var(--muted);
}

.icon-button:hover {
  background: var(--hover);
}

.sidebar-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border-radius: 10px;
  text-align: left;
}

.sidebar-item:hover {
  background: var(--hover);
}

.sidebar-item.active {
  background: var(--active);
}

.sidebar-label {
  padding: 18px 10px 6px;
  color: var(--muted);
  font-size: 0.8125rem;
}

.chat-list {
  flex: 1;
  overflow-y: auto;
}

.chat-row {
  display: flex;
  align-items: center;
  border-radius: 10px;
}

.chat-row:hover {
  background: var(--hover);
}

.chat-row.active {
  background: var(--active);
}

.chat-open {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  overflow: hidden;
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-delete {
  width: 28px;
  height: 28px;
  margin-right: 4px;
  border-radius: 6px;
  color: var(--muted);
  font-size: 1.125rem;
  line-height: 1;
  opacity: 0;
}

.chat-row:hover .chat-delete,
.chat-delete:focus-visible {
  opacity: 1;
}

.chat-delete:hover {
  color: var(--text);
}

@media (hover: none) {
  .chat-delete {
    opacity: 1;
  }
}

.sidebar-account {
  position: relative;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.account-button {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border-radius: 10px;
  text-align: left;
}

.account-button:hover {
  background: var(--hover);
}

.avatar {
  flex: none;
  width: 28px;
  height: 28px;
  border-radius: 50%;
}

.avatar-fallback {
  display: grid;
  place-items: center;
  background: var(--solid);
  color: var(--solid-text);
  font-size: 0.8125rem;
  font-weight: 600;
}

.user-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.menu {
  position: absolute;
  right: 0;
  bottom: calc(100% + 4px);
  left: 0;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.menu-email {
  padding: 6px 10px;
  overflow: hidden;
  color: var(--muted);
  font-size: 0.875rem;
  text-overflow: ellipsis;
}

.menu-item {
  width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  text-align: left;
}

.menu-item:hover {
  background: var(--hover);
}

.scrim {
  display: none;
}

.main {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 52px;
  padding: 0 12px;
}

#sidebar-open {
  display: none;
}

.sidebar-collapsed #sidebar-open {
  display: grid;
}

.model-name {
  padding: 6px 8px;
  font-size: 1.125rem;
  font-weight: 500;
}

.model-tag {
  color: var(--muted);
  font-weight: 400;
}

/* --- Thread --- */

.scroller {
  flex: 1;
  overflow-y: auto;
}

.thread {
  display: flex;
  flex-direction: column;
  gap: 20px;
  max-width: 768px;
  margin: 0 auto;
  padding: 16px 20px 32px;
}

.msg.user {
  align-self: flex-end;
  max-width: 75%;
}

.msg.user .content {
  padding: 10px 18px;
  border-radius: 22px;
  background: var(--user-bubble);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg.assistant {
  overflow-wrap: anywhere;
}

.msg.assistant .content > :first-child {
  margin-top: 0;
}

.msg.assistant .content > :last-child {
  margin-bottom: 0;
}

.msg.assistant p,
.msg.assistant ul,
.msg.assistant ol {
  margin: 0 0 14px;
}

.msg.assistant ul,
.msg.assistant ol {
  padding-left: 26px;
}

.msg.assistant li {
  margin: 4px 0;
}

.msg.assistant h2,
.msg.assistant h3,
.msg.assistant h4 {
  margin: 22px 0 10px;
  font-weight: 600;
  line-height: 1.3;
}

.msg.assistant h2 {
  font-size: 1.375rem;
}

.msg.assistant h3 {
  font-size: 1.1875rem;
}

.msg.assistant h4 {
  font-size: 1rem;
}

.msg.assistant hr {
  margin: 20px 0;
  border: 0;
  border-top: 1px solid var(--border);
}

.msg.assistant a {
  color: inherit;
  text-decoration: underline;
}

.msg.assistant :not(pre) > code {
  padding: 2px 5px;
  border-radius: 6px;
  background: var(--hover);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.875em;
}

.code-block {
  margin: 0 0 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--code-bg);
  overflow: hidden;
}

.code-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 8px 0 14px;
  color: var(--muted);
  font-size: 0.8125rem;
}

.copy-code,
.copy-message {
  padding: 4px 8px;
  border-radius: 6px;
  color: var(--muted);
  font-size: 0.8125rem;
}

.copy-code:hover,
.copy-message:hover {
  background: var(--hover);
  color: var(--text);
}

.code-block pre {
  margin: 0;
  padding: 10px 14px 14px;
  overflow-x: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.875rem;
  line-height: 1.5;
}

.copy-message {
  margin: 6px 0 0 -8px;
}

.msg.pending .copy-message {
  display: none;
}

.msg.pending .content::after {
  content: "";
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--text);
  animation: pulse 1.1s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(0.7); opacity: 0.5; }
  50% { transform: scale(1); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .msg.pending .content::after {
    animation: none;
  }
}

.msg.error {
  padding: 10px 14px;
  border: 1px solid var(--error);
  border-radius: 12px;
  color: var(--error);
}

/* --- Composer --- */

.composer-area {
  width: 100%;
  max-width: 768px;
  margin: 0 auto;
  padding: 0 20px 8px;
}

.greeting {
  display: none;
  margin: 0 0 28px;
  font-size: 1.75rem;
  font-weight: 500;
  text-align: center;
}

/* A new chat has no messages yet: centre the greeting and composer on the page. */
.main.empty .scroller {
  display: none;
}

.main.empty .composer-area {
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: center;
  padding-bottom: 12vh;
}

.main.empty .greeting {
  display: block;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 10px 10px 20px;
  border: 1px solid var(--composer-border);
  border-radius: 28px;
  background: var(--composer);
  box-shadow: var(--shadow);
}

.composer textarea {
  flex: 1;
  min-height: 36px;
  max-height: 200px;
  padding: 5px 0;
  border: 0;
  outline: 0;
  background: transparent;
  resize: none;
}

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

.send-button {
  display: grid;
  flex: none;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--solid);
  color: var(--solid-text);
}

.send-button:disabled {
  opacity: 0.3;
  cursor: default;
}

.send-button svg {
  stroke-width: 2.4;
}

.send-button .icon-stop,
.send-button.stop .icon-send {
  display: none;
}

.send-button.stop .icon-stop {
  display: block;
  fill: currentColor;
  stroke: none;
}

.disclaimer {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.75rem;
  text-align: center;
}

/* --- Compress image --- */

.compress-view {
  flex: 1;
  overflow-y: auto;
}

.main.compressing .scroller,
.main.compressing .composer-area {
  display: none;
}

.compress-inner {
  max-width: 768px;
  margin: 0 auto;
  padding: 16px 20px 40px;
}

.compress-inner h1 {
  margin: 0 0 4px;
  font-size: 1.75rem;
  font-weight: 500;
}

.compress-note {
  margin: 0 0 20px;
  color: var(--muted);
}

.drop-zone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 32px 20px;
  border: 1.5px dashed var(--composer-border);
  border-radius: 20px;
  text-align: center;
  cursor: pointer;
  overflow-wrap: anywhere;
}

.drop-zone:hover,
.drop-zone.dragging {
  background: var(--sidebar);
  border-color: var(--muted);
}

.drop-zone span {
  color: var(--muted);
  font-size: 0.875rem;
}

.compress-controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin: 20px 0;
}

.compress-controls label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--muted);
  font-size: 0.875rem;
}


.compress-controls select {
  padding: 8px 10px;
  border: 1px solid var(--composer-border);
  border-radius: 10px;
  background: var(--composer);
  color: var(--text);
}

.compress-controls input[type="range"] {
  accent-color: var(--solid);
}

.compress-status {
  min-height: 1.6em;
  margin: 0 0 12px;
  font-variant-numeric: tabular-nums;
}

.compress-status.error {
  color: var(--error);
}

.compress-result {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 14px;
}

.compress-result img {
  max-width: 100%;
  max-height: 420px;
  border: 1px solid var(--border);
  border-radius: 12px;
}

.download-button {
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--solid);
  color: var(--solid-text);
  font-weight: 500;
  text-decoration: none;
}

/* --- Small screens: the sidebar slides over the chat --- */

@media (max-width: 767px) {
  .sidebar,
  .sidebar-collapsed .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    display: flex;
    width: min(var(--sidebar-width), 85vw);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }

  .sidebar-open .sidebar {
    transform: none;
  }

  .sidebar-open .scrim {
    position: fixed;
    inset: 0;
    z-index: 10;
    display: block;
    background: rgba(0, 0, 0, 0.4);
  }

  #sidebar-open {
    display: grid;
  }

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

  .thread,
  .composer-area,
  .compress-inner {
    padding-right: 14px;
    padding-left: 14px;
  }
}
