:root {
  --primary-color: #3b82f6;
  --bg-color: #f2f4f7;
  --border-color: #d0d6df;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg-color);
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 12px;
  background: #e5e9ef;
  border-bottom: 1px solid var(--border-color);
  flex-wrap: wrap;
  position: relative;
}

@media (max-width: 768px) {
  .top-bar {
    flex-direction: column;
    align-items: stretch;
  }
}

.top-bar button {
  padding: 8px 14px;
  font-size: 18px;
  border-radius: 8px;
  border: 1px solid #c5cbd6;
  background: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  white-space: nowrap;
}

.top-bar button:hover {
  background: #f0f2f6;
}

.top-bar button:active {
  transform: scale(0.95);
}

.datetime-container {
  position: relative;
  display: inline-block;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}

@media (max-width: 768px) {
  .datetime-container {
    width: 100%;
  }
}

.dropdowns-container {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}

.radio-containers-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.date-selects-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.time-button-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.time-button-row button {
  padding: 8px 14px;
  font-size: 18px;
  border-radius: 8px;
  border: 1px solid #c5cbd6;
  background: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  white-space: nowrap;
}

.time-button-row button:hover {
  background: #f0f2f6;
}

.time-button-row button:active {
  transform: scale(0.95);
}

@media (max-width: 768px) {
  .dropdowns-container {
    flex-direction: column;
    width: 100%;
    align-items: stretch;
    gap: 12px;
  }

  /* First row: gender and calendar radio containers side by side */
  .radio-containers-row {
    display: flex;
    gap: 8px;
    width: 100%;
  }

  .gender-radio-container,
  .calendar-radio-container {
    flex: 1;
    justify-content: center;
    padding: 8px;
    box-sizing: border-box;
  }

  .gender-radio-label {
    flex-shrink: 0;
  }

  /* Second row: year, month, date */
  .date-selects-row {
    display: flex;
    gap: 8px;
    width: 100%;
  }

  .date-selects-row .datetime-select {
    flex: 1;
  }

  /* Third row: time and button */
  .time-button-row {
    display: flex;
    gap: 8px;
    width: 100%;
    align-items: stretch;
  }

  #hourSelect {
    flex: 1;
    min-width: 0;
    /* Allow flex shrinking */
  }

  .time-button-row #copyBtn {
    flex-shrink: 0;
    white-space: nowrap;
  }
}

.gender-radio-container,
.calendar-radio-container {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 8px 12px;
  background: #fff;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  box-sizing: border-box;
}

.gender-radio-label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-size: 16px;
  user-select: none;
}

.gender-radio-label input[type="radio"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
  accent-color: var(--primary-color);
}

.gender-radio-label span {
  color: #1f2937;
  font-weight: 500;
}

.datetime-select {
  padding: 8px 12px;
  font-size: 16px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background: #fff;
  cursor: pointer;
  transition: all 0.2s;
  min-width: 80px;
}

#hourSelect {
  min-width: 180px;
}

.datetime-select:hover {
  border-color: var(--primary-color);
  background: #f0f2f6;
}

.datetime-select:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.content-container {
  display: flex;
  gap: 20px;
  margin: 24px auto;
  width: 90%;
  max-width: 1400px;
  padding: 0 16px;
  box-sizing: border-box;
  align-items: flex-start;
}

.content {
  flex: 1;
  padding: 20px 16px;
  text-align: center;
  color: #99a2aa;
  border: 2px dashed var(--border-color);
  border-radius: 16px;
  background: #fff;
  box-sizing: border-box;
}

.content-bazi {
  min-width: 0;
  /* Allow flex shrinking */
}

.content-dayun {
  min-width: 0;
  /* Allow flex shrinking */
}

.content-title {
  margin: 0 0 20px 0;
  font-size: 20px;
  font-weight: 600;
  color: #1f2937;
  text-align: center;
}

@media (max-width: 768px) {
  .content-container {
    flex-direction: column;
    width: 100%;
    margin: 16px 0;
    padding: 0 12px;
    gap: 16px;
  }

  .content {
    width: 100%;
    margin: 0;
    padding: 16px 12px;
  }
}

.bazi-table {
  width: 100%;
  border-collapse: collapse;
  margin: 0 auto;
  font-size: 16px;
  background: #fff;
  table-layout: fixed;
}

.bazi-table th,
.bazi-table td {
  border: 1px solid var(--border-color);
  padding: 12px 8px;
  text-align: center;
  vertical-align: middle;
}

