:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --text: #0f172a;
  --text-soft: #475569;
  --line: #cbd5e1;
  --line-soft: rgba(71, 85, 105, 0.35);
  --accent: #0f766e;
  --accent-strong: #0d9488;
  --danger: #b91c1c;
  --bubble-in: #ffffff;
  --bubble-out: #0f766e;
  --grad-top: #dbeafe;
  --grad-bottom: #ccfbf1;
  --hover: #f8fafc;
  --active-chat: #ecfeff;
  --header-bg: #f8fafc;
  --field-bg: #ffffff;
  --shadow: rgba(15, 23, 42, 0.12);
  --bubble-shadow: rgba(15, 23, 42, 0.06);
  --sidebar-shadow: rgba(15, 23, 42, 0.2);
  --backdrop: rgba(15, 23, 42, 0.45);
}

[data-theme="dark"] {
  --bg: #0b1220;
  --surface: #111a2c;
  --text: #e2e8f0;
  --text-soft: #94a3b8;
  --line: #22324b;
  --line-soft: rgba(148, 163, 184, 0.45);
  --accent: #14b8a6;
  --accent-strong: #0ea5a4;
  --danger: #f87171;
  --bubble-in: #0f1a2b;
  --bubble-out: #0f766e;
  --grad-top: #17233a;
  --grad-bottom: #102536;
  --hover: #18263b;
  --active-chat: #12333f;
  --header-bg: #101a2b;
  --field-bg: #0f1a2b;
  --shadow: rgba(2, 6, 23, 0.45);
  --bubble-shadow: rgba(2, 6, 23, 0.5);
  --sidebar-shadow: rgba(2, 6, 23, 0.55);
  --backdrop: rgba(2, 6, 23, 0.65);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(circle at top left, var(--grad-top) 0%, transparent 32%),
    radial-gradient(circle at bottom right, var(--grad-bottom) 0%, transparent 30%),
    var(--bg);
  font-family: "Segoe UI", "Noto Sans", sans-serif;
}

.app-page {
  height: 100dvh;
  overflow: hidden;
}

body.modal-open {
  overflow: hidden;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(260px, 320px) 1fr;
  height: 100%;
  min-height: 0;
}

.sidebar {
  border-right: 1px solid var(--line);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.sidebar-backdrop {
  display: none;
}

.sidebar-header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 14px;
  border-bottom: 1px solid var(--line);
}

.sidebar-header h1 {
  margin: 0;
  font-size: 1rem;
}

.sidebar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.theme-switch {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-soft);
  font-size: 0.82rem;
  user-select: none;
}

.theme-switch input {
  appearance: none;
  width: 44px;
  height: 24px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--line);
  position: relative;
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease;
}

.theme-switch input::after {
  content: "";
  position: absolute;
  left: 2px;
  top: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--surface);
  box-shadow: 0 2px 4px var(--shadow);
  transition: transform 0.2s ease;
}

.theme-switch input:checked {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}

.theme-switch input:checked::after {
  transform: translateX(20px);
}

.theme-switch-label {
  font-weight: 600;
}

.add-chat-btn {
  border: 0;
  border-radius: 10px;
  min-width: 32px;
  height: 32px;
  background: var(--accent);
  color: #fff;
  font-size: 1.2rem;
  cursor: pointer;
}

.search-wrap {
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.search-wrap input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  font: inherit;
  color: var(--text);
  background: var(--field-bg);
}

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

.chat-item {
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--line);
  padding: 10px 14px;
  cursor: pointer;
  color: inherit;
  font: inherit;
}

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

.chat-item.active {
  border-left: 4px solid var(--accent);
  background: var(--active-chat);
  padding-left: 10px;
}

.chat-name {
  font-weight: 600;
}

.chat-meta {
  margin-top: 4px;
  color: var(--text-soft);
  font-size: 0.82rem;
}

.sidebar-footer {
  margin-top: auto;
  padding: 12px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.logout-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--field-bg);
  color: var(--text);
  text-decoration: none;
  font-size: 0.92rem;
  font-weight: 600;
  padding: 10px 12px;
}

.logout-btn:hover {
  background: var(--hover);
}

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

