/*
 * Feno web layer — minimal hand-written CSS.
 *
 * Authoritative design source: frontend/DESIGN.md
 * Tokens mirror frontend/src/lib/ui/tokens.css. If a value diverges, edit
 * DESIGN.md / tokens.css first, then mirror here. No raw hex outside this
 * token block.
 */

:root {
  color-scheme: light dark;

  /* Neutrals */
  --surface:           light-dark(#fbfbfa, #111311);
  --surface-raised:    light-dark(#ffffff, #181b18);
  --surface-hover:     light-dark(#f3f3f1, #222620);
  --surface-sunken:    light-dark(#f0f0ee, #0b0d0b);
  --border-subtle:     light-dark(#ecece9, #252a24);
  --border:            light-dark(#dddddb, #343a32);
  --border-strong:     light-dark(#c4c4bf, #4b5348);

  --on-surface-muted:  light-dark(#6f6f6b, #a1a89d);
  --on-surface:        light-dark(#2b2b29, #e1e5de);
  --on-surface-strong: light-dark(#0a0a0a, #fafcf8);

  --surface-translucent: color-mix(in srgb, var(--surface) 95%, transparent);
  --backdrop:            color-mix(in srgb, var(--on-surface-strong) 20%, transparent);

  /* Brand accent — Feno green */
  --primary:       light-dark(#489b6e, #78c997);
  --primary-hover: light-dark(#377f58, #93d8ad);
  --on-primary:    light-dark(#ffffff, #0b1f14);

  /* Semantic state */
  --success:    var(--primary);
  --warning:    light-dark(#eb5324, #ff8a5f);
  --error:      light-dark(#c83a3a, #ff7a7a);
  --error-bg:   light-dark(#fff1f0, #2a1614);
  --info:       light-dark(#2563eb, #8ab4ff);

  /* Radii */
  --radius-sm:   4px;
  --radius-md:   6px;
  --radius-lg:   8px;
  --radius-full: 9999px;

  /* Shadows — only two canonical tokens (see DESIGN.md §Elevation) */
  --shadow-sm:      0 1px 3px color-mix(in srgb, var(--on-surface-strong) 8%, transparent);
  --shadow-overlay: 0 4px 24px color-mix(in srgb, var(--on-surface-strong) 12%, transparent);

  --font-sans: 'Geist', Inter, -apple-system, BlinkMacSystemFont, system-ui, sans-serif;

  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  background: var(--surface);
  color: var(--on-surface);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--surface);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

a {
  color: var(--primary);
  text-decoration: none;
}
a:hover { text-decoration: underline; }

::selection {
  background: color-mix(in srgb, var(--primary) 20%, transparent);
  color: var(--on-surface-strong);
}

button { font: inherit; }

:focus-visible {
  outline: 1px solid var(--primary);
  outline-offset: 2px;
}

/* Shell ---------------------------------------------------------------- */

.shell {
  width: min(820px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 24px 0 48px;
}

/* Top nav --------------------------------------------------------------- */

.topnav {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  min-height: 32px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border-subtle);
  margin-bottom: 24px;
}
.topnav .brand {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  letter-spacing: -0.005em;
  color: var(--on-surface-strong);
}
.brand-logo {
  width: 22px;
  height: 22px;
  display: block;
}
.topnav-spacer { display: none; }
.topnav-auth {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 13px;
  line-height: 1.45;
}
.topnav-auth .principal { color: var(--on-surface-muted); }
.topnav-auth .role-badge {
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  background: var(--surface-hover);
  color: var(--on-surface-muted);
}
.topnav-auth .auth-link {
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--on-surface);
}
.topnav-auth .auth-link:hover {
  background: var(--surface-hover);
  text-decoration: none;
}

/* Flash ---------------------------------------------------------------- */

.flash {
  padding: 10px 14px;
  border-radius: var(--radius-md);
  margin-bottom: 16px;
  font-size: 13px;
  line-height: 1.45;
  border: 1px solid var(--border-subtle);
}
.flash-error {
  background: var(--error-bg);
  color: var(--error);
  border-color: var(--error);
}
.flash-info {
  background: var(--surface-raised);
  color: var(--info);
}

/* Cards & lists -------------------------------------------------------- */

.card {
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--surface-raised);
  padding: 12px 14px;
}
.card.empty { color: var(--on-surface-muted); }
.muted { color: var(--on-surface-muted); }

/* Typography ladder (see DESIGN.md §Typography) */

h1 {
  font-size: 20px;
  line-height: 1.3;
  font-weight: 600;
  letter-spacing: -0.005em;
  margin: 0;
  color: var(--on-surface-strong);
}
h2 {
  font-size: 16px;
  line-height: 1.35;
  font-weight: 600;
  margin: 0;
  color: var(--on-surface-strong);
}

.flow-index h1, .flow-show h1 { margin: 0 0 4px; }

/* Flow index ----------------------------------------------------------- */

.flow-list {
  list-style: none;
  margin: 16px 0 0;
  padding: 0;
  display: grid;
  gap: 8px;
}
.flow-card { padding: 0; }
.flow-card-link {
  display: block;
  padding: 12px 14px;
  color: var(--on-surface);
  text-decoration: none;
  border-radius: inherit;
}
.flow-card-link:hover {
  background: var(--surface-hover);
  text-decoration: none;
}
.flow-card-title {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
}
.flow-card-name {
  font-weight: 500;
  color: var(--on-surface-strong);
}
.flow-card-kind {
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--on-surface-muted);
  background: var(--surface-hover);
  padding: 2px 6px;
  border-radius: var(--radius-sm);
}
.flow-card-description {
  margin: 4px 0 0;
  color: var(--on-surface-muted);
  font-size: 13px;
  line-height: 1.45;
}
.flow-card-ref {
  display: inline-block;
  margin-top: 6px;
  font-size: 12px;
  line-height: 1.45;
  color: var(--on-surface-muted);
}

/* Flow detail ---------------------------------------------------------- */

.flow-show .back {
  font-size: 13px;
  color: var(--on-surface-muted);
  display: inline-block;
  margin-bottom: 12px;
}
.flow-show .lead {
  color: var(--on-surface-muted);
  margin: 4px 0 0;
  font-size: 14px;
}
.flow-show .meta {
  display: grid;
  grid-template-columns: max-content 1fr;
  column-gap: 16px;
  row-gap: 4px;
  margin: 12px 0 20px;
  font-size: 13px;
}
.flow-show .meta dt { color: var(--on-surface-muted); }
.flow-show .meta dd { margin: 0; }
.flow-show .placeholder { color: var(--on-surface-muted); }
.flow-show.error h1 { color: var(--error); }

.flow-show-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}
.flow-show-heading {
  min-width: 0;
  flex: 1;
}
.flow-header-actions {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  flex: none;
}

/* Icon button — ghost (see DESIGN.md §Components, "Button, ghost") ----- */

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--on-surface-muted);
  cursor: pointer;
}
.icon-button:hover {
  background: var(--surface-hover);
  color: var(--on-surface-strong);
}
.icon-button svg { display: block; }

/* Flow form ------------------------------------------------------------ */

.flow-form { display: grid; gap: 16px; }
.flow-form-fields {
  border: 0;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 14px;
}
.flow-form-fields[disabled] { opacity: 0.6; }

.field { display: grid; gap: 6px; }
.field-label {
  font-size: 13px;
  line-height: 1.4;
  font-weight: 500;
  color: var(--on-surface-strong);
}
.field-label .required { color: var(--error); margin-left: 2px; }
.field-description {
  margin: 0;
  font-size: 12px;
  color: var(--on-surface-muted);
  line-height: 1.45;
}

/* Inputs use surface-sunken per DESIGN.md §Colors */
.field input[type="text"],
.field input[type="number"],
.field select,
.field textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-sunken);
  color: var(--on-surface);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
}
.field input[type="text"]::placeholder,
.field input[type="number"]::placeholder,
.field textarea::placeholder {
  color: var(--on-surface-muted);
  opacity: 0.7;
}
.field input[type="text"]:hover,
.field input[type="number"]:hover,
.field select:hover,
.field textarea:hover {
  border-color: var(--border-strong);
}
.field input[type="text"]:focus,
.field input[type="number"]:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--primary);
}
.field textarea {
  display: block;
  min-height: 112px;
  padding: 10px 12px;
  resize: vertical;
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.55;
}

.field-error {
  margin: 0;
  color: var(--error);
  font-size: 12px;
}
.field-errors {
  list-style: none;
  padding: 0;
  margin: 4px 0 0;
  color: var(--error);
  font-size: 12px;
}
.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}
.checkbox-row input[type="checkbox"] {
  accent-color: var(--primary);
  width: 15px;
  height: 15px;
}

/* File upload --------------------------------------------------------- */

.file-upload {
  display: grid;
  gap: 8px;
}
/* Hide the native file input; the label is the clickable trigger. */
.file-upload-native {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.file-upload-trigger {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 1px dashed var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-sunken);
  color: var(--on-surface);
  font-size: 13px;
  line-height: 1.4;
  cursor: pointer;
  transition: border-color 80ms linear, background 80ms linear;
}
.file-upload-trigger:hover {
  border-color: var(--primary);
  background: var(--surface-hover);
}
.file-upload-trigger:focus-within {
  outline: 1px solid var(--primary);
  outline-offset: 2px;
}
.file-upload-trigger-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--on-surface-muted);
  flex: none;
}
.file-upload-trigger-label {
  font-weight: 500;
  color: var(--on-surface-strong);
}
.file-upload-trigger-hint {
  color: var(--on-surface-muted);
  font-size: 12px;
  margin-left: auto;
}


.flow-form-settings-body {
  display: grid;
  gap: 14px;
}

/* Settings modal (see DESIGN.md §Components, §Elevation) --------------- */

.flow-settings-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.flow-settings-modal.hidden { display: none; }

.flow-settings-modal-backdrop {
  position: absolute;
  inset: 0;
  background: var(--backdrop);
}
.flow-settings-modal-dialog {
  position: relative;
  z-index: 1;
  width: min(520px, 100%);
  max-height: calc(100vh - 48px);
  display: flex;
  flex-direction: column;
  background: var(--surface-raised);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-overlay);
  overflow: hidden;
}
.flow-settings-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border-subtle);
}
.flow-settings-modal-header h2 {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--on-surface-muted);
  margin: 0;
}
.flow-settings-modal-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  background: transparent;
  border: 0;
  color: var(--on-surface-muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  border-radius: var(--radius-sm);
}
.flow-settings-modal-close:hover {
  background: var(--surface-hover);
  color: var(--on-surface-strong);
}
.flow-settings-modal-body {
  padding: 14px;
  overflow-y: auto;
  display: grid;
  gap: 14px;
}

