:root {
  color-scheme: light;
  --bg: #f6f7f5;
  --surface: #ffffff;
  --surface-muted: #eef1ee;
  --line: #d9ded8;
  --text: #17211b;
  --muted: #66736b;
  --brand: #236b4a;
  --brand-strong: #174c35;
  --accent: #b85c38;
  --danger: #b33434;
  --warning: #aa7a17;
  --blue: #2f5f9e;
  --shadow: 0 12px 32px rgba(23, 33, 27, 0.08);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
}

button,
input,
select,
textarea {
  font: inherit;
}

.auth-gate {
  display: grid;
  gap: 20px;
  width: min(960px, calc(100vw - 40px));
  margin: 0 auto;
  padding: 32px 0;
}

.gate-header {
  display: block;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.gate-header h1 {
  margin: 0;
  font-size: 30px;
}

.gate-panel {
  display: grid;
  gap: 18px;
  align-items: start;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  max-width: 520px;
  padding: 24px;
}

.gate-panel h2 {
  margin: 0 0 8px;
  font-size: 20px;
}

.gate-panel .button {
  width: fit-content;
}

.gate-error {
  margin: 0;
  border: 1px solid rgba(179, 52, 52, 0.24);
  border-radius: 8px;
  background: #fff1f1;
  color: var(--danger);
  padding: 12px;
}

.app-shell {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 24px;
  border-right: 1px solid var(--line);
  background: #fbfcfa;
  padding: 20px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  border-radius: 8px;
  background: var(--brand);
  color: #fff;
  font-weight: 800;
}

.brand-title {
  font-size: 16px;
  font-weight: 800;
}

.brand-subtitle,
.subtle {
  color: var(--muted);
  font-size: 12px;
}

.nav-list {
  display: grid;
  gap: 6px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  border-radius: 8px;
  color: var(--text);
  padding: 8px 10px;
  text-decoration: none;
}

.nav-item.active {
  background: #e2ece6;
  color: var(--brand-strong);
  font-weight: 700;
}

.nav-icon {
  width: 20px;
  text-align: center;
}

.auth-panel,
.connection-panel {
  display: grid;
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 14px;
}

.auth-panel p {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 13px;
}

.connection-panel {
  margin-top: auto;
}

.signed-in-name {
  overflow: hidden;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.connection-panel h2,
.auth-panel h2,
.thread-list-panel h2,
.data-panel h2,
.detail-header h2,
.empty-detail h2,
.detail-sections h3 {
  margin: 0;
  font-size: 16px;
}

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

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 9px 10px;
}

textarea {
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--brand);
  outline: 3px solid rgba(35, 107, 74, 0.16);
}

.connection-actions,
.topbar-actions,
.detail-actions,
.reply-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.button,
.icon-button {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-weight: 700;
  padding: 8px 12px;
}

.button.primary {
  border-color: var(--brand);
  background: var(--brand);
  color: #fff;
}

.button.ghost {
  background: transparent;
}

.button.danger {
  border-color: rgba(179, 52, 52, 0.35);
  color: var(--danger);
}

.button:disabled,
.icon-button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.icon-button {
  width: 40px;
  padding: 0;
}