.bazi-table thead th {
  background: #f8f9fa;
  font-weight: 600;
  color: #1f2937;
  padding: 0 8px;
  height: 64px;
  box-sizing: border-box;
  vertical-align: middle;
}

.bazi-table thead th:first-child {
  width: 80px;
}

.bazi-table thead th:not(:first-child) {
  width: calc((100% - 80px) / 4);
}

.bazi-table tbody td {
  width: calc((100% - 80px) / 4);
}

.bazi-table tbody th {
  background: #f8f9fa;
  font-weight: 600;
  color: #1f2937;
  text-align: center;
  width: 80px;
}

.bazi-table .corner-cell {
  background: #fff;
  border: none;
  width: 80px;
}

.bazi-cell {
  color: #1f2937;
  min-height: 40px;
  line-height: 1.5;
  text-align: center;
  position: relative;
}

.day-master-frame {
  outline: 2.5px solid var(--dm-frame-color);
  outline-offset: -2.5px;
  z-index: 1;
}

.bazi-cell .bazi-char {
  display: inline-block;
  text-align: center;
}

.bazi-cell .element-circle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  position: absolute;
  left: calc(50% + 1em);
  top: 50%;
  transform: translateY(-50%);
}

.bazi-cell .element-text {
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  line-height: 1;
}

.bazi-cell .element-fire {
  background-color: #ef4444;
}

.bazi-cell .element-water {
  background-color: #3b82f6;
}

.bazi-cell .element-wood {
  background-color: #22c55e;
}

.bazi-cell .element-earth {
  background-color: #78716c;
}

.bazi-cell .element-metal {
  background-color: #f59e0b;
}

@media (max-width: 768px) {
  .bazi-table {
    font-size: 14px;
    table-layout: fixed;
    /* ensure equal distribution works */
  }

  .bazi-table th,
  .bazi-table td {
    padding: 8px 4px;
  }

  /* Force all 5 columns to equal width on mobile, overriding desktop first-column width */
  .bazi-table thead th:first-child,
  .bazi-table .corner-cell,
  .bazi-table tbody th {
    width: calc(100% / 5) !important;
  }

  .bazi-table thead th:not(:first-child),
  .bazi-table tbody td {
    width: calc(100% / 5) !important;
  }

  /* Avoid absolute element overlapping due to tighter columns */
  .bazi-cell .element-circle {
    width: 18px;
    height: 18px;
    left: auto;
    right: 6px;
    /* pin to right for consistency on small width */
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
  }

  .bazi-cell .element-text {
    font-size: 11px;
  }
}

.dayun-table {
  width: 100%;
  border-collapse: collapse;
  margin: 0 auto;
  font-size: 16px;
  background: #fff;
  table-layout: fixed;
}

.dayun-table thead th {
  width: 100%;
}

.dayun-table tbody td {
  width: calc(100% / 7);
}

.dayun-table th,
.dayun-table td {
  border: 1px solid var(--border-color);
  padding: 12px 8px;
  text-align: center;
  vertical-align: middle;
}

.dayun-table .dayun-header {
  background: #f8f9fa;
  font-weight: 600;
  color: #1f2937;
  padding: 0;
  text-align: left;
}

/* Renamed to force cache update */
.dayun-header-grid-wrapper {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  width: 100%;
  width: 100%;
  padding: 12px 0;
  /* box-sizing: content-box is default */
  align-items: center;
}

.dayun-header-line:nth-child(1) {
  grid-column: 1 / 5;
  padding-left: 8px;
}

.dayun-header-line:nth-child(2) {
  grid-column: 5 / 8;
  padding-left: 8px;
}

.dayun-header-line {
  line-height: 1.5;
  min-height: 40px;
  display: flex;
  align-items: center;
  text-align: left;
  font-size: 16px;
  font-weight: 600;
}

/* Differentiate font sizes for 起运 和 交运 values */
.dayun-header-line:nth-child(1) span,
.dayun-header-line:nth-child(2) span {
  font-size: 16px;
  /* Explicitly set to match the line */
  font-weight: 600;
  /* Bold the values slightly to differentiate from labels */
  display: inline-block;
}

.dayun-cell {
  color: #1f2937;
  min-height: 40px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}

