:root {
  color-scheme: light;
  --page: #f7f7f5;
  --surface: #ffffff;
  --surface-subtle: #fafafa;
  --text: #242424;
  --muted: #777;
  --line: #ddddda;
  --line-soft: #ececea;
  --accent: #2f5d50;
  --danger: #a43838;
  --code: #f3f1ee;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  font-family: "Segoe UI", "Hiragino Sans", "Yu Gothic UI", system-ui, sans-serif;
  background: var(--page);
  color: var(--text);
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  width: min(920px, 100%);
  height: 100vh;
  margin: 0 auto;
  padding: 16px;
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  gap: 0;
}

.sidebar {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  border: 1px solid var(--line);
  border-right: 0;
  background: #f0f0ed;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 48px;
  padding: 0 12px;
  border-bottom: 1px solid var(--line);
}

.sidebar-head strong {
  font-size: 14px;
}

.small-button,
.plain-button {
  min-height: 28px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  padding: 0 8px;
  font-size: 12px;
}

.small-button {
  width: 28px;
  padding: 0;
  font-size: 18px;
  line-height: 1;
}

.channel-list {
  overflow-y: auto;
  padding: 10px 8px;
}

.category {
  margin-bottom: 12px;
}

.category[draggable="true"],
.channel-row[draggable="true"] {
  cursor: grab;
}

.category.dragging,
.channel-row.dragging {
  opacity: 0.48;
}

.category.drop-target {
  outline: 1px dashed #b8b8b2;
  outline-offset: 2px;
}

.category-row,
.channel-row {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 4px;
}

.category-toggle {
  display: flex;
  align-items: center;
  min-width: 0;
  min-height: 26px;
  border: 0;
  padding: 0 4px;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  text-align: left;
}

.category-toggle:hover {
  color: var(--text);
}

.chevron {
  display: inline-grid;
  place-items: center;
  width: 16px;
  color: #999;
  font-size: 15px;
}

.category-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.category-tools,
.channel-tools {
  display: flex;
  gap: 2px;
  opacity: 0;
  pointer-events: none;
}

.drag-handle {
  display: grid;
  place-items: center;
  width: 18px;
  height: 22px;
  color: #9a9a94;
  font-size: 13px;
  line-height: 1;
  user-select: none;
}

.channel-row.drop-before::before,
.channel-row.drop-after::after {
  content: "";
  position: absolute;
  left: 8px;
  right: 8px;
  height: 2px;
  border-radius: 999px;
  background: var(--accent);
}

.channel-row.drop-before::before {
  top: -2px;
}

.channel-row.drop-after::after {
  bottom: -2px;
}

.category-row:hover .category-tools,
.channel-row:hover .channel-tools,
.category-tools:focus-within,
.channel-tools:focus-within {
  opacity: 1;
  pointer-events: auto;
}

.mini-icon {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: #777;
  line-height: 1;
}

.mini-icon:hover {
  background: #deded9;
  color: var(--text);
}

.mini-icon.danger:hover {
  color: var(--danger);
}

.mini-icon:disabled {
  opacity: 0.3;
  cursor: default;
}

.category-channels {
  display: grid;
  gap: 2px;
}

.category-channels[hidden] {
  display: none;
}

.channel-button {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  min-width: 0;
  min-height: 32px;
  border: 0;
  border-radius: 4px;
  padding: 0 8px;
  background: transparent;
  color: #555;
  text-align: left;
}

.channel-button span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.channel-button:hover,
.channel-button.active {
  background: #deded9;
  color: var(--text);
}

.sidebar-editor {
  display: grid;
  gap: 6px;
  margin: 4px 0 6px 20px;
  padding: 8px;
  border: 1px solid var(--line-soft);
  border-radius: 4px;
  background: #fbfbfa;
}

.sidebar-editor input {
  width: 100%;
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 8px;
  background: var(--surface);
  outline: none;
}

.sidebar-editor input:focus {
  border-color: var(--accent);
}

.sidebar-editor p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

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

.sidebar-editor-actions button {
  min-height: 28px;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 12px;
}

