/*
 * Additions layered on top of styles.css: danger buttons + confirmation
 * modal, the phone tab bar and touch-friendly layout, saved schedules,
 * per-month schedule tables, and the public view page controls.
 */

:root {
  --danger: #b3261e;
  --danger-soft: #fdecea;
}

body.modal-open {
  overflow: hidden;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

button.danger {
  background: var(--danger);
  color: #fff;
}

button.danger-ghost {
  background: transparent;
  color: var(--danger);
  border: 1px solid rgba(179, 38, 30, 0.5);
  box-shadow: none;
}

.button-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 18px;
  border-radius: 999px;
  border: 1px solid rgba(28, 27, 26, 0.2);
  color: var(--ink);
  font-weight: 600;
  text-decoration: none;
}

.lede a,
.view-footer a {
  color: var(--accent-2);
  font-weight: 600;
}

/* ---------------- Confirmation modal ---------------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(20, 12, 10, 0.62);
  overflow-y: auto;
}

.modal {
  width: 100%;
  max-width: 460px;
  max-height: 100%;
  overflow-y: auto;
  background: #fff;
  border-radius: 18px;
  padding: 22px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.4);
}

.modal h2 {
  margin: 0 0 8px;
  font-size: 1.35rem;
}

.modal-danger {
  border: 3px solid var(--danger);
  padding-top: 0;
}

.modal-banner {
  margin: 0 -22px 16px;
  padding: 10px 22px;
  background: var(--danger);
  color: #fff;
  font-weight: 700;
  font-size: 0.85rem;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}

.modal-message {
  margin: 0 0 12px;
  color: var(--ink);
  line-height: 1.45;
}

.modal-details {
  margin: 0 0 14px;
  padding: 12px 14px 12px 30px;
  background: var(--danger-soft);
  border-radius: 12px;
  font-size: 0.9rem;
  line-height: 1.5;
}

.modal-check {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 0 0 12px;
  font-size: 0.95rem;
  line-height: 1.35;
}

.modal-check input {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  margin: 0;
}

.modal-ack {
  font-weight: 600;
}

.modal-type {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0 0 14px;
  font-size: 0.95rem;
}

.modal-type input {
  padding: 12px;
  border-radius: 12px;
  border: 2px solid var(--danger);
  font-size: 16px;
  font-family: inherit;
  text-transform: uppercase;
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 6px;
}

.modal-actions button {
  min-height: 48px;
  width: 100%;
}

@media (min-width: 721px) {
  .modal-actions {
    flex-direction: row-reverse;
  }

  .modal-actions button {
    width: auto;
    flex: 1;
  }
}

/* ---------------- Saved schedules ---------------- */
.tab-bar {
  display: none;
}

#snapshots-panel {
  grid-column: 1 / -1;
}

.snapshot-row {
  grid-template-columns: minmax(0, 1fr) auto;
}

.snapshot-row strong {
  overflow-wrap: anywhere;
}

.snapshot-row .row-actions {
  align-items: center;
}

.auto-backups {
  margin-top: 18px;
}

.auto-backups summary {
  cursor: pointer;
  font-weight: 600;
  padding: 8px 0;
}

/* ---------------- Multi-month schedule ---------------- */
.month-section + .month-section {
  margin-top: 22px;
}

.month-heading {
  margin: 0 0 8px;
  font-family: 'Fraunces', serif;
  font-size: 1.15rem;
}

#schedule-grid,
#view-schedule-grid {
  overflow: visible;
}

/* ---------------- Public view page ---------------- */
.view-picker {
  display: none;
  flex-direction: column;
  gap: 4px;
  font-size: 0.8rem;
  color: var(--ink-soft);
}

.view-picker.is-visible {
  display: flex;
}

.view-picker select {
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid rgba(28, 27, 26, 0.18);
  font-family: inherit;
  font-size: 1rem;
  max-width: 100%;
}

.view-hero .hero-actions {
  align-items: flex-end;
}

.view-footer {
  margin-top: 28px;
  text-align: center;
  font-size: 0.85rem;
}

