/* ============================================================================
   Shared components. ui.js emits the markup these rules style; pages compose
   them instead of inventing their own. Built on the tokens in styles.css and
   on the button, badge, card and table classes already defined there, so a
   page migrates by switching to ui.*, not by restyling.
   ========================================================================== */

/* --- Buttons: size and state modifiers ------------------------------------ */

/* .table-action is the small ghost button; .small makes any variant that size. */
.primary.small,
.ghost.small,
.subtle.small,
.danger-button.small,
.danger-solid.small {
  padding: 5px 10px;
  font-size: 12px;
}

/* One destructive look for outlined buttons, wherever they sit. */
.danger-button,
.ghost.danger,
.table-action.danger {
  color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 35%, var(--border));
}

.danger-button:hover,
.ghost.danger:hover,
.table-action.danger:hover {
  color: var(--danger);
  border-color: var(--danger);
  background: var(--danger-dim);
}

button[aria-busy="true"] {
  cursor: progress;
  opacity: 0.7;
}

button[aria-busy="true"]::before {
  content: "";
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid currentColor;
  border-right-color: transparent;
  animation: ui-spin 0.7s linear infinite;
}

@keyframes ui-spin {
  to { transform: rotate(360deg); }
}

/* --- Fields --------------------------------------------------------------- */

.field {
  display: grid;
  gap: 6px;
  align-content: start;
  min-width: 0;
}

.field > .field-label {
  font-size: 12.5px;
  font-weight: 550;
  color: var(--n-10);
}

.field.required > .field-label::after {
  content: " *";
  color: var(--danger);
}

.field > .field-hint,
.field > .field-error {
  margin: 0;
}

.field-error {
  font-size: 12px;
  color: var(--danger);
}

.field-error:empty {
  display: none;
}

.field.invalid input,
.field.invalid select,
.field.invalid textarea {
  border-color: var(--danger);
}

/* An input with something beside it: a unit, or a Generate button. */
.field-control {
  display: flex;
  align-items: center;
  gap: var(--s-2);
}

.field-control > input,
.field-control > select {
  flex: 1;
  min-width: 0;
}

.field-unit {
  font-size: 12.5px;
  color: var(--text-muted);
  white-space: nowrap;
}

.field input[readonly] {
  background: var(--bg-sink);
  color: var(--text-muted);
}

/* --- Action rows ---------------------------------------------------------- */

/* The one row of buttons under a form, in a dialog or on a page. The error
   sits on the left, where it is read before the button that caused it. */
.form-foot {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--s-3);
  margin-top: var(--s-5);
}

.form-foot > .error {
  flex: 1;
  min-height: 0;
}

.form-foot > .error:empty {
  visibility: hidden;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s-2);
}

.actions.end {
  justify-content: flex-end;
}

/* --- Dialogs -------------------------------------------------------------- */

dialog.ui-dialog.sm { width: min(440px, calc(100vw - 32px)); }
dialog.ui-dialog.lg { width: min(780px, calc(100vw - 32px)); }
dialog.ui-dialog.wide { width: min(940px, calc(100vw - 32px)); }

.ui-dialog .dialog-head {
  margin-bottom: var(--s-4);
}

.ui-dialog .dialog-message {
  color: var(--text-muted);
  font-size: 13.5px;
  line-height: 1.55;
  margin: 0 0 var(--s-4);
}

.ui-dialog .dialog-body {
  display: grid;
  gap: var(--s-4);
}

.ui-dialog .secret-value {
  margin: 0;
}

/* Detail dialog: a titled section with its action on the right. */
.detail-section {
  display: grid;
  gap: var(--s-3);
  padding-top: var(--s-4);
  border-top: 1px solid var(--border-soft);
}

.detail-section:first-child {
  padding-top: 0;
  border-top: 0;
}

/* Actions on the record itself sit at the start of the footer, away from
   the button that closes the dialog. */
.form-foot > .foot-start {
  order: -1;
  display: flex;
  gap: var(--s-2);
}

.detail-section-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--s-4);
}

.detail-section-head h3 {
  margin: 0;
  font-size: 13.5px;
  font-weight: 600;
}

.detail-section-head p {
  margin: 2px 0 0;
  font-size: 12.5px;
  color: var(--text-muted);
  line-height: 1.5;
}

.detail-section-head > .btn,
.detail-section-head > button {
  flex: none;
}

