/* Food Diary */

/* Day navigation */

.np-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin: 16px 0 24px;
  font-size: 0.9rem;
}

.np-nav a,
.np-nav .disabled-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  padding: 8px 14px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-small);
  text-decoration: none;
}

.np-nav a {
  color: var(--color-plum);
  background: var(--color-surface);
  transition: background-color 150ms ease, border-color 150ms ease;
}

.np-nav a:hover {
  background: var(--color-surface-muted);
  border-color: var(--color-plum-light);
}

.np-nav .disabled-link {
  color: var(--color-text-muted);
  background: var(--color-surface-muted);
  border-color: transparent;
  cursor: not-allowed;
}

/* Diary date and entry forms */

#diary form {
  max-width: 100%;
}

#diary input[type="date"] {
  max-width: 100%;
}

/* Keep the diary table within its panel on desktop. */
@media (min-width: 1025px) {
  .responsive-table {
    table-layout: auto;
  }

  .responsive-table th,
  .responsive-table td {
    padding: 12px 8px;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  .responsive-table th:last-child,
  .responsive-table td:last-child {
    width: 76px;
  }

  .responsive-table td.diary-actions {
    overflow-wrap: normal;
  }
}

/* Responsive layout */

@media (max-width: 600px) {
  .page-container > h2 {
    font-size: 1.7rem;
  }

  .panel > h2 {
    font-size: 1.25rem;
  }
  .np-nav {
    gap: 8px;
  }

  .np-nav a,
  .np-nav .disabled-link {
    padding: 8px 10px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .np-nav a {
    transition: none;
  }
}

.carbohydrate-convention-notice {
  margin-block: 0 1rem;
  padding: 1rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-card);
  background: var(--color-surface-muted);
}

.carbohydrate-convention-notice p {
  margin: 0 0 0.75rem;
}

.carbohydrate-convention-notice .checkbox-label {
  min-height: 1.75rem;
}

/* Give diary actions a quieter, consistent hover treatment. */
td.diary-actions a,
td.diary-actions button.nobutton {
  transition: background-color 150ms ease, color 150ms ease;
}

td.diary-actions a {
  color: var(--color-plum);
}

td.diary-actions button.nobutton {
  color: var(--color-danger);
}

td.diary-actions a:hover {
  background: var(--color-surface-muted);
}

td.diary-actions button.nobutton:hover {
  background: var(--color-danger-background);
}

@media (prefers-reduced-motion: reduce) {
  td.diary-actions a,
  td.diary-actions button.nobutton {
    transition: none;
  }
}

/* Meal macro subtotals */
.responsive-table .meal-subtotal-row th,
.responsive-table .meal-subtotal-row td {
  background: var(--color-surface-muted);
  border-top: 1px solid var(--color-border);
  font-weight: 700;
}

.responsive-table .meal-subtotal-row th {
  text-align: left;
}

.responsive-table .meal-subtotal-row .diary-subtotal-actions {
  background: var(--color-surface-muted);
}

@media (max-width: 1024px) {
  .responsive-table .meal-subtotal-row th[colspan] {
    text-align: left;
  }
}

/* Group each meal and its subtotal into one card at the responsive-table breakpoint. */
@media (max-width: 1024px) {
  .responsive-table tbody.meal-group {
    display: block;
    width: 100%;
    margin-bottom: 16px;
    padding: 8px 16px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-card);
  }

  .responsive-table tbody.meal-group:last-of-type {
    margin-bottom: 0;
  }

  .responsive-table tbody.meal-group tr {
    display: block;
    margin: 0;
    padding: 0;
    background: transparent;
    border: 0;
    border-radius: 0;
  }

  .responsive-table tbody.meal-group tr:hover {
    background: transparent;
  }

  .responsive-table tbody.meal-group td {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 16px;
    padding: 10px 0;
    border-bottom: 1px solid var(--color-border);
    white-space: normal;
    text-align: right;
    overflow-wrap: anywhere;
  }

  .responsive-table tbody.meal-group td::before {
    content: attr(data-label);
    flex: 0 0 auto;
    color: var(--color-text-muted);
    font-size: 0.85rem;
    font-weight: 600;
    text-align: left;
    overflow-wrap: normal;
  }

  .responsive-table tbody.meal-group tr:not(.meal-subtotal-row) td:first-child {
    color: var(--color-heading);
    font-weight: 700;
  }

  .responsive-table tbody.meal-group .meal-subtotal-row {
    margin: 8px -8px 0;
    padding: 8px;
    background: var(--color-surface-muted);
    border-radius: var(--radius-small);
  }

  .responsive-table tbody.meal-group .meal-subtotal-row th {
    display: block;
    padding: 8px 0;
    text-align: left;
    border: 0;
    background: transparent;
  }

  .responsive-table tbody.meal-group .meal-subtotal-row td {
    padding: 7px 0;
    border-bottom: 1px solid var(--color-border);
    background: transparent;
  }

  .responsive-table tbody.meal-group .meal-subtotal-row td:last-child {
    border-bottom: 0;
  }

  .responsive-table
    tbody.meal-group
    .meal-subtotal-row
    td.diary-subtotal-actions {
    display: none;
  }

  .responsive-table tbody.meal-group .diary-actions {
    border-bottom: 0;
  }
}

