:root {
  --bg-top: #d7dbd6;
  --bg-bottom: #ece5dd;
  --shell: #111b21;
  --panel: #f0f2f5;
  --panel-border: #d8dee3;
  --sidebar: #ffffff;
  --accent: #008069;
  --accent-strong: #075e54;
  --accent-soft: #25d366;
  --sent: #d9fdd3;
  --received: #ffffff;
  --system: #fff3c4;
  --danger: #d64545;
  --text: #111b21;
  --muted: #667781;
  --focus: #1f7aec;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  color: var(--text);
  font-family: "Segoe UI", "Noto Sans", Tahoma, sans-serif;
  background:
    linear-gradient(180deg, #0b141a 0, #0b141a 132px, var(--bg-top) 132px, var(--bg-bottom) 100%);
  overflow: hidden;
}

body.login-active {
  grid-template-rows: minmax(0, 1fr);
}

body.login-active .topbar,
body.login-active .layout,
body.login-active #image-modal,
body.login-active #import-modal,
body.login-active #app-toast,
body.login-active #app-live-region {
  visibility: hidden;
  pointer-events: none;
}

.login-gate {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: grid;
  place-items: center;
  padding: 24px;
  background: color-mix(in srgb, var(--shell) 82%, transparent);
  backdrop-filter: blur(6px);
}

.login-gate[hidden] {
  display: none;
}

.login-gate-inner {
  width: min(100%, 380px);
  padding: 28px 26px;
  border-radius: 16px;
  background: var(--sidebar);
  border: 1px solid var(--panel-border);
  box-shadow: 0 18px 42px rgb(0 0 0 / 0.22);
}

.login-gate .eyebrow {
  margin: 0 0 6px;
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

.login-title {
  margin: 0 0 8px;
  font-size: 1.45rem;
  color: var(--shell);
}

.login-hint {
  margin: 0 0 22px;
  font-size: 0.95rem;
  color: var(--muted);
  line-height: 1.45;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.login-label {
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--shell);
}

.login-label input {
  padding: 11px 12px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  color: var(--text);
}

.login-submit {
  padding: 12px 16px;
  border: none;
  border-radius: 10px;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  cursor: pointer;
}

.login-submit:hover {
  background: var(--accent-strong);
}

.login-error {
  margin: 0;
  font-size: 0.88rem;
  color: var(--danger);
}

button,
input {
  font: inherit;
}

button:focus-visible,
input:focus-visible,
.chat-item:focus-visible,
.image-thumb:focus-visible,
.media-link:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--focus), white 30%);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  width: min(100%, 1480px);
  margin: 24px auto 0;
  padding: 12px 18px;
  color: #e9edef;
  background: var(--accent-strong);
  border-bottom: 1px solid color-mix(in srgb, var(--accent-strong), black 18%);
  border-radius: 14px 14px 0 0;
}

.brand h1 {
  margin: 0;
  font-size: clamp(1.15rem, 1rem + 0.45vw, 1.45rem);
  letter-spacing: 0;
}

.eyebrow {
  margin: 0;
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  opacity: 0.72;
}

.import-form {
  display: flex;
  align-items: center;
  gap: 8px;
}

.file-picker {
  position: relative;
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  border: 1px solid rgba(255, 255, 255, 0.42);
  border-radius: 999px;
  padding: 0 15px;
  background: rgba(255, 255, 255, 0.1);
  cursor: pointer;
}

.file-picker span {
  max-width: 190px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-picker input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 380px) 1fr;
  min-height: 0;
  width: min(100%, 1480px);
  margin: 0 auto 24px;
  min-height: min(78vh, calc(100vh - 112px));
  background: #e9edef;
  border-radius: 0 0 14px 14px;
  box-shadow: 0 6px 24px rgba(17, 27, 33, 0.18);
  overflow: hidden;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  padding: 10px 10px 0;
  background: var(--panel);
  border-right: 1px solid #d1d7db;
  overflow: hidden;
}

.progress {
  height: 7px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--panel-border), white 50%);
  overflow: hidden;
}

.progress-bar {
  width: 100%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #3ac97c, #15a36e);
  transform: scaleX(0);
  transform-origin: left center;
  transition: transform 180ms ease;
  will-change: transform;
}

.search-wrap {
  position: relative;
  display: block;
}

.search-wrap::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 14px;
  width: 16px;
  height: 16px;
  border: 2px solid #8696a0;
  border-radius: 50%;
  transform: translateY(-50%);
  opacity: 0.8;
}

.search-wrap::after {
  content: "";
  position: absolute;
  top: calc(50% + 5px);
  left: 27px;
  width: 7px;
  height: 2px;
  background: #8696a0;
  border-radius: 999px;
  transform: rotate(45deg);
}

