:root {
  color-scheme: light dark;
  --portal-bg: #f4f5f7;
  --portal-card-bg: #ffffff;
  --portal-text: #1c1e21;
  --portal-muted: #6b7280;
  --portal-border: #d9dce1;
  --portal-accent: #3478f6;
  --portal-accent-text: #ffffff;
  --portal-error: #b3261e;
  --portal-success: #1e7a34;
}

@media (prefers-color-scheme: dark) {
  :root {
    --portal-bg: #15171a;
    --portal-card-bg: #1f2226;
    --portal-text: #e7e9ec;
    --portal-muted: #9aa0a8;
    --portal-border: #333740;
    --portal-accent: #5b93ff;
    --portal-accent-text: #0b0d10;
    --portal-error: #ff8a80;
    --portal-success: #6bd18a;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--portal-bg);
  color: var(--portal-text);
}

.portal-wrap {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 2rem 1rem;
}

.portal-header h1 {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--portal-muted);
  margin: 0 0 1.5rem;
  text-align: center;
}

.portal-card {
  width: 100%;
  max-width: 26rem;
  background: var(--portal-card-bg);
  border: 1px solid var(--portal-border);
  border-radius: 0.75rem;
  padding: 1.75rem;
}

.portal-card h2 {
  margin-top: 0;
}

form {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

label {
  font-size: 0.85rem;
  font-weight: 600;
  margin-top: 0.75rem;
}

input, select, textarea {
  font: inherit;
  color: inherit;
  background: transparent;
  border: 1px solid var(--portal-border);
  border-radius: 0.4rem;
  padding: 0.5rem 0.6rem;
}

textarea {
  resize: vertical;
}

.portal-scroll-textarea {
  height: 10rem;
  overflow-y: auto;
}

.portal-fieldset {
  border: 1px solid var(--portal-border);
  border-radius: 0.5rem;
  margin-top: 0.75rem;
  padding: 0.75rem 1rem 1rem;
}

.portal-fieldset legend {
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0 0.4rem;
}

.portal-radio-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 400;
  margin-top: 0.6rem;
}

.portal-radio-label input {
  margin: 0;
  padding: 0;
  width: auto;
}

.portal-suboptions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0.4rem 0 0 1.6rem;
}

.portal-suboptions label {
  margin-top: 0;
  font-weight: 400;
}

/* Unlike .portal-suboptions (Day/Date, indented under their own frequency),
   Time applies to both weekly and monthly alike, so it's styled flush with
   the frequency radio rows rather than nested under either one. */
.portal-time-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0.6rem 0 0 0;
}

.portal-time-row label {
  margin-top: 0;
  font-weight: 400;
}

.portal-inline-form {
  margin-top: 0.75rem;
}

.portal-view-as-form {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.portal-view-as-form label {
  margin: 0;
  font-weight: 400;
  font-size: 0.9rem;
  color: var(--portal-muted);
}

.portal-view-as-form select {
  font: inherit;
}

.portal-view-as-form button {
  margin-top: 0;
}

button {
  margin-top: 1.25rem;
  font: inherit;
  font-weight: 600;
  padding: 0.6rem 1rem;
  border: none;
  border-radius: 0.4rem;
  background: var(--portal-accent);
  color: var(--portal-accent-text);
  cursor: pointer;
}

button.portal-secondary {
  background: transparent;
  color: var(--portal-muted);
  border: 1px solid var(--portal-border);
}

.portal-logout-form {
  margin-top: 1rem;
}

.portal-footer-link {
  margin-top: 1.5rem;
  font-size: 0.9rem;
  color: var(--portal-muted);
  text-align: center;
}

.portal-footer-link a {
  color: var(--portal-accent);
  text-decoration: none;
}

.portal-error {
  color: var(--portal-error);
  font-size: 0.9rem;
}

.portal-success {
  color: var(--portal-success);
  font-size: 0.9rem;
}

/* Save confirmation: pinned to the top of the viewport so it's seen however
   far down the form the Save button was, and fades out on its own after ~3s
   (the CSS animation alone does that; portal.js then drops it from the DOM). */
.portal-toast {
  position: fixed;
  top: 1rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  max-width: calc(100% - 2rem);
  padding: 0.7rem 1.4rem;
  background: var(--portal-success);
  color: var(--portal-card-bg);
  font-weight: 600;
  border-radius: 0.5rem;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  pointer-events: none;
  animation: portal-toast 3.3s ease forwards;
}

@keyframes portal-toast {
  0% { opacity: 0; transform: translate(-50%, -0.5rem); }
  8%, 88% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, -0.5rem); visibility: hidden; }
}

