dialog {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  background: var(--color-surface-raised);
  color: var(--color-text);
  box-shadow: var(--shadow-md);
  width: min(480px, calc(100vw - 2 * var(--space-4)));
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.4);
}

dialog h2 {
  margin: 0 0 var(--space-4);
  font-size: 1.1rem;
}

/* The edit heading takes focus on touch devices only as a neutral starting
   point (see dialogs.js); it isn't a control, so it shows no ring. */
dialog h2:focus {
  outline: none;
}

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  margin-bottom: var(--space-3);
}

.field label {
  font-size: 0.85rem;
  font-weight: 600;
}

.field input[type='text'],
.field input[type='date'],
.field textarea,
.field select {
  padding: var(--space-2);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  color: var(--color-text);
}

.field [aria-invalid='true'] {
  border-color: var(--color-danger);
}

.field textarea {
  resize: vertical;
  min-height: 4rem;
}

.field-error {
  color: var(--color-danger);
  font-size: 0.82rem;
  margin: 0;
}

.checkbox-field {
  flex-direction: row;
  align-items: center;
  gap: var(--space-2);
}

.checkbox-field input {
  margin: 0;
}

.dialog-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-top: var(--space-4);
}

.dialog-actions-spacer {
  flex: 1;
}

.dialog-actions button {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--color-border-strong);
  background: var(--color-surface);
  color: var(--color-text);
  border-radius: var(--radius-sm);
  padding: var(--space-2) var(--space-3);
  min-height: 24px;
}

.dialog-actions button:hover {
  background: var(--color-hover-raised);
}

.dialog-actions button[type='submit']:hover {
  background: var(--color-inverse-bg);
  opacity: 0.9;
}

.dialog-actions button[type='submit'] {
  background: var(--color-inverse-bg);
  border-color: var(--color-inverse-bg);
  color: var(--color-inverse-text);
  font-weight: 600;
}

.dialog-actions .secondary-button {
  border-color: var(--color-border);
  background: none;
}

/* Settings dialog (design D18, D19). */
.settings-dialog {
  width: min(560px, calc(100vw - 2 * var(--space-4)));
  max-height: calc(100vh - 2 * var(--space-5));
  box-sizing: border-box;
  overflow-y: auto;
  overflow-x: hidden;
}

.settings-dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  margin-bottom: var(--space-2);
}

.settings-dialog-header h2 {
  margin: 0;
  font-size: 1.35rem;
}

.settings-dialog .icon-button:hover,
.settings-dialog .icon-button:focus-visible {
  background: var(--color-hover-raised);
}

.settings-section {
  padding: var(--space-4) 0;
  border-top: 1px solid var(--color-divider);
}

.settings-section:last-of-type {
  padding-bottom: 0;
}

.settings-section h3 {
  margin: 0 0 var(--space-3);
  font-size: 1rem;
  font-weight: 700;
}

.settings-hint {
  margin: 0;
  color: var(--color-text-muted);
  font-size: var(--font-size-meta);
}

.settings-section .checkbox-field {
  margin-bottom: var(--space-1);
}

/* Settings → Data: export/import and copy/paste (design D13, D15). */
.data-row,
.data-confirm,
.data-paste {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  align-items: center;
}

.data-actions {
  display: grid;
  gap: var(--space-2);
}

.data-actions,
.data-confirm,
.data-paste {
  margin-bottom: var(--space-3);
}

.data-confirm p,
.data-paste label,
.data-paste textarea {
  flex-basis: 100%;
  margin: 0;
}

.data-paste label {
  font-size: 0.85rem;
  font-weight: 600;
}

.data-paste textarea {
  box-sizing: border-box;
  font: inherit;
  padding: var(--space-2);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  color: var(--color-text);
  resize: vertical;
  min-height: 4rem;
}

.data-row button,
.data-confirm button,
.data-paste button {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--color-border-strong);
  background: var(--color-surface);
  color: var(--color-text);
  border-radius: var(--radius-sm);
  padding: var(--space-2) var(--space-3);
  min-height: 24px;
}

.data-row button:hover,
.data-confirm button:hover,
.data-paste button:hover {
  background: var(--color-hover-raised);
}

#data-status {
  margin-top: var(--space-2);
  color: var(--color-text);
}

.layout-options {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: var(--space-2);
  margin: 0;
  padding: 0;
  border: none;
  min-width: 0;
}

.layout-options legend {
  padding: 0;
  margin-bottom: var(--space-2);
  font-size: 0.85rem;
  font-weight: 600;
}

.layout-option {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  cursor: pointer;
  min-width: 0;
}

.layout-option:hover {
  background: var(--color-hover-raised);
}

.layout-option:has(input:checked) {
  border-color: var(--color-border-strong);
  box-shadow: inset 0 0 0 1px var(--color-border-strong);
}