.detail-section .table-wrap {
  border: 1px solid var(--border-soft);
  border-radius: var(--r-sm);
}

td > .sub {
  display: block;
  margin-top: 2px;
}

tbody tr.clickable:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

/* Credentials dialog: each value sits in its own row beside its Copy button. */
.cred-list {
  display: grid;
  gap: var(--s-4);
  margin: var(--s-4) 0;
}

.cred-field {
  display: grid;
  gap: var(--s-1);
}

.cred-row {
  display: flex;
  align-items: flex-start;
  gap: var(--s-2);
}

.cred-row .secret-value {
  flex: 1;
  min-width: 0;
  padding: var(--s-3);
  font-size: 13px;
}

.cred-row .cred-snippet {
  white-space: pre-wrap;
  font-size: 12px;
  line-height: 1.5;
  color: var(--text);
}

.copy-done {
  color: var(--success);
  font-size: 12.5px;
}

/* --- Notices -------------------------------------------------------------- */

.notice.info {
  border-color: color-mix(in srgb, var(--info) 45%, var(--border));
  background: var(--info-dim);
}

.notice.danger {
  border-color: color-mix(in srgb, var(--danger) 45%, var(--border));
  background: var(--danger-dim);
}

.notice.success {
  border-color: color-mix(in srgb, var(--success) 45%, var(--border));
  background: var(--success-dim);
}

.notice.with-action {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-4);
}

.notice code {
  font-family: var(--mono);
  font-size: 12px;
}

/* --- Empty, loading and error states -------------------------------------- */

.empty .actions {
  justify-content: center;
  margin-top: var(--s-4);
}

.empty.loading strong::before,
.spinner {
  content: "";
  display: inline-block;
  width: 12px;
  height: 12px;
  margin-right: var(--s-2);
  vertical-align: -1px;
  border-radius: 50%;
  border: 2px solid var(--text-dim);
  border-right-color: transparent;
  animation: ui-spin 0.7s linear infinite;
}

/* --- Toasts --------------------------------------------------------------- */

.toast.warning { border-color: var(--warn); }
.toast.info { border-color: var(--info); }

.toast.persist { pointer-events: auto; }

.toast .toast-close {
  margin-left: var(--s-3);
  border: 0;
  background: transparent;
  color: inherit;
  opacity: 0.7;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
}

.toast .toast-close:hover { opacity: 1; }

/* --- UI kit page ---------------------------------------------------------- */

.ui-kit {
  display: grid;
  gap: var(--s-5);
}

.ui-kit .card > .actions + .actions {
  margin-top: var(--s-3);
}

/* A status line beside the form's buttons (e.g. "Connection OK"). */
.form-foot > .form-status:empty {
  display: none;
}

/* The info mark next to a field label; its text is the tooltip. */
.field-label .info-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  margin-left: var(--s-1);
  border: 1px solid var(--border);
  border-radius: 50%;
  color: var(--text-muted);
  font-size: 10px;
  font-style: italic;
  font-weight: 600;
  line-height: 1;
  cursor: help;
  vertical-align: middle;
}

.field-label .info-mark:hover,
.field-label .info-mark:focus-visible {
  color: var(--text);
  border-color: var(--text-muted);
}

/* --- Schema widgets: radio group, slider, collapsible fieldset ------------ */

.ui-radio { display: flex; flex-wrap: wrap; gap: var(--s-2, 8px); }
.ui-radio label {
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px;
  border: 1px solid var(--border); border-radius: 8px; cursor: pointer; font-size: 13px;
  white-space: nowrap; flex: 0 0 auto;
}
.ui-radio label:has(input:checked) { border-color: var(--accent, currentColor); background: color-mix(in srgb, var(--accent, currentColor) 10%, transparent); }
.ui-radio input { margin: 0; }
.ui-radio-hint { margin: 6px 0 0; }
.ui-radio-hint.hidden { display: none; }
.ui-probe-status { margin: 4px 0 0; color: var(--success); }
.field input[type="range"] { flex: 1; padding: 0; }
.ui-range-value { min-width: 3.5em; text-align: right; }
details.ui-fieldset { border: 1px solid var(--border); border-radius: 8px; padding: var(--s-3, 12px); }
details.ui-fieldset > summary { cursor: pointer; font-weight: 600; }
details.ui-fieldset[open] > summary { margin-bottom: var(--s-3, 12px); }
.ui-when.hidden { display: none; }

