/* CROC Design System — Components (Framework-Free, Light & Dark, iPhone-first + Desktop) */

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

html {
  -webkit-text-size-adjust: 100%;
  scrollbar-gutter: stable;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--lh-base);
  color: var(--text-primary);
  background: var(--bg-canvas);
}

body {
  margin: 0;
  min-height: 100dvh;
  color: var(--text-primary);
  background: var(--bg-canvas-grad, var(--bg-canvas));
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

:focus-visible {
  outline: 2px solid var(--accent, #2d5b8e);
  outline-offset: 2px;
  box-shadow: none;
}

.tabular,
.mono {
  font-variant-numeric: tabular-nums;
}

.mono {
  font-family: var(--font-mono);
}

.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;
}

/* ==================== BUTTONS ==================== */
.ds-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  height: 36px;
  padding: 0 var(--space-4);
  border-radius: var(--radius-full);
  border: none;
  background: var(--bg-raised);
  color: var(--text-primary);
  font-family: inherit;
  font-size: var(--text-sm);
  font-weight: var(--fw-medium);
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  text-decoration: none;
  box-shadow: var(--shadow-1);
}

.ds-btn:hover:not(:disabled):not([aria-disabled="true"]) {
  background: var(--bg-elevated);
}

.ds-btn:disabled,
.ds-btn[aria-disabled="true"] {
  opacity: 0.48;
  cursor: not-allowed;
  pointer-events: none;
}

.ds-btn--primary {
  background: var(--cta-bg);
  border-color: transparent;
  color: var(--cta-text);
  box-shadow: var(--shadow-1);
}
.ds-btn--primary:hover:not(:disabled) {
  background: var(--cta-hover);
  border-color: transparent;
}
.ds-btn--primary:active:not(:disabled) {
  background: var(--cta-press);
}

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

.ds-btn--ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-secondary);
}
.ds-btn--ghost:hover:not(:disabled) {
  background: var(--state-hover);
  color: var(--text-primary);
  border-color: transparent;
}

.ds-btn--danger {
  background: var(--danger);
  border-color: transparent;
  color: #ffffff;
}
.ds-btn--danger:hover:not(:disabled) {
  filter: brightness(0.92);
  border-color: transparent;
}

.ds-btn--icon {
  width: 36px;
  height: 36px;
  padding: 0;
}

.ds-btn--sm {
  height: 30px;
  padding: 0 var(--space-2);
  font-size: var(--text-2xs);
  border-radius: var(--radius-xs);
}
.ds-btn--icon.ds-btn--sm {
  width: 30px;
  height: 30px;
}

.ds-btn--lg {
  height: 44px;
  padding: 0 var(--space-5);
  font-size: var(--text-md);
  border-radius: var(--radius-md);
}
.ds-btn--icon.ds-btn--lg {
  width: 44px;
  height: 44px;
}

/* Loading state with zero width jump */
.ds-btn.is-loading {
  pointer-events: none;
}
.ds-btn.is-loading > .ds-btn-label {
  visibility: hidden;
}
.ds-btn.is-loading::after {
  content: '';
  position: absolute;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 2px solid currentColor;
  border-top-color: transparent;
  animation: ds-spin 0.75s linear infinite;
}

/* ==================== FORM CONTROLS ==================== */
.ds-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.ds-label {
  font-size: var(--text-2xs);
  font-weight: var(--fw-medium);
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}

.ds-hint {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
  min-height: 16px;
}

.ds-error-text {
  font-size: var(--text-xs);
  color: var(--danger-text);
  min-height: 16px;
}

.ds-input,
.ds-textarea,
.ds-select {
  width: 100%;
  min-height: 40px;
  padding: 9px 15px;
  border-radius: 18px;
  border: none;
  background: var(--bg-input);
  color: var(--text-primary);
  font-family: inherit;
  font-size: var(--text-base);
  line-height: var(--lh-base);
  box-shadow: var(--bevel-inset), var(--shadow-1);
  transition: transform var(--dur-fast) var(--ease-fluid), opacity var(--dur-fast) var(--ease-fluid);
}

.ds-input::placeholder,
.ds-textarea::placeholder {
  color: var(--text-disabled);
}

.ds-input:hover:not(:disabled),
.ds-textarea:hover:not(:disabled),
.ds-select:hover:not(:disabled) {
  border-color: var(--border-strong);
}

.ds-input:focus,
.ds-textarea:focus,
.ds-select:focus {
  outline: none;
  border-color: var(--focus-ring);
  box-shadow: var(--focus-ring-shadow);
}

.ds-input[aria-invalid="true"],
.ds-textarea[aria-invalid="true"],
.ds-select[aria-invalid="true"],
.ds-field.has-error .ds-input,
.ds-field.has-error .ds-textarea,
.ds-field.has-error .ds-select {
  border-color: var(--danger);
}

