:root {
  color-scheme: light dark;
  --bg: #fbfbfa;
  --surface: #ffffff;
  --text: #111111;
  --muted: #8a8a8a;
  --line: #ececec;
  --line-strong: #d8d8d8;
  --soft: #f3f3f2;
  --accent: #111111;
  --accent-text: #ffffff;
  --bubble-them: #f1f1ef;
  --danger: #b13434;
  --ok: #2f7a4a;
  --tap: 44px;
  --sidebar: 320px;
  --radius: 10px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.06);
  --ease: cubic-bezier(0.2, 0, 0, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0d0e;
    --surface: #131315;
    --text: #f2f2f1;
    --muted: #8f8f8f;
    --line: #232325;
    --line-strong: #2c2c30;
    --soft: #18181a;
    --accent: #f2f2f1;
    --accent-text: #0d0d0e;
    --bubble-them: #1a1a1c;
    --danger: #ff7a7a;
    --ok: #6dd49a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 12px 32px rgba(0, 0, 0, 0.45);
  }
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", system-ui, ui-sans-serif, sans-serif;
  letter-spacing: -0.005em;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-tap-highlight-color: transparent;
}

button,
input,
textarea {
  font: inherit;
  letter-spacing: inherit;
}

input,
textarea {
  font-size: 16px;
}

button {
  cursor: pointer;
  touch-action: manipulation;
}

::selection {
  background: var(--accent);
  color: var(--accent-text);
}

.shell {
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
}

/* Auth */
.auth {
  width: min(380px, calc(100vw - 32px));
  margin: 0 auto;
  padding: max(72px, 12vh) 0 32px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 36px;
}

.mark {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  font-size: 13px;
  letter-spacing: 0;
}

h1 {
  margin: 0;
  font-size: 26px;
  line-height: 1.1;
  font-weight: 600;
  letter-spacing: -0.02em;
}

h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 4px;
  margin-bottom: 20px;
  background: var(--soft);
  border-radius: var(--radius);
}

.tab {
  min-height: 36px;
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 6px;
  font-weight: 500;
  transition: background 0.15s var(--ease), color 0.15s var(--ease);
}

.tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

@media (prefers-color-scheme: dark) {
  .tab.active {
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}

.form {
  display: grid;
  gap: 14px;
}

label {
  display: grid;
  gap: 6px;
}

label span,
.muted,
.listTitle {
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--text);
  outline: none;
  border-radius: var(--radius-sm);
  appearance: none;
  transition: border-color 0.15s var(--ease), box-shadow 0.15s var(--ease);
}

input {
  height: var(--tap);
  padding: 0 12px;
}

textarea {
  min-height: var(--tap);
  max-height: 168px;
  padding: 11px 12px;
  resize: none;
  line-height: 1.4;
}

input:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 15%, transparent);
}

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

.primary,
.ghost,
.iconButton,
.backButton {
  min-height: var(--tap);
  border: 1px solid transparent;
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  font-weight: 500;
  transition: background 0.15s var(--ease), border-color 0.15s var(--ease), opacity 0.15s var(--ease);
}

.primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
  padding: 0 18px;
}

.primary:hover:not(:disabled) {
  opacity: 0.88;
}

.primary:disabled,
textarea:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.ghost {
  border-color: var(--line-strong);
  padding: 0 14px;
  color: var(--muted);
  background: transparent;
  min-height: 34px;
  border-radius: 7px;
  font-size: 13px;
}

.ghost:hover {
  color: var(--text);
  background: var(--soft);
}

.iconButton,
.backButton {
  display: grid;
  place-items: center;
  width: var(--tap);
  height: var(--tap);
  min-height: 0;
  padding: 0;
  font-size: 20px;
  border-color: transparent;
  color: var(--muted);
}

.iconButton:hover,
.backButton:hover {
  background: var(--soft);
  color: var(--text);
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.error,
.ok {
  min-height: 18px;
  margin: 0;
  font-size: 13px;
}

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

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

/* App */
.app {
  display: grid;
  grid-template-columns: var(--sidebar) minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  min-height: 600px;
  overflow: hidden;
}

.sidebar {
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr);
  min-width: 0;
  border-right: 1px solid var(--line);
  background: var(--surface);
}

.topbar,
.conversationHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 64px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  gap: 12px;
}

.topbarMe {
  min-width: 0;
}

.topbarActions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.appName {
  margin-bottom: 4px;
  font-weight: 600;
  font-size: 13px;
  color: var(--muted);
  letter-spacing: 0.01em;
}

#meLabel,
#chatTitle {
  display: block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.search {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--tap);
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}

