/* =====================================================================================================
   OpenBuilds CAM design system
   -----------------------------------------------------------------------------------------------------
   One place for the visual language: tokens (colour, type, spacing, radius, elevation, motion) for the
   light and dark themes, and the styling of the shared components (buttons, fields, menus, dialogs,
   notifications, tabs, trees, scroll bars).

   Scope: everything is under html[data-ds]. The legacy stylesheets (Metro UI, css/themes/dark.css) use
   high-specificity and !important rules; the extra attribute lets these rules win predictably without
   piling on overrides. Remove data-ds from <html> to fall back to the legacy look.

   Feature/layout styles (panels, operation list, measure box...) live in css/ui-refresh.css and use
   these tokens.
   ===================================================================================================== */

/* ---------------------------------------------------------------- tokens: light (default) ---------- */

html[data-ds] body {
  /* neutrals */
  --ds-bg: #f3f4f6;
  --ds-surface: #ffffff;
  --ds-surface-2: #f7f8fa;
  --ds-surface-3: #eceef2;
  --ds-border: #e2e5ea;
  --ds-border-strong: #cdd2da;
  --ds-text: #16191d;
  --ds-text-2: #3f4651;
  --ds-text-3: #646c78;
  --ds-inverse: #1b1e22;
  --ds-on-inverse: #f3f4f6;

  /* accent and states */
  --ds-accent: #2563eb;
  --ds-accent-hover: #1d4ed8;
  --ds-accent-soft: rgba(37, 99, 235, 0.1);
  --ds-accent-ring: rgba(37, 99, 235, 0.32);
  --ds-on-accent: #ffffff;
  --ds-success: #15803d;
  --ds-success-soft: #e8f6ed;
  --ds-warning: #b45309;
  --ds-warning-soft: #fff5e3;
  --ds-danger: #d92d20;
  --ds-danger-soft: #fdeceb;
  --ds-info: #0369a1;
  --ds-info-soft: #e7f3fb;

  /* elevation */
  --ds-shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --ds-shadow-md: 0 4px 12px rgba(16, 24, 40, 0.08), 0 1px 3px rgba(16, 24, 40, 0.06);
  --ds-shadow-lg: 0 18px 48px rgba(16, 24, 40, 0.18), 0 4px 12px rgba(16, 24, 40, 0.08);
  --ds-overlay: rgba(17, 20, 24, 0.32);

  /* compatibility: names used by css/ui-refresh.css */
  --ui-accent: var(--ds-accent);
  --ui-accent-soft: var(--ds-accent-soft);
  --ui-danger: var(--ds-danger);
  --ui-success: var(--ds-success);
  --ui-surface: var(--ds-surface);
  --ui-surface-2: var(--ds-surface-2);
  --ui-border: var(--ds-border);
  --ui-text: var(--ds-text);
  --ui-text-muted: var(--ds-text-3);
  --ui-caption: var(--ds-surface);
  --ui-radius: var(--ds-radius-md);
  --ui-shadow: var(--ds-shadow-sm);
  --ui-shadow-float: var(--ds-shadow-md);
}

/* ---------------------------------------------------------------- tokens: dark ---------------------- */

html[data-ds] body.theme_dark {
  --ds-bg: #0f1113;
  --ds-surface: #17191c;
  --ds-surface-2: #1d2024;
  --ds-surface-3: #25292e;
  --ds-border: #2b2f35;
  --ds-border-strong: #3a3f47;
  --ds-text: #eceef1;
  --ds-text-2: #c0c5cc;
  --ds-text-3: #8f96a0;
  --ds-inverse: #eceef1;
  --ds-on-inverse: #141618;

  --ds-accent: #5b93ff;
  --ds-accent-hover: #7aa7ff;
  --ds-accent-soft: rgba(91, 147, 255, 0.16);
  --ds-accent-ring: rgba(91, 147, 255, 0.4);
  --ds-on-accent: #0b1220;
  --ds-success: #4ade80;
  --ds-success-soft: rgba(74, 222, 128, 0.12);
  --ds-warning: #fbbf24;
  --ds-warning-soft: rgba(251, 191, 36, 0.12);
  --ds-danger: #f87171;
  --ds-danger-soft: rgba(248, 113, 113, 0.13);
  --ds-info: #38bdf8;
  --ds-info-soft: rgba(56, 189, 248, 0.12);

  --ds-shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --ds-shadow-md: 0 6px 16px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.3);
  --ds-shadow-lg: 0 20px 56px rgba(0, 0, 0, 0.55), 0 4px 14px rgba(0, 0, 0, 0.35);
  --ds-overlay: rgba(0, 0, 0, 0.5);
}