.layout-option input {
  width: 18px;
  height: 18px;
  margin: 0;
  flex: 0 0 auto;
  accent-color: var(--color-text);
}

.layout-diagram {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(2, 18px);
  grid-template-rows: repeat(2, 14px);
  gap: 2px;
}

.layout-diagram span {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--color-border-strong);
  border-radius: 2px;
  font-size: 0.6rem;
  font-weight: 600;
  line-height: 1;
  color: var(--color-text-muted);
}

.layout-diagram .is-do {
  background: var(--color-text);
  border-color: var(--color-text);
  color: var(--color-surface-raised);
}

.layout-option-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.layout-option-label {
  font-weight: 600;
}

.layout-option-hint {
  color: var(--color-text-muted);
  font-size: var(--font-size-meta);
}

.shortcut-group + .shortcut-group {
  margin-top: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px solid var(--color-divider);
}

.shortcut-group h4 {
  margin: 0 0 var(--space-2);
  color: var(--color-text-muted);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.shortcut-list {
  margin: 0;
}

.shortcut-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--space-3);
  padding: 5px 0;
  font-size: 0.88rem;
}

.shortcut-row dt {
  min-width: 0;
}

.shortcut-row dd {
  margin: 0;
  text-align: right;
  white-space: nowrap;
}

.shortcut-keys-separator {
  margin: 0 0.3rem;
  color: var(--color-text-muted);
}

@media (max-width: 399.98px) {
  .shortcut-row {
    grid-template-columns: minmax(0, 1fr);
    gap: 2px;
  }

  .shortcut-row dd {
    text-align: left;
    white-space: normal;
  }
}

.toast-container {
  position: fixed;
  bottom: var(--space-4);
  left: 50%;
  transform: translateX(-50%);
  z-index: 900;
}

.toast {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  background: var(--color-surface-raised);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: var(--space-2) var(--space-3);
}

.toast-undo-button {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: none;
  color: var(--color-text);
  font-weight: 600;
  min-height: 24px;
  padding: var(--space-1) var(--space-2);
}

.toast-undo-button:hover {
  background: var(--color-hover-raised);
}

/* The page behind an open dialog doesn't scroll, and scrolling inside a
   dialog doesn't chain to it. */
html:has(dialog[open]) {
  overflow: hidden;
}

dialog {
  overscroll-behavior: contain;
}

/* Small screens: dialogs are full-screen sheets sized to the *visible*
   viewport (src/ui/viewport.js keeps --vv-height / --vv-top current), so
   they sit above the on-screen keyboard instead of under it. The heading
   stays pinned at the top and the edit actions at the bottom while the
   content between them scrolls. */
@media (max-width: 767.98px) {
  dialog,
  .settings-dialog {
    position: fixed;
    inset: auto;
    top: var(--vv-top, 0px);
    left: 0;
    width: 100%;
    max-width: none;
    height: var(--vv-height, 100dvh);
    max-height: none;
    margin: 0;
    padding: 0 var(--space-4);
    border: none;
    border-radius: 0;
    box-sizing: border-box;
    overflow-y: auto;
  }

  #edit-form {
    display: flex;
    flex-direction: column;
    min-height: 100%;
  }

  #edit-dialog h2,
  .settings-dialog-header {
    position: sticky;
    top: 0;
    z-index: 1;
    margin: 0 calc(-1 * var(--space-4)) var(--space-3);
    padding: var(--space-4) var(--space-4) var(--space-3);
    background: var(--color-surface-raised);
    border-bottom: 1px solid var(--color-divider);
  }

  /* Notes take whatever height is left, so the sheet has no dead space. */
  #edit-form .field:has(textarea) {
    flex: 1 0 auto;
  }

  #edit-form textarea {
    flex: 1 0 6rem;
  }

  #edit-form .dialog-actions {
    position: sticky;
    bottom: 0;
    margin: var(--space-2) calc(-1 * var(--space-4)) 0;
    padding: var(--space-3) var(--space-4)
      calc(var(--space-3) + env(safe-area-inset-bottom, 0px));
    background: var(--color-surface-raised);
    border-top: 1px solid var(--color-divider);
  }

  /* Three buttons with their keycaps don't fit a phone's width (the
     non-Apple "Ctrl" + "Enter" pair is the widest), which pushed Save off
     the edge. The shortcuts still work and are listed in Settings. */
  #edit-form .dialog-actions .kbd-group {
    display: none;
  }

  /* Comfortable touch targets for the sheet's buttons. */
  .dialog-actions button,
  .data-row button,
  .data-confirm button,
  .data-paste button {
    min-height: 44px;
    padding-inline: var(--space-4);
  }

  .settings-dialog {
    padding-bottom: calc(var(--space-5) + env(safe-area-inset-bottom, 0px));
  }
}