.responsive-table th:first-child,
.responsive-table td:first-child {
  width: 8%;
  white-space: nowrap;
  overflow-wrap: normal;
}

.responsive-table th:nth-child(2),
.responsive-table td:nth-child(2) {
  width: 10%;
  white-space: nowrap;
  overflow-wrap: normal;
}

.responsive-table th:nth-child(3),
.responsive-table td.diary-food-cell {
  width: 20%;
}

.responsive-table th:not(:nth-child(3)),
.responsive-table td:not(.diary-food-cell) {
  overflow-wrap: normal;
}

/* Keep the diary date picker compact and its submit button beside it. */
#diary .diary-date-form {
  max-width: 100%;
}

#diary .diary-date-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.75rem;
}

#diary .diary-date-controls input[type="date"] {
  flex: 0 1 220px;
  width: 220px;
  max-width: 100%;
  margin: 0;
}

#diary .diary-date-controls button {
  flex: 0 0 auto;
  width: auto;
  margin: 0;
}

/* Daily totals stay within the diary table as its final two summary rows. */
.responsive-table .daily-summary th,
.responsive-table .daily-summary td {
  background: var(--color-background);
  font-weight: 700;
}

.responsive-table .daily-summary-total th,
.responsive-table .daily-summary-total td {
  border-top: 2px solid var(--color-border);
}

.responsive-table .daily-summary-progress th,
.responsive-table .daily-summary-progress td {
  border-bottom: 0;
}

@media (max-width: 1024px) {
  .responsive-table tbody.daily-summary {
    display: block;
    width: 100%;
    margin-top: 8px;
    padding: 8px 16px;
    background: var(--color-surface-muted);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-card);
  }

  .responsive-table tbody.daily-summary tr {
    display: block;
    margin: 0;
    padding: 0;
    background: transparent;
    border: 0;
    border-radius: 0;
  }

  .responsive-table tbody.daily-summary th {
    display: block;
    padding: 8px 0;
    text-align: left;
    border: 0;
    background: transparent;
  }

  .responsive-table tbody.daily-summary td {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 16px;
    padding: 8px 0;
    border-bottom: 1px solid var(--color-border);
    background: transparent;
    white-space: normal;
    text-align: right;
  }

  .responsive-table tbody.daily-summary td::before {
    content: attr(data-label);
    color: var(--color-text-muted);
    font-size: 0.85rem;
    font-weight: 600;
    text-align: left;
  }

  .responsive-table tbody.daily-summary .diary-summary-actions {
    display: none;
  }

  .checkbox > .diary-actions-inner {
    justify-content: center!important;
    margin-top:-20px;
  }

.responsive-table th:first-child,
.responsive-table td:first-child, 
.responsive-table th:nth-child(2),
.responsive-table td:nth-child(2),
.responsive-table th:nth-child(3),
.responsive-table td.diary-food-cell {
    width: 100%;
}
}

