:root {
  color-scheme: dark;

  --bg:            #0d0f12;
  --surface:       #111516;
  --surface-mid:   #15191b;
  --surface-green: #18211c;
  --surface-chat:  #203226;

  --border:        #2c3430;
  --border-subtle: #252b2a;
  --border-green:  #365343;
  --border-active: #5f8f6b;

  --text:          #f3f6f4;
  --text-user:     #e7f3ea;
  --text-muted:    #8f9b95;
  --text-dim:      #a8b3ad;
  --text-green:    #93b99d;
  --text-message:  #dce5df;

  --error:         #ffb4a8;
  --danger:        #ffcbc4;
  --danger-bg:     #351818;
  --danger-border: #6e2d2d;
}

* {
  box-sizing: border-box;
}

html {
  width: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  min-width: 320px;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

main {
  display: grid;
  grid-template-rows: auto 1fr;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  max-width: 1120px;
  margin: 0 auto;
  padding: 18px;
  gap: 16px;
  overflow: hidden;
}

header,
.header-actions,
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

header > div,
.header-actions,
.chat-panel {
  min-width: 0;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 1.35rem;
}

header p,
.muted,
.conversation-list small {
  color: var(--text-muted);
  font-size: 0.85rem;
}

button {
  min-height: 42px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-green);
  color: var(--text-user);
  padding: 0 14px;
  font: inherit;
  cursor: pointer;
  transition: background 150ms ease, border-color 150ms ease, color 150ms ease;
}

button:hover:not(:disabled) {
  background: #1c2720;
  border-color: #3d5449;
}

button:disabled {
  color: var(--text-muted);
  cursor: default;
  opacity: 0.6;
}

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

input,
select,
textarea {
  min-width: 0;
  min-height: 46px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  padding: 0 12px;
  font: inherit;
  font-size: 16px;
  transition: border-color 150ms ease;
}

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

textarea {
  min-height: 120px;
  padding: 10px;
  resize: vertical;
}

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

.status-online {
  color: var(--text-green);
}

.menu-button,
.sidebar-backdrop {
  display: none;
}

.mode-select {
  min-height: 36px;
  width: 116px;
  padding: 0 8px;
}

/* ── Auth ── */

.auth-panel {
  align-self: center;
  display: grid;
  gap: 14px;
  width: min(100%, 360px);
  margin: 0 auto;
  padding-bottom: 8vh;
}

.auth-copy {
  display: grid;
  gap: 6px;
}

.auth-copy p,
.modal p {
  color: var(--text-green);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
}

.auth-copy h2 {
  font-size: 1.55rem;
}

.field-label {
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-bottom: -6px;
}

.password-wrapper {
  position: relative;
  display: grid;
}

.password-wrapper input {
  padding-right: 48px;
}

.password-toggle {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  min-height: 0;
  height: 100%;
  width: 44px;
  border: 0;
  border-radius: 0 8px 8px 0;
  background: transparent;
  color: var(--text-muted);
  padding: 0;
  font-size: 0.8rem;
}

.password-toggle:hover:not(:disabled) {
  background: transparent;
  color: var(--text-user);
  border-color: transparent;
}

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

/* ── Layout ── */

.chat-shell {
  display: grid;
  grid-template-columns: 250px minmax(0, 1fr);
  min-height: 0;
  height: 100%;
  overflow: hidden;
  gap: 16px;
}

.sidebar {
  min-height: 0;
  height: 100%;
  border-right: 1px solid var(--border-subtle);
  padding-right: 14px;
  overflow-y: auto;
}

.sidebar-head h2 {
  font-size: 1rem;
}

.new-chat,
.memory-toggle,
.project-toggle,
.view-memories,
.activity-button,
.approvals-button,
.changes-button {
  width: 100%;
  margin: 10px 0 12px;
  background: var(--surface-chat);
  border-color: var(--border-green);
  font-weight: 700;
}

.new-chat:hover:not(:disabled),
.memory-toggle:hover:not(:disabled),
.project-toggle:hover:not(:disabled),
.view-memories:hover:not(:disabled),
.activity-button:hover:not(:disabled),
.approvals-button:hover:not(:disabled),
.changes-button:hover:not(:disabled) {
  background: #264030;
  border-color: #4a6e57;
}

.conversation-list,
.memory-list,
.memory-editor,
.project-editor,
.project-form {
  display: grid;
  gap: 8px;
}

.conversation-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  overflow: hidden;
}

.conversation-item > button:first-child {
  display: grid;
  gap: 4px;
  width: 100%;
  min-height: 54px;
  border: 0;
  border-radius: 0;
  background: transparent;
  padding: 10px;
  text-align: left;
}

.conversation-item > button:first-child:hover:not(:disabled) {
  background: #161c19;
  border-color: transparent;
}

