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

html {
  height: 100%;
  background: #0f4c6e;
  color: #102433;
  -webkit-text-size-adjust: 100%;
}

html.has-session,
html.has-session body {
  background: #e7eef2;
}

html.has-session .gate {
  display: none;
}

body {
  margin: 0;
  min-height: 100%;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  font-size: 1rem;
  line-height: 1.45;
}

body.in-app {
  height: 100%;
  overflow: hidden;
}

button,
input,
textarea,
select {
  font: inherit;
  -webkit-tap-highlight-color: transparent;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: default;
  opacity: 0.55;
}

:focus {
  outline: none;
}

:focus-visible {
  outline: 3px solid #f4e7c8;
  outline-offset: 2px;
}

[hidden] {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.gate {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    calc(1.25rem + env(safe-area-inset-top))
    calc(1.25rem + env(safe-area-inset-right))
    calc(1.25rem + env(safe-area-inset-bottom))
    calc(1.25rem + env(safe-area-inset-left));
}

.gate-form {
  width: min(100%, 22rem);
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.gate .field {
  color: #f7fbfc;
}

.gate .field input {
  border: 0;
  background: #f7fbfc;
}

.gate-form button {
  min-height: 3rem;
  border: 0;
  border-radius: 0.7rem;
  background: #f4e7c8;
  color: #102433;
  font-size: 1rem;
  font-weight: 600;
}

.gate :focus-visible {
  outline-color: #fff;
}

.gate .form-error {
  color: #ffe1e1;
}

.shell {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  position: relative;
  background: #e7eef2;
}

.top {
  height: calc(3.25rem + env(safe-area-inset-top));
  padding:
    env(safe-area-inset-top)
    calc(0.35rem + env(safe-area-inset-right))
    0
    calc(0.35rem + env(safe-area-inset-left));
  display: grid;
  grid-template-columns: 2.75rem 1fr 2.75rem;
  align-items: center;
  background: #0f4c6e;
  color: #f7fbfc;
  flex: none;
}

.brand {
  margin: 0;
  text-align: center;
  font-size: 1.05rem;
  font-weight: 700;
}

.icon-button {
  width: 2.75rem;
  height: 2.75rem;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 0.7rem;
  background: transparent;
  color: inherit;
  padding: 0;
  justify-self: center;
}

.icon-button svg {
  width: 1.5rem;
  height: 1.5rem;
}

.icon-button[aria-expanded='true'] {
  background: rgba(255, 255, 255, 0.16);
}

.menu {
  position: absolute;
  z-index: 6;
  top: calc(3.25rem + env(safe-area-inset-top));
  right: calc(0.5rem + env(safe-area-inset-right));
  left: calc(0.5rem + env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.35rem;
  background: #f7fbfc;
  color: #102433;
  border: 1px solid #c5d4de;
  border-radius: 0.8rem;
  box-shadow: 0 12px 32px rgba(16, 36, 51, 0.16);
}

.menu button {
  width: 100%;
  min-height: 3rem;
  text-align: left;
  border: 0;
  border-radius: 0.6rem;
  background: transparent;
  color: inherit;
  font-size: 1rem;
  padding: 0.65rem 0.8rem;
}

.menu button:hover,
.menu button:focus-visible {
  background: #e7eef2;
}

.menu button[data-view='logout'] {
  border-top: 1px solid #c5d4de;
  margin-top: 0.2rem;
}

.menu :focus-visible,
.history :focus-visible,
.page :focus-visible,
.composer :focus-visible {
  outline-color: #0f4c6e;
}

.workspace {
  flex: 1;
  min-height: 0;
  display: flex;
  position: relative;
}

.scrim {
  position: absolute;
  inset: 0;
  z-index: 3;
  border: 0;
  padding: 0;
  background: rgba(16, 36, 51, 0.35);
}

.view {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.history {
  z-index: 4;
  flex: none;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: #f7fbfc;
  color: #102433;
}

.history-head,
.history-foot {
  flex: none;
  padding: 0.75rem;
  padding-left: calc(0.75rem + env(safe-area-inset-left));
}

.history-head {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  border-bottom: 1px solid #c5d4de;
}

.history-head h2,
.history-empty,
.form-error,
.form-status,
.note,
.page p {
  margin: 0;
}

.history-head h2 {
  font-size: 1.05rem;
}

.history-head .text-button {
  align-self: flex-start;
  text-align: left;
}

.history-list {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 0.4rem;
  padding-left: calc(0.4rem + env(safe-area-inset-left));
}

.history-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  margin-bottom: 0.25rem;
}

.history-item {
  flex: 1 1 8rem;
  min-height: 3rem;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  text-align: left;
  border: 0;
  border-radius: 0.6rem;
  background: transparent;
  color: inherit;
  padding: 0.45rem 0.55rem;
}

.history-row[data-active='true'] .history-item {
  background: #d7e6f0;
}

.history-title {
  overflow-wrap: anywhere;
}

.history-time,
.history-empty,
.note,
.meta {
  color: #4d6270;
}

.history-time,
.meta {
  font-size: 0.85rem;
}

.meta {
  display: block;
}

.history-delete,
.text-button {
  min-height: 2.75rem;
  border: 0;
  border-radius: 0.6rem;
  background: transparent;
  color: #0f4c6e;
  font-size: 0.95rem;
  padding: 0.4rem 0.55rem;
}

.history-delete.icon-button {
  flex: 0 0 auto;
  min-width: 3rem;
  min-height: 3rem;
  padding: 0.5rem;
  border-radius: 0.65rem;
  color: #8d2b2b;
}

.history-delete.icon-button svg {
  width: 1.35rem;
  height: 1.35rem;
}

.history-delete,
.danger {
  color: #8d2b2b;
}

.confirm {
  flex: 1 1 100%;
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
  padding: 0.15rem 0.35rem 0.4rem;
}

.history-foot {
  border-top: 1px solid #c5d4de;
  padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
}

.history-foot .text-button {
  width: 100%;
  text-align: left;
  white-space: normal;
}

.chat {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.messages {
  flex: 1;
  min-height: 0;
  overflow: auto;
  list-style: none;
  margin: 0;
  padding: 0.75rem;
  padding-left: calc(0.75rem + env(safe-area-inset-left));
  padding-right: calc(0.75rem + env(safe-area-inset-right));
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.welcome {
  align-self: flex-start;
  max-width: 36rem;
  color: #4d6270;
}

.bubble {
  max-width: min(36rem, 85%);
  padding: 0.7rem 0.8rem;
  border-radius: 1rem;
  overflow-wrap: anywhere;
}

.bubble-user {
  align-self: flex-end;
  background: #0f4c6e;
  color: #f7fbfc;
  border-bottom-right-radius: 0.3rem;
}

.bubble-assistant {
  align-self: flex-start;
  background: #f7fbfc;
  border: 1px solid #c5d4de;
  border-bottom-left-radius: 0.3rem;
}

.bubble-error {
  align-self: flex-start;
  background: #fdecef;
  color: #6f1d2a;
  border: 1px solid #f0c2c8;
}

.bubble-pending {
  align-self: flex-start;
  max-width: min(36rem, 92%);
  padding: 0;
  color: #2f4c5c;
}

.working-indicator {
  display: flex;
  gap: 0.65rem;
  align-items: flex-start;
  padding: 0.75rem 0.85rem;
  background: #eef6fa;
  border: 1px solid #b8d4e2;
  border-radius: 0.9rem;
}

.working-indicator-inline {
  margin: 0;
}

.working-indicator-pulse {
  flex: none;
  width: 0.75rem;
  height: 0.75rem;
  margin-top: 0.35rem;
  border-radius: 999px;
  background: #0f4c6e;
  animation: working-pulse 1.2s ease-in-out infinite;
}

.working-indicator-text {
  margin: 0;
  font-size: 0.98rem;
  line-height: 1.45;
  color: #2f4c5c;
}

@keyframes working-pulse {
  0%,
  100% {
    transform: scale(0.92);
    opacity: 0.55;
  }
  50% {
    transform: scale(1);
    opacity: 1;
  }
}

.bubble a {
  color: inherit;
  font-weight: 600;
}

.bubble-assistant a {
  color: #0f4c6e;
}

.composer {
  flex: none;
  position: sticky;
  bottom: 0;
  z-index: 3;
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  padding: 0.6rem;
  padding-bottom: calc(0.6rem + env(safe-area-inset-bottom, 0px));
  padding-left: calc(0.6rem + env(safe-area-inset-left, 0px));
  padding-right: calc(0.6rem + env(safe-area-inset-right, 0px));
  background: #f7fbfc;
  border-top: 1px solid #c5d4de;
  box-shadow: 0 -6px 18px rgba(16, 36, 51, 0.08);
}

.composer textarea {
  flex: 1;
  width: 100%;
  resize: none;
  min-height: 3rem;
  max-height: 8.5rem;
  font-size: 1rem;
}

.composer button,
.primary {
  min-height: 3rem;
  border: 0;
  border-radius: 0.7rem;
  background: #0f4c6e;
  color: #f7fbfc;
  font-size: 1rem;
  font-weight: 600;
}

.composer button {
  min-width: 5.5rem;
}

.bubble a.download-link {
  font-weight: 600;
  text-decoration-thickness: 2px;
}

.page {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding:
    1rem
    calc(1rem + env(safe-area-inset-right))
    calc(1.25rem + env(safe-area-inset-bottom))
    calc(1rem + env(safe-area-inset-left));
}

.page-toolbar {
  margin: 0 0 0.75rem;
  display: flex;
  align-items: stretch;
}

.page-back {
  margin: 0;
  min-height: 3rem;
  min-width: 6.5rem;
  padding: 0.65rem 1.1rem;
  border: 1px solid #b8cad6;
  border-radius: 0.85rem;
  background: #fff;
  color: #0f4c6e;
  font-size: 1.05rem;
  font-weight: 600;
  text-align: center;
  box-shadow: 0 1px 0 rgba(16, 36, 51, 0.06);
  touch-action: manipulation;
}

.page-back:hover,
.page-back:focus-visible {
  background: #f4f8fa;
  border-color: #0f4c6e;
}

.page h1 {
  margin: 0 0 0.75rem;
  font-size: 1.35rem;
}

.page p {
  margin-bottom: 0.85rem;
  overflow-wrap: anywhere;
}

.note {
  margin-bottom: 1rem;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  width: min(100%, 28rem);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.95rem;
}

.field input,
.field select,
.composer textarea {
  font-size: 1rem;
  color: #102433;
  background: #fff;
  border: 1px solid #c5d4de;
  border-radius: 0.7rem;
  min-height: 3rem;
  padding: 0.65rem 0.75rem;
}

.field input,
.field select {
  width: 100%;
}

.form-status {
  min-height: 1.4rem;
}

.form-status[data-state='working'] {
  color: #2f4c5c;
}

.form-error,
.form-status[data-state='error'] {
  color: #8d2b2b;
}

.upload-field {
  display: block;
}

.upload-step {
  margin: 0.65rem 0 0;
  color: #2f4c5c;
  font-size: 0.98rem;
  line-height: 1.45;
}

.upload-step-done {
  color: #1f5c48;
  font-weight: 600;
}

.upload-queue-list {
  list-style: none;
  margin: 0.65rem 0 0;
  padding: 0;
  border: 1px solid #c5d4de;
  border-radius: 0.65rem;
  background: #f8fbfd;
}

.upload-queue-item {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.35rem 0.65rem;
  padding: 0.45rem 0.6rem;
  border-bottom: 1px solid #d8e3ea;
  font-size: 0.92rem;
}

.upload-queue-item:last-child {
  border-bottom: none;
}

.upload-queue-name {
  flex: 1 1 12rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.upload-queue-state {
  flex: 1 1 10rem;
  color: #4a5a66;
  font-size: 0.88rem;
  line-height: 1.35;
}

.upload-queue-item[data-queue-state='active'] .upload-queue-name {
  color: #0b4d6c;
}

.upload-queue-item[data-queue-state='ok'] .upload-queue-state {
  color: #1f5c48;
  font-weight: 600;
}

.upload-queue-item[data-queue-state='error'] .upload-queue-state {
  color: #8d2b2b;
}

.upload-queue-item[data-queue-state='skip'] .upload-queue-state {
  color: #6b4e12;
}

.brain-files {
  margin-bottom: 1.25rem;
}

.section-subtitle {
  margin: 0 0 0.5rem;
  font-size: 1rem;
  font-weight: 600;
}

.brain-files-list {
  list-style: none;
  margin: 0.35rem 0 0;
  padding: 0;
  border: 1px solid #c5d4de;
  border-radius: 0.65rem;
  background: #f8fbfd;
}

.brain-files-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 0.55rem;
  border-bottom: 1px solid #d8e3ea;
}

.brain-files-item:last-child {
  border-bottom: none;
}

.brain-files-name {
  flex: 1 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.95rem;
}

a.brain-files-name.download-link {
  color: #0f3d52;
  font-weight: 600;
  text-decoration: underline;
  text-decoration-thickness: 2px;
  cursor: pointer;
}

a.brain-files-name.download-link:hover {
  color: #0b4d6c;
}

.brain-files-empty {
  padding: 0.65rem 0.75rem;
  color: #4a5a66;
  font-size: 0.95rem;
}

.brain-files-delete.icon-button {
  flex: 0 0 auto;
  min-width: 2.75rem;
  min-height: 2.75rem;
  padding: 0.45rem;
  border-radius: 0.65rem;
  color: #8d2b2b;
}

.upload-progress {
  margin-top: 0.65rem;
}

.user-admin-list {
  display: grid;
  gap: 0.75rem;
  margin-top: 1.25rem;
}

.user-admin-card {
  border: 1px solid #c5d4de;
  border-radius: 0.9rem;
  background: #fff;
  padding: 0.85rem 1rem;
}

.user-admin-card h2 {
  margin: 0 0 0.35rem;
  font-size: 1.05rem;
}

.user-admin-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  margin-top: 0.65rem;
}

.user-admin-actions select {
  min-height: 2.75rem;
}

.file-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem 0.7rem;
  align-items: center;
  min-height: 3rem;
  padding: 0.35rem 0.45rem;
  border: 1px solid #c5d4de;
  border-radius: 0.7rem;
  background: #fff;
}

.file-picker .text-button {
  background: #e7eef2;
  min-height: 2.5rem;
}

.file-name {
  overflow-wrap: anywhere;
}

.primary {
  width: 100%;
}

@media (min-width: 480px) {
  .menu {
    left: auto;
    width: min(22rem, calc(100vw - 1rem));
  }
}

@media (max-width: 839px) {
  .history:not([hidden]) {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(19rem, 86vw);
    box-shadow: 8px 0 24px rgba(16, 36, 51, 0.18);
  }
}

@media (min-width: 840px) {
  .history:not([hidden]) {
    width: 18.5rem;
    border-right: 1px solid #c5d4de;
  }
}

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