/* ---------------------------------------------------------------- tokens: shared -------------------- */

html[data-ds] {
  --ds-font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI Variable Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --ds-font-mono: ui-monospace, "SF Mono", "Cascadia Mono", "Segoe UI Mono", Consolas, monospace;
  --ds-text-xs: 11px;
  --ds-text-sm: 12px;
  --ds-text-base: 13px;
  --ds-text-md: 14px;
  --ds-text-lg: 16px;
  --ds-text-xl: 19px;
  --ds-leading: 1.45;

  --ds-space-1: 4px;
  --ds-space-2: 8px;
  --ds-space-3: 12px;
  --ds-space-4: 16px;
  --ds-space-5: 24px;

  --ds-radius-sm: 6px;
  --ds-radius-md: 8px;
  --ds-radius-lg: 12px;
  --ds-radius-xl: 16px;
  --ds-radius-pill: 999px;

  --ds-control-h: 32px;
  --ds-control-h-sm: 28px;

  --ds-dur-fast: 110ms;
  --ds-dur: 170ms;
  --ds-ease: cubic-bezier(0.2, 0.8, 0.2, 1);
}

/* ---------------------------------------------------------------- base ------------------------------ */

html[data-ds] body {
  font-family: var(--ds-font);
  color: var(--ds-text);
  background: var(--ds-bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

html[data-ds] body.theme_dark {
  background: var(--ds-bg) !important; /* css/themes/dark.css forces #222 */
  color: var(--ds-text);
}

/* numbers line up in fields, readouts and tables */
html[data-ds] input[type=number],
html[data-ds] .ds-num,
html[data-ds] #measurebox input,
html[data-ds] .jobsummary-table,
html[data-ds] .op-summary {
  font-variant-numeric: tabular-nums;
}

html[data-ds] kbd {
  font-family: var(--ds-font-mono);
  font-size: var(--ds-text-xs);
  font-weight: 600;
  padding: 1px 5px;
  border: 1px solid var(--ds-border-strong);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--ds-surface);
  color: var(--ds-text-2);
}

html[data-ds] ::selection {
  background: var(--ds-accent-soft);
}

/* thin, quiet scroll bars for panels and lists */
html[data-ds] ::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

html[data-ds] ::-webkit-scrollbar-thumb {
  background: var(--ds-border-strong);
  border: 3px solid transparent;
  border-radius: 999px;
  background-clip: padding-box;
}

html[data-ds] ::-webkit-scrollbar-thumb:hover {
  background-color: var(--ds-text-3);
}

html[data-ds] ::-webkit-scrollbar-track {
  background: transparent;
}

/* ---------------------------------------------------------------- buttons --------------------------- */

html[data-ds] .button,
html[data-ds] .ob-button {
  height: var(--ds-control-h);
  line-height: calc(var(--ds-control-h) - 2px);
  padding: 0 var(--ds-space-3);
  border: 1px solid var(--ds-border-strong);
  border-radius: var(--ds-radius-md);
  background: var(--ds-surface);
  color: var(--ds-text);
  font-family: var(--ds-font);
  font-size: var(--ds-text-base);
  font-weight: 500;
  box-shadow: var(--ds-shadow-sm);
  transition: background-color var(--ds-dur-fast) var(--ds-ease), border-color var(--ds-dur-fast) var(--ds-ease), color var(--ds-dur-fast) var(--ds-ease), box-shadow var(--ds-dur-fast) var(--ds-ease), transform var(--ds-dur-fast) var(--ds-ease);
}

html[data-ds] .button:hover:not(:disabled):not(.disabled) {
  background: var(--ds-surface-2);
  border-color: var(--ds-text-3);
}

html[data-ds] .button:active:not(:disabled):not(.disabled) {
  background: var(--ds-surface-3);
  transform: translateY(0.5px);
}

html[data-ds] .button:disabled,
html[data-ds] .button.disabled {
  opacity: 0.45;
  cursor: not-allowed;
  box-shadow: none;
}

html[data-ds] .button.small {
  height: var(--ds-control-h-sm);
  line-height: calc(var(--ds-control-h-sm) - 2px);
  font-size: var(--ds-text-sm);
  padding: 0 var(--ds-space-2);
}

html[data-ds] .button .icon,
html[data-ds] .button svg {
  margin-right: 4px;
}

/* primary: one per area, the action you most likely want next */
html[data-ds] .button.primary,
html[data-ds] .button.success {
  background: var(--ds-accent);
  border-color: var(--ds-accent);
  color: var(--ds-on-accent);
}

html[data-ds] .button.primary:hover:not(:disabled):not(.disabled),
html[data-ds] .button.success:hover:not(:disabled):not(.disabled) {
  background: var(--ds-accent-hover);
  border-color: var(--ds-accent-hover);
  color: var(--ds-on-accent);
}

/* destructive */
html[data-ds] .button.alert {
  background: var(--ds-surface);
  border-color: var(--ds-border-strong);
  color: var(--ds-danger);
}

html[data-ds] .button.alert:hover:not(:disabled):not(.disabled) {
  background: var(--ds-danger-soft);
  border-color: var(--ds-danger);
  color: var(--ds-danger);
}

html[data-ds] .button.warning {
  color: var(--ds-warning);
}

/* quiet: secondary / outline / the legacy "dark" and "light" variants */
html[data-ds] .button.secondary,
html[data-ds] .button.outline,
html[data-ds] .button.dark,
html[data-ds] .button.light {
  background: var(--ds-surface);
  border-color: var(--ds-border-strong);
  color: var(--ds-text);
}

/* operation-row icon buttons: neutral, with meaning shown by the icon colour */
html[data-ds] .tool-button {
  width: 28px;
  height: 28px;
  line-height: 26px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--ds-radius-sm);
  background: var(--ds-surface-3);
  color: var(--ds-text-2);
  transition: background-color var(--ds-dur-fast) var(--ds-ease), color var(--ds-dur-fast) var(--ds-ease);
}