/* Flow definition — "factory line" view ------------------------------- */

.flow-def-section {
  border-top: 1px solid var(--border-subtle);
  padding-top: 12px;
  display: grid;
  gap: 8px;
}
.flow-def-heading {
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--on-surface-muted);
  margin: 0;
}
.flow-def-line {
  list-style: none;
  margin: 0;
  padding: 4px 2px 8px;
  display: flex;
  align-items: stretch;
  gap: 0;
  overflow-x: auto;
  scrollbar-width: thin;
}
.flow-def-step {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: none;
}
.flow-def-step + .flow-def-step {
  margin-left: 8px;
}
.flow-def-arrow {
  color: var(--on-surface-muted);
  font-size: 14px;
  line-height: 1;
  flex: none;
}
.flow-def-card {
  display: grid;
  gap: 2px;
  min-width: 96px;
  padding: 8px 10px;
  background: var(--surface-raised);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
}
.flow-def-card-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--on-surface-strong);
  line-height: 1.3;
}
.flow-def-card-ref {
  font-size: 11px;
  color: var(--on-surface-muted);
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 200px;
}
.flow-settings-modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--border-subtle);
}
.flow-settings-modal-footer button {
  padding: 5px 12px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--on-surface);
  border-radius: var(--radius-md);
  cursor: pointer;
  font-size: 13px;
}
.flow-settings-modal-footer button:hover {
  background: var(--surface-hover);
}

