*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --sidebar-w: 220px;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --primary: #1a1a2e;
  --primary-hover: #2d2d4e;
  --text: #1e293b;
  --muted: #64748b;
  --success: #16a34a;
  --error: #dc2626;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.07), 0 4px 16px rgba(0, 0, 0, 0.06);
}

.logo {
  object-fit: cover;
  height: 3rem;
  width: max-content;
}

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

/* ── Login ── */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--primary);
}

.login-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.login-card h1 {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--primary);
}

.login-card p {
  font-size: 0.875rem;
  color: var(--muted);
  margin-top: 0.2rem;
}

/* ── App Shell ── */
.app {
  display: flex;
  min-height: 100vh;
}

/* ── Sidebar ── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--primary);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 50;
}

.sidebar-logo {
  padding: 1.25rem 1.25rem 1rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.sidebar-logo img {
  height: 3rem;
  display: block;
}

.sidebar-nav {
  flex: 1;
  padding: 0.75rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  overflow-y: auto;
}

.nav-section {
  font-size: 0.65rem;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.35);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding: 0.75rem 0.6rem 0.35rem;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.55rem 0.75rem;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.6);
  cursor: pointer;
  border: none;
  background: transparent;
  width: 100%;
  text-align: left;
  transition:
    background 150ms,
    color 150ms;
}

.nav-item svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  opacity: 0.7;
}

.nav-item:hover {
  background: rgba(255, 255, 255, 0.07);
  color: rgba(255, 255, 255, 0.9);
}

.nav-item.active {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font-weight: 600;
}

.nav-item.active svg {
  opacity: 1;
}

.sidebar-footer {
  padding: 0.75rem;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.nav-item.logout {
  color: rgba(255, 255, 255, 0.4);
}

.nav-item.logout:hover {
  background: rgba(220, 38, 38, 0.15);
  color: #fca5a5;
}

/* ── Main ── */
.main {
  margin-left: var(--sidebar-w);
  flex: 1;
  padding: 2rem 2.5rem;
  max-width: calc(100% - var(--sidebar-w));
}

.page-header {
  margin-bottom: 1.75rem;
}

.page-header h1 {
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--primary);
}

.page-header p {
  font-size: 0.875rem;
  color: var(--muted);
  margin-top: 0.25rem;
}

/* ── Card ── */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  overflow: hidden;
}

.card-body {
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

/* ── Config Grid ── */
.config-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.25rem;
}

/* ── Fields ── */
.field {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

input,
select,
textarea {
  width: 100%;
  padding: 0.65rem 0.85rem;
  font-size: 0.925rem;
  border-radius: 8px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  transition:
    border-color 150ms,
    box-shadow 150ms;
  font-family: inherit;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(26, 26, 46, 0.1);
}

textarea {
  resize: vertical;
  min-height: calc(100vh - 280px);
  font-size: 0.875rem;
  font-family: "SF Mono", "Fira Code", "Consolas", monospace;
  line-height: 1.6;
}

/* ── Tabs (inside Prompt Manager) ── */
.tab-bar {
  display: flex;
  border-bottom: 2px solid var(--border);
  margin-bottom: 1.5rem;
}

.tab-button {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.65rem 1.1rem;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--muted);
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  cursor: pointer;
  transition:
    color 150ms,
    border-color 150ms;
  font-family: inherit;
}

.tab-button:hover {
  color: var(--primary);
}

.tab-button.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

/* ── Buttons ── */
.btn-row {
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1.4rem;
  font-size: 0.875rem;
  font-weight: 600;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition:
    background 150ms,
    transform 100ms,
    box-shadow 150ms;
  font-family: inherit;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-hover);
  box-shadow: 0 4px 12px rgba(26, 26, 46, 0.2);
}

.btn-primary:disabled {
  background: var(--muted);
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* ── Spinner ── */
.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
  display: none;
}

.btn.loading .spinner {
  display: block;
}
.btn.loading .btn-label {
  opacity: 0.6;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ── Toast ── */
#toast-container {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 999;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.75rem 1.1rem;
  border-radius: 10px;
  font-size: 0.875rem;
  font-weight: 500;
  color: #fff;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  animation: slideIn 0.25s ease forwards;
  pointer-events: all;
}

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

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeOut {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
    transform: translateY(4px);
  }
}

/* ── Users Table ── */
.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

thead th {
  text-align: left;
  padding: 0.65rem 1rem;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}

tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background 100ms;
}

tbody tr:last-child {
  border-bottom: none;
}
tbody tr:hover {
  background: #f8fafc;
}

