:root {
  --bg: #0f1419;
  --panel: #161b22;
  --panel-2: #1c2330;
  --line: #2a3340;
  --text: #e7ecf3;
  --muted: #8b98a8;
  --accent: #3d8bfd;
  --user: #243044;
  --staff: #1e3a2f;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  font-family: "Segoe UI", "IBM Plex Sans", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  min-height: 100%;
  min-height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}

body.app {
  display: grid;
  grid-template-columns: minmax(280px, 340px) 1fr;
  grid-template-rows: 1fr;
  height: 100%;
  height: 100dvh;
  min-height: 100dvh;
  max-height: 100dvh;
  background:
    radial-gradient(1200px 600px at 10% -10%, #1a2740, transparent),
    radial-gradient(900px 500px at 100% 0%, #1a3028, transparent),
    var(--bg);
}

.muted { color: var(--muted); }
.tiny { font-size: 12px; }

.login-page {
  display: grid;
  place-items: center;
  min-height: 100%;
  min-height: 100dvh;
  overflow: auto;
  padding: 24px;
  padding-bottom: calc(24px + var(--safe-bottom));
  background:
    radial-gradient(900px 500px at 50% 0%, #1a2740, transparent),
    var(--bg);
}
.login-card {
  width: min(400px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 28px;
  display: grid;
  gap: 14px;
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card label { display: grid; gap: 6px; font-size: 14px; }
.login-card input,
.composer textarea,
.composer input[type=file] {
  width: 100%;
  max-width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  padding: 10px 12px;
  font: inherit;
}
button, .ghost {
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
  font-weight: 600;
  font: inherit;
  touch-action: manipulation;
}
.ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text);
  font-weight: 500;
}
.error {
  background: #3a1a1a;
  border: 1px solid #633;
  color: #fbb;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
}

.sidebar,
.thread {
  min-width: 0;
  min-height: 0;
  height: 100%;
  max-height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.sidebar {
  border-right: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 94%, transparent);
}
.sidebar-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  padding-top: calc(14px + var(--safe-top));
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.filters {
  display: flex;
  gap: 6px;
  padding: 10px 12px;
  flex-wrap: wrap;
  flex-shrink: 0;
}
.filters a {
  color: var(--muted);
  text-decoration: none;
  font-size: 13px;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid transparent;
}
.filters a.active {
  color: var(--text);
  border-color: var(--line);
  background: var(--panel-2);
}
.list {
  overflow: auto;
  flex: 1 1 auto;
  min-height: 0;
  padding: 8px;
  -webkit-overflow-scrolling: touch;
}
.list-item {
  display: block;
  text-decoration: none;
  color: inherit;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid transparent;
  margin-bottom: 6px;
}
.list-item:hover { background: var(--panel-2); }
.list-item.active {
  background: var(--panel-2);
  border-color: var(--line);
}
.list-item .row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
}
.list-item .title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 4px;
  word-break: break-word;
}
.list-item .preview {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.badge {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .04em;
  padding: 2px 7px;
  border-radius: 999px;
  background: #333;
}
.badge.new { background: #3a2a10; color: #fc6; }
.badge.in_progress { background: #123528; color: #8ddaad; }
.badge.closed { background: #2a2a2a; color: #999; }
.empty, .placeholder {
  color: var(--muted);
  padding: 40px 20px;
  text-align: center;
  margin: auto;
}

.thread {
  background: color-mix(in srgb, var(--bg) 80%, #000);
}
.thread-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
  padding: 14px 18px;
  padding-top: calc(14px + var(--safe-top));
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.thread-head-main {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  min-width: 0;
  flex: 1;
}
.thread-head h2 {
  margin: 0 0 4px;
  font-size: 18px;
  word-break: break-word;
}
.back-btn {
  display: none;
  flex-shrink: 0;
  white-space: nowrap;
}
.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
  flex-shrink: 0;
}
.actions button {
  white-space: nowrap;
}

.messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}
.bubble {
  max-width: min(680px, 92%);
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
}
.bubble.user {
  align-self: flex-start;
  background: var(--user);
}
.bubble.staff {
  align-self: flex-end;
  background: var(--staff);
}
.bubble .meta {
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 6px;
}
.bubble .text {
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.4;
}
.media img, .media video {
  max-width: 100%;
  max-height: 360px;
  border-radius: 8px;
  display: block;
}
.file-link { color: #9cf; }

.composer {
  border-top: 1px solid var(--line);
  padding: 12px 16px;
  padding-bottom: calc(12px + var(--safe-bottom));
  display: grid;
  gap: 8px;
  background: var(--panel);
  flex-shrink: 0;
}
.composer-row {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
}
.composer textarea {
  resize: vertical;
  min-height: 56px;
  max-height: 30vh;
}

/* Tablet */
@media (max-width: 960px) {
  body.app {
    grid-template-columns: minmax(240px, 300px) 1fr;
  }
  .thread-head {
    flex-direction: column;
    align-items: stretch;
  }
  .actions { justify-content: stretch; }
  .actions button { flex: 1 1 auto; }
}

/* Mobile: list OR thread */
@media (max-width: 760px) {
  body.app {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
  }

  .sidebar,
  .thread {
    grid-column: 1;
    grid-row: 1;
    width: 100%;
    height: 100%;
    max-height: 100dvh;
  }

  body.app:not(.has-selected) .thread { display: none; }
  body.app.has-selected .sidebar { display: none; }

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

  .sidebar { border-right: 0; }

  .sidebar-head,
  .thread-head {
    padding: 12px 14px;
    padding-top: calc(12px + var(--safe-top));
  }

  .thread-head h2 { font-size: 16px; }

  .filters {
    padding: 8px 10px;
    gap: 4px;
  }
  .filters a {
    font-size: 12px;
    padding: 8px 10px;
  }

  .list-item { padding: 14px 12px; }

  .messages { padding: 12px; }

  .bubble { max-width: 95%; }

  .composer {
    padding: 10px 12px;
    padding-bottom: calc(10px + var(--safe-bottom));
  }
  .composer textarea {
    min-height: 48px;
    font-size: 16px; /* avoid iOS zoom */
  }
  .composer-row {
    flex-direction: column;
    align-items: stretch;
  }
  .composer-row input[type=file] {
    font-size: 14px;
  }
  .composer-row button {
    width: 100%;
    min-height: 44px;
  }
  .actions button {
    min-height: 40px;
    font-size: 13px;
    padding: 8px 10px;
  }
  .login-card {
    padding: 22px 18px;
  }
  .login-card input {
    font-size: 16px;
  }
}