.search {
  width: 100%;
  border: 0;
  border-radius: 9px;
  background: #ffffff;
  padding: 10px 14px 10px 42px;
  color: var(--text);
}

.chat-list {
  min-height: 0;
  overflow-y: auto;
  padding-right: 2px;
  background: #ffffff;
}

.chat-row {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 6px;
}

.chat-item {
  width: 100%;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 12px;
  border: 0;
  border-bottom: 1px solid #e9edef;
  border-radius: 0;
  background: transparent;
  padding: 11px 6px 11px 4px;
  text-align: left;
  cursor: pointer;
  transition: background 130ms ease;
}

.chat-item:hover {
  background: #f6f7f8;
}

.chat-item.active {
  background: #f0f2f5;
}

.chat-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 49px;
  height: 49px;
  border-radius: 50%;
  background: linear-gradient(180deg, #dfe5e7, #ccd4d9);
  color: #54656f;
  font-size: 0.86rem;
  font-weight: 700;
}

.chat-item-body {
  min-width: 0;
}

.chat-item-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.chat-item-trailing {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex: none;
}

.chat-pin {
  flex: none;
  width: 9px;
  height: 9px;
  border-radius: 999px;
  background: #25d366;
}

.chat-item-title {
  margin: 0;
  min-width: 0;
  font-size: 1rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-date {
  color: var(--muted);
  font-size: 0.72rem;
}

.chat-item.active .chat-date {
  color: var(--accent);
}

.chat-item.active .chat-pin {
  background: var(--accent-soft);
}

.chat-preview {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.35;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-meta {
  margin: 2px 0 0;
  font-size: 0.7rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-menu-wrap {
  position: relative;
}

.chat-menu-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  min-width: 32px;
  height: 32px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.chat-menu-button:hover,
.chat-menu-button[aria-expanded="true"] {
  color: var(--accent-strong);
  background: #f0f2f5;
}

.chat-menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 20;
  display: grid;
  gap: 2px;
  min-width: 108px;
  padding: 4px;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: rgba(250, 253, 252, 0.98);
  box-shadow: 0 12px 28px rgba(12, 35, 30, 0.15);
}

.chat-menu[hidden] {
  display: none;
}

.chat-menu-item {
  border: 0;
  border-radius: 8px;
  padding: 7px 9px;
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.chat-menu-item:hover {
  background: color-mix(in srgb, var(--panel), var(--sent) 45%);
}

.chat-menu-item.danger-text {
  color: var(--danger);
}

.chat-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: #efeae2;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid #d1d7db;
  background: var(--panel);
}

.mobile-back-button {
  display: none;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: inherit;
  cursor: pointer;
  flex: none;
}

.mobile-back-button span {
  font-size: 1.75rem;
  line-height: 1;
  transform: translateY(-1px);
}

.chat-header-copy {
  min-width: 0;
}

.chat-header-actions {
  flex: none;
}

.chat-subtitle {
  margin: 0;
  font-size: 0.7rem;
  color: var(--muted);
  letter-spacing: 0.01em;
}

.chat-title {
  margin: 2px 0 0;
  font-size: 1.02rem;
  font-weight: 500;
}

.danger {
  border: 0;
  color: var(--danger);
  background: transparent;
  border-radius: 999px;
  min-height: 36px;
  padding: 0 8px;
  font-weight: 600;
  cursor: pointer;
}

.danger:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.message-surface {
  position: relative;
  min-height: 0;
  flex: 1;
  background: #e5ddd5;
}

.message-list {
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow-y: auto;
  padding: 12px 14px 74px;
  background-color: #efeae2;
  background:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='240' viewBox='0 0 240 240'%3E%3Cg fill='none' stroke='%23d3dbd4' stroke-width='1.4' stroke-linecap='round' stroke-linejoin='round' opacity='.55'%3E%3Cpath d='M26 44c8-7 19-6 26 2 7 8 6 19-2 26-8 7-19 6-26-2-7-8-6-19 2-26Z'/%3E%3Cpath d='M173 29c15 3 22 15 18 28-3 12-17 18-31 15-15-3-22-15-18-27 3-13 17-19 31-16Z'/%3E%3Cpath d='M66 135c10-10 25-10 35-1 10 10 10 25 1 35-10 10-25 10-35 1-10-10-10-25-1-35Z'/%3E%3Cpath d='M157 144c12-9 27-7 35 4 9 12 7 27-5 36-11 8-27 7-35-5-8-11-6-26 5-35Z'/%3E%3Cpath d='M30 193c14-4 27 2 31 14 4 13-3 25-17 29-13 3-26-3-30-15-4-12 3-25 16-28Z'/%3E%3Cpath d='M112 37h19m-9-9v18M194 104h18m-9-9v18M99 211c8-8 20-8 28 0M124 92c3 7 10 11 18 11 8 0 15-4 18-11M42 112c4 6 11 10 18 10 8 0 15-4 19-10'/%3E%3Cpath d='M196 191c0-8 6-14 14-14s14 6 14 14-6 14-14 14-14-6-14-14Z'/%3E%3C/g%3E%3C/svg%3E"),
    linear-gradient(180deg, rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0.08));
  background-size: 240px 240px, auto;
}

.message {
  position: relative;
  max-width: min(74%, 660px);
  border-radius: 7.5px;
  box-shadow: 0 1px 0 rgba(17, 27, 33, 0.08);
  padding: 6px 8px 5px;
  content-visibility: auto;
  contain-intrinsic-size: 84px;
}

.message.them {
  align-self: flex-start;
  background: var(--received);
  border-top-left-radius: 0;
}

.message.me {
  align-self: flex-end;
  background: var(--sent);
  border-top-right-radius: 0;
}

.message.them::before,
.message.me::before {
  content: "";
  position: absolute;
  top: 0;
  width: 10px;
  height: 12px;
  background: inherit;
}

.message.them::before {
  left: -6px;
  clip-path: polygon(100% 0, 100% 100%, 0 0);
}

.message.me::before {
  right: -6px;
  clip-path: polygon(0 0, 100% 0, 0 100%);
}

.message.system {
  align-self: center;
  max-width: min(92%, 320px);
  text-align: center;
  background: var(--system);
  color: #564700;
  border-radius: 7px;
}

.sender {
  margin: 0 0 2px;
  color: #096555;
  font-size: 0.72rem;
  font-weight: 700;
}

.content {
  margin: 0;
  line-height: 1.38;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

.image-thumb {
  margin-top: 6px;
  padding: 0;
  border: none;
  border-radius: 8px;
  background: transparent;
  cursor: zoom-in;
}

.image-thumb img {
  display: block;
  max-width: min(260px, 100%);
  max-height: 240px;
  border-radius: 8px;
}

.media-link {
  display: inline-block;
  margin-top: 6px;
  color: #0b5fb9;
}

.timestamp {
  margin-top: 4px;
  display: block;
  text-align: right;
  color: var(--muted);
  font-size: 0.66rem;
}

.scroll-hud {
  position: absolute;
  right: 12px;
  bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid rgba(17, 27, 33, 0.08);
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 8px 18px rgba(17, 27, 33, 0.12);
  backdrop-filter: blur(8px);
}

.scroll-meter {
  width: 72px;
  height: 6px;
  border-radius: 999px;
  background: #dfe5e7;
  overflow: hidden;
}

.scroll-fill {
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, #25d366, #128c7e);
  transform: scaleX(0);
  transform-origin: left center;
  will-change: transform;
}

.scroll-label {
  min-width: 38px;
  color: var(--muted);
  font-size: 0.72rem;
}

#jump-latest {
  border: 1px solid rgba(18, 140, 126, 0.35);
  border-radius: 999px;
  min-height: 30px;
  padding: 0 10px;
  background: #fff;
  color: #128c7e;
  font-weight: 600;
  cursor: pointer;
}

.image-modal {
  position: fixed;
  inset: 0;
  z-index: 120;
}

.import-modal {
  position: fixed;
  inset: 0;
  z-index: 140;
}

.import-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(7, 25, 22, 0.24);
  backdrop-filter: blur(2px);
}