.ds-input:disabled,
.ds-textarea:disabled,
.ds-select:disabled {
  opacity: 0.52;
  cursor: not-allowed;
}

.ds-textarea {
  min-height: 88px;
  resize: vertical;
}

/* Search field */
.ds-search {
  position: relative;
  display: flex;
  align-items: center;
}

.ds-search-icon {
  position: absolute;
  left: 11px;
  color: var(--text-tertiary);
  pointer-events: none;
  display: inline-flex;
}

.ds-search .ds-input {
  padding-left: 34px;
  padding-right: 32px;
}

.ds-search-clear {
  position: absolute;
  right: 6px;
  width: 24px;
  height: 24px;
  border: 0;
  border-radius: var(--radius-full);
  background: transparent;
  color: var(--text-tertiary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.ds-search-clear:hover {
  background: var(--state-hover);
  color: var(--text-primary);
}

/* Checkbox & Radio */
.ds-check {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  cursor: pointer;
  font-size: var(--text-sm);
  color: var(--text-primary);
  user-select: none;
}
.ds-check input[type="checkbox"],
.ds-check input[type="radio"] {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}
.ds-check.is-disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Switch (Spring Knob) */
.ds-switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  cursor: pointer;
  user-select: none;
}

.ds-switch-input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
}

.ds-switch-track {
  position: relative;
  width: 42px;
  height: 24px;
  border-radius: var(--radius-full);
  background: var(--neutral-6);
  transition: background-color var(--dur-base) var(--ease-standard);
  flex-shrink: 0;
}

.ds-switch-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: var(--shadow-1);
  transition: transform var(--dur-base) var(--ease-spring);
}

.ds-switch-input:checked + .ds-switch-track {
  background: var(--accent);
}
.ds-switch-input:checked + .ds-switch-track .ds-switch-thumb {
  transform: translate3d(18px, 0, 0);
}
.ds-switch-input:focus-visible + .ds-switch-track {
  box-shadow: var(--focus-ring-shadow);
}
.ds-switch-input:disabled + .ds-switch-track {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Segmented Control */
.ds-segmented {
  display: inline-flex;
  align-items: center;
  padding: 3px;
  border-radius: var(--radius-sm);
  background: var(--bg-sunken);
  border: 1px solid var(--border-subtle);
  gap: 2px;
}

.ds-segmented-btn {
  border: 0;
  background: transparent;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: var(--text-2xs);
  font-weight: var(--fw-medium);
  padding: 5px 10px;
  border-radius: var(--radius-xs);
  cursor: pointer;
  white-space: nowrap;
  transition: background-color var(--dur-fast) var(--ease-standard), color var(--dur-fast) var(--ease-standard);
}

.ds-segmented-btn:hover:not(.is-active) {
  color: var(--text-primary);
}

.ds-segmented-btn.is-active {
  background: var(--bg-raised);
  color: var(--text-primary);
  box-shadow: var(--shadow-1);
}

/* Slider */
.ds-slider {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

/* ==================== TABS ==================== */
.ds-tabs {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--space-1);
  border-bottom: 1px solid var(--border-subtle);
  overflow-x: auto;
  scrollbar-width: none;
}
.ds-tabs::-webkit-scrollbar { display: none; }

.ds-tab {
  position: relative;
  border: 0;
  background: transparent;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: var(--text-sm);
  font-weight: var(--fw-medium);
  padding: 10px 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: color var(--dur-fast) var(--ease-standard);
}
.ds-tab:hover { color: var(--text-primary); }
.ds-tab.is-active {
  color: var(--text-primary);
}
.ds-tab.is-active::after {
  content: '';
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 0;
  height: 2px;
  border-radius: var(--radius-full);
  background: var(--accent);
}

/* ==================== CHIPS, BADGES, STATUS ==================== */
.ds-badge,
.ds-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 22px;
  padding: 0 8px;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: var(--fw-medium);
  line-height: 1;
  white-space: nowrap;
  border: 1px solid transparent;
  background: var(--bg-sunken);
  color: var(--text-secondary);
}

.ds-badge--accent {
  background: var(--accent-soft);
  border-color: var(--accent-border);
  color: var(--accent-text);
}
.ds-badge--success {
  background: var(--success-soft);
  border-color: var(--success-border);
  color: var(--success-text);
}
.ds-badge--warning {
  background: var(--warning-soft);
  border-color: var(--warning-border);
  color: var(--warning-text);
}
.ds-badge--danger {
  background: var(--danger-soft);
  border-color: var(--danger-border);
  color: var(--danger-text);
}
.ds-badge--info {
  background: var(--info-soft);
  border-color: var(--info-border);
  color: var(--info-text);
}