/* Viewport-fixed copy of the diary headers, controlled by diary-sticky-header.js. */
.diary-fixed-header-table {
  position: fixed;
  z-index: 20;
  margin: 0;
  border-radius: 0;
  pointer-events: none;
}

.diary-fixed-header-table th {
  background: var(--color-background);
}

/* Individually collapsible food entries on responsive diary layouts. */
.diary-entry-toggle {
  display: none;
}

.diary-food-name {
  display: inline;
}

@media (max-width: 1024px) {
  .responsive-table.diary-collapse-ready .diary-food-name {
    display: none;
  }

  .responsive-table tbody.meal-group {
    padding: 8px;
  }

  .responsive-table tbody.meal-group tr:not(.meal-subtotal-row) {
    margin-bottom: 10px;
    padding: 8px 12px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-card);
  }

  .responsive-table
    tbody.meal-group
    tr:not(.meal-subtotal-row)
    td.diary-food-cell {
    display: block;
    padding: 0;
    border-bottom: 0;
    text-align: left;
  }

  .responsive-table tbody.meal-group td.diary-food-cell::before {
    display: none;
  }

  .responsive-table.diary-collapse-ready .diary-entry-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    min-height: 44px;
    gap: 12px;
    margin: 0;
    padding: 4px 0;
    color: var(--color-heading);
    background: transparent;
    border: 0;
    border-radius: 0;
    text-align: left;
  }

  .diary-entry-toggle:hover {
    color: var(--color-heading);
    background: transparent;
    border-color: transparent;
  }

  .diary-entry-toggle-main {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    min-width: 0;
    gap: 2px;
  }

  .diary-entry-name {
    font-weight: 700;
    overflow-wrap: anywhere;
  }

  .diary-entry-context {
    color: var(--color-text-muted);
    font-size: 0.85rem;
    font-weight: 400;
  }

  .diary-entry-toggle-icon {
    flex: 0 0 9px;
    width: 9px;
    height: 9px;
    margin-right: 4px;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: rotate(45deg) translateY(-2px);
    transition: transform 150ms ease;
  }

  .diary-entry-toggle[aria-expanded="true"] .diary-entry-toggle-icon {
    transform: rotate(225deg) translate(-2px, -1px);
  }

  .responsive-table
    tbody.meal-group
    tr.diary-entry-collapsible
    td:not(.diary-food-cell) {
    display: none;
  }

  .responsive-table
    tbody.meal-group
    tr.diary-entry-collapsible.is-expanded
    td:not(.diary-food-cell) {
    display: flex;
  }

  /* Preserve the user's adjusted edit/delete button alignment. */
  .responsive-table
    tbody.meal-group
    tr.diary-entry-collapsible.is-expanded
    td.diary-actions {
    display: flex;
    justify-content: space-between;
  }

  .responsive-table
    tbody.meal-group
    tr.diary-entry-collapsible.is-expanded
    td.diary-food-cell {
    margin-bottom: 4px;
    border-bottom: 1px solid var(--color-border);
  }
}

@media (prefers-reduced-motion: reduce) {
  .diary-entry-toggle-icon {
    transition: none;
  }
}

/* Totals use the same compact disclosure pattern as individual diary entries. */
.diary-total-toggle {
  display: none;
}

@media (max-width: 1024px) {
  .responsive-table.diary-collapse-ready .diary-food-name {
    display: none;
  }

  .responsive-table.diary-collapse-ready .diary-entry-toggle,
  .responsive-table.diary-collapse-ready .diary-total-toggle {
    display: flex;
  }

  .responsive-table.diary-collapse-ready .diary-subtotal-label,
  .responsive-table.diary-collapse-ready .diary-summary-label {
    display: none;
  }

  .diary-total-toggle {
    align-items: center;
    justify-content: space-between;
    width: 100%;
    min-height: 40px;
    gap: 12px;
    margin: 0;
    padding: 4px 0;
    color: var(--color-heading);
    background: transparent;
    border: 0;
    border-radius: 0;
    text-align: left;
    font-weight: 700;
  }

  .diary-total-toggle:hover {
    color: var(--color-heading);
    background: transparent;
    border-color: transparent;
  }

  .diary-total-toggle-icon {
    flex: 0 0 8px;
    width: 8px;
    height: 8px;
    margin-right: 4px;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: rotate(45deg) translateY(-2px);
    transition: transform 150ms ease;
  }

  .diary-total-toggle[aria-expanded="true"] .diary-total-toggle-icon {
    transform: rotate(225deg) translate(-2px, -1px);
  }

  .responsive-table tbody.meal-group .meal-subtotal-row td[hidden],
  .responsive-table tbody.daily-summary td[hidden],
  .responsive-table tbody.daily-summary th[hidden] {
    display: none !important;
  }

  .responsive-table tbody.daily-summary tr.daily-summary-progress th {
    padding-top: 12px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .diary-total-toggle-icon {
    transition: none;
  }
}

/* Logged meal editing and bulk diary actions */
.diary-bulk-toolbar form,
.diary-form-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-start;
  align-items: flex-end;
  flex-direction: row;
}
.diary-bulk-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  justify-content: flex-end;
}
.diary-bulk-toolbar {
  margin: 16px 0;
}
.diary-logged-meal-name,
.diary-food-meal-name {
  display: block;
  font-size: 0.88em;
  color: var(--color-text-muted);
}
.diary-meal-edit-link {
  white-space: nowrap;
}
.diary-selection-label {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.diary-selection-label input {
  width: 18px;
  height: 18px;
}
.diary-bulk-form {
  display: grid;
  gap: 22px;
}
.diary-bulk-property {
  display: grid;
  gap: 10px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--border-color, #ddd);
}
.diary-bulk-property h3 {
  margin: 0;
}
.diary-bulk-property-options {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 24px;
}
.diary-bulk-property-options label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.diary-bulk-property-options input {
  width: 16px;
  height: 16px;
}
.diary-bulk-property [data-bulk-shared] {
  max-width: 360px;
}
.diary-bulk-property [data-bulk-shared] label,
.diary-bulk-individual-details > div {
  display: grid;
  gap: 6px;
}
.diary-bulk-ingredients {
  display: grid;
  gap: 12px;
}
.diary-bulk-ingredients > .saved-meals-heading {
  display: grid;
  gap: 4px;
}
.diary-bulk-ingredients h3,
.diary-bulk-ingredients p {
  margin: 0;
}
.diary-bulk-individual-details {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}
.diary-form-actions {
  margin-top: 4px;
}
@media (max-width: 700px) {
  .diary-bulk-toolbar {
    align-items: stretch;
    flex-direction: column;
  }
  .diary-bulk-individual-details {
    grid-template-columns: 1fr;
  }
}

/* Logged meals are deleted directly; only standalone entries have bulk-edit checkboxes. */
.diary-actions-inner .diary-delete-meal {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-small);
  background: transparent;
  cursor: pointer;
}

.diary-actions-inner .diary-delete-meal img {
  display: block;
  width: 22px;
  height: 22px;
}

.diary-actions-inner .diary-delete-meal:hover {
  background: var(--color-surface-muted);
}

.diary-actions-inner .diary-delete-meal:focus-visible {
  outline: 2px solid var(--color-focus);
  outline-offset: 2px;
}

/* Fix flex layout for header and anchor link. */
.diary-header-flex {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.diary-header-flex h2 {
  margin-top: 0;
}


/* Daily totals preference */
.diary-display-preference {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  gap: 0.5rem;
  margin: 8px 0 12px;
}

.diary-display-preference .checkbox-label {
  display: inline-flex;
  align-items: center;
  justify-content: flex-start;
  gap: 0.5rem;
  width: auto;
  margin: 0;
  white-space: nowrap;
}
/* Hide only the two daily summary rows when the preference is off. */
.responsive-table tbody.daily-summary.is-preference-hidden {
  display: none;
}