/* Form actions --------------------------------------------------------- */

.flow-form-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 4px;
}
.flow-form-actions button {
  padding: 7px 14px;
  border: 1px solid var(--primary);
  background: var(--primary);
  color: var(--on-primary);
  border-radius: var(--radius-md);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
.flow-form-actions button:hover:not(:disabled) {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}
.flow-form-actions button.secondary {
  background: transparent;
  color: var(--on-surface);
  border-color: var(--border);
}
.flow-form-actions button.secondary:hover:not(:disabled) {
  background: var(--surface-hover);
}
.flow-form-actions button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
.flow-form-actions .status {
  color: var(--on-surface-muted);
  font-size: 12px;
}

.flow-error {
  margin-top: 12px;
  background: var(--error-bg);
  color: var(--error);
  padding: 10px 12px;
  border: 1px solid var(--error);
  border-radius: var(--radius-md);
  font-size: 13px;
}

/* Output --------------------------------------------------------------- */

.flow-output {
  margin-top: 18px;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--surface-raised);
  padding: 12px 14px;
}
.flow-output-header,
.message-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}
.flow-output h2 {
  font-size: 11px;
  margin: 0;
  color: var(--on-surface-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 500;
}
.copy-button {
  padding: 3px 9px;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--on-surface-muted);
  cursor: pointer;
  font: inherit;
  font-size: 12px;
}
.copy-button:hover {
  background: var(--surface-hover);
  color: var(--on-surface-strong);
}
.message-header {
  color: var(--on-surface-muted);
  font-size: 12px;
}
.flow-output pre {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.55;
  color: var(--on-surface);
}
.flow-output.live pre { color: var(--on-surface-muted); }
.flow-output.inline {
  margin-top: 0;
  border: 0;
  padding: 0;
  background: transparent;
}