@media (max-width: 768px) {
  .dayun-table {
    font-size: 14px;
  }

  .dayun-table th,
  .dayun-table td {
    padding: 8px 4px;
  }

  .dayun-table .dayun-header {
    padding: 12px 4px;
  }

  .dayun-header-grid-wrapper {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
    font-size: 13px;
    padding: 0;
  }
}

/* Bottom Actions Container */
#bottom-actions {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 16px;
  padding: 8px 16px;
  background: white;
  border-radius: 50px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  width: calc(100% - 64px);
  max-width: 380px;
}

.bottom-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 0;
  font-size: 16px;
  font-weight: 600;
  border: none;
  border-radius: 40px;
  background: #3b82f6;
  color: white;
  cursor: pointer;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
  white-space: nowrap;
}

.bottom-btn:active {
  transform: scale(0.95);
  background: #2563eb;
}

/* Modal Styling */
.modal {
  display: none;
  position: fixed;
  z-index: 2000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.4);
}

.modal-content {
  position: fixed;
  /* Changed from absolute for better stability */
  bottom: 0;
  left: 0;
  width: 100%;
  background: white;
  border-radius: 20px 20px 0 0;
  padding: 20px 0;
  box-sizing: border-box;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.2);
  z-index: 2001;
}

.slide-up {
  animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
  from {
    transform: translateY(100%);
  }

  to {
    transform: translateY(0);
  }
}

@keyframes slideDown {
  from {
    transform: translateY(0);
  }

  to {
    transform: translateY(100%);
  }
}

.slide-down {
  animation: slideDown 0.3s ease-out forwards;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  padding: 0 24px 16px 24px;
  flex-shrink: 0;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  color: #1f2937;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #9ca3af;
}

.history-controls {
  display: flex;
  gap: 12px;
  padding: 0 24px 12px 24px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  margin-bottom: 8px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.history-controls .filter-group,
.history-controls .sort-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.history-controls label {
  font-size: 14px;
  font-weight: 500;
  color: #6b7280;
  white-space: nowrap;
}

.history-select {
  padding: 6px 10px;
  font-size: 14px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background: #fdfdfd;
  cursor: pointer;
  outline: none;
  min-width: 90px;
  color: #1f2937;
}

.history-select:focus {
  border-color: var(--primary-color);
}

/* Swipe-to-Action History List */
.history-list {
  display: flex;
  flex-direction: column;
  gap: 0px;
  overflow-x: hidden;
  overflow-y: auto;
  flex: 1;
  padding: 0 0px 20px 0px;
  -webkit-overflow-scrolling: touch;
}

.history-item-wrapper {
  position: relative;
  overflow: hidden;
  background: white;
  flex-shrink: 0;
}

.history-item {
  position: relative;
  z-index: 2;
  padding: 15px 24px;
  background: white;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: transform 0.2s cubic-bezier(0.18, 0.89, 0.32, 1.28);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  box-sizing: border-box;
  min-height: 70px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.history-item:active {
  background: #f8f8f8;
}

.history-item-name {
  font-weight: 600;
  font-size: 16px;
  color: #1f2937;
  margin-bottom: 4px;
}

.history-item-date {
  font-size: 14px;
  color: #6b7280;
}

/* Swipe Actions */
.swipe-actions {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  z-index: 1;
}

.action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 60px;
  height: 100%;
  font-size: 20px;
  cursor: pointer;
  transition: transform 0.2s;
  border: none;
  background: transparent;
}

.action-btn:active {
  transform: scale(0.9);
}

.edit-action {
  background: #3b82f6;
}

.delete-action {
  background: #ef4444;
}



/* Body padding to accommodate fixed bottom bar */
body {
  padding-bottom: 100px !important;
}

.desktop-btn {
  padding: 8px 16px;
  font-size: 15px;
  font-weight: 500;
  border-radius: 8px;
  border: 1px solid #c5cbd6;
  background: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.2s;
  color: #1f2937;
}

.desktop-btn:hover {
  background: #f0f2f6;
  border-color: #3b82f6;
}

.desktop-btn:active {
  transform: scale(0.95);
}

@media (max-width: 1024px) {
  .desktop-actions {
    position: static;
    transform: none;
    order: -1;
    width: 100%;
    margin-bottom: 12px;
    justify-content: center;
  }
}

@media (max-width: 768px) {
  .desktop-actions {
    display: none !important;
  }
}

@media (display-mode: standalone) {
  .desktop-actions {
    display: none !important;
  }
}

body.is-standalone .desktop-actions {
  display: none !important;
}