.conversation-item span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.active-chat {
  border-color: var(--border-active);
  background: #18251c;
}

.rename-chat,
.delete-chat {
  min-height: 44px;
  border: 0;
  border-left: 1px solid var(--border);
  border-radius: 0;
  background: transparent;
  font-size: 0.78rem;
  padding: 0 10px;
}

.rename-chat {
  color: var(--text-muted);
}

.rename-chat:hover:not(:disabled) {
  background: #161c19;
  color: var(--text-user);
  border-color: var(--border);
}

.delete-chat,
.memory-list button {
  color: #b78b83;
}

.delete-chat:hover:not(:disabled),
.memory-list button:hover:not(:disabled) {
  color: var(--danger);
  background: rgba(107, 42, 42, 0.25);
  border-color: var(--border);
}

/* ── Memory panel ── */

.memory-panel {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--border-subtle);
}

.project-panel {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--border-subtle);
}

.project-form {
  margin-top: 8px;
  padding-top: 10px;
  border-top: 1px solid var(--border-subtle);
}

.project-form input {
  width: 100%;
}

.active-project-card {
  display: grid;
  gap: 4px;
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  background: var(--surface);
  padding: 10px;
}

.active-project-card strong {
  font-size: 0.9rem;
}

.active-project-card span {
  color: var(--text-muted);
  font-size: 0.78rem;
  overflow-wrap: anywhere;
}

.memory-list article {
  max-width: none;
  padding: 10px;
}

.memory-list article p {
  display: -webkit-box;
  overflow: hidden;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 4;
  color: var(--text-message);
  font-size: 0.86rem;
}

.memory-list article button {
  width: 100%;
  min-height: 34px;
  margin-top: 8px;
  background: transparent;
}

.view-memories {
  margin: 8px 0 0;
}

/* ── Memories view ── */

.memories-page {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  height: 100%;
  overflow: hidden;
  gap: 14px;
}

.page-head,
.memory-group-head,
.memory-card-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.page-head p {
  color: var(--text-green);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
}

.page-head h2 {
  margin-top: 4px;
  font-size: 1.4rem;
}

.memory-groups {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  min-height: 0;
  overflow-y: auto;
  padding-right: 4px;
}

.memory-group {
  align-self: start;
  display: grid;
  gap: 10px;
  min-width: 0;
  border-top: 1px solid var(--border-subtle);
  padding-top: 10px;
}

.memory-group h3 {
  margin: 0;
  font-size: 0.96rem;
}

.memory-group-head span,
.memory-card-actions span {
  color: var(--text-green);
  font-size: 0.75rem;
  text-transform: uppercase;
}

.memory-card {
  max-width: none;
  width: 100%;
}

.memory-card p {
  color: var(--text-message);
  overflow-wrap: anywhere;
}

.memory-card-actions {
  margin-top: 10px;
}

.memory-card-actions button {
  min-height: 34px;
  background: transparent;
  color: #b78b83;
}

.memory-card-actions button:hover:not(:disabled) {
  color: var(--danger);
  background: rgba(107, 42, 42, 0.25);
}

/* ── Activity view ── */

.activity-page {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  height: 100%;
  overflow: hidden;
  gap: 14px;
}

.activity-list {
  display: grid;
  align-content: start;
  gap: 10px;
  min-height: 0;
  overflow-y: auto;
  padding-right: 4px;
}

.activity-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  max-width: none;
  width: 100%;
}

.activity-item span {
  margin-bottom: 4px;
}

.activity-item p {
  color: var(--text-message);
  overflow-wrap: anywhere;
}

.activity-item time {
  color: var(--text-muted);
  font-size: 0.78rem;
  white-space: nowrap;
}

/* ── Approvals view ── */

.approvals-page {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  height: 100%;
  overflow: hidden;
  gap: 14px;
}

.approval-list {
  display: grid;
  align-content: start;
  gap: 10px;
  min-height: 0;
  overflow-y: auto;
  padding-right: 4px;
}

.approval-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  max-width: none;
  width: 100%;
}

.approval-copy {
  min-width: 0;
}

.approval-copy span {
  margin-bottom: 5px;
}

.approval-copy h3 {
  margin: 0 0 6px;
  font-size: 1rem;
  overflow-wrap: anywhere;
}

.approval-copy p {
  color: var(--text-muted);
  overflow-wrap: anywhere;
}

.data-block {
  max-height: 220px;
  overflow: auto;
  margin: 8px 0 0;
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text-message);
  padding: 10px;
  font: 0.78rem/1.45 ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  white-space: pre;
}

.approval-actions {
  display: flex;
  gap: 8px;
}

.approval-actions-wrap,
.change-actions-wrap {
  display: grid;
  gap: 8px;
}

