:root {
  color-scheme: light;
  --bg: #f6f7f4;
  --panel: #ffffff;
  --panel-2: #eef3ef;
  --ink: #18201d;
  --muted: #68736d;
  --line: #dfe5df;
  --accent: #1f7a5c;
  --accent-2: #0f5f88;
  --danger: #b42318;
  --shadow: 0 16px 40px rgba(24, 32, 29, 0.12);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101413;
  --panel: #171d1b;
  --panel-2: #202926;
  --ink: #eef5f1;
  --muted: #a8b5ae;
  --line: #2d3934;
  --accent: #39a77e;
  --accent-2: #55a6d8;
  --danger: #ff8b7f;
  --shadow: 0 16px 40px rgba(0, 0, 0, 0.34);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--ink);
}

button,
input,
textarea {
  font: inherit;
}

button {
  border: 0;
  background: var(--accent);
  color: white;
  cursor: pointer;
  border-radius: 8px;
  min-height: 42px;
  padding: 0 16px;
  font-weight: 700;
}

button:disabled {
  opacity: 0.55;
  cursor: wait;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  padding: 12px 13px;
  outline: none;
}

:root[data-theme="dark"] input,
:root[data-theme="dark"] textarea {
  background: #111715;
}

textarea {
  min-height: 42px;
  max-height: 130px;
  resize: none;
}

label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.hidden {
  display: none !important;
}

.auth {
  height: 100vh;
  overflow: auto;
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(135deg, rgba(31, 122, 92, 0.12), transparent 42%),
    linear-gradient(315deg, rgba(15, 95, 136, 0.14), transparent 38%),
    var(--bg);
}

.auth-panel {
  width: min(440px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 28px;
}

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

.brand-mark {
  width: 54px;
  height: 54px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
  color: white;
  font-weight: 900;
  font-size: 28px;
}

h1,
h2,
p {
  margin: 0;
}

.brand h1,
.admin h1 {
  font-size: 28px;
}

.brand p,
.admin p,
.status {
  color: var(--muted);
}

.auth-form {
  display: grid;
  gap: 10px;
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 18px;
}

.auth-tab {
  background: var(--panel-2);
  color: var(--ink);
}

.auth-tab.active {
  background: var(--accent);
  color: white;
}

#nameField {
  display: grid;
  gap: 10px;
}

.ghost {
  background: var(--panel-2);
  color: var(--ink);
}

.status {
  min-height: 22px;
  margin-top: 14px;
  font-size: 14px;
}

.shell {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-columns: 360px minmax(0, 1fr);
  overflow: hidden;
}

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

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

.me {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.me div,
.chat-title {
  min-width: 0;
}

.me strong,
.me small,
.chat-title strong,
.chat-title small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.me small,
.chat-title small,
.chat-preview {
  color: var(--muted);
  font-size: 12px;
}

.avatar-button,
.avatar {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  padding: 0;
  overflow: hidden;
  background: var(--panel-2);
  color: var(--accent);
  display: grid;
  place-items: center;
  flex: 0 0 auto;
}

.avatar-button img,
.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.icon-button {
  width: 42px;
  height: 42px;
  padding: 0;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: var(--panel-2);
  color: var(--ink);
  flex: 0 0 auto;
}

.icon-button svg,
.send-button svg,
.message-menu svg,
.new-chat button svg {
  width: 21px;
  height: 21px;
  display: block;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

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

.new-chat button {
  padding: 0;
  display: grid;
  place-items: center;
}

.chat-list {
  overflow: auto;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}

.chat-item {
  width: 100%;
  min-height: 74px;
  border-radius: 0;
  padding: 12px 14px;
  background: transparent;
  color: var(--ink);
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr);
  gap: 10px;
  text-align: left;
  border-bottom: 1px solid var(--line);
}

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

.chat-line {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.chat-line strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.unread-badge {
  min-width: 22px;
  height: 22px;
  padding: 0 7px;
  border-radius: 999px;
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  background: var(--accent);
  color: white;
  font-size: 12px;
  font-weight: 800;
}

.chat-item.active,
.chat-item:hover {
  background: var(--panel-2);
}

.chat-pane {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.75), rgba(246, 247, 244, 0.92)),
    var(--bg);
}

:root[data-theme="dark"] .chat-pane {
  background:
    linear-gradient(180deg, rgba(23, 29, 27, 0.82), rgba(16, 20, 19, 0.94)),
    var(--bg);
}

