:root {
  color-scheme: dark;
  --bg: #0b1118;
  --surface: #111b25;
  --surface-2: #162432;
  --surface-3: #1b2d3d;
  --text: #edf4fb;
  --muted: #8ea2b3;
  --line: #263848;
  --accent: #4aa8ff;
  --accent-strong: #66c0f4;
  --green: #61d394;
  --yellow: #f0bd55;
  --danger: #ff6b6b;
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(180deg, rgba(25, 48, 70, .52), rgba(11, 17, 24, 0) 220px),
    var(--tg-theme-bg-color, var(--bg));
  color: var(--tg-theme-text-color, var(--text));
  font: 14px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.app {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: 10px;
}

.week-header {
  display: grid;
  grid-template-columns: minmax(150px, .75fr) minmax(0, 1fr);
  align-items: stretch;
  gap: 8px;
  padding: 8px;
  border: 1px solid rgba(102, 192, 244, .18);
  border-radius: var(--radius);
  background: rgba(17, 27, 37, .92);
  backdrop-filter: blur(10px);
  margin-bottom: 8px;
}

.section-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-width: 0;
  min-height: 42px;
}

.account-strip {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 8px;
}

.chip-subtitle,
.item-subtitle,
.muted,
.hint {
  color: var(--tg-theme-hint-color, var(--muted));
}

.section-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 18px;
  letter-spacing: 0;
}

.account-chip {
  min-width: 0;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #0d1721;
}

.chip-title,
.chip-subtitle {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chip-title {
  font-weight: 700;
}

.chip-subtitle {
  margin-top: 1px;
  font-size: 12px;
}

.status-badge,
.counter,
.unread-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 22px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
}

.status-badge {
  min-width: 70px;
  padding: 3px 8px;
  border: 1px solid var(--line);
  background: #0e1923;
  color: var(--muted);
}

.status-badge.online {
  border-color: rgba(97, 211, 148, .35);
  background: rgba(97, 211, 148, .12);
  color: var(--green);
}

.status-badge.offline {
  border-color: rgba(255, 107, 107, .24);
  background: rgba(255, 107, 107, .09);
  color: #ff9b9b;
}

.status-badge.connecting {
  border-color: rgba(240, 189, 85, .28);
  background: rgba(240, 189, 85, .1);
  color: var(--yellow);
}

.icon-button,
.plus-button,
.secondary,
.primary,
.tab,
.list button,
.back-button {
  border: 0;
  border-radius: 7px;
  cursor: pointer;
}

.icon-button {
  width: 40px;
  height: 40px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 19px;
}

.plus-button {
  flex: 0 0 auto;
  width: 28px;
  height: 28px;
  background: rgba(102, 192, 244, .15);
  color: var(--accent-strong);
  font-size: 20px;
  line-height: 1;
  font-weight: 800;
}

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  margin: 0;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(17, 27, 37, .92);
}

.tab {
  min-height: 38px;
  background: transparent;
  color: var(--text);
  font-weight: 650;
}

.tab.active {
  background: var(--surface-3);
}

.section {
  display: none;
  margin-top: 8px;
}

.section.active {
  display: block;
}

.login-card,
.workspace,
.family-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(17, 27, 37, .94);
}

.login-card {
  margin-bottom: 8px;
  overflow: hidden;
}

.login-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 58px;
  padding: 10px 12px;
}

h1,
h2,
p {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: 16px;
}

h2 {
  font-size: 14px;
}

.login-head p {
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.login-body {
  padding: 0 12px 12px;
  border-top: 1px solid var(--line);
}

.form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto;
  gap: 8px;
  margin-top: 10px;
  align-items: end;
}

.guard-form {
  grid-template-columns: minmax(0, 1fr) minmax(160px, .45fr) auto;
}

label span {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 12px;
}

input {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 8px 10px;
  background: #070d13;
  color: var(--text);
  outline: none;
}

input:focus {
  border-color: rgba(102, 192, 244, .72);
  box-shadow: 0 0 0 3px rgba(102, 192, 244, .12);
}

input:disabled {
  opacity: .55;
}

.primary,
.secondary {
  min-height: 40px;
  padding: 0 14px;
  font-weight: 750;
}

