/*
 * Design-system component classes — a CSS port of the mockup's inline styles.
 * Every value references a token from tokens/*.css.
 */

/* ---- Buttons ------------------------------------------------------------ */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font-family: var(--font-sans); font-weight: var(--weight-semibold);
  font-size: var(--font-size-body-sm); line-height: 1;
  height: 44px; padding: 0 18px;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: background var(--duration-fast) var(--ease-standard),
              color var(--duration-fast) var(--ease-standard),
              border-color var(--duration-fast) var(--ease-standard);
}
.btn:disabled, .btn[aria-disabled="true"] { opacity: .5; cursor: not-allowed; }
.btn-sm { height: 38px; padding: 0 14px; font-size: var(--font-size-caption); }
.btn-lg { height: 50px; padding: 0 24px; font-size: var(--font-size-body); }

.btn-primary { background: var(--color-accent); color: var(--color-text-on-accent); }
.btn-primary:hover { background: var(--color-accent-hover); color: #fff; }
.btn-primary:active { background: var(--color-accent-active); }

.btn-secondary { background: var(--color-bg-surface); color: var(--color-text-primary); border-color: var(--color-border-default); }
.btn-secondary:hover { background: var(--color-bg-muted); color: var(--color-text-primary); }

.btn-ghost { background: transparent; color: var(--color-text-secondary); }
.btn-ghost:hover { background: var(--color-bg-muted); color: var(--color-text-primary); }

.btn-inverse { background: var(--color-bg-inverse); color: var(--color-text-on-inverse); }
.btn-inverse:hover { background: var(--color-bg-inverse-hover); color: #fff; }

.btn-danger { background: var(--color-danger); color: #fff; }
.btn-danger:hover { filter: brightness(1.08); color: #fff; }

/* ---- Form controls ------------------------------------------------------ */
.field { display: flex; flex-direction: column; gap: 6px; }
.label {
  font-size: var(--font-size-caption); font-weight: var(--weight-semibold);
  color: var(--color-text-secondary);
}
.input, .select, .textarea {
  font-family: var(--font-sans); font-size: var(--font-size-body);
  color: var(--color-text-primary); background: var(--color-bg-surface);
  border: 1px solid var(--color-border-default); border-radius: var(--radius-sm);
  padding: 10px 12px; width: 100%;
  transition: border-color var(--duration-fast) var(--ease-standard),
              box-shadow var(--duration-fast) var(--ease-standard);
}
/* Match .btn's 44px so a control and a button on the same row share a
   baseline. Without this they differ by ~2px wherever they sit side by side. */
.input, .select { height: 44px; }
.input-sm, .select-sm { height: 38px; padding: 6px 10px; font-size: var(--font-size-body-sm); }
.textarea { min-height: 110px; resize: vertical; line-height: var(--leading-normal); }
.input:focus, .select:focus, .textarea:focus {
  outline: none; border-color: var(--color-border-focus); box-shadow: var(--focus-ring);
}
.input::placeholder, .textarea::placeholder { color: var(--color-text-disabled); }
.field-hint { font-size: var(--font-size-caption); color: var(--color-text-secondary); }
.field-error { font-size: var(--font-size-caption); color: var(--color-danger); }

/* ---- Tags & pills ------------------------------------------------------- */
.tag {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: var(--radius-pill);
  font-size: var(--font-size-label); font-weight: var(--weight-semibold);
  background: var(--color-bg-muted); color: var(--color-text-secondary);
}

/* ---- Cards & sections --------------------------------------------------- */
.card { background: var(--color-bg-surface); border: 1px solid var(--color-border-subtle); border-radius: var(--radius-md); }
.card-pad { padding: 24px; }
.section-title { font-size: var(--font-size-h4); font-weight: var(--weight-bold); color: var(--color-text-primary); }

/* ---- Data table --------------------------------------------------------- */
.data-table { width: 100%; border-collapse: collapse; }
.data-table th {
  text-align: left; padding: 12px 16px;
  font-size: var(--font-size-label); font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-label); text-transform: uppercase;
  color: var(--color-text-secondary); border-bottom: 1px solid var(--color-border-subtle);
}
.data-table td { padding: 14px 16px; border-bottom: 1px solid var(--color-border-subtle); font-size: var(--font-size-body-sm); }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr { transition: background var(--duration-fast) var(--ease-standard); }
.data-table tbody tr:hover { background: color-mix(in oklch, var(--color-bg-muted) 45%, white); }

/* ---- Filter bar --------------------------------------------------------- */
/* Replaces the inline `style:` that was passed to form_with on every index
   page — Rails silently drops that option, so those bars rendered with no
   flex and no gap at all. */
.filter-bar {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin-bottom: var(--space-5);
}
.filter-bar .filter-search { position: relative; flex: 1; min-width: 240px; }
.filter-bar .filter-search .input { padding-left: 36px; }
.filter-bar .filter-icon {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  color: var(--color-text-secondary); display: inline-flex; pointer-events: none;
}
.filter-bar .select { width: auto; min-width: 170px; }
.filter-bar--end { margin-left: auto; }

/* Constrains a form to a comfortable reading width. */
.form-narrow { max-width: 820px; }
.form-narrow--sm { max-width: 640px; }

/* ---- Page header -------------------------------------------------------- */
/* `center`, not `flex-start`: the action button was floating to the top of a
   two-line title block. */
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 20px; flex-wrap: wrap; margin-bottom: 32px; }
.page-title { font-size: var(--font-size-h2); font-weight: var(--weight-bold); color: var(--color-text-primary); letter-spacing: var(--tracking-tight); }
.page-sub { font-size: var(--font-size-body); color: var(--color-text-secondary); margin-top: 6px; }

/* ---- Flash / toast ------------------------------------------------------ */
.toast {
  background: var(--color-bg-inverse); color: var(--color-text-on-inverse);
  padding: 12px 18px; border-radius: var(--radius-sm); box-shadow: var(--shadow-md);
  font-size: var(--font-size-body-sm); max-width: 360px;
}
.toast-success { background: var(--color-success); color: #fff; }
.toast-danger  { background: var(--color-danger); color: #fff; }

/* ---- Segmented tabs ----------------------------------------------------- */
.tabs { display: inline-flex; gap: 4px; background: var(--color-bg-muted); padding: 4px; border-radius: var(--radius-md); }
.tab {
  display: inline-flex; align-items: center;
  padding: 8px 16px; border-radius: var(--radius-sm);
  font-size: var(--font-size-body-sm); font-weight: var(--weight-semibold);
  color: var(--color-text-secondary); text-decoration: none;
  /* Tabs driven by tabs_controller are <button>s, which otherwise arrive with
     the UA's own font, border and background. */
  background: none; border: 0; font-family: inherit; cursor: pointer;
}
.tab.is-active { background: var(--color-bg-surface); color: var(--color-text-primary); box-shadow: var(--shadow-xs); }

/* The `hidden` attribute is only `display: none` from the UA stylesheet, so any
   class setting `display` beats it — a hidden .settings-stack (display: flex)
   stayed on screen while its tab looked inactive. */
[hidden] { display: none !important; }

/* ---- Settings ----------------------------------------------------------- */
.settings-stack { max-width: 860px; display: flex; flex-direction: column; gap: var(--space-6); }
.settings-form { display: flex; flex-direction: column; gap: var(--space-4); align-items: stretch; }
/* A submit in a flex column stretches to full width otherwise. */
.settings-form > input[type="submit"], .settings-form > .btn { align-self: flex-start; }
.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: var(--space-4);
}
.settings-subhead {
  font-size: var(--font-size-label);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-label);
  text-transform: uppercase;
  color: var(--color-text-secondary);
  padding-top: var(--space-2);
  border-top: 1px solid var(--color-border-subtle);
}

/* ---- Agent workspace ----------------------------------------------------
 * The resolution agent is the intended way most tickets get solved, so it is a
 * full-width workspace rather than a sidebar card: a chat column showing the
 * exact turns sent to and received from the model, plus a rail with what the
 * run produced.
 */
.agent-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 20px; background: var(--ls-graphite); color: var(--ls-linen);
}
.agent-branch {
  font-family: var(--font-mono); font-size: var(--font-size-caption);
  background: rgba(255, 255, 255, .12); padding: 2px 8px; border-radius: 4px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.agent-head-meta {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: var(--font-size-caption); opacity: .75;
}

.agent-body {
  display: grid; grid-template-columns: minmax(0, 1fr) 260px;
  align-items: start;
}
.agent-chat { min-width: 0; display: flex; flex-direction: column; }
.agent-rail {
  display: flex; flex-direction: column; gap: 20px;
  padding: 20px; border-left: 1px solid var(--color-border-subtle);
  position: sticky; top: 24px;
}
/* Below the two-column threshold the rail reads better as a footer than as a
   90px-wide stripe. */
@media (max-width: 1100px) {
  .agent-body { grid-template-columns: minmax(0, 1fr); }
  .agent-rail { border-left: 0; border-top: 1px solid var(--color-border-subtle); position: static; }
}

.agent-empty { display: flex; gap: 12px; align-items: flex-start; color: var(--color-text-secondary); }

/* Prompts are editable text, so they get a monospace box that makes trailing
   whitespace and indentation visible. */
.agent-prompt { font-family: var(--font-mono); font-size: var(--font-size-caption); line-height: 1.55; }
.agent-pre {
  font-family: var(--font-mono); font-size: var(--font-size-caption); line-height: 1.5;
  white-space: pre-wrap; word-break: break-word; margin: 10px 0 0;
  background: var(--color-bg-muted); padding: 12px; border-radius: var(--radius-sm);
  max-height: 420px; overflow: auto;
}

.agent-system { padding: 14px 20px; border-bottom: 1px solid var(--color-border-subtle); }
.agent-system > summary {
  cursor: pointer; font-size: var(--font-size-caption); font-weight: var(--weight-semibold);
  color: var(--color-text-secondary);
}

.agent-thread {
  display: flex; flex-direction: column; gap: 16px;
  padding: 20px; min-height: 120px; max-height: 62vh; overflow-y: auto;
}
.agent-turn { display: flex; flex-direction: column; gap: 6px; max-width: 88%; }
.agent-turn.is-mine { align-self: flex-end; align-items: flex-end; }
.agent-turn-head {
  display: flex; gap: 8px; align-items: baseline;
  font-size: var(--font-size-caption); font-weight: var(--weight-semibold);
  color: var(--color-text-secondary);
}
.agent-turn-ts { font-weight: var(--weight-regular); opacity: .7; }
.agent-bubble {
  background: var(--color-bg-muted); padding: 10px 14px; border-radius: var(--radius-md);
  font-size: var(--font-size-body-sm); line-height: 1.55; word-break: break-word;
}
.agent-bubble p { margin: 0 0 8px; }
.agent-bubble p:last-child { margin-bottom: 0; }
.agent-turn.is-mine .agent-bubble { background: var(--ls-orange-tint-10); }

/* A run makes dozens of tool calls; collapsed, they read as a log rather than
   burying the model's reasoning. */
.agent-tool { border: 1px solid var(--color-border-subtle); border-radius: var(--radius-sm); padding: 8px 12px; }
.agent-tool > summary {
  cursor: pointer; display: flex; align-items: center; gap: 8px;
  font-size: var(--font-size-caption); color: var(--color-text-secondary);
}
.agent-tool > summary code { font-family: var(--font-mono); color: var(--color-text-primary); font-weight: var(--weight-semibold); }
.agent-tool-hint {
  font-family: var(--font-mono); opacity: .7;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0;
}
/* One folded row now stands in for a whole turn of tool calls, so the calls
   themselves are listed inside it. */
.agent-tool-row {
  display: flex; align-items: baseline; gap: 8px; min-width: 0;
  font-size: var(--font-size-caption); margin-top: 8px;
}
.agent-tool-row code { font-family: var(--font-mono); font-weight: var(--weight-semibold); }

.agent-waiting {
  display: flex; align-items: center; gap: 8px; align-self: flex-start;
  font-size: var(--font-size-caption); color: var(--color-text-secondary);
}

.agent-error {
  margin: 0 20px 20px; padding: 16px;
  border: 1px solid var(--color-danger); border-radius: var(--radius-sm);
  background: color-mix(in oklch, #c23b22 6%, white); color: var(--color-danger);
}
.agent-error .label, .agent-error .agent-system > summary { color: var(--color-danger); }
.agent-error-full > summary {
  cursor: pointer; font-size: var(--font-size-caption); margin-top: 8px; opacity: .8;
}

.agent-composer {
  padding: 16px 20px; border-top: 1px solid var(--color-border-subtle);
  background: var(--color-bg-page);
}

/* ---- Confirm dialog -----------------------------------------------------
 * Used where a turbo_confirm is too weak: deleting a client cascades through
 * its invoices, so the name has to be typed out.
 */
.confirm-dialog {
  border: 1px solid var(--color-border-default); border-radius: var(--radius-md);
  padding: 0; max-width: 480px; width: calc(100% - 32px);
  background: var(--color-bg-surface); color: var(--color-text-primary);
  box-shadow: var(--shadow-md);
}
.confirm-dialog::backdrop { background: rgba(42, 44, 46, .45); }
.confirm-dialog-body { padding: 24px; display: flex; flex-direction: column; gap: 14px; }
.confirm-dialog h2 { font-size: var(--font-size-h4); margin: 0; }
.confirm-dialog p { font-size: var(--font-size-body-sm); margin: 0; line-height: 1.55; }
.confirm-dialog ul { margin: 0; padding-left: 20px; font-size: var(--font-size-body-sm); color: var(--color-text-secondary); }
.confirm-recommend {
  background: var(--ls-orange-tint-10); border-radius: var(--radius-sm); padding: 12px 14px;
  font-size: var(--font-size-body-sm); line-height: 1.55;
}
.confirm-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }
