:root {
  color-scheme: dark;
  --bg: #101114;
  --panel: #1b1d23;
  --panel-2: #242833;
  --line: #343945;
  --text: #f5f7fb;
  --muted: #aeb6c5;
  --accent: #4cc9a6;
  --accent-2: #ffb454;
  --danger: #ff6b6b;
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

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

button,
input,
select {
  font: inherit;
}

button,
a.secondary {
  border: 0;
  border-radius: var(--radius);
  background: var(--accent);
  color: #08110f;
  cursor: pointer;
  font-weight: 700;
  min-height: 42px;
  padding: 0 16px;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  user-select: none;
}

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

input,
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #12141a;
  color: var(--text);
  min-height: 42px;
  padding: 0 12px;
}

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.calculator-shell,
.auth-shell {
  min-height: 100vh;
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding: max(14px, env(safe-area-inset-top)) 14px max(14px, env(safe-area-inset-bottom));
}

.calculator {
  width: min(430px, 100%);
  background: #17191f;
  border: 1px solid #2a2e38;
  border-radius: 18px;
  box-shadow: 0 18px 70px rgba(0, 0, 0, 0.32);
  padding: 18px;
}

.calc-brand {
  color: var(--muted);
  font-size: 0.8rem;
  margin-bottom: 10px;
  text-transform: uppercase;
}

.calc-display {
  width: 100%;
  height: 86px;
  border-radius: 12px;
  background: #0b0d10;
  border: 1px solid #252934;
  text-align: right;
  padding: 0 16px;
  font-size: 2.25rem;
  color: #f7fafc;
}

.calc-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-top: 16px;
}

.calc-grid button {
  min-height: 68px;
  background: #2b2f3a;
  color: var(--text);
  font-size: 1.28rem;
  -webkit-tap-highlight-color: transparent;
  transition: transform 80ms ease, filter 80ms ease;
}

.calc-grid button:active {
  transform: scale(0.97);
  filter: brightness(1.12);
}

.calc-grid .muted {
  background: #3a404d;
}

.calc-grid .op {
  background: var(--accent-2);
  color: #1d1303;
}

.calc-grid .equals {
  background: var(--accent);
  color: #041512;
}

.wide {
  grid-column: span 2;
}

.auth-card,
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
}

.auth-card {
  width: min(430px, 100%);
}

.private-brand {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #151820;
  padding: 14px;
  margin-bottom: 14px;
}

.private-brand h1 {
  margin: 0 0 6px;
  font-size: 1.1rem;
}

.private-brand p {
  margin: 0 0 5px;
  color: var(--text);
  font-weight: 700;
}

.private-brand span {
  color: var(--muted);
  font-size: 0.88rem;
}

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

.auth-tabs button,
.secondary,
.ghost {
  background: var(--panel-2);
  color: var(--text);
}

.auth-tabs button.active {
  background: var(--accent);
  color: #06130f;
}

.stack {
  display: grid;
  gap: 12px;
}

.compact {
  gap: 8px;
}

.stack h1,
.panel h2,
.sidebar h2,
.admin-header h1 {
  margin: 0;
}

.terms {
  display: grid;
  grid-template-columns: 20px 1fr;
  gap: 10px;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.4;
  align-items: start;
}

.terms input {
  min-height: auto;
}

.text-button {
  min-height: 28px;
  width: fit-content;
  padding: 0;
  background: transparent;
  color: var(--accent);
  font-weight: 700;
}

.modal-layer {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(0, 0, 0, 0.66);
}

.terms-modal {
  width: min(520px, 100%);
  max-height: min(720px, 88dvh);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #151922;
  padding: 16px;
  display: grid;
  gap: 14px;
  box-shadow: 0 20px 90px rgba(0, 0, 0, 0.46);
}

.terms-content {
  display: grid;
  gap: 10px;
  overflow: auto;
  color: var(--muted);
  line-height: 1.45;
}

.terms-content p {
  margin: 0;
}

.form-error {
  min-height: 20px;
  margin: 0;
  color: var(--danger);
  font-size: 0.9rem;
}

.ghost {
  width: 100%;
  margin-top: 14px;
}

.chat-app {
  min-height: 100vh;
  min-height: 100dvh;
  height: 100dvh;
  display: grid;
  grid-template-columns: 320px 1fr;
  background: #0b0f16;
}

.sidebar {
  position: relative;
  border-right: 1px solid var(--line);
  background: #0f141d;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
}

.side-top,
.admin-header,
.header-actions,
.inline-form {
  display: flex;
  align-items: center;
  gap: 10px;
}

.side-actions {
  display: flex;
  gap: 6px;
}

.side-top,
.admin-header {
  justify-content: space-between;
}

