:root {
  --bg: #201831;
  --bg-elevated: #2d2442;
  --panel: rgba(59, 48, 90, 0.9);
  --panel-2: rgba(78, 63, 116, 0.94);
  --ink: #faf8ff;
  --muted: #cbc4e2;
  --border: rgba(214, 196, 255, 0.26);
  --border-strong: rgba(214, 196, 255, 0.42);
  --accent: #ba9cff;
  --accent-strong: #e6dbff;
  --accent-soft: rgba(186, 156, 255, 0.22);
  --accent-soft-2: rgba(186, 156, 255, 0.34);
  --success: #22c55e;
  --warn: #fbbf24;
  --error: #f87171;
  --shadow-lg: 0 18px 55px rgba(3, 2, 9, 0.45);
  --shadow-md: 0 8px 24px rgba(5, 4, 12, 0.28);
  --radius-xl: 18px;
  --radius-lg: 14px;
  --radius-md: 12px;
  --radius-sm: 10px;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: "Segoe UI Variable", "IBM Plex Sans", "Inter", system-ui, sans-serif;
  background:
    radial-gradient(900px 500px at 10% -5%, rgba(194, 168, 255, 0.34), transparent 62%),
    radial-gradient(700px 420px at 95% 8%, rgba(230, 216, 255, 0.22), transparent 60%),
    radial-gradient(1100px 600px at 50% 120%, rgba(158, 102, 255, 0.24), transparent 65%),
    linear-gradient(180deg, #1d1630 0%, #241b39 45%, #201831 100%);
  overflow-x: auto;
}

body::before,
body::after {
  content: "";
  position: fixed;
  inset: auto;
  pointer-events: none;
  z-index: -1;
  filter: blur(50px);
  opacity: 0.7;
}

body::before {
  width: 260px;
  height: 260px;
  left: 6%;
  top: 14%;
  background: rgba(194, 168, 255, 0.28);
  border-radius: 999px;
}

body::after {
  width: 340px;
  height: 340px;
  right: 4%;
  top: 58%;
  background: rgba(158, 102, 255, 0.2);
  border-radius: 999px;
}

a {
  color: var(--accent-strong);
}

code {
  font-family: "Cascadia Code", "IBM Plex Mono", "Consolas", monospace;
  background: rgba(194, 168, 255, 0.12);
  border: 1px solid rgba(194, 168, 255, 0.18);
  border-radius: 6px;
  padding: 0.05rem 0.35rem;
}

.page {
  max-width: 1160px;
  margin: 0 auto;
  padding: 1.15rem 1rem 1.35rem;
  display: grid;
  gap: 0.95rem;
  min-width: 0;
}

.app-shell {
  position: relative;
}

.panel {
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 1rem;
  box-shadow: var(--shadow-md);
  backdrop-filter: blur(10px);
  min-width: 0;
}

.panel-header {
  padding: 1rem 1.1rem 0.95rem;
  border-color: var(--border-strong);
  box-shadow: var(--shadow-lg);
}

.header {
  display: grid;
  gap: 0.5rem;
}

.header-top {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  align-items: center;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  border-radius: 999px;
  border: 1px solid rgba(194, 168, 255, 0.26);
  background: rgba(176, 137, 255, 0.15);
  color: #efe6ff;
  padding: 0.3rem 0.65rem;
  font-size: 0.78rem;
  line-height: 1;
  letter-spacing: 0.01em;
}

.badge-subtle {
  background: rgba(255, 255, 255, 0.03);
  color: var(--muted);
  border-color: rgba(255, 255, 255, 0.08);
}

.header h1 {
  margin: 0;
  font-size: clamp(1.35rem, 2vw, 1.8rem);
  font-weight: 700;
  letter-spacing: -0.02em;
}

.header p {
  margin: 0;
  color: var(--muted);
  max-width: 68ch;
}

.section-heading {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  margin-bottom: 0.7rem;
}

.section-heading.split {
  justify-content: space-between;
  flex-wrap: wrap;
}

.section-heading h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.section-subtitle {
  margin: 0.25rem 0 0;
  color: var(--muted);
  font-size: 0.87rem;
  line-height: 1.35;
}

.field-label {
  display: block;
  margin-bottom: 0.45rem;
  color: #e4ddf6;
  font-weight: 600;
  font-size: 0.92rem;
}

.composer-card {
  box-shadow: var(--shadow-lg);
}

textarea {
  width: 100%;
  resize: vertical;
  min-height: 112px;
  padding: 0.9rem 0.95rem;
  border-radius: var(--radius-lg);
  border: 1px solid rgba(194, 168, 255, 0.24);
  background: linear-gradient(180deg, rgba(30, 24, 47, 0.95), rgba(23, 19, 36, 0.95));
  color: var(--ink);
  font: inherit;
  line-height: 1.45;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.02);
  transition: border-color 140ms ease, box-shadow 140ms ease, background 140ms ease;
}