.search input {
  border-color: var(--line);
  background: var(--soft);
}

.search input:focus {
  background: var(--surface);
}

.search .iconButton {
  border-color: var(--line);
}

.listBlock {
  min-height: 0;
  padding: 14px 8px 8px;
  border-bottom: 1px solid var(--line);
}

.listBlock.grow {
  overflow: auto;
  border-bottom: 0;
  -webkit-overflow-scrolling: touch;
}

.listTitle {
  padding: 0 10px 6px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-size: 11px;
}

.list {
  display: grid;
  gap: 1px;
}

.list.empty,
.emptyState {
  color: var(--muted);
}

.list.empty {
  padding: 8px 10px;
  font-size: 13px;
}

.userButton {
  display: grid;
  width: 100%;
  min-height: 54px;
  gap: 2px;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 9px 10px;
  text-align: left;
  border-radius: var(--radius-sm);
  transition: background 0.12s var(--ease);
}

.userButton:hover {
  background: var(--soft);
}

.userButton.active {
  background: var(--soft);
}

.userButton strong {
  font-weight: 600;
  letter-spacing: -0.005em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preview {
  color: var(--muted);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
  background: var(--bg);
}

.conversationHeader {
  background: var(--surface);
}

.conversationTitle {
  min-width: 0;
}

.conversationTitle .muted {
  margin-bottom: 2px;
}

.backButton {
  display: none;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 0;
  overflow: auto;
  padding: 20px 18px;
  -webkit-overflow-scrolling: touch;
}

.emptyState {
  width: min(380px, 100%);
  margin: auto;
  padding: 18px;
  text-align: center;
  font-size: 13px;
}

.bubble {
  max-width: min(640px, 76%);
  padding: 9px 13px;
  border-radius: 16px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.4;
  font-size: 14.5px;
  animation: bubbleIn 0.2s var(--ease);
}

@keyframes bubbleIn {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: none; }
}

.bubble.me {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-text);
  border-bottom-right-radius: 5px;
}

.bubble.them {
  align-self: flex-start;
  background: var(--bubble-them);
  border-bottom-left-radius: 5px;
}

.bubble + .bubble.me,
.bubble + .bubble.them {
  margin-top: 2px;
}

.meta {
  margin-top: 4px;
  color: var(--muted);
  font-size: 11px;
}

.bubble.me .meta {
  color: color-mix(in srgb, var(--accent-text) 65%, transparent);
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  padding: 12px 14px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.composer textarea {
  border-color: var(--line);
  background: var(--soft);
}

.composer textarea:focus {
  background: var(--surface);
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
}

.modalBackdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  animation: fadeIn 0.15s var(--ease);
}

.modalCard {
  position: relative;
  width: min(380px, 100%);
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  padding: 18px;
  animation: cardIn 0.18s var(--ease);
}

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes cardIn {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to   { opacity: 1; transform: none; }
}

.modalHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.modalCard .iconButton {
  font-size: 22px;
  width: 32px;
  height: 32px;
}

/* Responsive */
@media (max-width: 900px) {
  :root {
    --sidebar: 300px;
  }
}

@media (max-width: 760px) {
  :root {
    --tap: 46px;
  }

  body {
    overflow: hidden;
  }

  .shell,
  .app {
    height: 100vh;
    height: 100dvh;
    min-height: 0;
  }

  .auth {
    width: min(400px, calc(100vw - 28px));
    min-height: 100vh;
    min-height: 100dvh;
    padding: max(28px, env(safe-area-inset-top)) 0 max(24px, env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .brand {
    margin-bottom: 28px;
  }

  .app {
    display: block;
    overflow: hidden;
  }

  .sidebar,
  .conversation {
    height: 100%;
    min-height: 0;
  }

  .sidebar {
    grid-template-rows: auto auto auto minmax(0, 1fr);
    border-right: 0;
  }

  .topbar,
  .conversationHeader {
    min-height: calc(60px + env(safe-area-inset-top));
    padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
  }

  .conversation {
    display: none;
  }

  .app.conversationOpen .sidebar {
    display: none;
  }

  .app.conversationOpen .conversation {
    display: grid;
  }

  .conversationHeader {
    display: grid;
    grid-template-columns: var(--tap) minmax(0, 1fr);
    justify-content: start;
    gap: 8px;
  }

  .backButton {
    display: grid;
  }

  .messages {
    padding: 14px 12px;
  }

  .bubble {
    max-width: 84%;
  }

  .composer {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  }

  .composer .primary {
    min-width: 88px;
    padding: 0 14px;
  }
}

@media (max-width: 380px) {
  .topbarActions .ghost:first-child {
    display: none;
  }
}