/* Interface-native runner polish --------------------------------------- */

.flow-show {
  max-width: 860px;
  margin: 0 auto;
}

.flow-show-header {
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border-subtle);
}

.flow-form,
.chat-panel {
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--surface-raised);
}

.flow-form {
  padding: 12px;
}

.file-upload-trigger {
  min-height: 72px;
  border-radius: var(--radius-md);
}

.chat-panel {
  display: grid;
  overflow: hidden;
}

.chat-transcript {
  list-style: none;
  margin: 0;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 320px;
  max-height: min(560px, calc(100vh - 240px));
  overflow-y: auto;
  background: var(--surface-raised);
}

.chat-message {
  max-width: min(72ch, 88%);
  padding: 8px 10px;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--surface-sunken);
}

.chat-user {
  align-self: flex-end;
  background: var(--surface-hover);
  color: var(--on-surface-strong);
}

.chat-assistant {
  align-self: flex-start;
}

.chat-assistant.pending {
  color: var(--on-surface-muted);
}

.chat-text {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.55;
}

.chat-tools {
  list-style: none;
  margin: 0;
  padding: 8px 12px 0;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chat-tool-notice {
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  background: var(--surface-hover);
  color: var(--on-surface-muted);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
}

.chat-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: end;
  padding: 12px;
  border-top: 1px solid var(--border-subtle);
  background: var(--surface-raised);
}

.chat-input {
  width: 100%;
  min-height: 44px;
  max-height: 144px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-raised);
  color: var(--on-surface);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.45;
  resize: vertical;
}

.chat-input:focus {
  outline: none;
  border-color: var(--primary);
}

.chat-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.chat-actions button {
  min-width: 64px;
  padding: 8px 12px;
  border: 1px solid var(--primary);
  border-radius: var(--radius-md);
  background: var(--primary);
  color: var(--on-primary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}

.chat-actions button:hover:not(:disabled) {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

.chat-actions button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.chat-actions .status,
.flow-show > .status {
  color: var(--on-surface-muted);
  font-size: 12px;
}

@media (max-width: 720px) {
  .chat-form {
    grid-template-columns: 1fr;
  }

  .chat-actions {
    justify-content: space-between;
  }

  .chat-message {
    max-width: 94%;
  }
}