.danger-confirm-wrap {
  display: grid;
  gap: 6px;
  border: 1px solid var(--danger-border);
  border-radius: 8px;
  background: var(--danger-bg);
  padding: 10px;
}

.danger-confirm-wrap p {
  color: var(--danger);
  font-size: 0.82rem;
}

.danger-confirm {
  border-color: var(--danger-border);
}

.approval-actions button:first-child {
  background: transparent;
  color: var(--danger);
}

.approval-item time {
  color: var(--text-muted);
  font-size: 0.78rem;
  white-space: nowrap;
}

/* Changes view */

.changes-page {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  height: 100%;
  overflow: hidden;
  gap: 14px;
}

.changes-list {
  display: grid;
  align-content: start;
  gap: 12px;
  min-height: 0;
  overflow-y: auto;
  padding-right: 4px;
}

.change-item {
  display: grid;
  gap: 10px;
  max-width: none;
  width: 100%;
}

.change-head {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
}

.change-head span {
  margin-bottom: 5px;
}

.change-head h3 {
  margin: 0 0 6px;
  font-size: 1rem;
  overflow-wrap: anywhere;
}

.change-head p,
.change-meta {
  color: var(--text-muted);
  overflow-wrap: anywhere;
}

.change-meta.success {
  color: var(--text-green);
}

.change-meta.error {
  color: var(--danger);
}

.change-head time {
  color: var(--text-muted);
  font-size: 0.78rem;
  white-space: nowrap;
}

.change-diff {
  max-height: 340px;
  overflow: auto;
  margin: 0;
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text-message);
  padding: 12px;
  font: 0.82rem/1.45 ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  white-space: pre;
}

.change-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.change-actions button:first-child {
  background: transparent;
  color: var(--danger);
}

/* ── Chat panel ── */

.chat-panel {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto auto;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  gap: 10px;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  padding: 4px 0;
  min-height: 0;
  overflow-anchor: none;
}

.empty {
  color: var(--text-muted);
  padding: 40px 0;
  text-align: center;
}

article {
  align-self: flex-start;
  max-width: 88%;
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  background: var(--surface-mid);
  padding: 10px 12px;
}

article span {
  display: block;
  margin-bottom: 5px;
  color: var(--text-green);
  font-size: 0.75rem;
  text-transform: uppercase;
}