textarea::placeholder {
  color: rgba(203, 196, 226, 0.78);
}

textarea:hover {
  border-color: rgba(194, 168, 255, 0.34);
}

textarea:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow:
    0 0 0 3px rgba(176, 137, 255, 0.18),
    0 0 0 1px rgba(194, 168, 255, 0.42) inset;
}

input[type="password"] {
  width: 100%;
  padding: 0.82rem 0.9rem;
  border-radius: var(--radius-lg);
  border: 1px solid rgba(194, 168, 255, 0.24);
  background: linear-gradient(180deg, rgba(30, 24, 47, 0.95), rgba(23, 19, 36, 0.95));
  color: var(--ink);
  font: inherit;
  transition: border-color 140ms ease, box-shadow 140ms ease, background 140ms ease;
}

input[type="password"]:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow:
    0 0 0 3px rgba(176, 137, 255, 0.18),
    0 0 0 1px rgba(194, 168, 255, 0.42) inset;
}

.composer-meta {
  margin-top: 0.55rem;
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  flex-wrap: wrap;
  font-size: 0.86rem;
}

.actions {
  margin-top: 0.8rem;
  display: flex;
  gap: 0.55rem;
  flex-wrap: wrap;
}

.inline-actions {
  margin-top: 0;
}

button {
  border: 1px solid rgba(194, 168, 255, 0.38);
  background: linear-gradient(180deg, var(--accent) 0%, #9b75ff 100%);
  color: white;
  border-radius: 12px;
  padding: 0.62rem 0.9rem;
  cursor: pointer;
  font: inherit;
  font-weight: 600;
  line-height: 1;
  letter-spacing: 0.01em;
  box-shadow: 0 8px 16px rgba(98, 55, 171, 0.24);
  transition:
    transform 120ms ease,
    box-shadow 120ms ease,
    border-color 120ms ease,
    background 120ms ease,
    color 120ms ease,
    opacity 120ms ease;
}

button:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 12px 24px rgba(98, 55, 171, 0.28);
  background: linear-gradient(180deg, #c5adff 0%, #a987ff 100%);
}

button:active:not(:disabled) {
  transform: translateY(0);
}

button.secondary {
  background: rgba(255, 255, 255, 0.03);
  color: #efe7ff;
  border-color: rgba(194, 168, 255, 0.22);
  box-shadow: none;
}

button.secondary:hover:not(:disabled) {
  background: rgba(176, 137, 255, 0.14);
  border-color: rgba(194, 168, 255, 0.38);
}

button.danger {
  background: rgba(248, 113, 113, 0.06);
  color: #fecaca;
  border-color: rgba(248, 113, 113, 0.24);
  box-shadow: none;
}

button.danger:hover:not(:disabled) {
  background: rgba(248, 113, 113, 0.12);
  border-color: rgba(248, 113, 113, 0.34);
}

button:focus-visible {
  outline: none;
  box-shadow:
    0 0 0 3px rgba(176, 137, 255, 0.2),
    0 0 0 1px rgba(194, 168, 255, 0.58);
}

button:disabled {
  opacity: 0.52;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.conversation-card {
  padding-bottom: 0.85rem;
}

.auth-grid {
  display: grid;
  gap: 0.75rem;
  align-items: end;
}

.auth-actions {
  margin-top: 0;
}

.meta-pill {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
  min-height: 2rem;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  border: 1px solid rgba(194, 168, 255, 0.18);
  background: rgba(255, 255, 255, 0.02);
  font-size: 0.82rem;
  line-height: 1.15;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-log {
  margin-top: 0.15rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  min-height: 0;
  max-height: 420px;
  overflow: auto;
  padding: 0.35rem 0.4rem 0.2rem;
}

.chat-log:empty {
  min-height: 72px;
}

.chat-item {
  width: fit-content;
  max-width: min(86%, 820px);
  border: 1px solid rgba(194, 168, 255, 0.16);
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.02);
  padding: 0.7rem 0.8rem;
  box-shadow: 0 4px 14px rgba(4, 3, 10, 0.14);
  min-width: 0;
}

.chat-item.user {
  align-self: flex-end;
  background: linear-gradient(180deg, rgba(176, 137, 255, 0.2), rgba(126, 92, 214, 0.14));
  border-color: rgba(194, 168, 255, 0.32);
}

.chat-item.assistant {
  align-self: flex-start;
  background: rgba(255, 255, 255, 0.025);
}

.chat-role {
  font-size: 0.72rem;
  color: rgba(216, 205, 240, 0.84);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 0.32rem;
}

.chat-text {
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.45;
  color: var(--ink);
}

.utility-stack {
  display: grid;
  gap: 0.95rem;
}

.status {
  display: inline-flex;
  align-items: center;
  padding: 0.45rem 0.7rem;
  border-radius: 999px;
  border: 1px solid rgba(194, 168, 255, 0.18);
  background: rgba(255, 255, 255, 0.03);
  font-size: 0.86rem;
  line-height: 1;
}

.status.muted {
  color: #f0e8ff;
}

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

.message-list {
  margin-top: 0.15rem;
  display: grid;
  gap: 0.45rem;
}

.msg {
  padding: 0.6rem 0.7rem;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.06);
  background: rgba(255, 255, 255, 0.025);
  color: #e6e1f7;
  line-height: 1.35;
}

.msg.error {
  border-color: rgba(248, 113, 113, 0.25);
  color: #fecaca;
  background: rgba(127, 29, 29, 0.15);
}

.msg.warn {
  border-color: rgba(251, 191, 36, 0.24);
  color: #fde68a;
  background: rgba(120, 53, 15, 0.16);
}

.sql-box {
  margin: 0;
  padding: 0.9rem;
  min-height: 86px;
  border-radius: var(--radius-lg);
  border: 1px solid rgba(194, 168, 255, 0.18);
  background:
    linear-gradient(180deg, rgba(31, 25, 49, 0.96), rgba(39, 31, 60, 0.96)),
    radial-gradient(circle at 100% 0%, rgba(186, 156, 255, 0.14), transparent 40%);
  color: #f4efff;
  font-family: "Cascadia Code", "IBM Plex Mono", "Consolas", monospace;
  font-size: 0.88rem;
  line-height: 1.45;
  overflow: auto;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.02);
}

