:root {
  color-scheme: light;
  --bg: #f7faf7;
  --panel: #ffffff;
  --panel-soft: #edf4ef;
  --panel-stripe: #f3f8f4;
  --panel-stripe-strong: #eef5f0;
  --ink: #18231f;
  --muted: #65746d;
  --line: #dbe6df;
  --accent: #126a57;
  --accent-strong: #0d503f;
  --accent-soft: #dff3ea;
  --domain-selected: #dff3ea;
  --domain-selected-line: #8dcbb6;
  --domain-selected-rail: #128064;
  --user-selected: #e8eef8;
  --user-selected-line: #9eb7df;
  --user-selected-rail: #3f6fb5;
  --warning: #b85332;
  --warning-soft: #f7e7df;
  --shadow: 0 18px 60px rgba(20, 45, 36, 0.12);
  --radius: 8px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  letter-spacing: 0;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button,
input,
select,
textarea {
  font: inherit;
  letter-spacing: 0;
}

button {
  cursor: pointer;
}

[hidden] {
  display: none !important;
}

.auth-screen {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: max(24px, env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
}

.login-card {
  width: min(100%, 390px);
  display: grid;
  gap: 16px;
  padding: 28px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.brand-mark {
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: var(--accent);
  color: #ffffff;
  font-weight: 800;
  text-transform: uppercase;
}

.brand-mark.small {
  width: 36px;
  height: 36px;
  border-radius: 9px;
  font-size: 13px;
}

.login-card h1,
.panel-head h2,
.chat-title h2 {
  margin: 0;
  line-height: 1.1;
}

.login-copy,
.panel-head p,
.chat-title p {
  margin: 0;
  color: var(--muted);
}

.login-card label {
  display: grid;
  gap: 7px;
  font-weight: 650;
}

.login-card input,
.to-field input,
textarea,
select {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #ffffff;
  color: var(--ink);
  font-size: 16px;
}

.login-card input,
.to-field input,
textarea {
  padding: 11px 12px;
}

select {
  max-width: 280px;
  padding: 0 36px 0 12px;
}

textarea {
  resize: none;
  line-height: 1.35;
  max-height: 130px;
}

.primary-button,
.secondary-button,
.send-button,
.icon-button,
.chip-button,
.media-button {
  min-height: 44px;
  border: 1px solid transparent;
  border-radius: var(--radius);
}

.primary-button,
.send-button {
  padding: 0 18px;
  background: var(--accent);
  color: #ffffff;
  font-weight: 750;
}

.primary-button:disabled,
.send-button:disabled,
.secondary-button:disabled,
.icon-button:disabled,
.chip-button:disabled,
.media-button.disabled {
  opacity: 0.48;
  cursor: not-allowed;
}

.secondary-button,
.chip-button {
  padding: 0 13px;
  background: #ffffff;
  border-color: var(--line);
  color: var(--ink);
  font-weight: 700;
}

.chip-button[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: #a7d8c8;
  color: var(--accent-strong);
}

.chip-button.danger[aria-pressed="true"] {
  background: var(--warning-soft);
  border-color: #edbda7;
  color: var(--warning);
}

.icon-button,
.media-button {
  width: 44px;
  min-width: 44px;
  display: grid;
  place-items: center;
  background: #ffffff;
  border-color: var(--line);
  color: var(--ink);
  font-weight: 800;
  text-decoration: none;
}

.form-error {
  min-height: 20px;
  margin: 0;
  color: var(--warning);
  font-weight: 650;
}

.app-shell {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr;
  overflow: hidden;
}

.top-bar {
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: max(10px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  background: rgba(247, 250, 247, 0.94);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}

.top-title,
.top-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.top-title div:last-child {
  display: grid;
  min-width: 0;
}

.top-title strong {
  line-height: 1.1;
}

.top-title span {
  overflow: hidden;
  color: var(--muted);
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.top-actions {
  justify-content: flex-end;
}

.app-main {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(310px, 0.38fr) minmax(0, 1fr);
}

.conversation-panel,
.chat-panel {
  min-height: 0;
  background: var(--panel);
}

.conversation-panel {
  display: grid;
  grid-template-rows: auto 1fr;
  border-right: 1px solid var(--line);
}

.panel-head,
.chat-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.panel-head {
  justify-content: space-between;
}

.conversation-list {
  min-height: 0;
  overflow-y: auto;
  padding: 8px;
}

.conversation-row {
  width: 100%;
  min-height: 70px;
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 10px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  color: inherit;
  text-align: left;
}

.conversation-row:hover,
.conversation-row.active {
  background: var(--panel-soft);
  border-color: var(--line);
}

.row-avatar {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: #e6efe9;
  color: var(--accent);
  font-weight: 800;
}

.row-avatar::after {
  content: attr(data-initial);
}

.row-body {
  min-width: 0;
  display: grid;
  gap: 3px;
}

.row-body strong,
.row-body small,
.row-time {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row-body small,
.row-time {
  color: var(--muted);
  font-size: 13px;
}

.chat-panel {
  display: grid;
  grid-template-rows: auto 1fr auto;
}

.back-button {
  display: none;
}

.chat-title {
  min-width: 0;
  flex: 1;
}

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

.chat-flags {
  display: flex;
  gap: 8px;
}

.message-list {
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 18px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.84), rgba(255, 255, 255, 0.32)),
    var(--bg);
}

.message-bubble {
  max-width: min(72%, 620px);
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  box-shadow: 0 4px 14px rgba(23, 45, 36, 0.05);
}

.message-bubble.outbound {
  align-self: flex-end;
  background: var(--accent-soft);
  border-color: #bce1d3;
}

.message-bubble.inbound {
  align-self: flex-start;
}

.message-bubble p {
  margin: 0;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.message-bubble small {
  color: var(--muted);
  font-size: 12px;
}

.compose-bar {
  display: grid;
  grid-template-columns: minmax(160px, 0.28fr) auto minmax(0, 1fr) auto;
  align-items: end;
  gap: 8px;
  padding: 10px max(16px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: #ffffff;
  border-top: 1px solid var(--line);
}

.to-field {
  display: grid;
  gap: 4px;
}

.to-field span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.media-button input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.media-preview {
  grid-column: 1 / -1;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel-soft);
  color: var(--muted);
  font-size: 13px;
}

.empty-state {
  align-self: start;
  margin: 14px;
  padding: 18px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  color: var(--muted);
  text-align: center;
}

.admin-shell {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
}

.admin-main-shell {
  min-height: 0;
  height: 100%;
  overflow-x: auto;
  overflow-y: hidden;
}

.admin-main {
  min-height: 0;
  height: 100%;
  display: grid;
  min-width: 1180px;
  grid-template-columns: 260px 320px minmax(600px, 1fr);
}

.admin-column {
  min-height: 0;
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr;
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow: hidden;
}

.admin-services-column {
  border-right: 0;
}

.admin-list,
.admin-service-detail {
  min-height: 0;
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 10px;
  overscroll-behavior: contain;
}

.admin-list {
  display: grid;
  gap: 8px;
  align-content: start;
}

.admin-service-detail {
  display: grid;
  gap: 12px;
  align-content: start;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.88), rgba(255, 255, 255, 0.72)), var(--bg);
}

.admin-row {
  width: 100%;
  min-height: 72px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: inherit;
  text-align: left;
}

.admin-row:nth-child(even) {
  background: var(--panel-stripe);
}

.admin-row:hover,
.admin-row.active {
  background: var(--panel-soft);
  border-color: var(--line);
}

.admin-row.domain-row.active {
  background: var(--domain-selected);
  border-color: var(--domain-selected-line);
  box-shadow: inset 4px 0 0 var(--domain-selected-rail);
}

.admin-row.user-row.active {
  background: var(--user-selected);
  border-color: var(--user-selected-line);
  box-shadow: inset 4px 0 0 var(--user-selected-rail);
}

.admin-row-body {
  min-width: 0;
  display: grid;
  gap: 4px;
}

.admin-row-body strong,
.admin-row-body small {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.admin-row-body small,
.detail-copy,
.service-copy small,
.status-strip p {
  color: var(--muted);
}

.admin-row-badge,
.tag {
  padding: 4px 9px;
  border-radius: 999px;
  background: var(--panel-soft);
  color: var(--accent-strong);
  font-size: 12px;
  font-weight: 700;
}

.admin-nav-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}

.detail-card,
.service-group {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.88);
}

.detail-card strong,
.service-group h3 {
  margin: 0;
  line-height: 1.2;
}

.detail-card p,
.service-group-head {
  margin: 6px 0 0;
}

.service-groups {
  display: grid;
  gap: 12px;
}

.service-list {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}

.service-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: start;
  gap: 10px;
  padding: 10px 12px;
  border-top: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
}

.service-row:first-child {
  border-top: 0;
  padding-top: 0;
}

.service-row:nth-child(even) {
  background: var(--panel-stripe-strong);
}

.service-check {
  width: 18px;
  height: 18px;
  margin-top: 2px;
}

.service-copy {
  min-width: 0;
  display: grid;
  gap: 4px;
}

.service-copy strong {
  overflow-wrap: anywhere;
}

.service-field-row {
  display: grid;
  gap: 12px;
  padding: 14px 12px;
  border-top: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
}

.service-field-copy {
  display: grid;
  gap: 4px;
}

.service-field-copy strong {
  line-height: 1.2;
}

.service-field-copy small {
  color: var(--muted);
}

.service-field-controls {
  display: grid;
  gap: 10px;
}

.service-field-label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.service-field-input {
  min-height: 44px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--ink);
}

.service-field-textarea {
  min-height: 92px;
  resize: vertical;
}

.service-field-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.service-copy small {
  line-height: 1.35;
}

.service-state {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.tag-cloud {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.status-strip {
  padding: 10px max(16px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  border-top: 1px solid var(--line);
  background: rgba(247, 250, 247, 0.94);
}

.status-strip p {
  margin: 0;
  font-size: 14px;
}

@media (max-width: 860px) {
  .top-bar {
    align-items: stretch;
    flex-direction: column;
  }

  .top-actions,
  .top-actions select {
    width: 100%;
    max-width: none;
  }

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

  .conversation-panel,
  .chat-panel {
    grid-column: 1;
    grid-row: 1;
  }

  .chat-panel {
    display: none;
  }

  .app-shell.chat-visible .conversation-panel {
    display: none;
  }

  .app-shell.chat-visible .chat-panel {
    display: grid;
  }

  .back-button {
    display: grid;
  }

  .chat-head {
    flex-wrap: wrap;
  }

  .chat-flags {
    width: 100%;
    overflow-x: auto;
    padding-bottom: 2px;
  }

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

  .to-field {
    grid-column: 1 / -1;
  }

  .media-button {
    grid-column: 1;
  }

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

@media (max-width: 470px) {
  .login-card {
    padding: 22px;
  }

  .panel-head,
  .chat-head {
    padding: 12px;
  }

  .message-list {
    padding: 12px;
  }

  .row-time {
    display: none;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #141917;
    --panel: #1b211f;
    --panel-soft: #232d29;
    --panel-stripe: #27322d;
    --panel-stripe-strong: #2b3731;
    --ink: #eef5f0;
    --muted: #a0aea7;
    --line: #34413b;
    --accent: #4fb896;
    --accent-strong: #8ed9bf;
    --accent-soft: #16392f;
    --domain-selected: #1d3f35;
    --domain-selected-line: #367b66;
    --domain-selected-rail: #68cfad;
    --user-selected: #283548;
    --user-selected-line: #516b91;
    --user-selected-rail: #8eb4f2;
    --warning: #ff9b72;
    --warning-soft: #3d241c;
    --shadow: 0 18px 60px rgba(0, 0, 0, 0.34);
  }

  .top-bar {
    background: rgba(20, 25, 23, 0.94);
  }

  .login-card input,
  .to-field input,
  textarea,
  select,
  .secondary-button,
  .chip-button,
  .icon-button,
  .media-button,
  .message-bubble,
  .compose-bar,
  .detail-card,
  .service-group {
    background: #202724;
    color: var(--ink);
  }

  .row-avatar {
    background: #24372f;
  }

  .admin-service-detail {
    background: linear-gradient(180deg, rgba(20, 25, 23, 0.82), rgba(20, 25, 23, 0.7)), var(--bg);
  }

  .status-strip {
    background: rgba(20, 25, 23, 0.94);
  }
}