.ds-chip-btn {
  cursor: pointer;
}
.ds-chip-btn:hover {
  background: var(--bg-elevated);
  border-color: var(--border-default);
}
.ds-chip-btn.is-selected {
  background: var(--accent-soft);
  border-color: var(--accent-border);
  color: var(--accent-text);
}

.ds-status-dot {
  position: relative;
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--st-offline);
  color: var(--st-offline);
}
.ds-status-dot[data-status="working"] { background: var(--st-working); color: var(--st-working); }
.ds-status-dot[data-status="thinking"] { background: var(--st-thinking); color: var(--st-thinking); }
.ds-status-dot[data-status="idle"] { background: var(--st-idle); color: var(--st-idle); }
.ds-status-dot[data-status="waiting"] { background: var(--st-waiting); color: var(--st-waiting); }
.ds-status-dot[data-status="delayed"] { background: var(--st-delayed); color: var(--st-delayed); }
.ds-status-dot[data-status="blocked"] { background: var(--st-blocked); color: var(--st-blocked); }
.ds-status-dot[data-status="error"] { background: var(--st-error); color: var(--st-error); }
.ds-status-dot[data-status="offline"] { background: var(--st-offline); color: var(--st-offline); }

/* ==================== AVATAR ==================== */
.ds-avatar {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  background: var(--accent-soft);
  color: var(--accent-text);
  font-size: var(--text-sm);
  font-weight: var(--fw-semibold);
  flex-shrink: 0;
  user-select: none;
}
.ds-avatar--sm { width: 28px; height: 28px; font-size: var(--text-xs); }
.ds-avatar--lg { width: 48px; height: 48px; font-size: var(--text-lg); }

.ds-avatar-presence {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid var(--bg-surface);
}

/* ==================== CARD & LIST ROW ==================== */
.ds-card {
  background: var(--bg-raised);
  border: none;
  border-radius: var(--radius-md);
  padding: var(--space-4);
  box-shadow: var(--shadow-2);
}

.ds-list {
  display: flex;
  flex-direction: column;
}

.ds-list-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  text-decoration: none;
  cursor: pointer;
  border: none;
}
.ds-list-row:hover {
  background: var(--state-hover);
}
.ds-list-row.is-selected {
  background: var(--accent-soft);
}

/* ==================== TABLE ==================== */
.ds-table-wrap {
  width: 100%;
  overflow: visible;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--bg-raised);
}

.ds-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
  text-align: left;
}

.ds-table th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--bg-surface);
  color: var(--text-tertiary);
  font-size: var(--text-xs);
  font-weight: var(--fw-semibold);
  padding: 10px 14px;
  border-bottom: 1px solid var(--border-default);
  white-space: nowrap;
}

.ds-table td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-primary);
  vertical-align: middle;
}

.ds-table--compact th,
.ds-table--compact td {
  padding: 7px 10px;
}

.ds-table tbody tr:hover {
  background: var(--state-hover);
}
.ds-table tbody tr:last-child td {
  border-bottom: 0;
}

/* ==================== BANNER / OFFLINE BAR ==================== */
.ds-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-default);
  background: var(--bg-surface);
  font-size: var(--text-sm);
}
.ds-banner--info { background: var(--info-soft); border-color: var(--info-border); color: var(--info-text); }
.ds-banner--warning { background: var(--warning-soft); border-color: var(--warning-border); color: var(--warning-text); }
.ds-banner--danger { background: var(--danger-soft); border-color: var(--danger-border); color: var(--danger-text); }
.ds-banner--success { background: var(--success-soft); border-color: var(--success-border); color: var(--success-text); }

/* ==================== EMPTY & ERROR STATES ==================== */
.ds-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: var(--space-2);
  padding: var(--space-10) var(--space-6);
  max-width: 420px;
  margin: auto;
}
.ds-empty-icon {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-full);
  background: var(--bg-sunken);
  color: var(--text-tertiary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-bottom: var(--space-1);
}
.ds-empty-title {
  margin: 0;
  font-size: var(--text-md);
  font-weight: var(--fw-semibold);
  color: var(--text-primary);
}
.ds-empty-desc {
  margin: 0;
  font-size: var(--text-sm);
  color: var(--text-secondary);
}

/* ==================== PROGRESS ==================== */
.ds-progress {
  width: 100%;
  height: 6px;
  border-radius: var(--radius-full);
  background: var(--bg-sunken);
  overflow: hidden;
}
.ds-progress-bar {
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transform-origin: left center;
  transition: transform var(--dur-base) var(--ease-standard);
}

/* ==================== DIALOG, SHEET, POPOVER, COMMAND PALETTE ==================== */
.ds-backdrop {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  background: rgba(10, 12, 16, 0.48);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
}