.context-used {
  margin-top: 0.55rem;
  font-size: 0.86rem;
  line-height: 1.35;
}

.table-wrap {
  overflow: auto;
  border: 1px solid rgba(194, 168, 255, 0.16);
  border-radius: var(--radius-lg);
  background: rgba(255, 255, 255, 0.02);
}

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.9rem;
  table-layout: fixed;
}

th,
td {
  padding: 0.52rem 0.66rem;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: rgba(27, 23, 42, 0.96);
  color: #f0e8ff;
  font-weight: 600;
  border-bottom-color: rgba(194, 168, 255, 0.18);
  backdrop-filter: blur(8px);
}

tbody tr:nth-child(odd) {
  background: rgba(255, 255, 255, 0.012);
}

tbody tr:hover {
  background: rgba(176, 137, 255, 0.12);
}

tbody tr:last-child td {
  border-bottom: none;
}

#results {
  min-height: 0;
}

#result-meta {
  margin-bottom: 0.55rem;
  font-size: 0.86rem;
}

#messages:empty {
  display: none;
}

.chat-log::-webkit-scrollbar,
.sql-box::-webkit-scrollbar,
.table-wrap::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

.chat-log::-webkit-scrollbar-track,
.sql-box::-webkit-scrollbar-track,
.table-wrap::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.02);
}

.chat-log::-webkit-scrollbar-thumb,
.sql-box::-webkit-scrollbar-thumb,
.table-wrap::-webkit-scrollbar-thumb {
  background: rgba(194, 168, 255, 0.24);
  border-radius: 999px;
  border: 2px solid transparent;
  background-clip: padding-box;
}

.chat-log::-webkit-scrollbar-thumb:hover,
.sql-box::-webkit-scrollbar-thumb:hover,
.table-wrap::-webkit-scrollbar-thumb:hover {
  background: rgba(194, 168, 255, 0.38);
  background-clip: padding-box;
}

@media (max-width: 980px) {
  .page {
    padding-inline: 0.8rem;
    gap: 0.8rem;
  }

  .panel {
    padding: 0.9rem;
    border-radius: 16px;
  }

  .chat-log {
    max-height: 360px;
  }

  .chat-log:empty {
    min-height: 64px;
    max-height: 360px;
  }

  .section-heading.split {
    align-items: flex-start;
  }

  .meta-pill {
    width: 100%;
    white-space: normal;
  }
}

@media (max-width: 700px) {
  .header-top {
    gap: 0.4rem;
  }

  .badge {
    font-size: 0.74rem;
    padding: 0.27rem 0.58rem;
  }

  .composer-meta {
    font-size: 0.8rem;
  }

  .actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.5rem;
  }

  .inline-actions {
    display: flex;
    width: 100%;
  }

  .inline-actions button {
    width: 100%;
  }

  button {
    width: 100%;
    justify-content: center;
  }

  .chat-item {
    max-width: 100%;
  }

  .chat-item.user,
  .chat-item.assistant {
    align-self: stretch;
  }

  .auth-actions {
    margin-top: 0.6rem;
  }

  th,
  td {
    padding: 0.5rem 0.55rem;
    font-size: 0.86rem;
  }
}

@media (max-width: 480px) {
  .actions {
    grid-template-columns: 1fr;
  }

  .header h1 {
    font-size: 1.25rem;
  }

  textarea {
    min-height: 104px;
  }

  .chat-log:empty {
    min-height: 56px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    scroll-behavior: auto !important;
  }

  button,
  textarea {
    transition: none;
  }
}