.sidebar-editor-actions button[type="submit"] {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.sidebar-editor.danger .sidebar-editor-actions button[type="submit"] {
  border-color: var(--danger);
  background: var(--danger);
}

.profile-box {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr);
  gap: 8px;
  align-items: center;
  padding: 10px;
  border-top: 1px solid var(--line);
  background: #e7e7e2;
}

.profile-avatar {
  width: 36px;
  height: 36px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 50%;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  font-weight: 700;
}

.profile-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.profile-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  font-weight: 700;
}

.chat {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100%;
  min-width: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  background: var(--surface);
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 48px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line-soft);
  background: var(--surface);
}

.chat-header h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
}

#statusText {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  color: var(--muted);
  font-size: 12px;
}

.icon-button,
.tool-button {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
}

.icon-button:hover,
.tool-button:hover,
.reaction:hover,
.reaction-add:hover {
  background: var(--surface-subtle);
}

.messages {
  overflow-y: auto;
  padding: 12px 16px 18px;
}

.message {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  gap: 10px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line-soft);
}

.message:last-child {
  border-bottom: 0;
}

.avatar {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: #f1f1ef;
  color: #555;
  font-size: 12px;
  font-weight: 700;
}

img.avatar {
  object-fit: cover;
  display: block;
}

.message-body {
  min-width: 0;
}

.message-content {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: start;
}

.message-main {
  min-width: 0;
}

.message-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 30px;
}

.action-button {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border: 1px solid transparent;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  font-size: 17px;
  line-height: 1;
}

.action-button:hover {
  border-color: var(--line);
  background: var(--surface-subtle);
  color: var(--text);
}

.meta {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}

.author {
  font-size: 14px;
  font-weight: 700;
}

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

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

.confirm-text {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.confirm-delete,
.cancel-delete {
  min-height: 28px;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 12px;
  white-space: nowrap;
}

.confirm-delete {
  border-color: var(--danger);
  color: var(--danger);
}

.confirm-delete:hover,
.cancel-delete:hover {
  background: var(--surface-subtle);
}

.text {
  max-width: min(680px, 100%);
  margin-top: 5px;
  white-space: normal;
  word-break: break-word;
  line-height: 1.5;
  font-size: 14px;
}

.text h1,
.text h2,
.text h3 {
  margin: 2px 0 6px;
  line-height: 1.35;
}

.text h1 {
  font-size: 18px;
}

.text h2 {
  font-size: 17px;
}

.text h3 {
  font-size: 15px;
}

.text a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.text code {
  padding: 1px 5px;
  border-radius: 3px;
  background: var(--code);
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 0.92em;
}

.text pre {
  max-width: 100%;
  overflow-x: auto;
  margin: 8px 0 2px;
  padding: 10px;
  border: 1px solid var(--line-soft);
  border-radius: 4px;
  background: var(--code);
}

.text pre code {
  padding: 0;
  background: transparent;
  white-space: pre;
}

.text ul {
  margin: 4px 0;
  padding-left: 18px;
}

.text li {
  margin: 1px 0;
}

.text div + div,
.text ul + div,
.text div + ul,
.text h1 + ul,
.text h2 + ul,
.text h3 + ul {
  margin-top: 4px;
}

.file-card {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  max-width: min(520px, 100%);
  margin-top: 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface-subtle);
  color: var(--text);
  text-decoration: none;
}

.file-card:hover {
  border-color: #c8c8c4;
}

.image-card {
  display: grid;
  gap: 6px;
  width: min(360px, 100%);
  margin-top: 8px;
  color: var(--muted);
  font-size: 12px;
  text-decoration: none;
  text-align: left;
  border: 0;
  padding: 0;
  background: transparent;
  cursor: zoom-in;
}

.image-card img {
  display: block;
  width: 100%;
  max-height: 320px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface-subtle);
}

.image-card:hover span {
  text-decoration: underline;
}

.file-icon {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  color: var(--muted);
}

.file-name {
  display: block;
  overflow-wrap: anywhere;
  font-size: 13px;
  font-weight: 700;
}

.file-size {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 8px;
}

.reaction,
.reaction-add {
  min-height: 26px;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 8px;
  background: var(--surface);
  color: #555;
  font-size: 12px;
}

.edit-form {
  display: grid;
  gap: 8px;
  margin-top: 6px;
}

