:root {
  color-scheme: light dark;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-alt: #f1f3f5;
  --border: #d7dce2;
  --border-strong: #b8c0ca;
  --text: #17202a;
  --muted: #64707d;
  --accent: #2454d6;
  --accent-hover: #1e45ad;
  --danger: #b42318;
  --danger-bg: #fff1f0;
  --success: #067647;
  --shadow: 0 10px 32px rgba(20, 32, 50, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101317;
    --panel: #171b21;
    --panel-alt: #20262e;
    --border: #303844;
    --border-strong: #465160;
    --text: #edf1f5;
    --muted: #a3adba;
    --accent: #8ab4ff;
    --accent-hover: #a9c8ff;
    --danger: #ff9b92;
    --danger-bg: #3a1d1d;
    --success: #70d6a3;
    --shadow: 0 12px 36px rgba(0, 0, 0, 0.22);
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

button,
.button-link,
input,
textarea {
  font: inherit;
}

button,
.button-link {
  border: 1px solid transparent;
  border-radius: 6px;
  background: var(--accent);
  color: #ffffff;
  min-height: 40px;
  padding: 0 15px;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  white-space: nowrap;
}

button:hover,
.button-link:hover {
  background: var(--accent-hover);
}

button:disabled {
  cursor: wait;
  opacity: 0.7;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
  padding: 10px 12px;
  outline: none;
}

input:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}

textarea {
  min-height: 40px;
  max-height: 150px;
  resize: none;
  line-height: 1.35;
  overflow-y: auto;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(1.35rem, 2.5vw, 2rem);
  letter-spacing: 0;
}

h2 {
  margin: 24px 0 12px;
  color: var(--text);
  font-size: 1rem;
}

.login-shell,
.admin-grid,
.chat-shell {
  width: min(1180px, calc(100vw - 28px));
  margin: 0 auto;
  padding: 28px 0;
}

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
}

.login-shell.wide {
  width: min(900px, calc(100vw - 28px));
}

.admin-grid,
.chat-shell {
  display: grid;
  gap: 18px;
}

.admin-grid {
  grid-template-columns: minmax(280px, 0.9fr) minmax(0, 1.1fr);
  align-items: start;
}

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

.terminal-panel {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.terminal-bar {
  display: flex;
  align-items: center;
  min-height: 44px;
  padding: 0 18px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-alt);
}

.terminal-bar span {
  display: none;
}

.terminal-bar strong {
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.terminal-content {
  padding: 22px;
}

.prompt {
  color: var(--muted);
  margin-bottom: 8px;
  font-size: 0.92rem;
}

.muted,
small {
  color: var(--muted);
}

.stack-form,
.create-form,
.file-form {
  display: grid;
  gap: 14px;
}

.create-form {
  grid-template-columns: 1fr;
  align-items: stretch;
  margin-top: 18px;
}

.create-form label,
.stack-form label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 600;
}

.primary-action,
.create-form button {
  width: 100%;
}

.admin-head,
.chat-status,
.button-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.button-row {
  justify-content: flex-start;
  margin-top: 18px;
}

.ghost-button {
  background: transparent;
  border-color: var(--border-strong);
  color: var(--text);
}

.ghost-button:hover {
  background: var(--panel-alt);
}

.danger-button {
  background: transparent;
  border-color: var(--danger);
  color: var(--danger);
}

.danger-button:hover {
  background: var(--danger-bg);
}

.flash.error,
.error-box {
  border: 1px solid var(--danger);
  border-radius: 6px;
  color: var(--danger);
  background: var(--danger-bg);
  padding: 12px;
}

.chat-table {
  display: grid;
  gap: 10px;
}

.chat-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  background: var(--panel);
}

.chat-row small {
  display: block;
  margin-top: 5px;
  overflow-wrap: anywhere;
}

.copy-label {
  display: block;
  margin: 18px 0 8px;
  color: var(--muted);
}

.copy-input {
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-panel,
.side-panel {
  min-height: calc(100vh - 56px);
}

.chat-content {
  display: grid;
  grid-template-rows: auto auto minmax(360px, 1fr) auto;
  gap: 14px;
  min-height: calc(100vh - 100px);
}

.status-pill {
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  padding: 7px 11px;
  white-space: nowrap;
  font-size: 0.88rem;
  background: var(--panel);
}

.status-pill.ok {
  border-color: color-mix(in srgb, var(--success) 50%, var(--border));
  color: var(--success);
}

.messages {
  min-height: 360px;
  max-height: calc(100vh - 304px);
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  padding: 14px;
}

.message {
  margin-bottom: 14px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
}

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

.message-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  color: var(--muted);
  font-size: 0.82rem;
}

.message-text {
  margin: 8px 0 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message-form {
  display: grid;
  grid-template-columns: 150px minmax(0, 1fr) auto;
  gap: 10px;
  align-items: end;
  background: var(--panel);
}

.message-form textarea {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
}

.drop-zone {
  display: grid;
  gap: 7px;
  border: 1px dashed var(--border-strong);
  border-radius: 6px;
  background: var(--panel-alt);
  padding: 16px;
  cursor: pointer;
  grid-column: 1 / -1;
}

.drop-zone:hover {
  border-color: var(--accent);
}

.file-form {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

#fileInput,
#looseFileInput {
  display: none;
}

.upload-queue,
.file-list {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.upload-item,
.file-item {
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  padding: 12px;
}

.upload-item progress {
  width: 100%;
  accent-color: var(--accent);
}

.file-item strong {
  display: block;
  overflow-wrap: anywhere;
}

.file-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.hidden,
.sr-only {
  display: none;
}

@media (max-width: 900px) {
  .admin-grid,
  .chat-shell {
    grid-template-columns: 1fr;
  }

  .chat-panel,
  .side-panel {
    min-height: auto;
  }

  .chat-content {
    min-height: auto;
    grid-template-rows: auto auto minmax(280px, 50vh) auto;
  }

  .admin-head,
  .chat-status {
    align-items: flex-start;
    flex-direction: column;
  }
}

@media (max-width: 640px) {
  .login-shell,
  .admin-grid,
  .chat-shell {
    width: 100%;
    padding: 10px;
  }

  .terminal-panel {
    border-radius: 0;
    box-shadow: none;
  }

  .terminal-bar {
    min-height: 40px;
    padding: 0 14px;
  }

  .terminal-content {
    padding: 14px;
  }

  .chat-shell {
    gap: 10px;
  }

  .chat-status {
    gap: 10px;
  }

  .chat-status h1 {
    font-size: 1.25rem;
  }

  .status-pill {
    width: 100%;
    text-align: center;
  }

  .chat-content {
    gap: 10px;
    grid-template-rows: auto auto minmax(260px, 45dvh) auto;
  }

  .messages {
    min-height: 260px;
    max-height: 45dvh;
    padding: 10px;
  }

  .message {
    margin-bottom: 11px;
    padding-bottom: 11px;
  }

  .message-form {
    grid-template-columns: minmax(0, 1fr) auto;
    position: sticky;
    bottom: 0;
    padding-top: 8px;
    border-top: 1px solid var(--border);
  }

  .message-form #aliasInput {
    grid-column: 1 / -1;
  }

  .message-form button {
    min-width: 74px;
    padding: 0 12px;
  }

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

  .button-row,
  .file-actions {
    flex-direction: column;
  }

  .button-link,
  .file-actions button,
  .file-form button {
    width: 100%;
  }

  .chat-row {
    grid-template-columns: 1fr;
  }
}