.eyebrow,
.member-title {
  color: var(--muted);
  margin: 0 0 4px;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.icon-btn {
  width: 42px;
  height: 42px;
  padding: 0;
  background: #252b36;
  color: var(--text);
  border-radius: 50%;
  flex: 0 0 auto;
}

.panic-icon {
  border-radius: 8px;
  background: #222734;
}

.conversation-list {
  display: flex;
  flex-direction: column;
  gap: 0;
  overflow-y: auto;
  min-height: 120px;
  padding-right: 0;
  border-top: 1px solid rgba(255, 255, 255, 0.04);
}

.conversation-item {
  width: 100%;
  min-height: 72px;
  display: grid;
  grid-template-columns: 50px minmax(0, 1fr);
  align-items: center;
  gap: 11px;
  padding: 10px 6px;
  background: transparent;
  color: var(--text);
  border: 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  text-align: left;
  font-weight: 500;
  border-radius: 0;
  min-width: 0;
}

.conversation-item.active,
.conversation-item:hover {
  background: #172130;
}

.conversation-item strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-main {
  min-width: 0;
  display: grid;
  gap: 4px;
}

.conversation-line {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.conversation-line time {
  color: var(--muted);
  font-size: 0.72rem;
  white-space: nowrap;
}

.conversation-line small {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.unread-badge {
  min-width: 22px;
  height: 22px;
  padding: 0 7px;
  border-radius: 999px;
  background: var(--accent);
  color: #06130f;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.72rem;
  font-weight: 800;
  flex: 0 0 auto;
}

.conversation-item small,
.empty-state {
  color: var(--muted);
  font-size: 0.78rem;
}

.conversation-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: #1d3a34;
  color: var(--accent);
  font-weight: 800;
}

.settings-panel {
  position: fixed;
  inset: 0;
  z-index: 40;
  overflow-y: auto;
  background: #0f141d;
  padding: max(18px, env(safe-area-inset-top)) 16px max(18px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.settings-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.settings-head h3 {
  margin: 0;
  font-size: 1.35rem;
}

.member-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 8px;
  overflow: auto;
  max-height: 180px;
}

.member-drawer {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px;
  background: #171a22;
}

.member-drawer summary {
  cursor: pointer;
  color: var(--muted);
  font-weight: 700;
}

.member-list li,
.list-row {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px;
  background: #171a22;
}

.member-list li {
  display: block;
}

.avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: inline-grid;
  place-items: center;
  object-fit: cover;
  margin-right: 7px;
  background: var(--panel-2);
  color: var(--accent);
  font-size: 0.76rem;
  font-weight: 800;
  vertical-align: middle;
}

.group-tools {
  display: grid;
  gap: 8px;
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.new-chat-form,
.settings-tools {
  display: grid;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px;
  background: #171a22;
}

.field-label,
.settings-note {
  color: var(--muted);
  font-size: 0.82rem;
}

.field-label {
  font-weight: 700;
}

.settings-note {
  margin: 4px 0 0;
}

.avatar-upload {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--muted);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.avatar-upload input {
  display: none;
}

.chat-panel {
  position: relative;
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0;
  overflow: hidden;
  background: #0b0f16;
}

.chat-header {
  position: relative;
  z-index: 3;
  min-height: 62px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
  border-bottom: 1px solid var(--line);
  background: rgba(18, 23, 32, 0.98);
  padding: 10px 14px;
}

.mobile-back {
  display: none;
  flex: 0 0 auto;
}

.chat-header h2 {
  margin: 0;
  font-size: 1.05rem;
  max-width: min(62vw, 680px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.typing-status {
  min-height: 18px;
  color: var(--accent) !important;
}

.inviter-panel {
  position: relative;
  z-index: 3;
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 18px;
}

.watermark {
  pointer-events: none;
  position: absolute;
  inset: 0;
  opacity: 0.045;
  background-image: var(--watermark);
  background-size: 320px 170px;
  z-index: 1;
}

.messages {
  position: relative;
  z-index: 2;
  overflow-y: auto;
  padding: 14px 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.message {
  width: fit-content;
  max-width: min(650px, 86%);
  background: #18202b;
  border: 0;
  border-radius: 16px;
  border-bottom-left-radius: 6px;
  padding: 8px 10px 7px;
  line-height: 1.35;
  box-shadow: 0 7px 18px rgba(0, 0, 0, 0.12);
}

.message.mine {
  align-self: flex-end;
  background: #17614f;
  border-bottom-left-radius: 14px;
  border-bottom-right-radius: 6px;
}

.message.pending {
  opacity: 0.72;
}

.message-meta {
  color: rgba(255, 255, 255, 0.64);
  font-size: 0.7rem;
  margin-bottom: 4px;
}

.message-media {
  display: block;
  max-width: min(360px, 100%);
  border-radius: var(--radius);
  margin-top: 8px;
}

.message-state {
  margin-top: 5px;
  color: var(--muted);
  font-size: 0.72rem;
}

.date-divider {
  align-self: center;
  color: var(--muted);
  background: #171a22;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 10px;
  margin: 6px 0;
  font-size: 0.73rem;
}

.composer {
  position: relative;
  z-index: 3;
  display: flex;
  align-items: end;
  gap: 8px;
  border-top: 1px solid var(--line);
  padding: 10px 12px;
  background: #111722;
}

.composer-main {
  flex: 1;
  display: grid;
  gap: 6px;
  min-width: 0;
}

.composer-main input {
  border-radius: 22px;
  background: #1a202b;
}

.file-preview {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--muted);
  background: #10141b;
  padding: 7px 9px;
  font-size: 0.78rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-picker {
  min-height: 42px;
  width: 42px;
  border-radius: var(--radius);
  background: var(--panel-2);
  display: grid;
  place-items: center;
  cursor: pointer;
  font-weight: 800;
}

.file-picker input {
  display: none;
}

.admin-app {
  min-height: 100vh;
  padding: 20px;
}

.admin-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-top: 18px;
}

.wide-panel {
  grid-column: 1 / -1;
}

.panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.panel-subtitle {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.86rem;
}

.list,
.admin-messages {
  display: grid;
  gap: 8px;
  margin-top: 12px;
  max-height: 420px;
  overflow: auto;
}

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

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

.request-list {
  max-height: 520px;
}

.request-row {
  border-left: 3px solid var(--accent);
}

.request-row.reviewed {
  border-left-color: var(--line);
  opacity: 0.72;
}

.request-row p {
  margin: 8px 0 0;
  color: var(--text);
  line-height: 1.4;
}

.request-topline {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.status-pill {
  min-height: 34px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  display: inline-flex;
  align-items: center;
  color: var(--muted);
  font-size: 0.82rem;
}

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

.admin-chat-browser {
  display: grid;
  grid-template-columns: minmax(240px, 0.38fr) minmax(0, 1fr);
  gap: 12px;
  margin-top: 12px;
  min-height: 560px;
}

.admin-chat-rooms {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: auto;
  background: #12161f;
}

.admin-chat-room {
  width: 100%;
  min-height: 76px;
  padding: 10px 12px;
  border-radius: 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  background: transparent;
  color: var(--text);
  display: grid;
  justify-items: stretch;
  text-align: left;
  gap: 3px;
}

.admin-chat-room.active,
.admin-chat-room:hover {
  background: #172130;
}

.admin-chat-room strong,
.admin-chat-room span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.admin-chat-room small {
  color: var(--muted);
  font-size: 0.75rem;
}

.admin-chat-room span {
  color: var(--muted);
  font-size: 0.82rem;
}

.admin-chat-view {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #0f141d;
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
}

.admin-chat-title {
  min-height: 48px;
  border-bottom: 1px solid var(--line);
  padding: 12px 14px;
  font-weight: 800;
}

.admin-chat-messages {
  overflow: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.admin-message-bubble {
  width: fit-content;
  max-width: min(720px, 88%);
  border-radius: 14px;
  border-bottom-left-radius: 5px;
  background: #18202b;
  padding: 9px 11px;
  line-height: 1.35;
}

.admin-message-bubble a {
  color: var(--accent);
}

.admin-reply-form {
  border-top: 1px solid var(--line);
  padding: 10px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  background: #111722;
}

.admin-reply-form .form-error {
  grid-column: 1 / -1;
}

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

.secret-output {
  border: 1px dashed var(--accent);
  border-radius: var(--radius);
  padding: 10px;
  color: var(--accent);
  word-break: break-all;
}

@media (max-width: 760px) {
  body {
    overflow: hidden;
  }

  .chat-app {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
  }

  .sidebar {
    border-right: 0;
    border-bottom: 0;
    height: 100dvh;
    max-height: none;
    padding: max(18px, env(safe-area-inset-top)) 14px max(14px, env(safe-area-inset-bottom));
  }

  .side-top {
    padding-top: 2px;
  }

  .side-top h2 {
    max-width: 54vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .chat-panel {
    display: none;
    min-height: 100dvh;
    height: 100dvh;
  }

  .chat-app.show-thread .sidebar {
    display: none;
  }

  .chat-app.show-thread .chat-panel {
    display: grid;
  }

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

  .composer {
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
  
  .composer > button {
    min-width: 68px;
    padding: 0 12px;
    border-radius: 22px;
  }

  .messages {
    padding: 10px 10px 8px;
  }

  .message {
    max-width: 84%;
  }

  .conversation-list {
    min-height: 220px;
  }

  .conversation-item {
    min-height: 74px;
    padding: 10px 4px;
  }

  .settings-panel {
    padding: max(20px, env(safe-area-inset-top)) 16px max(18px, env(safe-area-inset-bottom));
  }

  .member-drawer,
  .settings-tools,
  .group-tools,
  .avatar-upload {
    font-size: 0.92rem;
  }

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

  .admin-app {
    height: 100dvh;
    overflow: auto;
    padding: max(14px, env(safe-area-inset-top)) 14px max(14px, env(safe-area-inset-bottom));
  }

  .admin-chat-browser {
    grid-template-columns: 1fr;
    min-height: auto;
  }

  .admin-chat-rooms {
    max-height: 260px;
  }

  .admin-chat-view {
    min-height: 520px;
  }

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