article p {
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.item-user {
  align-self: flex-end;
  background: var(--surface-chat);
}

.item-system {
  max-width: 100%;
  background: var(--surface);
}

/* ── Thinking dots ── */

.thinking p {
  display: flex;
  gap: 5px;
  min-height: 22px;
}

.stream-waiting {
  display: flex;
  gap: 5px;
  min-height: 22px;
}

.dot {
  width: 7px;
  height: 7px;
  border-radius: 999px;
  background: var(--text-green);
  animation: pulse 1s infinite ease-in-out;
}

.dot:nth-child(2) {
  animation-delay: 0.15s;
}

.dot:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes pulse {
  0%, 80%, 100% {
    opacity: 0.35;
    transform: translateY(0);
  }
  40% {
    opacity: 1;
    transform: translateY(-3px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .dot {
    animation: none;
    opacity: 0.7;
  }
  .sidebar {
    transition: none;
  }
  button,
  input,
  select,
  textarea {
    transition: none;
  }
}

/* ── Chat form ── */

.chat-form,
.modal-actions {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
}

.jump-latest {
  justify-self: center;
  min-height: 34px;
  padding: 0 12px;
  background: var(--surface-chat);
  border-color: var(--border-green);
  color: var(--text-user);
  font-size: 0.88rem;
}

.hidden {
  display: none;
}

/* ── Modal ── */

.modal-backdrop {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(5, 7, 8, 0.74);
  backdrop-filter: blur(8px);
}

.modal {
  display: grid;
  gap: 16px;
  width: min(100%, 360px);
  border: 1px solid #333c38;
  border-radius: 8px;
  background: var(--surface);
  padding: 18px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}

.modal h2 {
  margin-top: 4px;
  font-size: 1.15rem;
  overflow-wrap: anywhere;
}

.modal span {
  color: var(--text-dim);
  line-height: 1.5;
}

.danger {
  border-color: var(--danger-border);
  background: var(--danger-bg);
  color: var(--danger);
}

.danger:hover:not(:disabled) {
  background: #4a1f1f;
  border-color: #8a3a3a;
}

.approval-popout {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 30;
  width: min(460px, calc(100vw - 28px));
  pointer-events: none;
}

.approval-popout-panel {
  display: grid;
  gap: 12px;
  max-height: min(76vh, 620px);
  overflow: auto;
  border: 1px solid var(--border-green);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.42);
  padding: 14px;
  pointer-events: auto;
}

.approval-popout-head {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: start;
}

.approval-popout-head p {
  color: var(--text-green);
  font-size: 0.75rem;
  text-transform: uppercase;
}

.approval-popout-head h2 {
  margin-top: 3px;
  font-size: 1rem;
  overflow-wrap: anywhere;
}

.approval-popout-meta {
  display: grid;
  gap: 4px;
}

.approval-popout-meta p {
  color: var(--text-muted);
  font-size: 0.86rem;
  overflow-wrap: anywhere;
}

.approval-popout-diff {
  max-height: 240px;
  overflow: auto;
  margin: 0;
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text-message);
  padding: 10px;
  font: 0.78rem/1.45 ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  white-space: pre;
}

.approval-popout-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

/* ── Mobile ── */

@media (max-width: 700px) {
  main {
    max-width: 100%;
    padding: 10px;
    gap: 10px;
  }

  header {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    align-items: start;
    gap: 10px;
  }

  h1 {
    font-size: 1.1rem;
  }

  header p {
    max-width: 170px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.75rem;
  }

  .header-actions {
    display: grid;
    grid-template-columns: auto minmax(0, 96px) auto auto;
    width: 100%;
    gap: 8px;
  }

  .mode-select {
    width: 96px;
    min-height: 34px;
    font-size: 0.86rem;
  }

  .menu-button {
    display: inline-flex;
    align-items: center;
  }

  .header-actions button {
    min-height: 34px;
    padding: 0 10px;
  }

  .status-online {
    align-self: center;
    white-space: nowrap;
  }

  .chat-shell {
    grid-template-columns: 1fr;
    min-width: 0;
  }

  .sidebar {
    position: fixed;
    z-index: 20;
    top: 0;
    left: 0;
    width: min(86vw, 330px);
    height: 100vh;
    height: 100dvh;
    padding: 14px;
    border-right: 1px solid #2d3631;
    border-bottom: 0;
    background: var(--bg);
    box-shadow: 20px 0 60px rgba(0, 0, 0, 0.35);
    overflow-y: auto;
    transform: translateX(-105%);
    transition: transform 160ms ease;
  }

  .sidebar-open {
    transform: translateX(0);
  }

  .sidebar-backdrop {
    position: fixed;
    z-index: 10;
    inset: 0;
    display: block;
    min-height: 0;
    border: 0;
    border-radius: 0;
    background: rgba(5, 7, 8, 0.62);
    padding: 0;
  }

  .sidebar-backdrop:hover:not(:disabled) {
    background: rgba(5, 7, 8, 0.62);
    border-color: transparent;
  }

  .new-chat,
  .memory-toggle,
.project-toggle,
  .view-memories,
  .activity-button,
  .approvals-button,
  .changes-button {
    min-height: 38px;
    margin: 8px 0;
  }

  .conversation-list {
    display: grid;
    overflow-x: visible;
    padding-bottom: 0;
  }

  .conversation-item {
    min-width: 0;
  }

  .rename-chat,
  .delete-chat {
    padding: 0 8px;
    font-size: 0.72rem;
  }

  .memory-panel {
    margin-top: 10px;
    padding-top: 10px;
  }

  .memory-editor textarea {
    min-height: 84px;
  }

  article {
    max-width: 94%;
    padding: 9px 10px;
  }

  .item-system {
    max-width: 94%;
  }

  .memories-page {
    gap: 10px;
  }

  .page-head {
    align-items: flex-start;
  }

  .page-head h2 {
    font-size: 1.2rem;
  }

  .page-head button {
    min-height: 36px;
    padding: 0 10px;
    white-space: nowrap;
  }

  .memory-groups {
    grid-template-columns: 1fr;
    gap: 14px;
    padding-right: 0;
  }

  .memory-card {
    max-width: none;
  }

  .activity-page {
    gap: 10px;
  }

  .activity-list {
    padding-right: 0;
  }

  .activity-item {
    grid-template-columns: 1fr;
    align-items: start;
    gap: 8px;
    max-width: none;
  }

  .approvals-page {
    gap: 10px;
  }

  .approval-list {
    padding-right: 0;
  }

  .approval-item {
    grid-template-columns: 1fr;
    align-items: start;
    max-width: none;
  }

  .approval-actions {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .changes-page {
    gap: 10px;
  }

  .changes-list {
    padding-right: 0;
  }

  .change-item {
    max-width: none;
  }

  .change-head {
    grid-template-columns: 1fr;
  }

  .change-diff {
    max-height: 280px;
    font-size: 0.76rem;
  }

  .change-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .chat-form {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .chat-form button {
    width: 100%;
    min-height: 40px;
  }

  .modal-backdrop {
    padding: 10px;
  }
}