.primary {
  background: var(--tg-theme-button-color, var(--accent));
  color: var(--tg-theme-button-text-color, #fff);
}

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

.primary:disabled {
  cursor: default;
  opacity: .55;
}

.hint {
  min-height: 20px;
  margin-top: 8px;
  white-space: pre-wrap;
}

.hidden {
  display: none !important;
}

.workspace {
  display: grid;
  grid-template-columns: 330px minmax(0, 1fr);
  min-height: min(680px, calc(100vh - 155px));
  overflow: hidden;
}

.sidebar {
  display: block;
  min-width: 0;
  padding: 10px;
  border-right: 1px solid var(--line);
}

.side-block {
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.side-head,
.chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.counter {
  min-width: 24px;
  padding: 0 7px;
  background: #0e1923;
  color: var(--muted);
}

.list {
  display: grid;
  align-content: start;
  gap: 6px;
  margin-top: 8px;
  min-height: 0;
  overflow: auto;
}

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

.list.empty {
  display: block;
  padding: 10px;
  border: 1px dashed rgba(142, 162, 179, .22);
  border-radius: 7px;
}

.list button {
  width: 100%;
  min-height: 48px;
  padding: 8px 9px;
  text-align: left;
  background: #0d1721;
  color: var(--text);
  border: 1px solid transparent;
}

.list button:hover {
  background: #102030;
}

.list button.active {
  border-color: rgba(102, 192, 244, .48);
  background: #122538;
}

.item-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
}

.item-title,
.item-subtitle {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item-title {
  font-weight: 750;
}

.item-subtitle {
  margin-top: 1px;
  font-size: 12px;
}

.item-meta {
  display: grid;
  justify-items: end;
  gap: 3px;
  color: var(--muted);
  font-size: 11px;
}

.mini-status {
  color: var(--muted);
}

.mini-status.online {
  color: var(--green);
}

.mini-status.offline {
  color: #ff9b9b;
}

.unread-badge {
  min-width: 22px;
  padding: 0 7px;
  background: var(--accent);
  color: #fff;
}

.chat {
  min-width: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: #0b131c;
}

.chat-head {
  min-height: 58px;
  justify-content: flex-start;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: rgba(13, 23, 33, .82);
}

.back-button {
  display: none;
  width: 36px;
  height: 36px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 18px;
}

.chat-title-wrap {
  min-width: 0;
}

.chat-title-wrap h2,
.chat-title-wrap .muted {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.messages {
  min-height: 320px;
  padding: 12px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.empty-state {
  margin: auto;
  width: min(360px, 100%);
  padding: 18px;
  border: 1px dashed rgba(142, 162, 179, .2);
  border-radius: var(--radius);
  text-align: center;
}

.empty-state strong,
.empty-state span {
  display: block;
}

.empty-state strong {
  color: var(--text);
  font-size: 16px;
}

.empty-state span {
  margin-top: 4px;
}

.message {
  max-width: min(76%, 640px);
  padding: 8px 10px;
  border: 1px solid rgba(255, 255, 255, .04);
  border-radius: 8px;
  background: #111f2b;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message.out {
  align-self: flex-end;
  background: #1b4962;
  border-color: rgba(102, 192, 244, .18);
}

.message.in {
  align-self: flex-start;
}

.message-meta {
  margin-top: 4px;
  color: #b7c6d3;
  font-size: 11px;
  text-align: right;
}

.send-form {
  position: sticky;
  bottom: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--line);
  background: rgba(13, 23, 33, .94);
}

.send-button {
  min-width: 82px;
}

.family-card {
  min-height: 220px;
  display: grid;
  place-content: center;
  gap: 6px;
  text-align: center;
}

.family-card p {
  color: var(--muted);
}

.toast {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: 12px;
  z-index: 20;
  max-width: 680px;
  margin: 0 auto;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #07111a;
  color: var(--text);
  box-shadow: 0 10px 30px rgba(0, 0, 0, .3);
}

@media (max-width: 760px) {
  .app {
    padding: 8px;
  }

  .week-header {
    grid-template-columns: 1fr;
  }

  .account-strip {
    grid-template-columns: minmax(0, 1fr) auto auto;
  }

  .form,
  .guard-form {
    grid-template-columns: 1fr;
  }

  .login-head {
    align-items: flex-start;
  }

  .workspace {
    grid-template-columns: 1fr;
    min-height: calc(100vh - 138px);
  }

  .sidebar {
    border-right: 0;
    min-height: calc(100vh - 156px);
  }

  .chat {
    display: none;
    min-height: calc(100vh - 156px);
  }

  body.chat-open .sidebar {
    display: none;
  }

  body.chat-open .chat {
    display: grid;
  }

  .back-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .message {
    max-width: 88%;
  }
}