/* ---------------- Phone layout ---------------- */
@media (max-width: 720px) {
  body {
    min-height: 100dvh;
  }

  .page {
    padding: 12px 12px 72px;
  }

  .hero {
    padding: 16px;
    gap: 12px;
    border-radius: 18px;
  }

  .hero h1 {
    font-size: 1.6rem;
    margin-bottom: 4px;
  }

  .kicker {
    margin-bottom: 4px;
  }

  .hero .lede {
    font-size: 0.9rem;
  }

  .hero-actions {
    gap: 8px;
    width: 100%;
  }

  .hero-actions > button,
  .hero-actions > .file-button {
    flex: 1 1 auto;
    justify-content: center;
  }

  .view-hero .hero-actions > * {
    flex: 1 1 100%;
  }

  .view-picker select {
    width: 100%;
  }

  /* Touch-friendly controls. 16px text stops iOS zooming into fields. */
  button,
  .file-button,
  .button-link {
    min-height: 44px;
  }

  input[type='text'],
  input[type='date'],
  input[type='month'],
  input[type='number'],
  input[type='password'],
  select {
    font-size: 16px;
    min-height: 44px;
  }

  input[type='checkbox'] {
    width: 22px;
    height: 22px;
  }

  .info-button {
    width: 36px;
    height: 36px;
  }

  /* One section at a time, chosen from a sticky tab strip. */
  .tab-bar {
    display: flex;
    gap: 6px;
    position: sticky;
    top: 0;
    z-index: 50;
    margin: 10px -12px 0;
    padding: 8px 12px;
    overflow-x: auto;
    background: rgba(253, 252, 251, 0.96);
    backdrop-filter: blur(8px);
    border-bottom: var(--border);
    scrollbar-width: none;
  }

  .tab-bar::-webkit-scrollbar {
    display: none;
  }

  .tab-bar button {
    flex: 0 0 auto;
    min-height: 40px;
    padding: 8px 14px;
    background: #fff;
    box-shadow: none;
    border: 1px solid rgba(28, 27, 26, 0.15);
    font-size: 0.9rem;
  }

  .tab-bar button.is-active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
  }

  .layout {
    margin-top: 12px;
    gap: 0;
    /* minmax(0, …) stops a wide table from stretching the whole page. */
    grid-template-columns: minmax(0, 1fr);
  }

  .layout > .panel {
    min-width: 0;
  }

  .layout > .panel {
    display: none;
  }

  .layout > .panel.is-active {
    display: block;
  }

  .panel {
    padding: 14px;
    border-radius: 14px;
  }

  .panel-header {
    gap: 10px;
  }

  .scheduler-actions {
    width: 100%;
  }

  .scheduler-actions button {
    flex: 1 1 calc(50% - 8px);
  }

  .scheduler-actions #generate-schedule-btn {
    flex-basis: 100%;
  }

  /* The desktop rules use id selectors, so repeat them to stack rows. */
  .row,
  #people-panel .row,
  .role-row,
  .couple-row,
  .snapshot-row {
    grid-template-columns: minmax(0, 1fr);
    gap: 8px;
  }

  .snapshot-row .row-actions {
    justify-content: flex-start;
  }

  .snapshot-row .row-actions > * {
    flex: 1 1 auto;
  }

  .person-name {
    min-width: 0;
  }

  .scroll-list {
    max-height: none;
    overflow: visible;
    padding-right: 0;
    scrollbar-gutter: auto;
  }

  .inline-form > * {
    flex: 1 1 100%;
  }

  .inline-form .switch,
  .inline-form .number-field,
  .inline-form .color-swatch {
    flex: 0 0 auto;
  }

  .calendar-controls > label,
  .calendar-controls > button {
    flex: 1 1 100%;
  }

  .calendar-controls input,
  .calendar-controls select {
    width: 100%;
  }

  .unavailability-item {
    min-height: 44px;
  }

  #eligibility-panel .table-wrap {
    max-height: 70vh;
  }

  /* Compact schedule grid: narrow sticky role column, scroll for the dates. */
  .table-wrap th,
  .table-wrap td {
    padding: 6px;
  }

  .table-wrap th:first-child,
  .table-wrap td:first-child {
    min-width: 92px;
    max-width: 110px;
    font-size: 0.8rem;
  }

  .schedule-table th:not(:first-child),
  .schedule-table td:not(:first-child) {
    min-width: 128px;
  }

  .schedule-table select {
    font-size: 16px;
    min-height: 40px;
  }

  .save-status {
    right: 12px;
    left: 12px;
    bottom: calc(12px + env(safe-area-inset-bottom));
    text-align: center;
  }

  .help-popover {
    max-width: calc(100vw - 24px);
  }

  .month-heading {
    font-size: 1.05rem;
  }
}

