/* Build 12.31 — shared selection buttons, nested-surface spacing and form dialogs.
   Native inputs retain their names, values, validation and keyboard behaviour. */
:root {
  --cs-inset: clamp(16px, 1.5vw, 24px);
  --cs-gap: 16px;
  --cs-line: var(--ui-border, #d9e0e8);
  --cs-ink: var(--ui-text, #152033);
  --cs-muted: var(--ui-muted, #667085);
  --cs-primary: var(--ui-primary, var(--m-brand, #1769ff));
  --cs-choice-bg: color-mix(in srgb, var(--cs-primary), white 93%);
}
/* A selection is one complete hit target, not a checkbox inside a button. */
body label.cs-choice {
  position: relative; display: inline-flex !important; flex-direction: row !important;
  align-items: center !important; justify-content: flex-start; gap: 9px !important;
  width: auto; max-width: 100%; min-width: 0; min-height: 42px;
  margin: 0; padding: 10px 14px !important; border: 1px solid var(--cs-line) !important;
  border-radius: 9px; background: var(--ui-surface, #fff) !important;
  color: var(--cs-ink) !important; font-size: 13px; font-weight: 550; line-height: 1.4;
  cursor: pointer; user-select: none; box-shadow: none;
  transition: background-color .12s ease, border-color .12s ease, box-shadow .12s ease;
  overflow-wrap: anywhere; vertical-align: middle;
}
body label.cs-choice:hover { border-color: var(--ui-border-strong, #aab8c8) !important; background: var(--ui-surface-soft, #f7f9fc) !important; }
body label.cs-choice::after {
  content: '\2713'; display: inline-block; flex: 0 0 1em; margin-inline-start: auto;
  font-size: 14px; font-weight: 750; line-height: 1; visibility: hidden;
}
body label.cs-choice:has(.cs-choice-input:checked),
body label.cs-choice:has(.cs-choice-input:indeterminate) {
  border-color: var(--cs-primary) !important; background: var(--cs-choice-bg) !important;
  box-shadow: inset 0 0 0 1px var(--cs-primary); color: var(--cs-ink) !important;
}
body label.cs-choice:has(.cs-choice-input:checked)::after,
body label.cs-choice:has(.cs-choice-input:indeterminate)::after { visibility: visible; }
body label.cs-choice:has(.cs-choice-input:indeterminate)::after { content: '\2212'; }
body label.cs-choice:has(.cs-choice-input:focus-visible) { outline: 2px solid var(--cs-primary); outline-offset: 3px; }
body label.cs-choice:has(.cs-choice-input:disabled) { opacity: .55; cursor: not-allowed; }
body label.cs-choice > :is(span, div) { min-width: 0; }
/* Do not use display:none/visibility:hidden on the input: it remains focusable. */
body input.cs-choice-input[type=checkbox], body input.cs-choice-input[type=radio] {
  position: absolute !important; width: 1px !important; height: 1px !important;
  min-width: 0 !important; min-height: 0 !important; margin: -1px !important;
  padding: 0 !important; overflow: hidden !important; clip: rect(0,0,0,0) !important;
  clip-path: inset(50%) !important; white-space: nowrap !important;
  border: 0 !important; box-shadow: none !important;
}
body label.cs-choice.cs-choice--compact { padding: 7px 9px !important; min-height: 34px; font-size: 12px; flex: none; }
body .cs-choice--compact::after { flex-basis: .85em; font-size: 12px; }
body label.cs-choice.cs-choice--block { width: 100%; }
body .cs-choice .cs-choice-label-off, body .cs-choice .cs-choice-label-on { white-space: nowrap; }
body .cs-choice:has(input:checked) .cs-choice-label-off,
body .cs-choice:not(:has(input:checked)) .cs-choice-label-on { display: none; }
/* Shared spacing contract. Intentionally full-bleed tables/calendar cells are excluded. */
body :is(.panel, .card, .summary-card, .section-card):not([data-ui-edge]):has(> :is(.panel, .card, .summary-card, .section-card)) {
  padding: var(--cs-inset);
}
body :is(.panel, .card, .summary-card, .section-card) > :is(.panel, .card, .summary-card, .section-card) { margin-inline: 0; }
body :is(.panel, .card, .summary-card, .section-card) > :is(.panel, .card, .summary-card, .section-card) + :is(.panel, .card, .summary-card, .section-card) { margin-block-start: var(--cs-gap); }
body :is(.panel, .card, .summary-card, .section-card) > :is(.panel, .card, .summary-card, .section-card):last-child { margin-block-end: 0; }
body :is(.panel, .card, .section-card) > form > :is(.panel, .card, .section-card) + :is(.panel, .card, .section-card) { margin-block-start: var(--cs-gap); }
body :is(.panel, .card, .section-card) > :is(.field-grid, .form-grid, .grid-2, .grid-3) + :is(.panel, .card, .section-card, .field-grid, .form-grid) { margin-block-start: var(--cs-gap); }
body .cs-surface-stack > :is(.panel,.card,.summary-card,.section-card,.ag-card) + :is(.panel,.card,.summary-card,.section-card,.ag-card) { margin-block-start: var(--cs-gap); }
body .cs-surface-layout > :is(.panel,.card,.summary-card,.section-card,.ag-card) { margin-block: 0 !important; }
body form > :is(.inline-form,.field-grid,.form-grid) + :is(.inline-form,.field-grid,.form-grid,button) { margin-block-start: var(--cs-gap); }
body .cs-surface-grid { gap: var(--cs-gap) !important; }
body .cs-surface-inset { padding: var(--cs-inset) !important; }
body :is(.panel, .card, .section-card, .ag-card) > :is(h2, h3, p):last-child { margin-bottom: 0; }
/* Calendar locations: separate the nested panels from each other and the shell. */
.ag .ag-locations { padding: var(--cs-inset); gap: var(--cs-gap); align-items: start; grid-template-columns: minmax(0,1fr) minmax(0,1fr); }
.ag .ag-locations > * { min-width: 0; }
.ag .ag-locations > .ag-card { margin: 0; box-shadow: none; }
.ag .ag-locations .ag-empty { margin: 0; padding: 32px 16px; border-radius: 9px; background: var(--ag-soft, #f7f9fc); }
.ag .ag-footnote { margin: 0; padding: 16px var(--cs-inset); }
.ag .ag-filters { gap: 12px; }
.ag .ag-filters > label.cs-choice { flex: 0 1 auto; align-self: flex-end; min-height: 42px; }
.ag .ag-choice-grid { gap: 10px; padding: 4px; margin: -4px; }
.ag .ag-choice-grid > label.cs-choice { width: 100%; }
.ag .ag-card > form.ag-stack { padding-block-start: 16px; border-top: 1px solid var(--cs-line); }
.ag .ag-card > .ag-alert { margin-bottom: 0; }
/* Native dialogs supply focus containment and top-layer stacking. */
body.cs-dialog-active { overflow: hidden; }
dialog.cs-dialog {
  box-sizing: border-box; width: min(880px, calc(100vw - 40px)); max-width: none;
  max-height: calc(100dvh - 48px); margin: auto; padding: 0; overflow: hidden;
  color: var(--cs-ink); background: #fff; border: 1px solid var(--cs-line);
  border-radius: 16px; box-shadow: 0 24px 80px #15223438;
}
dialog.cs-dialog:not([open]) { display: none !important; }
dialog.cs-dialog[open] { display: flex; flex-direction: column; }
dialog.cs-dialog::backdrop { background: rgb(16 26 42 / 48%); }
.cs-dialog-header { display: flex; align-items: start; justify-content: space-between; gap: 20px; padding: 20px 24px; flex: none; border-bottom: 1px solid var(--cs-line); }
.cs-dialog-header h2 { margin: 0; font-size: 21px; line-height: 1.3; color: var(--cs-ink); }
.cs-dialog-header p { margin: 6px 0 0; font-size: 13px; color: var(--cs-muted); line-height: 1.5; }
.cs-dialog-header-actions { display: flex; align-items: center; gap: 12px; flex: none; }
.cs-dialog-page-link { color: var(--cs-muted); font-size: 12px; text-decoration: underline; }
.cs-dialog-close { display: grid; place-items: center; flex: none; width: 36px; height: 36px; padding: 0; border: 1px solid var(--cs-line); border-radius: 9px; background: #fff; color: var(--cs-ink); font: inherit; font-size: 25px; cursor: pointer; }
.cs-dialog-body { padding: 24px; overflow: auto; min-height: 0; overscroll-behavior: contain; }
.cs-dialog .ag-editor { gap: 18px; }
/* Mobile shell does not import the desktop button reset. */
.cs-event-modal .btn { display:inline-flex;align-items:center;justify-content:center;gap:8px;min-height:42px;padding:10px 14px;border:1px solid var(--cs-primary);border-radius:8px;background:var(--cs-primary);color:#fff;font:inherit;font-size:14px;font-weight:600;cursor:pointer; }
.cs-event-modal .btn.secondary { background:#fff;color:var(--cs-ink);border-color:var(--cs-line); }
.cs-event-modal .btn:focus-visible { outline:2px solid var(--cs-primary);outline-offset:3px; }

.cs-dialog .ag-editor > .ag-card:first-of-type { border: 0; border-radius: 0; padding: 0; box-shadow: none; }
.cs-dialog .ag-editor .ag-savebar { position: sticky; bottom: -24px; margin: 0 -24px -24px; border: 0; border-top: 1px solid var(--cs-line); border-radius: 0; padding: 16px 24px; box-shadow: none; z-index: 8; }
.cs-dialog .ag-editor > details { padding: 18px; }
.cs-dialog .ag-editor label:not(.cs-choice), .cs-dialog .ag-editor input:not([type=checkbox]):not([type=radio]), .cs-dialog .ag-editor select, .cs-dialog .ag-editor textarea { min-width: 0; }
.cs-dialog .ag-editor input:not([type=checkbox]):not([type=radio]), .cs-dialog .ag-editor select, .cs-dialog .ag-editor textarea { width: 100%; box-sizing: border-box; }
.cs-dialog .ag-editor .ag-fields label:not(.cs-choice) { display: flex; flex-direction: column; gap: 7px; font-size: 13px; }
.cs-dialog-discard { flex: none; border-top: 1px solid #e7c791; background: #fff8eb; padding: 16px 24px; }
.cs-dialog-discard p { margin: 0 0 12px; font-size: 14px; color: #634515; }
.cs-dialog-discard > div { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }
.cs-dialog-discard button { border: 1px solid var(--cs-line); border-radius: 8px; padding: 9px 13px; background: white; color: var(--cs-ink); cursor: pointer; font: inherit; }
.cs-dialog-discard [data-cs-discard-confirm] { border-color: #b42318; color: #9c2118; }
.cs-dialog [hidden] { display: none !important; }
.cs-dialog-status { margin: 0; padding: 16px; color: var(--cs-muted); font-size: 14px; line-height: 1.6; }
.cs-dialog-error { color: #9c2118; background: #fff1ef; border-radius: 8px; padding: 16px; margin-bottom: 16px; }
.cs-ui-toast { position: fixed; z-index: 1700; bottom: 24px; left: 50%; transform: translateX(-50%); max-width: calc(100vw - 32px); display: flex; align-items: center; flex-wrap: wrap; gap: 14px; padding: 14px 18px; border: 1px solid var(--cs-line); border-radius: 12px; background: #fff; box-shadow: 0 8px 32px #1522342a; color: var(--cs-ink); font-size: 14px; }
.cs-ui-toast a { text-decoration: underline; font-weight: 600; }
.cs-ui-toast button { border: 0; background: transparent; color: var(--cs-muted); padding: 4px 8px; font-size: 20px; cursor: pointer; }
/* To-do editors share the same modal dimensions, not the narrow widget width. */
#cs-todo-panel.cs-todo dialog.cs-dialog { width: min(780px, calc(100vw - 40px)); }
#cs-todo-panel.cs-todo .td-editor-dialog .td-detail-pane { display: flex; flex-direction: column; min-height: 0; overflow: hidden; border: 0; height: auto; width: 100%; }
#cs-todo-panel.cs-todo .td-editor-dialog .td-detail-head { padding: 18px 24px; }
#cs-todo-panel.cs-todo .td-editor-dialog .td-detail-head strong { font-size: 20px; }
#cs-todo-panel.cs-todo .td-editor-dialog .td-detail-scroll { padding: 24px; overflow: auto; max-height: calc(100dvh - 145px); }
#cs-todo-panel.cs-todo .td-editor-dialog .td-form-grid { gap: 18px; }
#cs-todo-panel.cs-todo .td-editor-dialog .td-help { font-size: 12px; line-height: 1.6; }
#cs-todo-panel.cs-todo .td-editor-dialog .td-form-actions { position: sticky; bottom: -24px; margin: 20px -24px -24px; padding: 16px 24px; background: #fff; z-index: 3; }
#cs-todo-panel.cs-todo .td-editor-dialog .td-error { flex: none; margin: 12px 24px 0; }
#cs-todo-panel.cs-todo .td-editor-dialog .cs-dialog-discard { margin: 0; }
#cs-todo-panel.cs-todo .td-row { grid-template-columns: auto minmax(0,1fr); }
#cs-todo-panel.cs-todo .td-row > label.cs-choice { margin-top: 2px; }
#cs-todo-panel.cs-todo .td-check > label.cs-choice { flex: 0 0 auto; }
#cs-todo-panel.cs-todo label.cs-choice { width: auto; }
@media (max-width: 760px) {
  .ag .ag-locations { grid-template-columns: minmax(0,1fr); }
}
@media (max-width: 600px) {
  :root { --cs-inset: 16px; --cs-gap: 14px; }
  body label.cs-choice { min-height: 42px; padding: 9px 12px !important; }
  dialog.cs-dialog, #cs-todo-panel.cs-todo dialog.cs-dialog { width: 100%; max-width: 100%; max-height: 100dvh; height: 100dvh; margin: 0; border: 0; border-radius: 0; }
  .cs-dialog-header { padding: max(16px, env(safe-area-inset-top)) 16px 16px; gap: 10px; }
  .cs-dialog-header h2 { font-size: 19px; }
  .cs-dialog-header-actions { gap: 8px; }
  .cs-dialog-page-link { max-width: 64px; }
  .cs-dialog-body { padding: 16px; }
  .cs-dialog .ag-editor .ag-savebar { bottom: -16px; margin: 0 -16px -16px; padding: 14px 16px max(14px, env(safe-area-inset-bottom)); }
  .cs-dialog .ag-editor input:not([type=checkbox]):not([type=radio]), .cs-dialog .ag-editor select, .cs-dialog .ag-editor textarea { font-size: 16px; }
  #cs-todo-panel.cs-todo .td-editor-dialog .td-detail-head { padding: max(16px, env(safe-area-inset-top)) 16px 16px; }
  #cs-todo-panel.cs-todo .td-editor-dialog .td-detail-scroll { padding: 16px; max-height: none; }
  #cs-todo-panel.cs-todo .td-editor-dialog .td-form-grid { grid-template-columns: 1fr; gap: 16px; }
  #cs-todo-panel.cs-todo .td-editor-dialog .td-form-actions { bottom: -16px; margin: 20px -16px -16px; padding: 14px 16px max(14px, env(safe-area-inset-bottom)); }
  .cs-dialog-discard { padding: 14px 16px; }
}
@media (prefers-reduced-motion: reduce) { body label.cs-choice { transition: none; } }
@media (forced-colors: active) {
  body label.cs-choice:has(input:checked), body label.cs-choice:has(input:indeterminate) { border-color: Highlight !important; outline: 2px solid Highlight; }
}
@media print {
  dialog.cs-dialog, .cs-ui-toast { display: none !important; }
  body label.cs-choice { border-color: #888 !important; box-shadow: none; }
}