.import-modal-panel {
  position: relative;
  width: min(92vw, 420px);
  margin: 18vh auto 0;
  padding: 18px;
  border: 1px solid color-mix(in srgb, var(--panel-border), white 10%);
  border-radius: 16px;
  background: rgba(250, 253, 252, 0.96);
  box-shadow: 0 24px 60px rgba(12, 35, 30, 0.18);
}

.import-modal-eyebrow {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.12em;
}

.import-modal-panel h2 {
  margin: 0;
  font-size: 1.1rem;
}

.import-modal-message {
  margin: 8px 0 12px;
  color: var(--muted);
  line-height: 1.35;
}

.import-modal-progress {
  height: 10px;
}

.import-modal-meta {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-top: 10px;
}

.import-modal-meta span {
  color: var(--muted);
}

.app-toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 30;
  max-width: min(320px, calc(100vw - 24px));
  padding: 10px 14px;
  border: 1px solid color-mix(in srgb, var(--accent), white 60%);
  border-radius: 14px;
  background: rgba(11, 76, 67, 0.94);
  color: #f4fffc;
  box-shadow: 0 18px 40px rgba(12, 35, 30, 0.22);
  backdrop-filter: blur(8px);
}

.image-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(2, 12, 10, 0.78);
}

.image-modal-panel {
  position: relative;
  margin: min(5vh, 28px) auto;
  width: min(92vw, 1080px);
  max-height: 90vh;
  background: #fbfffd;
  border-radius: 12px;
  border: 1px solid var(--panel-border);
  padding: 12px 12px 8px;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 10px;
}