/* ---------------- Printing ---------------- */
@media print {
  .tab-bar,
  #snapshots-panel,
  .view-actions,
  .view-footer,
  .modal-overlay {
    display: none !important;
  }

  /* Phones hide inactive panels; printing must still show the schedule. */
  #scheduler-panel {
    display: block !important;
  }

  @page {
    size: landscape;
    margin: 8mm;
  }

  .view-page .hero {
    display: block !important;
    padding: 0 0 2mm;
    margin: 0;
    border: none;
    box-shadow: none;
    background: none;
  }

  .view-page .hero::after {
    display: none;
  }

  .view-page .hero h1 {
    font-size: 14pt;
    margin: 0;
  }

  .view-page .hero .kicker {
    display: none;
  }

  .view-page .hero .lede {
    font-size: 9pt;
  }

  .layout {
    margin-top: 0;
    animation: none;
  }

  #schedule-grid,
  #view-schedule-grid {
    transform: none;
  }

  /* Each month is kept whole (never split across pages); short months share a page, so a one-month
     schedule always fits on a single page. */
  .month-section {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  .month-section + .month-section {
    margin-top: 6mm;
  }

  .month-heading {
    font-size: 11pt;
    margin: 0 0 1.5mm;
  }

  /* Tighter cells so about 20 rows fit on one landscape page. */
  .schedule-table th,
  .schedule-table td {
    font-size: 7.5pt;
    line-height: 1.15;
    padding: 2px 4px;
    border-width: 1pt;
  }

  .schedule-table .cell-stack {
    gap: 1px;
  }

  /* The editor's cells are dropdowns: print them as plain compact text. */
  .schedule-table select {
    font-size: 7.5pt !important;
    line-height: 1.15;
    height: auto;
    min-height: 0;
    padding: 0;
    margin: 0;
    border: none;
    background: transparent;
    appearance: none;
    -webkit-appearance: none;
    text-align: center;
    text-align-last: center;
  }

  /* On-screen hints and conflict highlighting don't belong on paper. */
  .cell-meta {
    display: none !important;
  }

  .schedule-table td[class*='conflict-'] {
    box-shadow: none;
    outline: none;
  }

  .department-row td {
    font-size: 6.5pt;
    padding: 1px 4px;
  }

  .schedule-table th:not(:first-child),
  .schedule-table td:not(:first-child) {
    min-width: 0;
  }

  .table-wrap th:first-child,
  .table-wrap td:first-child {
    max-width: none;
  }
}

/* ---------------- Departments + groups ---------------- */
.scope-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin: 0 0 8px;
}

.scope-bar label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.8rem;
  color: var(--ink-soft);
}

.scope-bar select,
.modal-type select {
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid rgba(28, 27, 26, 0.25);
  font-family: inherit;
  font-size: 1rem;
  max-width: 100%;
  background: #fff;
}

#scope-note {
  margin: 0 0 14px;
}

.print-title {
  display: none;
}

/* Group cells: the members are the main content, the group name a small label. */
.cell-group-name {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  color: var(--ink-soft);
  line-height: 1.2;
}

.cell-members {
  margin-top: 3px;
  font-size: inherit;
  color: var(--ink);
  line-height: 1.3;
}

.cell-away {
  color: var(--danger);
  font-weight: 600;
}

.group-card {
  padding: 12px;
  border-radius: 14px;
  background: var(--paper);
  border: 1px solid rgba(28, 27, 26, 0.08);
}

.group-head {
  display: flex;
  gap: 10px;
  align-items: center;
}

.group-head .group-name {
  flex: 1 1 auto;
  min-width: 0;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid rgba(28, 27, 26, 0.2);
  background: #fff;
  font-family: inherit;
  font-weight: 600;
}

.group-summary {
  margin: 8px 0;
  overflow-wrap: anywhere;
}

.group-summary.is-warning {
  color: var(--danger);
}

.group-members summary {
  cursor: pointer;
  font-weight: 600;
  padding: 6px 0;
}

.member-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 4px 12px;
  max-height: 260px;
  overflow-y: auto;
  padding: 8px 4px;
}

.member-option {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.9rem;
  min-height: 32px;
}

@media (max-width: 720px) {
  .scope-bar label {
    flex: 1 1 100%;
  }

  .scope-bar select {
    width: 100%;
  }

  .member-grid {
    grid-template-columns: 1fr;
    max-height: 50vh;
  }

  .member-option {
    min-height: 44px;
  }
}

@media print {
  .print-title {
    display: block;
    margin: 0 0 2mm;
    font-size: 14pt;
  }

  .scope-bar,
  #scope-note {
    display: none !important;
  }

  .cell-group-name {
    font-size: 6pt;
    letter-spacing: 0.2px;
    color: #333;
  }

  .cell-members {
    font-size: 7.5pt;
    margin-top: 0;
    color: #000;
  }
}

/* ---------------- Users and sign-in ---------------- */
.whoami {
  margin: 6px 0 0;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.modal-error {
  min-height: 1.2em;
  margin: 0 0 8px;
  color: var(--danger);
  font-size: 0.9rem;
}

.row.is-readonly {
  opacity: 0.6;
}

.user-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 560px;
}

.user-form input,
.user-form select,
.user-edit-form input,
.user-edit-form select {
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid rgba(28, 27, 26, 0.18);
  font-family: inherit;
  font-size: 1rem;
  background: #fff;
}

.dept-picks {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 4px 12px;
  margin: 0;
  padding: 8px 12px;
  border: 1px solid rgba(28, 27, 26, 0.18);
  border-radius: 12px;
  background: #fff;
}

.dept-picks legend {
  padding: 0 6px;
  font-size: 0.8rem;
  color: var(--ink-soft);
}

.user-card .group-head {
  justify-content: space-between;
}

.user-edit-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 8px 0 4px;
  max-width: 560px;
}

.role-badge {
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  background: rgba(28, 138, 122, 0.14);
  color: var(--accent-2);
}

.role-badge.role-admin {
  background: rgba(232, 107, 60, 0.16);
  color: #a8431d;
}

@media (max-width: 720px) {
  .user-form,
  .user-edit-form {
    max-width: none;
  }
}