.ds-dialog {
  width: 100%;
  max-width: 480px;
  max-height: min(86dvh, 720px);
  display: flex;
  flex-direction: column;
  background: var(--bg-surface-solid);
  border: none;
  border-radius: 30px;
  box-shadow: var(--shadow-5), inset 0 1px 0 rgba(255, 255, 255, 0.12);
  overflow: visible;
}

.ds-dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: 18px 22px 12px;
}

.ds-dialog-title {
  margin: 0;
  font-size: var(--text-lg);
  font-weight: var(--fw-semibold);
}

.ds-dialog-body {
  padding: 12px 22px 22px;
  overflow-y: auto;
  scrollbar-gutter: stable;
}

.ds-dialog-foot {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
  padding: 12px 22px 18px;
  background: transparent;
}

/* Bottom Sheet */
.ds-sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: var(--z-sheet);
  background: rgba(10, 12, 16, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.ds-sheet {
  width: 100%;
  max-width: 600px;
  max-height: 88dvh;
  background: var(--bg-raised);
  border-top: 1px solid var(--border-default);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  box-shadow: var(--shadow-5);
  padding-bottom: calc(var(--space-4) + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  touch-action: pan-y;
}

.ds-sheet-handle {
  width: 36px;
  height: 4px;
  border-radius: var(--radius-full);
  background: var(--neutral-7);
  margin: 10px auto 6px;
  flex-shrink: 0;
}

/* Popover / Menu */
.ds-popover {
  position: fixed;
  z-index: var(--z-popover);
  min-width: 196px;
  max-width: 300px;
  padding: 8px;
  border-radius: 24px;
  background: var(--bg-surface-solid);
  border: none;
  box-shadow: var(--shadow-3), inset 0 1px 0 rgba(255, 255, 255, 0.12);
}

.ds-menu-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: 9px 14px;
  border: 0;
  border-radius: var(--radius-full);
  background: transparent;
  color: var(--text-primary);
  font-family: inherit;
  font-size: var(--text-sm);
  text-align: left;
  cursor: pointer;
}
.ds-menu-item:hover,
.ds-menu-item.is-active {
  background: var(--state-hover);
}
.ds-menu-item--danger {
  color: var(--danger-text);
}

/* Command Palette */
.ds-cmd-backdrop {
  position: fixed;
  inset: 0;
  z-index: var(--z-command);
  background: rgba(10, 12, 16, 0.45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: min(14vh, 110px) var(--space-4) var(--space-4);
}

.ds-cmd {
  width: 100%;
  max-width: 580px;
  background: var(--bg-surface-solid);
  border: none;
  border-radius: 30px;
  box-shadow: var(--shadow-4), inset 0 1px 0 rgba(255, 255, 255, 0.12);
  overflow: visible;
}

.ds-cmd-input {
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--text-primary);
  padding: 16px 20px;
  font-family: inherit;
  font-size: var(--text-md);
  outline: none;
}

.ds-cmd-list {
  max-height: 340px;
  overflow-y: auto;
  scrollbar-gutter: stable;
  padding: 6px;
}

/* Toast Stack */
.ds-toast-stack {
  position: fixed;
  right: var(--space-4);
  bottom: calc(var(--space-4) + var(--safe-bottom));
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  max-width: 360px;
  pointer-events: none;
}

.ds-toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 10px 14px;
  border-radius: var(--radius-md);
  background: var(--bg-surface-solid);
  border: none;
  box-shadow: var(--shadow-3), inset 0 1px 0 rgba(255, 255, 255, 0.12);
  font-size: var(--text-sm);
  color: var(--text-primary);
}

/* Code block & Markdown body */
.ds-code {
  position: relative;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
  background: var(--bg-sunken);
  font-family: var(--font-mono);
  font-size: var(--text-2xs);
  line-height: 1.55;
  overflow: visible;
}
.ds-code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-tertiary);
  font-size: var(--text-xs);
}
.ds-code pre {
  margin: 0;
  padding: 10px 12px;
  overflow-x: auto;
}

.ds-markdown {
  font-size: var(--text-base);
  line-height: var(--lh-base);
  word-break: break-word;
}
.ds-markdown p { margin: 0 0 0.6em; }
.ds-markdown p:last-child { margin-bottom: 0; }
.ds-markdown code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--bg-sunken);
}

/* Touch Targets >= 44px on coarse pointer / mobile */
@media (pointer: coarse), (max-width: 768px) {
  .ds-btn,
  .ds-btn--sm,
  .ds-btn--icon,
  .ds-menu-item,
  .ds-tab,
  .ds-list-row {
    min-height: 44px;
  }
  .ds-btn--icon {
    min-width: 44px;
  }
  .ds-toast-stack {
    left: var(--space-3);
    right: var(--space-3);
    bottom: calc(var(--tabbar-h) + var(--space-3) + var(--safe-bottom));
    max-width: none;
  }
}