.chat-contact {
  min-width: 0;
  min-height: 48px;
  padding: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  flex: 1 1 auto;
}

.chat-contact:not(:disabled):hover {
  background: var(--panel-2);
}

.chat-contact:disabled {
  cursor: default;
}

.small-avatar {
  width: 42px;
  height: 42px;
  display: none;
}

.chat-contact:not(:disabled) .small-avatar {
  display: grid;
}

.messages {
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 18px clamp(12px, 3vw, 32px);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.messages.empty {
  display: grid;
  place-items: center;
}

.empty-state {
  color: var(--muted);
}

.bubble {
  position: relative;
  max-width: min(680px, 78%);
  border-radius: 8px;
  padding: 9px 11px;
  background: white;
  border: 1px solid var(--line);
  align-self: flex-start;
  box-shadow: 0 4px 12px rgba(24, 32, 29, 0.06);
  word-wrap: break-word;
}

:root[data-theme="dark"] .bubble {
  background: #1b2421;
}

.bubble.deleted {
  color: var(--muted);
  background: #f5f7f5;
}

.deleted-text {
  font-style: normal;
  color: var(--muted);
}

.message-menu {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 28px;
  height: 28px;
  min-height: 28px;
  padding: 0;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.72);
  color: var(--ink);
  opacity: 0;
}

.message-menu svg {
  width: 18px;
  height: 18px;
  margin: auto;
}

.bubble:hover .message-menu,
.bubble:focus-within .message-menu {
  opacity: 1;
}

.message-status {
  margin-left: 4px;
  color: var(--muted);
  font-weight: 900;
}

.message-status.read {
  color: var(--accent-2);
}

.bubble.mine {
  align-self: flex-end;
  background: #dff3e9;
  border-color: #c4e5d5;
}

:root[data-theme="dark"] .bubble.mine {
  background: #17392e;
  border-color: #24664f;
}

.bubble small {
  display: block;
  color: var(--muted);
  font-size: 11px;
  margin-top: 6px;
}

.bubble img,
.bubble video {
  display: block;
  width: min(420px, 100%);
  max-height: 420px;
  object-fit: contain;
  border-radius: 8px;
  background: #111;
}

.bubble audio {
  width: min(360px, 100%);
}

.attachment-link {
  color: var(--accent-2);
  font-weight: 700;
  text-decoration: none;
}

.composer {
  position: relative;
  display: grid;
  grid-template-columns: 42px 42px 42px minmax(0, 1fr) 48px;
  gap: 8px;
  padding: 12px;
  background: var(--panel);
  border-top: 1px solid var(--line);
  align-items: end;
}

#emojiButton {
  grid-column: 1;
}

#fileButton {
  grid-column: 2;
}

#cameraButton {
  grid-column: 3;
}

#messageInput,
.voice-wave {
  grid-column: 4;
}

#actionButton {
  grid-column: 5;
}

.file-button input {
  display: none;
}

.send-button {
  padding: 0;
  display: grid;
  place-items: center;
  touch-action: none;
}

.action-button.recording {
  background: var(--danger);
  transform: scale(1.04);
}

.voice-wave {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 12px;
}

.voice-wave span {
  width: 4px;
  height: 12px;
  border-radius: 999px;
  background: var(--accent);
  animation: voiceWave 700ms ease-in-out infinite;
}

.voice-wave span:nth-child(2) {
  animation-delay: 80ms;
}

.voice-wave span:nth-child(3) {
  animation-delay: 160ms;
}

.voice-wave span:nth-child(4) {
  animation-delay: 240ms;
}

.voice-wave span:nth-child(5) {
  animation-delay: 320ms;
}

.voice-wave span:nth-child(6) {
  animation-delay: 400ms;
}

.voice-wave b {
  margin-left: auto;
  color: var(--muted);
  font-size: 13px;
}

@keyframes voiceWave {
  0%, 100% {
    height: 10px;
    opacity: 0.55;
  }

  50% {
    height: 28px;
    opacity: 1;
  }
}

.emoji-panel {
  position: absolute;
  left: 12px;
  bottom: 66px;
  width: min(330px, calc(100vw - 24px));
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  box-shadow: var(--shadow);
}

:root[data-theme="dark"] .emoji-panel {
  background: var(--panel);
}

.emoji-panel button {
  min-height: 34px;
  padding: 0;
  background: transparent;
  color: var(--ink);
  font-size: 20px;
}