@media (prefers-reduced-motion: reduce) {
  .portal-toast {
    animation-name: portal-toast-fade;
  }

  @keyframes portal-toast-fade {
    0%, 90% { opacity: 1; }
    100% { opacity: 0; visibility: hidden; }
  }
}

.portal-hint {
  color: var(--portal-muted);
  font-size: 0.8rem;
  margin: 0;
}

/* --- Device tokens / phone app (/account/devices) --- */

.portal-section-heading {
  margin-top: 2rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--portal-border);
}

.portal-download {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin: 1rem 0;
}

.portal-download-link {
  align-self: flex-start;
  font-weight: 600;
  text-decoration: none;
  padding: 0.6rem 1rem;
  border-radius: 0.4rem;
  background: var(--portal-accent);
  color: var(--portal-accent-text);
}

.portal-token-reveal {
  margin: 1rem 0;
  padding: 0.75rem 1rem 1rem;
  border: 1px solid var(--portal-success);
  border-radius: 0.5rem;
}

.portal-token-row {
  display: flex;
  gap: 0.4rem;
  margin: 0.6rem 0;
}

.portal-token-row input {
  flex: 1;
  min-width: 0;
  /* The token is a 43-char urlsafe blob; monospace makes a hand-copied
     character easier to check, and it must not wrap the layout. */
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85rem;
}

.portal-token-row button {
  margin-top: 0;
  white-space: nowrap;
}

.portal-token-list {
  list-style: none;
  margin: 1rem 0 0;
  padding: 0;
}

.portal-token-item {
  padding: 0.75rem 0;
  border-top: 1px solid var(--portal-border);
}

.portal-token-item:last-child {
  border-bottom: 1px solid var(--portal-border);
}

.portal-token-revoked {
  opacity: 0.55;
}

.portal-token-name {
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.portal-token-badge {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--portal-muted);
  border: 1px solid var(--portal-border);
  border-radius: 0.25rem;
  padding: 0.05rem 0.35rem;
}

.portal-token-revoke-form {
  margin-top: 0.4rem;
}

.portal-token-revoke-form button {
  margin-top: 0;
  padding: 0.3rem 0.7rem;
  font-size: 0.8rem;
  font-weight: 400;
}

.portal-password-field {
  position: relative;
  display: flex;
}

.portal-password-field input {
  flex: 1;
  padding-right: 2.25rem;
}

.portal-password-toggle {
  position: absolute;
  right: 0.35rem;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.75rem;
  height: 1.75rem;
  margin: 0;
  padding: 0;
  border: none;
  border-radius: 0.3rem;
  background: transparent;
  color: var(--portal-muted);
  cursor: pointer;
}

.portal-password-toggle:hover,
.portal-password-toggle:focus-visible {
  color: var(--portal-text);
}

.portal-password-toggle:focus-visible {
  outline: 2px solid var(--portal-accent);
  outline-offset: 1px;
}

/* --- Custom Query chat (/account/profile) --- */

/* Used for speaker labels and composer labels that are in the markup for a
   screen reader but not on screen. The clip-path/1px recipe rather than
   display:none, which removes the element from the accessibility tree too. */
.portal-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.portal-chat {
  /* Wider than .portal-card: the card is a form at 26rem, while this is prose
     to read. ~34rem keeps answer lines near the 65-75 character span that
     stays comfortable to read. */
  width: min(34rem, calc(100vw - 2rem));
  max-height: min(42rem, calc(100vh - 3rem));
  padding: 0;
  border: 1px solid var(--portal-border);
  border-radius: 0.75rem;
  background: var(--portal-card-bg);
  color: var(--portal-text);
  overflow: hidden;
}

.portal-chat::backdrop {
  background: rgba(0, 0, 0, 0.45);
}

/* The dialog is a column: header and footer fixed, transcript the only part
   that scrolls. Without this the whole dialog grows and the composer walks
   off the bottom of the screen mid-conversation.

   Scoped to [open] deliberately. A bare `display: flex` on a <dialog>
   overrides the UA stylesheet's `display: none` for the closed state, and the
   panel would then be on the page from first paint. */
.portal-chat[open] {
  display: flex;
  flex-direction: column;
}

.portal-chat-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 1rem 1.25rem 0.75rem;
  border-bottom: 1px solid var(--portal-border);
}

.portal-chat-header h2 {
  margin: 0 0 0.15rem;
  font-size: 1.05rem;
}

.portal-chat-dismiss {
  margin: 0;
  padding: 0 0.4rem;
  font-size: 1.4rem;
  line-height: 1;
  background: transparent;
  color: var(--portal-muted);
  border: none;
  cursor: pointer;
}