tbody td {
  padding: 0.75rem 1rem;
  vertical-align: middle;
  color: var(--text);
}

.user-name {
  font-weight: 600;
  color: var(--primary);
}

.user-sub {
  font-size: 0.78rem;
  color: var(--muted);
  margin-top: 0.1rem;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  white-space: nowrap;
}

.badge-green {
  background: #dcfce7;
  color: #15803d;
}
.badge-red {
  background: #fee2e2;
  color: #b91c1c;
}
.badge-gray {
  background: #f1f5f9;
  color: var(--muted);
}
.badge-blue {
  background: #dbeafe;
  color: #1d4ed8;
}

.action-btns {
  display: flex;
  gap: 0.4rem;
  justify-content: flex-end;
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 7px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
  transition: all 150ms;
}

.btn-icon:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: #f8fafc;
}
.btn-icon.danger:hover {
  border-color: var(--error);
  color: var(--error);
  background: #fff5f5;
}

/* ── Modal ── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 1rem;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
  width: 100%;
  max-width: 540px;
  display: flex;
  flex-direction: column;
  max-height: 90vh;
  overflow: hidden;
}

.modal.modal-lg {
  max-width: 720px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-header h2 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--primary);
}

.modal-close {
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.1rem;
  transition: background 150ms;
}

.modal-close:hover {
  background: var(--bg);
  color: var(--text);
}

.modal-body {
  padding: 1.5rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.modal-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
  flex-shrink: 0;
}

/* ── Chat View ── */
.chat-tabs {
  display: flex;
  border-bottom: 2px solid var(--border);
  flex-shrink: 0;
}

.chat-tab {
  padding: 0.65rem 1.1rem;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--muted);
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  cursor: pointer;
  font-family: inherit;
  transition:
    color 150ms,
    border-color 150ms;
}

.chat-tab:hover {
  color: var(--primary);
}
.chat-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

.chat-messages {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1rem 1.5rem;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.chat-bubble-wrap {
  display: flex;
  flex-direction: column;
}

.chat-bubble-wrap.user {
  align-items: flex-end;
}
.chat-bubble-wrap.assistant {
  align-items: flex-start;
}

.chat-meta {
  font-size: 0.7rem;
  color: var(--muted);
  margin-bottom: 0.2rem;
  padding: 0 0.2rem;
}

.chat-bubble {
  max-width: 75%;
  padding: 0.55rem 0.85rem;
  border-radius: 12px;
  font-size: 0.875rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.chat-bubble-wrap.user .chat-bubble {
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.chat-bubble-wrap.assistant .chat-bubble {
  background: #f1f5f9;
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.chat-empty {
  text-align: center;
  color: var(--muted);
  font-size: 0.875rem;
  padding: 2rem 0;
}

/* ── Message action buttons ── */
.msg-actions {
  display: flex;
  gap: 0.25rem;
  margin-top: 0.3rem;
  padding: 0 0.2rem;
  opacity: 0;
  transition: opacity 150ms;
}

.chat-bubble-wrap:hover .msg-actions {
  opacity: 1;
}

.msg-action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
  transition: all 150ms;
  padding: 0;
}

.msg-action-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: #f8fafc;
}

.msg-action-btn.danger:hover {
  border-color: var(--error);
  color: var(--error);
  background: #fff5f5;
}

/* ── Message edit ── */
.msg-edit-container {
  max-width: 75%;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.msg-edit-textarea {
  min-height: 80px !important;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  border-radius: 8px;
  border: 1.5px solid var(--primary);
  font-family: inherit;
  resize: vertical;
  line-height: 1.55;
  width: 100%;
}

.msg-edit-actions {
  display: flex;
  gap: 0.4rem;
  justify-content: flex-end;
}

.msg-edit-actions .btn {
  font-size: 0.8rem;
  padding: 0.35rem 0.9rem;
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1.5px solid var(--border);
}

.btn-ghost:hover {
  background: var(--bg);
  color: var(--text);
  border-color: var(--text);
}

/* ── Edit form grid ── */
.edit-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.edit-grid .field.full {
  grid-column: 1 / -1;
}

.toggle-row {
  display: flex;
  gap: 1.5rem;
}

.toggle-field {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.875rem;
  cursor: pointer;
}

.toggle-field input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

/* ── Responsive ── */
@media (max-width: 700px) {
  :root {
    --sidebar-w: 0px;
  }
  .sidebar {
    display: none;
  }
  .main {
    margin-left: 0;
    padding: 1rem;
    max-width: 100%;
  }
  .config-grid {
    grid-template-columns: 1fr;
  }
}