.main-header {
  border-bottom: 1px solid var(--line);
  background: var(--header-bg);
  padding: 14px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.main-header h2 {
  margin: 0;
  font-size: 1.1rem;
}

.main-header p {
  margin: 3px 0 0;
  font-size: 0.82rem;
  color: var(--text-soft);
}

.toggle-sidebar-btn {
  display: none;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  padding: 7px 10px;
  font: inherit;
  cursor: pointer;
}

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

.message-bubble {
  position: relative;
  max-width: min(78%, 700px);
  padding: 10px 40px 10px 12px;
  border-radius: 14px;
  line-height: 1.4;
  word-break: break-word;
  white-space: pre-wrap;
  background: var(--bubble-in);
  border: 1px solid var(--line);
  box-shadow: 0 6px 16px var(--bubble-shadow);
}

.message-bubble.out {
  margin-left: auto;
  background: var(--bubble-out);
  color: #fff;
  border-color: transparent;
}

.message-delete-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 26px;
  height: 26px;
  border: 0;
  border-radius: 8px;
  background: rgba(15, 23, 42, 0.68);
  color: #fff;
  font-size: 1rem;
  line-height: 1;
  display: grid;
  place-items: center;
  cursor: pointer;
  opacity: 0;
  transform: translateY(-3px);
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease, background 0.18s ease;
}

.message-bubble:hover .message-delete-btn,
.message-bubble:focus-within .message-delete-btn {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.message-delete-btn:hover {
  background: var(--danger);
}

.message-bubble.out .message-delete-btn {
  background: rgba(2, 6, 23, 0.5);
}

@media (hover: none) {
  .message-delete-btn {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
  }
}

.message-media {
  display: block;
  max-width: 100%;
  margin-top: 8px;
  border-radius: 10px;
}

.file-link {
  display: inline-block;
  margin-top: 8px;
  color: inherit;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  padding: 5px 8px;
  text-decoration: none;
}

.message-time {
  display: block;
  margin-top: 6px;
  font-size: 0.75rem;
  opacity: 0.75;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: var(--backdrop);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 40;
  padding: 16px;
}

.modal-backdrop.open {
  display: flex;
}

.modal-card {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 22px 44px var(--shadow);
  padding: 18px;
}

.modal-card h3 {
  margin: 0 0 8px;
  font-size: 1.05rem;
}

.modal-card p {
  margin: 0;
  color: var(--text-soft);
}

.modal-error {
  margin-top: 10px;
  min-height: 1.1rem;
  color: var(--danger);
  font-size: 0.86rem;
}

.modal-actions {
  margin-top: 14px;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.modal-actions button {
  border: 0;
  border-radius: 10px;
  height: 36px;
  padding: 0 14px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.btn-secondary {
  background: var(--hover);
  color: var(--text);
  border: 1px solid var(--line);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.modal-actions button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.composer {
  border-top: 1px solid var(--line);
  background: var(--surface);
  padding: 12px;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 10px;
}

.composer textarea {
  width: 100%;
  min-height: 44px;
  max-height: 220px;
  resize: none;
  border-radius: 12px;
  border: 1px solid var(--line);
  padding: 10px 12px;
  font: inherit;
  line-height: 1.35;
  color: var(--text);
  background: var(--field-bg);
}

.composer button {
  border: 0;
  border-radius: 12px;
  background: var(--accent-strong);
  color: #fff;
  height: 44px;
  padding: 10px 16px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.composer button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.login-page {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 20px;
  position: relative;
}

.login-theme-toggle {
  position: absolute;
  top: 16px;
  right: 16px;
}

.login-card {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 20px 40px var(--shadow);
}

.login-card h1 {
  margin-top: 0;
  margin-bottom: 18px;
}

.login-form {
  display: grid;
  gap: 8px;
}

.login-form input {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  font: inherit;
  color: var(--text);
  background: var(--field-bg);
}

.login-form button {
  margin-top: 8px;
  border: 0;
  border-radius: 10px;
  background: var(--accent-strong);
  color: #fff;
  padding: 10px;
  font: inherit;
  cursor: pointer;
}

.error {
  min-height: 1.2rem;
  color: var(--danger);
  margin: 6px 0 0;
}

@media (max-width: 960px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .toggle-sidebar-btn {
    display: inline-block;
  }

  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: min(88vw, 360px);
    transform: translateX(-102%);
    transition: transform 0.2s ease;
    z-index: 11;
    box-shadow: 10px 0 30px var(--sidebar-shadow);
  }

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

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: var(--backdrop);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
    z-index: 10;
  }

  .sidebar-backdrop.open {
    opacity: 1;
    pointer-events: auto;
  }

  .main {
    min-height: 100dvh;
  }

  .message-bubble {
    max-width: 90%;
  }
}