/* the legacy theme colours each variant (green arrows, red delete...): keep them neutral too */
html[data-ds] .tool-button.success,
html[data-ds] .tool-button.secondary,
html[data-ds] .tool-button.alert,
html[data-ds] .tool-button.warning,
html[data-ds] .tool-button.light,
html[data-ds] .tool-button.dark {
  background: var(--ds-surface-3);
  border-color: transparent;
  color: var(--ds-text-2);
}

html[data-ds] .tool-button:hover:not(:disabled):not(.disabled) {
  background: var(--ds-accent-soft);
  color: var(--ds-accent);
}

/* split button arrow (eg next to "Create Toolpath") follows the primary button beside it */
html[data-ds] .split-button .split,
html[data-ds] .split-button .split.success,
html[data-ds] .split-button .split.outline {
  height: var(--ds-control-h);
  border-color: var(--ds-border-strong);
  background: var(--ds-surface);
  color: var(--ds-text-2);
}

html[data-ds] .split-button .button.success + .split,
html[data-ds] .split-button .button.success ~ .split.success {
  background: var(--ds-accent);
  border-color: var(--ds-accent);
  color: var(--ds-on-accent);
}

html[data-ds] .tool-button.primary {
  width: auto;
  padding: 0 10px;
  background: var(--ds-accent);
  color: var(--ds-on-accent);
  font-weight: 600;
}

html[data-ds] .tool-button.primary:hover:not(:disabled):not(.disabled) {
  background: var(--ds-accent-hover);
  color: var(--ds-on-accent);
}

html[data-ds] .tool-button.warning {
  color: var(--ds-warning);
}

html[data-ds] .tool-button.alert:hover:not(:disabled):not(.disabled) {
  background: var(--ds-danger-soft);
  color: var(--ds-danger);
}