.close-modal {
  justify-self: end;
  border: 1px solid #c4d4d0;
  border-radius: 8px;
  padding: 7px 10px;
  background: white;
  cursor: pointer;
}

.image-modal-panel img {
  width: 100%;
  height: auto;
  max-height: min(74vh, 900px);
  object-fit: contain;
  border-radius: 10px;
  background: #dde8e5;
}

.image-modal-panel figcaption {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.chat-list::-webkit-scrollbar,
.message-list::-webkit-scrollbar {
  width: 9px;
}

.chat-list::-webkit-scrollbar-thumb,
.message-list::-webkit-scrollbar-thumb {
  background: #c6d1d5;
  border-radius: 999px;
}

@media (max-width: 980px) {
  .topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    flex-wrap: nowrap;
    width: 100%;
    margin: 0;
    padding: 10px 14px;
    border-radius: 0;
  }

  .brand h1 {
    font-size: 1.55rem;
  }

  .eyebrow {
    font-size: 0.62rem;
  }

  .file-picker {
    min-height: 34px;
    padding: 0 12px;
    font-size: 0.9rem;
  }

  .layout {
    position: relative;
    grid-template-columns: 1fr;
    width: 100%;
    margin: 0;
    min-height: 0;
    height: 100%;
    border-radius: 0;
    box-shadow: none;
  }

  .sidebar {
    position: relative;
    z-index: 1;
    border-right: 0;
    max-height: none;
    padding: 8px 10px 0;
    transition: transform 180ms ease, opacity 180ms ease;
  }

  .chat-item {
    padding-right: 0;
  }

  .chat-menu-button {
    margin-top: 15px;
  }

  .chat-header {
    position: sticky;
    top: 0;
    z-index: 5;
    padding: 9px 12px;
    min-height: calc(58px + env(safe-area-inset-top, 0px));
    background: var(--accent-strong);
    border-bottom-color: rgba(0, 0, 0, 0.08);
  }

  .mobile-back-button {
    display: inline-flex;
  }

  .chat-subtitle,
  .chat-title {
    color: #e9edef;
  }

  .chat-subtitle {
    opacity: 0.84;
  }

  .danger {
    color: #ffffff;
    background: rgba(255, 255, 255, 0.14);
    padding: 0 12px;
  }

  .message-list {
    min-height: 0;
    padding: 12px 10px 76px;
    background-size: 180px 180px, auto;
  }

  .message {
    max-width: 88%;
  }

  .scroll-hud {
    right: 10px;
    bottom: 10px;
  }

  .app-toast {
    right: 12px;
    bottom: 12px;
    left: 12px;
    max-width: none;
  }

  .image-modal-panel {
    width: calc(100vw - 16px);
  }

  .chat-pane {
    position: absolute;
    inset: 0;
    z-index: 2;
    transform: translateX(100%);
    opacity: 0;
    pointer-events: none;
    transition: transform 180ms ease, opacity 180ms ease;
  }

  body.mobile-list-view {
    grid-template-rows: auto minmax(0, 1fr);
  }

  body.mobile-chat-view {
    grid-template-rows: minmax(0, 1fr);
  }

  body.mobile-chat-view .topbar {
    display: none;
  }

  body.mobile-list-view .chat-pane {
    transform: translateX(100%);
    opacity: 0;
    pointer-events: none;
  }

  body.mobile-chat-view .sidebar {
    transform: translateX(-8%);
    opacity: 0;
    pointer-events: none;
  }

  body.mobile-chat-view .chat-pane {
    transform: translateX(0);
    opacity: 1;
    pointer-events: auto;
  }
}

@media (max-width: 620px) {
  body {
    background: var(--shell);
  }

  .topbar {
    padding-top: calc(10px + env(safe-area-inset-top, 0px));
  }

  .chat-header {
    padding-top: calc(9px + env(safe-area-inset-top, 0px));
  }

  .chat-title {
    font-size: 0.98rem;
  }

  .chat-preview {
    font-size: 0.79rem;
  }

  .message-list {
    padding-bottom: calc(80px + env(safe-area-inset-bottom, 0px));
  }

  .scroll-hud {
    bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
