/* Contract dates are a duration guide, not a measure of performance.
   All presentation is scoped to the contract controls; theme tokens stay local. */
@media screen {
  :root {
    --contract-accent: #285a4b;
    --contract-accent-fill: #dfece3;
    --contract-accent-soft: #f2f7f1;
    --contract-accent-border: #b9d1be;
    --contract-tab-fill: #e6eee5;
    --contract-tab-text: #244b3b;
    --contract-tab-border: #b9ccba;
    --contract-warning: #80551c;
    --contract-warning-fill: #f6e8c8;
    --contract-warning-border: #d9bc7f;
    --contract-danger: #98423e;
    --contract-danger-fill: #f4dfdc;
    --contract-danger-border: #d6aaa5;
    --contract-neutral: #64706b;
    --contract-neutral-fill: #e9eeea;
    --contract-neutral-border: #c7d0c8;
    --contract-track: #e7ede5;
    --contract-notice: #846221;
    --contract-grid: #e8ede5;
    --contract-row-hover: #f7faf3;
    --contract-selected: #edf4e9;
    --contract-label-width: 232px;
    --contract-lane-width: 768px;
  }

  html[data-workbench-theme="studio"] {
    --contract-accent: #506b30;
    --contract-accent-fill: #dceba9;
    --contract-accent-soft: #f5f8ed;
    --contract-accent-border: #bfd18d;
    --contract-tab-fill: #e9e1f2;
    --contract-tab-text: #675077;
    --contract-tab-border: #dacbe7;
    --contract-warning: #84581e;
    --contract-warning-fill: #f9ebce;
    --contract-warning-border: #e0c18b;
    --contract-danger: #96484b;
    --contract-danger-fill: #f6e2e4;
    --contract-danger-border: #dfb3b7;
    --contract-neutral: #705f83;
    --contract-neutral-fill: #eae3f2;
    --contract-neutral-border: #d1c4df;
    --contract-track: #edf0e7;
    --contract-notice: #806129;
    --contract-grid: #edf0e8;
    --contract-row-hover: #f9fbf5;
    --contract-selected: #f3f7e8;
  }

  .contract-view-switch {
    display: inline-flex; align-items: center; flex-wrap: nowrap; gap: 4px;
    min-width: 0; max-width: 100%; padding: 4px;
    border: 1px solid var(--line); border-radius: calc(var(--workbench-control-radius, 6px) + 4px);
    background: var(--surface-subtle); vertical-align: middle;
  }
  html[data-workbench-theme] #table-toolbar .contract-view-switch > button {
    min-width: 76px; min-height: 44px; padding: 9px 15px;
    border: 1px solid transparent; border-radius: var(--workbench-control-radius, 6px);
    background: transparent; color: var(--muted); font-size: 12px; font-weight: 550;
    line-height: 1.5; white-space: normal;
  }
  html[data-workbench-theme] #table-toolbar .contract-view-switch > button[aria-pressed="true"] {
    background: var(--contract-tab-fill); color: var(--contract-tab-text);
    border-color: var(--contract-tab-border); font-weight: 650; box-shadow: none;
  }
  html[data-workbench-theme] #table-toolbar .contract-view-switch > button:not(:disabled):hover {
    background: var(--contract-accent-soft); color: var(--text); filter: none;
  }
  html[data-workbench-theme] #table-toolbar .contract-view-switch > button[aria-pressed="true"]:hover {
    background: var(--contract-tab-fill); color: var(--contract-tab-text);
  }

  /* The full record width is available until a contract detail is opened. */
  html[data-workbench-theme] .split-view.contract-browse-layout:not(.detail-open) {
    grid-template-columns: minmax(0, 1fr);
  }
  .contract-browse-layout > .record-panel { min-width: 0; max-width: 100%; border-right: 0; }
  .contract-timeline-layout > .record-panel,
  .contract-timeline-layout .record-list { min-width: 0; max-width: 100%; }
  .contract-timeline-layout .record-list { max-height: none; overflow: visible; }

  .contract-term, .contract-timeline-row {
    --contract-state: var(--contract-accent);
    --contract-state-fill: var(--contract-accent-fill);
    --contract-state-border: var(--contract-accent-border);
  }
  :is(.contract-term, .contract-timeline-row)[data-tone="warning"] {
    --contract-state: var(--contract-warning);
    --contract-state-fill: var(--contract-warning-fill);
    --contract-state-border: var(--contract-warning-border);
  }
  :is(.contract-term, .contract-timeline-row)[data-tone="danger"] {
    --contract-state: var(--contract-danger);
    --contract-state-fill: var(--contract-danger-fill);
    --contract-state-border: var(--contract-danger-border);
  }
  :is(.contract-term, .contract-timeline-row)[data-tone="neutral"] {
    --contract-state: var(--contract-neutral);
    --contract-state-fill: var(--contract-neutral-fill);
    --contract-state-border: var(--contract-neutral-border);
  }

  /* A bounded, wrapping component also works inside a resized table column. */
  .contract-term { display: grid; gap: 7px; width: 100%; min-width: 0; max-width: 360px; white-space: normal; }
  .ledger-table .contract-term { width: 100%; max-width: 250px; min-width: 0; margin-block: 3px; }
  .record-row .contract-term { margin-top: 12px; max-width: none; }
  .contract-term-heading { display: flex; align-items: baseline; justify-content: space-between; flex-wrap: wrap; gap: 3px 8px; }
  .contract-term-status { color: var(--contract-state); font-size: 12px; font-weight: 650; line-height: 1.5; overflow-wrap: anywhere; }
  .contract-term-progress-label { color: var(--muted); font-size: 12px; line-height: 1.5; font-variant-numeric: tabular-nums; }
  .contract-term-track { position: relative; height: 6px; margin-block: 2px; border-radius: 5px; background: var(--contract-track); }
  .contract-term-fill { display: block; height: 100%; max-width: 100%; border-radius: inherit; background: var(--contract-state); }
  .contract-term-notice-marker {
    position: absolute; z-index: 1; top: 50%; width: 9px; height: 9px;
    border: 1px solid var(--surface); background: var(--contract-notice); transform: translate(-50%, -50%) rotate(45deg);
  }
  .contract-term-dates { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 3px 8px; color: var(--muted); font-size: 11px; line-height: 1.5; font-variant-numeric: tabular-nums; }
  .contract-term-dates > span { max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; overflow-wrap: normal; }
  .contract-term-notice { color: var(--contract-state); font-size: 12px; line-height: 1.5; overflow-wrap: anywhere; }

  .contract-timeline { min-width: 0; max-width: 100%; padding: 20px 18px 18px; background: var(--surface); color: var(--text); }
  .contract-timeline-toolbar { display: flex; align-items: flex-start; justify-content: space-between; flex-wrap: wrap; gap: 12px 20px; margin-bottom: 10px; }
  .contract-timeline-title { min-width: 0; flex: 1 1 210px; }
  h3.contract-timeline-title, .contract-timeline-title h3 { margin: 0 0 4px; font-size: 17px; font-weight: 650; letter-spacing: -.2px; line-height: 1.5; }
  .contract-timeline-title p { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.6; }
  .contract-timeline-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; max-width: 100%; }
  html[data-workbench-theme] .contract-timeline-controls > button {
    min-width: 44px; min-height: 44px; padding: 9px 12px; color: var(--text);
    border: 1px solid var(--line); background: var(--surface); font-size: 12px; line-height: 1.5;
  }
  html[data-workbench-theme] .contract-timeline-controls > button:not(:disabled):hover { background: var(--contract-accent-soft); border-color: var(--contract-accent-border); }
  .contract-timeline-summary { margin: 0 0 10px; color: var(--muted); font-size: 12px; line-height: 1.7; overflow-wrap: anywhere; }
  .contract-timeline-legend { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 18px; margin: 0 0 16px; color: var(--muted); font-size: 11px; line-height: 1.6; }
  .contract-timeline-legend > span { display: inline-flex; align-items: center; gap: 7px; }
  .contract-timeline-legend-today::before { content: ""; width: 2px; height: 14px; background: var(--contract-accent); }
  .contract-timeline-legend-notice::before { content: ""; width: 8px; height: 8px; margin: 2px; background: var(--contract-notice); transform: rotate(45deg); }

  /* Only this area scrolls sideways; the name column stays visible within it. */
  .contract-timeline-scroll {
    position: relative; width: 100%; min-width: 0; max-width: 100%; overflow-x: auto;
    border: 1px solid var(--line); border-radius: var(--workbench-control-radius, 6px);
    background: var(--surface); overscroll-behavior-x: contain;
    scrollbar-width: thin; scrollbar-color: var(--contract-neutral-border) var(--surface-subtle);
    scroll-padding-inline: var(--contract-label-width) 24px;
  }
  .contract-timeline-scroll:focus-visible { outline: 2px solid var(--focus); outline-offset: 3px; }
  .contract-timeline-grid { width: 100%; min-width: calc(var(--contract-label-width) + var(--contract-lane-width)); }
  .contract-timeline-header, .contract-timeline-row { display: grid; grid-template-columns: var(--contract-label-width) minmax(0, 1fr); }
  .contract-timeline-header { min-height: 50px; background: var(--surface-subtle); color: var(--muted); border-bottom: 1px solid var(--line); }
  .contract-timeline-label {
    position: sticky; left: 0; z-index: 5; min-width: 0; padding: 14px 16px;
    border-right: 1px solid var(--line); background: var(--surface);
    box-shadow: 5px 0 8px -8px #26312c55; overflow-wrap: anywhere;
  }
  .contract-timeline-header > .contract-timeline-label { display: flex; align-items: center; z-index: 7; padding-block: 10px; background: var(--surface-subtle); font-size: 11px; font-weight: 600; }
  .contract-timeline-months { position: relative; display: flex; min-width: 0; min-height: 50px; }
  .contract-timeline-month { display: flex; flex: 0 0 auto; justify-content: center; align-items: center; padding: 6px 2px; min-width: 0; border-left: 1px solid var(--contract-grid); font-size: 11px; line-height: 1.4; font-variant-numeric: tabular-nums; white-space: nowrap; }
  .contract-timeline-row { min-height: 130px; border-bottom: 1px solid var(--line); }
  .contract-timeline-row:last-child { border-bottom: 0; }
  .contract-timeline-row:hover, .contract-timeline-row:hover > .contract-timeline-label { background: var(--contract-row-hover); }
  .contract-timeline-row[data-selected="true"], .contract-timeline-row[data-selected="true"] > .contract-timeline-label { background: var(--contract-selected); }
  .contract-timeline-row[data-selected="true"] > .contract-timeline-label { box-shadow: inset 3px 0 var(--contract-accent), 5px 0 8px -8px #26312c55; }
  .contract-timeline-name { margin: 0; }
  html[data-workbench-theme] button.contract-timeline-name {
    display: block; width: 100%; min-width: 0; min-height: 44px; padding: 0;
    border: 0; border-radius: 4px; background: transparent; color: var(--text);
    text-align: left; font-size: 13px; font-weight: 650; line-height: 1.55; white-space: normal; overflow-wrap: anywhere;
  }
  html[data-workbench-theme] button.contract-timeline-name:not(:disabled):hover { background: transparent; color: var(--contract-accent); text-decoration: underline; text-underline-offset: 3px; }
  .contract-timeline-counterparty { margin: 2px 0 5px; color: var(--muted); font-size: 12px; line-height: 1.5; }
  .contract-timeline-status { display: inline-block; margin-top: 2px; padding: 2px 6px; border: 1px solid var(--contract-state-border); border-radius: 5px; background: var(--contract-state-fill); color: var(--contract-state); font-size: 11px; line-height: 1.5; font-weight: 600; }
  .contract-timeline-dates, .contract-timeline-note { margin-top: 5px; color: var(--muted); font-size: 11px; line-height: 1.5; font-variant-numeric: tabular-nums; }
  .contract-timeline-note { color: var(--contract-state); font-size: 12px; }
  :is(.contract-term-notice, .contract-timeline-note)[data-tone="warning"] { color: var(--contract-warning); }
  :is(.contract-term-notice, .contract-timeline-note)[data-tone="danger"] { color: var(--contract-danger); }
  :is(.contract-term-notice, .contract-timeline-note)[data-tone="neutral"] { color: var(--contract-neutral); }
  .contract-timeline-lane { position: relative; min-width: 0; min-height: 130px; }
  .contract-timeline-month-line { position: absolute; z-index: 0; inset-block: 0; border-left: 1px solid var(--contract-grid); pointer-events: none; }
  html[data-workbench-theme] .contract-timeline-bar {
    position: absolute; z-index: 2; top: 50%; transform: translateY(-50%); display: block;
    min-width: 2px; min-height: 44px; max-width: 100%; padding: 0;
    border: 1px solid var(--contract-state-border); border-radius: 7px;
    background: var(--contract-state-fill); color: var(--contract-state);
    font-size: 11px; font-weight: 600; line-height: 1.5; text-align: left; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .contract-timeline-bar-label { display: block; padding: 9px 10px; overflow: hidden; text-overflow: ellipsis; }
  html[data-workbench-theme] .contract-timeline-bar:not(:disabled):hover { background: var(--contract-state-fill); border-color: var(--contract-state); filter: none; box-shadow: 0 0 0 1px var(--contract-state); }
  html[data-workbench-theme] .contract-timeline-bar[data-clipped-start="true"] { border-left: 2px dashed var(--contract-state); border-top-left-radius: 0; border-bottom-left-radius: 0; }
  html[data-workbench-theme] .contract-timeline-bar[data-clipped-end="true"] { border-right: 2px dashed var(--contract-state); border-top-right-radius: 0; border-bottom-right-radius: 0; }
  .contract-timeline-notice { position: absolute; z-index: 4; top: 50%; width: 12px; height: 12px; background: var(--contract-notice); border: 2px solid var(--surface); transform: translate(-50%, -50%) rotate(45deg); pointer-events: none; }
  :is(.contract-term-notice-marker, .contract-timeline-notice)[data-tone="neutral"] { background: var(--contract-neutral); }
  :is(.contract-term-notice-marker, .contract-timeline-notice)[data-tone="warning"] { background: var(--contract-warning); }
  :is(.contract-term-notice-marker, .contract-timeline-notice)[data-tone="danger"] { background: var(--contract-danger); }
  .contract-timeline-today { position: absolute; z-index: 3; top: 0; bottom: 0; width: 2px; background: var(--contract-accent); pointer-events: none; }
  .contract-timeline-today > span { position: absolute; top: 2px; left: 50%; transform: translateX(-50%); padding: 1px 5px; border-radius: 4px; background: var(--contract-accent); color: #fff; font-size: 10px; line-height: 1.5; white-space: nowrap; }
  .contract-timeline-outside { position: relative; z-index: 1; display: flex; align-items: center; min-height: 130px; max-width: 100%; padding: 20px; color: var(--muted); font-size: 12px; line-height: 1.7; }
  .contract-timeline-empty { margin: 0; padding: 32px 16px; color: var(--muted); text-align: center; font-size: 13px; }
}

@media screen and (max-width: 1100px) {
  html[data-workbench-theme] .split-view.contract-timeline-layout.detail-open:not(.full-width) { grid-template-columns: minmax(0, 1fr); }
  .contract-timeline-layout > .detail { border-top: 1px solid var(--line); }
}

@media screen and (max-width: 650px) {
  :root { --contract-label-width: 148px; --contract-lane-width: 720px; }
  html[data-workbench-theme] .contract-browse-layout #table-toolbar { flex-basis: 100%; width: 100%; min-width: 0; }
  .contract-view-switch { width: 100%; }
  html[data-workbench-theme] #table-toolbar .contract-view-switch > button { flex: 1 1 0; min-width: 0; padding-inline: 10px; white-space: nowrap; }
  .contract-timeline { padding: 16px 12px; }
  .contract-timeline-toolbar { gap: 10px; }
  .contract-timeline-title { flex-basis: 100%; }
  h3.contract-timeline-title, .contract-timeline-title h3 { font-size: 16px; }
  .contract-timeline-controls { width: 100%; }
  html[data-workbench-theme] .contract-timeline-controls > button { flex: 1 1 auto; padding-inline: 8px; }
  .contract-timeline-legend { gap: 6px 12px; margin-bottom: 12px; }
  .contract-timeline-label { padding: 12px 10px; }
  html[data-workbench-theme] button.contract-timeline-name { font-size: 12px; }
  .contract-timeline-dates { max-width: 120px; }
  .contract-timeline-status { max-width: 100%; }
}

@media screen and (prefers-reduced-motion: reduce) {
  .contract-timeline-scroll { scroll-behavior: auto; }
  .contract-term *, .contract-timeline *, .contract-view-switch > button { transition: none; animation: none; }
}

@media screen and (forced-colors: active) {
  .contract-term, .contract-timeline, .contract-view-switch { --contract-state: CanvasText; --contract-state-fill: Canvas; --contract-state-border: CanvasText; --contract-grid: GrayText; --contract-accent: Highlight; }
  .contract-term-track { border: 1px solid CanvasText; background: Canvas; }
  .contract-term-fill { background: Highlight; forced-color-adjust: none; }
  :is(.contract-term-notice-marker, .contract-timeline-notice), :is(.contract-term-notice-marker, .contract-timeline-notice)[data-tone] { background: CanvasText; border-color: Canvas; forced-color-adjust: none; }
  .contract-timeline-today, .contract-timeline-legend-today::before { background: Highlight; forced-color-adjust: none; }
  .contract-timeline-legend-notice::before { background: CanvasText; forced-color-adjust: none; }
  .contract-timeline-label, .contract-timeline-scroll { background: Canvas; border-color: CanvasText; box-shadow: none; }
  html[data-workbench-theme] .contract-timeline-bar { border-color: CanvasText; background: Canvas; color: CanvasText; }
  html[data-workbench-theme] #table-toolbar .contract-view-switch > button[aria-pressed="true"] { border-color: Highlight; background: Highlight; color: HighlightText; }
  .contract-timeline-row[data-selected="true"] > .contract-timeline-label { border-inline-start: 3px solid Highlight; box-shadow: none; }
}