html[data-ds] .tool-button.disabled,
html[data-ds] .tool-button:disabled {
  opacity: 0.35;
  cursor: default;
}

/* ---------------------------------------------------------------- fields ---------------------------- */

html[data-ds] input[type=text],
html[data-ds] input[type=number],
html[data-ds] input[type=search],
html[data-ds] select,
html[data-ds] textarea,
html[data-ds] .cam-form-field {
  font-family: var(--ds-font);
  font-size: var(--ds-text-base);
  color: var(--ds-text);
  background-color: var(--ds-surface);
  border: 1px solid var(--ds-border-strong);
  border-radius: var(--ds-radius-sm);
  transition: border-color var(--ds-dur-fast) var(--ds-ease), box-shadow var(--ds-dur-fast) var(--ds-ease);
}

html[data-ds] input[type=text]:focus,
html[data-ds] input[type=number]:focus,
html[data-ds] input[type=search]:focus,
html[data-ds] select:focus,
html[data-ds] textarea:focus,
html[data-ds] .cam-form-field:focus {
  border-color: var(--ds-accent);
  box-shadow: 0 0 0 3px var(--ds-accent-ring);
  outline: none;
}

/* Metro's input wrapper and the toolpath dialog's labelled fields */
html[data-ds] div.input {
  border: 1px solid var(--ds-border-strong);
  border-radius: var(--ds-radius-sm);
  background: var(--ds-surface);
  overflow: hidden;
}

html[data-ds] div.input input {
  border: 0;
  box-shadow: none;
}

html[data-ds] div.input .prepend,
html[data-ds] div.input .append,
html[data-ds] .input-addon-label-left,
html[data-ds] .input-addon-label-right {
  background: var(--ds-surface-2) !important;
  color: var(--ds-text-3) !important;
  border-color: var(--ds-border-strong) !important;
  font-size: var(--ds-text-sm);
}

html[data-ds] .input-addon .active-border {
  border-color: var(--ds-border-strong);
}

html[data-ds] .input-addon:hover .active-border,
html[data-ds] .input-addon:focus-within .active-border {
  border-color: var(--ds-accent);
}

html[data-ds] .cam-form-field {
  height: var(--ds-control-h);
}

/* ---------------------------------------------------------------- menus ----------------------------- */

html[data-ds] .d-menu,
html[data-ds] .ribbon-dropdown,
html[data-ds] .v-menu {
  background: var(--ds-surface);
  color: var(--ds-text);
  border: 1px solid var(--ds-border) !important;
  border-radius: var(--ds-radius-lg);
  box-shadow: var(--ds-shadow-lg) !important;
  padding: var(--ds-space-1);
}

html[data-ds] .d-menu li a,
html[data-ds] .ribbon-dropdown li a,
html[data-ds] .ribbon-dropdown li > a {
  border-radius: var(--ds-radius-sm);
  color: var(--ds-text);
  font-size: var(--ds-text-base);
  border: 0 !important;
  transition: background-color var(--ds-dur-fast) var(--ds-ease);
}

html[data-ds] .d-menu li a:hover,
html[data-ds] .ribbon-dropdown li a:hover,
html[data-ds] .ribbon-dropdown li:hover > a {
  background: var(--ds-accent-soft);
  color: var(--ds-text);
}

/* ---------------------------------------------------------------- dialogs --------------------------- */

html[data-ds] .dialog,
html[data-ds] .dialog.dark {
  background: var(--ds-surface);
  color: var(--ds-text);
  border: 1px solid var(--ds-border);
  border-radius: var(--ds-radius-xl);
  box-shadow: var(--ds-shadow-lg) !important;
  overflow: hidden;
}

html[data-ds] .dialog .dialog-title,
html[data-ds] .dialog.dark .dialog-title {
  background: transparent;
  color: var(--ds-text);
  font-size: var(--ds-text-lg);
  font-weight: 600;
  letter-spacing: -0.01em;
  padding: var(--ds-space-4) var(--ds-space-5) var(--ds-space-2);
  border: 0;
  line-height: 1.3;
  height: auto;
}