.portal-chat-dismiss:hover,
.portal-chat-dismiss:focus-visible {
  color: var(--portal-text);
}

.portal-chat-log {
  flex: 1 1 auto;
  min-height: 8rem;
  overflow-y: auto;
  padding: 1rem 1.25rem 0.5rem;
  /* overscroll containment stops a flick at the end of the transcript from
     scrolling the page behind the dialog on a phone. */
  overscroll-behavior: contain;
}

.portal-chat-log:focus-visible {
  outline: 2px solid var(--portal-accent);
  outline-offset: -2px;
}

.portal-chat-welcome p {
  margin: 0 0 0.6rem;
  font-size: 0.9rem;
}

.portal-chat-chips-label {
  margin: 0.9rem 0 0.4rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--portal-muted);
}

.portal-chat-chips {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.portal-chat-chip {
  margin: 0;
  width: 100%;
  text-align: left;
  font-weight: 400;
  font-size: 0.85rem;
  padding: 0.5rem 0.7rem;
  background: transparent;
  color: var(--portal-text);
  border: 1px solid var(--portal-border);
  border-radius: 0.5rem;
  cursor: pointer;
}

.portal-chat-chip:hover,
.portal-chat-chip:focus-visible {
  border-color: var(--portal-accent);
}

/* Full-width turns rather than chat bubbles: bubbles read as a messenger and
   undermine the "this is a tool looking at my data" framing. The question is
   distinguished by a rule and weight, not by alignment. */
.portal-chat-turn {
  margin-bottom: 1.1rem;
}

.portal-chat-turn-user .portal-chat-body {
  font-weight: 600;
  padding-left: 0.75rem;
  border-left: 3px solid var(--portal-border);
  white-space: pre-wrap;
}

.portal-chat-turn-assistant .portal-chat-body {
  /* pre-wrap while streaming plain text; the Markdown pass that runs when the
     turn completes replaces this content with real <p>/<ul> elements. */
  white-space: pre-wrap;
  font-size: 0.95rem;
  line-height: 1.5;
}

.portal-chat-turn-assistant .portal-chat-body p,
.portal-chat-turn-assistant .portal-chat-body ul {
  white-space: normal;
  margin: 0 0 0.7rem;
}

.portal-chat-turn-assistant .portal-chat-body ul {
  padding-left: 1.2rem;
}

.portal-chat-turn-assistant .portal-chat-body li {
  margin-bottom: 0.3rem;
}

.portal-chat-sources {
  margin-top: 0.4rem;
  font-size: 0.8rem;
  color: var(--portal-muted);
}

.portal-chat-sources summary {
  cursor: pointer;
}

.portal-chat-sources ul {
  margin: 0.4rem 0 0;
  padding-left: 1.2rem;
}

.portal-chat-sources a {
  color: var(--portal-accent);
}

.portal-chat-jump {
  align-self: center;
  margin: 0 0 0.4rem;
  padding: 0.3rem 0.8rem;
  font-size: 0.8rem;
  font-weight: 400;
}

.portal-chat-progress {
  margin: 0;
  padding: 0 1.25rem 0.5rem;
  font-size: 0.8rem;
  color: var(--portal-muted);
  /* A long reasoning summary must not push the composer around, so it is
     clamped to two lines and ellipsised. */
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.portal-chat-error {
  margin: 0 1.25rem 0.5rem;
  padding: 0.5rem 0.7rem;
  font-size: 0.85rem;
  color: var(--portal-error);
  border: 1px solid var(--portal-error);
  border-radius: 0.4rem;
}

.portal-chat-composer {
  gap: 0.4rem;
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--portal-border);
}

.portal-chat-composer textarea {
  width: 100%;
  resize: vertical;
  min-height: 3rem;
  max-height: 9rem;
}

.portal-chat-send-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
}

.portal-chat-send-row .portal-hint {
  margin-right: auto;
}

.portal-chat-send-row button {
  margin-top: 0;
  /* 44px minimum touch target. */
  min-height: 2.75rem;
}

.portal-chat-confirm {
  margin: 0;
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--portal-border);
  background: var(--portal-bg);
}

.portal-chat-confirm p {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
}

.portal-chat-confirm-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
}

.portal-chat-confirm-actions button {
  margin-top: 0;
}

.portal-chat-footer {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.75rem 1.25rem 1rem;
  border-top: 1px solid var(--portal-border);
}

.portal-chat-footer button {
  margin-top: 0;
  flex: 1 1 auto;
  min-height: 2.75rem;
}

@media (max-width: 30rem) {
  .portal-chat {
    width: 100vw;
    max-width: 100vw;
    max-height: 100vh;
    height: 100vh;
    border-radius: 0;
    border: none;
  }
}