.modal {
  width: min(460px, calc(100vw - 28px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  box-shadow: var(--shadow);
}

.modal::backdrop {
  background: rgba(24, 32, 29, 0.45);
}

.modal form,
.camera-box {
  padding: 20px;
  display: grid;
  gap: 12px;
}

.settings-row {
  width: 100%;
  justify-content: flex-start;
  text-align: left;
  background: var(--panel-2);
  color: var(--ink);
}

.theme-choice {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.theme-button {
  background: var(--panel-2);
  color: var(--ink);
}

.theme-button.active {
  background: var(--accent);
  color: white;
}

.settings-row.danger {
  background: #fff3f0;
  color: var(--danger);
}

:root[data-theme="dark"] .settings-row.danger {
  background: #3a201d;
}

.contact-card {
  padding: 22px;
  display: grid;
  justify-items: center;
  text-align: center;
  gap: 10px;
}

.contact-avatar {
  width: 104px;
  height: 104px;
  font-size: 38px;
}

.contact-card menu {
  width: 100%;
  justify-content: center;
}

.modal menu,
.camera-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 0;
  margin: 8px 0 0;
}

.camera-modal {
  width: min(720px, calc(100vw - 24px));
}

#cameraPreview {
  width: 100%;
  max-height: 70dvh;
  background: #111;
  border-radius: 8px;
}

.mobile-only {
  display: none;
}

.admin {
  height: 100vh;
  overflow: auto;
  padding: 24px;
}

.admin-grid {
  max-width: 1180px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}

.admin-header {
  grid-column: 1 / -1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
}

.stat-card,
.admin-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  overflow: hidden;
}

.stat-card span {
  display: block;
  color: var(--muted);
  font-size: 13px;
}

.stat-card strong {
  font-size: 34px;
}

.wide {
  grid-column: span 2;
}

.table,
.type-list,
.error-list {
  display: grid;
  gap: 8px;
  margin-top: 12px;
  min-width: 0;
}

#codesTable,
#errorLogs {
  max-height: 420px;
  overflow: auto;
  padding-right: 4px;
  -webkit-overflow-scrolling: touch;
}

.row {
  display: grid;
  grid-template-columns: minmax(110px, 1fr) 90px minmax(120px, 1fr) 80px;
  gap: 8px;
  align-items: center;
  padding: 10px;
  border-radius: 8px;
  background: var(--panel-2);
  font-size: 13px;
  min-width: 0;
  width: 100%;
}

.type-list .row {
  grid-template-columns: minmax(0, 1fr) auto;
}

.type-list .row strong,
.row strong,
.row span {
  min-width: 0;
  overflow-wrap: anywhere;
}

.pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 26px;
  border-radius: 999px;
  padding: 0 10px;
  background: #e4eef5;
  color: #174864;
  font-weight: 800;
}

.error-item {
  padding: 10px;
  border-radius: 8px;
  background: #fff3f0;
  color: var(--danger);
  font-size: 13px;
}

@media (hover: none) {
  .message-menu {
    opacity: 1;
  }
}

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

  .sidebar {
    height: 100vh;
    height: 100dvh;
  }

  .chat-pane {
    position: fixed;
    inset: 0;
    transform: translateX(100%);
    transition: transform 180ms ease;
    z-index: 10;
  }

  .shell.chat-open .chat-pane {
    transform: translateX(0);
  }

  .mobile-only {
    display: grid;
  }

  .composer {
    grid-template-columns: 38px 38px 38px minmax(96px, 1fr) 42px;
    gap: 6px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

  .icon-button,
  .composer .icon-button,
  .send-button {
    width: 38px;
    height: 38px;
    min-height: 38px;
  }

  .bubble {
    max-width: 88%;
  }

  .topbar,
  .chat-header {
    min-height: 68px;
  }

  .messages {
    padding: 18px 12px;
  }

  .admin {
    padding: 14px;
  }

  .admin-grid {
    grid-template-columns: 1fr;
  }

  .wide,
  .admin-header {
    grid-column: auto;
  }

  .admin-header {
    align-items: stretch;
    flex-direction: column;
  }

  .row {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 420px) {
  .composer {
    grid-template-columns: repeat(3, 36px) minmax(0, 1fr) 40px;
    gap: 5px;
  }

  .icon-button,
  .composer .icon-button,
  .send-button {
    width: 36px;
    height: 36px;
    min-height: 36px;
  }
}