html[data-ds] .dialog .dialog-content {
  padding: var(--ds-space-2) var(--ds-space-5) var(--ds-space-3);
  color: var(--ds-text-2);
  font-size: var(--ds-text-base);
  line-height: var(--ds-leading);
}

html[data-ds] .dialog .dialog-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--ds-space-2);
  padding: var(--ds-space-3) var(--ds-space-5) var(--ds-space-4);
  border-top: 1px solid var(--ds-border);
  background: var(--ds-surface);
}

html[data-ds] .dialog .dialog-actions .button {
  margin: 0;
  min-width: 84px;
}

/* opening: a short rise and fade (Metro shows a dialog by switching its inline visibility) */
html[data-ds] .dialog[style*="visibility: visible"] {
  animation: ds-dialog-in var(--ds-dur) var(--ds-ease);
}

@keyframes ds-dialog-in {
  from {
    opacity: 0;
    transform: translateY(6px) scale(0.985);
  }

  to {
    opacity: 1;
    transform: none;
  }
}

html[data-ds] .overlay {
  background: var(--ds-overlay) !important;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}

/* forms laid out as tables inside dialogs (the toolpath dialog): quiet rows instead of heavy stripes */
html[data-ds] .dialog .table tbody tr,
html[data-ds] .dialog .table.striped tbody tr:nth-child(odd),
html[data-ds] .dialog .table.striped tbody tr:nth-child(even) {
  background: transparent !important;
}

html[data-ds] .dialog .table td {
  padding: 6px 6px;
  border-top: 0;
  border-bottom: 1px solid var(--ds-border);
  vertical-align: middle;
  color: var(--ds-text-2);
  font-size: var(--ds-text-sm);
}

/* Metro's "clear" button inside fields */
html[data-ds] .input .button.input-clear-button,
html[data-ds] .input-addon .input-clear-button,
html[data-ds] .input .button-group .button {
  background: transparent !important;
  border: 0 !important;
  box-shadow: none !important;
  color: var(--ds-text-3) !important;
  width: 28px;
  min-width: 0;
}

/* ---------------------------------------------------------------- notifications --------------------- */

html[data-ds] .toast {
  font-family: var(--ds-font);
  font-size: var(--ds-text-base);
  line-height: 1.4;
  max-width: min(520px, calc(100vw - 32px));
  padding: 10px 14px 10px 14px;
  border-radius: var(--ds-radius-lg);
  background: var(--ds-inverse) !important;
  color: var(--ds-on-inverse) !important;
  box-shadow: var(--ds-shadow-lg);
  border: 0;
  border-left: 4px solid var(--ds-text-3);
}

html[data-ds] .toast.bg-green {
  border-left-color: var(--ds-success);
}

html[data-ds] .toast.bg-amber,
html[data-ds] .toast.bg-orange {
  border-left-color: var(--ds-warning);
}

html[data-ds] .toast.bg-red {
  border-left-color: var(--ds-danger);
}

/* ---------------------------------------------------------------- tabs, trees, misc ----------------- */

html[data-ds] .treeview {
  font-size: var(--ds-text-base);
  color: var(--ds-text);
}

html[data-ds] .treeview li .caption {
  color: var(--ds-text);
}

html[data-ds] .treeview li > .node-toggle::before {
  border-color: var(--ds-text-3) !important;
}

html[data-ds] .tabs > ul.tabs-list > li > a {
  font-size: var(--ds-text-base);
}

html[data-ds] .text-muted,
html[data-ds] .text-secondary {
  color: var(--ds-text-3) !important;
}

html[data-ds] .remark {
  border-radius: var(--ds-radius-md);
  background: var(--ds-warning-soft);
  border-color: var(--ds-warning);
  color: var(--ds-text);
}

/* Metro's checkbox (view options card, settings) */
html[data-ds] .checkbox .check {
  border-radius: 4px;
}

html[data-ds] .checkbox input:checked ~ .check {
  background-color: var(--ds-accent) !important;
  border-color: var(--ds-accent) !important;
}

/* ---------------------------------------------------------------- motion preferences --------------- */

@media (prefers-reduced-motion: reduce) {
  html[data-ds] *,
  html[data-ds] *::before,
  html[data-ds] *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