/* Menu: a button opening a short list of links (ui.menu). */
.ui-suggest { position: relative; flex: 1; min-width: 0; }
.ui-suggest > input { width: 100%; }
.ui-sugg { position: fixed; z-index: 1000; display: grid; max-height: 240px; overflow: auto; overscroll-behavior: contain; scroll-padding-top: 22px; border: 1px solid var(--border); border-radius: var(--r-sm); background: var(--surface-raised); box-shadow: 0 6px 18px rgba(0, 0, 0, 0.2); }
.ui-sugg-opt { padding: var(--s-2) var(--s-3); border: 0; background: transparent; color: var(--text); font: 12px var(--mono); text-align: left; cursor: pointer; overflow-wrap: anywhere; }
.ui-sugg-opt[aria-selected="true"] { background: var(--accent-dim); color: var(--accent-strong); }
.ui-sugg-group { position: sticky; top: 0; z-index: 1; padding: 3px var(--s-3); background: var(--surface-raised); font-size: 10.5px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-dim); }
.ui-menu { position: relative; display: inline-block; }
.ui-menu > summary { list-style: none; cursor: pointer; }
.ui-menu > summary::-webkit-details-marker { display: none; }
.ui-menu-caret { width: 0; height: 0; border-left: 4px solid transparent; border-right: 4px solid transparent; border-top: 5px solid currentColor; opacity: 0.6; }
.ui-menu-list { position: absolute; top: calc(100% + 6px); z-index: 40; min-width: 300px; padding: 6px; background: var(--surface-raised); border: 1px solid var(--border); border-radius: var(--r-md); box-shadow: 0 8px 24px rgb(0 0 0 / 0.12); display: grid; gap: 2px; }
.ui-menu.end .ui-menu-list { right: 0; }
.ui-menu.start .ui-menu-list { left: 0; }
.ui-menu-item { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: var(--r-sm); color: inherit; text-decoration: none; }
.ui-menu-item:hover, .ui-menu-item:focus-visible { background: var(--surface-hover); }
.ui-menu-item .icon { width: 18px; height: 18px; flex: none; }
.ui-menu-item span { display: grid; }
.ui-menu-item strong { font-size: 13px; font-weight: 550; }
.ui-menu-item small { font-size: 12px; color: var(--text-muted); }

/* Rows whose order matters: dragged into place by the grip. */
.ui-row-sortable { cursor: grab; }
.ui-row-sortable.dragging { opacity: .5; }
.ui-row-sortable.drop-target td { box-shadow: inset 0 2px 0 var(--accent); }
.ui-row-grip { color: var(--text-muted); font-size: 16px; line-height: 1; user-select: none; }

/* Drawer: a right-hand panel with a stack of views (ui.drawer). */
.ui-drawer {
  width: min(760px, 94vw); max-width: none; height: 100dvh; max-height: 100dvh;
  margin: 0 0 0 auto; padding: 0; border: 0; border-left: 1px solid var(--border); border-radius: 0;
  background: var(--surface); color: var(--text); box-shadow: -24px 0 60px rgba(3, 5, 8, .22);
}
.ui-drawer.lg { width: min(1040px, 94vw); }
.ui-drawer[open] { display: flex; flex-direction: column; animation: ui-drawer-in 220ms cubic-bezier(.22, 1, .36, 1); }
@keyframes ui-drawer-in { from { transform: translateX(100%); } to { transform: translateX(0); } }
@media (prefers-reduced-motion: reduce) { .ui-drawer[open] { animation: none; } }
.ui-drawer-head { display: flex; align-items: center; gap: var(--s-3); padding: var(--s-4) var(--s-5); border-bottom: 1px solid var(--border-soft, var(--border)); }
.ui-drawer-title { flex: 1; min-width: 0; }
.ui-drawer-title h2 { margin: 0; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ui-drawer-title .eyebrow { margin: 0 0 2px; }
.ui-drawer-views { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.ui-drawer-view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.ui-drawer-view[hidden] { display: none; }
.ui-drawer-body { flex: 1; overflow: auto; padding: var(--s-5); }
.ui-drawer-foot { display: flex; align-items: center; justify-content: flex-end; flex-wrap: wrap; gap: var(--s-2); padding: var(--s-3) var(--s-5); border-top: 1px solid var(--border-soft, var(--border)); }
.ui-drawer-foot .error { margin: 0 auto 0 0; color: var(--danger, #c0392b); font-size: 13px; }