.main {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0;
  padding: 24px;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.topbar h1 {
  margin: 0;
  font-size: 26px;
}

.topbar p {
  margin: 4px 0 0;
  color: var(--muted);
}

.metrics {
  display: grid;
  grid-template-columns: repeat(4, minmax(120px, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}

.dashboard-metrics {
  grid-template-columns: repeat(6, minmax(120px, 1fr));
}

.metric {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 14px;
}

.metric-label {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.metric strong {
  display: block;
  margin-top: 6px;
  font-size: 26px;
}

.workbench {
  display: grid;
  grid-template-columns: minmax(360px, 42%) minmax(0, 1fr);
  min-height: 0;
  gap: 16px;
}

.thread-list-panel,
.detail-panel,
.data-panel {
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.view {
  min-height: 0;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.split-workbench {
  display: grid;
  grid-template-columns: minmax(340px, 36%) minmax(0, 1fr);
  min-height: 0;
  gap: 16px;
}

.data-panel {
  overflow: hidden;
}

.list-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.thread-list-panel {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
}

.panel-header,
.detail-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--line);
  padding: 16px;
}

.inline-search {
  width: min(320px, 42vw);
}

.filters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  border-bottom: 1px solid var(--line);
  padding: 12px 16px;
}

.filters label:last-child {
  grid-column: 1 / -1;
}

.admin-form {
  display: grid;
  gap: 14px;
  padding: 16px;
}

.admin-form.readonly {
  opacity: 0.72;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.form-actions,
.row-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

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

.result-box {
  margin: 0 16px 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfa;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  overflow: auto;
  padding: 12px;
  white-space: pre-wrap;
}

.metric-list {
  display: grid;
  gap: 10px;
  padding: 14px 16px 16px;
}

.metric-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 10px;
}

.metric-row:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}

.embedded-panel {
  box-shadow: none;
}

.thread-list {
  overflow: auto;
  padding: 8px;
}

.thread-row {
  display: grid;
  gap: 8px;
  width: 100%;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  padding: 12px;
  text-align: left;
}

.thread-row:hover,
.thread-row.active {
  border-color: var(--line);
  background: #f6faf7;
}

.thread-row-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-weight: 800;
}

.thread-row-preview {
  overflow: hidden;
  color: var(--muted);
  display: -webkit-box;
  font-size: 13px;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.thread-row-meta,
.detail-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.badge,
.status-badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 999px;
  background: var(--surface-muted);
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  padding: 3px 8px;
}

.badge.bug {
  background: #fae9e1;
  color: var(--accent);
}

.badge.feature {
  background: #e4edf9;
  color: var(--blue);
}

.status-badge.waitingForOperator {
  background: #fff2cf;
  color: var(--warning);
}

.status-badge.waitingForUser {
  background: #e1f0e8;
  color: var(--brand-strong);
}

.status-badge.closed {
  background: #eeeeee;
  color: #555;
}

.detail-panel {
  min-width: 0;
}

.empty-detail {
  display: grid;
  height: 100%;
  min-height: 420px;
  place-content: center;
  gap: 8px;
  color: var(--muted);
  text-align: center;
}

.thread-detail {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  height: 100%;
  min-height: 0;
}

.group-detail {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  height: 100%;
  min-height: 0;
}

.hidden {
  display: none !important;
}

.thread-facts {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 1px;
  margin: 0;
  border-bottom: 1px solid var(--line);
  background: var(--line);
}

.thread-facts div {
  min-width: 0;
  background: #fbfcfa;
  padding: 12px 14px;
}

.thread-facts dt {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.thread-facts dd {
  overflow: hidden;
  margin: 4px 0 0;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: auto;
  padding: 18px;
}

.message {
  display: grid;
  max-width: min(680px, 86%);
  gap: 4px;
}

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

.message.operator {
  align-self: flex-end;
}

.message-meta {
  color: var(--muted);
  font-size: 11px;
}

.message-body {
  border-radius: 8px;
  background: #eef2f8;
  padding: 12px;
  white-space: pre-wrap;
}

.message.operator .message-body {
  background: var(--brand);
  color: #fff;
}

.reply-form {
  display: grid;
  gap: 10px;
  border-top: 1px solid var(--line);
  padding: 14px 16px 16px;
}

.compact-list,
.activity-list,
.detail-sections {
  display: grid;
  gap: 10px;
  padding: 14px 16px 16px;
}

.compact-row,
.activity-item {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  padding: 0 0 10px;
}

.stacked-row {
  align-items: center;
}

.compact-row:last-child,
.activity-item:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}

.primary-text {
  display: grid;
  min-width: 0;
  gap: 3px;
}

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

.primary-text span {
  color: var(--muted);
  font-size: 12px;
}

.empty-inline {
  color: var(--muted);
  font-size: 13px;
  padding: 8px 0;
}

.table-wrap {
  overflow: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 12px 16px;
  text-align: left;
  vertical-align: top;
}

th {
  position: sticky;
  top: 0;
  background: #fbfcfa;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  z-index: 1;
}

td {
  font-size: 13px;
}

.detail-sections {
  overflow: auto;
}

.detail-sections section {
  display: grid;
  gap: 8px;
}

.reply-actions {
  justify-content: space-between;
}

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  max-width: min(440px, calc(100vw - 40px));
  border-radius: 8px;
  background: #17211b;
  color: #fff;
  box-shadow: var(--shadow);
  padding: 12px 14px;
}

.toast.error {
  background: var(--danger);
}

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

  .sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .connection-panel {
    margin-top: 0;
  }

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

  .dashboard-metrics,
  .dashboard-grid,
  .split-workbench {
    grid-template-columns: 1fr;
  }

  .detail-panel {
    min-height: 640px;
  }
}

@media (max-width: 720px) {
  .main,
  .sidebar {
    padding: 16px;
  }

  .metrics,
  .dashboard-metrics,
  .thread-facts,
  .filters {
    grid-template-columns: 1fr;
  }

  .filters label:last-child {
    grid-column: auto;
  }

  .detail-header,
  .topbar {
    flex-direction: column;
  }

  .detail-actions {
    width: 100%;
    flex-wrap: wrap;
  }

  .inline-search {
    width: 100%;
  }
}