.edit-form textarea {
  width: 100%;
  min-height: 84px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 9px 10px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}

.edit-form textarea:focus {
  border-color: var(--accent);
}

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

.inline-actions button {
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
}

.inline-actions button[type="submit"] {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.composer {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  background: var(--surface-subtle);
}

.upload-preview {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 8px;
  width: fit-content;
  max-width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  color: var(--muted);
  font-size: 12px;
}

.upload-preview[hidden] {
  display: none;
}

.upload-preview img {
  width: 72px;
  height: 54px;
  object-fit: cover;
  border: 1px solid var(--line-soft);
  border-radius: 3px;
}

.upload-preview span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.clear-upload {
  width: 24px;
  height: 24px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  color: var(--muted);
}

.image-preview,
.dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0, 0, 0, 0.72);
}

.image-preview {
  display: grid;
  place-items: center;
  padding: 48px 20px;
}

.image-preview[hidden],
.dialog-backdrop[hidden] {
  display: none;
}

.image-preview img {
  max-width: min(100%, 1100px);
  max-height: calc(100vh - 130px);
  object-fit: contain;
  background: #111;
}

.preview-close {
  position: fixed;
  top: 18px;
  right: 18px;
  width: 36px;
  height: 36px;
  border: 1px solid #555;
  border-radius: 4px;
  background: #222;
  color: #fff;
  font-size: 22px;
}

#previewDownload {
  position: fixed;
  bottom: 18px;
  color: #fff;
}

.dialog-backdrop {
  display: grid;
  place-items: center;
  padding: 20px;
}

.dialog {
  width: min(720px, 100%);
  max-height: min(720px, calc(100vh - 40px));
  overflow-y: auto;
  border: 1px solid var(--line);
  background: var(--surface);
  padding: 16px;
}

.dialog-head,
.dialog-form,
.admin-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.dialog-head {
  justify-content: space-between;
  margin-bottom: 14px;
}

.dialog-form {
  margin-bottom: 10px;
}

.dialog-form input,
.dialog-form select {
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  padding: 0 8px;
}

.dialog-form input {
  flex: 1;
}

.dialog-form button,
.admin-row button {
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface-subtle);
  color: var(--text);
  padding: 0 10px;
}

.admin-category {
  border-top: 1px solid var(--line-soft);
  padding: 10px 0;
}

.admin-row {
  justify-content: space-between;
  min-height: 34px;
}

.admin-row.child {
  padding-left: 18px;
}

.file-input {
  display: none;
}

.composer textarea {
  width: 100%;
  max-height: 160px;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 9px 10px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}

.composer textarea:focus,
.login-card input:focus {
  border-color: var(--accent);
}

.send-button,
.login-card button {
  border: 1px solid var(--accent);
  border-radius: 4px;
  padding: 0 16px;
  background: var(--accent);
  color: #ffffff;
  font-weight: 700;
}

.send-button:hover,
.login-card button:hover {
  background: #24483e;
}

.login-panel {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  padding: 20px;
  background: var(--page);
  z-index: 10;
}

.login-panel.visible {
  display: grid;
}

.login-card {
  width: min(340px, 100%);
  display: grid;
  gap: 14px;
  padding: 22px;
  border: 1px solid var(--line);
  background: var(--surface);
}

.login-card label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
}

.login-card input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 9px 10px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}

.login-card button {
  min-height: 40px;
}

#loginError {
  min-height: 18px;
  margin: 0;
  color: var(--danger);
  font-size: 13px;
}

@media (max-width: 760px) {
  .app-shell {
    padding: 0;
    grid-template-columns: 150px minmax(0, 1fr);
  }

  .chat {
    border-inline: 0;
  }

  .chat-header {
    min-height: 46px;
  }

  .messages {
    padding: 10px 12px 16px;
  }

  .message {
    grid-template-columns: 32px minmax(0, 1fr);
    gap: 9px;
  }

  .avatar {
    width: 32px;
    height: 32px;
  }

  .composer {
    grid-template-columns: auto minmax(0, 1fr);
    padding: 10px 12px;
  }

  .send-button {
    grid-column: 2;
    min-height: 36px;
  }
}
